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