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: NMTTools_Tools.cxx
24 // Created: Mon Dec 8 10:35:15 2003
25 // Author: Peter KURNEV
28 #include <NMTTools_Tools.hxx>
30 #include <TColStd_IndexedMapOfInteger.hxx>
34 #include <gp_Pnt2d.hxx>
36 #include <Geom_Surface.hxx>
37 #include <GeomAPI_ProjectPointOnSurf.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Edge.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <BRep_Tool.hxx>
50 #include <BRep_Builder.hxx>
51 #include <BRepTools.hxx>
53 #include <BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger.hxx>
54 #include <BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger.hxx>
56 #include <BOPTools_VVInterference.hxx>
57 #include <BOPTools_SSInterference.hxx>
59 #include <BOPTools_Tools2D.hxx>
60 #include <BOPTools_Tools.hxx>
61 #include <NMTTools_ListIteratorOfListOfCoupleOfShape.hxx>
62 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
63 #include <NMTTools_CoupleOfShape.hxx>
64 #include <TopTools_IndexedMapOfShape.hxx>
65 #include <Geom2d_Curve.hxx>
66 #include <Geom_Curve.hxx>
67 #include <Geom_TrimmedCurve.hxx>
68 #include <BOPTools_Tools2D.hxx>
69 #include <BRepLib.hxx>
70 #include <BOPTools_Tools3D.hxx>
71 #include <TopExp_Explorer.hxx>
73 #include <TopTools_MapOfShape.hxx>
74 #include <TopTools_MapIteratorOfMapOfShape.hxx>
75 #include <TopoDS_Iterator.hxx>
78 void ProcessBlock(const Standard_Integer iV,
79 const BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMCV,
80 TColStd_IndexedMapOfInteger& aProcessed,
81 TColStd_IndexedMapOfInteger& aChain);
83 void ProcessBlock(const TopoDS_Shape& aF,
84 const NMTTools_IndexedDataMapOfShapeIndexedMapOfShape& aMCV,
85 TopTools_IndexedMapOfShape& aProcessed,
86 TopTools_IndexedMapOfShape& aChain);
88 //modified by NIZNHY-PKV Thu Nov 16 10:46:53 2006f SKL/PartC5
89 //=======================================================================
90 // function: UpdateEdge
92 //=======================================================================
93 void NMTTools_Tools::UpdateEdge(const TopoDS_Edge& aE,
94 const Standard_Real aTolR)
96 Standard_Real aTolE, aTolES, aTolV;
100 aTolE=BRep_Tool::Tolerance(aE);
101 aTolES=Max(aTolR, aTolE);
102 aBB.UpdateEdge(aE, aTolES);
105 for (; aIt.More(); aIt.Next()) {
106 const TopoDS_Vertex& aV=TopoDS::Vertex(aIt.Value());
107 aTolV=BRep_Tool::Tolerance(aV);
109 aBB.UpdateVertex(aV, aTolES);
113 //=======================================================================
114 // function: MakePCurve
116 //=======================================================================
117 void NMTTools_Tools::MakePCurve(const TopoDS_Edge& aE,
118 const TopoDS_Face& aF,
119 const Handle(Geom2d_Curve)& aC2Dx1)
122 Standard_Real aTolE, aT1, aT2, aOutFirst, aOutLast, aOutTol;
123 Handle(Geom2d_Curve) aC2D, aC2DA;
128 aFFWD.Orientation(TopAbs_FORWARD);
130 aTolE=BRep_Tool::Tolerance(aE);
132 const Handle(Geom_Curve)& aC3DE=BRep_Tool::Curve(aE, aT1, aT2);
133 Handle(Geom_TrimmedCurve)aC3DETrim=new Geom_TrimmedCurve(aC3DE, aT1, aT2);
136 if (aC2D.IsNull()) { // ?
137 BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aFFWD);
138 BOPTools_Tools2D::CurveOnSurface(aE, aFFWD, aC2D, aOutFirst, aOutLast, aOutTol, Standard_True);
141 if (aC3DE->IsPeriodic()) {
142 BOPTools_Tools2D::AdjustPCurveOnFace(aFFWD, aT1, aT2, aC2D, aC2DA);
145 BOPTools_Tools2D::AdjustPCurveOnFace(aFFWD, aC3DETrim, aC2D, aC2DA);
148 aBB.UpdateEdge(aE, aC2DA, aFFWD, aTolE);
149 BRepLib::SameParameter(aE);
152 //=======================================================================
153 // function: MakePCurve
155 //=======================================================================
156 void NMTTools_Tools::MakePCurve(const TopoDS_Edge& aE,
157 const TopoDS_Face& aF,
158 const Handle(Geom2d_Curve)& aC2Dx,
159 const Standard_Real aTolR2D)
161 Standard_Integer k, aNbV;
162 Standard_Real aTolEdge, aTolFact, aTolV, aTolVmax;
163 Standard_Real aTFirst, aTLast, aOutFirst, aOutLast, aOutTol;
165 TopTools_IndexedMapOfShape aVMap;
169 aFFWD.Orientation(TopAbs_FORWARD);
171 aTolEdge=BRep_Tool::Tolerance(aE);
172 aTolFact=Max(aTolEdge, aTolR2D);
174 TopExp::MapShapes(aE, TopAbs_VERTEX, aVMap);
178 for (k=1; k<=aNbV; ++k) {
179 const TopoDS_Vertex& aV=TopoDS::Vertex(aVMap(k));
180 aTolV=BRep_Tool::Tolerance(aV);
181 if (aTolV>aTolVmax) {
186 if (aTolFact>aTolVmax) {
190 const Handle(Geom_Curve)& aC3DE=BRep_Tool::Curve(aE, aTFirst, aTLast);
191 Handle(Geom_TrimmedCurve)aC3DETrim=new Geom_TrimmedCurve(aC3DE, aTFirst, aTLast);
193 Handle(Geom2d_Curve) aC2D, aC2DA;
197 BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aFFWD);
198 BOPTools_Tools2D::CurveOnSurface(aE, aFFWD, aC2D, aOutFirst, aOutLast, aOutTol, Standard_True);
200 if (aC3DE->IsPeriodic()) {
201 BOPTools_Tools2D::AdjustPCurveOnFace(aFFWD, aTFirst, aTLast, aC2D, aC2DA);
204 BOPTools_Tools2D::AdjustPCurveOnFace(aFFWD, aC3DETrim, aC2D, aC2DA);
207 aBB.UpdateEdge(aE, aC2DA, aFFWD, aTolFact);
208 BRepLib::SameParameter(aE);
211 //modified by NIZNHY-PKV Thu Nov 16 10:46:55 2006t
212 //=======================================================================
213 // function: IsSplitInOnFace
215 //=======================================================================
216 Standard_Boolean NMTTools_Tools::IsSplitInOnFace(const TopoDS_Edge& aE,
217 const TopoDS_Face& aF,
218 const Handle(IntTools_Context)& aContext)
220 Standard_Boolean bFlag;
221 Standard_Real aT, aTolE, aTolF, aTol, aDist, aU, aV;
225 aTolE=BRep_Tool::Tolerance(aE);
226 aTolF=BRep_Tool::Tolerance(aF);
229 GeomAPI_ProjectPointOnSurf& aProjector=aContext->ProjPS(aF);
231 aT=BOPTools_Tools2D::IntermediatePoint(aE);
232 BOPTools_Tools::PointOnEdge(aE, aT, aP);
234 aProjector.Perform(aP);
235 bFlag=aProjector.IsDone();
240 aDist=aProjector.LowerDistance();
241 bFlag=(aDist <= aTol);
246 aProjector.LowerDistanceParameters(aU, aV);
247 aP2D.SetCoord(aU, aV);
248 bFlag=aContext->IsPointInOnFace (aF, aP2D);
251 //=======================================================================
252 // function: NMTTools_Tools::MakeNewVertex
254 //=======================================================================
255 void NMTTools_Tools::MakeNewVertex(const TopTools_ListOfShape& aLVs,
256 TopoDS_Vertex& aNewVertex)
258 Standard_Integer aNb;
259 Standard_Real aTi, aDi, aDmax=-1.e5;
261 gp_XYZ aXYZ(0.,0.,0.), aXYZi;
262 TopTools_ListIteratorOfListOfShape anIt;
269 anIt.Initialize(aLVs);
270 for (; anIt.More(); anIt.Next()) {
271 TopoDS_Vertex aVi=TopoDS::Vertex(anIt.Value());
272 aPi=BRep_Tool::Pnt(aVi);
277 aXYZ.Divide((Standard_Real)aNb);
280 anIt.Initialize(aLVs);
281 for (; anIt.More(); anIt.Next()) {
282 TopoDS_Vertex aVi=TopoDS::Vertex(anIt.Value());
283 aPi=BRep_Tool::Pnt(aVi);
284 aTi=BRep_Tool::Tolerance(aVi);
285 aDi=aP.Distance(aPi);
292 aBB.MakeVertex (aNewVertex, aP, aDmax);
294 //=======================================================================
295 // function: FindChains
297 //=======================================================================
298 void NMTTools_Tools::FindChains(const BOPTools_CArray1OfSSInterference& FFs,
299 BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMapChains)
301 Standard_Boolean bIsTangentFaces;
302 Standard_Integer j, aNb, anIndex1, anIndex2;
303 BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger aMCV;
306 for (j=1; j<=aNb; ++j) {
307 const BOPTools_SSInterference& aFF=FFs(j);
309 bIsTangentFaces=aFF.IsTangentFaces();
310 if (!bIsTangentFaces) {
314 aFF.Indices(anIndex1, anIndex2);
316 if (aMCV.Contains(anIndex1)) {
317 TColStd_IndexedMapOfInteger& aMV=aMCV.ChangeFromKey(anIndex1);
322 TColStd_IndexedMapOfInteger aMV;
325 aMCV.Add(anIndex1, aMV);
328 if (aMCV.Contains(anIndex2)) {
329 TColStd_IndexedMapOfInteger& aMV=aMCV.ChangeFromKey(anIndex2);
334 TColStd_IndexedMapOfInteger aMV;
337 aMCV.Add(anIndex2, aMV);
340 NMTTools_Tools::FindChains(aMCV, aMapChains);
342 //=======================================================================
343 // function: FindChains
345 //=======================================================================
346 void NMTTools_Tools::FindChains(const BOPTools_CArray1OfVVInterference& VVs,
347 BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMapChains)
349 Standard_Integer j, aNb, anIndex1, anIndex2;
350 BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger aMCV;
353 for (j=1; j<=aNb; ++j) {
354 const BOPTools_VVInterference& VV=VVs(j);
355 VV.Indices(anIndex1, anIndex2);
357 if (aMCV.Contains(anIndex1)) {
358 TColStd_IndexedMapOfInteger& aMV=aMCV.ChangeFromKey(anIndex1);
363 TColStd_IndexedMapOfInteger aMV;
366 aMCV.Add(anIndex1, aMV);
369 if (aMCV.Contains(anIndex2)) {
370 TColStd_IndexedMapOfInteger& aMV=aMCV.ChangeFromKey(anIndex2);
375 TColStd_IndexedMapOfInteger aMV;
378 aMCV.Add(anIndex2, aMV);
381 NMTTools_Tools::FindChains(aMCV, aMapChains);
384 //=======================================================================
385 // function: FindChains
387 //=======================================================================
388 void NMTTools_Tools::FindChains(const BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMCV,
389 BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMapChains)
391 Standard_Integer i, j, aNbCV, aNbV, iV, iVx;
392 TColStd_IndexedMapOfInteger aProcessed, aChain;
395 for (i=1; i<=aNbCV; ++i) {
397 if (aProcessed.Contains(iV)) {
404 const TColStd_IndexedMapOfInteger& aMV=aMCV(i);
406 for (j=1; j<=aNbV; ++j) {
408 ProcessBlock(iVx, aMCV, aProcessed, aChain);
410 aMapChains.Add(i, aChain);
414 //=======================================================================
415 // function: ProcessBlock
417 //=======================================================================
418 void ProcessBlock(const Standard_Integer iV,
419 const BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMCV,
420 TColStd_IndexedMapOfInteger& aProcessed,
421 TColStd_IndexedMapOfInteger& aChain)
423 Standard_Integer j, aNbV, iVx;
425 if (aProcessed.Contains(iV)) {
431 const TColStd_IndexedMapOfInteger& aMV=aMCV.FindFromKey(iV);
433 for (j=1; j<=aNbV; ++j) {
435 ProcessBlock(iVx, aMCV, aProcessed, aChain);
438 //=======================================================================
439 // function: AreFacesSameDomain
441 //=======================================================================
442 Standard_Boolean NMTTools_Tools::AreFacesSameDomain(const TopoDS_Face& aF1x,
443 const TopoDS_Face& aF2y,
444 const Handle(IntTools_Context)& aCtx)
446 Standard_Boolean bFlag;
447 // Modified Thu Sep 14 14:35:18 2006
448 // Contribution of Samtech www.samcef.com BEGIN
449 Standard_Integer aNbE1, aNbE2;
450 Standard_Real aTolF1, aTolF2, aTol;
453 TopoDS_Face aF1, aF2;
454 TopExp_Explorer aExp;
455 TopTools_MapOfShape aME1, aME2;
456 TopTools_MapIteratorOfMapOfShape aIt;
458 bFlag=Standard_False;
459 // Contribution of Samtech www.samcef.com END
462 aF1.Orientation(TopAbs_FORWARD);
464 aF2.Orientation(TopAbs_FORWARD);
466 // Modified Thu Sep 14 14:35:18 2006
467 // Contribution of Samtech www.samcef.com BEGIN
470 aExp.Init(aF1, TopAbs_EDGE);
471 for (; aExp.More(); aExp.Next()) {
472 const TopoDS_Edge& aE=TopoDS::Edge(aExp.Current());
473 if (!BRep_Tool::Degenerated(aE)) {
478 aExp.Init(aF2, TopAbs_EDGE);
479 for (; aExp.More(); aExp.Next()) {
480 const TopoDS_Edge& aE=TopoDS::Edge(aExp.Current());
481 if (!BRep_Tool::Degenerated(aE)) {
482 if (!aME1.Contains(aE)) {
489 // Contribution of Samtech www.samcef.com END
494 if(!aNbE1 || !aNbE2){
503 aTolF1=BRep_Tool::Tolerance(aF1);
504 aTolF2=BRep_Tool::Tolerance(aF2);
507 aIt.Initialize(aME1);
508 for (; aIt.More(); aIt.Next()) {
509 const TopoDS_Edge& aE=TopoDS::Edge(aIt.Key());
510 BOPTools_Tools3D::PointNearEdge(aE, aF1, aP2D, aP);
511 bFlag=aCtx->IsValidPointForFace(aP, aF2, aTol);
517 //=======================================================================
518 // function: FindChains
520 //=======================================================================
521 void NMTTools_Tools::FindChains(const NMTTools_ListOfCoupleOfShape& aLCS,
522 NMTTools_IndexedDataMapOfShapeIndexedMapOfShape& aMapChains)
524 NMTTools_ListIteratorOfListOfCoupleOfShape aItCS;
525 NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMCV;
527 aItCS.Initialize(aLCS);
528 for (; aItCS.More(); aItCS.Next()) {
529 const NMTTools_CoupleOfShape& aCS=aItCS.Value();
531 const TopoDS_Shape& aF1=aCS.Shape1();
532 const TopoDS_Shape& aF2=aCS.Shape2();
535 if (aMCV.Contains(aF1)) {
536 TopTools_IndexedMapOfShape& aMV=aMCV.ChangeFromKey(aF1);
541 TopTools_IndexedMapOfShape aMV;
547 if (aMCV.Contains(aF2)) {
548 TopTools_IndexedMapOfShape& aMV=aMCV.ChangeFromKey(aF2);
553 TopTools_IndexedMapOfShape aMV;
559 NMTTools_Tools::FindChains(aMCV, aMapChains);
561 //=======================================================================
562 // function: FindChains
564 //=======================================================================
565 void NMTTools_Tools::FindChains(const NMTTools_IndexedDataMapOfShapeIndexedMapOfShape& aMCV,
566 NMTTools_IndexedDataMapOfShapeIndexedMapOfShape& aMapChains)
568 Standard_Integer i, j, aNbCV, aNbV;
569 TopTools_IndexedMapOfShape aProcessed, aChain;
572 for (i=1; i<=aNbCV; ++i) {
573 const TopoDS_Shape& aF=aMCV.FindKey(i);
574 if (aProcessed.Contains(aF)) {
581 const TopTools_IndexedMapOfShape& aMV=aMCV(i);
583 for (j=1; j<=aNbV; ++j) {
584 const TopoDS_Shape& aFx=aMV(j);
585 ProcessBlock(aFx, aMCV, aProcessed, aChain);
587 aMapChains.Add(aF, aChain);
591 //=======================================================================
592 // function: ProcessBlock
594 //=======================================================================
595 void ProcessBlock(const TopoDS_Shape& aF,
596 const NMTTools_IndexedDataMapOfShapeIndexedMapOfShape& aMCV,
597 TopTools_IndexedMapOfShape& aProcessed,
598 TopTools_IndexedMapOfShape& aChain)
600 Standard_Integer j, aNbV;
602 if (aProcessed.Contains(aF)) {
608 const TopTools_IndexedMapOfShape& aMV=aMCV.FindFromKey(aF);
610 for (j=1; j<=aNbV; ++j) {
611 const TopoDS_Shape& aFx=aMV(j);
612 ProcessBlock(aFx, aMCV, aProcessed, aChain);