1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: GEOMAlgo_Tools.cxx
23 // Created: Mon Dec 6 11:35:29 2004
24 // Author: Peter KURNEV
26 #include <GEOMAlgo_Tools.ixx>
28 #include <GEOMAlgo_PassKeyShape.hxx>
29 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
31 #include <Basics_OCCTVersion.hxx>
35 #include <gp_Pnt2d.hxx>
36 #include <gp_Vec2d.hxx>
37 #include <gp_Dir2d.hxx>
39 #include <Geom2d_Curve.hxx>
40 #include <Geom2d_TrimmedCurve.hxx>
42 #include <Geom_Curve.hxx>
43 #include <Geom_Surface.hxx>
45 #include <GeomAdaptor_Surface.hxx>
47 #include <GeomAPI_ProjectPointOnSurf.hxx>
48 #include <GeomAPI_ProjectPointOnCurve.hxx>
50 #include <TopAbs_ShapeEnum.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <TopoDS_Edge.hxx>
55 #include <TopoDS_Iterator.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
59 #include <TopTools_IndexedMapOfShape.hxx>
61 #include <BRep_Tool.hxx>
62 #include <BRep_Builder.hxx>
63 #include <BRepTools.hxx>
65 #include <BOPTools_Tools2D.hxx>
66 #include <IntTools_Context.hxx>
69 void GetCount(const TopoDS_Shape& aS,
70 Standard_Integer& iCnt);
72 //=======================================================================
73 //function : IsCompositeShape
75 //=======================================================================
76 Standard_Boolean GEOMAlgo_Tools::IsCompositeShape(const TopoDS_Shape& aS)
78 Standard_Boolean bRet;
79 Standard_Integer iCnt;
89 //=======================================================================
92 //=======================================================================
93 void GetCount(const TopoDS_Shape& aS,
94 Standard_Integer& iCnt)
101 if (aTS==TopAbs_SHAPE) {
104 if (aTS!=TopAbs_COMPOUND) {
110 for (; aIt.More(); aIt.Next()) {
111 const TopoDS_Shape& aSx=aIt.Value();
116 //=======================================================================
117 //function : RefineSDShapes
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)
125 IntTools_Context& aCtx)
128 Standard_Integer i, aNbE, iErr, j, aNbEE, aNbToAdd;
129 TopTools_IndexedDataMapOfShapeListOfShape aMEE, aMSDE, aMEToAdd;
133 aNbE=aMPKLE.Extent();
134 for (i=1; i<=aNbE; ++i) {
135 TopTools_ListOfShape& aLSDE=aMPKLE.ChangeFromIndex(i);
138 iErr=GEOMAlgo_Tools::FindSDShapes(aLSDE, aTol, aMEE, aCtx);
145 continue; // nothing to do
148 for (j=1; j<=aNbEE; ++j) {
149 TopTools_ListOfShape& aLEE=aMEE.ChangeFromIndex(j);
156 const TopoDS_Shape& aE1=aLEE.First();
157 aMEToAdd.Add(aE1, aLEE);
162 aNbToAdd=aMEToAdd.Extent();
167 for (i=1; i<=aNbToAdd; ++i) {
168 GEOMAlgo_PassKeyShape aPKE1;
170 const TopoDS_Shape& aE1=aMEToAdd.FindKey(i);
171 const TopTools_ListOfShape& aLE=aMEToAdd(i);
175 aPKE1.SetShapes(aE1);
177 aMPKLE.Add(aPKE1, aLE);
182 //=======================================================================
183 //function : FindSDShapes
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)
192 IntTools_Context& aCtx)
195 Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
196 TopTools_ListOfShape aLESD;
197 TopTools_ListIteratorOfListOfShape aIt, aIt1;
198 TopTools_IndexedMapOfShape aMProcessed;
199 TopAbs_ShapeEnum aType;
205 //modified by NIZNHY-PKV Thu Dec 30 10:56:52 2004 f
207 return 0; // Nothing to do
209 //modified by NIZNHY-PKV Thu Dec 30 10:56:56 2004 t
212 aNbEProcessed=aMProcessed.Extent();
213 if (aNbEProcessed==aNbE) {
218 for (; aIt.More(); aIt.Next()) {
219 const TopoDS_Shape& aS=aIt.Value();
221 if (aMProcessed.Contains(aS)) {
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)) {
234 //modified by NIZNHY-PKV Thu Dec 30 10:57:03 2004 t
237 iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
242 aNbESD=aLESD.Extent();
249 aIt1.Initialize(aLESD);
250 for (; aIt1.More(); aIt1.Next()) {
251 const TopoDS_Shape& aE1=aIt1.Value();
252 aMProcessed.Add(aE1);
258 //=======================================================================
259 //function : FindSDShapes
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)
269 IntTools_Context& aCtx)
272 Standard_Boolean bIsDone;
273 Standard_Real aTol2, aD2;
275 TopTools_ListIteratorOfListOfShape aIt;
278 GEOMAlgo_Tools::PointOnShape(aE1, aP1);
281 for (; aIt.More(); aIt.Next()) {
282 const TopoDS_Shape& aE2=aIt.Value();
283 if (aE2.IsSame(aE1)) {
287 bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
290 continue; // jfa BUG 20361
292 aD2=aP1.SquareDistance(aP2);
301 //=======================================================================
302 //function : ProjectPointOnShape
304 //=======================================================================
305 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
306 const TopoDS_Shape& aS,
308 #if OCC_VERSION_LARGE > 0x06050200
309 const Handle(IntTools_Context)& aCtx)
311 IntTools_Context& aCtx)
314 Standard_Boolean bIsDone = Standard_False;
316 TopAbs_ShapeEnum aType;
318 aType = aS.ShapeType();
323 const TopoDS_Edge& aE2 = TopoDS::Edge(aS);
325 if (BRep_Tool::Degenerated(aE2)) { // jfa
326 return Standard_True;
330 Handle(Geom_Curve) aC3D = BRep_Tool::Curve (aE2, f, l);
332 return Standard_True;
334 #if OCC_VERSION_LARGE > 0x06050200
335 bIsDone = aCtx->ProjectPointOnEdge(aP1, aE2, aT2);
337 bIsDone = aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
344 GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
350 const TopoDS_Face& aF2 = TopoDS::Face(aS);
351 #if OCC_VERSION_LARGE > 0x06050200
352 GeomAPI_ProjectPointOnSurf& aProj = aCtx->ProjPS(aF2);
354 GeomAPI_ProjectPointOnSurf& aProj = aCtx.ProjPS(aF2);
358 bIsDone = aProj.IsDone();
363 aP2 = aProj.NearestPoint();
372 //=======================================================================
373 //function : PointOnShape
375 //=======================================================================
376 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
379 TopAbs_ShapeEnum aType;
381 aP3D.SetCoord(99.,99.,99.);
382 aType=aS.ShapeType();
385 const TopoDS_Edge& aE=TopoDS::Edge(aS);
386 GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
391 const TopoDS_Face& aF=TopoDS::Face(aS);
392 GEOMAlgo_Tools::PointOnFace(aF, aP3D);
400 //=======================================================================
401 //function : PointOnFace
403 //=======================================================================
404 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
407 Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
409 BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
411 aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax);
412 aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax);
414 GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
416 //=======================================================================
417 //function : PointOnFace
419 //=======================================================================
420 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
421 const Standard_Real aU,
422 const Standard_Real aV,
425 Handle(Geom_Surface) aS;
427 aS=BRep_Tool::Surface(aF);
428 aS->D0(aU, aV, aP3D);
430 //=======================================================================
431 //function : PointOnEdge
433 //=======================================================================
434 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
437 Standard_Real aTx, aT1, aT2;
439 BRep_Tool::Curve(aE, aT1, aT2);
440 aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
441 GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
443 //=======================================================================
444 //function : PointOnEdge
446 //=======================================================================
447 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
448 const Standard_Real aT,
451 Standard_Real aT1, aT2;
452 Handle(Geom_Curve) aC3D;
454 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
457 //=======================================================================
458 //function : RefinePCurveForEdgeOnFace
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)
466 Standard_Real aT1, aT2, aTx, aUx, aTol;
468 Handle(Geom_Surface) aS;
469 Handle(Geom2d_Curve) aC2D;
472 aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
473 if (!aC2D.IsNull()) {
474 if (BRep_Tool::IsClosed(aE, aF)) {
477 aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
480 if (aUx < aUMin || aUx > aUMax) {
482 Handle(Geom2d_Curve) aC2Dx;
484 aTol=BRep_Tool::Tolerance(aE);
485 aBB.UpdateEdge(aE, aC2Dx, aF, aTol);
489 //=======================================================================
490 //function : IsUPeriodic
492 //=======================================================================
493 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const Handle(Geom_Surface) &aS)
495 Standard_Boolean bRet;
496 GeomAbs_SurfaceType aType;
497 GeomAdaptor_Surface aGAS;
500 aType=aGAS.GetType();
501 bRet=(aType==GeomAbs_Cylinder||
502 aType==GeomAbs_Cone ||
503 aType==GeomAbs_Sphere);
508 //modified by NIZNHY-PKV Fri Feb 03 11:16:35 2012f
509 //=======================================================================
510 //function : BuildPCurveForEdgeOnFace
512 //=======================================================================
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)
520 IntTools_Context& aCtx)
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;
528 gp_Pnt2d aP2DS1, aP2DS2, aP2D;
529 gp_Vec2d aV2DS1, aV2DS2;
530 Handle(Geom2d_Curve) aC2D, aC2DS1, aC2DS2;
531 Handle(Geom_Surface) aS;
536 bHasOld=BOPTools_Tools2D::HasCurveOnSurface(aEnew, aF, aC2D, aT1, aT2, aTol);
541 BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aEnew, aF);
542 aC2D=BRep_Tool::CurveOnSurface(aEnew, aF, aT1, aT2);
548 bIsClosed=BRep_Tool::IsClosed(aEold, aF);
555 // 1. bUClosed - direction of closeness
558 aES.Orientation(TopAbs_FORWARD);
559 aC2DS1=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
561 aES.Orientation(TopAbs_REVERSED);
562 aC2DS2=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
564 aTS=BOPTools_Tools2D::IntermediatePoint(aTS1, aTS2);
566 aC2DS1->D1(aTS, aP2DS1, aV2DS1);
567 aC2DS2->D1(aTS, aP2DS2, aV2DS2);
569 gp_Vec2d aV2DS12(aP2DS1, aP2DS2);
570 gp_Dir2d aD2DS12(aV2DS12);
571 const gp_Dir2d& aD2DX=gp::DX2d();
574 bUClosed=Standard_True;
575 if (fabs(aScPr) < aTol) {
579 // 2. aP2D - point on curve aC2D, that corresponds to aP2DS1
580 aP2DS1.Coord(aUS1, aVS1);
581 aP2DS2.Coord(aUS2, aVS2);
583 aS=BRep_Tool::Surface(aF);
584 aS->D0(aUS1, aVS1, aP);
586 #if OCC_VERSION_LARGE > 0x06050200
587 GeomAPI_ProjectPointOnCurve& aProjPC=aCtx->ProjPC(aEnew);
589 GeomAPI_ProjectPointOnCurve& aProjPC=aCtx.ProjPC(aEnew);
593 aNbPoints=aProjPC.NbPoints();
599 aT=aProjPC.LowerDistanceParameter();
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;
609 aC2D->D1(aT, aP2D, aV2D);
612 aC2Dnew=Handle(Geom2d_Curve)::DownCast(aC2D->Copy());
613 aC2DTnew = new Geom2d_TrimmedCurve(aC2Dnew, aT1, aT2);
616 if (!bUClosed) { // V Closed
617 if (fabs(aV-aVS2)<aTol) {
622 if (fabs(aU-aUS2)<aTol) {
627 aC2DTnew->Translate(aV2DT);
629 // 4 Order the 2D curves
630 bRevOrder=Standard_False;
633 bRevOrder=!bRevOrder;
636 // 5. Update the edge
637 aTol=BRep_Tool::Tolerance(aEnew);
639 aBB.UpdateEdge(aEnew, aC2D, aC2DTnew, aF, aTol);
642 aBB.UpdateEdge(aEnew, aC2DTnew, aC2D , aF, aTol);