Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestCurveFitting1.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-27"
36
37 TOLERANCE = 1.e-7
38
39 class TestInterpolation(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):
69     for p in self.myPoints:
70       shape = p.defaultResult().shape()
71       self.assertTrue(shapetools.minimalDistance(shape, theCurve) < TOLERANCE)
72
73
74   def test_interpolation(self):
75     """ Test 1. Create B-spline curve by set of points
76     """
77     self.mySpline = self.mySketch.addInterpolation(self.myPointRefs)
78     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
79     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
80     self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs) - 2
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() == 3)
87     self.assertTrue(curve.isPeriodic() == False)
88     EXPECTED_LEN = 172.237458
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_interpolation_periodic(self):
94     """ Test 2. Create periodic B-spline curve by set of points
95     """
96     self.mySpline = self.mySketch.addInterpolation(self.myPointRefs, periodic = True)
97     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
98     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
99     self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs)
100     self.myNbPeriodicSplines += 1
101     model.do()
102
103     self.assertTrue(self.mySpline.feature())
104     self.assertTrue(self.mySpline.feature().error() == "")
105     self.assertTrue(self.mySpline.degree().value() == 3)
106     self.assertTrue(curve.isPeriodic() == True)
107     EXPECTED_LEN = 262.60929483
108     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
109     # check points lying of the created curve
110     self.checkPointsOnCurve(edge)
111
112   def test_interpolation_closed(self):
113     """ Test 3. Create closed but not periodic B-spline curve by set of points
114     """
115     self.mySpline = self.mySketch.addInterpolation(self.myPointRefs, closed = True)
116     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
117     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
118     self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs) - 3
119     self.myNbSplines += 1
120     model.do()
121
122     self.assertTrue(self.mySpline.feature())
123     self.assertTrue(self.mySpline.feature().error() == "")
124     self.assertTrue(self.mySpline.degree().value() == 3)
125     self.assertTrue(curve.isPeriodic() == False)
126     self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
127     EXPECTED_LEN = 274.25674928
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_interpolation_reorder(self):
133     """ Test 4. Check reordering of points works properly
134     """
135     model.do()
136     # use low-level API to create feature
137     curveFitting = featureToCompositeFeature(self.mySketch.feature()).addFeature("SketchCurveFitting")
138     curveFitting.string("type").setValue("interpolation_type")
139     curveFitting.boolean("periodic").setValue(False)
140     curveFitting.boolean("closed").setValue(False)
141     pointsAttr = curveFitting.refattrlist("points")
142     for ind in [0, 3, 4, 2, 1]:
143       pointsAttr.append(self.myPointRefs[ind])
144     # perform reordering
145     curveFitting.customAction("reorder_points")
146     model.do()
147
148     self.mySpline = SketchAPI_BSpline(model.lastSubFeature(self.mySketch, "SketchBSpline"))
149     edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
150     curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
151     self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs) - 2
152     self.myNbSplines += 1
153     model.do()
154
155     self.assertTrue(self.mySpline.feature())
156     self.assertTrue(self.mySpline.feature().error() == "")
157     self.assertTrue(self.mySpline.degree().value() == 3)
158     self.assertTrue(curve.isPeriodic() == False)
159     EXPECTED_LEN = 172.237458
160     self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
161     # check points lying of the created curve
162     self.checkPointsOnCurve(edge)
163
164
165 if __name__ == "__main__":
166     test_program = unittest.main(exit=False)
167     assert test_program.result.wasSuccessful(), "Test failed"
168     assert model.checkPythonDump()