Salome HOME
Update mail address
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Tools.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        GEOMAlgo_Tools.cxx
21 // Created:     Mon Dec  6 11:35:29 2004
22 // Author:      Peter KURNEV
23 //              <pkv@irinox>
24
25 #include <GEOMAlgo_Tools.ixx>
26
27 #include <gp_Pnt.hxx>
28 #include <gp_Pnt2d.hxx>
29
30 #include <Geom_Surface.hxx>
31 #include <Geom_Curve.hxx>
32 #include <Geom2d_Curve.hxx>
33 #include <GeomAdaptor_Surface.hxx>
34
35 #include <GeomAPI_ProjectPointOnSurf.hxx>
36
37 #include <TopAbs_ShapeEnum.hxx>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Iterator.hxx>
43
44 #include <TopTools_ListOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47
48 #include <BRep_Tool.hxx>
49 #include <BRep_Builder.hxx>
50 #include <BRepTools.hxx>
51
52 #include <BOPTools_Tools2D.hxx>
53 #include <IntTools_Context.hxx>
54
55 #include <GEOMAlgo_PassKeyShape.hxx>
56 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
57
58 static 
59   void GetCount(const TopoDS_Shape& aS,
60                 Standard_Integer& iCnt);
61
62 //=======================================================================
63 //function : IsCompositeShape
64 //purpose  : 
65 //=======================================================================
66 Standard_Boolean GEOMAlgo_Tools::IsCompositeShape(const TopoDS_Shape& aS)
67 {
68   Standard_Boolean bRet;
69   Standard_Integer iCnt;
70   TopoDS_Iterator aIt;
71   //
72   iCnt=0;
73   GetCount(aS, iCnt);
74   bRet=(iCnt>1);
75   //
76   return bRet;
77 }
78
79 //=======================================================================
80 //function : GetCount
81 //purpose  : 
82 //=======================================================================
83 void GetCount(const TopoDS_Shape& aS,
84               Standard_Integer& iCnt)
85 {
86   TopoDS_Iterator aIt;
87   TopAbs_ShapeEnum aTS;
88   //
89   aTS=aS.ShapeType();
90   //
91   if (aTS==TopAbs_SHAPE) {
92     return;
93   }
94   if (aTS!=TopAbs_COMPOUND) {
95     ++iCnt;
96     return;
97   }
98   //
99   aIt.Initialize(aS);
100   for (; aIt.More(); aIt.Next()) {
101     const TopoDS_Shape& aSx=aIt.Value();
102     GetCount(aSx, iCnt); 
103   }
104 }
105
106 //=======================================================================
107 //function : RefineSDShapes
108 //purpose  : 
109 //=======================================================================
110 Standard_Integer GEOMAlgo_Tools::RefineSDShapes(GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape &aMPKLE,
111                                                 const Standard_Real aTol,
112                                                 IntTools_Context& aCtx)
113 {
114   Standard_Integer i, aNbE, iErr, j, aNbEE, aNbToAdd;
115   TopTools_IndexedDataMapOfShapeListOfShape aMEE, aMSDE, aMEToAdd;
116   //
117   iErr=1;
118   //
119   aNbE=aMPKLE.Extent();
120   for (i=1; i<=aNbE; ++i) {
121     TopTools_ListOfShape& aLSDE=aMPKLE.ChangeFromIndex(i);
122     //
123     aMEE.Clear();
124     iErr=GEOMAlgo_Tools::FindSDShapes(aLSDE, aTol, aMEE, aCtx);
125     if (iErr) {
126       return iErr;
127     }
128     //
129     aNbEE=aMEE.Extent();
130     if (aNbEE==1) {
131       continue;  // nothing to do 
132     }
133     //
134     for (j=1; j<=aNbEE; ++j) {
135       TopTools_ListOfShape& aLEE=aMEE.ChangeFromIndex(j);
136       //
137       if (j==1) {
138         aLSDE.Clear();
139         aLSDE.Append(aLEE);
140       }
141       else {
142         const TopoDS_Shape& aE1=aLEE.First();
143         aMEToAdd.Add(aE1, aLEE);
144       }
145     }
146   }
147   //
148   aNbToAdd=aMEToAdd.Extent();
149   if (!aNbToAdd) {
150     return aNbToAdd;
151   }
152   //
153   for (i=1; i<=aNbToAdd; ++i) {
154     GEOMAlgo_PassKeyShape aPKE1;
155     //
156     const TopoDS_Shape& aE1=aMEToAdd.FindKey(i);
157     const TopTools_ListOfShape& aLE=aMEToAdd(i);
158     //
159     aPKE1.SetIds(aE1);
160     aMPKLE.Add(aPKE1, aLE);
161   }
162   //
163   return 0;
164 }
165 //=======================================================================
166 //function : FindSDShapes
167 //purpose  : 
168 //=======================================================================
169 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopTools_ListOfShape& aLE,
170                                               const Standard_Real aTol,
171                                               TopTools_IndexedDataMapOfShapeListOfShape& aMEE,
172                                               IntTools_Context& aCtx)
173 {
174   Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
175   TopTools_ListOfShape aLESD;
176   TopTools_ListIteratorOfListOfShape aIt, aIt1;
177   TopTools_IndexedMapOfShape aMProcessed;
178   TopAbs_ShapeEnum aType;
179   //
180   aNbE=aLE.Extent();
181   if (!aNbE) {
182     return 3; // Err
183   } 
184   //modified by NIZNHY-PKV Thu Dec 30 10:56:52 2004 f
185   if (aNbE==1) {
186     return 0; // Nothing to do
187   } 
188   //modified by NIZNHY-PKV Thu Dec 30 10:56:56 2004 t
189   //
190   while(1) {
191     aNbEProcessed=aMProcessed.Extent();
192     if (aNbEProcessed==aNbE) {
193       break;
194     }
195     //
196     aIt.Initialize(aLE);
197     for (; aIt.More(); aIt.Next()) {
198       const TopoDS_Shape& aS=aIt.Value();
199       //
200       if (aMProcessed.Contains(aS)) {
201         continue;
202       }
203       //
204       //modified by NIZNHY-PKV Thu Dec 30 10:57:01 2004 f
205       aType=aS.ShapeType();
206       if (aType==TopAbs_EDGE) {
207         const TopoDS_Edge& aE=TopoDS::Edge(aS);
208         if (BRep_Tool::Degenerated(aE)) {
209           aMProcessed.Add(aE);
210           continue;
211         }
212       }
213       //modified by NIZNHY-PKV Thu Dec 30 10:57:03 2004 t
214       //
215       aLESD.Clear();
216       iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
217       if (iErr) {
218         return 2; // Err
219       }
220       //
221       aNbESD=aLESD.Extent();
222       if (!aNbESD) {
223         return 1; // Err
224       }
225       //
226       aMEE.Add(aS, aLESD);
227       //
228       aIt1.Initialize(aLESD);
229       for (; aIt1.More(); aIt1.Next()) {
230         const TopoDS_Shape& aE1=aIt1.Value();
231         aMProcessed.Add(aE1);
232       }
233     }
234   }
235   return 0;
236 }
237 //=======================================================================
238 //function : FindSDShapes
239 //purpose  : 
240 //=======================================================================
241 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopoDS_Shape& aE1,
242                                               const TopTools_ListOfShape& aLE,
243                                               const Standard_Real aTol,
244                                               TopTools_ListOfShape& aLESD,
245                                               IntTools_Context& aCtx)
246 {
247   Standard_Boolean bIsDone;
248   Standard_Real aTol2, aD2;
249   gp_Pnt aP1, aP2;
250   TopTools_ListIteratorOfListOfShape aIt;
251   //
252   aTol2=aTol*aTol;
253   GEOMAlgo_Tools::PointOnShape(aE1, aP1);
254   //
255   aIt.Initialize(aLE);
256   for (; aIt.More(); aIt.Next()) {
257     const TopoDS_Shape& aE2=aIt.Value();
258     if (aE2.IsSame(aE1)) {
259        aLESD.Append(aE2);
260     }
261     else {
262       bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
263       if (!bIsDone) {
264         return 1; 
265       }
266       aD2=aP1.SquareDistance(aP2);
267       if(aD2<aTol2) {
268         aLESD.Append(aE2);
269       }
270     }
271   }
272   return 0;
273 }
274
275 //=======================================================================
276 //function : ProjectPointOnShape
277 //purpose  : 
278 //=======================================================================
279 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
280                                                      const TopoDS_Shape& aS,
281                                                      gp_Pnt& aP2,
282                                                      IntTools_Context& aCtx)
283 {
284   Standard_Boolean bIsDone=Standard_False;
285   Standard_Real aT2;
286   TopAbs_ShapeEnum aType;
287   //
288   aType=aS.ShapeType();
289   switch(aType) {
290     case TopAbs_EDGE: {
291       const TopoDS_Edge& aE2=TopoDS::Edge(aS);
292       //
293       bIsDone=aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
294       if (!bIsDone) {
295         return bIsDone;
296       }
297       //
298       GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
299     }
300       break;
301       //
302     case TopAbs_FACE: {
303       const TopoDS_Face& aF2=TopoDS::Face(aS);
304       GeomAPI_ProjectPointOnSurf& aProj=aCtx.ProjPS(aF2);
305       //
306       aProj.Perform(aP1);
307       bIsDone=aProj.IsDone();
308       if (!bIsDone) {
309         return bIsDone;
310       }
311       //
312       aP2=aProj.NearestPoint(); 
313     }
314       break;
315       //  
316     default:
317       break; // Err
318   }
319   return bIsDone;
320 }
321 //=======================================================================
322 //function : PointOnShape
323 //purpose  : 
324 //=======================================================================
325 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
326                                   gp_Pnt& aP3D)
327 {
328   TopAbs_ShapeEnum aType;
329   //
330   aP3D.SetCoord(99.,99.,99.);
331   aType=aS.ShapeType();
332   switch(aType) {
333     case TopAbs_EDGE: {
334       const TopoDS_Edge& aE=TopoDS::Edge(aS);
335       GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
336       }
337       break;
338       //
339     case TopAbs_FACE: {
340       const TopoDS_Face& aF=TopoDS::Face(aS);
341       GEOMAlgo_Tools::PointOnFace(aF, aP3D);
342       }
343       break;
344       //  
345     default:
346       break; // Err
347   }
348 }
349 //=======================================================================
350 //function : PointOnFace
351 //purpose  : 
352 //=======================================================================
353 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
354                                  gp_Pnt& aP3D)
355 {
356   Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
357   //
358   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
359   //
360   aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax); 
361   aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax); 
362   //
363   GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
364 }
365 //=======================================================================
366 //function : PointOnFace
367 //purpose  : 
368 //=======================================================================
369 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
370                                  const Standard_Real aU,
371                                  const Standard_Real aV,
372                                  gp_Pnt& aP3D)
373 {
374   Handle(Geom_Surface) aS;
375   //
376   aS=BRep_Tool::Surface(aF);
377   aS->D0(aU, aV, aP3D);
378 }
379 //=======================================================================
380 //function : PointOnEdge
381 //purpose  : 
382 //=======================================================================
383 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
384                                  gp_Pnt& aP3D)
385 {
386   Standard_Real aTx, aT1, aT2;
387   //
388   BRep_Tool::Curve(aE, aT1, aT2);
389   aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
390   GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
391 }
392 //=======================================================================
393 //function : PointOnEdge
394 //purpose  : 
395 //=======================================================================
396 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
397                                  const Standard_Real aT,
398                                  gp_Pnt& aP3D)
399 {
400   Standard_Real aT1, aT2;
401   Handle(Geom_Curve) aC3D;
402   //
403   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
404   aC3D->D0(aT, aP3D);
405 }
406 //=======================================================================
407 //function : RefinePCurveForEdgeOnFace
408 //purpose  : 
409 //=======================================================================
410 void GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(const TopoDS_Edge& aE,
411                                                const TopoDS_Face& aF,
412                                                const Standard_Real aUMin, 
413                                                const Standard_Real aUMax) 
414 {
415   Standard_Real aT1, aT2, aTx, aUx, aTol, aTwoPI;
416   gp_Pnt2d aP2D;
417   Handle(Geom_Surface) aS;
418   Handle(Geom2d_Curve) aC2D;
419   BRep_Builder aBB;
420   //
421   aTwoPI=PI+PI;
422   //
423   aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
424   if (!aC2D.IsNull()) {
425     if (BRep_Tool::IsClosed(aE, aF)) {
426       return;
427     }
428     aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
429     aC2D->D0(aTx, aP2D);
430     aUx=aP2D.X();
431     if (aUx < aUMin || aUx > aUMax) {
432       // need to rebuild
433       Handle(Geom2d_Curve) aC2Dx;
434       //
435       aTol=BRep_Tool::Tolerance(aE);
436       aBB.UpdateEdge(aE, aC2Dx, aF, aTol); 
437     }
438   }
439 }
440 //=======================================================================
441 //function : IsUPeriodic
442 //purpose  : 
443 //=======================================================================
444 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const  Handle(Geom_Surface) &aS)
445 {
446   Standard_Boolean bRet;
447   GeomAbs_SurfaceType aType;
448   GeomAdaptor_Surface aGAS;
449   //
450   aGAS.Load(aS);
451   aType=aGAS.GetType();
452   bRet=(aType==GeomAbs_Cylinder||
453         aType==GeomAbs_Cone ||
454         aType==GeomAbs_Sphere);
455   //
456   return bRet;
457 }