0027961: Visualization - remove unused and no more working OpenGl_AVIWriter
[occt.git] / src / Interface / Interface_Graph.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Interface_BitMap.hxx>
16 #include <Interface_EntityIterator.hxx>
17 #include <Interface_GeneralLib.hxx>
18 #include <Interface_GeneralModule.hxx>
19 #include <Interface_Graph.hxx>
20 #include <Interface_GTool.hxx>
21 #include <Interface_InterfaceModel.hxx>
22 #include <Interface_Protocol.hxx>
23 #include <Interface_ReportEntity.hxx>
24 #include <Interface_ShareTool.hxx>
25 #include <Standard_DomainError.hxx>
26 #include <Standard_Transient.hxx>
27 #include <TCollection_HAsciiString.hxx>
28 #include <TColStd_Array1OfInteger.hxx>
29 #include <TColStd_HSequenceOfTransient.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
31
32 // Flags : 0 = Presence, 1 = Sharing Error
33 #define Graph_Present 0
34 #define Graph_ShareError 1
35
36
37 //  ###########################################################################
38
39 //  ....                           CONSTRUCTEURS                           ....
40
41 //  ....       Construction a partir de la connaissance des Entites        ....
42
43
44 Interface_Graph::Interface_Graph
45 (const Handle(Interface_InterfaceModel)& amodel,
46  const Interface_GeneralLib& /*lib*/,
47  Standard_Boolean theModeStat)
48  : themodel   (amodel), thepresents ("") 
49 {
50   if(theModeStat)
51     InitStats();
52   Evaluate();
53 }
54
55 Interface_Graph::Interface_Graph
56 (const Handle(Interface_InterfaceModel)& amodel,
57  const Handle(Interface_Protocol)& /*protocol*/,
58  Standard_Boolean theModeStat)
59  : themodel   (amodel) , thepresents ("")    
60
61 {
62  if(theModeStat)
63     InitStats();  
64   Evaluate();
65 }
66
67 Interface_Graph::Interface_Graph
68 (const Handle(Interface_InterfaceModel)& amodel,
69  const Handle(Interface_GTool)& /*gtool*/,
70  Standard_Boolean theModeStat)
71  : themodel   (amodel) , thepresents ("") 
72 {
73   if(theModeStat)
74     InitStats();
75   Evaluate();
76 }
77
78 Interface_Graph::Interface_Graph
79 (const Handle(Interface_InterfaceModel)& amodel,
80  Standard_Boolean theModeStat)
81 : themodel   (amodel) , thepresents ("")  
82 {
83   if(theModeStat)
84     InitStats();
85   Evaluate ();
86 }
87
88 //  ....                Construction depuis un autre Graph                ....
89
90 Interface_Graph::Interface_Graph
91 (const Interface_Graph& agraph, const Standard_Boolean /*copied*/)
92 : themodel   (agraph.Model()), thepresents ("") 
93 {
94   thesharings = agraph.SharingTable();
95   Standard_Integer nb = agraph.NbStatuses();
96   if(!nb)
97     return;
98   if(thestats.IsNull())
99     thestats = new TColStd_HArray1OfInteger(1,nb);
100   for (Standard_Integer i = 1; i <= nb; i ++)
101     thestats->SetValue (i,agraph.Status(i));
102   theflags.Initialize(agraph.BitMap(),Standard_True);
103 }
104
105 void Interface_Graph::InitStats()
106 {
107   thestats = new TColStd_HArray1OfInteger(1,themodel->NbEntities()) , 
108     theflags.Initialize(themodel->NbEntities(),2);
109   theflags.AddFlag ("ShareError");
110 }
111
112 Standard_Integer Interface_Graph::NbStatuses() const
113 {
114   return (thestats.IsNull() ? 0 : thestats->Length());
115 }
116
117 const Handle(TColStd_HArray1OfListOfInteger)& Interface_Graph::SharingTable () const
118 {  return thesharings;  }
119
120 void Interface_Graph::Evaluate()
121 {
122   //  Evaluation d un Graphe de dependances : sur chaque Entite, on prend sa
123   //  liste "Shared". On en deduit les "Sharing"  directement
124   Standard_Integer n = Size();
125   thesharings = new TColStd_HArray1OfListOfInteger(1,n);//TColStd_HArray1OfTransient(1,n);//Clear();
126   if(themodel->GTool().IsNull())
127     return;
128   
129
130   Standard_Integer i; // svv Jan11 2000 : porting on DEC
131   for (i = 1; i <= n; i ++) {
132      //    ATTENTION : Si Entite non chargee donc illisible, basculer sur son
133     //    "Contenu" equivalent
134     Handle(Standard_Transient) ent = themodel->Value(i);
135   
136
137     //    Resultat obtenu via GeneralLib
138     Interface_EntityIterator iter = GetShareds(ent);
139
140     //    Mise en forme : liste d entiers
141     for (iter.Start(); iter.More(); iter.Next()) {
142       //    num = 0 -> on sort du Model de depart, le noter "Error" et passer
143       Handle(Standard_Transient) entshare = iter.Value();
144       if(entshare == ent)
145         continue;
146
147       Standard_Integer num = EntityNumber(entshare);
148
149       if (!num )  
150       {
151         if(!thestats.IsNull())
152           theflags.SetTrue (i,Graph_ShareError);
153         continue;
154       }
155       thesharings->ChangeValue(num).Append(i);
156     }
157   }
158 }
159
160 //  ....                Construction depuis un autre Graph                ....
161
162
163 //  ###########################################################################
164
165 //  ....                ACCES UNITAIRES AUX DONNEES DE BASE                ....
166
167 void  Interface_Graph::Reset ()
168 {
169   if(!thestats.IsNull())
170   {
171     thestats.Nullify();
172     theflags.Init (Standard_False, Graph_Present);
173   }
174
175 }
176
177 void  Interface_Graph::ResetStatus ()
178
179   if(!thestats.IsNull())
180   {
181     thestats->Init(0);  
182     theflags.Init (Standard_False, Graph_Present);
183   }
184 }
185
186
187 Standard_Integer  Interface_Graph::Size () const 
188 {  return themodel->NbEntities(); }//thestats.Upper();  }
189
190 Standard_Integer  Interface_Graph::EntityNumber
191 (const Handle(Standard_Transient)& ent) const 
192 {  return themodel->Number(ent);  }
193
194 Standard_Boolean  Interface_Graph::IsPresent
195 (const Standard_Integer num) const 
196 {
197   if (num <= 0 || num > Size()) 
198     return Standard_False;
199   return (!thestats.IsNull() ? theflags.Value (num,Graph_Present) : Standard_False);
200 }
201
202 Standard_Boolean  Interface_Graph::IsPresent
203 (const Handle(Standard_Transient)& ent) const 
204 {  return IsPresent(EntityNumber(ent));  }
205
206 const Handle(Standard_Transient)&  Interface_Graph::Entity
207 (const Standard_Integer num) const 
208 {  
209   return themodel->Value(num);  
210 }
211
212
213 Standard_Integer  Interface_Graph::Status (const Standard_Integer num) const
214 {  
215   return (!thestats.IsNull() ? thestats->Value(num) : 0);  
216 }
217
218 void  Interface_Graph::SetStatus
219 (const Standard_Integer num, const Standard_Integer stat)
220 {  
221   if(!thestats.IsNull())
222     thestats->SetValue(num,stat);  
223 }
224
225 void  Interface_Graph::RemoveItem(const Standard_Integer num)
226 {
227   if(!thestats.IsNull())
228   {
229     thestats->SetValue(num,0);  
230     theflags.SetFalse (num,Graph_Present);
231   }
232 }
233
234 void  Interface_Graph::ChangeStatus
235 (const Standard_Integer oldstat, const Standard_Integer newstat)
236 {
237   if(thestats.IsNull())
238     return;
239   Standard_Integer nb = thestats->Upper();
240   for (Standard_Integer i = 1; i <= nb; i ++) {
241     if (thestats->Value(i) == oldstat) 
242       thestats->SetValue(i,newstat);
243   }
244 }
245
246 void  Interface_Graph::RemoveStatus(const Standard_Integer stat)
247 {
248   if(thestats.IsNull())
249     return;
250   Standard_Integer nb = thestats->Upper();
251   for (Standard_Integer i = 1; i <= nb; i ++) {
252     if (thestats->Value(i) == stat) RemoveItem(i);
253   }
254 }
255
256 const Interface_BitMap&  Interface_Graph::BitMap () const
257 {  return theflags;  }
258
259 Interface_BitMap&  Interface_Graph::CBitMap ()
260 {  return theflags;  }
261
262 //  ###########################################################################
263
264 //  ....      Chargements Elementaires avec Propagation de "Share"      .... //
265
266 const Handle(Interface_InterfaceModel)&  Interface_Graph::Model() const 
267 {  return themodel;  }
268
269 void  Interface_Graph::GetFromModel ()
270 {
271   if (themodel.IsNull() || thestats.IsNull()) 
272     return;    // no model ... (-> on n ira pas loin)
273   theflags.Init (Standard_True,Graph_Present);
274   thestats->Init (0);
275 }
276
277 void  Interface_Graph::GetFromEntity
278 (const Handle(Standard_Transient)& ent, const Standard_Boolean shared,
279  const Standard_Integer newstat)
280 {
281   if(thestats.IsNull())
282     return;
283   Standard_Integer num = EntityNumber(ent);
284   if (!num ) 
285     return;
286   if (theflags.CTrue(num,Graph_Present)) return;  // deja pris : on passe
287   thestats->SetValue(num,newstat);
288   if (!shared) return;
289   //  Attention a la redefinition !
290   Interface_EntityIterator aIter = GetShareds(ent);
291
292   for ( ; aIter.More() ; aIter.Next())    
293     GetFromEntity(aIter.Value(),Standard_True,newstat);
294 }
295
296 void  Interface_Graph::GetFromEntity
297 (const Handle(Standard_Transient)& ent, const Standard_Boolean shared,
298  const Standard_Integer newstat, const Standard_Integer overlapstat,
299  const Standard_Boolean cumul)
300 {
301   if(thestats.IsNull())
302     return;
303   Standard_Integer num   = EntityNumber(ent);
304   if (!num ) return;
305   Standard_Boolean pasla = !theflags.CTrue (num,Graph_Present);
306   Standard_Integer stat  = thestats->Value(num); 
307
308   if (pasla) {
309     ///    theflags.SetTrue (num, Graph_Present);   // nouveau : noter avec newstat
310     thestats->SetValue(num,newstat);
311   } else {
312     Standard_Integer overstat = stat;
313     if (stat != newstat) {                   // deja pris, meme statut : passer
314       if (cumul) overstat += overlapstat;    // nouveau statut : avec cumul ...
315       else       overstat  = overlapstat;    // ... ou sans (statut force)
316       if (stat != overstat)                  // si repasse deja faite, passer
317         thestats->SetValue(num,overstat);
318     }
319   }
320   if (!shared) return;
321   //  Attention a la redefinition !
322   Interface_EntityIterator aIter = GetShareds(ent);
323
324   for ( ; aIter.More() ; aIter.Next())    
325     GetFromEntity(aIter.Value(),Standard_True,newstat);
326 }
327
328 void  Interface_Graph::GetFromIter
329 (const Interface_EntityIterator& iter, const Standard_Integer newstat)
330 {
331    if(thestats.IsNull())
332     return;
333   for (iter.Start(); iter.More(); iter.Next()) {
334     Handle(Standard_Transient) ent = iter.Value();
335     Standard_Integer num = EntityNumber(ent);
336     if (!num) 
337       continue;
338     if (theflags.CTrue(num,Graph_Present)) 
339       continue;
340     thestats->SetValue(num,newstat);
341   }
342 }
343
344
345 void  Interface_Graph::GetFromIter
346 (const Interface_EntityIterator& iter,
347  const Standard_Integer newstat, const Standard_Integer overlapstat,
348  const Standard_Boolean cumul)
349 {
350   if(thestats.IsNull())
351     return;
352   for (iter.Start(); iter.More(); iter.Next()) {
353     Handle(Standard_Transient) ent = iter.Value();
354     Standard_Integer num   = EntityNumber(ent);
355     if (!num) 
356       continue;
357     /*Standard_Boolean pasla = !*/theflags.Value(num,Graph_Present);
358     /*Standard_Integer stat  = */thestats->Value(num); 
359     GetFromEntity (ent,Standard_False,newstat,overlapstat,cumul);
360   }
361 }
362
363
364 void Interface_Graph::GetFromGraph (const Interface_Graph& agraph)
365 {
366   if (Model() != agraph.Model()) Standard_DomainError::Raise
367     ("Graph from Interface : GetFromGraph");
368   Standard_Integer nb = Size();
369   for (Standard_Integer i = 1; i <= nb; i ++) {
370     if (agraph.IsPresent(i))
371       GetFromEntity (agraph.Entity(i),Standard_False,agraph.Status(i));
372   }
373 }
374
375 void Interface_Graph::GetFromGraph
376 (const Interface_Graph& agraph, const Standard_Integer stat)
377 {
378   if (Model() != agraph.Model()) Standard_DomainError::Raise
379     ("Graph from Interface : GetFromGraph");
380   Standard_Integer nb = Size();
381   for (Standard_Integer i = 1; i <= nb; i ++) {
382     if (agraph.IsPresent(i) && agraph.Status(i) == stat)
383       GetFromEntity (agraph.Entity(i),Standard_False,stat);
384   }
385 }
386
387 //  #####################################################################
388
389 //  ....                Listage des Entites Partagees                ....
390
391 Standard_Boolean Interface_Graph::HasShareErrors
392 (const Handle(Standard_Transient)& ent) const
393 {
394   if(thestats.IsNull())
395     return Standard_False;
396   Standard_Integer num   = EntityNumber(ent);
397   if (num == 0) return Standard_True;
398   return theflags.Value (num,Graph_ShareError);
399 }
400
401 Interface_EntityIterator Interface_Graph::Shareds
402 (const Handle(Standard_Transient)& ent) const
403 {
404   Interface_EntityIterator iter;
405   Standard_Integer num   = EntityNumber(ent);
406   if(!num)
407     return iter;
408
409   Handle(Standard_Transient) aCurEnt =  ent;
410   if (themodel->IsRedefinedContent(num)) 
411      aCurEnt = themodel->ReportEntity(num)->Content();
412
413   //if (num == 0)  Standard_DomainError::Raise ("Interface : Shareds");
414   Handle(Interface_GeneralModule) module;
415   Standard_Integer CN;
416   if (themodel->GTool()->Select(aCurEnt,module,CN))  
417     module->FillShared(themodel,CN,aCurEnt,iter);
418   return iter;
419 }
420
421 Handle(TColStd_HSequenceOfTransient) Interface_Graph::GetShareds(const Handle(Standard_Transient)& ent) const
422 {
423   Handle(TColStd_HSequenceOfTransient) aseq = new TColStd_HSequenceOfTransient;
424   Interface_EntityIterator iter = Shareds(ent);
425   for( ; iter.More(); iter.Next())
426     aseq->Append(iter.Value());
427   return aseq;
428 }
429
430 Handle(TColStd_HSequenceOfTransient) Interface_Graph::GetSharings(const Handle(Standard_Transient)& ent) const
431 {
432   Standard_Integer num   = EntityNumber(ent);
433   if(!num)
434     return 0;
435   //return Handle(TColStd_HSequenceOfTransient)::DownCast(thesharings->Value(num));
436   const TColStd_ListOfInteger& alist = thesharings->Value(num);
437   Handle(TColStd_HSequenceOfTransient) aSharings = new TColStd_HSequenceOfTransient;
438   TColStd_ListIteratorOfListOfInteger aIt(alist);
439   for( ; aIt.More() ; aIt.Next())
440     aSharings->Append(Entity(aIt.Value()));
441   return aSharings;
442 }
443
444 Interface_EntityIterator Interface_Graph::Sharings
445 (const Handle(Standard_Transient)& ent) const
446 {
447   Interface_EntityIterator iter;
448   iter.AddList(GetSharings(ent));
449   return iter;
450
451 }
452
453 static void AddTypedSharings
454 (const Handle(Standard_Transient)& ent, const Handle(Standard_Type)& type,
455  Interface_EntityIterator& iter, const Standard_Integer n,
456  const Interface_Graph& G)
457 {
458   if (ent.IsNull()) return;
459   if (ent->IsKind(type))  {  iter.AddItem (ent);  return;  }
460   if (iter.NbEntities() > n) return;
461
462   Handle(TColStd_HSequenceOfTransient) list = G.GetSharings(ent);
463   if(list.IsNull())
464     return;
465
466   Standard_Integer nb = list->Length();
467   for (Standard_Integer i = 1; i <= nb; i ++)
468     AddTypedSharings (list->Value(i) ,type,iter,nb,G);
469 }
470
471 Interface_EntityIterator Interface_Graph::TypedSharings
472 (const Handle(Standard_Transient)& ent, const Handle(Standard_Type)& type) const
473 {
474   Interface_EntityIterator iter;
475   Standard_Integer n = Size();
476   AddTypedSharings (ent,type,iter,n,*this);
477   return iter;
478 }
479
480
481 Interface_EntityIterator Interface_Graph::RootEntities () const
482 {
483   Interface_EntityIterator iter;
484   Standard_Integer nb = thesharings->Length();
485   for (Standard_Integer i = 1; i <= nb; i ++) {
486     if(!thesharings->Value(i).IsEmpty())
487       continue;
488     iter.AddItem(Entity(i));
489   }
490   return iter;
491 }
492
493 Handle(TCollection_HAsciiString)  Interface_Graph::Name(const Handle(Standard_Transient)& ent) const
494 {
495   Handle(TCollection_HAsciiString) str;
496   if (themodel.IsNull()) return str;
497   if (themodel->Number(ent)) return str;
498
499   Handle(Interface_GTool) gtool = themodel->GTool();
500   if (gtool.IsNull()) return str;
501
502   Handle(Interface_GeneralModule) module;
503   Standard_Integer CN;
504   if (!gtool->Select(ent,module,CN)) return str;
505
506   Interface_ShareTool sht (*this);
507   return module->Name (CN,ent,sht);
508 }
509
510 Standard_Boolean Interface_Graph::ModeStat() const
511 {
512   return (!thestats.IsNull());
513 }