1 // Created on: 2016-07-04
2 // Copyright (c) 2016 OPEN CASCADE SAS
3 // Created by: Oleg AGASHIN
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <BRepMesh_FaceChecker.hxx>
17 #include <IMeshData_Wire.hxx>
18 #include <IMeshData_Edge.hxx>
19 #include <OSD_Parallel.hxx>
20 #include <BRepMesh_GeomTool.hxx>
24 const Standard_Real MaxTangentAngle = 5. * M_PI / 180.;
26 //! Functor to be used to fill segments and bounding box tree in parallel.
31 SegmentsFiller(const IMeshData::IFaceHandle& theDFace,
32 Handle(BRepMesh_FaceChecker::ArrayOfSegments)& theWiresSegments,
33 Handle(BRepMesh_FaceChecker::ArrayOfBndBoxTree)& theWiresBndBoxTree)
35 myWiresSegments(theWiresSegments),
36 myWiresBndBoxTree(theWiresBndBoxTree)
38 myWiresSegments = new BRepMesh_FaceChecker::ArrayOfSegments (0, myDFace->WiresNb() - 1);
39 myWiresBndBoxTree = new BRepMesh_FaceChecker::ArrayOfBndBoxTree (0, myDFace->WiresNb() - 1);
42 //! Performs initialization of wire with the given index.
43 void operator()(const Standard_Integer theWireIndex) const
45 const IMeshData::IWireHandle& aDWire = myDFace->GetWire(theWireIndex);
47 Handle(NCollection_IncAllocator) aTmpAlloc1 = new NCollection_IncAllocator();
49 Handle(BRepMesh_FaceChecker::Segments) aSegments =
50 new BRepMesh_FaceChecker::Segments(aDWire->EdgesNb(), aTmpAlloc1);
51 Handle(IMeshData::BndBox2dTree) aBndBoxTree = new IMeshData::BndBox2dTree(aTmpAlloc1);
53 myWiresSegments ->ChangeValue(theWireIndex) = aSegments;
54 myWiresBndBoxTree->ChangeValue(theWireIndex) = aBndBoxTree;
56 Handle(NCollection_IncAllocator) aTmpAlloc2 = new NCollection_IncAllocator();
57 IMeshData::BndBox2dTreeFiller aBndBoxTreeFiller(*aBndBoxTree, aTmpAlloc2);
59 for (Standard_Integer aEdgeIt = 0; aEdgeIt < aDWire->EdgesNb(); ++aEdgeIt)
61 // TODO: check 2d wire for consistency.
63 const IMeshData::IEdgePtr& aDEdge = aDWire->GetEdge(aEdgeIt);
64 const IMeshData::IPCurveHandle& aPCurve = aDEdge->GetPCurve(myDFace.get(), aDWire->GetEdgeOrientation(aEdgeIt));
66 for (Standard_Integer aPointIt = 1; aPointIt < aPCurve->ParametersNb(); ++aPointIt)
68 gp_Pnt2d& aPnt1 = aPCurve->GetPoint(aPointIt - 1);
69 gp_Pnt2d& aPnt2 = aPCurve->GetPoint(aPointIt);
74 aBox.Enlarge(Precision::Confusion());
76 aBndBoxTreeFiller.Add(aSegments->Size(), aBox);
77 aSegments->Append(BRepMesh_FaceChecker::Segment(aDEdge, &aPnt1, &aPnt2));
81 aBndBoxTreeFiller.Fill();
86 SegmentsFiller (const SegmentsFiller& theOther);
88 void operator=(const SegmentsFiller& theOther);
92 const IMeshData::IFaceHandle& myDFace;
93 Handle(BRepMesh_FaceChecker::ArrayOfSegments)& myWiresSegments;
94 Handle(BRepMesh_FaceChecker::ArrayOfBndBoxTree)& myWiresBndBoxTree;
98 //! Used to identify segments with overlapped bounding boxes.
99 class BndBox2dTreeSelector : public IMeshData::BndBox2dTree::Selector
103 BndBox2dTreeSelector(const Standard_Real theTolerance)
104 : myMaxLoopSize(M_PI * theTolerance * theTolerance),
105 mySelfSegmentIndex(-1),
106 myIndices(256, new NCollection_IncAllocator(IMeshData::MEMORY_BLOCK_SIZE_HUGE))
110 //! Sets working set of segments.
111 void SetSegments(const Handle(BRepMesh_FaceChecker::Segments)& theSegments)
113 mySegments = theSegments;
116 //! Resets current selector.
117 void Reset(const BRepMesh_FaceChecker::Segment* theSegment,
118 const Standard_Integer theSelfSegmentIndex)
122 mySelfSegmentIndex = theSelfSegmentIndex;
123 mySegment = theSegment;
126 myBox.Add(*mySegment->Point1);
127 myBox.Add(*mySegment->Point2);
128 myBox.Enlarge(Precision::Confusion());
131 //! Indicates should the given box be rejected or not.
132 virtual Standard_Boolean Reject(const Bnd_Box2d& theBox) const
134 return myBox.IsOut(theBox);
137 //! Accepts segment with the given index in case if it fits conditions.
138 virtual Standard_Boolean Accept(const Standard_Integer& theSegmentIndex)
140 const BRepMesh_FaceChecker::Segment& aSegment = mySegments->Value(theSegmentIndex);
143 const BRepMesh_GeomTool::IntFlag aIntStatus = BRepMesh_GeomTool::IntSegSeg(
144 mySegment->Point1->XY(), mySegment->Point2->XY(),
145 aSegment.Point1->XY(), aSegment.Point2->XY(),
146 Standard_False, Standard_False, aIntPnt);
148 if (aIntStatus == BRepMesh_GeomTool::Cross)
150 const Standard_Real aAngle = gp_Vec2d(mySegment->Point1->XY(), mySegment->Point2->XY()).Angle(
151 gp_Vec2d(aSegment.Point1->XY(), aSegment.Point2->XY()));
153 if (Abs(aAngle) < MaxTangentAngle)
155 return Standard_False;
158 if (mySelfSegmentIndex != -1)
161 Standard_Real aSumS = 0.;
162 const gp_XY& aRefPnt = aIntPnt.Coord();
163 for (Standard_Integer i = mySelfSegmentIndex; i < theSegmentIndex; ++i)
165 const BRepMesh_FaceChecker::Segment& aCurrSegment = mySegments->Value(i);
166 gp_XY aCurVec = aCurrSegment.Point2->XY() - aRefPnt;
168 if (aCurVec.SquareModulus() < gp::Resolution())
171 if (aPrevVec.SquareModulus() > gp::Resolution())
172 aSumS += aPrevVec ^ aCurVec;
177 if (Abs(aSumS / 2.) < myMaxLoopSize)
179 return Standard_False;
183 myIndices.Append(theSegmentIndex);
184 return Standard_True;
187 return Standard_False;
190 //! Returns indices of intersecting segments.
191 const IMeshData::VectorOfInteger& Indices() const
198 Standard_Real myMaxLoopSize;
199 Standard_Integer mySelfSegmentIndex;
200 Handle(BRepMesh_FaceChecker::Segments) mySegments;
201 const BRepMesh_FaceChecker::Segment* mySegment;
203 IMeshData::VectorOfInteger myIndices;
207 //=======================================================================
208 //function : Constructor
210 //=======================================================================
211 BRepMesh_FaceChecker::BRepMesh_FaceChecker(
212 const IMeshData::IFaceHandle& theFace,
213 const IMeshTools_Parameters& theParameters)
215 myParameters(theParameters)
219 //=======================================================================
220 //function : Destructor
222 //=======================================================================
223 BRepMesh_FaceChecker::~BRepMesh_FaceChecker()
227 //=======================================================================
230 //=======================================================================
231 Standard_Boolean BRepMesh_FaceChecker::Perform()
233 myIntersectingEdges = new IMeshData::MapOfIEdgePtr;
236 OSD_Parallel::For(0, myDFace->WiresNb(), *this, !isParallel());
239 myWiresBndBoxTree.Nullify();
240 myWiresSegments.Nullify();
241 myWiresIntersectingEdges.Nullify();
242 return myIntersectingEdges->IsEmpty();
245 //=======================================================================
246 //function : collectSegments
248 //=======================================================================
249 void BRepMesh_FaceChecker::collectSegments()
251 SegmentsFiller aSegmentsFiller(myDFace, myWiresSegments, myWiresBndBoxTree);
252 OSD_Parallel::For(0, myDFace->WiresNb(), aSegmentsFiller, !isParallel());
254 myWiresIntersectingEdges = new ArrayOfMapOfIEdgePtr(0, myDFace->WiresNb() - 1);
257 //=======================================================================
260 //=======================================================================
261 void BRepMesh_FaceChecker::perform(const Standard_Integer theWireIndex) const
263 const Handle(Segments)& aSegments1 = myWiresSegments->Value(theWireIndex);
264 Handle(IMeshData::MapOfIEdgePtr)& aIntersections = myWiresIntersectingEdges->ChangeValue(theWireIndex);
266 // TODO: Tolerance is set to twice value of face deflection in order to fit regressions.
267 BndBox2dTreeSelector aSelector(2 * myDFace->GetDeflection());
268 for (Standard_Integer aWireIt = theWireIndex; aWireIt < myDFace->WiresNb(); ++aWireIt)
270 const Handle(IMeshData::BndBox2dTree)& aBndBoxTree2 = myWiresBndBoxTree->Value(aWireIt);
271 const Handle(Segments)& aSegments2 = myWiresSegments->Value(aWireIt);
273 aSelector.SetSegments(aSegments2);
274 for (Standard_Integer aSegmentIt = 0; aSegmentIt < aSegments1->Size(); ++aSegmentIt)
276 const BRepMesh_FaceChecker::Segment& aSegment1 = aSegments1->Value(aSegmentIt);
277 aSelector.Reset(&aSegment1, (aWireIt == theWireIndex) ? aSegmentIt : -1);
278 if (aBndBoxTree2->Select(aSelector) != 0)
280 if (aIntersections.IsNull())
282 aIntersections = new IMeshData::MapOfIEdgePtr;
285 aIntersections->Add(aSegment1.EdgePtr);
287 const IMeshData::VectorOfInteger& aSegments = aSelector.Indices();
288 for (Standard_Integer aSelIt = 0; aSelIt < aSegments.Size(); ++aSelIt)
290 const BRepMesh_FaceChecker::Segment& aSegment2 = aSegments2->Value(aSegments(aSelIt));
291 aIntersections->Add(aSegment2.EdgePtr);
298 //=======================================================================
299 //function : collectResult
301 //=======================================================================
302 void BRepMesh_FaceChecker::collectResult()
304 for (Standard_Integer aWireIt = 0; aWireIt < myDFace->WiresNb(); ++aWireIt)
306 const Handle(IMeshData::MapOfIEdgePtr)& aEdges = myWiresIntersectingEdges->Value(aWireIt);
307 if (!aEdges.IsNull())
309 myIntersectingEdges->Unite(*aEdges);