Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestCurveFitting4.py
1 # Copyright (C) 2020-2023  CEA, EDF
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 creation of B-spline curve passing through the list of points
22 """
23
24 import math
25 import unittest
26
27 from salome.shaper import model
28
29 from GeomAPI import *
30 from ModelAPI import *
31 from ModelHighAPI import *
32 from SketchAPI import *
33 from GeomAlgoAPI import GeomAlgoAPI_ShapeTools as shapetools
34
35 __updated__ = "2020-06-28"
36
37 TOLERANCE = 1.e-7
38 POINT_NAMES = []
39
40 class TestApproximationExternal(unittest.TestCase):
41   def setUp(self):
42     model.begin()
43     self.myDocument = model.moduleDocument()
44     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
45     self.myPoints = []
46     self.myPointRefs = []
47     for name in POINT_NAMES:
48       proj = self.mySketch.addProjection(model.selection("VERTEX", name), False)
49       self.myPoints.append(SketchAPI_Point(proj.createdFeature()))
50       self.myPointRefs.append(self.myPoints[-1].coordinates())
51     self.myDOF = 0
52     self.myNbPoints = len(self.myPoints)
53     self.myNbLines = 0
54     self.myNbSplines = 0
55     self.myNbPeriodicSplines = 0
56
57   def tearDown(self):
58     self.checkDOF()
59     model.end()
60     model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
61     model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
62     model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbSplines)
63     model.testNbSubFeatures(self.mySketch, "SketchBSplinePeriodic", self.myNbPeriodicSplines)
64
65
66   def checkDOF(self):
67     self.assertEqual(model.dof(self.mySketch), self.myDOF)
68
69   def checkPointsOnCurve(self, theCurve, theTolerance = 1.e-3):
70     for p in self.myPoints:
71       shape = p.defaultResult().shape()
72       self.assertTrue(shapetools.minimalDistance(shape, theCurve) < theTolerance)
73
74
75   def test_approximation_default(self):
76     """ Test 1. Create B-spline curve approximating a set of points with the default precision
77     """
78     self.mySpline = self.mySketch.addApproximation(self.myPointRefs)
79     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
80     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
81     self.myDOF += len(curve.poles()) * 2 - 4
82     self.myNbSplines += 1
83     model.do()
84
85     self.assertTrue(self.mySpline.feature())
86     self.assertTrue(self.mySpline.feature().error() == "")
87     self.assertTrue(self.mySpline.degree().value() == 4)
88     self.assertTrue(curve.isPeriodic() == False)
89     EXPECTED_LEN = 189.17832318
90     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
91     # check points lying of the created curve
92     self.checkPointsOnCurve(edge)
93
94   def test_approximation_custom(self):
95     """ Test 2. Create B-spline curve approximating a set of points with the custom precision
96     """
97     PRECISION = 10
98     self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION)
99     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
100     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
101     self.myDOF += len(curve.poles()) * 2 - 4
102     self.myNbSplines += 1
103     model.do()
104
105     self.assertTrue(self.mySpline.feature())
106     self.assertTrue(self.mySpline.feature().error() == "")
107     self.assertTrue(self.mySpline.degree().value() == 3)
108     self.assertTrue(curve.isPeriodic() == False)
109     EXPECTED_LEN = 166.97400096
110     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
111     # check points lying of the created curve
112     self.checkPointsOnCurve(edge, PRECISION)
113
114   def test_approximation_periodic_default(self):
115     """ Test 3. Create periodic B-spline curve approximating set of points with the default precision
116     """
117     self.mySpline = self.mySketch.addApproximation(self.myPointRefs, periodic = True)
118     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
119     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
120     self.myDOF += len(curve.poles()) * 2
121     self.myNbPeriodicSplines += 1
122     model.do()
123
124     self.assertTrue(self.mySpline.feature())
125     self.assertTrue(self.mySpline.feature().error() == "")
126     self.assertTrue(self.mySpline.degree().value() == 3)
127     self.assertTrue(curve.isPeriodic() == True)
128     EXPECTED_LEN = 390.98013747
129     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
130     # check points lying of the created curve
131     self.checkPointsOnCurve(edge)
132
133   def test_approximation_periodic_custom(self):
134     """ Test 4. Create periodic B-spline curve approximating set of points with the custom precision
135     """
136     PRECISION = 10
137     self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION, periodic = True)
138     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
139     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
140     self.myDOF += len(curve.poles()) * 2
141     self.myNbPeriodicSplines += 1
142     model.do()
143
144     self.assertTrue(self.mySpline.feature())
145     self.assertTrue(self.mySpline.feature().error() == "")
146     self.assertTrue(self.mySpline.degree().value() == 3)
147     self.assertTrue(curve.isPeriodic() == True)
148     EXPECTED_LEN = 390.98013747
149     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
150     # check points lying of the created curve
151     self.checkPointsOnCurve(edge, PRECISION)
152
153   def test_approximation_closed_default(self):
154     """ Test 5. Create closed but not periodic B-spline curve approximating set of points with the default precision
155     """
156     self.mySpline = self.mySketch.addApproximation(self.myPointRefs, closed = True)
157     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
158     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
159     self.myDOF += len(curve.poles()) * 2 - 4
160     self.myNbSplines += 1
161     model.do()
162
163     self.assertTrue(self.mySpline.feature())
164     self.assertTrue(self.mySpline.feature().error() == "")
165     self.assertTrue(self.mySpline.degree().value() == 5)
166     self.assertTrue(curve.isPeriodic() == False)
167     self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
168     EXPECTED_LEN = 413.18489109
169     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
170     # check points lying of the created curve
171     self.checkPointsOnCurve(edge)
172
173   def test_approximation_closed_custom(self):
174     """ Test 6. Create closed but not periodic B-spline curve approximating set of points with the custom precision
175     """
176     PRECISION = 10
177     self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION, closed = True)
178     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
179     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
180     self.myDOF += len(curve.poles()) * 2 - 4
181     self.myNbSplines += 1
182     model.do()
183
184     self.assertTrue(self.mySpline.feature())
185     self.assertTrue(self.mySpline.feature().error() == "")
186     self.assertTrue(self.mySpline.degree().value() == 3)
187     self.assertTrue(curve.isPeriodic() == False)
188     self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
189     EXPECTED_LEN = 276.27724118
190     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
191     # check points lying of the created curve
192     self.checkPointsOnCurve(edge, PRECISION)
193
194   def test_approximation_reorder(self):
195     """ Test 7. Check reordering of points works properly
196     """
197     PRECISION = 10
198     model.do()
199     # use low-level API to create feature
200     curveFitting = featureToCompositeFeature(self.mySketch.feature()).addFeature("SketchCurveFitting")
201     curveFitting.string("type").setValue("approximation_type")
202     curveFitting.real("precision").setValue(PRECISION)
203     curveFitting.boolean("periodic").setValue(False)
204     curveFitting.boolean("closed").setValue(False)
205     pointsAttr = curveFitting.refattrlist("points")
206     for ind in [0, 3, 4, 2, 1]:
207       pointsAttr.append(self.myPointRefs[ind])
208     # perform reordering
209     curveFitting.customAction("reorder_points")
210     model.do()
211
212     self.mySpline = SketchAPI_BSpline(model.lastSubFeature(self.mySketch, "SketchBSpline"))
213     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
214     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
215     self.myDOF += len(curve.poles()) * 2 - 4
216     self.myNbSplines += 1
217     model.do()
218
219     self.assertTrue(self.mySpline.feature())
220     self.assertTrue(self.mySpline.feature().error() == "")
221     self.assertTrue(self.mySpline.degree().value() == 3)
222     self.assertTrue(curve.isPeriodic() == False)
223     EXPECTED_LEN = 166.97400096
224     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
225     # check points lying of the created curve
226     self.checkPointsOnCurve(edge, PRECISION)
227
228
229 if __name__ == "__main__":
230     # Create a sketch with the external points
231     model.begin()
232     document = model.moduleDocument()
233     coordinates = [(50., 50.), (70., 70.), (80., 30.), (50., 10.), (10., -30.)]
234     for p in coordinates:
235       point3D = model.addPoint(document, p[0], p[1], 0)
236       POINT_NAMES.append(point3D.name())
237     model.end()
238
239     # Run testing
240     test_program = unittest.main(exit=False)
241     assert test_program.result.wasSuccessful(), "Test failed"
242     assert model.checkPythonDump()