Salome HOME
[bos #36247] EDF 25230 - conversion xyz => uv KO
[modules/geom.git] / test / test_kind_of_shape.py
1 # Test KindOfShape method for Edges
2
3 import math
4 import salome
5 from inspect import getfile
6 from os.path import abspath, dirname, join
7 salome.salome_init_without_session()
8 import GEOM
9
10 from salome.geom import geomBuilder
11 geompy = geomBuilder.New()
12
13
14 def isEqual(v1, v2, tol=1.e-5):
15   return abs(v1 - v2) < tol
16
17 def isEqualPoint(p1, p2, tol=1.e-5):
18   return isEqual(p1[0], p2[0], tol) and isEqual(p1[1], p2[1], tol) and isEqual(p1[2], p2[2], tol)
19
20 def assertEqualType(props, t):
21   assert (props[0]==t), f"Expected type {t}, but was {props[0]}"
22
23 def checkSegment(props, p1, p2):
24   assertEqualType(props, geompy.kind.SEGMENT)
25   p = props[1:4]
26   assert isEqualPoint(p, p1), f"Expected SEGMENT start point ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
27   p = props[4:7]
28   assert isEqualPoint(p, p2), f"Expected SEGMENT end point ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
29
30 def checkCircle(props, c, d, r):
31   assertEqualType(props, geompy.kind.CIRCLE)
32   p = props[1:4]
33   assert isEqualPoint(p, c), f"Expected CIRCLE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
34   p = props[4:7]
35   assert isEqualPoint(p, d), f"Expected CIRCLE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
36   assert isEqual(props[7], r), f"Expected CIRCLE radius {r:.4f}, but was {props[7]:.4f}"
37
38 def checkArcCircle(props, c, d, r, p1, p2):
39   assertEqualType(props, geompy.kind.ARC_CIRCLE)
40   p = props[1:4]
41   assert isEqualPoint(p, c), f"Expected ARC_CIRCLE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
42   p = props[4:7]
43   assert isEqualPoint(p, d), f"Expected ARC_CIRCLE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
44   assert isEqual(props[7], r), f"Expected ARC_CIRCLE radius {r:.4f}, but was {props[7]:.4f}"
45   p = props[8:11]
46   assert isEqualPoint(p, p1), f"Expected ARC_CIRCLE start point ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
47   p = props[11:14]
48   assert isEqualPoint(p, p2), f"Expected ARC_CIRCLE end point ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
49
50 def checkEllipse(props, c, d, r1, r2, vx, vy):
51   assertEqualType(props, geompy.kind.ELLIPSE)
52   p = props[1:4]
53   assert isEqualPoint(p, c), f"Expected ELLIPSE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
54   p = props[4:7]
55   assert isEqualPoint(p, d), f"Expected ELLIPSE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
56   assert isEqual(props[7], r1), f"Expected ELLIPSE major radius {r1:.4f}, but was {props[7]:.4f}"
57   assert isEqual(props[8], r2), f"Expected ELLIPSE minor radius {r2:.4f}, but was {props[8]:.4f}"
58   p = props[9:12]
59   assert isEqualPoint(p, vx), f"Expected ELLIPSE x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
60   p = props[12:15]
61   assert isEqualPoint(p, vy), f"Expected ELLIPSE y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
62
63 def checkArcEllipse(props, c, d, r1, r2, p1, p2, vx, vy):
64   assertEqualType(props, geompy.kind.ARC_ELLIPSE)
65   p = props[1:4]
66   assert isEqualPoint(p, c), f"Expected ARC_ELLIPSE center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
67   p = props[4:7]
68   assert isEqualPoint(props[4:7], d), f"Expected ARC_ELLIPSE direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
69   assert isEqual(props[7], r1), f"Expected ARC_ELLIPSE major radius {r1:.4f}, but was {props[7]:.4f}"
70   assert isEqual(props[8], r2), f"Expected ARC_ELLIPSE minor radius {r2:.4f}, but was {props[8]:.4f}"
71   p = props[9:12]
72   assert isEqualPoint(props[9:12], p1), f"Expected ARC_ELLIPSE start point ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
73   p = props[12:15]
74   assert isEqualPoint(props[12:15], p2), f"Expected ARC_ELLIPSE end point ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
75   p = props[15:18]
76   assert isEqualPoint(props[15:18], vx), f"Expected ARC_ELLIPSE x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
77   p = props[18:21]
78   assert isEqualPoint(props[18:21], vy), f"Expected ARC_ELLIPSE y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
79
80 def checkHyperbola(props, c, d, r1, r2, vx, vy):
81   assertEqualType(props, geompy.kind.HYPERBOLA)
82   p = props[1:4]
83   assert isEqualPoint(p, c), f"Expected HYPERBOLA center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
84   p = props[4:7]
85   assert isEqualPoint(p, d), f"Expected HYPERBOLA direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
86   assert isEqual(props[7], r1), f"Expected HYPERBOLA major radius {r1:.4f}, but was {props[7]:.4f}"
87   assert isEqual(props[8], r2), f"Expected HYPERBOLA minor radius {r2:.4f}, but was {props[8]:.4f}"
88   p = props[9:12]
89   assert isEqualPoint(p, vx), f"Expected HYPERBOLA x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
90   p = props[12:15]
91   assert isEqualPoint(p, vy), f"Expected HYPERBOLA y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
92
93 def checkParabola(props, c, d, f, vx, vy):
94   assertEqualType(props, geompy.kind.PARABOLA)
95   p = props[1:4]
96   assert isEqualPoint(p, c), f"Expected PARABOLA center point ({c[0]:.4f}, {c[1]:.4f}, {c[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
97   p = props[4:7]
98   assert isEqualPoint(p, d), f"Expected PARABOLA direction ({d[0]:.4f}, {d[1]:.4f}, {d[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
99   assert isEqual(props[7], f), f"Expected PARABOLA focal length {f:.4f}, but was {props[7]:.4f}"
100   p = props[8:11]
101   assert isEqualPoint(p, vx), f"Expected PARABOLA x-direction ({vx[0]:.4f}, {vx[1]:.4f}, {vx[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
102   p = props[11:14]
103   assert isEqualPoint(p, vy), f"Expected PARABOLA y-direction ({vy[0]:.4f}, {vy[1]:.4f}, {vy[2]:.4f}), but was ({p[0]:.4f}, {p[1]:.4f}, {p[2]:.4f})"
104
105 def assertPoles(props, np, pfirst, poles):
106   n = len(poles)
107   assert (n<=np), f"Too much poles given ({n}), but should not be more than {np}"
108   for i in range(0,n-1):
109     p1 = poles[i]
110     p2 = props[pfirst+i*3:pfirst+(i+1)*3]
111     assert isEqualPoint(p1, p2), f"Expected pole #{i+1} ({p1[0]:.4f}, {p1[1]:.4f}, {p1[2]:.4f}), but was ({p2[0]:.4f}, {p2[1]:.4f}, {p2[2]:.4f})"
112
113 def assertKnots(props, nk, kfirst, knots):
114   n = len(knots)
115   assert (n<=nk), f"Too much knots given ({n}), but should not be more than {nk}"
116   try:
117     for i in range(0,n-1):
118       assert isEqual(knots[i], props[kfirst+i]), f"Expected knot #{i+1} {knots[i]:.4f}, but was {props[kfirst+i]:.4f}"
119   except:
120     assert("Number of knots does not match expected number")
121
122 def assertWeights(props, nw, wfirst, weights):
123   n = len(weights)
124   assert (n<=nw), f"Too much weights given ({n}), but should not be more than {nw}"
125   try:
126     for i in range(0,n-1):
127       assert isEqual(weights[i], props[wfirst+i]), f"Expected weight #{i+1} {weights[i]:.4f}, but was {props[wfirst+i]:.4f}"
128   except:
129     assert("Number of weights does not match expected number")
130
131 def assertMultiplicities(props, nm, mfirst, multis):
132   n = len(multis)
133   assert (n<=nm), f"Too much multiplicities given ({n}), but should not be more than {nm}"
134   try:
135     for i in range(0,n-1):
136       assert isEqual(multis[i], props[mfirst+i]), f"Expected multiplicity #{i+1} {multis[i]:.4f}, but was {props[mfirst+i]:.4f}"
137   except:
138     assert("Number of multiplicities does not match expected number")
139
140 def checkBSpline(props, period, deg, np, nk, nw, nm, poles=[], knots=[], weights=[], multis=[]):
141   assertEqualType(props, geompy.kind.CRV_BSPLINE)
142   assert (period==props[1]), f"Expected CRV_BSPLINE periodicity ({period}), but was ({props[1]})"
143   assert (deg==props[2]), f"Expected CRV_BSPLINE degree ({deg}), but was ({props[2]})"
144   assert (np==props[3]), f"Expected CRV_BSPLINE number of poles ({np}), but was ({props[3]})"
145   assert (nk==props[4]), f"Expected CRV_BSPLINE number of knots ({nk}), but was ({props[4]})"
146   assert (nw==props[5]), f"Expected CRV_BSPLINE number of weights ({nw}), but was ({props[5]})"
147   assert (nm==props[6]), f"Expected CRV_BSPLINE number of multiplicities ({nm}), but was ({props[6]})"
148   if poles and len(poles):
149     assertPoles(props, np, 7, poles)
150   if knots and len(knots):
151     assertKnots(props, nk, 7+3*np, knots)
152   if weights and len(weights):
153     assertWeights(props, nw, 7+3*np+nk, weights)
154
155 def checkBezier(props, np, nw, poles=[]):
156   assertEqualType(props, geompy.kind.CRV_BEZIER)
157   assert (np==props[1]), f"Expected CRV_BEZIER number of poles ({np}), but was ({props[1]})"
158   assert (nw==props[2]), f"Expected CRV_BEZIER number of weights ({nw}), but was ({props[2]})"
159   if poles and len(poles):
160     assertPoles(props, np, 3, poles)
161
162     
163 data_dir = abspath(join(dirname(getfile(lambda: None)), 'data'))
164
165 O = geompy.MakeVertex(0, 0, 0)
166 OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
167 OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
168 OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
169 Vertex_1 = geompy.MakeVertex(0, 0, 0)
170 Vertex_2 = geompy.MakeVertex(50, 100, 0)
171 Vertex_3 = geompy.MakeVertex(-10, 60, 0)
172 Vertex_4 = geompy.MakeVertex(0, 100, 0)
173 Vertex_5 = geompy.MakeVertex(-100, 100, 0)
174 Vertex_6 = geompy.MakeVertex(-100, 0, 0)
175 Vertex_7 = geompy.MakeVertex(-200, 0, 0)
176 Vertex_8 = geompy.MakeVertex(-200, 100, 0)
177
178 # create or import some curves
179 Line_1 = geompy.MakeLineTwoPnt(Vertex_1, Vertex_2)
180 Circle_1 = geompy.MakeCircle(Vertex_2, OZ, 50)
181 Ellipse_1 = geompy.MakeEllipse(Vertex_1, OZ, 200, 100, Line_1)
182 Arc_1 = geompy.MakeArc(Vertex_2, Vertex_3, Vertex_1)
183 Curve_1 = geompy.MakeCurveParametric("t", "50*sin(t)", "0", 0, 360, 30, GEOM.Interpolation, True)
184 Curve_2 = geompy.MakeCurveParametric("-t", "50*cos(t)", "t", 0, 360, 14, GEOM.Bezier, True)
185 Curve_3 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve3.brep"))
186 Curve_4 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve4.brep"))
187 Curve_5 = geompy.MakeInterpol([Vertex_1, Vertex_4, Vertex_5, Vertex_6, Vertex_7, Vertex_8], False, False)
188 Curve_6 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve6.brep"))
189 Curve_7 = geompy.MakeBezier([Vertex_5, Vertex_6, Vertex_7, Vertex_8], True)
190 Curve_8 = geompy.MakeBezier([Vertex_5, Vertex_6, Vertex_1, Vertex_4], False)
191 Curve_9 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve9.brep"))
192 Curve_10 = geompy.ImportBREP(join(data_dir, "KindOfShape_Curve10.brep"))
193
194 # check all curves
195 props = geompy.KindOfShape(Line_1)
196 # [SEGMENT, 0.,0.,0., 50.,100.,0.]
197 checkSegment(props, [0,0,0], [50,100,0])
198
199 props = geompy.KindOfShape(Circle_1)
200 # [CIRCLE, 50.,100.,0., 0.,0.,1., 50.]
201 checkCircle(props, [50,100,0], [0,0,1], 50)
202
203 props = geompy.KindOfShape(Ellipse_1)
204 # [ELLIPSE, 0.,0.,0., 0.,0.,1., 200., 100., 0.44721,0.89443,0., 0.44721,0.89443,0.]
205 checkEllipse(props, [0,0,0], [0,0,1], 200, 100, [0.4472136,0.8944272,0], [-0.8944272,0.4472136,0])
206
207 props = geompy.KindOfShape(Arc_1)
208 # [ARC_CIRCLE, 47.5,38.75,0., 0.,0.,1., 61.301, 50.,100.,0., 0.,0.,0.]
209 checkArcCircle(props, [47.5,38.75,0], [0,0,1], 61.301, [50,100,0], [0,0,0])
210
211 props = geompy.KindOfShape(Curve_1)
212 # [CRV_BSPLINE, 0, 3, 33, 31, 0, 31, 0.,0.,0.,..., 4,1,...,1,4]
213 checkBSpline(props, 0, 3, 33, 31, 0, 31, [[0,0,0],[5.246092,-6.374961,0],[10.613646,-25.338811,0],[19.662636,-44.299221,0]], [0,29.39007,51.399444,64.149986], [], [4,1,1,1,1])
214
215 props = geompy.KindOfShape(Curve_2)
216 # [CRV_BEZIER, 15, 0, 0.,50.,0.,...,-360.,-14.18455,360.]
217 checkBezier(props, 15, 0, [[0,50,0],[-25.714286,41.780762,25.714286],[-51.428571,19.825283,51.428571],[-77.142857,-8.648146,77.142857]])
218
219 props = geompy.KindOfShape(Curve_3)
220 # [CRV_BSPLINE, 1, 3, 8, 9, 0, 0, -100.,0.,0.,..., 0.,1.,2.,3.,4.,5.,6.,7.,8.]
221 checkBSpline(props, 1, 3, 8, 9, 0, 0, [[-100,0,0],[-200,200,0],[0,100,0],[200,200,0],[100,0,0],[200,-200,0],[0,-100,0],[-200,-200,0]], [0,1,2,3,4,5,6,7,8])
222
223 props = geompy.KindOfShape(Curve_4)
224 # [CRV_BSPLINE, 1, 2, 3, 4, 3, 0, 0.,0.,0., 100.,200.,0., 200.,0.,0., 0,1,2,3, 200.,100.,100.]
225 checkBSpline(props, 1, 2, 3, 4, 3, 0, [[0,0,0],[100,200,0],[200,0,0]], [0,1,2,3], [200,100,100])
226
227 props = geompy.KindOfShape(Curve_5)
228 # [CRV_BSPLINE, 0, 3, 8, 6, 0, 6, 0.,0.,0.,..., 100.,0.,0.,100.,200.,300.,400.,500., 4,1,1,1,1,4]
229 checkBSpline(props, 0, 3, 8, 6, 0, 6, [[0,0,0],[38.888889,50,0],[23.684211,113.157895,0]], [0,100,200,300,400,500], [], [4,1,1,1,1,4])
230
231 props = geompy.KindOfShape(Curve_6)
232 # [ARC_ELLIPSE, -18.46154,0.,64.61538, 0.57668,0.,-0.81697, 96.03993, 76.25867, 50.,-37.25439,112.94118, 50.,37.25439,112.94118, -0.81697,0.,-0.57668, 0.,1.,0.]
233 checkArcEllipse(props, [-18.46154,0,64.61538], [0.57668,0,-0.81697], 96.03993, 76.25867, [50,-37.25439,112.94118], [50,37.25439,112.94118], [-0.81697,0,-0.57668], [0,1,0])
234
235 props = geompy.KindOfShape(Curve_7)
236 # [CRV_BEZIER, 5, 0, -100.,100.,0., -100.,0.,0., -200.,0.,0., -200.,100.,0., -100.,100.,0.]
237 checkBezier(props, 5, 0, [[-100,100,0],[-100,0,0],[-200,0,0],[-200,100,0],[-100,100,0]])
238
239 props = geompy.KindOfShape(Curve_8)
240 # [CRV_BEZIER, 4, 0, -100.,100.,0., -100.,0.,0., 0.,0.,0., 0.,100.,0.]
241 checkBezier(props, 4, 0, [[-100,100,0],[-100,0,0],[0,0,0],[0,100,0]])
242
243 props = geompy.KindOfShape(Curve_9)
244 # [HYPERBOLA, -50., 0., 300., 1., 0., 0., 75., 50., 0., 0., -1., 0., 1., 0.]
245 checkHyperbola(props, [-50,0,300], [1,0,0], 75, 50, [0,0,-1], [0,1,0])
246
247 props = geompy.KindOfShape(Curve_10)
248 # [PARABOLA, -25.,0.,262.5, -0.83205,0.,-0.5547, 13.8675, 0.5547,0.,-0.83205, 0.,-1.,0.]
249 checkParabola(props, [-25,0,262.5], [-0.83205,0,-0.5547], 13.867505, [0.5547,0,-0.83205], [0,-1,0])