Salome HOME
Bug 0020052: EDF 867 GEOM: Non removable extra edges are created with fuse operation...
[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     case TopAbs_EDGE: {
296       const TopoDS_Edge& aE2=TopoDS::Edge(aS);
297       //
298       bIsDone=aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
299       if (!bIsDone) {
300         return bIsDone;
301       }
302       //
303       GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
304     }
305       break;
306       //
307     case TopAbs_FACE: {
308       const TopoDS_Face& aF2=TopoDS::Face(aS);
309       GeomAPI_ProjectPointOnSurf& aProj=aCtx.ProjPS(aF2);
310       //
311       aProj.Perform(aP1);
312       bIsDone=aProj.IsDone();
313       if (!bIsDone) {
314         return bIsDone;
315       }
316       //
317       aP2=aProj.NearestPoint(); 
318     }
319       break;
320       //  
321     default:
322       break; // Err
323   }
324   return bIsDone;
325 }
326 //=======================================================================
327 //function : PointOnShape
328 //purpose  : 
329 //=======================================================================
330 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
331                                   gp_Pnt& aP3D)
332 {
333   TopAbs_ShapeEnum aType;
334   //
335   aP3D.SetCoord(99.,99.,99.);
336   aType=aS.ShapeType();
337   switch(aType) {
338     case TopAbs_EDGE: {
339       const TopoDS_Edge& aE=TopoDS::Edge(aS);
340       GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
341       }
342       break;
343       //
344     case TopAbs_FACE: {
345       const TopoDS_Face& aF=TopoDS::Face(aS);
346       GEOMAlgo_Tools::PointOnFace(aF, aP3D);
347       }
348       break;
349       //  
350     default:
351       break; // Err
352   }
353 }
354 //=======================================================================
355 //function : PointOnFace
356 //purpose  : 
357 //=======================================================================
358 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
359                                  gp_Pnt& aP3D)
360 {
361   Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
362   //
363   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
364   //
365   aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax); 
366   aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax); 
367   //
368   GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
369 }
370 //=======================================================================
371 //function : PointOnFace
372 //purpose  : 
373 //=======================================================================
374 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
375                                  const Standard_Real aU,
376                                  const Standard_Real aV,
377                                  gp_Pnt& aP3D)
378 {
379   Handle(Geom_Surface) aS;
380   //
381   aS=BRep_Tool::Surface(aF);
382   aS->D0(aU, aV, aP3D);
383 }
384 //=======================================================================
385 //function : PointOnEdge
386 //purpose  : 
387 //=======================================================================
388 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
389                                  gp_Pnt& aP3D)
390 {
391   Standard_Real aTx, aT1, aT2;
392   //
393   BRep_Tool::Curve(aE, aT1, aT2);
394   aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
395   GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
396 }
397 //=======================================================================
398 //function : PointOnEdge
399 //purpose  : 
400 //=======================================================================
401 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
402                                  const Standard_Real aT,
403                                  gp_Pnt& aP3D)
404 {
405   Standard_Real aT1, aT2;
406   Handle(Geom_Curve) aC3D;
407   //
408   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
409   aC3D->D0(aT, aP3D);
410 }
411 //=======================================================================
412 //function : RefinePCurveForEdgeOnFace
413 //purpose  : 
414 //=======================================================================
415 void GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(const TopoDS_Edge& aE,
416                                                const TopoDS_Face& aF,
417                                                const Standard_Real aUMin, 
418                                                const Standard_Real aUMax) 
419 {
420   Standard_Real aT1, aT2, aTx, aUx, aTol, aTwoPI;
421   gp_Pnt2d aP2D;
422   Handle(Geom_Surface) aS;
423   Handle(Geom2d_Curve) aC2D;
424   BRep_Builder aBB;
425   //
426   aTwoPI=PI+PI;
427   //
428   aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
429   if (!aC2D.IsNull()) {
430     if (BRep_Tool::IsClosed(aE, aF)) {
431       return;
432     }
433     aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
434     aC2D->D0(aTx, aP2D);
435     aUx=aP2D.X();
436     if (aUx < aUMin || aUx > aUMax) {
437       // need to rebuild
438       Handle(Geom2d_Curve) aC2Dx;
439       //
440       aTol=BRep_Tool::Tolerance(aE);
441       aBB.UpdateEdge(aE, aC2Dx, aF, aTol); 
442     }
443   }
444 }
445 //=======================================================================
446 //function : IsUPeriodic
447 //purpose  : 
448 //=======================================================================
449 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const  Handle(Geom_Surface) &aS)
450 {
451   Standard_Boolean bRet;
452   GeomAbs_SurfaceType aType;
453   GeomAdaptor_Surface aGAS;
454   //
455   aGAS.Load(aS);
456   aType=aGAS.GetType();
457   bRet=(aType==GeomAbs_Cylinder||
458         aType==GeomAbs_Cone ||
459         aType==GeomAbs_Sphere);
460   //
461   return bRet;
462 }