1 // Created on: 2016-04-19
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_BaseMeshAlgo.hxx>
17 #include <BRepMesh_DataStructureOfDelaun.hxx>
18 #include <IMeshData_Face.hxx>
19 #include <IMeshData_Wire.hxx>
20 #include <IMeshData_Edge.hxx>
21 #include <IMeshData_PCurve.hxx>
22 #include <IMeshData_Curve.hxx>
23 #include <BRepMesh_Delaun.hxx>
24 #include <BRepMesh_ShapeTool.hxx>
25 #include <Standard_ErrorHandler.hxx>
27 //=======================================================================
28 // Function: Constructor
30 //=======================================================================
31 BRepMesh_BaseMeshAlgo::BRepMesh_BaseMeshAlgo()
35 //=======================================================================
36 // Function: Destructor
38 //=======================================================================
39 BRepMesh_BaseMeshAlgo::~BRepMesh_BaseMeshAlgo()
43 //=======================================================================
46 //=======================================================================
47 void BRepMesh_BaseMeshAlgo::Perform(
48 const IMeshData::IFaceHandle& theDFace,
49 const IMeshTools_Parameters& theParameters)
56 myParameters = theParameters;
57 myAllocator = new NCollection_IncAllocator(IMeshData::MEMORY_BLOCK_SIZE_HUGE);
58 myStructure = new BRepMesh_DataStructureOfDelaun(myAllocator);
59 myNodesMap = new VectorOfPnt(256, myAllocator);
60 myUsedNodes = new DMapOfIntegerInteger(1, myAllocator);
62 if (initDataStructure())
65 commitSurfaceTriangulation();
68 catch (Standard_Failure& /*theExeption*/)
72 myDFace.Nullify(); // Do not hold link to face.
73 myStructure.Nullify();
74 myNodesMap .Nullify();
75 myUsedNodes.Nullify();
76 myAllocator.Nullify();
79 //=======================================================================
80 //function : initDataStructure
82 //=======================================================================
83 Standard_Boolean BRepMesh_BaseMeshAlgo::initDataStructure()
85 for (Standard_Integer aWireIt = 0; aWireIt < myDFace->WiresNb(); ++aWireIt)
87 const IMeshData::IWireHandle& aDWire = myDFace->GetWire(aWireIt);
88 if (aDWire->IsSet(IMeshData_SelfIntersectingWire))
90 // TODO: here we can add points of self-intersecting wire as fixed points
91 // in order to keep consistency of nodes with adjacent faces.
95 for (Standard_Integer aEdgeIt = 0; aEdgeIt < aDWire->EdgesNb(); ++aEdgeIt)
97 const IMeshData::IEdgeHandle aDEdge = aDWire->GetEdge(aEdgeIt);
98 const IMeshData::ICurveHandle& aCurve = aDEdge->GetCurve();
99 const IMeshData::IPCurveHandle& aPCurve = aDEdge->GetPCurve(
100 myDFace.get(), aDWire->GetEdgeOrientation(aEdgeIt));
102 const TopAbs_Orientation aOri = fixSeamEdgeOrientation(aDEdge, aPCurve);
104 Standard_Integer aPrevNodeIndex = -1;
105 const Standard_Integer aLastPoint = aPCurve->ParametersNb() - 1;
106 for (Standard_Integer aPointIt = 0; aPointIt <= aLastPoint; ++aPointIt)
108 const Standard_Integer aNodeIndex = registerNode(
109 aCurve ->GetPoint(aPointIt),
110 aPCurve->GetPoint(aPointIt),
111 BRepMesh_Frontier, Standard_False/*aPointIt > 0 && aPointIt < aLastPoint*/);
113 aPCurve->GetIndex(aPointIt) = aNodeIndex;
114 myUsedNodes->Bind(aNodeIndex, aNodeIndex);
116 if (aPrevNodeIndex != -1 && aPrevNodeIndex != aNodeIndex)
118 const Standard_Integer aLinksNb = myStructure->NbLinks();
119 const Standard_Integer aLinkIndex = addLinkToMesh(aPrevNodeIndex, aNodeIndex, aOri);
120 if (aWireIt != 0 && aLinkIndex <= aLinksNb)
122 // Prevent holes around wire of zero area.
123 BRepMesh_Edge& aLink = const_cast<BRepMesh_Edge&>(myStructure->GetLink(aLinkIndex));
124 aLink.SetMovability(BRepMesh_Fixed);
128 aPrevNodeIndex = aNodeIndex;
133 return Standard_True;
136 //=======================================================================
137 // Function: registerNode
139 //=======================================================================
140 Standard_Integer BRepMesh_BaseMeshAlgo::registerNode(
141 const gp_Pnt& thePoint,
142 const gp_Pnt2d& thePoint2d,
143 const BRepMesh_DegreeOfFreedom theMovability,
144 const Standard_Boolean isForceAdd)
146 const Standard_Integer aNodeIndex = addNodeToStructure(
147 thePoint2d, myNodesMap->Size(), theMovability, isForceAdd);
149 if (aNodeIndex > myNodesMap->Size())
151 myNodesMap->Append(thePoint);
157 //=======================================================================
160 //=======================================================================
161 Standard_Integer BRepMesh_BaseMeshAlgo::addNodeToStructure(
162 const gp_Pnt2d& thePoint,
163 const Standard_Integer theLocation3d,
164 const BRepMesh_DegreeOfFreedom theMovability,
165 const Standard_Boolean isForceAdd)
167 BRepMesh_Vertex aNode(thePoint.XY(), theLocation3d, theMovability);
168 return myStructure->AddNode(aNode, isForceAdd);
171 //=======================================================================
172 //function : addLinkToMesh
174 //=======================================================================
175 Standard_Integer BRepMesh_BaseMeshAlgo::addLinkToMesh(
176 const Standard_Integer theFirstNodeId,
177 const Standard_Integer theLastNodeId,
178 const TopAbs_Orientation theOrientation)
180 Standard_Integer aLinkIndex;
181 if (theOrientation == TopAbs_REVERSED)
182 aLinkIndex = myStructure->AddLink(BRepMesh_Edge(theLastNodeId, theFirstNodeId, BRepMesh_Frontier));
183 else if (theOrientation == TopAbs_INTERNAL)
184 aLinkIndex = myStructure->AddLink(BRepMesh_Edge(theFirstNodeId, theLastNodeId, BRepMesh_Fixed));
186 aLinkIndex = myStructure->AddLink(BRepMesh_Edge(theFirstNodeId, theLastNodeId, BRepMesh_Frontier));
188 return Abs(aLinkIndex);
191 //=======================================================================
192 //function : fixSeamEdgeOrientation
194 //=======================================================================
195 TopAbs_Orientation BRepMesh_BaseMeshAlgo::fixSeamEdgeOrientation(
196 const IMeshData::IEdgeHandle& theDEdge,
197 const IMeshData::IPCurveHandle& thePCurve) const
199 for (Standard_Integer aPCurveIt = 0; aPCurveIt < theDEdge->PCurvesNb(); ++aPCurveIt)
201 const IMeshData::IPCurveHandle& aPCurve = theDEdge->GetPCurve(aPCurveIt);
202 if (aPCurve->GetFace() == myDFace && thePCurve != aPCurve)
204 // Simple check that another pcurve of seam edge does not coincide with reference one.
205 const gp_Pnt2d& aPnt1_1 = thePCurve->GetPoint(0);
206 const gp_Pnt2d& aPnt2_1 = thePCurve->GetPoint(thePCurve->ParametersNb() - 1);
208 const gp_Pnt2d& aPnt1_2 = aPCurve->GetPoint(0);
209 const gp_Pnt2d& aPnt2_2 = aPCurve->GetPoint(aPCurve->ParametersNb() - 1);
211 const Standard_Real aSqDist1 = Min(aPnt1_1.SquareDistance(aPnt1_2), aPnt1_1.SquareDistance(aPnt2_2));
212 const Standard_Real aSqDist2 = Min(aPnt2_1.SquareDistance(aPnt1_2), aPnt2_1.SquareDistance(aPnt2_2));
213 if (aSqDist1 < Precision::SquareConfusion() &&
214 aSqDist2 < Precision::SquareConfusion())
216 return TopAbs_INTERNAL;
221 return thePCurve->GetOrientation();
224 //=======================================================================
225 //function : commitSurfaceTriangulation
227 //=======================================================================
228 void BRepMesh_BaseMeshAlgo::commitSurfaceTriangulation()
230 Handle(Poly_Triangulation) aTriangulation = collectTriangles();
231 if (aTriangulation.IsNull())
233 myDFace->SetStatus(IMeshData_Failure);
237 collectNodes(aTriangulation);
239 aTriangulation->Deflection(myDFace->GetDeflection());
240 BRepMesh_ShapeTool::AddInFace(myDFace->GetFace(), aTriangulation);
243 //=======================================================================
244 //function : collectTriangles
246 //=======================================================================
247 Handle(Poly_Triangulation) BRepMesh_BaseMeshAlgo::collectTriangles()
249 const IMeshData::MapOfInteger& aTriangles = myStructure->ElementsOfDomain();
250 if (aTriangles.IsEmpty())
252 return Handle(Poly_Triangulation)();
255 Poly_Array1OfTriangle aPolyTrianges(1, aTriangles.Extent());
256 IMeshData::IteratorOfMapOfInteger aTriIt(aTriangles);
257 for (Standard_Integer aTriangeId = 1; aTriIt.More(); aTriIt.Next(), ++aTriangeId)
259 const BRepMesh_Triangle& aCurElem = myStructure->GetElement(aTriIt.Key());
261 Standard_Integer aNode[3];
262 myStructure->ElementNodes(aCurElem, aNode);
264 for (Standard_Integer i = 0; i < 3; ++i)
266 if (!myUsedNodes->IsBound(aNode[i]))
268 myUsedNodes->Bind(aNode[i], myUsedNodes->Size() + 1);
271 aNode[i] = myUsedNodes->Find(aNode[i]);
274 aPolyTrianges(aTriangeId).Set(aNode[0], aNode[1], aNode[2]);
277 Handle(Poly_Triangulation) aTriangulation = new Poly_Triangulation(
278 myUsedNodes->Extent(), aTriangles.Extent(), Standard_True);
280 aTriangulation->ChangeTriangles() = aPolyTrianges;
281 return aTriangulation;
284 //=======================================================================
285 //function : collectNodes
287 //=======================================================================
288 void BRepMesh_BaseMeshAlgo::collectNodes(
289 const Handle(Poly_Triangulation)& theTriangulation)
292 TColgp_Array1OfPnt& aNodes = theTriangulation->ChangeNodes();
293 TColgp_Array1OfPnt2d& aNodes2d = theTriangulation->ChangeUVNodes();
295 for (Standard_Integer i = 1; i <= myNodesMap->Size(); ++i)
297 if (myUsedNodes->IsBound(i))
299 const BRepMesh_Vertex& aVertex = myStructure->GetNode(i);
301 const Standard_Integer aNodeIndex = myUsedNodes->Find(i);
302 aNodes(aNodeIndex) = myNodesMap->Value(aVertex.Location3d());
303 aNodes2d(aNodeIndex) = getNodePoint2d(aVertex);
308 //=======================================================================
309 // Function: getNodePoint2d
311 //=======================================================================
312 gp_Pnt2d BRepMesh_BaseMeshAlgo::getNodePoint2d(
313 const BRepMesh_Vertex& theVertex) const
315 return theVertex.Coord();