Salome HOME
[bos #40619][CEA] Add Fuzzy parameter to partition and boolean operators
[modules/geom.git] / test / test_boolean_fuzzy.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import salome
22 ###
23 salome.salome_init()
24
25 import GEOM
26 from salome.geom import geomBuilder
27 import math
28 import SALOMEDS
29
30 geompy = geomBuilder.New()
31
32 import unittest
33
34 def HasSameSubShapes( shape, expected_info ):
35     """
36     Compare topology information about shape with expected data
37     param shape - shape to be checked
38     param expected_info - expected WhatIsInformation for the shape
39     return True, if number of shapes are equal - False, otherwise
40     """
41     name = shape.GetName()
42     if name:
43         name = '"%s"' % name
44     what_is = geompy.WhatIs( shape )
45     what_is_list = what_is.strip().split( "\n" )
46     # Remove flat content
47     if what_is_list.count( " Flat content : " ):
48         wIndex = what_is_list.index( " Flat content : " )
49         what_is_list = what_is_list[:( wIndex - len( what_is_list ) - 1 )]
50     got_info_dict = {}
51     for item in what_is_list:
52         pair = item.split( ":" )
53         if len(pair)==2:
54             type = item.split( ":" )[0].strip()
55             value = item.split( ":" )[1].strip()
56             if type.find( "Number of sub-shapes" ) == -1:
57                 got_info_dict[type] = value
58     if len( expected_info ) > len( got_info_dict ):
59         print( "ERROR!!! Got topology information about shape %s isn't complete..." % name )
60         return False
61     for key in expected_info:
62         if key not in got_info_dict:
63             print( "ERROR!!! There is no information about number of " + key + "(s) in %s shape!!!" % name )
64             return False
65         elif str( expected_info[key] ).find( str( got_info_dict[key] )) == -1 or len( str( expected_info[key] )) != len( str( got_info_dict[key] )):
66             print( "ERROR!!! The number of " + key + "(s) is incorrect in %s shape!!! ( " % name + str( got_info_dict[key] ) + " instead of " + str( expected_info[key] ) + " )" )
67             return False
68     return True
69
70
71 class GEOMTestBooleanFuzzy(unittest.TestCase):
72     def testFuse(self):
73         Vertex_1 = geompy.MakeVertex(10, 0, 0)
74         Vertex_2 = geompy.MakeVertex(20, 10.0001, 10.0001)
75         Box_1 = geompy.MakeBoxDXDYDZ(10, 10, 10)
76         Box_2 = geompy.MakeBoxTwoPnt(Vertex_1, Vertex_2)
77         # Fuse without fuzzy parameter
78         Fuse_1 = geompy.MakeFuse(Box_1, Box_2, True, True)
79         WHAT_IS_1={"VERTEX":14, "EDGE":21, "WIRE":9, "FACE":9, "SHELL":1, "SOLID":1, "COMPSOLID":0, "COMPOUND":0, "SHAPE":55}
80         assert(HasSameSubShapes(Fuse_1, WHAT_IS_1))
81         # Fuse with fuzzy parameter
82         Fuse_2 = geompy.MakeFuse(Box_1, Box_2, True, True, None, 1.e-4)
83         WHAT_IS_2={"VERTEX":11, "EDGE":17, "WIRE":8, "FACE":8, "SHELL":1, "SOLID":1, "COMPSOLID":0, "COMPOUND":0, "SHAPE":46}
84         assert(HasSameSubShapes(Fuse_2, WHAT_IS_2))
85
86     def testCommon(self):
87         Vertex_1 = geompy.MakeVertex(0, 9.999995, 0)
88         Sphere_1 = geompy.MakeSphereR(5)
89         Sphere_2 = geompy.MakeSpherePntR(Vertex_1, 5)
90         # Common without fuzzy parameter
91         Common_1 = geompy.MakeCommon(Sphere_1, Sphere_2, True)
92         WHAT_IS_1={"VERTEX":1, "EDGE":1, "WIRE":2, "FACE":2, "SHELL":1, "SOLID":1, "COMPSOLID":0, "COMPOUND":0, "SHAPE":8}
93         assert(HasSameSubShapes(Common_1, WHAT_IS_1))
94         # Common with fuzzy parameter
95         Common_2 = geompy.MakeCommon(Sphere_1, Sphere_2, True, None, 1.e-5)
96         WHAT_IS_2={"VERTEX":0, "EDGE":0, "WIRE":0, "FACE":0, "SHELL":0, "SOLID":0, "COMPSOLID":0, "COMPOUND":1, "SHAPE":1}
97         assert(HasSameSubShapes(Common_2, WHAT_IS_2))
98
99     def testCut(self):
100         OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
101         Vertex_1 = geompy.MakeVertex(5.e-5, 5, 5)
102         Box_1 = geompy.MakeBoxDXDYDZ(10, 10, 10)
103         Cylinder_1 = geompy.MakeCylinder(Vertex_1, OX, 6, 13)
104         # Cut with low fuzzy parameter
105         Cut_1 = geompy.MakeCut(Box_1, Cylinder_1, True, None, 1.e-5)
106         WHAT_IS_1={"VERTEX":24, "EDGE":36, "WIRE":14, "FACE":14, "SHELL":1, "SOLID":1, "COMPSOLID":0, "COMPOUND":0, "SHAPE":90}
107         assert(HasSameSubShapes(Cut_1, WHAT_IS_1))
108         # Cut with high fuzzy parameter
109         Cut_2 = geompy.MakeCut(Box_1, Cylinder_1, True, None, 5.e-5)
110         WHAT_IS_2={"VERTEX":24, "EDGE":36, "WIRE":20, "FACE":20, "SHELL":4, "SOLID":4, "COMPSOLID":0, "COMPOUND":1, "SHAPE":109}
111         assert(HasSameSubShapes(Cut_2, WHAT_IS_2))
112
113     def testBoolean(self):
114         Vertex_1 = geompy.MakeVertex(0, 9.999995, 0)
115         Sphere_1 = geompy.MakeSphereR(5)
116         Sphere_2 = geompy.MakeSpherePntR(Vertex_1, 5)
117         # Common without fuzzy parameter
118         Common_1 = geompy.MakeBoolean(Sphere_1, Sphere_2, 1, True)
119         WHAT_IS_1={"VERTEX":1, "EDGE":1, "WIRE":2, "FACE":2, "SHELL":1, "SOLID":1, "COMPSOLID":0, "COMPOUND":0, "SHAPE":8}
120         assert(HasSameSubShapes(Common_1, WHAT_IS_1))
121         # Common with fuzzy parameter
122         Common_2 = geompy.MakeBoolean(Sphere_1, Sphere_2, 1, True, None, 1.e-5)
123         WHAT_IS_2={"VERTEX":0, "EDGE":0, "WIRE":0, "FACE":0, "SHELL":0, "SOLID":0, "COMPSOLID":0, "COMPOUND":1, "SHAPE":1}
124         assert(HasSameSubShapes(Common_2, WHAT_IS_2))
125
126     def testPartition(self):
127         Box_1 = geompy.MakeBoxDXDYDZ(200, 200, 200)
128         Cylinder_1 = geompy.MakeCylinderRH(100, 300)
129         Box_2 = geompy.MakeBoxDXDYDZ(200, 200, 200)
130         Box_translated_slightly_inside = geompy.MakeTranslation(Box_2, 0, -199.9999, 0)
131         Box_translated_slightly_outside = geompy.MakeTranslation(Box_2, 0, -200.0001, 0)
132         Partitition_small_intersection = geompy.MakePartition([Box_1, Cylinder_1, Box_translated_slightly_inside])
133         WHAT_IS_1={"VERTEX":25, "EDGE":49, "WIRE":32, "FACE":32, "SHELL":7, "SOLID":7, "COMPSOLID":0, "COMPOUND":1, "SHAPE":153}
134         assert(HasSameSubShapes(Partitition_small_intersection, WHAT_IS_1))
135         Partitition_small_intersection_fixed_by_fuzzy = geompy.MakePartition([Box_1, Cylinder_1, Box_translated_slightly_inside], theFuzzyParam=0.0001)
136         WHAT_IS_2={"VERTEX":19, "EDGE":36, "WIRE":23, "FACE":23, "SHELL":5, "SOLID":5, "COMPSOLID":0, "COMPOUND":1, "SHAPE":112}
137         assert(HasSameSubShapes(Partitition_small_intersection_fixed_by_fuzzy, WHAT_IS_2))
138         Partitition_small_gap = geompy.MakePartition([Box_1, Cylinder_1, Box_translated_slightly_outside])
139         WHAT_IS_3={"VERTEX":25, "EDGE":44, "WIRE":25, "FACE":25, "SHELL":5, "SOLID":5, "COMPSOLID":0, "COMPOUND":1, "SHAPE":130}
140         assert(HasSameSubShapes(Partitition_small_gap, WHAT_IS_3))
141         Partitition_small_gap_fixed_by_fuzzy = geompy.MakePartition([Box_1, Cylinder_1, Box_translated_slightly_inside], theFuzzyParam=0.0001)
142         WHAT_IS_4={"VERTEX":19, "EDGE":36, "WIRE":23, "FACE":23, "SHELL":5, "SOLID":5, "COMPSOLID":0, "COMPOUND":1, "SHAPE":112}
143         assert(HasSameSubShapes(Partitition_small_gap_fixed_by_fuzzy, WHAT_IS_4))
144
145 if __name__ == '__main__':
146     unittest.main()