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