0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / HLRBRep / HLRBRep_InternalAlgo.cxx
1 // Created on: 1997-04-17
2 // Created by: Christophe MARION
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <HLRAlgo.hxx>
19 #include <HLRBRep_Data.hxx>
20 #include <HLRBRep_Hider.hxx>
21 #include <HLRBRep_InternalAlgo.hxx>
22 #include <HLRBRep_ShapeBounds.hxx>
23 #include <HLRBRep_ShapeToHLR.hxx>
24 #include <HLRTopoBRep_OutLiner.hxx>
25 #include <Standard_Transient.hxx>
26 #include <Standard_ErrorHandler.hxx>
27 #include <Standard_OutOfRange.hxx>
28 #include <Standard_Stream.hxx>
29 #include <Standard_Type.hxx>
30 #include <TColStd_Array1OfReal.hxx>
31
32 #include <stdio.h>
33 IMPLEMENT_STANDARD_RTTIEXT(HLRBRep_InternalAlgo,Standard_Transient)
34
35 extern Standard_Integer nbPtIntersection;   // total P.I.
36 extern Standard_Integer nbSegIntersection;  // total S.I
37 extern Standard_Integer nbClassification;   // total classification
38 extern Standard_Integer nbOkIntersection;   // pairs of intersecting edges
39 extern Standard_Integer nbCal1Intersection; // pairs of unrejected edges
40 extern Standard_Integer nbCal2Intersection; // true intersections (not vertex)
41 extern Standard_Integer nbCal3Intersection; // curve-surface intersections
42
43 static Standard_Integer HLRBRep_InternalAlgo_TRACE = Standard_True;
44 static Standard_Integer HLRBRep_InternalAlgo_TRACE10 = Standard_True; 
45
46 //=======================================================================
47 //function : HLRBRep_InternalAlgo
48 //purpose  : 
49 //=======================================================================
50
51 HLRBRep_InternalAlgo::HLRBRep_InternalAlgo () :
52 myDebug       (Standard_False)
53 {
54 }
55
56 //=======================================================================
57 //function : HLRBRep_InternalAlgo
58 //purpose  : 
59 //=======================================================================
60
61 HLRBRep_InternalAlgo::
62 HLRBRep_InternalAlgo (const Handle(HLRBRep_InternalAlgo)& A)
63 {
64   myDS          = A->DataStructure();
65   myProj        = A->Projector();
66   myShapes      = A->SeqOfShapeBounds();
67   myDebug       = A->Debug();
68 }
69
70 //=======================================================================
71 //function : Projector
72 //purpose  : 
73 //=======================================================================
74
75 void HLRBRep_InternalAlgo::Projector (const HLRAlgo_Projector& P)
76 {
77   myProj = P;
78 }
79
80 //=======================================================================
81 //function : Projector
82 //purpose  : 
83 //=======================================================================
84
85 HLRAlgo_Projector & HLRBRep_InternalAlgo::Projector ()
86 { return myProj; }
87
88 //=======================================================================
89 //function : Update
90 //purpose  : 
91 //=======================================================================
92
93 void HLRBRep_InternalAlgo::Update ()
94 {
95   if (!myShapes.IsEmpty()) {
96     Standard_Integer n = myShapes.Length();
97     Handle(HLRBRep_Data) *DS = new Handle(HLRBRep_Data) [n];
98
99     Standard_Integer i,dv,de,df,nv=0,ne=0,nf=0;
100
101     for (i = 1; i <= n; i++) {
102       HLRBRep_ShapeBounds& SB = myShapes(i);
103       try {
104         OCC_CATCH_SIGNALS
105         DS[i-1] = HLRBRep_ShapeToHLR::Load(SB.Shape(),
106                                            myProj,
107                                            myMapOfShapeTool,
108                                            SB.NbOfIso());
109         dv = DS[i-1]->NbVertices();
110         de = DS[i-1]->NbEdges   ();
111         df = DS[i-1]->NbFaces   ();
112       }
113       catch(Standard_Failure const& anException) {
114         if (myDebug)
115         {
116           std::cout << "An exception was caught when preparing the Shape " << i;
117           std::cout << " and computing its OutLines " << std::endl;
118           std::cout << anException << std::endl;
119         }
120         DS[i-1] = new HLRBRep_Data(0,0,0);
121         dv = 0;
122         de = 0;
123         df = 0;
124       }
125
126       SB = HLRBRep_ShapeBounds
127         (SB.Shape(),SB.ShapeData(),SB.NbOfIso(),1,dv,1,de,1,df);
128       nv += dv;
129       ne += de;
130       nf += df;
131     }
132
133     if (n == 1) myDS = DS[0];
134     else {
135       myDS = new HLRBRep_Data(nv,ne,nf);
136       nv = 0;
137       ne = 0;
138       nf = 0;
139
140       for (i = 1; i <= n; i++) {
141         HLRBRep_ShapeBounds& SB = myShapes(i);
142         SB.Sizes(dv,de,df);
143         SB.Translate(nv,ne,nf);
144         myDS->Write(DS[i-1],nv,ne,nf);
145         nv += dv;
146         ne += de;
147         nf += df;
148       }
149     }
150
151     delete [] DS;
152
153     myDS->Update(myProj);
154
155     HLRAlgo_EdgesBlock::MinMaxIndices ShapMin, ShapMax, MinMaxShap;
156     HLRAlgo_EdgesBlock::MinMaxIndices TheMin, TheMax;
157     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
158     HLRBRep_Array1OfFData& aFDataArray = myDS->FDataArray();
159
160     for (i = 1; i <= n; i++) {
161       Standard_Boolean FirstTime = Standard_True;
162       HLRBRep_ShapeBounds& SB = myShapes(i);
163       Standard_Integer v1,v2,e1,e2,f1,f2;
164       SB.Bounds(v1,v2,e1,e2,f1,f2);
165
166       for (Standard_Integer e = e1; e <= e2; e++) {
167         HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
168         HLRAlgo::DecodeMinMax(ed.MinMax(), TheMin, TheMax);
169         if (FirstTime) {
170           FirstTime = Standard_False;
171           HLRAlgo::CopyMinMax(TheMin, TheMax, ShapMin, ShapMax);
172         }
173         else
174           HLRAlgo::AddMinMax(TheMin, TheMax, ShapMin, ShapMax);
175       }
176
177       for (Standard_Integer f = f1; f <= f2; f++) {
178         HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
179         HLRAlgo::DecodeMinMax(fd.Wires()->MinMax(), TheMin, TheMax);
180         HLRAlgo::AddMinMax(TheMin, TheMax, ShapMin, ShapMax);
181       }
182       HLRAlgo::EncodeMinMax(ShapMin, ShapMax, MinMaxShap);
183       SB.UpdateMinMax(MinMaxShap);
184     }
185   }
186 }
187
188 //=======================================================================
189 //function : Load
190 //purpose  : 
191 //=======================================================================
192
193 void HLRBRep_InternalAlgo::Load (const Handle(HLRTopoBRep_OutLiner)& S,
194                                  const Handle(Standard_Transient)& SData, 
195                                  const Standard_Integer nbIso)
196
197   myShapes.Append(HLRBRep_ShapeBounds(S,SData,nbIso,0,0,0,0,0,0));
198   myDS.Nullify();
199 }
200
201 //=======================================================================
202 //function : Load
203 //purpose  : 
204 //=======================================================================
205
206 void HLRBRep_InternalAlgo::Load (const Handle(HLRTopoBRep_OutLiner)& S,
207                                  const Standard_Integer nbIso)
208
209   myShapes.Append(HLRBRep_ShapeBounds(S,nbIso,0,0,0,0,0,0));
210   myDS.Nullify();
211 }
212
213 //=======================================================================
214 //function : Index
215 //purpose  : 
216 //=======================================================================
217
218 Standard_Integer HLRBRep_InternalAlgo::
219 Index (const Handle(HLRTopoBRep_OutLiner)& S) const
220 {
221   Standard_Integer n = myShapes.Length();
222
223   for (Standard_Integer i = 1; i <= n; i++)
224     if (myShapes(i).Shape() == S) return i;
225
226   return 0;
227 }
228
229 //=======================================================================
230 //function : Remove
231 //purpose  : 
232 //=======================================================================
233
234 void HLRBRep_InternalAlgo::Remove (const Standard_Integer I)
235 {
236   Standard_OutOfRange_Raise_if
237     (I == 0 || I > myShapes.Length(),
238      "HLRBRep_InternalAlgo::Remove : unknown Shape");
239   myShapes.Remove(I);
240   
241   myMapOfShapeTool.Clear();
242   myDS.Nullify();
243 }
244
245 //=======================================================================
246 //function : ShapeData
247 //purpose  : 
248 //=======================================================================
249
250 void HLRBRep_InternalAlgo::ShapeData (const Standard_Integer I,
251                                       const Handle(Standard_Transient)& SData)
252 {
253   Standard_OutOfRange_Raise_if
254     (I == 0 || I > myShapes.Length(),
255      "HLRBRep_InternalAlgo::ShapeData : unknown Shape");
256
257   myShapes(I).ShapeData(SData);
258 }
259
260 //=======================================================================
261 //function : SeqOfShapeBounds
262 //purpose  : 
263 //=======================================================================
264
265 HLRBRep_SeqOfShapeBounds & HLRBRep_InternalAlgo::SeqOfShapeBounds ()
266 {
267   return myShapes;
268 }
269
270 //=======================================================================
271 //function : NbShapes
272 //purpose  : 
273 //=======================================================================
274
275 Standard_Integer HLRBRep_InternalAlgo::NbShapes () const
276 { return myShapes.Length(); }
277
278 //=======================================================================
279 //function : ShapeBounds
280 //purpose  : 
281 //=======================================================================
282
283 HLRBRep_ShapeBounds & HLRBRep_InternalAlgo::
284 ShapeBounds (const Standard_Integer I)
285 {
286   Standard_OutOfRange_Raise_if
287     (I == 0 || I > myShapes.Length(),
288      "HLRBRep_InternalAlgo::ShapeBounds : unknown Shape");
289
290   return myShapes(I);
291 }
292
293 //=======================================================================
294 //function : InitEdgeStatus
295 //purpose  : 
296 //=======================================================================
297
298 void HLRBRep_InternalAlgo::InitEdgeStatus () 
299 {
300   Standard_Boolean visible;
301   HLRBRep_FaceIterator faceIt;
302   
303   HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
304   HLRBRep_Array1OfFData& aFDataArray = myDS->FDataArray();
305   Standard_Integer ne = myDS->NbEdges();
306   Standard_Integer nf = myDS->NbFaces();
307
308   for (Standard_Integer e = 1; e <= ne; e++) {
309     HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
310     if (ed.Selected()) ed.Status().ShowAll();
311   }
312 //  for (Standard_Integer f = 1; f <= nf; f++) {
313   Standard_Integer f;
314   for ( f = 1; f <= nf; f++) {
315     HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
316     if (fd.Selected()) {
317       
318       for (faceIt.InitEdge(fd);
319            faceIt.MoreEdge();
320            faceIt.NextEdge()) {
321         HLRBRep_EdgeData* edf = &(myDS->EDataArray().ChangeValue(faceIt.Edge()));
322         if (edf->Selected()) edf->Status().HideAll();
323       }
324     }
325   }
326
327   for (f = 1; f <= nf; f++) {
328     HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
329     visible = Standard_True;
330     if (fd.Selected() && fd.Closed()) {
331       if      ( fd.Side())      visible =  Standard_False;
332       else if ( !fd.WithOutL()) {
333         switch (fd.Orientation()) {
334         case TopAbs_REVERSED : visible =  fd.Back()   ; break;
335         case TopAbs_FORWARD  : visible = !fd.Back()   ; break;
336         case TopAbs_EXTERNAL :
337         case TopAbs_INTERNAL : visible =  Standard_True; break;
338         }
339       }
340     } 
341     if (visible) {
342       
343       for (faceIt.InitEdge(fd);
344            faceIt.MoreEdge();
345            faceIt.NextEdge()) {
346         Standard_Integer E = faceIt.Edge();
347         HLRBRep_EdgeData* edf = &(myDS->EDataArray().ChangeValue(E));
348         if ( edf->Selected() &&
349             !edf->Vertical())
350           edf->Status().ShowAll();
351       }
352     }
353   }
354 }
355
356 //=======================================================================
357 //function : Select
358 //purpose  : 
359 //=======================================================================
360
361 void HLRBRep_InternalAlgo::Select ()
362 {
363   if (!myDS.IsNull()) {
364     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
365     HLRBRep_Array1OfFData& aFDataArray = myDS->FDataArray();
366     Standard_Integer ne = myDS->NbEdges();
367     Standard_Integer nf = myDS->NbFaces();
368     
369     for (Standard_Integer e = 1; e <= ne; e++) {
370       HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
371       ed.Selected(Standard_True);
372     }
373     
374     for (Standard_Integer f = 1; f <= nf; f++) {
375       HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
376       fd.Selected(Standard_True);
377     }
378   }
379 }
380
381 //=======================================================================
382 //function : Select
383 //purpose  : 
384 //=======================================================================
385
386 void HLRBRep_InternalAlgo::Select (const Standard_Integer I)
387 {
388   if (!myDS.IsNull()) { 
389     Standard_OutOfRange_Raise_if
390       (I == 0 || I > myShapes.Length(),
391        "HLRBRep_InternalAlgo::Select : unknown Shape");
392
393     Standard_Integer v1,v2,e1,e2,f1,f2;
394     myShapes(I).Bounds(v1,v2,e1,e2,f1,f2);
395     
396     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
397     HLRBRep_Array1OfFData& aFDataArray = myDS->FDataArray();
398     Standard_Integer ne = myDS->NbEdges();
399     Standard_Integer nf = myDS->NbFaces();
400     
401     for (Standard_Integer e = 1; e <= ne; e++) {
402       HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
403       ed.Selected(e >= e1 && e <= e2);
404     }
405     
406     for (Standard_Integer f = 1; f <= nf; f++) {
407       HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
408       fd.Selected(f >= f1 && f <= f2);
409     }
410   }
411 }
412
413 //=======================================================================
414 //function : SelectEdge
415 //purpose  : 
416 //=======================================================================
417
418 void HLRBRep_InternalAlgo::SelectEdge (const Standard_Integer I)
419 {
420   if (!myDS.IsNull()) {
421     Standard_OutOfRange_Raise_if
422       (I == 0 || I > myShapes.Length(),
423        "HLRBRep_InternalAlgo::SelectEdge : unknown Shape");
424     
425     Standard_Integer v1,v2,e1,e2,f1,f2;
426     myShapes(I).Bounds(v1,v2,e1,e2,f1,f2);
427     
428     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
429     Standard_Integer ne = myDS->NbEdges();
430     
431     for (Standard_Integer e = 1; e <= ne; e++) {
432       HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
433       ed.Selected(e >= e1 && e <= e2);
434     }
435   }
436 }
437
438 //=======================================================================
439 //function : SelectFace
440 //purpose  : 
441 //=======================================================================
442
443 void HLRBRep_InternalAlgo::SelectFace (const Standard_Integer I)
444 {
445   if (!myDS.IsNull()) {
446     Standard_OutOfRange_Raise_if
447       (I == 0 || I > myShapes.Length(),
448        "HLRBRep_InternalAlgo::SelectFace : unknown Shape");
449     
450     Standard_Integer v1,v2,e1,e2,f1,f2;
451     myShapes(I).Bounds(v1,v2,e1,e2,f1,f2);
452     
453     HLRBRep_Array1OfFData& aFDataArray = myDS->FDataArray();
454     Standard_Integer nf = myDS->NbFaces();
455     
456     for (Standard_Integer f = 1; f <= nf; f++) {
457       HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
458       fd.Selected(f >= f1 && f <= f2);
459     }
460   }
461 }
462
463 //=======================================================================
464 //function : ShowAll
465 //purpose  : 
466 //=======================================================================
467
468 void HLRBRep_InternalAlgo::ShowAll ()
469 {
470   if (!myDS.IsNull()) {
471     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
472     Standard_Integer ne = myDS->NbEdges();
473
474     for (Standard_Integer ie = 1; ie <= ne; ie++) {
475       HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(ie);
476       ed.Status().ShowAll();
477     }
478   }
479 }
480
481 //=======================================================================
482 //function : ShowAll
483 //purpose  : 
484 //=======================================================================
485
486 void HLRBRep_InternalAlgo::ShowAll (const Standard_Integer I)
487 {
488   if (!myDS.IsNull()) {
489     Standard_OutOfRange_Raise_if
490       (I == 0 || I > myShapes.Length(),
491        "HLRBRep_InternalAlgo::ShowAll : unknown Shape");
492     
493     Select(I);
494     
495     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
496     Standard_Integer ne = myDS->NbEdges();
497     
498     for (Standard_Integer e = 1; e <= ne; e++) {
499       HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
500       if (ed.Selected()) ed.Status().ShowAll();
501     }
502   }
503 }
504
505 //=======================================================================
506 //function : HideAll
507 //purpose  : 
508 //=======================================================================
509
510 void HLRBRep_InternalAlgo::HideAll ()
511 {
512   if (!myDS.IsNull()) {
513     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
514     Standard_Integer ne = myDS->NbEdges();
515     
516     for (Standard_Integer ie = 1; ie <= ne; ie++) {
517       HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(ie);
518       ed.Status().HideAll();
519     }
520   }
521 }
522
523 //=======================================================================
524 //function : HideAll
525 //purpose  : 
526 //=======================================================================
527
528 void HLRBRep_InternalAlgo::HideAll (const Standard_Integer I)
529 {
530   if (!myDS.IsNull()) {
531     Standard_OutOfRange_Raise_if
532       (I == 0 || I > myShapes.Length(),
533        "HLRBRep_InternalAlgo::HideAll : unknown Shape");
534     
535     Select(I);
536     
537     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
538     Standard_Integer ne = myDS->NbEdges();
539     
540     for (Standard_Integer e = 1; e <= ne; e++) {
541       HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
542       if (ed.Selected()) ed.Status().HideAll();
543     }
544   }
545 }
546
547 //=======================================================================
548 //function : PartialHide
549 //purpose  : 
550 //=======================================================================
551
552 void HLRBRep_InternalAlgo::PartialHide ()
553 {
554   if (!myDS.IsNull()) {
555     Standard_Integer i,n = myShapes.Length();
556
557     if (myDebug)
558       std::cout << " Partial hiding" << std::endl << std::endl;
559
560     for (i = 1; i <= n; i++)
561       Hide(i);
562     
563     Select();
564   }
565 }
566
567 //=======================================================================
568 //function : Hide
569 //purpose  : 
570 //=======================================================================
571
572 void HLRBRep_InternalAlgo::Hide ()
573 {
574   if (!myDS.IsNull()) {
575     Standard_Integer i,j,n = myShapes.Length();
576
577     if (myDebug)
578       std::cout << " Total hiding" << std::endl;
579
580     for (i = 1; i <= n; i++)
581       Hide(i);
582     
583     for (i = 1; i <= n; i++)
584       for (j = 1; j <= n; j++)
585         if (i != j) Hide(i,j);
586     
587     Select();
588   }
589 }
590
591 //=======================================================================
592 //function : Hide
593 //purpose  : 
594 //=======================================================================
595
596 void HLRBRep_InternalAlgo::Hide (const Standard_Integer I)
597 {
598   if (!myDS.IsNull()) {
599     Standard_OutOfRange_Raise_if
600       (I == 0 || I > myShapes.Length(),
601        "HLRBRep_InternalAlgo::Hide : unknown Shape");
602     
603     if (myDebug)
604       std::cout << " hiding the shape " << I << " by itself" << std::endl;
605     
606     Select(I);
607     InitEdgeStatus();
608     HideSelected(I,Standard_True);
609   }
610 }
611
612 //=======================================================================
613 //function : Hide
614 //purpose  : 
615 //=======================================================================
616
617 void HLRBRep_InternalAlgo::Hide (const Standard_Integer I,
618                                  const Standard_Integer J)
619 {
620   if (!myDS.IsNull()) {
621     Standard_OutOfRange_Raise_if
622       (I == 0 || I > myShapes.Length() ||
623        J == 0 || J > myShapes.Length(),
624        "HLRBRep_InternalAlgo::Hide : unknown Shapes");
625
626     if (I == J) Hide(I);
627     else {
628       HLRAlgo_EdgesBlock::MinMaxIndices* MinMaxShBI = &myShapes(I).MinMax();
629       HLRAlgo_EdgesBlock::MinMaxIndices* MinMaxShBJ = &myShapes(J).MinMax();
630       if (((MinMaxShBJ->Max[0] - MinMaxShBI->Min[0]) & 0x80008000) == 0 &&
631           ((MinMaxShBI->Max[0] - MinMaxShBJ->Min[0]) & 0x80008000) == 0 &&
632           ((MinMaxShBJ->Max[1] - MinMaxShBI->Min[1]) & 0x80008000) == 0 &&
633           ((MinMaxShBI->Max[1] - MinMaxShBJ->Min[1]) & 0x80008000) == 0 &&
634           ((MinMaxShBJ->Max[2] - MinMaxShBI->Min[2]) & 0x80008000) == 0 &&
635           ((MinMaxShBI->Max[2] - MinMaxShBJ->Min[2]) & 0x80008000) == 0 &&
636           ((MinMaxShBJ->Max[3] - MinMaxShBI->Min[3]) & 0x80008000) == 0 &&
637           ((MinMaxShBI->Max[3] - MinMaxShBJ->Min[3]) & 0x80008000) == 0 &&
638           ((MinMaxShBJ->Max[4] - MinMaxShBI->Min[4]) & 0x80008000) == 0 &&
639           ((MinMaxShBI->Max[4] - MinMaxShBJ->Min[4]) & 0x80008000) == 0 &&
640           ((MinMaxShBJ->Max[5] - MinMaxShBI->Min[5]) & 0x80008000) == 0 &&
641           ((MinMaxShBI->Max[5] - MinMaxShBJ->Min[5]) & 0x80008000) == 0 &&
642           ((MinMaxShBJ->Max[6] - MinMaxShBI->Min[6]) & 0x80008000) == 0 &&
643           ((MinMaxShBJ->Max[7] - MinMaxShBI->Min[7]) & 0x80008000) == 0) {
644         if (myDebug) {
645           std::cout << " hiding the shape " << I;
646           std::cout << " by the shape : " << J << std::endl;
647         }
648         SelectEdge(I);
649         SelectFace(J);
650         HideSelected(I,Standard_False);
651       }
652     }
653   }
654 }
655
656 //=======================================================================
657 //function : HideSelected
658 //purpose  : 
659 //=======================================================================
660
661 void HLRBRep_InternalAlgo::HideSelected (const Standard_Integer I,
662                                          const Standard_Boolean SideFace)
663 {
664   Standard_Integer e,f,j,nbVisEdges,nbSelEdges,nbSelFaces,nbCache;
665   Standard_Integer nbFSide,nbFSimp;
666
667 #ifdef OCCT_DEBUG
668   if (myDebug) {
669     nbPtIntersection = 0;
670     nbSegIntersection = 0;
671     nbOkIntersection = 0;
672     nbClassification = 0;
673     nbCal1Intersection = 0;
674     nbCal2Intersection = 0;
675     nbCal3Intersection = 0;
676   }
677 #endif
678
679   HLRBRep_ShapeBounds& SB = myShapes(I);
680   Standard_Integer v1,v2,e1,e2,f1,f2;
681   SB.Bounds(v1,v2,e1,e2,f1,f2);
682
683   if (e2 >= e1) {
684     myDS->InitBoundSort(SB.MinMax(),e1,e2);
685     HLRBRep_Hider Cache(myDS);
686     HLRBRep_Array1OfEData& aEDataArray = myDS->EDataArray();
687     HLRBRep_Array1OfFData& aFDataArray = myDS->FDataArray();
688     Standard_Integer ne = myDS->NbEdges();
689     Standard_Integer nf = myDS->NbFaces();
690     
691     if (myDebug) {
692       nbVisEdges = 0;
693       nbSelEdges = 0;
694       nbSelFaces = 0;
695       nbCache = 0;
696       nbFSide = 0;
697       nbFSimp = 0;
698       
699       for (e = 1; e <= ne; e++) {
700         HLRBRep_EdgeData& ed = aEDataArray.ChangeValue(e);
701         if (ed.Selected()) {
702           nbSelEdges++;
703           if (!ed.Status().AllHidden()) nbVisEdges++;
704         }
705       }
706       
707       for (f = 1; f <= nf; f++) {
708         HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
709         if (fd.Selected()) {
710           nbSelFaces++;
711           if (fd.Hiding()) nbCache++;
712           if (fd.Side  ()) nbFSide++;
713           if (fd.Simple()) nbFSimp++;
714         }
715       }
716       
717       if (myDebug)
718       {
719         std::cout << std::endl;
720         std::cout << "Vertices  : " << std::setw(5) << myDS->NbVertices() << std::endl;
721         std::cout << "Edges     : " << std::setw(5) << myDS->NbEdges()    << " , ";
722         std::cout << "Selected  : " << std::setw(5) << nbSelEdges         << " , ";
723         std::cout << "Visible   : " << std::setw(5) << nbVisEdges         << std::endl;
724         std::cout << "Faces     : " << std::setw(5) << myDS->NbFaces()    << " , ";
725         std::cout << "Selected  : " << std::setw(5) << nbSelFaces         << " , ";
726         std::cout << "Simple    : " << std::setw(5) << nbFSimp            << std::endl;
727         if (SideFace)
728           std::cout << "Side      : " << std::setw(5) << nbFSide            << " , ";
729         std::cout << "Cachantes : " << std::setw(5) << nbCache            << std::endl << std::endl;
730       }
731     }
732
733     if (nf == 0)
734       return;
735
736     Standard_Integer QWE=0,QWEQWE;
737     QWEQWE=nf/10;
738
739     if (SideFace) {
740       j = 0;
741       
742       for (f = 1; f <= nf; f++) {
743         HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
744         if (fd.Selected()) {
745           if (fd.Side()) {
746             if(HLRBRep_InternalAlgo_TRACE10) { 
747               if(++QWE>QWEQWE) { 
748                 QWE=0; 
749                 if (myDebug)
750                   std::cout<<"*";
751               } 
752             }
753             else {  
754               if (myDebug && HLRBRep_InternalAlgo_TRACE) {
755                 j++;
756                 std::cout << " OwnHiding " << j << " of face : " << f << std::endl;
757               }
758             }
759             Cache.OwnHiding(f);
760           }
761         }
762       }
763     }
764     
765
766 //--
767     TColStd_Array1OfInteger Val(1, nf);
768     TColStd_Array1OfReal    Size(1, nf);
769     TColStd_Array1OfInteger Index(1, nf);
770
771
772     for (f = 1; f <= nf; f++) {
773       HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
774       if (fd.Plane())          Val(f) = 10;
775       else if(fd.Cylinder())  Val(f)=9;
776       else if(fd.Cone())      Val(f)=8;
777       else if(fd.Sphere())    Val(f)=7;
778       else if(fd.Torus())     Val(f)=6;
779       else Val(f)=0;
780       if(fd.Cut())            Val(f)-=10;
781       if(fd.Side())           Val(f)-=100;
782       if(fd.WithOutL())       Val(f)-=20;
783   
784       Size(f)=fd.Size();
785     }
786
787     for(Standard_Integer tt=1;tt<=nf;tt++) { 
788       Index(tt)=tt;
789     }
790
791     //-- ======================================================================
792 /*    Standard_Boolean TriOk; //-- a refaire
793     do { 
794       Standard_Integer t,tp1;
795       TriOk=Standard_True;
796       for(t=1,tp1=2;t<nf;t++,tp1++) { 
797         if(Val(Index(t))<Val(Index(tp1))) {
798           Standard_Integer q=Index(t); Index(t)=Index(tp1); Index(tp1)=q;
799           TriOk=Standard_False;
800         }
801         else if(Val(Index(t))==Val(Index(tp1))) { 
802           if(Size(Index(t))<Size(Index(tp1))) { 
803             Standard_Integer q=Index(t); Index(t)=Index(tp1); Index(tp1)=q;
804             TriOk=Standard_False;
805           }
806         }
807       }
808     }
809     while(TriOk==Standard_False);
810 */
811     //-- ======================================================================
812     if(nf>2)  {
813       Standard_Integer i,ir,k,l;
814       Standard_Integer rra;
815       l=(nf>>1)+1;
816       ir=nf;
817       for(;;) { 
818         if(l>1) { 
819           rra=Index(--l); 
820         } 
821         else {    
822           rra=Index(ir); 
823           Index(ir)=Index(1);
824           if(--ir == 1) { 
825             Index(1)=rra;
826             break;
827           }
828         }
829         i=l;
830         k=l+l;
831         while(k<=ir) { 
832           if(k<ir) { 
833             if(Val(Index(k)) > Val(Index(k+1)))
834               k++;
835             else if(Val(Index(k)) == Val(Index(k+1))) { 
836               if(Size(Index(k)) > Size(Index(k+1))) 
837                 k++;
838             }
839           }
840           if(Val(rra) > Val(Index(k))) { 
841             Index(i)=Index(k);
842             i=k;
843             k<<=1;
844           }
845           else if((Val(rra) == Val(Index(k))) && (Size(rra) > Size(Index(k)))) { 
846             Index(i)=Index(k);
847             i=k;
848             k<<=1;
849           }
850           else {  
851             k=ir+1;
852           }
853         }
854         Index(i)=rra;
855       }
856     }
857
858     j = 0;
859     
860     QWE=0;
861     for (f = 1; f <= nf; f++) {
862       Standard_Integer fi = Index(f);
863       HLRBRep_FaceData& fd = aFDataArray.ChangeValue(fi);
864       if (fd.Selected()) {
865         if (fd.Hiding()) {
866           if(HLRBRep_InternalAlgo_TRACE10 && HLRBRep_InternalAlgo_TRACE==Standard_False) { 
867             if(++QWE>QWEQWE) { 
868               if (myDebug)
869                 std::cout<<".";
870               QWE=0;
871             }
872           }
873           else if (myDebug && HLRBRep_InternalAlgo_TRACE) {
874             static int rty=0;
875             j++;
876             printf("%6d",fi); fflush(stdout);
877             if(++rty>25) { rty=0; printf("\n"); } 
878           }
879           Cache.Hide(fi,myMapOfShapeTool);
880         }
881       }
882     }
883     
884 #ifdef OCCT_DEBUG
885     if (myDebug) {
886       nbFSimp = 0;
887       
888       for (f = 1; f <= nf; f++) {
889         HLRBRep_FaceData& fd = aFDataArray.ChangeValue(f);
890         if (fd.Selected() && fd.Simple())
891           nbFSimp++;
892       }
893
894       std::cout << "\n";
895       std::cout << "Simple Faces                  : ";
896       std::cout << nbFSimp  << "\n";
897       std::cout << "Intersections calculees       : ";
898       std::cout << nbCal2Intersection << "\n";
899       std::cout << "Intersections Ok              : ";
900       std::cout << nbOkIntersection << "\n";
901       std::cout << "Points                        : ";
902       std::cout << nbPtIntersection << "\n";
903       std::cout << "Segments                      : ";
904       std::cout << nbSegIntersection << "\n";
905       std::cout << "Classification                : ";
906       std::cout << nbClassification << "\n";
907       std::cout << "Intersections curve-surface   : ";
908       std::cout << nbCal3Intersection << "\n";
909       std::cout << std::endl << std::endl;
910     }
911 #endif
912   }
913 }
914
915 //=======================================================================
916 //function : Debug
917 //purpose  : 
918 //=======================================================================
919
920 void HLRBRep_InternalAlgo::Debug (const Standard_Boolean deb)
921 { myDebug = deb; }
922
923 //=======================================================================
924 //function : Debug
925 //purpose  : 
926 //=======================================================================
927
928 Standard_Boolean HLRBRep_InternalAlgo::Debug () const
929 { return myDebug; }
930
931 //=======================================================================
932 //function : DataStructure
933 //purpose  : 
934 //=======================================================================
935
936 Handle(HLRBRep_Data) HLRBRep_InternalAlgo::DataStructure () const
937 { return myDS; }