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