1 // Copyright (C) 2007-2012 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
23 // File: GEOMAlgo_Tools.cxx
24 // Created: Mon Dec 6 11:35:29 2004
25 // Author: Peter KURNEV
27 #include <GEOMAlgo_Tools.hxx>
31 #include <gp_Pnt2d.hxx>
32 #include <gp_Vec2d.hxx>
33 #include <gp_Dir2d.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <Geom2d_TrimmedCurve.hxx>
38 #include <Geom_Curve.hxx>
39 #include <Geom_Surface.hxx>
41 #include <GeomAdaptor_Surface.hxx>
43 #include <GeomAPI_ProjectPointOnSurf.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <TopAbs_ShapeEnum.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Iterator.hxx>
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <TopTools_IndexedMapOfShape.hxx>
57 #include <BRep_Tool.hxx>
58 #include <BRep_Builder.hxx>
59 #include <BRepTools.hxx>
61 #include <BOPTools_Tools2D.hxx>
62 #include <IntTools_Context.hxx>
64 #include <GEOMAlgo_PassKeyShape.hxx>
65 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
68 void GetCount(const TopoDS_Shape& aS,
69 Standard_Integer& iCnt);
71 //=======================================================================
72 //function : IsCompositeShape
74 //=======================================================================
75 Standard_Boolean GEOMAlgo_Tools::IsCompositeShape(const TopoDS_Shape& aS)
77 Standard_Boolean bRet;
78 Standard_Integer iCnt;
88 //=======================================================================
91 //=======================================================================
92 void GetCount(const TopoDS_Shape& aS,
93 Standard_Integer& iCnt)
100 if (aTS==TopAbs_SHAPE) {
103 if (aTS!=TopAbs_COMPOUND) {
109 for (; aIt.More(); aIt.Next()) {
110 const TopoDS_Shape& aSx=aIt.Value();
115 //=======================================================================
116 //function : RefineSDShapes
118 //=======================================================================
119 Standard_Integer GEOMAlgo_Tools::RefineSDShapes(GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape& aMPKLE,
120 const Standard_Real aTol,
121 const Handle(IntTools_Context)& aCtx)
123 Standard_Integer i, aNbE, iErr, j, aNbEE, aNbToAdd;
124 TopTools_IndexedDataMapOfShapeListOfShape aMEE, aMSDE, aMEToAdd;
128 aNbE=aMPKLE.Extent();
129 for (i=1; i<=aNbE; ++i) {
130 TopTools_ListOfShape& aLSDE=aMPKLE.ChangeFromIndex(i);
133 iErr=GEOMAlgo_Tools::FindSDShapes(aLSDE, aTol, aMEE, aCtx);
140 continue; // nothing to do
143 for (j=1; j<=aNbEE; ++j) {
144 TopTools_ListOfShape& aLEE=aMEE.ChangeFromIndex(j);
151 const TopoDS_Shape& aE1=aLEE.First();
152 aMEToAdd.Add(aE1, aLEE);
157 aNbToAdd=aMEToAdd.Extent();
162 for (i=1; i<=aNbToAdd; ++i) {
163 GEOMAlgo_PassKeyShape aPKE1;
165 const TopoDS_Shape& aE1=aMEToAdd.FindKey(i);
166 const TopTools_ListOfShape& aLE=aMEToAdd(i);
170 aPKE1.SetShapes(aE1);
172 aMPKLE.Add(aPKE1, aLE);
177 //=======================================================================
178 //function : FindSDShapes
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)
186 Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
187 TopTools_ListOfShape aLESD;
188 TopTools_ListIteratorOfListOfShape aIt, aIt1;
189 TopTools_IndexedMapOfShape aMProcessed;
190 TopAbs_ShapeEnum aType;
196 //modified by NIZNHY-PKV Thu Dec 30 10:56:52 2004 f
198 return 0; // Nothing to do
200 //modified by NIZNHY-PKV Thu Dec 30 10:56:56 2004 t
203 aNbEProcessed=aMProcessed.Extent();
204 if (aNbEProcessed==aNbE) {
209 for (; aIt.More(); aIt.Next()) {
210 const TopoDS_Shape& aS=aIt.Value();
212 if (aMProcessed.Contains(aS)) {
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)) {
225 //modified by NIZNHY-PKV Thu Dec 30 10:57:03 2004 t
228 iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
233 aNbESD=aLESD.Extent();
240 aIt1.Initialize(aLESD);
241 for (; aIt1.More(); aIt1.Next()) {
242 const TopoDS_Shape& aE1=aIt1.Value();
243 aMProcessed.Add(aE1);
249 //=======================================================================
250 //function : FindSDShapes
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)
259 Standard_Boolean bIsDone;
260 Standard_Real aTol2, aD2;
262 TopTools_ListIteratorOfListOfShape aIt;
265 GEOMAlgo_Tools::PointOnShape(aE1, aP1);
268 for (; aIt.More(); aIt.Next()) {
269 const TopoDS_Shape& aE2=aIt.Value();
270 if (aE2.IsSame(aE1)) {
274 bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
277 continue; // jfa BUG 20361
279 aD2=aP1.SquareDistance(aP2);
288 //=======================================================================
289 //function : ProjectPointOnShape
291 //=======================================================================
292 Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
293 const TopoDS_Shape& aS,
295 const Handle(IntTools_Context)& aCtx)
297 Standard_Boolean bIsDone = Standard_False;
299 TopAbs_ShapeEnum aType;
301 aType = aS.ShapeType();
306 const TopoDS_Edge& aE2 = TopoDS::Edge(aS);
308 if (BRep_Tool::Degenerated(aE2)) { // jfa
309 return Standard_True;
313 Handle(Geom_Curve) aC3D = BRep_Tool::Curve (aE2, f, l);
315 return Standard_True;
317 bIsDone = aCtx->ProjectPointOnEdge(aP1, aE2, aT2);
323 GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
329 const TopoDS_Face& aF2 = TopoDS::Face(aS);
330 GeomAPI_ProjectPointOnSurf& aProj = aCtx->ProjPS(aF2);
333 bIsDone = aProj.IsDone();
338 aP2 = aProj.NearestPoint();
347 //=======================================================================
348 //function : PointOnShape
350 //=======================================================================
351 void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
354 TopAbs_ShapeEnum aType;
356 aP3D.SetCoord(99.,99.,99.);
357 aType=aS.ShapeType();
360 const TopoDS_Edge& aE=TopoDS::Edge(aS);
361 GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
366 const TopoDS_Face& aF=TopoDS::Face(aS);
367 GEOMAlgo_Tools::PointOnFace(aF, aP3D);
375 //=======================================================================
376 //function : PointOnFace
378 //=======================================================================
379 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
382 Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
384 BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
386 aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax);
387 aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax);
389 GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
391 //=======================================================================
392 //function : PointOnFace
394 //=======================================================================
395 void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
396 const Standard_Real aU,
397 const Standard_Real aV,
400 Handle(Geom_Surface) aS;
402 aS=BRep_Tool::Surface(aF);
403 aS->D0(aU, aV, aP3D);
405 //=======================================================================
406 //function : PointOnEdge
408 //=======================================================================
409 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
412 Standard_Real aTx, aT1, aT2;
414 BRep_Tool::Curve(aE, aT1, aT2);
415 aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
416 GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
418 //=======================================================================
419 //function : PointOnEdge
421 //=======================================================================
422 void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
423 const Standard_Real aT,
426 Standard_Real aT1, aT2;
427 Handle(Geom_Curve) aC3D;
429 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
432 //=======================================================================
433 //function : RefinePCurveForEdgeOnFace
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)
441 Standard_Real aT1, aT2, aTx, aUx, aTol;
443 Handle(Geom_Surface) aS;
444 Handle(Geom2d_Curve) aC2D;
447 aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
448 if (!aC2D.IsNull()) {
449 if (BRep_Tool::IsClosed(aE, aF)) {
452 aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
455 if (aUx < aUMin || aUx > aUMax) {
457 Handle(Geom2d_Curve) aC2Dx;
459 aTol=BRep_Tool::Tolerance(aE);
460 aBB.UpdateEdge(aE, aC2Dx, aF, aTol);
464 //=======================================================================
465 //function : IsUPeriodic
467 //=======================================================================
468 Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const Handle(Geom_Surface) &aS)
470 Standard_Boolean bRet;
471 GeomAbs_SurfaceType aType;
472 GeomAdaptor_Surface aGAS;
475 aType=aGAS.GetType();
476 bRet=(aType==GeomAbs_Cylinder||
477 aType==GeomAbs_Cone ||
478 aType==GeomAbs_Sphere);
483 //modified by NIZNHY-PKV Fri Feb 03 11:16:35 2012f
484 //=======================================================================
485 //function : BuildPCurveForEdgeOnFace
487 //=======================================================================
489 GEOMAlgo_Tools::BuildPCurveForEdgeOnFace(const TopoDS_Edge& aEold,
490 const TopoDS_Edge& aEnew,
491 const TopoDS_Face& aF,
492 const Handle(IntTools_Context)& aCtx)
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;
499 gp_Pnt2d aP2DS1, aP2DS2, aP2D;
500 gp_Vec2d aV2DS1, aV2DS2;
501 Handle(Geom2d_Curve) aC2D, aC2DS1, aC2DS2;
502 Handle(Geom_Surface) aS;
507 bHasOld=BOPTools_Tools2D::HasCurveOnSurface(aEnew, aF, aC2D, aT1, aT2, aTol);
512 BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aEnew, aF);
513 aC2D=BRep_Tool::CurveOnSurface(aEnew, aF, aT1, aT2);
519 bIsClosed=BRep_Tool::IsClosed(aEold, aF);
526 // 1. bUClosed - direction of closeness
529 aES.Orientation(TopAbs_FORWARD);
530 aC2DS1=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
532 aES.Orientation(TopAbs_REVERSED);
533 aC2DS2=BRep_Tool::CurveOnSurface(aES, aF, aTS1, aTS2);
535 aTS=BOPTools_Tools2D::IntermediatePoint(aTS1, aTS2);
537 aC2DS1->D1(aTS, aP2DS1, aV2DS1);
538 aC2DS2->D1(aTS, aP2DS2, aV2DS2);
540 gp_Vec2d aV2DS12(aP2DS1, aP2DS2);
541 gp_Dir2d aD2DS12(aV2DS12);
542 const gp_Dir2d& aD2DX=gp::DX2d();
545 bUClosed=Standard_True;
546 if (fabs(aScPr) < aTol) {
550 // 2. aP2D - point on curve aC2D, that corresponds to aP2DS1
551 aP2DS1.Coord(aUS1, aVS1);
552 aP2DS2.Coord(aUS2, aVS2);
554 aS=BRep_Tool::Surface(aF);
555 aS->D0(aUS1, aVS1, aP);
557 GeomAPI_ProjectPointOnCurve& aProjPC=aCtx->ProjPC(aEnew);
560 aNbPoints=aProjPC.NbPoints();
566 aT=aProjPC.LowerDistanceParameter();
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;
576 aC2D->D1(aT, aP2D, aV2D);
579 aC2Dnew=Handle(Geom2d_Curve)::DownCast(aC2D->Copy());
580 aC2DTnew = new Geom2d_TrimmedCurve(aC2Dnew, aT1, aT2);
583 if (!bUClosed) { // V Closed
584 if (fabs(aV-aVS2)<aTol) {
589 if (fabs(aU-aUS2)<aTol) {
594 aC2DTnew->Translate(aV2DT);
596 // 4 Order the 2D curves
597 bRevOrder=Standard_False;
600 bRevOrder=!bRevOrder;
603 // 5. Update the edge
604 aTol=BRep_Tool::Tolerance(aEnew);
606 aBB.UpdateEdge(aEnew, aC2D, aC2DTnew, aF, aTol);
609 aBB.UpdateEdge(aEnew, aC2DTnew, aC2D , aF, aTol);