Salome HOME
15465e0c24c8c60bc68b5258fe931519644c6f07
[modules/geom.git] / test / test_CR.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import salome
22 ###
23 salome.salome_init()
24
25 import GEOM
26 from salome.geom import geomBuilder
27 import math
28 import SALOMEDS
29
30 geompy = geomBuilder.New()
31
32 import unittest
33
34 def GetShapeType(theShape):
35     CR = geompy.CanonicalRecognition()
36     if CR.isLine(theShape, 0.1)[0]:
37         return "Line"
38     if CR.isPlane(theShape, 0.1)[0]:
39         if CR.isCircle(theShape, 0.1)[0]:
40             return ("Plane","Circle")
41         if CR.isEllipse(theShape, 0.1)[0]:
42             return ("Plane","Ellipse")
43         return "Plane"
44     if CR.isSphere(theShape, 0.1)[0]:
45         return "Sphere"
46     if CR.isCone(theShape, 0.1)[0]:
47         return "Cone"
48     if CR.isCylinder(theShape, 0.1)[0]:
49         return "Cylinder"
50     return "Not defined"
51
52 class GEOMTestCR(unittest.TestCase):
53     def testBasic(self):
54         """
55         Acceptance test for tuleap29469
56         """
57         O = geompy.MakeVertex(0, 0, 0)
58         OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
59         OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
60         OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
61         Cylinder_1 = geompy.MakeCylinderRH(100, 300)
62         [Face_1,Face_2,Face_3] = geompy.ExtractShapes(Cylinder_1, geompy.ShapeType["FACE"], True)
63         [Edge_1,Edge_2,Edge_3] = geompy.ExtractShapes(Cylinder_1, geompy.ShapeType["EDGE"], True)
64         Sphere_1 = geompy.MakeSphereR(100)
65         [Shell_1] = geompy.ExtractShapes(Sphere_1, geompy.ShapeType["SHELL"], True)
66         Cone_1 = geompy.MakeConeR1R2H(100, 0, 300)
67         [Shell_2] = geompy.ExtractShapes(Cone_1, geompy.ShapeType["SHELL"], True)
68         [Face_4,Face_5] = geompy.ExtractShapes(Shell_2, geompy.ShapeType["FACE"], True)
69
70         ## Create ellips
71         Cylinder_2 = geompy.MakeCylinderRH(100, 300)
72         Plane_1 = geompy.MakePlaneLCS(None, 350, 1)
73         Translation_1 = geompy.MakeTranslation(Plane_1, 0, 0, 150)
74         Rotation_1 = geompy.MakeRotation(Translation_1, OX, 20*math.pi/180.0)
75         Partition_1 = geompy.MakePartition([Cylinder_2], [Rotation_1], [], [], geompy.ShapeType["SOLID"], 0, [], 0)
76         [Edge_4,Edge_5,Edge_6,Edge_7,Edge_8] = geompy.ExtractShapes(Partition_1, geompy.ShapeType["EDGE"], True)
77
78         assert GetShapeType(Face_1) == "Plane"
79         assert GetShapeType(Face_2) == "Cylinder"
80         assert GetShapeType(Edge_3) == "Line"
81         assert GetShapeType(Edge_2)[1] == "Circle"
82         assert GetShapeType(Shell_1) == "Sphere"
83         assert GetShapeType(Shell_2) == "Cone"
84         assert GetShapeType(Edge_5)[1] == "Ellipse"
85         
86     def testOnSetOfPtsOnCyl(self):
87         import numpy as np
88         tab = [[49.9999999999997, 86.6025403784442, 0.0], [1.43524931876432e-12, 100.0, 0.0], [6.12323399573677e-15, 100.0, 300.0], [50.0, 86.6025403784439, 300.0], [34.20201433256674, 93.96926207859089, 300.0], [17.364817766692976, 98.48077530122082, 300.0], [0.0, 100.0, 19.999999999999854], [0.0, 100.0, 39.99999999999963], [0.0, 100.0, 59.99999999999965], [0.0, 100.0, 79.99999999999966], [0.0, 100.0, 99.9999999999997], [0.0, 100.0, 119.99999999999973], [0.0, 100.0, 139.99999999999974], [0.0, 100.0, 159.99999999999977], [0.0, 100.0, 179.9999999999998], [0.0, 100.0, 199.99999999999983], [0.0, 100.0, 219.9999999999999], [0.0, 100.0, 239.99999999999994], [0.0, 100.0, 259.9999999999999], [0.0, 100.0, 279.99999999999994], [34.20201433256736, 93.96926207859066, 0.0], [17.364817766694355, 98.48077530122058, 0.0], [50.0, 86.60254037844386, 19.999999999999854], [50.0, 86.60254037844386, 39.99999999999963], [50.0, 86.60254037844386, 59.99999999999965], [50.0, 86.60254037844386, 79.99999999999966], [50.0, 86.60254037844386, 99.9999999999997], [50.0, 86.60254037844386, 119.99999999999973], [50.0, 86.60254037844386, 139.99999999999974], [50.0, 86.60254037844386, 159.99999999999977], [50.0, 86.60254037844386, 179.9999999999998], [50.0, 86.60254037844386, 199.99999999999983], [50.0, 86.60254037844386, 219.9999999999999], [50.0, 86.60254037844386, 239.99999999999994], [50.0, 86.60254037844386, 259.9999999999999], [50.0, 86.60254037844386, 279.99999999999994], [29.61877316560949, 95.5129743865417, 277.8870679979398], [17.205660214608802, 98.50870650140234, 30.630206624576903], [16.710324551353793, 98.59394024679419, 72.64745394595525], [17.100883188710984, 98.52694958317781, 107.80372339391727], [17.635764230289126, 98.4326156312716, 152.57612984579256], [13.644784679867689, 99.06472556384563, 189.91477232835643], [17.211909165765633, 98.50761484712454, 226.50789445170153], [16.947190833941324, 98.55350182940215, 264.31714685564623], [33.70205726794775, 94.14972828377132, 23.514274138894333], [33.9866992950552, 94.04735121749837, 45.6467424308921], [33.80199583830124, 94.11389417799832, 66.95510896864648], [31.934324162661458, 94.76391159230425, 89.88408369489096], [34.465261348238855, 93.8730299936973, 112.129816095113], [37.584878577685345, 92.66810078069287, 129.81302524607864], [34.76442821196357, 93.76264997905739, 147.223574108196], [33.921830158958535, 94.07076824745704, 167.36545102566686], [31.81206857237576, 94.80502251013108, 189.95083125203968], [33.807919537982364, 94.11176640842159, 212.57124554127284], [34.08001408422917, 94.01357689195078, 233.1435048617797], [33.7912654017101, 94.11774743666143, 253.72564508639465], [17.227853600937443, 98.50482759897946, 52.4555873421884], [13.783376774516714, 99.04553763139313, 90.1191962222661], [19.72881682971234, 98.03455404345786, 129.98468730013374], [16.735452147700688, 98.58967816872119, 172.6689841537882], [16.664342186017773, 98.60172259908703, 207.05379129229303], [17.201463406704146, 98.50943942926392, 246.03943188659505]]
89
90         vtx = [ geompy.MakeVertex(x,y,z) for x,y,z in tab]
91
92         theShape = geompy.MakeSmoothingSurface( vtx, 2, 8, 0)
93         CR = geompy.CanonicalRecognition()
94         recog,axis,pt,radius = CR.isCylinder(theShape, 0.1)
95         self.assertTrue( recog )
96         self.assertTrue( np.isclose(100,radius,rtol=1e-2, atol = 0) )
97         axisA = np.array(axis).reshape(1,3)
98         self.assertTrue( np.isclose( np.linalg.norm( axisA ) , 1.0 , rtol=1e-10, atol = 0 ) )
99         self.assertTrue( np.isclose( np.abs( np.dot( axisA.reshape(3), np.array([0,0,1]) ) ), 1.0 , rtol=1e-7, atol = 0 ) )
100         relDistOfPtToIdealAxis = np.linalg.norm( np.cross( axisA.reshape(3), np.array(pt) )/np.linalg.norm( np.array(pt) ) )
101         self.assertTrue( np.isclose( relDistOfPtToIdealAxis, 0.0, rtol = 0, atol = 1e-3 ) )
102
103 if __name__ == '__main__':
104     unittest.main()