Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[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>//qft
56 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>//qft
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     //qf
160     //aPKE1.SetIds(aE1);
161     aPKE1.SetShapes(aE1);
162     //qt
163     aMPKLE.Add(aPKE1, aLE);
164   }
165   //
166   return 0;
167 }
168 //=======================================================================
169 //function : FindSDShapes
170 //purpose  : 
171 //=======================================================================
172 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopTools_ListOfShape& aLE,
173                                               const Standard_Real aTol,
174                                               TopTools_IndexedDataMapOfShapeListOfShape& aMEE,
175                                               IntTools_Context& aCtx)
176 {
177   Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
178   TopTools_ListOfShape aLESD;
179   TopTools_ListIteratorOfListOfShape aIt, aIt1;
180   TopTools_IndexedMapOfShape aMProcessed;
181   TopAbs_ShapeEnum aType;
182   //
183   aNbE=aLE.Extent();
184   if (!aNbE) {
185     return 3; // Err
186   } 
187   //modified by NIZNHY-PKV Thu Dec 30 10:56:52 2004 f
188   if (aNbE==1) {
189     return 0; // Nothing to do
190   } 
191   //modified by NIZNHY-PKV Thu Dec 30 10:56:56 2004 t
192   //
193   while(1) {
194     aNbEProcessed=aMProcessed.Extent();
195     if (aNbEProcessed==aNbE) {
196       break;
197     }
198     //
199     aIt.Initialize(aLE);
200     for (; aIt.More(); aIt.Next()) {
201       const TopoDS_Shape& aS=aIt.Value();
202       //
203       if (aMProcessed.Contains(aS)) {
204         continue;
205       }
206       //
207       //modified by NIZNHY-PKV Thu Dec 30 10:57:01 2004 f
208       aType=aS.ShapeType();
209       if (aType==TopAbs_EDGE) {
210         const TopoDS_Edge& aE=TopoDS::Edge(aS);
211         if (BRep_Tool::Degenerated(aE)) {
212           aMProcessed.Add(aE);
213           continue;
214         }
215       }
216       //modified by NIZNHY-PKV Thu Dec 30 10:57:03 2004 t
217       //
218       aLESD.Clear();
219       iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
220       if (iErr) {
221         return 2; // Err
222       }
223       //
224       aNbESD=aLESD.Extent();
225       if (!aNbESD) {
226         return 1; // Err
227       }
228       //
229       aMEE.Add(aS, aLESD);
230       //
231       aIt1.Initialize(aLESD);
232       for (; aIt1.More(); aIt1.Next()) {
233         const TopoDS_Shape& aE1=aIt1.Value();
234         aMProcessed.Add(aE1);
235       }
236     }
237   }
238   return 0;
239 }
240 //=======================================================================
241 //function : FindSDShapes
242 //purpose  : 
243 //=======================================================================
244 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopoDS_Shape& aE1,
245                                               const TopTools_ListOfShape& aLE,
246                                               const Standard_Real aTol,
247                                               TopTools_ListOfShape& aLESD,
248                                               IntTools_Context& aCtx)
249 {
250   Standard_Boolean bIsDone;
251   Standard_Real aTol2, aD2;
252   gp_Pnt aP1, aP2;
253   TopTools_ListIteratorOfListOfShape aIt;
254   //
255   aTol2=aTol*aTol;
256   GEOMAlgo_Tools::PointOnShape(aE1, aP1);
257   //
258   aIt.Initialize(aLE);
259   for (; aIt.More(); aIt.Next()) {
260     const TopoDS_Shape& aE2=aIt.Value();
261     if (aE2.IsSame(aE1)) {
262        aLESD.Append(aE2);
263     }
264     else {
265       bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
266       if (!bIsDone) {
267         return 1; 
268       }
269       aD2=aP1.SquareDistance(aP2);
270       if(aD2<aTol2) {
271         aLESD.Append(aE2);
272       }
273     }
274   }
275   return 0;
276 }
277
278 //=======================================================================
279 //function : ProjectPointOnShape
280 //purpose  : 
281 //=======================================================================
282 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
283                                                      const TopoDS_Shape& aS,
284                                                      gp_Pnt& aP2,
285                                                      IntTools_Context& aCtx)
286 {
287   Standard_Boolean bIsDone=Standard_False;
288   Standard_Real aT2;
289   TopAbs_ShapeEnum aType;
290   //
291   aType=aS.ShapeType();
292   switch(aType) {
293     case TopAbs_EDGE: {
294       const TopoDS_Edge& aE2=TopoDS::Edge(aS);
295       //
296       bIsDone=aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
297       if (!bIsDone) {
298         return bIsDone;
299       }
300       //
301       GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
302     }
303       break;
304       //
305     case TopAbs_FACE: {
306       const TopoDS_Face& aF2=TopoDS::Face(aS);
307       GeomAPI_ProjectPointOnSurf& aProj=aCtx.ProjPS(aF2);
308       //
309       aProj.Perform(aP1);
310       bIsDone=aProj.IsDone();
311       if (!bIsDone) {
312         return bIsDone;
313       }
314       //
315       aP2=aProj.NearestPoint(); 
316     }
317       break;
318       //  
319     default:
320       break; // Err
321   }
322   return bIsDone;
323 }
324 //=======================================================================
325 //function : PointOnShape
326 //purpose  : 
327 //=======================================================================
328 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
329                                   gp_Pnt& aP3D)
330 {
331   TopAbs_ShapeEnum aType;
332   //
333   aP3D.SetCoord(99.,99.,99.);
334   aType=aS.ShapeType();
335   switch(aType) {
336     case TopAbs_EDGE: {
337       const TopoDS_Edge& aE=TopoDS::Edge(aS);
338       GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
339       }
340       break;
341       //
342     case TopAbs_FACE: {
343       const TopoDS_Face& aF=TopoDS::Face(aS);
344       GEOMAlgo_Tools::PointOnFace(aF, aP3D);
345       }
346       break;
347       //  
348     default:
349       break; // Err
350   }
351 }
352 //=======================================================================
353 //function : PointOnFace
354 //purpose  : 
355 //=======================================================================
356 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
357                                  gp_Pnt& aP3D)
358 {
359   Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
360   //
361   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
362   //
363   aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax); 
364   aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax); 
365   //
366   GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
367 }
368 //=======================================================================
369 //function : PointOnFace
370 //purpose  : 
371 //=======================================================================
372 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
373                                  const Standard_Real aU,
374                                  const Standard_Real aV,
375                                  gp_Pnt& aP3D)
376 {
377   Handle(Geom_Surface) aS;
378   //
379   aS=BRep_Tool::Surface(aF);
380   aS->D0(aU, aV, aP3D);
381 }
382 //=======================================================================
383 //function : PointOnEdge
384 //purpose  : 
385 //=======================================================================
386 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
387                                  gp_Pnt& aP3D)
388 {
389   Standard_Real aTx, aT1, aT2;
390   //
391   BRep_Tool::Curve(aE, aT1, aT2);
392   aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
393   GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
394 }
395 //=======================================================================
396 //function : PointOnEdge
397 //purpose  : 
398 //=======================================================================
399 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
400                                  const Standard_Real aT,
401                                  gp_Pnt& aP3D)
402 {
403   Standard_Real aT1, aT2;
404   Handle(Geom_Curve) aC3D;
405   //
406   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
407   aC3D->D0(aT, aP3D);
408 }
409 //=======================================================================
410 //function : RefinePCurveForEdgeOnFace
411 //purpose  : 
412 //=======================================================================
413 void GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(const TopoDS_Edge& aE,
414                                                const TopoDS_Face& aF,
415                                                const Standard_Real aUMin, 
416                                                const Standard_Real aUMax) 
417 {
418   Standard_Real aT1, aT2, aTx, aUx, aTol, aTwoPI;
419   gp_Pnt2d aP2D;
420   Handle(Geom_Surface) aS;
421   Handle(Geom2d_Curve) aC2D;
422   BRep_Builder aBB;
423   //
424   aTwoPI=PI+PI;
425   //
426   aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
427   if (!aC2D.IsNull()) {
428     if (BRep_Tool::IsClosed(aE, aF)) {
429       return;
430     }
431     aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
432     aC2D->D0(aTx, aP2D);
433     aUx=aP2D.X();
434     if (aUx < aUMin || aUx > aUMax) {
435       // need to rebuild
436       Handle(Geom2d_Curve) aC2Dx;
437       //
438       aTol=BRep_Tool::Tolerance(aE);
439       aBB.UpdateEdge(aE, aC2Dx, aF, aTol); 
440     }
441   }
442 }
443 //=======================================================================
444 //function : IsUPeriodic
445 //purpose  : 
446 //=======================================================================
447 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const  Handle(Geom_Surface) &aS)
448 {
449   Standard_Boolean bRet;
450   GeomAbs_SurfaceType aType;
451   GeomAdaptor_Surface aGAS;
452   //
453   aGAS.Load(aS);
454   aType=aGAS.GetType();
455   bRet=(aType==GeomAbs_Cylinder||
456         aType==GeomAbs_Cone ||
457         aType==GeomAbs_Sphere);
458   //
459   return bRet;
460 }