Salome HOME
Merge with OCC-V2_1_0_deb
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Tools.cxx
1 // File:        GEOMAlgo_Tools.cxx
2 // Created:     Mon Dec  6 11:35:29 2004
3 // Author:      Peter KURNEV
4 //              <pkv@irinox>
5
6 #include <GEOMAlgo_Tools.ixx>
7
8 #include <gp_Pnt.hxx>
9
10 #include <Geom_Surface.hxx>
11 #include <Geom_Curve.hxx>
12
13 #include <GeomAPI_ProjectPointOnSurf.hxx>
14
15 #include <TopAbs_ShapeEnum.hxx>
16
17 #include <TopoDS.hxx>
18 #include <TopoDS_Shape.hxx>
19 #include <TopoDS_Edge.hxx>
20
21 #include <TopTools_ListOfShape.hxx>
22 #include <TopTools_ListIteratorOfListOfShape.hxx>
23 #include <TopTools_IndexedMapOfShape.hxx>
24
25 #include <BRep_Tool.hxx>
26 #include <BOPTools_Tools2D.hxx>
27 #include <BRepTools.hxx>
28
29 #include <BOPTools_Tools2D.hxx>
30 #include <IntTools_Context.hxx>
31
32 #include <GEOMAlgo_PassKey.hxx>
33 #include <GEOMAlgo_IndexedDataMapOfPassKeyListOfShape.hxx>
34
35 //=======================================================================
36 //function : RefineSDShapes
37 //purpose  : 
38 //=======================================================================
39 Standard_Integer GEOMAlgo_Tools::RefineSDShapes(GEOMAlgo_IndexedDataMapOfPassKeyListOfShape &aMPKLE,
40                                                 const Standard_Real aTol,
41                                                 IntTools_Context& aCtx)
42 {
43   Standard_Integer i, aNbE, iErr, j, aNbEE, aNbToAdd;
44   TopTools_IndexedDataMapOfShapeListOfShape aMEE, aMSDE, aMEToAdd;
45   //
46   iErr=1;
47   //
48   aNbE=aMPKLE.Extent();
49   for (i=1; i<=aNbE; ++i) {
50     TopTools_ListOfShape& aLSDE=aMPKLE.ChangeFromIndex(i);
51     aMEE.Clear();
52     //
53     iErr=GEOMAlgo_Tools::FindSDShapes(aLSDE, aTol, aMEE, aCtx);
54     if (iErr) {
55       return iErr;
56     }
57     //
58     aNbEE=aMEE.Extent();
59     if (aNbEE==1) {
60       continue;  // nothing to do 
61     }
62     //
63     for (j=1; j<=aNbEE; ++j) {
64       TopTools_ListOfShape& aLEE=aMEE.ChangeFromIndex(j);
65       //
66       if (j==1) {
67         aLSDE.Clear();
68         aLSDE.Append(aLEE);
69       }
70       else {
71         const TopoDS_Shape& aE1=aLEE.First();
72         aMEToAdd.Add(aE1, aLEE);
73       }
74     }
75   }
76   //
77   aNbToAdd=aMEToAdd.Extent();
78   if (!aNbToAdd) {
79     return aNbToAdd;
80   }
81   //
82   for (i=1; i<=aNbToAdd; ++i) {
83     GEOMAlgo_PassKey aPKE1;
84     //
85     const TopoDS_Shape& aE1=aMEToAdd.FindKey(i);
86     const TopTools_ListOfShape& aLE=aMEToAdd(i);
87     //
88     aPKE1.SetIds(aE1);
89     aMPKLE.Add(aPKE1, aLE);
90   }
91   //
92   return 0;
93 }
94 //=======================================================================
95 //function : FindSDShapes
96 //purpose  : 
97 //=======================================================================
98 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopTools_ListOfShape& aLE,
99                                               const Standard_Real aTol,
100                                               TopTools_IndexedDataMapOfShapeListOfShape& aMEE,
101                                               IntTools_Context& aCtx)
102 {
103   Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
104   TopTools_ListOfShape aLESD;
105   TopTools_ListIteratorOfListOfShape aIt, aIt1;
106   TopTools_IndexedMapOfShape aMProcessed;
107   //
108   aNbE=aLE.Extent();
109   if (!aNbE) {
110     return 3; // Err
111   }
112   //
113   while(1) {
114     aNbEProcessed=aMProcessed.Extent();
115     if (aNbEProcessed==aNbE) {
116       break;
117     }
118     //
119     aIt.Initialize(aLE);
120     for (; aIt.More(); aIt.Next()) {
121       const TopoDS_Shape& aS=aIt.Value();
122       if (aMProcessed.Contains(aS)) {
123         continue;
124       }
125       //
126       aLESD.Clear();
127       iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
128       if (iErr) {
129         return 2; // Err
130       }
131       //
132       aNbESD=aLESD.Extent();
133       if (!aNbESD) {
134         return 1; // Err
135       }
136       //
137       aMEE.Add(aS, aLESD);
138       //
139       aIt1.Initialize(aLESD);
140       for (; aIt1.More(); aIt1.Next()) {
141         const TopoDS_Shape& aE1=aIt1.Value();
142         aMProcessed.Add(aE1);
143       }
144     }
145   }
146   return 0;
147 }
148 //=======================================================================
149 //function : FindSDShapes
150 //purpose  : 
151 //=======================================================================
152 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopoDS_Shape& aE1,
153                                               const TopTools_ListOfShape& aLE,
154                                               const Standard_Real aTol,
155                                               TopTools_ListOfShape& aLESD,
156                                               IntTools_Context& aCtx)
157 {
158   Standard_Boolean bIsDone;
159   Standard_Real aTol2, aD2;
160   gp_Pnt aP1, aP2;
161   TopTools_ListIteratorOfListOfShape aIt;
162   //
163   aTol2=aTol*aTol;
164   GEOMAlgo_Tools::PointOnShape(aE1, aP1);
165   //
166   aIt.Initialize(aLE);
167   for (; aIt.More(); aIt.Next()) {
168     const TopoDS_Shape& aE2=aIt.Value();
169     if (aE2.IsSame(aE1)) {
170        aLESD.Append(aE2);
171     }
172     else {
173       bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
174       if (!bIsDone) {
175         return 1; 
176       }
177       aD2=aP1.SquareDistance(aP2);
178       if(aD2<aTol2) {
179         aLESD.Append(aE2);
180       }
181     }
182   }
183   return 0;
184 }
185
186 //=======================================================================
187 //function : ProjectPointOnShape
188 //purpose  : 
189 //=======================================================================
190 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
191                                                      const TopoDS_Shape& aS,
192                                                      gp_Pnt& aP2,
193                                                      IntTools_Context& aCtx)
194 {
195   Standard_Boolean bIsDone=Standard_False;
196   Standard_Real aT2;
197   TopAbs_ShapeEnum aType;
198   //
199   aType=aS.ShapeType();
200   switch(aType) {
201     case TopAbs_EDGE: {
202       const TopoDS_Edge& aE2=TopoDS::Edge(aS);
203       //
204       bIsDone=aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
205       if (!bIsDone) {
206         return bIsDone;
207       }
208       //
209       GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
210     }
211       break;
212       //
213     case TopAbs_FACE: {
214       const TopoDS_Face& aF2=TopoDS::Face(aS);
215       GeomAPI_ProjectPointOnSurf& aProj=aCtx.ProjPS(aF2);
216       //
217       aProj.Perform(aP1);
218       bIsDone=aProj.IsDone();
219       if (!bIsDone) {
220         return bIsDone;
221       }
222       //
223       aP2=aProj.NearestPoint(); 
224     }
225       break;
226       //  
227     default:
228       break; // Err
229   }
230   return bIsDone;
231 }
232 //=======================================================================
233 //function : PointOnShape
234 //purpose  : 
235 //=======================================================================
236 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
237                                   gp_Pnt& aP3D)
238 {
239   TopAbs_ShapeEnum aType;
240   //
241   aP3D.SetCoord(99.,99.,99.);
242   aType=aS.ShapeType();
243   switch(aType) {
244     case TopAbs_EDGE: {
245       const TopoDS_Edge& aE=TopoDS::Edge(aS);
246       GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
247       }
248       break;
249       //
250     case TopAbs_FACE: {
251       const TopoDS_Face& aF=TopoDS::Face(aS);
252       GEOMAlgo_Tools::PointOnFace(aF, aP3D);
253       }
254       break;
255       //  
256     default:
257       break; // Err
258   }
259 }
260 //=======================================================================
261 //function : PointOnFace
262 //purpose  : 
263 //=======================================================================
264 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
265                                  gp_Pnt& aP3D)
266 {
267   Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
268   //
269   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
270   //
271   aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax); 
272   aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax); 
273   //
274   GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
275 }
276 //=======================================================================
277 //function : PointOnFace
278 //purpose  : 
279 //=======================================================================
280 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
281                                  const Standard_Real aU,
282                                  const Standard_Real aV,
283                                  gp_Pnt& aP3D)
284 {
285   Handle(Geom_Surface) aS;
286   //
287   aS=BRep_Tool::Surface(aF);
288   aS->D0(aU, aV, aP3D);
289 }
290 //=======================================================================
291 //function : PointOnEdge
292 //purpose  : 
293 //=======================================================================
294 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
295                                  gp_Pnt& aP3D)
296 {
297   Standard_Real aTx, aT1, aT2;
298   //
299   BRep_Tool::Curve(aE, aT1, aT2);
300   aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
301   GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
302 }
303 //=======================================================================
304 //function : PointOnEdge
305 //purpose  : 
306 //=======================================================================
307 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
308                                  const Standard_Real aT,
309                                  gp_Pnt& aP3D)
310 {
311   Standard_Real aT1, aT2;
312   Handle(Geom_Curve) aC3D;
313   //
314   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
315   aC3D->D0(aT, aP3D);
316 }
317 /*
318 //=======================================================================
319 //function : FindSDEdges
320 //purpose  : 
321 //=======================================================================
322 Standard_Integer GEOMAlgo_Tools::FindSDEdges(const TopoDS_Edge& aE1,
323                                              const TopTools_ListOfShape& aLE,
324                                              const Standard_Real aTol,
325                                              TopTools_ListOfShape& aLESD,
326                                              IntTools_Context& aCtx)
327 {
328   Standard_Boolean bIsDone;
329   Standard_Real aT2, aTol2, aD2;
330   gp_Pnt aP1, aP2;
331   TopTools_ListIteratorOfListOfShape aIt;
332   //
333   aTol2=aTol*aTol;
334   //
335   GEOMAlgo_Tools::PointOnEdge(aE1, aP1);
336   aIt.Initialize(aLE);
337   for (; aIt.More(); aIt.Next()) {
338     const TopoDS_Edge& aE2=TopoDS::Edge(aIt.Value());
339     if (aE2.IsSame(aE1)) {
340        aLESD.Append(aE2);
341     }
342     else {
343       bIsDone=aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
344       if (!bIsDone) {
345         return 1; 
346       }
347       GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
348       aD2=aP1.SquareDistance(aP2);
349       if(aD2<aTol2) {
350         aLESD.Append(aE2);
351       }
352     }
353   }
354   return 0;
355 }
356 //=======================================================================
357 //function : FindSDFaces
358 //purpose  : 
359 //=======================================================================
360 Standard_Integer GEOMAlgo_Tools::FindSDFaces(const TopoDS_Face& aF1,
361                                              const TopTools_ListOfShape& aLF,
362                                              const Standard_Real aTol,
363                                              TopTools_ListOfShape& aLFSD,
364                                              IntTools_Context& aCtx)
365 {
366   Standard_Boolean bIsDone;
367   Standard_Real aTol2, aD2;
368   gp_Pnt aP1, aP2;
369   TopTools_ListIteratorOfListOfShape aIt;
370   //
371   aTol2=aTol*aTol;
372   //
373   GEOMAlgo_Tools::PointOnFace(aF1, aP1);
374   aIt.Initialize(aLF);
375   for (; aIt.More(); aIt.Next()) {
376     const TopoDS_Face& aF2=TopoDS::Face(aIt.Value());
377     if (aF2.IsSame(aF1)) {
378        aLFSD.Append(aF2);
379     }
380     else {
381       GeomAPI_ProjectPointOnSurf& aProj=aCtx.ProjPS(aF2);
382       aProj.Perform(aP1);
383       bIsDone=aProj.IsDone();
384       if (!bIsDone) {
385         return 1; //??
386       }
387       //
388       aP2=aProj.NearestPoint();
389       aD2=aP1.SquareDistance(aP2);
390       if(aD2<aTol2) {
391         aLFSD.Append(aF2);
392       }
393     }
394   }
395   return 0;
396 }
397 */