Coverage for test/unit/utilities/test_pyocc_tools.py: 99%
67 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 17:09 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 17:09 +0000
1import unittest
3from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
4from OCC.Core.TopoDS import TopoDS_Face
5from OCC.Core.gp import gp_Pnt, gp_XYZ
7from bim2sim.utilities.pyocc_tools import PyOCCTools
10class TestOCCTools(unittest.TestCase):
11 """Unittests for bim2sim OCC Tools"""
12 def test_face_from_pnts_tuples(self):
13 """Test if face can be created from coordinate tuple."""
14 pnt_list = [(0, 0, 0), (10, 0, 0), (10, 10, 0), (0, 10, 0)]
15 face = PyOCCTools.make_faces_from_pnts(pnt_list)
16 self.assertIsInstance(face, TopoDS_Face)
18 def test_face_from_pnts_gp_pnts(self):
19 """Test if face can be created from gp_Pnt."""
20 pnt_list = [gp_Pnt(gp_XYZ(0, 0, 0)), gp_Pnt(gp_XYZ(10, 0, 0)),
21 gp_Pnt(gp_XYZ(10, 10, 0)), gp_Pnt(gp_XYZ(0, 10, 0))]
22 face = PyOCCTools.make_faces_from_pnts(pnt_list)
23 self.assertIsInstance(face, TopoDS_Face)
25 def test_get_points_of_face(self):
26 """Test if points from face are returned correctly."""
27 pnt_list = [(0, 0, 0), (10, 0, 0), (10, 10, 0), (0, 10, 0)]
28 face = PyOCCTools.make_faces_from_pnts(pnt_list)
29 pnts = PyOCCTools.get_points_of_face(face)
30 self.assertEqual(4, len(pnts))
31 for p in pnts:
32 self.assertIsInstance(p, gp_Pnt)
34 def test_remove_collinear_vertices(self):
35 """Test if collinear points are removed correctly."""
36 pnt_list1 = [(0, 0, 0), (5, 0, 0), (10, 0, 0), (10, 10, 0), (0, 10, 0)]
37 pnt_list2 = [(0, 0, 0), (5, 0, 0), (10, 0, 0), (10, 10, 0), (0, 10, 0),
38 (0, 5, 0)]
39 face1 = PyOCCTools.make_faces_from_pnts(pnt_list1)
40 face2 = PyOCCTools.make_faces_from_pnts(pnt_list2)
41 pnts1 = PyOCCTools.get_points_of_face(face1)
42 pnts2 = PyOCCTools.get_points_of_face(face2)
44 new_pnts1 = PyOCCTools.remove_collinear_vertices2(pnts1)
45 new_pnts2 = PyOCCTools.remove_collinear_vertices2(pnts2)
47 self.assertEqual(5, len(pnts1))
48 self.assertEqual(6, len(pnts2))
49 self.assertEqual(4, len(new_pnts1))
50 self.assertEqual(4, len(new_pnts2))
51 for p in new_pnts1:
52 self.assertIsInstance(p, gp_Pnt)
53 for p in new_pnts2:
54 self.assertIsInstance(p, gp_Pnt)
56 def test_remove_coincident_vertices(self):
57 """ test if coincident points are removed correctly """
58 pnt_list1 = [(0, 0, 0), (0.001, 0, 0), (10, 0, 0), (10, 10, 0),
59 (0, 10, 0)]
60 pnt_list2 = [(-0.01, 0, 0), (0, 0, 0), (10, 0, 0), (10, 10, 0),
61 (0, 10, 0), (0, 10.01, 0)]
62 face1 = PyOCCTools.make_faces_from_pnts(pnt_list1)
63 face2 = PyOCCTools.make_faces_from_pnts(pnt_list2)
64 pnts1 = PyOCCTools.get_points_of_face(face1)
65 pnts2 = PyOCCTools.get_points_of_face(face2)
67 new_pnts1 = PyOCCTools.remove_coincident_vertices(pnts1)
68 new_pnts2 = PyOCCTools.remove_coincident_vertices(pnts2)
70 self.assertEqual(5, len(pnts1))
71 self.assertEqual(6, len(pnts2))
72 self.assertEqual(4, len(new_pnts1))
73 self.assertEqual(4, len(new_pnts2))
74 for p in new_pnts1:
75 self.assertIsInstance(p, gp_Pnt)
76 for p in new_pnts2:
77 self.assertIsInstance(p, gp_Pnt)
79 def test_obj2_in_obj1(self):
80 """test if obj2 in obj1 is detected correctly."""
81 obj1 = BRepPrimAPI_MakeBox(5., 10., 15.).Shape()
82 obj2 = BRepPrimAPI_MakeBox(-5., -10., -15.).Shape()
83 obj3 = BRepPrimAPI_MakeBox(1., 1., 1.).Shape()
85 obj2_in_obj1 = PyOCCTools.obj2_in_obj1(obj1, obj2)
86 obj3_in_obj1 = PyOCCTools.obj2_in_obj1(obj1, obj3)
87 obj1_in_obj3 = PyOCCTools.obj2_in_obj1(obj3, obj1)
89 self.assertEqual(False, obj2_in_obj1)
90 self.assertEqual(True, obj3_in_obj1)
91 self.assertEqual(False, obj1_in_obj3)
94if __name__ == '__main__':
95 unittest.main()