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.hxx>
30 #include <gp_Pnt2d.hxx>
31 #include <gp_Vec2d.hxx>
32 #include <gp_Dir2d.hxx>
34 #include <Geom2d_Curve.hxx>
35 #include <Geom2d_TrimmedCurve.hxx>
37 #include <Geom_Curve.hxx>
38 #include <Geom_Surface.hxx>
40 #include <GeomAdaptor_Surface.hxx>
42 #include <GeomAPI_ProjectPointOnSurf.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
45 #include <TopAbs_ShapeEnum.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Iterator.hxx>
52 #include <TopTools_ListOfShape.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <TopTools_IndexedMapOfShape.hxx>
56 #include <BRep_Tool.hxx>
57 #include <BRep_Builder.hxx>
58 #include <BRepTools.hxx>
60 #include <BOPTools_Tools2D.hxx>
61 #include <IntTools_Context.hxx>
63 #include <GEOMAlgo_PassKeyShape.hxx>
64 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
67 void GetCount(const TopoDS_Shape& aS,
68 Standard_Integer& iCnt);
70 //=======================================================================
71 //function : IsCompositeShape
73 //=======================================================================
74 Standard_Boolean GEOMAlgo_Tools::IsCompositeShape(const TopoDS_Shape& aS)
76 Standard_Boolean bRet;
77 Standard_Integer iCnt;
87 //=======================================================================
90 //=======================================================================
91 void GetCount(const TopoDS_Shape& aS,
92 Standard_Integer& iCnt)
99 if (aTS==TopAbs_SHAPE) {
102 if (aTS!=TopAbs_COMPOUND) {
108 for (; aIt.More(); aIt.Next()) {
109 const TopoDS_Shape& aSx=aIt.Value();
114 //=======================================================================
115 //function : RefineSDShapes
117 //=======================================================================
118 Standard_Integer GEOMAlgo_Tools::RefineSDShapes(GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape& aMPKLE,
119 const Standard_Real aTol,
120 const Handle(IntTools_Context)& aCtx)
122 Standard_Integer i, aNbE, iErr, j, aNbEE, aNbToAdd;
123 TopTools_IndexedDataMapOfShapeListOfShape aMEE, aMSDE, aMEToAdd;
127 aNbE=aMPKLE.Extent();
128 for (i=1; i<=aNbE; ++i) {
129 TopTools_ListOfShape& aLSDE=aMPKLE.ChangeFromIndex(i);
132 iErr=GEOMAlgo_Tools::FindSDShapes(aLSDE, aTol, aMEE, aCtx);
139 continue; // nothing to do
142 for (j=1; j<=aNbEE; ++j) {
143 TopTools_ListOfShape& aLEE=aMEE.ChangeFromIndex(j);
150 const TopoDS_Shape& aE1=aLEE.First();
151 aMEToAdd.Add(aE1, aLEE);
156 aNbToAdd=aMEToAdd.Extent();
161 for (i=1; i<=aNbToAdd; ++i) {
162 GEOMAlgo_PassKeyShape aPKE1;
164 const TopoDS_Shape& aE1=aMEToAdd.FindKey(i);
165 const TopTools_ListOfShape& aLE=aMEToAdd(i);
169 aPKE1.SetShapes(aE1);
171 aMPKLE.Add(aPKE1, aLE);
176 //=======================================================================
177 //function : FindSDShapes
179 //=======================================================================
180 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopTools_ListOfShape& aLE,
181 const Standard_Real aTol,
182 TopTools_IndexedDataMapOfShapeListOfShape& aMEE,
183 const Handle(IntTools_Context)& aCtx)
185 Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
186 TopTools_ListOfShape aLESD;
187 TopTools_ListIteratorOfListOfShape aIt, aIt1;
188 TopTools_IndexedMapOfShape aMProcessed;
189 TopAbs_ShapeEnum aType;
195 //modified by NIZNHY-PKV Thu Dec 30 10:56:52 2004 f
197 return 0; // Nothing to do
199 //modified by NIZNHY-PKV Thu Dec 30 10:56:56 2004 t
202 aNbEProcessed=aMProcessed.Extent();
203 if (aNbEProcessed==aNbE) {
208 for (; aIt.More(); aIt.Next()) {
209 const TopoDS_Shape& aS=aIt.Value();
211 if (aMProcessed.Contains(aS)) {
215 //modified by NIZNHY-PKV Thu Dec 30 10:57:01 2004 f
216 aType=aS.ShapeType();
217 if (aType==TopAbs_EDGE) {
218 const TopoDS_Edge& aE=TopoDS::Edge(aS);
219 if (BRep_Tool::Degenerated(aE)) {
224 //modified by NIZNHY-PKV Thu Dec 30 10:57:03 2004 t
227 iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
232 aNbESD=aLESD.Extent();
239 aIt1.Initialize(aLESD);
240 for (; aIt1.More(); aIt1.Next()) {
241 const TopoDS_Shape& aE1=aIt1.Value();
242 aMProcessed.Add(aE1);
248 //=======================================================================
249 //function : FindSDShapes
251 //=======================================================================
252 Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopoDS_Shape& aE1,
253 const TopTools_ListOfShape& aLE,
254 const Standard_Real aTol,
255 TopTools_ListOfShape& aLESD,
256 const Handle(IntTools_Context)& aCtx)
258 Standard_Boolean bIsDone;
259 Standard_Real aTol2, aD2;
261 TopTools_ListIteratorOfListOfShape aIt;
264 GEOMAlgo_Tools::PointOnShape(aE1, aP1);
267 for (; aIt.More(); aIt.Next()) {
268 const TopoDS_Shape& aE2=aIt.Value();
269 if (aE2.IsSame(aE1)) {
273 bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
276 continue; // jfa BUG 20361
278 aD2=aP1.SquareDistance(aP2);
287 //=======================================================================
288 //function : ProjectPointOnShape
290 //=======================================================================
291 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
292 const TopoDS_Shape& aS,
294 const Handle(IntTools_Context)& aCtx)
296 Standard_Boolean bIsDone = Standard_False;
298 TopAbs_ShapeEnum aType;
300 aType = aS.ShapeType();
305 const TopoDS_Edge& aE2 = TopoDS::Edge(aS);
307 if (BRep_Tool::Degenerated(aE2)) { // jfa
308 return Standard_True;
312 Handle(Geom_Curve) aC3D = BRep_Tool::Curve (aE2, f, l);
314 return Standard_True;
316 bIsDone = aCtx->ProjectPointOnEdge(aP1, aE2, aT2);
322 GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
328 const TopoDS_Face& aF2 = TopoDS::Face(aS);
329 GeomAPI_ProjectPointOnSurf& aProj = aCtx->ProjPS(aF2);
332 bIsDone = aProj.IsDone();
337 aP2 = aProj.NearestPoint();
346 //=======================================================================
347 //function : PointOnShape
349 //=======================================================================
350 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
353 TopAbs_ShapeEnum aType;
355 aP3D.SetCoord(99.,99.,99.);
356 aType=aS.ShapeType();
359 const TopoDS_Edge& aE=TopoDS::Edge(aS);
360 GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
365 const TopoDS_Face& aF=TopoDS::Face(aS);
366 GEOMAlgo_Tools::PointOnFace(aF, aP3D);
374 //=======================================================================
375 //function : PointOnFace
377 //=======================================================================
378 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
381 Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
383 BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
385 aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax);
386 aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax);
388 GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
390 //=======================================================================
391 //function : PointOnFace
393 //=======================================================================
394 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
395 const Standard_Real aU,
396 const Standard_Real aV,
399 Handle(Geom_Surface) aS;
401 aS=BRep_Tool::Surface(aF);
402 aS->D0(aU, aV, aP3D);
404 //=======================================================================
405 //function : PointOnEdge
407 //=======================================================================
408 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
411 Standard_Real aTx, aT1, aT2;
413 BRep_Tool::Curve(aE, aT1, aT2);
414 aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
415 GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
417 //=======================================================================
418 //function : PointOnEdge
420 //=======================================================================
421 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
422 const Standard_Real aT,
425 Standard_Real aT1, aT2;
426 Handle(Geom_Curve) aC3D;
428 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
431 //=======================================================================
432 //function : RefinePCurveForEdgeOnFace
434 //=======================================================================
435 void GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(const TopoDS_Edge& aE,
436 const TopoDS_Face& aF,
437 const Standard_Real aUMin,
438 const Standard_Real aUMax)
440 Standard_Real aT1, aT2, aTx, aUx, aTol;
442 Handle(Geom_Surface) aS;
443 Handle(Geom2d_Curve) aC2D;
446 aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
447 if (!aC2D.IsNull()) {
448 if (BRep_Tool::IsClosed(aE, aF)) {
451 aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
454 if (aUx < aUMin || aUx > aUMax) {
456 Handle(Geom2d_Curve) aC2Dx;
458 aTol=BRep_Tool::Tolerance(aE);
459 aBB.UpdateEdge(aE, aC2Dx, aF, aTol);
463 //=======================================================================
464 //function : IsUPeriodic
466 //=======================================================================
467 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const Handle(Geom_Surface) &aS)
469 Standard_Boolean bRet;
470 GeomAbs_SurfaceType aType;
471 GeomAdaptor_Surface aGAS;
474 aType=aGAS.GetType();
475 bRet=(aType==GeomAbs_Cylinder||
476 aType==GeomAbs_Cone ||
477 aType==GeomAbs_Sphere);
482 //modified by NIZNHY-PKV Fri Feb 03 11:16:35 2012f
483 //=======================================================================
484 //function : BuildPCurveForEdgeOnFace
486 //=======================================================================
488 GEOMAlgo_Tools::BuildPCurveForEdgeOnFace(const TopoDS_Edge& aEold,
489 const TopoDS_Edge& aEnew,
490 const TopoDS_Face& aF,
491 const Handle(IntTools_Context)& aCtx)
493 Standard_Boolean bIsClosed, bUClosed, bHasOld;
494 Standard_Integer iRet, aNbPoints;
495 Standard_Real aTS, aTS1, aTS2, aT, aT1, aT2, aScPr, aTol;
496 Standard_Real aU, aV, aUS1, aVS1, aUS2, aVS2;
498 gp_Pnt2d aP2DS1, aP2DS2, aP2D;
499 gp_Vec2d aV2DS1, aV2DS2;
500 Handle(Geom2d_Curve) aC2D, aC2DS1, aC2DS2;
501 Handle(Geom_Surface) aS;
506 bHasOld=BOPTools_Tools2D::HasCurveOnSurface(aEnew, aF, aC2D, aT1, aT2, aTol);
511 BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aEnew, aF);
512 aC2D=BRep_Tool::CurveOnSurface(aEnew, aF, aT1, aT2);
518 bIsClosed=BRep_Tool::IsClosed(aEold, aF);
525 // 1. bUClosed - direction of closeness
528 aES.Orientation(TopAbs_FORWARD);
529 aC2DS1=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
531 aES.Orientation(TopAbs_REVERSED);
532 aC2DS2=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
534 aTS=BOPTools_Tools2D::IntermediatePoint(aTS1, aTS2);
536 aC2DS1->D1(aTS, aP2DS1, aV2DS1);
537 aC2DS2->D1(aTS, aP2DS2, aV2DS2);
539 gp_Vec2d aV2DS12(aP2DS1, aP2DS2);
540 gp_Dir2d aD2DS12(aV2DS12);
541 const gp_Dir2d& aD2DX=gp::DX2d();
544 bUClosed=Standard_True;
545 if (fabs(aScPr) < aTol) {
549 // 2. aP2D - point on curve aC2D, that corresponds to aP2DS1
550 aP2DS1.Coord(aUS1, aVS1);
551 aP2DS2.Coord(aUS2, aVS2);
553 aS=BRep_Tool::Surface(aF);
554 aS->D0(aUS1, aVS1, aP);
556 GeomAPI_ProjectPointOnCurve& aProjPC=aCtx->ProjPC(aEnew);
559 aNbPoints=aProjPC.NbPoints();
565 aT=aProjPC.LowerDistanceParameter();
568 // 3. Build the second 2D curve
569 Standard_Boolean bRevOrder;
570 gp_Vec2d aV2DT, aV2D;
571 Handle(Geom2d_Curve) aC2Dnew;
572 Handle(Geom2d_TrimmedCurve) aC2DTnew;
575 aC2D->D1(aT, aP2D, aV2D);
578 aC2Dnew=Handle(Geom2d_Curve)::DownCast(aC2D->Copy());
579 aC2DTnew = new Geom2d_TrimmedCurve(aC2Dnew, aT1, aT2);
582 if (!bUClosed) { // V Closed
583 if (fabs(aV-aVS2)<aTol) {
588 if (fabs(aU-aUS2)<aTol) {
593 aC2DTnew->Translate(aV2DT);
595 // 4 Order the 2D curves
596 bRevOrder=Standard_False;
599 bRevOrder=!bRevOrder;
602 // 5. Update the edge
603 aTol=BRep_Tool::Tolerance(aEnew);
605 aBB.UpdateEdge(aEnew, aC2D, aC2DTnew, aF, aTol);
608 aBB.UpdateEdge(aEnew, aC2DTnew, aC2D , aF, aTol);