Salome HOME
Implemented [bos #35094] [EDF] (2023-T1) X,Y,Z to U,V.
[modules/geom.git] / doc / salome / examples / XYZ_to_UV.py
1 # Test XYZtoUV and UVtoXYZ methods
2
3 import salome
4 salome.salome_init_without_session()
5 import GEOM
6 from salome.geom import geomBuilder
7 geompy = geomBuilder.New()
8 import math
9 import numpy as np
10
11 toler = 1e-04
12
13 # Create "Horse saddle"
14 OX = geompy.MakeVectorDXDYDZ(1, 0, 0, 'OX')
15 OY = geompy.MakeVectorDXDYDZ(0, 1, 0, 'OY')
16 Sphere_1 = geompy.MakeSphereR(100, 'Sphere_1')
17 [Edge_1,Edge_2,Edge_3] = geompy.ExtractShapes(Sphere_1, geompy.ShapeType["EDGE"], True)
18 geompy.addToStudyInFather( Sphere_1, Edge_1, 'Edge_1' )
19 geompy.addToStudyInFather( Sphere_1, Edge_2, 'Edge_2' )
20 geompy.addToStudyInFather( Sphere_1, Edge_3, 'Edge_3' )
21
22 Rotation_1 = geompy.MakeRotation(Edge_3, OX, 90*math.pi/180.0, 'Rotation_1')
23 Rotation_2 = geompy.MakeRotation(Rotation_1, OY, 180*math.pi/180.0, 'Rotation_2')
24 Translation_1 = geompy.MakeTranslation(Rotation_2, 200, 0, 0, 'Translation_1')
25 Translation_2 = geompy.MakeTranslation(Edge_3, 100, 100, 0, 'Translation_2')
26 Translation_3 = geompy.MakeTranslation(Translation_2, 0, -200, 0, 'Translation_3')
27
28 Filling_1 = geompy.MakeFilling([Translation_2, Edge_3, Translation_3])
29 geompy.addToStudy(Filling_1, 'Filling_1')
30
31 HorseSaddle = geompy.LimitTolerance(Filling_1, toler, 'HorseSaddle')
32
33 # Get 100 equidistant points on the "Horse saddle"
34 CompoundOfVertices = geompy.MakeVertexInsideFace(HorseSaddle, 100, "CompoundOfVertices")
35 assert(geompy.NumberOfSubShapes(CompoundOfVertices, geompy.ShapeType["VERTEX"]) == 100)
36
37 # Extract the vertices
38 listOfVertices = geompy.ExtractShapes(CompoundOfVertices, geompy.ShapeType["VERTEX"], True)
39
40 # Get list of coordinates of all 100 vertices
41 listOfCoords = []
42 for aV in listOfVertices:
43     listOfCoords += geompy.PointCoordinates(aV)
44     pass
45
46 # Test 1: with normalization of parameters
47
48 # Convert XYZ to UV
49 listOfParams_norm = geompy.XYZtoUV(HorseSaddle, listOfCoords, True)
50 assert(len(listOfParams_norm) == 200)
51
52 # Convert UV to XYZ
53 listOfCoords_new1 = geompy.UVtoXYZ(HorseSaddle, listOfParams_norm, True)
54 assert(len(listOfCoords_new1) == 300)
55
56 # Compare listOfCoords_new with listOfCoords
57 for (c1, c2) in zip(listOfCoords, listOfCoords_new1):
58     assert(abs(c1 - c2) < toler)
59     pass
60
61 # Test 2: without normalization of parameters
62
63 # Convert XYZ to UV
64 listOfParams = geompy.XYZtoUV(HorseSaddle, listOfCoords, False)
65 assert(len(listOfParams) == 200)
66
67 # Convert UV to XYZ
68 listOfCoords_new2 = geompy.UVtoXYZ(HorseSaddle, listOfParams, False)
69 assert(len(listOfCoords_new2) == 300)
70
71 # Compare listOfCoords_new with listOfCoords
72 for (c1, c2) in zip(listOfCoords, listOfCoords_new2):
73     assert(abs(c1 - c2) < toler)
74     pass
75
76 # Test 3: Check exceptions throwing if point (XYZ or UV) is out of face
77 listXYZ_3 = listOfCoords[:3]
78 listXYZ_3[2] = listXYZ_3[2] + 1.0 # move along OZ
79 try:
80     geompy.XYZtoUV(HorseSaddle, listXYZ_3, True)
81 except RuntimeError:
82     print(geompy.MeasuOp.GetErrorCode(), ", it's OK")
83     pass
84 except Exception:
85     assert False, 'Unexpected exception raised'
86 else:
87     assert False, 'XYZtoUV should raise an exception if input point is out of face'
88
89 listUV_2 = [2, 2] # each parameter value should be in [0,1] range (for normalized case)
90 try:
91     geompy.UVtoXYZ(HorseSaddle, listUV_2, True)
92 except RuntimeError:
93     print(geompy.MeasuOp.GetErrorCode(), ", it's OK")
94     pass
95 except Exception:
96     assert False, 'Unexpected exception raised'
97 else:
98     assert False, 'UVtoXYZ should raise an exception if input parameters are out of face'
99
100 # parameter U should be in [4.71239, 7.85398] range (on this face, for not normalized case)
101 # parameter V should be in [0, 1] range (on this face, for not normalized case)
102 listUV_2 = [10, 10]
103 try:
104     geompy.UVtoXYZ(HorseSaddle, listUV_2, True)
105 except RuntimeError:
106     print(geompy.MeasuOp.GetErrorCode(), ", it's OK")
107     pass
108 except Exception:
109     assert False, 'Unexpected exception raised'
110 else:
111     assert False, 'UVtoXYZ should raise an exception if input parameters are out of face'
112
113 # Test 4: Check exceptions in case of invalid data type (wrong length of array or type of elements)
114
115 #   1. Length of input array is not divisible by 3 (for XYZtoUV) or by 2 (for UVtoXYZ)
116 listXYZ_4 = listOfCoords[:4]
117 assert(len(listXYZ_4) == 4)
118 try:
119     geompy.XYZtoUV(HorseSaddle, listXYZ_4, True)
120 except RuntimeError:
121     print(geompy.MeasuOp.GetErrorCode(), ", it's OK")
122     pass
123 except Exception:
124     assert False, 'Unexpected exception raised'
125 else:
126     assert False, 'XYZtoUV should raise an exception if input list length is not divisible by 3'
127
128 listUV_3 = listOfParams[:3]
129 assert(len(listUV_3) == 3)
130 try:
131     geompy.UVtoXYZ(HorseSaddle, listUV_3, True)
132 except RuntimeError:
133     print(geompy.MeasuOp.GetErrorCode(), ", it's OK")
134     pass
135 except Exception:
136     assert False, 'Unexpected exception raised'
137 else:
138     assert False, 'UVtoXYZ should raise an exception if input list length is not divisible by 2'
139
140 #   2. Input array contains data of wrong type
141 listXYZ_w = ['a', 'b', 'c']
142 try:
143     geompy.XYZtoUV(HorseSaddle, listXYZ_w, True)
144 except Exception:
145     pass
146 else:
147     assert False, 'XYZtoUV should raise TypeError if input list contains not numerical data'
148
149 listXYZ_w = [10.0, '10.0', 10.0]
150 try:
151     geompy.XYZtoUV(HorseSaddle, listXYZ_w, True)
152 except Exception:
153     pass
154 else:
155     assert False, 'XYZtoUV should raise TypeError if input list contains not numerical data'
156
157 listUV_w = ['a', 'b']
158 try:
159     geompy.UVtoXYZ(HorseSaddle, listUV_w, True)
160 except Exception:
161     pass
162 else:
163     assert False, 'UVtoXYZ should raise TypeError if input list contains not numerical data'
164
165 listUV_w = [10.0, '10.0']
166 try:
167     geompy.UVtoXYZ(HorseSaddle, listUV_w, True)
168 except Exception:
169     pass
170 else:
171     assert False, 'UVtoXYZ should raise TypeError if input list contains not numerical data'
172
173 # Test 5: a. Translate each of the 100 points by toler*2.0 along the face normal
174 #            and check that the XYZtoUV method fails by raising an exception.
175 #         b. Translate each of the 100 points by toler*0.7 along the face normal
176 #            and check that we obtain a result.
177 for ii in range(100):
178     # cc - coordinates of point #ii
179     cc = listOfCoords[ii*3 : ii*3 + 3]
180
181     pnt = geompy.MakeVertex(cc[0], cc[1], cc[2])
182     normal = geompy.GetNormal(HorseSaddle, pnt)
183     vv = geompy.VectorCoordinates(normal)
184     norm = np.linalg.norm(vv)
185     if norm > toler: 
186         vec = vv / norm
187         # a. Move cc by toler*2.0 (XYZtoUV should fail)
188         pp_2tol = [cc[0] + vec[0]*toler*2.0,
189                    cc[1] + vec[1]*toler*2.0,
190                    cc[2] + vec[2]*toler*2.0]
191         try:
192             geompy.XYZtoUV(HorseSaddle, pp_2tol)
193         except RuntimeError:
194             pass
195         except Exception:
196             assert False, 'Unexpected exception raised'
197         else:
198             assert False, 'XYZtoUV should raise an exception if input point is out of face'
199             pass
200
201         # b. Move cc by toler*0.7 (XYZtoUV should not fail)
202         pp_07tol = [cc[0] + vec[0]*toler*0.7,
203                     cc[1] + vec[1]*toler*0.7,
204                     cc[2] + vec[2]*toler*0.7]
205         UV_pp = geompy.XYZtoUV(HorseSaddle, pp_07tol, False)
206         # compare with value from listOfParams (computed above)
207         UV_cc = listOfParams[ii*2 : ii*2 + 2]
208         for (c1, c2) in zip(UV_pp, UV_cc):
209             assert(abs(c1 - c2) < toler)
210             pass
211         pass
212     pass