Skip to content

Commit e87231d

Browse files
authored
Merge pull request #776 from tpaviot/review/move-tesselator
Refactored Tesselator as a package independent from Display so that i…
2 parents 908d9a7 + 99abd3a commit e87231d

File tree

10 files changed

+141
-100
lines changed

10 files changed

+141
-100
lines changed

CMakeLists.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ foreach(OCE_MODULE ${OCE_TOOLKIT_MODEL})
256256
swig_link_libraries(${OCE_MODULE} ${OCE_MODEL_LIBRARIES} Python3::Module)
257257
endforeach(OCE_MODULE)
258258

259+
##############
260+
# Tesselator #
261+
##############
262+
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory src/Tesselator)
263+
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/Tesselator/Tesselator.i PROPERTIES CPLUSPLUS ON)
264+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Tesselator)
265+
set(TESSELATOR_SOURCE_FILES
266+
${CMAKE_CURRENT_SOURCE_DIR}/src/Tesselator/Tesselator.i
267+
${CMAKE_CURRENT_SOURCE_DIR}/src/Tesselator/ShapeTesselator.cpp)
268+
269+
swig_add_library(Tesselator LANGUAGE python SOURCES ${TESSELATOR_SOURCE_FILES} TYPE MODULE)
270+
swig_link_libraries(Tesselator ${OCE_MODEL_LIBRARIES} Python3::Module)
271+
259272
#################
260273
# Visualisation #
261274
#################
@@ -277,8 +290,7 @@ if(PYTHONOCC_WRAP_VISU)
277290
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization)
278291
set(VISUALIZATION_SOURCE_FILES
279292
${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization/Visualization.i
280-
${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization/Display3d.cpp
281-
${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization/Tesselator.cpp)
293+
${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization/Display3d.cpp)
282294

283295
swig_add_library(Visualization LANGUAGE python SOURCES ${VISUALIZATION_SOURCE_FILES} TYPE MODULE)
284296
swig_link_libraries(Visualization ${OCE_MODEL_LIBRARIES} ${OCE_VISUALIZATION_LIBRARIES} Python3::Module)
@@ -354,6 +366,10 @@ foreach(OCE_MODULE ${OCE_TOOLKIT_MODEL})
354366
install(FILES ${BUILD_DIR}/${OCE_MODULE}.py DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core)
355367
install(TARGETS _${OCE_MODULE} DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core)
356368
endforeach(OCE_MODULE)
369+
# install tesselator
370+
install(FILES ${BUILD_DIR}/Tesselator.py DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core)
371+
install(TARGETS _Tesselator DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core)
372+
357373
if(PYTHONOCC_WRAP_VISU)
358374
foreach(OCE_MODULE ${OCE_TOOLKIT_VISUALIZATION})
359375
install(FILES ${BUILD_DIR}/${OCE_MODULE}.py DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core)

src/Display/WebGl/jupyter_renderer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from OCC.Core.TopoDS import TopoDS_Compound
4949
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex
5050
from OCC.Core.BRep import BRep_Builder
51-
from OCC.Core.Visualization import Tesselator
51+
from OCC.Core.Tesselator import ShapeTesselator
5252

5353
from OCC.Extend.TopologyUtils import (TopologyExplorer, is_edge, is_wire, discretize_edge,
5454
discretize_wire, get_type_as_string)
@@ -682,7 +682,7 @@ def AddShapeToScene(self,
682682
transparency=False,
683683
opacity=1.):
684684
# first, compute the tesselation
685-
tess = Tesselator(shp)
685+
tess = ShapeTesselator(shp)
686686
tess.Compute(compute_edges=render_edges,
687687
mesh_quality=quality,
688688
parallel=True)

src/Display/WebGl/threejs_renderer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import json
2323

2424
from OCC.Core.gp import gp_Vec
25-
from OCC.Core.Visualization import Tesselator
25+
from OCC.Core.Tesselator import ShapeTesselator
2626
from OCC import VERSION as OCC_VERSION
2727

2828
from OCC.Extend.TopologyUtils import is_edge, is_wire, discretize_edge, discretize_wire
@@ -444,7 +444,7 @@ def DisplayShape(self,
444444
shape_uuid = uuid.uuid4().hex
445445
shape_hash = "shp%s" % shape_uuid
446446
# tesselate
447-
tess = Tesselator(shape)
447+
tess = ShapeTesselator(shape)
448448
tess.Compute(compute_edges=export_edges,
449449
mesh_quality=mesh_quality,
450450
parallel=True)

src/Display/WebGl/x3dom_renderer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import uuid
2222
from xml.etree import ElementTree
2323

24-
from OCC.Core.Visualization import Tesselator
24+
from OCC.Core.Tesselator import ShapeTesselator
2525
from OCC import VERSION as OCC_VERSION
2626

2727
from OCC.Extend.TopologyUtils import is_edge, is_wire, discretize_edge, discretize_wire
@@ -284,7 +284,7 @@ def __init__(self,
284284
self._x3d_string = "" # the string that contains the x3d description
285285

286286
def compute(self):
287-
shape_tesselator = Tesselator(self._shape)
287+
shape_tesselator = ShapeTesselator(self._shape)
288288
shape_tesselator.Compute(compute_edges=self._export_edges,
289289
mesh_quality=self._mesh_quality,
290290
parallel=True)

src/Visualization/Tesselator.cpp renamed to src/Tesselator/ShapeTesselator.cpp

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1717

1818
//---------------------------------------------------------------------------
19-
#include "Tesselator.h"
19+
#include "ShapeTesselator.h"
2020
#include <sstream>
2121
#include <algorithm>
2222
#include <cmath>
@@ -40,7 +40,7 @@
4040
#include <TopoDS_Face.hxx>
4141

4242
//---------------------------------------------------------------------------
43-
Tesselator::Tesselator(TopoDS_Shape aShape):
43+
ShapeTesselator::ShapeTesselator(TopoDS_Shape aShape):
4444
myShape(aShape),
4545
locVertexcoord(NULL),
4646
locNormalcoord(NULL),
@@ -50,13 +50,13 @@ Tesselator::Tesselator(TopoDS_Shape aShape):
5050
ComputeDefaultDeviation();
5151
}
5252

53-
void Tesselator::Compute(bool compute_edges, float mesh_quality, bool parallel)
53+
void ShapeTesselator::Compute(bool compute_edges, float mesh_quality, bool parallel)
5454
{
5555
Tesselate(compute_edges, mesh_quality, parallel);
5656
computed=true;
5757
}
5858

59-
Tesselator::~Tesselator()
59+
ShapeTesselator::~ShapeTesselator()
6060
{
6161
if (locVertexcoord)
6262
delete [] locVertexcoord;
@@ -81,14 +81,14 @@ Tesselator::~Tesselator()
8181
}
8282

8383
//---------------------------------------------------------------------------
84-
void Tesselator::SetDeviation(Standard_Real aDeviation)
84+
void ShapeTesselator::SetDeviation(Standard_Real aDeviation)
8585
{
8686
myDeviation = aDeviation;
8787
}
8888

8989

9090
//---------------------------------------------------------------------------
91-
void Tesselator::Tesselate(bool compute_edges, float mesh_quality, bool parallel)
91+
void ShapeTesselator::Tesselate(bool compute_edges, float mesh_quality, bool parallel)
9292
{
9393
TopExp_Explorer ExpFace;
9494
// clean shape to remove any previous tringulation
@@ -184,7 +184,7 @@ void Tesselator::Tesselate(bool compute_edges, float mesh_quality, bool parallel
184184

185185

186186
//---------------------------INTERFACE---------------------------------------
187-
void Tesselator::ComputeDefaultDeviation()
187+
void ShapeTesselator::ComputeDefaultDeviation()
188188
{
189189
// This method automatically computes precision from the bounding box of the shape
190190
Bnd_Box aBox;
@@ -198,7 +198,7 @@ void Tesselator::ComputeDefaultDeviation()
198198
myDeviation = adeviation;
199199
}
200200

201-
void Tesselator::ComputeEdges()
201+
void ShapeTesselator::ComputeEdges()
202202
{
203203
TopLoc_Location aTrsf;
204204

@@ -293,7 +293,7 @@ void Tesselator::ComputeEdges()
293293
}
294294
}
295295

296-
void Tesselator::EnsureMeshIsComputed()
296+
void ShapeTesselator::EnsureMeshIsComputed()
297297
{
298298
// this method ensures that the mesh is computed before returning any
299299
// related data
@@ -318,36 +318,33 @@ std::string formatFloatNumber(float f)
318318
return formatted_float.str();
319319
}
320320

321-
std::vector<float> Tesselator::GetVerticesPositionAsTuple()
321+
std::vector<float> ShapeTesselator::GetVerticesPositionAsTuple()
322322
{
323323
EnsureMeshIsComputed();
324324
// create the vector and allocate memory
325325
std::vector<float> vertices_position;
326326
vertices_position.reserve(tot_triangle_count);
327327
// loop over tertices
328-
int pID = 0;
329-
int qID = 0;
330-
int rID = 0;
331328
for (int i=0;i<tot_triangle_count;i++) {
332-
pID = locTriIndices[(i * 3) + 0] * 3;
329+
int pID = locTriIndices[(i * 3) + 0] * 3;
333330
vertices_position.push_back(locVertexcoord[pID]);
334331
vertices_position.push_back(locVertexcoord[pID+1]);
335332
vertices_position.push_back(locVertexcoord[pID+2]);
336333
// Second vertex
337-
qID = locTriIndices[(i * 3) + 1] * 3;
334+
int qID = locTriIndices[(i * 3) + 1] * 3;
338335
vertices_position.push_back(locVertexcoord[qID]);
339336
vertices_position.push_back(locVertexcoord[qID+1]);
340337
vertices_position.push_back(locVertexcoord[qID+2]);
341338
// Third vertex
342-
rID = locTriIndices[(i * 3) + 2] * 3;
339+
int rID = locTriIndices[(i * 3) + 2] * 3;
343340
vertices_position.push_back(locVertexcoord[rID]);
344341
vertices_position.push_back(locVertexcoord[rID+1]);
345342
vertices_position.push_back(locVertexcoord[rID+2]);
346343
}
347344
return vertices_position;
348345
}
349346

350-
std::vector<float> Tesselator::GetNormalsAsTuple()
347+
std::vector<float> ShapeTesselator::GetNormalsAsTuple()
351348
{
352349
EnsureMeshIsComputed();
353350
// create the vector and allocate memory
@@ -373,7 +370,7 @@ std::vector<float> Tesselator::GetNormalsAsTuple()
373370
return normals;
374371
}
375372

376-
std::string Tesselator::ExportShapeToX3DIndexedFaceSet()
373+
std::string ShapeTesselator::ExportShapeToX3DIndexedFaceSet()
377374
{
378375
EnsureMeshIsComputed();
379376
std::stringstream str_ifs, str_vertices, str_normals;
@@ -427,7 +424,7 @@ std::string Tesselator::ExportShapeToX3DIndexedFaceSet()
427424
return str_ifs.str();
428425
}
429426

430-
void Tesselator::ExportShapeToX3D(char * filename, int diffR, int diffG, int diffB)
427+
void ShapeTesselator::ExportShapeToX3D(char * filename, int diffR, int diffG, int diffB)
431428
{
432429
EnsureMeshIsComputed();
433430
std::ofstream X3Dfile;
@@ -448,7 +445,7 @@ void Tesselator::ExportShapeToX3D(char * filename, int diffR, int diffG, int dif
448445

449446
}
450447

451-
std::string Tesselator::ExportShapeToThreejsJSONString(char *shape_function_name)
448+
std::string ShapeTesselator::ExportShapeToThreejsJSONString(char *shape_function_name)
452449
{
453450
EnsureMeshIsComputed();
454451
// a method that export a shape to a JSON BufferGeometry object
@@ -531,55 +528,55 @@ std::string Tesselator::ExportShapeToThreejsJSONString(char *shape_function_name
531528
}
532529

533530
//---------------------------------------------------------------------------
534-
Standard_Real* Tesselator::VerticesList()
531+
Standard_Real* ShapeTesselator::VerticesList()
535532
{
536533
EnsureMeshIsComputed();
537534
return locVertexcoord;
538535
}
539536
//---------------------------------------------------------------------------
540-
Standard_Real* Tesselator::NormalsList()
537+
Standard_Real* ShapeTesselator::NormalsList()
541538
{
542539
EnsureMeshIsComputed();
543540
return locNormalcoord;
544541
}
545542
//---------------------------------------------------------------------------
546-
Standard_Integer Tesselator::ObjGetInvalidTriangleCount()
543+
Standard_Integer ShapeTesselator::ObjGetInvalidTriangleCount()
547544
{
548545
EnsureMeshIsComputed();
549546
return tot_invalid_triangle_count;
550547
}
551548
//---------------------------------------------------------------------------
552-
Standard_Integer Tesselator::ObjGetTriangleCount()
549+
Standard_Integer ShapeTesselator::ObjGetTriangleCount()
553550
{
554551
EnsureMeshIsComputed();
555552
return tot_triangle_count;
556553
}
557554
//---------------------------------------------------------------------------
558-
Standard_Integer Tesselator::ObjGetVertexCount()
555+
Standard_Integer ShapeTesselator::ObjGetVertexCount()
559556
{
560557
EnsureMeshIsComputed();
561558
return tot_vertex_count;
562559
}
563560
//---------------------------------------------------------------------------
564-
Standard_Integer Tesselator::ObjGetNormalCount()
561+
Standard_Integer ShapeTesselator::ObjGetNormalCount()
565562
{
566563
EnsureMeshIsComputed();
567564
return tot_normal_count;
568565
}
569566
//---------------------------------------------------------------------------
570-
Standard_Integer Tesselator::ObjGetInvalidNormalCount()
567+
Standard_Integer ShapeTesselator::ObjGetInvalidNormalCount()
571568
{
572569
EnsureMeshIsComputed();
573570
return tot_invalid_normal_count;
574571
}
575572
//---------------------------------------------------------------------------
576-
Standard_Integer Tesselator::ObjGetEdgeCount()
573+
Standard_Integer ShapeTesselator::ObjGetEdgeCount()
577574
{
578575
EnsureMeshIsComputed();
579576
return edgelist.size();
580577
}
581578
//---------------------------------------------------------------------------
582-
Standard_Integer Tesselator::ObjEdgeGetVertexCount(int iEdge)
579+
Standard_Integer ShapeTesselator::ObjEdgeGetVertexCount(int iEdge)
583580
{
584581
EnsureMeshIsComputed();
585582
aedge* edge = edgelist.at(iEdge);
@@ -589,31 +586,31 @@ Standard_Integer Tesselator::ObjEdgeGetVertexCount(int iEdge)
589586
return edge->number_of_coords;
590587
}
591588
//---------------------------------------------------------------------------
592-
void Tesselator::GetVertex(int ivert, float& x, float& y, float& z)
589+
void ShapeTesselator::GetVertex(int ivert, float& x, float& y, float& z)
593590
{
594591
EnsureMeshIsComputed();
595592
x = locVertexcoord[ivert*3 + 0];
596593
y = locVertexcoord[ivert*3 + 1];
597594
z = locVertexcoord[ivert*3 + 2];
598595
}
599596
//---------------------------------------------------------------------------
600-
void Tesselator::GetNormal(int ivert, float& x, float& y, float& z)
597+
void ShapeTesselator::GetNormal(int ivert, float& x, float& y, float& z)
601598
{
602599
EnsureMeshIsComputed();
603600
x = locNormalcoord[ivert*3 + 0];
604601
y = locNormalcoord[ivert*3 + 1];
605602
z = locNormalcoord[ivert*3 + 2];
606603
}
607604
//---------------------------------------------------------------------------
608-
void Tesselator::GetTriangleIndex(int triangleIdx, int &v1, int &v2, int &v3)
605+
void ShapeTesselator::GetTriangleIndex(int triangleIdx, int &v1, int &v2, int &v3)
609606
{
610607
EnsureMeshIsComputed();
611608
v1 = locTriIndices[3*triangleIdx + 0];
612609
v2 = locTriIndices[3*triangleIdx + 1];
613610
v3 = locTriIndices[3*triangleIdx + 2];
614611
}
615612
//---------------------------------------------------------------------------
616-
void Tesselator::GetEdgeVertex(int iEdge, int ivert, float &x, float &y, float &z)
613+
void ShapeTesselator::GetEdgeVertex(int iEdge, int ivert, float &x, float &y, float &z)
617614
{
618615
EnsureMeshIsComputed();
619616
aedge* e = edgelist.at(iEdge);
@@ -626,7 +623,7 @@ void Tesselator::GetEdgeVertex(int iEdge, int ivert, float &x, float &y, float &
626623
z = e->vertex_coord[3*ivert + 2];
627624
}
628625
//---------------------------------------------------------------------------
629-
void Tesselator::ObjGetTriangle(int trianglenum, int *vertices, int *normals)
626+
void ShapeTesselator::ObjGetTriangle(int trianglenum, int *vertices, int *normals)
630627
{
631628
EnsureMeshIsComputed();
632629
int pID = locTriIndices[(trianglenum * 3) + 0] * 3;
@@ -644,7 +641,7 @@ void Tesselator::ObjGetTriangle(int trianglenum, int *vertices, int *normals)
644641
//---------------------------------------------------------------------------
645642
//---------------------------------HELPERS-----------------------------------
646643
//---------------------------------------------------------------------------
647-
void Tesselator::JoinPrimitives()
644+
void ShapeTesselator::JoinPrimitives()
648645
{
649646
int obP = 0;
650647
int obN = 0;
@@ -670,7 +667,7 @@ void Tesselator::JoinPrimitives()
670667
tot_normal_count = tot_normal_count + myface->number_of_normals;
671668
tot_invalid_normal_count = tot_invalid_normal_count + myface->number_of_invalid_normals;
672669

673-
anIterator++;
670+
++anIterator;
674671
}
675672

676673
locTriIndices= new Standard_Integer[tot_triangle_count * 3 ];

src/Visualization/Tesselator.h renamed to src/Tesselator/ShapeTesselator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct aedge {
4242
Standard_Integer number_of_coords;
4343
};
4444

45-
class Tesselator
45+
class ShapeTesselator
4646
{
4747
protected:
4848
Standard_Boolean computed;
@@ -66,8 +66,8 @@ class Tesselator
6666
void EnsureMeshIsComputed();
6767

6868
public:
69-
Tesselator(TopoDS_Shape aShape);
70-
~Tesselator();
69+
ShapeTesselator(TopoDS_Shape aShape);
70+
~ShapeTesselator();
7171
void Compute(bool compute_edges=false, float mesh_quality=1.0, bool parallel=false);
7272
void Tesselate(bool compute_edges, float mesh_quality, bool parallel);
7373
void JoinPrimitives();

0 commit comments

Comments
 (0)