0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / IntCurvesFace / IntCurvesFace_ShapeIntersector.cxx
1 // Created on: 1998-01-27
2 // Created by: Laurent BUCHARD
3 // Copyright (c) 1998-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 <Adaptor3d_HCurve.hxx>
19 #include <Bnd_BoundSortBox.hxx>
20 #include <Bnd_HArray1OfBox.hxx>
21 #include <ElCLib.hxx>
22 #include <gp_Lin.hxx>
23 #include <gp_Pnt.hxx>
24 #include <IntCurvesFace_Intersector.hxx>
25 #include <IntCurvesFace_ShapeIntersector.hxx>
26 #include <TColStd_ListIteratorOfListOfInteger.hxx>
27 #include <TColStd_ListOfInteger.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <TopoDS_Shape.hxx>
32
33 //-- ================================================================================
34 IntCurvesFace_ShapeIntersector::IntCurvesFace_ShapeIntersector() {
35   nbfaces=0;
36   done=Standard_False;
37   PtrJetons=NULL;
38   PtrJetonsIndex=NULL;
39 }
40 //-- ================================================================================
41 void IntCurvesFace_ShapeIntersector::Load(const TopoDS_Shape& shape,
42                                           const Standard_Real tol) { 
43   PtrJetons=NULL;
44   PtrJetonsIndex=NULL;
45   if(nbfaces) { 
46     Destroy();
47   }
48   TopExp_Explorer Ex;
49   Standard_Integer i;
50   for(nbfaces=0,i=0,Ex.Init(shape,TopAbs_FACE); Ex.More(); i++,Ex.Next()) { 
51     nbfaces++;
52     TopoDS_Face currentface = TopoDS::Face(Ex.Current());
53     PtrIntersector.Append((void *)(new IntCurvesFace_Intersector(currentface,tol)));
54   }  
55 }
56 //-- ================================================================================
57 void IntCurvesFace_ShapeIntersector::Destroy() { 
58   if(PtrJetons) { 
59     delete ((Standard_Integer *)PtrJetons);
60     PtrJetons=NULL;
61   }
62   if(PtrJetonsIndex) { 
63     delete ((Standard_Integer *)PtrJetonsIndex);
64     PtrJetonsIndex=NULL;
65   }
66   for(Standard_Integer i=1; i<=nbfaces; i++) { 
67     IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
68     delete Ptr;
69   }
70   done=Standard_False;
71   nbfaces=0;
72   PtrIntersector.Clear(); 
73   IndexPt.Clear();  IndexFace.Clear();   IndexIntPnt.Clear();IndexPar.Clear();
74 }
75 //-- ================================================================================
76 void IntCurvesFace_ShapeIntersector::Perform(const gp_Lin& L,
77                                              const Standard_Real ParMin,
78                                              const Standard_Real ParMax) { 
79   done = Standard_False;
80   for(Standard_Integer i=1; i<=nbfaces; i++) { 
81     IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
82     Ptr->Perform(L,ParMin,ParMax);
83   }
84   SortResult();
85 }
86 //-- ================================================================================
87 void IntCurvesFace_ShapeIntersector::PerformNearest(const gp_Lin& L,
88                                                     const Standard_Real ParMin,
89                                                     const Standard_Real _ParMax) { 
90
91   Standard_Integer i;
92   Standard_Integer* _PtrJetons=(Standard_Integer *)PtrJetons;
93   Standard_Integer* _PtrJetonsIndex=(Standard_Integer *)PtrJetonsIndex;
94
95
96   if(nbfaces>2) { 
97     if(PtrJetons==NULL) { 
98       PtrJetons      = (void *) new Standard_Integer [nbfaces];
99       PtrJetonsIndex = (void *) new Standard_Integer [nbfaces];
100       Standard_Integer *Ptr =(Standard_Integer *)PtrJetons;
101       Standard_Integer *PtrI=(Standard_Integer *)PtrJetonsIndex;
102       for(i=0;i<nbfaces;i++) { 
103         Ptr[i]=0;
104         PtrI[i]=i+1;
105       }
106       _PtrJetons=(Standard_Integer *)PtrJetons;
107       _PtrJetonsIndex=(Standard_Integer *)PtrJetonsIndex;
108     }
109   }
110
111   
112   Standard_Integer Indexface=-1;  
113   Standard_Real ParMax=_ParMax;
114   
115
116   done = Standard_False;
117   for(Standard_Integer ii=1; ii<=nbfaces; ii++) { 
118     if(_PtrJetons) { 
119       i=_PtrJetonsIndex[ii-1];
120     }
121     else { 
122       i=ii;
123     }
124       
125     IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
126     if(ParMin<ParMax) { 
127       Ptr->Perform(L,ParMin,ParMax);
128       if(Ptr->IsDone()) {
129         Standard_Integer n=Ptr->NbPnt();
130         for(Standard_Integer j=1;j<=n;j++) { 
131           Standard_Real w=Ptr->WParameter(j);
132           if(w<ParMax) { 
133             ParMax=w;       
134             Indexface=ii-1;
135           }
136         }
137       }
138       else { 
139         done = Standard_False;
140         return;
141       }
142     }
143   }
144   if(PtrJetons && Indexface>=0) { 
145     _PtrJetons[Indexface]++;
146
147     Standard_Integer im1;
148     for(im1=Indexface-1,i=Indexface; i>=1 && _PtrJetons[i]>_PtrJetons[i-1]; i--,im1--) { 
149       Standard_Integer t=_PtrJetonsIndex[i];
150       _PtrJetonsIndex[i]=_PtrJetonsIndex[im1];
151       _PtrJetonsIndex[im1]=t;
152       t=_PtrJetons[i];
153       _PtrJetons[i]=_PtrJetons[im1];
154       _PtrJetons[im1]=t;
155     }
156     //--for(Standard_Integer dd=0; dd<nbfaces;dd++) { if(_PtrJetons[dd]) { printf("\n<%3d %3d %3d>",dd,_PtrJetons[dd],_PtrJetonsIndex[dd]); }  } 
157     //--printf("\n");
158   }
159   SortResult();
160 }
161 //-- ================================================================================
162 void IntCurvesFace_ShapeIntersector::Perform(const Handle(Adaptor3d_HCurve)& HCu,
163                                              const Standard_Real ParMin,
164                                              const Standard_Real ParMax) { 
165   done = Standard_False;
166   for(Standard_Integer i=1; i<=nbfaces; i++) { 
167     IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
168     Ptr->Perform(HCu,ParMin,ParMax);
169   }
170   SortResult();
171 }
172 //-- ================================================================================
173 //-- PtrIntersector   : Sequence d addresses 
174 //-- IndexPt          : 1 2 3 .... n   points avant le tri 
175 //-- IndexFace        : Numero de la face (de l intersector) du point IndexPt(i)
176 //-- IndexIntPnt      : Numero du point  IndexPt(i) dans l'intersector IndexFace(IndexPt(i))
177 //-- IndexPar         : W parameter du point IndexPt(i)
178 //--
179 //-- En resume, pour chaque point indice par K = IndexPt(i) on a 
180 //--      * la face a laquelle il appartient                  : IndexFace(K)
181 //--      * le numero du point dans l'intersecteur FaceCurve  : IndexIntPnt(K)
182 //--      * le parametre W du point sur la courbe             : IndexPar(K)
183 //--
184 //-- SortResult Trie les points par ordre croissant de W 
185 //-- (remet a jour le tableau d index TabPt(.))
186 //-- 
187 //-- ================================================================================
188 void IntCurvesFace_ShapeIntersector::SortResult() { 
189   done = Standard_True;
190   Standard_Integer nbpnt=0;
191   IndexPt.Clear();  IndexFace.Clear();  IndexIntPnt.Clear();IndexPar.Clear();
192   //-- -----------------------------------------------------
193   //-- r e c u p e r a t i o n   d e s   r e s u l t a t s  
194   //--
195   for(Standard_Integer f=1; f<=nbfaces; f++) { 
196     IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(f);
197     if(Ptr->IsDone()) { 
198       Standard_Integer n=Ptr->NbPnt();
199       for(Standard_Integer j=1;j<=n;j++) { 
200         IndexPt.Append(++nbpnt);
201         IndexFace.Append(f);
202         IndexIntPnt.Append(j);
203         IndexPar.Append(Ptr->WParameter(j));
204       }
205     }
206     else { 
207       done = Standard_False;
208       return;
209     }
210   }
211   //-- -----------------------------------------------------
212   //-- t r i   s e l o n   l  e   p a r a m e t r e   w 
213   //--
214   Standard_Boolean triok;
215   do { 
216     triok=Standard_True;
217     for(Standard_Integer ind0=1;ind0<nbpnt;ind0++) { 
218       Standard_Integer ind  =IndexPt(ind0);
219       Standard_Integer indp1=IndexPt(ind0+1);
220       if(IndexPar(ind) > IndexPar(indp1)) { 
221         IndexPt(ind0)  =indp1;
222         IndexPt(ind0+1)=ind;
223         triok=Standard_False;
224       }
225     }
226   }
227   while(triok==Standard_False);
228 }
229 //-- ================================================================================
230 //-- Creation le 28 jan 98
231 //--