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