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