forked from tpaviot/pythonocc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_visualization_unittest.py
More file actions
110 lines (96 loc) · 4.43 KB
/
Copy pathcore_visualization_unittest.py
File metadata and controls
110 lines (96 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
##Copyright 2009-2016 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
""" This module provides unittests for the visualization wrapper
Usage :
$ python core_visualization_unittest.python """
import os
import unittest
import json
from OCC.Core.Visualization import Tesselator, atNormal
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeTorus, BRepPrimAPI_MakeSphere
class TestTesselator(unittest.TestCase):
""" A class for testing tesselation algorithm """
def test_tesselate_box(self):
""" 1st test : tesselation of a box """
a_box = BRepPrimAPI_MakeBox(10, 20, 30).Shape()
tess = Tesselator(a_box, atNormal, 1.0, 1, 0.01, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.)
tess.Compute()
self.assert_(tess.ObjGetTriangleCount() == 12)
self.assert_(tess.ObjGetNormalCount() == 24)
def test_tesselate_torus(self):
""" 2st test : tesselation of a torus """
a_torus = BRepPrimAPI_MakeTorus(10, 4).Shape()
tess = Tesselator(a_torus, atNormal, 1.0, 1, 0.01, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.)
tess.Compute()
self.assert_(tess.ObjGetTriangleCount() > 100)
self.assert_(tess.ObjGetNormalCount() > 100)
def test_tesselate_torus_with_edges(self):
""" 2st test : tesselation of a torus """
a_torus = BRepPrimAPI_MakeTorus(10, 4).Shape()
tess = Tesselator(a_torus, atNormal, 1.0, 1, 0.01, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.)
tess.Compute(compute_edges=True)
self.assert_(tess.ObjGetTriangleCount() > 100)
self.assert_(tess.ObjGetNormalCount() > 100)
def test_tesselate_torus_with_bad_quality(self):
""" 2st test : tesselation of a torus """
a_torus = BRepPrimAPI_MakeTorus(10, 4).Shape()
tess = Tesselator(a_torus, atNormal, 1.0, 1, 0.01, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.)
tess.Compute(mesh_quality=40.)
# since mesh quality is much lower, we should count less vertices and
# triangles
self.assert_(tess.ObjGetTriangleCount() > 10)
self.assert_(tess.ObjGetTriangleCount() < 100)
self.assert_(tess.ObjGetNormalCount() > 10)
self.assert_(tess.ObjGetNormalCount() < 100)
def test_export_to_x3d(self):
""" 3rd test : export a sphere to X3D file format """
a_sphere = BRepPrimAPI_MakeSphere(10.).Shape()
tess = Tesselator(a_sphere)
tess.Compute()
tess.ExportShapeToX3D(os.path.join("test_io", "sphere.x3d"))
self.assert_(os.path.exists(os.path.join("test_io", "sphere.x3d")))
def test_export_to_x3d_TriangleSet(self):
""" 3rd test : export a sphere to an X3D TriangleSet triangle mesh """
a_sphere = BRepPrimAPI_MakeBox(10., 10., 10.).Shape()
tess = Tesselator(a_sphere)
tess.Compute()
ifs = tess.ExportShapeToX3DIndexedFaceSet()
self.assert_(ifs.startswith("<TriangleSet"))
self.assert_("0 10 0" in ifs) # a vertex
self.assert_("0 0 1" in ifs) # a normal
def test_export_to_3js_JSON(self):
a_box = BRepPrimAPI_MakeBox(10, 20, 30).Shape()
tess = Tesselator(a_box)
tess.Compute()
# get the JSON string
JSON_str = tess.ExportShapeToThreejsJSONString("myshapeid")
# check the python JSON parser can decode the string
# i.e. the JSON string is well formed
dico = json.loads(JSON_str)
# after that, check that the number of vertices is ok
assert len(dico["data"]["attributes"]["position"]["array"]) == 36*3
def suite():
""" builds the test suite """
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(TestTesselator))
return test_suite
if __name__ == '__main__':
unittest.main()