Salome HOME
Python API for periodic B-splines and unit-tests for creation and modification.
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintTangentBSpline.py
1 # Copyright (C) 2019-2020  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21     Test constraint "Tangent" applied to B-spline and another entity
22 """
23
24 import unittest
25 import math
26
27 from salome.shaper import model
28
29 from GeomAPI import *
30 from GeomAlgoAPI import *
31 from SketchAPI import *
32
33 __updated__ = "2020-01-22"
34
35 class TestTangentBSpline(unittest.TestCase):
36   def setUp(self):
37     model.begin()
38     self.myDocument = model.moduleDocument()
39     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
40     self.myPoles = [GeomAPI_Pnt2d(-10, -30), GeomAPI_Pnt2d(20, -15), GeomAPI_Pnt2d(-10, 0), GeomAPI_Pnt2d(20, 15), GeomAPI_Pnt2d(-10, 30)]
41     self.myWeights = [1, 3, 5, 3, 1]
42     self.mySpline = self.mySketch.addSpline(poles = self.myPoles, weights = self.myWeights)
43     self.myControlPoles = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
44     self.myControlLines = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
45     model.do()
46
47     self.myExpectedFailure = False
48     self.myDOF = len(self.myPoles) * 2
49     self.myNbPoints = len(self.myPoles)
50     self.myNbLines = len(self.myPoles) - 1
51     self.myNbArcs = 0
52     self.myNbCircles = 0
53     self.myNbEllipses = 0
54     self.myNbEllipticArcs = 0
55     self.myNbBSplines = 1
56     self.myNbInternals = len(self.myPoles) * 3 - 2
57     self.myNbCoincidence = 0
58     self.myNbTangency = 0
59
60   def tearDown(self):
61     model.end()
62     if self.myExpectedFailure:
63       assert(self.mySketch.solverError() != ""), "PlaneGCS limitation: if you see this message, then PlaneGCS has solved the set of constraints correctly"
64       model.undo()
65     else:
66       self.checkDOF()
67       model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
68       model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
69       model.testNbSubFeatures(self.mySketch, "SketchArc", self.myNbArcs)
70       model.testNbSubFeatures(self.mySketch, "SketchCircle", self.myNbCircles)
71       model.testNbSubFeatures(self.mySketch, "SketchEllipse", self.myNbEllipses)
72       model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", self.myNbEllipticArcs)
73       model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbBSplines)
74       model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidenceInternal", self.myNbInternals)
75       model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", self.myNbCoincidence)
76       model.testNbSubFeatures(self.mySketch, "SketchConstraintTangent", self.myNbTangency)
77
78
79   def checkDOF(self):
80     self.assertEqual(model.dof(self.mySketch), self.myDOF)
81
82   def assertTangentFeatures(self, theFeature1, theFeature2):
83     shapes = [theFeature1.results()[-1].resultSubShapePair()[0].shape(),
84               theFeature2.results()[-1].resultSubShapePair()[0].shape()]
85     for s in shapes:
86       e = shapeToEdge(s)
87       if e.isLine() or e.isArc() or e.isEllipse():
88         params = e.getRange()
89         e.setRange(params[0] - 100, params[1] + 100)
90     # TODO (azv): complete checking the tangent curves
91
92   def assertPointLineDistance(self, thePoint, theLine, theExpectedDistance = 0):
93     dist = model.distancePointLine(thePoint, theLine)
94     self.assertAlmostEqual(dist, theExpectedDistance)
95
96   def assertTangentLineCircle(self, theLine, theCircle):
97     self.assertPointLineDistance(theCircle.center(), theLine, theCircle.radius().value())
98
99   def assertTangentLineEllipse(self, theLine, theEllipse):
100     aLine = GeomAPI_Lin2d(theLine.startPoint().pnt(), theLine.endPoint().pnt())
101     projF1 = aLine.project(theEllipse.firstFocus().pnt())
102     projF2 = aLine.project(theEllipse.secondFocus().pnt())
103
104     distF1P1 = model.distancePointPoint(theEllipse.firstFocus(), projF1)
105     distF2P2 = model.distancePointPoint(theEllipse.secondFocus(), projF2)
106
107     tgPoint = GeomAPI_Pnt2d((projF1.x() * distF2P2 + projF2.x() * distF1P1) / (distF1P1 + distF2P2), (projF1.y() * distF2P2 + projF2.y() * distF1P1) / (distF1P1 + distF2P2))
108     distF1T = model.distancePointPoint(theEllipse.firstFocus(), tgPoint)
109     distF2T = model.distancePointPoint(theEllipse.secondFocus(), tgPoint)
110     self.assertAlmostEqual(distF1T + distF2T, 2 * theEllipse.majorRadius().value())
111
112
113   def test_line_tangent(self):
114     """ Test 1. Set tangency between B-spline and a line
115     """
116     aLine = self.mySketch.addLine(10, -10, 90, 40)
117     self.myNbLines += 1
118     self.myDOF += 4
119     model.do()
120
121     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
122     self.myNbTangency += 1
123     self.myDOF -= 1
124     model.do()
125
126     self.assertTangentFeatures(aLine, self.mySpline)
127
128   def test_circle_tangent(self):
129     """ Test 2. Set tangency between B-spline and a circle
130     """
131     aCircle = self.mySketch.addCircle(10, 10, 20)
132     self.myNbCircles += 1
133     self.myDOF += 3
134     model.do()
135
136     self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
137     self.myNbTangency += 1
138     self.myDOF -= 1
139     model.do()
140
141     self.assertTangentFeatures(aCircle, self.mySpline)
142
143   def test_arc_tangent(self):
144     """ Test 3. Set tangency between B-spline and an arc
145     """
146     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
147     self.myNbArcs += 1
148     self.myDOF += 5
149     model.do()
150
151     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
152     self.myNbTangency += 1
153     self.myDOF -= 1
154     model.do()
155
156     self.assertTangentFeatures(anArc, self.mySpline)
157
158   def test_ellipse_tangent(self):
159     """ Test 4. Set tangency between B-spline and an ellipse
160     """
161     anEllipse = self.mySketch.addEllipse(10, 10, 20, 10, 7)
162     self.myNbEllipses += 1
163     self.myDOF += 5
164     model.do()
165
166     self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
167     self.myNbTangency += 1
168     self.myDOF -= 1
169     model.do()
170
171     self.assertTangentFeatures(anEllipse, self.mySpline)
172
173   def test_elliptic_arc_tangent(self):
174     """ Test 5. Set tangency between B-spline and an elliptic arc
175     """
176     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
177     self.myNbEllipticArcs += 1
178     self.myDOF += 7
179     model.do()
180
181     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
182     self.myNbTangency += 1
183     self.myDOF -= 1
184     model.do()
185
186     self.assertTangentFeatures(anEllipticArc, self.mySpline)
187
188   def test_spline_tangent(self):
189     """ Test 6. Set tangency between two B-spline curves
190     """
191     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
192     self.myNbBSplines += 1
193     self.myDOF += aSpline.poles().size() * 2
194     model.do()
195
196     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
197     self.myNbTangency += 1
198     self.myDOF -= 1
199     model.do()
200
201     #self.assertTangentFeatures(aSpline, self.mySpline)
202     self.myExpectedFailure = True
203
204
205   def test_line_tangent_coincident_by_pole(self):
206     """ Test 7. Set tangency between B-spline and a line coincident with B-spline start point
207     """
208     aLine = self.mySketch.addLine(10, -10, 90, 40)
209     self.myNbLines += 1
210     self.myDOF += 4
211     model.do()
212
213     self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.result())
214     self.myNbCoincidence += 1
215     self.myDOF -= 1
216     model.do()
217
218     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
219     self.myNbTangency += 1
220     self.myDOF -= 1
221     model.do()
222
223     self.assertPointLineDistance(aLine.startPoint(), self.myControlLines[0])
224     self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
225
226   def test_circle_tangent_coincident_by_pole(self):
227     """ Test 8. Set tangency between B-spline and a circle coincident with B-spline end point
228     """
229     aCircle = self.mySketch.addCircle(10, 10, 20)
230     self.myNbCircles += 1
231     self.myDOF += 3
232     model.do()
233
234     self.mySketch.setCoincident(self.mySpline.startPoint(), aCircle.defaultResult())
235     self.myNbCoincidence += 1
236     self.myDOF -= 1
237     model.do()
238
239     self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
240     self.myNbTangency += 1
241     self.myDOF -= 1
242     model.do()
243
244     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), aCircle)
245
246   def test_arc_tangent_coincident_by_pole(self):
247     """ Test 9. Set tangency between B-spline and an arc coincident with B-spline end point
248     """
249     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
250     self.myNbArcs += 1
251     self.myDOF += 5
252     model.do()
253
254     self.mySketch.setCoincident(self.mySpline.endPoint(), anArc.defaultResult())
255     self.myNbCoincidence += 1
256     self.myDOF -= 1
257     model.do()
258
259     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
260     self.myNbTangency += 1
261     self.myDOF -= 1
262     model.do()
263
264     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[-1]), anArc)
265
266   def test_ellipse_tangent_coincident_by_pole(self):
267     """ Test 10. Set tangency between B-spline and an ellipse coincident with B-spline start point
268     """
269     anEllipse = self.mySketch.addEllipse(10, 10, 20, 10, 7)
270     self.myNbEllipses += 1
271     self.myDOF += 5
272     model.do()
273
274     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipse.defaultResult())
275     self.myNbCoincidence += 1
276     self.myDOF -= 1
277     model.do()
278
279     self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
280     self.myNbTangency += 1
281     self.myDOF -= 1
282     model.do()
283
284     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipse)
285
286   def test_elliptic_arc_tangent_coincident_by_pole(self):
287     """ Test 11. Set tangency between B-spline and an elliptic arc coincident with B-spline start point
288     """
289     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
290     self.myNbEllipticArcs += 1
291     self.myDOF += 7
292     model.do()
293
294     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.defaultResult())
295     self.myNbCoincidence += 1
296     self.myDOF -= 1
297     model.do()
298
299     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
300     self.myNbTangency += 1
301     self.myDOF -= 1
302     model.do()
303
304     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
305
306
307   def test_line_tangent_coincident_by_boundaries(self):
308     """ Test 12. Set tangency between B-spline and a line, coincident by their start points
309     """
310     aLine = self.mySketch.addLine(10, -10, 90, 40)
311     self.myNbLines += 1
312     self.myDOF += 4
313     model.do()
314
315     self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.startPoint())
316     self.myNbCoincidence += 1
317     self.myDOF -= 2
318     model.do()
319
320     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
321     self.myNbTangency += 1
322     self.myDOF -= 1
323     model.do()
324
325     self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
326
327   def test_arc_tangent_coincident_by_boundaries(self):
328     """ Test 13. Set tangency between B-spline and an arc, coincident by their start points
329     """
330     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
331     self.myNbArcs += 1
332     self.myDOF += 5
333     model.do()
334
335     self.mySketch.setCoincident(self.mySpline.startPoint(), anArc.startPoint())
336     self.myNbCoincidence += 1
337     self.myDOF -= 2
338     model.do()
339
340     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
341     self.myNbTangency += 1
342     self.myDOF -= 1
343     model.do()
344
345     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
346
347   def test_elliptic_arc_tangent_coincident_by_boundaries(self):
348     """ Test 14. Set tangency between B-spline and an elliptic arc, coincident by their start points
349     """
350     anEllipticArc = self.mySketch.addEllipticArc(10, -10, 20, -10, 22.2065556157337, -10, 10, 3, True)
351     self.myNbEllipticArcs += 1
352     self.myDOF += 7
353     model.do()
354
355     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.startPoint())
356     self.myNbCoincidence += 1
357     self.myDOF -= 2
358     model.do()
359
360     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
361     self.myNbTangency += 1
362     self.myDOF -= 1
363     model.do()
364
365     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
366
367   def test_spline_tangent_coincident_by_boundaries(self):
368     """ Test 15. Set tangency between two B-spline curves coincident with B-spline start point
369     """
370     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
371     self.myNbBSplines += 1
372     self.myDOF += aSpline.poles().size() * 2
373     model.do()
374
375     self.mySketch.setCoincident(self.mySpline.startPoint(), aSpline.startPoint())
376     self.myNbCoincidence += 1
377     self.myDOF -= 2
378     model.do()
379
380     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
381     self.myNbTangency += 1
382     self.myDOF -= 1
383     model.do()
384
385     #self.assertPointLineDistance(aSpline.poles()[1], self.myControlLines[0])
386     self.myExpectedFailure = True
387
388
389   def test_line_tangent_coincident_by_aux(self):
390     """ Test 16. Set tangency between B-spline and a line, coincident by their start points
391     """
392     aLine = self.mySketch.addLine(10, -10, 90, 40)
393     self.myNbLines += 1
394     self.myDOF += 4
395     model.do()
396
397     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aLine.startPoint())
398     self.myNbCoincidence += 1
399     self.myDOF -= 2
400     model.do()
401
402     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
403     self.myNbTangency += 1
404     self.myDOF -= 1
405     model.do()
406
407     self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
408
409   def test_arc_tangent_coincident_by_aux(self):
410     """ Test 17. Set tangency between B-spline and an arc, coincident by their start points
411     """
412     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
413     self.myNbArcs += 1
414     self.myDOF += 5
415     model.do()
416
417     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anArc.startPoint())
418     self.myNbCoincidence += 1
419     self.myDOF -= 2
420     model.do()
421
422     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
423     self.myNbTangency += 1
424     self.myDOF -= 1
425     model.do()
426
427     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
428
429   def test_elliptic_arc_tangent_coincident_by_aux(self):
430     """ Test 18. Set tangency between B-spline and an elliptic arc, coincident by their start points
431     """
432     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
433     self.myNbEllipticArcs += 1
434     self.myDOF += 7
435     model.do()
436
437     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anEllipticArc.startPoint())
438     self.myNbCoincidence += 1
439     self.myDOF -= 2
440     model.do()
441
442     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
443     self.myNbTangency += 1
444     self.myDOF -= 1
445     model.do()
446
447     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
448
449   def test_spline_tangent_coincident_by_aux(self):
450     """ Test 19. Set tangency between two B-spline curves coincident with B-spline start point
451     """
452     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
453     self.myNbBSplines += 1
454     self.myDOF += aSpline.poles().size() * 2
455     model.do()
456
457     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aSpline.startPoint())
458     self.myNbCoincidence += 1
459     self.myDOF -= 2
460     model.do()
461
462     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
463     self.myNbTangency += 1
464     self.myDOF -= 1
465     model.do()
466
467     #self.assertPointLineDistance(aSpline.poles().pnt(1), self.myControlLines[0])
468     self.myExpectedFailure = True
469
470
471
472 if __name__ == "__main__":
473     test_program = unittest.main(exit=False)
474     assert test_program.result.wasSuccessful(), "Test failed"
475     #assert model.checkPythonDump()