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,
50 const Message_ProgressRange& theRange)
57 myParameters = theParameters;
58 myAllocator = new NCollection_IncAllocator(IMeshData::MEMORY_BLOCK_SIZE_HUGE);
59 myStructure = new BRepMesh_DataStructureOfDelaun(myAllocator);
60 myNodesMap = new VectorOfPnt(256, myAllocator);
61 myUsedNodes = new DMapOfIntegerInteger(1, myAllocator);
63 if (initDataStructure())
69 generateMesh(theRange);
70 commitSurfaceTriangulation();
73 catch (Standard_Failure const& /*theExeption*/)
77 myDFace.Nullify(); // Do not hold link to face.
78 myStructure.Nullify();
79 myNodesMap .Nullify();
80 myUsedNodes.Nullify();
81 myAllocator.Nullify();
84 //=======================================================================
85 //function : initDataStructure
87 //=======================================================================
88 Standard_Boolean BRepMesh_BaseMeshAlgo::initDataStructure()
90 for (Standard_Integer aWireIt = 0; aWireIt < myDFace->WiresNb(); ++aWireIt)
92 const IMeshData::IWireHandle& aDWire = myDFace->GetWire(aWireIt);
93 if (aDWire->IsSet(IMeshData_SelfIntersectingWire))
95 // TODO: here we can add points of self-intersecting wire as fixed points
96 // in order to keep consistency of nodes with adjacent faces.
100 for (Standard_Integer aEdgeIt = 0; aEdgeIt < aDWire->EdgesNb(); ++aEdgeIt)
102 const IMeshData::IEdgeHandle aDEdge = aDWire->GetEdge(aEdgeIt);
103 const IMeshData::ICurveHandle& aCurve = aDEdge->GetCurve();
104 const IMeshData::IPCurveHandle& aPCurve = aDEdge->GetPCurve(
105 myDFace.get(), aDWire->GetEdgeOrientation(aEdgeIt));
107 const TopAbs_Orientation aOri = fixSeamEdgeOrientation(aDEdge, aPCurve);
109 Standard_Integer aPrevNodeIndex = -1;
110 const Standard_Integer aLastPoint = aPCurve->ParametersNb() - 1;
111 for (Standard_Integer aPointIt = 0; aPointIt <= aLastPoint; ++aPointIt)
113 const Standard_Integer aNodeIndex = registerNode(
114 aCurve ->GetPoint(aPointIt),
115 aPCurve->GetPoint(aPointIt),
116 BRepMesh_Frontier, Standard_False/*aPointIt > 0 && aPointIt < aLastPoint*/);
118 aPCurve->GetIndex(aPointIt) = aNodeIndex;
119 myUsedNodes->Bind(aNodeIndex, aNodeIndex);
121 if (aPrevNodeIndex != -1 && aPrevNodeIndex != aNodeIndex)
123 const Standard_Integer aLinksNb = myStructure->NbLinks();
124 const Standard_Integer aLinkIndex = addLinkToMesh(aPrevNodeIndex, aNodeIndex, aOri);
125 if (aWireIt != 0 && aLinkIndex <= aLinksNb)
127 // Prevent holes around wire of zero area.
128 BRepMesh_Edge& aLink = const_cast<BRepMesh_Edge&>(myStructure->GetLink(aLinkIndex));
129 aLink.SetMovability(BRepMesh_Fixed);
133 aPrevNodeIndex = aNodeIndex;
138 return Standard_True;
141 //=======================================================================
142 // Function: registerNode
144 //=======================================================================
145 Standard_Integer BRepMesh_BaseMeshAlgo::registerNode(
146 const gp_Pnt& thePoint,
147 const gp_Pnt2d& thePoint2d,
148 const BRepMesh_DegreeOfFreedom theMovability,
149 const Standard_Boolean isForceAdd)
151 const Standard_Integer aNodeIndex = addNodeToStructure(
152 thePoint2d, myNodesMap->Size(), theMovability, isForceAdd);
154 if (aNodeIndex > myNodesMap->Size())
156 myNodesMap->Append(thePoint);
162 //=======================================================================
165 //=======================================================================
166 Standard_Integer BRepMesh_BaseMeshAlgo::addNodeToStructure(
167 const gp_Pnt2d& thePoint,
168 const Standard_Integer theLocation3d,
169 const BRepMesh_DegreeOfFreedom theMovability,
170 const Standard_Boolean isForceAdd)
172 BRepMesh_Vertex aNode(thePoint.XY(), theLocation3d, theMovability);
173 return myStructure->AddNode(aNode, isForceAdd);
176 //=======================================================================
177 //function : addLinkToMesh
179 //=======================================================================
180 Standard_Integer BRepMesh_BaseMeshAlgo::addLinkToMesh(
181 const Standard_Integer theFirstNodeId,
182 const Standard_Integer theLastNodeId,
183 const TopAbs_Orientation theOrientation)
185 Standard_Integer aLinkIndex;
186 if (theOrientation == TopAbs_REVERSED)
187 aLinkIndex = myStructure->AddLink(BRepMesh_Edge(theLastNodeId, theFirstNodeId, BRepMesh_Frontier));
188 else if (theOrientation == TopAbs_INTERNAL)
189 aLinkIndex = myStructure->AddLink(BRepMesh_Edge(theFirstNodeId, theLastNodeId, BRepMesh_Fixed));
191 aLinkIndex = myStructure->AddLink(BRepMesh_Edge(theFirstNodeId, theLastNodeId, BRepMesh_Frontier));
193 return Abs(aLinkIndex);
196 //=======================================================================
197 //function : fixSeamEdgeOrientation
199 //=======================================================================
200 TopAbs_Orientation BRepMesh_BaseMeshAlgo::fixSeamEdgeOrientation(
201 const IMeshData::IEdgeHandle& theDEdge,
202 const IMeshData::IPCurveHandle& thePCurve) const
204 for (Standard_Integer aPCurveIt = 0; aPCurveIt < theDEdge->PCurvesNb(); ++aPCurveIt)
206 const IMeshData::IPCurveHandle& aPCurve = theDEdge->GetPCurve(aPCurveIt);
207 if (aPCurve->GetFace() == myDFace && thePCurve != aPCurve)
209 // Simple check that another pcurve of seam edge does not coincide with reference one.
210 const gp_Pnt2d& aPnt1_1 = thePCurve->GetPoint(0);
211 const gp_Pnt2d& aPnt2_1 = thePCurve->GetPoint(thePCurve->ParametersNb() - 1);
213 const gp_Pnt2d& aPnt1_2 = aPCurve->GetPoint(0);
214 const gp_Pnt2d& aPnt2_2 = aPCurve->GetPoint(aPCurve->ParametersNb() - 1);
216 const Standard_Real aSqDist1 = Min(aPnt1_1.SquareDistance(aPnt1_2), aPnt1_1.SquareDistance(aPnt2_2));
217 const Standard_Real aSqDist2 = Min(aPnt2_1.SquareDistance(aPnt1_2), aPnt2_1.SquareDistance(aPnt2_2));
218 if (aSqDist1 < Precision::SquareConfusion() &&
219 aSqDist2 < Precision::SquareConfusion())
221 return TopAbs_INTERNAL;
226 return thePCurve->GetOrientation();
229 //=======================================================================
230 //function : commitSurfaceTriangulation
232 //=======================================================================
233 void BRepMesh_BaseMeshAlgo::commitSurfaceTriangulation()
235 Handle(Poly_Triangulation) aTriangulation = collectTriangles();
236 if (aTriangulation.IsNull())
238 myDFace->SetStatus(IMeshData_Failure);
242 collectNodes(aTriangulation);
244 aTriangulation->Deflection(myDFace->GetDeflection());
245 BRepMesh_ShapeTool::AddInFace(myDFace->GetFace(), aTriangulation);
248 //=======================================================================
249 //function : collectTriangles
251 //=======================================================================
252 Handle(Poly_Triangulation) BRepMesh_BaseMeshAlgo::collectTriangles()
254 const IMeshData::MapOfInteger& aTriangles = myStructure->ElementsOfDomain();
255 if (aTriangles.IsEmpty())
257 return Handle(Poly_Triangulation)();
260 Poly_Array1OfTriangle aPolyTrianges(1, aTriangles.Extent());
261 IMeshData::IteratorOfMapOfInteger aTriIt(aTriangles);
262 for (Standard_Integer aTriangeId = 1; aTriIt.More(); aTriIt.Next(), ++aTriangeId)
264 const BRepMesh_Triangle& aCurElem = myStructure->GetElement(aTriIt.Key());
266 Standard_Integer aNode[3];
267 myStructure->ElementNodes(aCurElem, aNode);
269 for (Standard_Integer i = 0; i < 3; ++i)
271 if (!myUsedNodes->IsBound(aNode[i]))
273 myUsedNodes->Bind(aNode[i], myUsedNodes->Size() + 1);
276 aNode[i] = myUsedNodes->Find(aNode[i]);
279 aPolyTrianges(aTriangeId).Set(aNode[0], aNode[1], aNode[2]);
282 Handle(Poly_Triangulation) aTriangulation = new Poly_Triangulation(
283 myUsedNodes->Extent(), aTriangles.Extent(), Standard_True);
285 aTriangulation->ChangeTriangles() = aPolyTrianges;
286 return aTriangulation;
289 //=======================================================================
290 //function : collectNodes
292 //=======================================================================
293 void BRepMesh_BaseMeshAlgo::collectNodes(
294 const Handle(Poly_Triangulation)& theTriangulation)
297 TColgp_Array1OfPnt& aNodes = theTriangulation->ChangeNodes();
298 TColgp_Array1OfPnt2d& aNodes2d = theTriangulation->ChangeUVNodes();
300 for (Standard_Integer i = 1; i <= myNodesMap->Size(); ++i)
302 if (myUsedNodes->IsBound(i))
304 const BRepMesh_Vertex& aVertex = myStructure->GetNode(i);
306 const Standard_Integer aNodeIndex = myUsedNodes->Find(i);
307 aNodes(aNodeIndex) = myNodesMap->Value(aVertex.Location3d());
308 aNodes2d(aNodeIndex) = getNodePoint2d(aVertex);
313 //=======================================================================
314 // Function: getNodePoint2d
316 //=======================================================================
317 gp_Pnt2d BRepMesh_BaseMeshAlgo::getNodePoint2d(
318 const BRepMesh_Vertex& theVertex) const
320 return theVertex.Coord();