0029296: Data Exchange - implement import of mesh data from files in OBJ format
[occt.git] / src / BRepMesh / BRepMesh_BaseMeshAlgo.cxx
CommitLineData
7bd071ed 1// Created on: 2016-04-19
2// Copyright (c) 2016 OPEN CASCADE SAS
3// Created by: Oleg AGASHIN
4//
5// This file is part of Open CASCADE Technology software library.
6//
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.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
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>
26
27//=======================================================================
28// Function: Constructor
29// Purpose :
30//=======================================================================
31BRepMesh_BaseMeshAlgo::BRepMesh_BaseMeshAlgo()
32{
33}
34
35//=======================================================================
36// Function: Destructor
37// Purpose :
38//=======================================================================
39BRepMesh_BaseMeshAlgo::~BRepMesh_BaseMeshAlgo()
40{
41}
42
43//=======================================================================
44// Function: Perform
45// Purpose :
46//=======================================================================
47void BRepMesh_BaseMeshAlgo::Perform(
48 const IMeshData::IFaceHandle& theDFace,
49 const IMeshTools_Parameters& theParameters)
50{
51 try
52 {
53 OCC_CATCH_SIGNALS
54
55 myDFace = theDFace;
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);
61
62 if (initDataStructure())
63 {
64 generateMesh();
65 commitSurfaceTriangulation();
66 }
67 }
c2a25d52 68 catch (Standard_Failure const& /*theExeption*/)
7bd071ed 69 {
70 }
71
72 myDFace.Nullify(); // Do not hold link to face.
73 myStructure.Nullify();
74 myNodesMap .Nullify();
75 myUsedNodes.Nullify();
76 myAllocator.Nullify();
77}
78
79//=======================================================================
80//function : initDataStructure
81//purpose :
82//=======================================================================
83Standard_Boolean BRepMesh_BaseMeshAlgo::initDataStructure()
84{
85 for (Standard_Integer aWireIt = 0; aWireIt < myDFace->WiresNb(); ++aWireIt)
86 {
87 const IMeshData::IWireHandle& aDWire = myDFace->GetWire(aWireIt);
88 if (aDWire->IsSet(IMeshData_SelfIntersectingWire))
89 {
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.
92 continue;
93 }
94
95 for (Standard_Integer aEdgeIt = 0; aEdgeIt < aDWire->EdgesNb(); ++aEdgeIt)
96 {
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));
101
102 const TopAbs_Orientation aOri = fixSeamEdgeOrientation(aDEdge, aPCurve);
103
104 Standard_Integer aPrevNodeIndex = -1;
105 const Standard_Integer aLastPoint = aPCurve->ParametersNb() - 1;
106 for (Standard_Integer aPointIt = 0; aPointIt <= aLastPoint; ++aPointIt)
107 {
108 const Standard_Integer aNodeIndex = registerNode(
109 aCurve ->GetPoint(aPointIt),
110 aPCurve->GetPoint(aPointIt),
111 BRepMesh_Frontier, Standard_False/*aPointIt > 0 && aPointIt < aLastPoint*/);
112
113 aPCurve->GetIndex(aPointIt) = aNodeIndex;
114 myUsedNodes->Bind(aNodeIndex, aNodeIndex);
115
116 if (aPrevNodeIndex != -1 && aPrevNodeIndex != aNodeIndex)
117 {
118 const Standard_Integer aLinksNb = myStructure->NbLinks();
119 const Standard_Integer aLinkIndex = addLinkToMesh(aPrevNodeIndex, aNodeIndex, aOri);
120 if (aWireIt != 0 && aLinkIndex <= aLinksNb)
121 {
122 // Prevent holes around wire of zero area.
123 BRepMesh_Edge& aLink = const_cast<BRepMesh_Edge&>(myStructure->GetLink(aLinkIndex));
124 aLink.SetMovability(BRepMesh_Fixed);
125 }
126 }
127
128 aPrevNodeIndex = aNodeIndex;
129 }
130 }
131 }
132
133 return Standard_True;
134}
135
136//=======================================================================
137// Function: registerNode
138// Purpose :
139//=======================================================================
140Standard_Integer BRepMesh_BaseMeshAlgo::registerNode(
141 const gp_Pnt& thePoint,
142 const gp_Pnt2d& thePoint2d,
143 const BRepMesh_DegreeOfFreedom theMovability,
144 const Standard_Boolean isForceAdd)
145{
146 const Standard_Integer aNodeIndex = addNodeToStructure(
147 thePoint2d, myNodesMap->Size(), theMovability, isForceAdd);
148
149 if (aNodeIndex > myNodesMap->Size())
150 {
151 myNodesMap->Append(thePoint);
152 }
153
154 return aNodeIndex;
155}
156
157//=======================================================================
158// Function: addNode
159// Purpose :
160//=======================================================================
161Standard_Integer BRepMesh_BaseMeshAlgo::addNodeToStructure(
162 const gp_Pnt2d& thePoint,
163 const Standard_Integer theLocation3d,
164 const BRepMesh_DegreeOfFreedom theMovability,
165 const Standard_Boolean isForceAdd)
166{
167 BRepMesh_Vertex aNode(thePoint.XY(), theLocation3d, theMovability);
168 return myStructure->AddNode(aNode, isForceAdd);
169}
170
171//=======================================================================
172//function : addLinkToMesh
173//purpose :
174//=======================================================================
175Standard_Integer BRepMesh_BaseMeshAlgo::addLinkToMesh(
176 const Standard_Integer theFirstNodeId,
177 const Standard_Integer theLastNodeId,
178 const TopAbs_Orientation theOrientation)
179{
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));
185 else
186 aLinkIndex = myStructure->AddLink(BRepMesh_Edge(theFirstNodeId, theLastNodeId, BRepMesh_Frontier));
187
188 return Abs(aLinkIndex);
189}
190
191//=======================================================================
192//function : fixSeamEdgeOrientation
193//purpose :
194//=======================================================================
195TopAbs_Orientation BRepMesh_BaseMeshAlgo::fixSeamEdgeOrientation(
196 const IMeshData::IEdgeHandle& theDEdge,
197 const IMeshData::IPCurveHandle& thePCurve) const
198{
199 for (Standard_Integer aPCurveIt = 0; aPCurveIt < theDEdge->PCurvesNb(); ++aPCurveIt)
200 {
201 const IMeshData::IPCurveHandle& aPCurve = theDEdge->GetPCurve(aPCurveIt);
202 if (aPCurve->GetFace() == myDFace && thePCurve != aPCurve)
203 {
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);
207
208 const gp_Pnt2d& aPnt1_2 = aPCurve->GetPoint(0);
209 const gp_Pnt2d& aPnt2_2 = aPCurve->GetPoint(aPCurve->ParametersNb() - 1);
210
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())
215 {
216 return TopAbs_INTERNAL;
217 }
218 }
219 }
220
221 return thePCurve->GetOrientation();
222}
223
224//=======================================================================
225//function : commitSurfaceTriangulation
226//purpose :
227//=======================================================================
228void BRepMesh_BaseMeshAlgo::commitSurfaceTriangulation()
229{
230 Handle(Poly_Triangulation) aTriangulation = collectTriangles();
231 if (aTriangulation.IsNull())
232 {
233 myDFace->SetStatus(IMeshData_Failure);
234 return;
235 }
236
237 collectNodes(aTriangulation);
238
239 aTriangulation->Deflection(myDFace->GetDeflection());
240 BRepMesh_ShapeTool::AddInFace(myDFace->GetFace(), aTriangulation);
241}
242
243//=======================================================================
244//function : collectTriangles
245//purpose :
246//=======================================================================
247Handle(Poly_Triangulation) BRepMesh_BaseMeshAlgo::collectTriangles()
248{
249 const IMeshData::MapOfInteger& aTriangles = myStructure->ElementsOfDomain();
250 if (aTriangles.IsEmpty())
251 {
252 return Handle(Poly_Triangulation)();
253 }
254
255 Poly_Array1OfTriangle aPolyTrianges(1, aTriangles.Extent());
256 IMeshData::IteratorOfMapOfInteger aTriIt(aTriangles);
257 for (Standard_Integer aTriangeId = 1; aTriIt.More(); aTriIt.Next(), ++aTriangeId)
258 {
259 const BRepMesh_Triangle& aCurElem = myStructure->GetElement(aTriIt.Key());
260
261 Standard_Integer aNode[3];
262 myStructure->ElementNodes(aCurElem, aNode);
263
264 for (Standard_Integer i = 0; i < 3; ++i)
265 {
266 if (!myUsedNodes->IsBound(aNode[i]))
267 {
268 myUsedNodes->Bind(aNode[i], myUsedNodes->Size() + 1);
269 }
270
271 aNode[i] = myUsedNodes->Find(aNode[i]);
272 }
273
274 aPolyTrianges(aTriangeId).Set(aNode[0], aNode[1], aNode[2]);
275 }
276
277 Handle(Poly_Triangulation) aTriangulation = new Poly_Triangulation(
278 myUsedNodes->Extent(), aTriangles.Extent(), Standard_True);
279
280 aTriangulation->ChangeTriangles() = aPolyTrianges;
281 return aTriangulation;
282}
283
284//=======================================================================
285//function : collectNodes
286//purpose :
287//=======================================================================
288void BRepMesh_BaseMeshAlgo::collectNodes(
289 const Handle(Poly_Triangulation)& theTriangulation)
290{
291 // Store mesh nodes
292 TColgp_Array1OfPnt& aNodes = theTriangulation->ChangeNodes();
293 TColgp_Array1OfPnt2d& aNodes2d = theTriangulation->ChangeUVNodes();
294
295 for (Standard_Integer i = 1; i <= myNodesMap->Size(); ++i)
296 {
297 if (myUsedNodes->IsBound(i))
298 {
299 const BRepMesh_Vertex& aVertex = myStructure->GetNode(i);
300
301 const Standard_Integer aNodeIndex = myUsedNodes->Find(i);
302 aNodes(aNodeIndex) = myNodesMap->Value(aVertex.Location3d());
303 aNodes2d(aNodeIndex) = getNodePoint2d(aVertex);
304 }
305 }
306}
307
308//=======================================================================
309// Function: getNodePoint2d
310// Purpose :
311//=======================================================================
312gp_Pnt2d BRepMesh_BaseMeshAlgo::getNodePoint2d(
313 const BRepMesh_Vertex& theVertex) const
314{
315 return theVertex.Coord();
316}