Skip to content

Commit 537b4e5

Browse files
authored
Merge pull request #1414 from rsm-gh/master
Tesselator minor coding fixes + Fix of a calculation error
2 parents 7a6fec7 + 5f52611 commit 537b4e5

4 files changed

Lines changed: 150 additions & 137 deletions

File tree

src/Display/WebGl/x3dom_renderer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ def __init__(
295295

296296
def compute(self):
297297
shape_tesselator = ShapeTesselator(self._shape)
298+
299+
if shape_tesselator.GetDeviation() <= 0:
300+
raise ValueError("The deviation is <= 0.")
301+
298302
shape_tesselator.Compute(
299303
compute_edges=self._export_edges,
300304
mesh_quality=self._mesh_quality,

src/Tesselator/ShapeTesselator.cpp

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <algorithm>
2222
#include <cmath>
2323
#include <iomanip>
24+
#include <stdexcept>
2425
//---------------------------------------------------------------------------
2526
#include <TopExp_Explorer.hxx>
2627
#include <Bnd_Box.hxx>
@@ -38,14 +39,15 @@
3839
#include <BRep_Tool.hxx>
3940
#include <TopoDS_Face.hxx>
4041
#include <Precision.hxx>
42+
#include <utility>
4143

4244
//---------------------------------------------------------------------------
43-
ShapeTesselator::ShapeTesselator(TopoDS_Shape aShape):
44-
myShape(aShape),
45-
locVertexcoord(NULL),
46-
locNormalcoord(NULL),
47-
locTriIndices(NULL),
48-
computed(false)
45+
ShapeTesselator::ShapeTesselator(TopoDS_Shape& aShape):
46+
computed(false),
47+
locVertexcoord(nullptr),
48+
locNormalcoord(nullptr),
49+
locTriIndices(nullptr),
50+
myShape(aShape)
4951
{
5052
ComputeDefaultDeviation();
5153
}
@@ -61,23 +63,17 @@ void ShapeTesselator::Compute(bool compute_edges, float mesh_quality, bool paral
6163

6264
ShapeTesselator::~ShapeTesselator()
6365
{
64-
if (locVertexcoord)
65-
delete [] locVertexcoord;
6666

67-
if (locNormalcoord)
68-
delete [] locNormalcoord;
69-
70-
if (locTriIndices)
71-
delete [] locTriIndices;
67+
delete [] locVertexcoord;
68+
delete [] locNormalcoord;
69+
delete [] locTriIndices;
7270

7371
for (std::vector<aedge*>::iterator edgeit = edgelist.begin(); edgeit != edgelist.end(); ++edgeit) {
7472
aedge* edge = *edgeit;
7573
if (edge) {
76-
if (edge->vertex_coord)
77-
delete[] edge->vertex_coord;
78-
74+
delete[] edge->vertex_coord;
7975
delete edge;
80-
*edgeit = NULL;
76+
*edgeit = nullptr;
8177
}
8278
}
8379
edgelist.clear();
@@ -89,17 +85,29 @@ void ShapeTesselator::SetDeviation(Standard_Real aDeviation)
8985
myDeviation = aDeviation;
9086
}
9187

88+
Standard_Real ShapeTesselator::GetDeviation() const
89+
{
90+
return myDeviation;
91+
}
9292

9393
//---------------------------------------------------------------------------
9494
void ShapeTesselator::Tesselate(bool compute_edges, float mesh_quality, bool parallel)
9595
{
9696
TopExp_Explorer ExpFace;
97-
// clean shape to remove any previous tringulation
97+
// clean shape to remove any previous triangulation
9898
BRepTools::Clean(myShape);
99+
100+
if (myDeviation <= 0){
101+
throw std::invalid_argument("The deviation must be greater than 0");
102+
};
103+
104+
if (mesh_quality <= 0){
105+
throw std::invalid_argument("The mesh quality must be greater than 0");
106+
};
107+
99108
//Triangulate
100109
BRepMesh_IncrementalMesh(myShape, myDeviation*mesh_quality, false, 0.5*mesh_quality, parallel);
101110

102-
103111
for (ExpFace.Init(myShape, TopAbs_FACE); ExpFace.More(); ExpFace.Next()) {
104112
Standard_Integer validFaceTriCount = 0;
105113
Standard_Integer invalidFaceTriCount = 0;
@@ -110,7 +118,6 @@ void ShapeTesselator::Tesselate(bool compute_edges, float mesh_quality, bool par
110118
Handle(Poly_Triangulation) myT = BRep_Tool::Triangulation(myFace, aLocation);
111119

112120
if (myT.IsNull()) {
113-
invalidFaceTriCount++;
114121
continue;
115122
}
116123

@@ -190,14 +197,17 @@ void ShapeTesselator::ComputeDefaultDeviation()
190197
{
191198
// This method automatically computes precision from the bounding box of the shape
192199
Bnd_Box aBox;
193-
Standard_Real aXmin,aYmin ,aZmin ,aXmax ,aYmax ,aZmax;
200+
BRepBndLib::Add(myShape, aBox);
201+
202+
if (aBox.IsVoid()) { // there is no shape
203+
myDeviation = 0;
204+
return;
205+
}
194206

195207
//calculate the bounding box
196-
BRepBndLib::Add(myShape, aBox);
197208
aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
198209

199-
Standard_Real adeviation = std::max(aXmax-aXmin, std::max(aYmax-aYmin, aZmax-aZmin)) * 2e-2 ;
200-
myDeviation = adeviation;
210+
myDeviation = std::max(aXmax-aXmin, std::max(aYmax-aYmin, aZmax-aZmin)) * 2e-2;
201211
}
202212

203213
void ShapeTesselator::ComputeEdges()
@@ -208,11 +218,9 @@ void ShapeTesselator::ComputeEdges()
208218
std::vector<aedge*>::iterator it;
209219
for (it = edgelist.begin(); it != edgelist.end(); ++it) {
210220
if (*it) {
211-
if ((*it)->vertex_coord)
212-
delete[] (*it)->vertex_coord;
213-
221+
delete[] (*it)->vertex_coord;
214222
delete *it;
215-
*it = NULL;
223+
*it = nullptr;
216224
}
217225
}
218226
edgelist.clear();
@@ -247,7 +255,7 @@ void ShapeTesselator::ComputeEdges()
247255
aedge* theEdge = new aedge;
248256
Standard_Integer nbNodesInFace;
249257

250-
// edge triangulation successfull
258+
// edge triangulation successful
251259
if (!aPoly.IsNull ()) {
252260
if (!aLoc.IsIdentity()) myTransf = aLoc.Transformation();
253261
nbNodesInFace = aPoly->NbNodes();
@@ -271,18 +279,18 @@ void ShapeTesselator::ComputeEdges()
271279
Handle(Poly_Triangulation) aPolyTria = BRep_Tool::Triangulation(aFace, aLoc);
272280
if (!aLoc.IsIdentity()) myTransf = aLoc.Transformation();
273281
// this holds the indices of the edge's triangulation to the actual points
274-
Handle(Poly_PolygonOnTriangulation) aPoly = BRep_Tool::PolygonOnTriangulation(anEdge, aPolyTria, aLoc);
275-
if (aPoly.IsNull()) continue; // polygon does not exist
282+
Handle(Poly_PolygonOnTriangulation) aPoly2 = BRep_Tool::PolygonOnTriangulation(anEdge, aPolyTria, aLoc);
283+
if (aPoly2.IsNull()) continue; // polygon does not exist
276284

277285
// getting size and create the array
278-
nbNodesInFace = aPoly->NbNodes();
286+
nbNodesInFace = aPoly2->NbNodes();
279287
theEdge->number_of_coords = nbNodesInFace;
280288
theEdge->vertex_coord = new Standard_Real[nbNodesInFace * 3 * sizeof(Standard_Real)];
281289

282-
const TColStd_Array1OfInteger& indices = aPoly->Nodes();
290+
const TColStd_Array1OfInteger& indices = aPoly2->Nodes();
283291

284292
// go through the index array
285-
for (Standard_Integer i=1;i <= aPoly->NbNodes();i++) {
293+
for (Standard_Integer i=1;i <= aPoly2->NbNodes();i++) {
286294
gp_Pnt V = aPolyTria->Node(indices(i)).Transformed(myTransf).XYZ();
287295
int idx = (i - 1) * 3;
288296
theEdge->vertex_coord[idx] = V.X();
@@ -302,7 +310,7 @@ void ShapeTesselator::EnsureMeshIsComputed()
302310
printf("The mesh is not computed. Currently computing with default parameters ...");
303311
Compute(true, 1.0, false);
304312
printf("done\n");
305-
printf("Call explicitely the Compute method to set the parameters value.\n");
313+
printf("Call explicitly the Compute method to set the parameters value.\n");
306314
}
307315
}
308316

@@ -425,17 +433,17 @@ std::string ShapeTesselator::ExportShapeToX3DTriangleSet()
425433
return str_ifs.str();
426434
}
427435

428-
void ShapeTesselator::ExportShapeToX3D(char * filename, int diffR, int diffG, int diffB)
436+
void ShapeTesselator::ExportShapeToX3D(const char * filename, int diffR, int diffG, int diffB)
429437
{
430438
EnsureMeshIsComputed();
431439
std::ofstream X3Dfile;
432440
X3Dfile.open (filename);
433441
// write header
434442
X3Dfile << "<?xml version='1.0' encoding='UTF-8'?>" ;
435-
X3Dfile << "<!DOCTYPE X3D PUBLIC 'ISO//Web3D//DTD X3D 3.1//EN' 'http://www.web3d.org/specifications/x3d-3.1.dtd'>";
443+
X3Dfile << "<!DOCTYPE X3D PUBLIC 'ISO//Web3D//DTD X3D 3.1//EN' 'https://www.web3d.org/specifications/x3d-3.1.dtd'>";
436444
X3Dfile << "<X3D>";
437445
X3Dfile << "<Head>";
438-
X3Dfile << "<meta name='generator' content='pythonOCC, http://www.pythonocc.org'/>";
446+
X3Dfile << "<meta name='generator' content='pythonOCC, https://github.com/tpaviot/pythonocc-core'/>";
439447
X3Dfile << "</Head>";
440448
X3Dfile << "<Scene><Transform scale='1 1 1'><Shape><Appearance><Material DEF='Shape_Mat' diffuseColor='0.65 0.65 0.7' ";
441449
X3Dfile << "specularColor='0.2 0.2 0.2'></Material></Appearance>";
@@ -702,16 +710,15 @@ void ShapeTesselator::JoinPrimitives()
702710
advance = obP;
703711

704712
delete [] myface->vertex_coord;
705-
myface->vertex_coord = NULL;
713+
myface->vertex_coord = nullptr;
706714

707715
delete [] myface->normal_coord;
708-
myface->normal_coord = NULL;
716+
myface->normal_coord = nullptr;
709717

710718
delete [] myface->tri_indexes;
711-
myface->tri_indexes = NULL;
719+
myface->tri_indexes = nullptr;
712720

713721
delete myface;
714-
myface = NULL;
715722

716723
++anIterator;
717724
}

0 commit comments

Comments
 (0)