Salome HOME
bos #29468: Advanced geometry features: distance Edge-Edge & Face-Face
[modules/geom.git] / test / test_proximity_edge_edge.py
1 # Shape Proximity between edges
2
3 import math
4 import salome
5 salome.salome_init_without_session()
6 import GEOM
7 from salome.geom import geomBuilder
8 geompy = geomBuilder.New()
9
10 O = geompy.MakeVertex(0, 0, 0)
11 OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
12 OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
13 OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
14
15 # Case 1: two bezier curves (original Cas2_29468.py)
16 from math import sqrt
17
18 # 283x384
19 szY = 384
20 listOfPtsRed_gimp = [(10,84), (54,96),(145,146),(167,167),(185,212),(187,234),(176,302)]
21 listOfPtsBlue_gimp = [(120,72),(170,87),(227,118),(238,126),(243,157),(203,216),(134,281),(94,324)]
22 #
23 listOfPtsRed = [(x,szY-y) for x,y in listOfPtsRed_gimp]
24 listOfPtsBlue = [(x,szY-y) for x,y in listOfPtsBlue_gimp]
25 #
26 verticesRed = [geompy.MakeVertex(x,y,0) for x,y in listOfPtsRed]
27 verticesBlue = [geompy.MakeVertex(x,y,0) for x,y in listOfPtsBlue]
28 for i,(x,y) in enumerate(listOfPtsRed):
29     geompy.addToStudy(geompy.MakeVertex(x,y,0),"red_pt{}".format(i))
30 for i,(x,y) in enumerate(listOfPtsBlue):
31     geompy.addToStudy(geompy.MakeVertex(x,y,0),"blue_pt{}".format(i))
32 redEdge = geompy.MakeBezier(verticesRed)
33 blueEdge = geompy.MakeBezier(verticesBlue)
34 #
35 geompy.addToStudy(redEdge,"red")
36 geompy.addToStudy(blueEdge,"blue")
37
38 XY_red = (152,214)
39 XY_blue = (215,260)
40 exp_red = geompy.MakeVertex(*XY_red,0)
41 exp_blue = geompy.MakeVertex(*XY_blue,0)
42 geompy.addToStudy(exp_red,"exp_red")
43 geompy.addToStudy(exp_blue,"exp_blue")
44
45 p = geompy.ShapeProximity()
46 p.setShapes(redEdge, blueEdge)
47 p.setSampling(redEdge, 1000)
48 p.setSampling(blueEdge, 1000)
49 p_coarse  = p.coarseProximity()
50 p_precise = p.preciseProximity()
51 print( "coarse = {} ; fine = {}".format(p_coarse,p_precise) )
52 print( "Manually obtained value = {}".format( sqrt( (XY_red[0]-XY_blue[0])**2 + (XY_red[1]-XY_blue[1])**2 ) ) )
53
54 assert(math.fabs(p_coarse - 223.00892775) < 1.e-7)
55
56 prev = geompy.ShapeProximity()
57 prev.setShapes(blueEdge, redEdge)
58 prev.setSampling(redEdge, 1000)
59 prev.setSampling(blueEdge, 1000)
60 p_coarse  = prev.coarseProximity()
61 p_precise = prev.preciseProximity()
62 print( "coarse = {} ; fine = {}".format(p_coarse,p_precise) )
63
64 assert(math.fabs(p_coarse - 84.89994110) < 1.e-7)
65
66 # Case 2: two bezier curves (different coarse and fine proximities)
67 V1 = geompy.MakeVertex(10, 10, 0)
68 V2 = geompy.MakeVertex(20, -10, 0)
69 V3 = geompy.MakeVertex(30, 0, 0)
70 V4 = geompy.MakeVertex(0, -3, 0)
71 V5 = geompy.MakeVertex(13, -10, 0)
72 V6 = geompy.MakeVertex(25, 10, 0)
73 V7 = geompy.MakeVertex(30, 5, 0)
74 BC1 = geompy.MakeBezier([ O, V1, V2, V3], False, "BC1")
75 BC2 = geompy.MakeBezier([V4, V5, V6, V7], False, "BC2")
76
77 pcalc = geompy.ShapeProximity()
78 pcalc.setShapes(BC1, BC2)
79 p_coarse = pcalc.coarseProximity()
80 p_fine = pcalc.preciseProximity()
81
82 assert(math.fabs(p_coarse - 7.3126564) < 1.e-7)
83 assert(math.fabs(p_fine - 7.380468495) < 1.e-7)
84
85 # Case 3: arc and segment
86 Vertex_1 = geompy.MakeVertex(0, 0, -1)
87 Vertex_2 = geompy.MakeVertex(1, 0, 0)
88 Vertex_3 = geompy.MakeVertex(0, 0, 1)
89 Arc_1 = geompy.MakeArc(Vertex_1, Vertex_2, Vertex_3)
90 Arc_1_vertex_2 = geompy.GetSubShape(Arc_1, [2])
91 Edge_1 = geompy.MakeEdgeOnCurveByLength(Arc_1, 3, Arc_1_vertex_2)
92 Edge_2 = geompy.MakeEdge(Vertex_1, Vertex_3)
93
94 shape1 = Edge_1
95 shape2 = Edge_2
96
97 # perform proximity calculation with the default parameters
98 p1 = geompy.ShapeProximity()
99 proximity1 = p1.proximity(shape1, shape2)
100
101 # perform proximity calculation with custom parameters
102 p2 = geompy.ShapeProximity()
103 p2.setShapes(shape1, shape2)
104 p2.setSampling(shape1, 100) # number of sample points for the first shape
105 p2.setSampling(shape2, 40)  # number of sample points for the second shape
106 proximity2_coarse = p2.coarseProximity()
107 proximity2_fine = p2.preciseProximity()
108
109 assert(math.fabs(proximity1 - proximity2_fine) < 1.e-7)
110 assert(math.fabs(proximity2_coarse - 0.99998769) < 1.e-7)
111 assert(math.fabs(proximity2_fine - 1) < 1.e-7)
112
113 # move second edge and check proximity
114 Translation_1 = geompy.MakeTranslation(Edge_2, 0.3, 0, 0)
115 shape2 = Translation_1
116
117 # perform proximity calculation with the default parameters
118 p1 = geompy.ShapeProximity()
119 proximity1 = p1.proximity(shape1, shape2)
120
121 # perform proximity calculation with custom parameters
122 p2 = geompy.ShapeProximity()
123 p2.setShapes(shape1, shape2)
124 p2.setSampling(shape1, 100) # number of sample points for the first shape
125 p2.setSampling(shape2, 40)  # number of sample points for the second shape
126 proximity2_coarse = p2.coarseProximity()
127 proximity2_fine = p2.preciseProximity()
128
129 assert(math.fabs(proximity1 - 0.7) < 1.e-7)
130 assert(math.fabs(proximity2_fine - 0.7) < 1.e-7)