Salome HOME
This commit was generated by cvs2git to create tag 'TRIPOLI_323'.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Tools.cxx
1 // Copyright (C) 2007-2012  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
27 #include <GEOMAlgo_Tools.hxx>
28
29 #include <gp.hxx>
30 #include <gp_Pnt.hxx>
31 #include <gp_Pnt2d.hxx>
32 #include <gp_Vec2d.hxx>
33 #include <gp_Dir2d.hxx>
34
35 #include <Geom2d_Curve.hxx>
36 #include <Geom2d_TrimmedCurve.hxx>
37
38 #include <Geom_Curve.hxx>
39 #include <Geom_Surface.hxx>
40
41 #include <GeomAdaptor_Surface.hxx>
42
43 #include <GeomAPI_ProjectPointOnSurf.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
45
46 #include <TopAbs_ShapeEnum.hxx>
47
48 #include <TopoDS.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Iterator.hxx>
52
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <TopTools_IndexedMapOfShape.hxx>
56
57 #include <BRep_Tool.hxx>
58 #include <BRep_Builder.hxx>
59 #include <BRepTools.hxx>
60
61 #include <BOPTools_Tools2D.hxx>
62 #include <IntTools_Context.hxx>
63
64 #include <GEOMAlgo_PassKeyShape.hxx>
65 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
66
67 static
68   void GetCount(const TopoDS_Shape& aS,
69                 Standard_Integer& iCnt);
70
71 //=======================================================================
72 //function : IsCompositeShape
73 //purpose  :
74 //=======================================================================
75 Standard_Boolean GEOMAlgo_Tools::IsCompositeShape(const TopoDS_Shape& aS)
76 {
77   Standard_Boolean bRet;
78   Standard_Integer iCnt;
79   TopoDS_Iterator aIt;
80   //
81   iCnt=0;
82   GetCount(aS, iCnt);
83   bRet=(iCnt>1);
84   //
85   return bRet;
86 }
87
88 //=======================================================================
89 //function : GetCount
90 //purpose  :
91 //=======================================================================
92 void GetCount(const TopoDS_Shape& aS,
93               Standard_Integer& iCnt)
94 {
95   TopoDS_Iterator aIt;
96   TopAbs_ShapeEnum aTS;
97   //
98   aTS=aS.ShapeType();
99   //
100   if (aTS==TopAbs_SHAPE) {
101     return;
102   }
103   if (aTS!=TopAbs_COMPOUND) {
104     ++iCnt;
105     return;
106   }
107   //
108   aIt.Initialize(aS);
109   for (; aIt.More(); aIt.Next()) {
110     const TopoDS_Shape& aSx=aIt.Value();
111     GetCount(aSx, iCnt);
112   }
113 }
114
115 //=======================================================================
116 //function : RefineSDShapes
117 //purpose  :
118 //=======================================================================
119   Standard_Integer GEOMAlgo_Tools::RefineSDShapes(GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape& aMPKLE,
120                                                   const Standard_Real aTol,
121                                                   const Handle(IntTools_Context)& aCtx)
122 {
123   Standard_Integer i, aNbE, iErr, j, aNbEE, aNbToAdd;
124   TopTools_IndexedDataMapOfShapeListOfShape aMEE, aMSDE, aMEToAdd;
125   //
126   iErr=1;
127   //
128   aNbE=aMPKLE.Extent();
129   for (i=1; i<=aNbE; ++i) {
130     TopTools_ListOfShape& aLSDE=aMPKLE.ChangeFromIndex(i);
131     //
132     aMEE.Clear();
133     iErr=GEOMAlgo_Tools::FindSDShapes(aLSDE, aTol, aMEE, aCtx);
134     if (iErr) {
135       return iErr;
136     }
137     //
138     aNbEE=aMEE.Extent();
139     if (aNbEE==1) {
140       continue;  // nothing to do
141     }
142     //
143     for (j=1; j<=aNbEE; ++j) {
144       TopTools_ListOfShape& aLEE=aMEE.ChangeFromIndex(j);
145       //
146       if (j==1) {
147         aLSDE.Clear();
148         aLSDE.Append(aLEE);
149       }
150       else {
151         const TopoDS_Shape& aE1=aLEE.First();
152         aMEToAdd.Add(aE1, aLEE);
153       }
154     }
155   }
156   //
157   aNbToAdd=aMEToAdd.Extent();
158   if (!aNbToAdd) {
159     return aNbToAdd;
160   }
161   //
162   for (i=1; i<=aNbToAdd; ++i) {
163     GEOMAlgo_PassKeyShape aPKE1;
164     //
165     const TopoDS_Shape& aE1=aMEToAdd.FindKey(i);
166     const TopTools_ListOfShape& aLE=aMEToAdd(i);
167     //
168     //qf
169     //aPKE1.SetIds(aE1);
170     aPKE1.SetShapes(aE1);
171     //qt
172     aMPKLE.Add(aPKE1, aLE);
173   }
174   //
175   return 0;
176 }
177 //=======================================================================
178 //function : FindSDShapes
179 //purpose  :
180 //=======================================================================
181 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopTools_ListOfShape& aLE,
182                                               const Standard_Real aTol,
183                                               TopTools_IndexedDataMapOfShapeListOfShape& aMEE,
184                                               const Handle(IntTools_Context)& aCtx)
185 {
186   Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
187   TopTools_ListOfShape aLESD;
188   TopTools_ListIteratorOfListOfShape aIt, aIt1;
189   TopTools_IndexedMapOfShape aMProcessed;
190   TopAbs_ShapeEnum aType;
191   //
192   aNbE=aLE.Extent();
193   if (!aNbE) {
194     return 3; // Err
195   }
196   //modified by NIZNHY-PKV Thu Dec 30 10:56:52 2004 f
197   if (aNbE==1) {
198     return 0; // Nothing to do
199   }
200   //modified by NIZNHY-PKV Thu Dec 30 10:56:56 2004 t
201   //
202   while(1) {
203     aNbEProcessed=aMProcessed.Extent();
204     if (aNbEProcessed==aNbE) {
205       break;
206     }
207     //
208     aIt.Initialize(aLE);
209     for (; aIt.More(); aIt.Next()) {
210       const TopoDS_Shape& aS=aIt.Value();
211       //
212       if (aMProcessed.Contains(aS)) {
213         continue;
214       }
215       //
216       //modified by NIZNHY-PKV Thu Dec 30 10:57:01 2004 f
217       aType=aS.ShapeType();
218       if (aType==TopAbs_EDGE) {
219         const TopoDS_Edge& aE=TopoDS::Edge(aS);
220         if (BRep_Tool::Degenerated(aE)) {
221           aMProcessed.Add(aE);
222           continue;
223         }
224       }
225       //modified by NIZNHY-PKV Thu Dec 30 10:57:03 2004 t
226       //
227       aLESD.Clear();
228       iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
229       if (iErr) {
230         return 2; // Err
231       }
232       //
233       aNbESD=aLESD.Extent();
234       if (!aNbESD) {
235         return 1; // Err
236       }
237       //
238       aMEE.Add(aS, aLESD);
239       //
240       aIt1.Initialize(aLESD);
241       for (; aIt1.More(); aIt1.Next()) {
242         const TopoDS_Shape& aE1=aIt1.Value();
243         aMProcessed.Add(aE1);
244       }
245     }
246   }
247   return 0;
248 }
249 //=======================================================================
250 //function : FindSDShapes
251 //purpose  :
252 //=======================================================================
253 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopoDS_Shape& aE1,
254                                               const TopTools_ListOfShape& aLE,
255                                               const Standard_Real aTol,
256                                               TopTools_ListOfShape& aLESD,
257                                               const Handle(IntTools_Context)& aCtx)
258 {
259   Standard_Boolean bIsDone;
260   Standard_Real aTol2, aD2;
261   gp_Pnt aP1, aP2;
262   TopTools_ListIteratorOfListOfShape aIt;
263   //
264   aTol2=aTol*aTol;
265   GEOMAlgo_Tools::PointOnShape(aE1, aP1);
266   //
267   aIt.Initialize(aLE);
268   for (; aIt.More(); aIt.Next()) {
269     const TopoDS_Shape& aE2=aIt.Value();
270     if (aE2.IsSame(aE1)) {
271        aLESD.Append(aE2);
272     }
273     else {
274       bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
275       if (!bIsDone) {
276         //return 1;
277         continue; // jfa BUG 20361
278       }
279       aD2=aP1.SquareDistance(aP2);
280       if(aD2<aTol2) {
281         aLESD.Append(aE2);
282       }
283     }
284   }
285   return 0;
286 }
287
288 //=======================================================================
289 //function : ProjectPointOnShape
290 //purpose  :
291 //=======================================================================
292 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
293                                                      const TopoDS_Shape& aS,
294                                                      gp_Pnt& aP2,
295                                                      const Handle(IntTools_Context)& aCtx)
296 {
297   Standard_Boolean bIsDone = Standard_False;
298   Standard_Real aT2;
299   TopAbs_ShapeEnum aType;
300   //
301   aType = aS.ShapeType();
302   switch (aType)
303     {
304     case TopAbs_EDGE:
305       {
306         const TopoDS_Edge& aE2 = TopoDS::Edge(aS);
307         //
308         if (BRep_Tool::Degenerated(aE2)) { // jfa
309           return Standard_True;
310         }
311         else {
312           Standard_Real f, l;
313           Handle(Geom_Curve) aC3D = BRep_Tool::Curve (aE2, f, l);
314           if (aC3D.IsNull()) {
315             return Standard_True;
316           }
317           bIsDone = aCtx->ProjectPointOnEdge(aP1, aE2, aT2);
318         }
319         if (!bIsDone) {
320           return bIsDone;
321         }
322         //
323         GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
324       }
325       break;
326       //
327     case TopAbs_FACE:
328       {
329         const TopoDS_Face& aF2 = TopoDS::Face(aS);
330         GeomAPI_ProjectPointOnSurf& aProj = aCtx->ProjPS(aF2);
331         //
332         aProj.Perform(aP1);
333         bIsDone = aProj.IsDone();
334         if (!bIsDone) {
335           return bIsDone;
336         }
337         //
338         aP2 = aProj.NearestPoint();
339       }
340       break;
341       //
342     default:
343       break; // Err
344     }
345   return bIsDone;
346 }
347 //=======================================================================
348 //function : PointOnShape
349 //purpose  :
350 //=======================================================================
351 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
352                                   gp_Pnt& aP3D)
353 {
354   TopAbs_ShapeEnum aType;
355   //
356   aP3D.SetCoord(99.,99.,99.);
357   aType=aS.ShapeType();
358   switch(aType) {
359     case TopAbs_EDGE: {
360       const TopoDS_Edge& aE=TopoDS::Edge(aS);
361       GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
362       }
363       break;
364       //
365     case TopAbs_FACE: {
366       const TopoDS_Face& aF=TopoDS::Face(aS);
367       GEOMAlgo_Tools::PointOnFace(aF, aP3D);
368       }
369       break;
370       //
371     default:
372       break; // Err
373   }
374 }
375 //=======================================================================
376 //function : PointOnFace
377 //purpose  :
378 //=======================================================================
379 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
380                                  gp_Pnt& aP3D)
381 {
382   Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
383   //
384   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
385   //
386   aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax);
387   aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax);
388   //
389   GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
390 }
391 //=======================================================================
392 //function : PointOnFace
393 //purpose  :
394 //=======================================================================
395 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
396                                  const Standard_Real aU,
397                                  const Standard_Real aV,
398                                  gp_Pnt& aP3D)
399 {
400   Handle(Geom_Surface) aS;
401   //
402   aS=BRep_Tool::Surface(aF);
403   aS->D0(aU, aV, aP3D);
404 }
405 //=======================================================================
406 //function : PointOnEdge
407 //purpose  :
408 //=======================================================================
409 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
410                                  gp_Pnt& aP3D)
411 {
412   Standard_Real aTx, aT1, aT2;
413   //
414   BRep_Tool::Curve(aE, aT1, aT2);
415   aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
416   GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
417 }
418 //=======================================================================
419 //function : PointOnEdge
420 //purpose  :
421 //=======================================================================
422 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
423                                  const Standard_Real aT,
424                                  gp_Pnt& aP3D)
425 {
426   Standard_Real aT1, aT2;
427   Handle(Geom_Curve) aC3D;
428   //
429   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
430   aC3D->D0(aT, aP3D);
431 }
432 //=======================================================================
433 //function : RefinePCurveForEdgeOnFace
434 //purpose  :
435 //=======================================================================
436 void GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(const TopoDS_Edge& aE,
437                                                const TopoDS_Face& aF,
438                                                const Standard_Real aUMin,
439                                                const Standard_Real aUMax)
440 {
441   Standard_Real aT1, aT2, aTx, aUx, aTol;
442   gp_Pnt2d aP2D;
443   Handle(Geom_Surface) aS;
444   Handle(Geom2d_Curve) aC2D;
445   BRep_Builder aBB;
446   //
447   aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
448   if (!aC2D.IsNull()) {
449     if (BRep_Tool::IsClosed(aE, aF)) {
450       return;
451     }
452     aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
453     aC2D->D0(aTx, aP2D);
454     aUx=aP2D.X();
455     if (aUx < aUMin || aUx > aUMax) {
456       // need to rebuild
457       Handle(Geom2d_Curve) aC2Dx;
458       //
459       aTol=BRep_Tool::Tolerance(aE);
460       aBB.UpdateEdge(aE, aC2Dx, aF, aTol);
461     }
462   }
463 }
464 //=======================================================================
465 //function : IsUPeriodic
466 //purpose  :
467 //=======================================================================
468 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const  Handle(Geom_Surface) &aS)
469 {
470   Standard_Boolean bRet;
471   GeomAbs_SurfaceType aType;
472   GeomAdaptor_Surface aGAS;
473   //
474   aGAS.Load(aS);
475   aType=aGAS.GetType();
476   bRet=(aType==GeomAbs_Cylinder||
477         aType==GeomAbs_Cone ||
478         aType==GeomAbs_Sphere);
479   //
480   return bRet;
481 }
482
483 //modified by NIZNHY-PKV Fri Feb 03 11:16:35 2012f
484 //=======================================================================
485 //function : BuildPCurveForEdgeOnFace
486 //purpose  :
487 //=======================================================================
488 Standard_Integer
489   GEOMAlgo_Tools::BuildPCurveForEdgeOnFace(const TopoDS_Edge& aEold,
490                                            const TopoDS_Edge& aEnew,
491                                            const TopoDS_Face& aF,
492                                            const Handle(IntTools_Context)& aCtx)
493 {
494   Standard_Boolean bIsClosed, bUClosed, bHasOld;
495   Standard_Integer iRet, aNbPoints;
496   Standard_Real aTS, aTS1, aTS2, aT, aT1, aT2, aScPr, aTol;
497   Standard_Real aU, aV, aUS1, aVS1, aUS2, aVS2;
498   gp_Pnt aP;
499   gp_Pnt2d aP2DS1, aP2DS2, aP2D;
500   gp_Vec2d aV2DS1, aV2DS2;
501   Handle(Geom2d_Curve) aC2D, aC2DS1, aC2DS2;
502   Handle(Geom_Surface) aS;
503   TopoDS_Edge aES;
504   //
505   iRet=0;
506   //
507   bHasOld=BOPTools_Tools2D::HasCurveOnSurface(aEnew, aF, aC2D, aT1, aT2, aTol);
508   if (bHasOld) {
509     return iRet;
510   }
511   //
512   BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aEnew, aF);
513   aC2D=BRep_Tool::CurveOnSurface(aEnew, aF, aT1, aT2);
514   if (aC2D.IsNull()){
515     iRet=1;
516     return iRet;
517   }
518   //
519   bIsClosed=BRep_Tool::IsClosed(aEold, aF);
520   if (!bIsClosed) {
521     return iRet;
522   }
523   //
524   aTol=1.e-7;
525   //
526   // 1. bUClosed - direction of closeness
527   //
528   aES=aEold;
529   aES.Orientation(TopAbs_FORWARD);
530   aC2DS1=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
531   //
532   aES.Orientation(TopAbs_REVERSED);
533   aC2DS2=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
534   //
535   aTS=BOPTools_Tools2D::IntermediatePoint(aTS1, aTS2);
536   //
537   aC2DS1->D1(aTS, aP2DS1, aV2DS1);
538   aC2DS2->D1(aTS, aP2DS2, aV2DS2);
539   //
540   gp_Vec2d aV2DS12(aP2DS1, aP2DS2);
541   gp_Dir2d aD2DS12(aV2DS12);
542   const gp_Dir2d& aD2DX=gp::DX2d();
543   //
544   aScPr=aD2DS12*aD2DX;
545   bUClosed=Standard_True;
546   if (fabs(aScPr) < aTol) {
547     bUClosed=!bUClosed;
548   }
549   //
550   // 2. aP2D - point on curve aC2D, that corresponds to aP2DS1
551   aP2DS1.Coord(aUS1, aVS1);
552   aP2DS2.Coord(aUS2, aVS2);
553   //
554   aS=BRep_Tool::Surface(aF);
555   aS->D0(aUS1, aVS1, aP);
556   //
557   GeomAPI_ProjectPointOnCurve& aProjPC=aCtx->ProjPC(aEnew);
558   //
559   aProjPC.Perform(aP);
560   aNbPoints=aProjPC.NbPoints();
561   if (!aNbPoints) {
562     iRet=2;
563     return iRet;
564   }
565   //
566   aT=aProjPC.LowerDistanceParameter();
567
568   //
569   // 3. Build the second 2D curve
570   Standard_Boolean bRevOrder;
571   gp_Vec2d aV2DT, aV2D;
572   Handle(Geom2d_Curve) aC2Dnew;
573   Handle(Geom2d_TrimmedCurve) aC2DTnew;
574   BRep_Builder aBB;
575   //
576   aC2D->D1(aT, aP2D, aV2D);
577   aP2D.Coord(aU, aV);
578   //
579   aC2Dnew=Handle(Geom2d_Curve)::DownCast(aC2D->Copy());
580   aC2DTnew = new Geom2d_TrimmedCurve(aC2Dnew, aT1, aT2);
581   //
582   aV2DT=aV2DS12;
583   if (!bUClosed) {    // V Closed
584     if (fabs(aV-aVS2)<aTol) {
585       aV2DT.Reverse();
586     }
587   }
588   else {   // U Closed
589     if (fabs(aU-aUS2)<aTol) {
590       aV2DT.Reverse();
591     }
592   }
593   //
594   aC2DTnew->Translate(aV2DT);
595   //
596   // 4 Order the 2D curves
597   bRevOrder=Standard_False;
598   aScPr=aV2D*aV2DS1;
599   if(aScPr<0.) {
600     bRevOrder=!bRevOrder;
601   }
602   //
603   // 5. Update the edge
604   aTol=BRep_Tool::Tolerance(aEnew);
605   if (!bRevOrder) {
606     aBB.UpdateEdge(aEnew, aC2D, aC2DTnew, aF, aTol);
607   }
608   else {
609     aBB.UpdateEdge(aEnew, aC2DTnew, aC2D , aF, aTol);
610   }
611   //
612   return iRet;
613 }