Salome HOME
Preparation of intermediate revision
[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         continue; // jfa BUG 20361
271       }
272       aD2=aP1.SquareDistance(aP2);
273       if(aD2<aTol2) {
274         aLESD.Append(aE2);
275       }
276     }
277   }
278   return 0;
279 }
280
281 //=======================================================================
282 //function : ProjectPointOnShape
283 //purpose  : 
284 //=======================================================================
285 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
286                                                      const TopoDS_Shape& aS,
287                                                      gp_Pnt& aP2,
288                                                      IntTools_Context& aCtx)
289 {
290   Standard_Boolean bIsDone = Standard_False;
291   Standard_Real aT2;
292   TopAbs_ShapeEnum aType;
293   //
294   aType = aS.ShapeType();
295   switch (aType)
296     {
297     case TopAbs_EDGE:
298       {
299         const TopoDS_Edge& aE2 = TopoDS::Edge(aS);
300         //
301         if (BRep_Tool::Degenerated(aE2)) { // jfa
302           return Standard_True;
303         }
304         else {
305           Standard_Real f, l;
306           Handle(Geom_Curve) aC3D = BRep_Tool::Curve (aE2, f, l);
307           if (aC3D.IsNull()) {
308             return Standard_True;
309           }
310           bIsDone = aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
311         }
312         if (!bIsDone) {
313           return bIsDone;
314         }
315         //
316         GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
317       }
318       break;
319       //
320     case TopAbs_FACE:
321       {
322         const TopoDS_Face& aF2 = TopoDS::Face(aS);
323         GeomAPI_ProjectPointOnSurf& aProj = aCtx.ProjPS(aF2);
324         //
325         aProj.Perform(aP1);
326         bIsDone = aProj.IsDone();
327         if (!bIsDone) {
328           return bIsDone;
329         }
330         //
331         aP2 = aProj.NearestPoint(); 
332       }
333       break;
334       //  
335     default:
336       break; // Err
337     }
338   return bIsDone;
339 }
340 //=======================================================================
341 //function : PointOnShape
342 //purpose  : 
343 //=======================================================================
344 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
345                                   gp_Pnt& aP3D)
346 {
347   TopAbs_ShapeEnum aType;
348   //
349   aP3D.SetCoord(99.,99.,99.);
350   aType=aS.ShapeType();
351   switch(aType) {
352     case TopAbs_EDGE: {
353       const TopoDS_Edge& aE=TopoDS::Edge(aS);
354       GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
355       }
356       break;
357       //
358     case TopAbs_FACE: {
359       const TopoDS_Face& aF=TopoDS::Face(aS);
360       GEOMAlgo_Tools::PointOnFace(aF, aP3D);
361       }
362       break;
363       //  
364     default:
365       break; // Err
366   }
367 }
368 //=======================================================================
369 //function : PointOnFace
370 //purpose  : 
371 //=======================================================================
372 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
373                                  gp_Pnt& aP3D)
374 {
375   Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
376   //
377   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
378   //
379   aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax); 
380   aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax); 
381   //
382   GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
383 }
384 //=======================================================================
385 //function : PointOnFace
386 //purpose  : 
387 //=======================================================================
388 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
389                                  const Standard_Real aU,
390                                  const Standard_Real aV,
391                                  gp_Pnt& aP3D)
392 {
393   Handle(Geom_Surface) aS;
394   //
395   aS=BRep_Tool::Surface(aF);
396   aS->D0(aU, aV, aP3D);
397 }
398 //=======================================================================
399 //function : PointOnEdge
400 //purpose  : 
401 //=======================================================================
402 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
403                                  gp_Pnt& aP3D)
404 {
405   Standard_Real aTx, aT1, aT2;
406   //
407   BRep_Tool::Curve(aE, aT1, aT2);
408   aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
409   GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
410 }
411 //=======================================================================
412 //function : PointOnEdge
413 //purpose  : 
414 //=======================================================================
415 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
416                                  const Standard_Real aT,
417                                  gp_Pnt& aP3D)
418 {
419   Standard_Real aT1, aT2;
420   Handle(Geom_Curve) aC3D;
421   //
422   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
423   aC3D->D0(aT, aP3D);
424 }
425 //=======================================================================
426 //function : RefinePCurveForEdgeOnFace
427 //purpose  : 
428 //=======================================================================
429 void GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(const TopoDS_Edge& aE,
430                                                const TopoDS_Face& aF,
431                                                const Standard_Real aUMin, 
432                                                const Standard_Real aUMax) 
433 {
434   Standard_Real aT1, aT2, aTx, aUx, aTol, aTwoPI;
435   gp_Pnt2d aP2D;
436   Handle(Geom_Surface) aS;
437   Handle(Geom2d_Curve) aC2D;
438   BRep_Builder aBB;
439   //
440   aTwoPI=PI+PI;
441   //
442   aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
443   if (!aC2D.IsNull()) {
444     if (BRep_Tool::IsClosed(aE, aF)) {
445       return;
446     }
447     aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
448     aC2D->D0(aTx, aP2D);
449     aUx=aP2D.X();
450     if (aUx < aUMin || aUx > aUMax) {
451       // need to rebuild
452       Handle(Geom2d_Curve) aC2Dx;
453       //
454       aTol=BRep_Tool::Tolerance(aE);
455       aBB.UpdateEdge(aE, aC2Dx, aF, aTol); 
456     }
457   }
458 }
459 //=======================================================================
460 //function : IsUPeriodic
461 //purpose  : 
462 //=======================================================================
463 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const  Handle(Geom_Surface) &aS)
464 {
465   Standard_Boolean bRet;
466   GeomAbs_SurfaceType aType;
467   GeomAdaptor_Surface aGAS;
468   //
469   aGAS.Load(aS);
470   aType=aGAS.GetType();
471   bRet=(aType==GeomAbs_Cylinder||
472         aType==GeomAbs_Cone ||
473         aType==GeomAbs_Sphere);
474   //
475   return bRet;
476 }