-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathel_geometry.py
More file actions
410 lines (311 loc) · 9.63 KB
/
el_geometry.py
File metadata and controls
410 lines (311 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""
The following functions execute elementary geometrical calculations.
"""
from math import sin, cos ,sqrt, fabs, atan, tan
def get_line_point_nearest2point(p,theta,c):
"""
This Function calculates the distance from a line to a point.
Input:
- p [float,float] : a point p=[x,y].
- theta float : angle defined by line angular coefficient.
- c float : linear coefficient.
Output:
- [float,flot] : The coordinates of the nearest point in the line to the point p.
"""
if(theta!=2*atan(1)) and (theta!=0.):
a=tan(theta)
m=-1./a
b=p[1]-m*p[0]
x=(c-b)/(m-a)
y=a*x+c
dx=p[0]-x
dy=p[1]-y
return x,y
elif (theta==2*atan(1)): # case of the perpendicular bisector is a vertical line a-->inf.
dx=fabs(p[0]-c)
return c,p[1]
elif theta==0.: # case of the perpendicular bisector is a horizontal line a-->0
dy=fabs(p[1]-c)
return p[0],c
def two_points_to_line(p1,p2):
"""
Function that calculates the line defined by two points p_i=[x_i,y_i].
Input:
- p1 [float,float] : first point.
- p2 [float,float] : second point.
Output:
- float : angle defined by angular coefficient.
- float : linear coefficient.
"""
x1=p1[0]
y1=p1[1]
x2=p2[0]
y2=p2[1]
Dy=float(y2-y1)
Dx=float(x2-x1)
b1=0
My=0
Mx=0
if (fabs(Dx)>0):
a=(Dy)/(Dx)
theta=atan(a)
b1=y1-a*x1
elif Dx==0:
a=0
theta=2*atan(1)
b1=x1
return theta,b1
def get_length_c(points):
"""
Function to calculate the length in a path determined by several points.
Input:
- points <list> : list of p_i points where p_i=x_i+J*y_i.
Output:
- <float> : the length.
"""
L=0.
err=0
for j in range(0,len(points)-1):
dl=abs(points[j]-points[j+1])
L=L+dl
return L
def get_extrema_2loops( ximg, yimg, ref_position ):
"""
Function to determine the two extrema of a set of points through a reference point.
The first extreme point (P1) is found by searching for the point furthest from the reference
point (usually is some definition of center). The second extreme point is the furthest one from
point P1.
Input:
- ximg <list> : x coordinates of the image
- yimg <list> : y coordinates of the image
- ref_position <int> : the (Python) position of the reference point in the ximg and yimg lists
Output:
- <int> : position in the lists ximg and yimg of one extreme point
- <int> : position in the lists ximg and yimg of the other extreme point
"""
# define the reference point coordinates
x_ref = ximg[ref_position]
y_ref = yimg[ref_position]
# find the furthest point from the reference point (1st extreme)
furthest_pt1 = 0
distmax = -1
for i in range(0,len(ximg)): # loops over all points
if (ximg[i] - x_ref)**2 + (yimg[i] - y_ref)**2 > distmax:
distmax = (ximg[i] - x_ref)**2 + (yimg[i] - y_ref)**2
furthest_pt1 = i
# find the furthest point from the first extreme (2nd extreme)
furthest_pt2 = 0
distmax = -1
for j in range(0,len(ximg)): # loops over all points
if (ximg[j] - ximg[furthest_pt1])**2 + (yimg[j] - yimg[furthest_pt1])**2 > distmax:
distmax = (ximg[j] - ximg[furthest_pt1])**2 + (yimg[j] - yimg[furthest_pt1])**2
furthest_pt2 = j
return furthest_pt1,furthest_pt2;
def three_points_to_circle(p1, p2, p3):
"""
Function that calculates circle parameters defined by three points.
Input:
- p1 [float,float] : pair (x,y) of coordinates of the 1st point
- p2 [float,float] : pair (x,y) of coordinates of the 2nd point
- p3 [float,float] : pair (x,y) of coordinates of the 3rd point
Output:
- float : center x coordinate
- float : center y coordinate
- float : circle radius
"""
theta1,b1=define_perpendicular_bisector(p1,p2)
theta2,b2=define_perpendicular_bisector(p2,p3)
xc,yc,Flag=get_intercept_point(theta1,b1,theta2,b2)
if Flag==1:
return 0,0,0
r=((p1[0]-xc)*(p1[0]-xc))+((p1[1]-yc)*(p1[1]-yc))
r=sqrt(r)
return xc,yc,r
def get_intercept_point(theta,b_t,phi,b_p):
"""
Function that calculates the interception point from two straight lines.
Input:
- theta float : angle defined by angular coefficient from first line.
- b_t float : linear coeficient from the first line.
- phi float : angle defined by angular coefficient from second line.
- b_p float : linear coeficient from the second line.
Output:
- float : intercept x coordinate.
- float : intercept y coordinate.
- int : a Flag. if Flag= 1 the lines are parallels, otherwise Flag=0,
"""
a_t=tan(theta)
a_p=tan(phi)
Flag=0
if(fabs(a_t-a_p)<0.001):
print "parallels found"
Flag=1
xi=0
yi=0
elif (theta==2*atan(1) and phi==0.):
xi=b_t
yi=b_p
elif (theta==0. and phi==2*atan(1)):
xi=b_p
yi=b_t
elif theta==2*atan(1):
xi=b_t
yi=a_p*xi+b_p
elif phi==2*atan(1):
xi=b_p
yi=a_t*xi+b_t
elif theta==0:
yi=b_t
xi=(yi-b_p)/a_p
elif phi==0:
yi=b_p
xi=(yi-b_t)/a_t
else:
xi=(b_p-b_t)/(a_t-a_p)
yi=a_t*xi+b_t
return xi,yi,Flag
def define_perpendicular_bisector(p1,p2):
"""
Function that calculates the perpendicular bisector line between two points
p_i=[x_i,y_i].
Input:
- p1 [float,float] : first point.
- p2 [float,float] : second point.
Output:
- float : angle defined by angular coefficient.
- float : linear coefficient.
"""
x1=p1[0]
y1=p1[1]
x2=p2[0]
y2=p2[1]
Dy=float(y2-y1)
Dx=float(x2-x1)
b1=0
My=0
Mx=0
if (fabs(Dy)>0):
m=-(Dx)/(Dy)
theta=atan(m)
My=float(y2+y1)/2.
Mx=float(x2+x1)/2.
b1=My-m*Mx
elif Dy==0:
m=0
theta=2*atan(1)
b1=float(x2+x1)/2.
return theta,b1
def two_points_to_line(p1,p2):
"""
Function that calculates the line defined by two points p_i=[x_i,y_i].
Input:
- p1 [float,float] : first point.
- p2 [float,float] : second point.
Output:
- float : angle defined by angular coefficient.
- float : linear coefficient.
"""
x1=p1[0]
y1=p1[1]
x2=p2[0]
y2=p2[1]
Dy=float(y2-y1)
Dx=float(x2-x1)
b1=0
My=0
Mx=0
if (fabs(Dx)>0):
a=(Dy)/(Dx)
theta=atan(a)
b1=y1-a*x1
elif Dx==0:
a=0
theta=2*atan(1)
b1=x1
return theta,b1
def get_distance_from_line_to_point(p,theta,c):
"""
This Function calculates the distance from a line to a point.
Input:
- p [float,float] : a point p=[x,y].
- theta float : angle defined by line angular coefficient.
- c float : linear coefficient.
Output:
- float : The distance from the line to the point p.
"""
if(theta!=2*atan(1)) and (theta!=0.):
a=tan(theta)
m=-1./a
b=p[1]-m*p[0]
x=(c-b)/(m-a)
y=a*x+c
dx=p[0]-x
dy=p[1]-y
Distance=sqrt(dx*dx + dy*dy)
elif (theta==2*atan(1)): # case of the perpendicular bisector is a vertical line a-->inf.
dx=fabs(p[0]-c)
Distance=dx
elif theta==0.: # case of the perpendicular bisector is a horizontal line a-->0
dy=fabs(p[1]-c)
Distance=dy
return Distance
def get_line_point_nearest2point(p,theta,c):
"""
This Function calculates the distance from a line to a point.
Input:
- p [float,float] : a point p=[x,y].
- theta float : angle defined by line angular coefficient.
- c float : linear coefficient.
Output:
- [float,flot] : The coordinates of the nearest point in the line to the point p.
"""
if(theta!=2*atan(1)) and (theta!=0.):
a=tan(theta)
m=-1./a
b=p[1]-m*p[0]
x=(c-b)/(m-a)
y=a*x+c
dx=p[0]-x
dy=p[1]-y
return x,y
elif (theta==2*atan(1)): # case of the perpendicular bisector is a vertical line a-->inf.
dx=fabs(p[0]-c)
return c,p[1]
elif theta==0.: # case of the perpendicular bisector is a horizontal line a-->0
dy=fabs(p[1]-c)
return p[0],c
def width_ellipse(l,area):
"""
Function to calculate wiidth of an ellipse of given area and length l.
Input:
- l float : the ellipse length.
- area float : the ellipse area.
Output:
- float : the width.
"""
w=area/(atan(1)*l)
return w
def length_from_connected_dots(points):
"""
Function to calculate the sum of distance from a previous point to the next on
a list of points.
Input:
- points [[float,float],...] : list of p_i points where p_i=(x_i,y_i).
Output:
- float : the length.
"""
x=[]
y=[]
for i in range(0,len(points)):
x.append(points[i][0])
y.append(points[i][1])
l=0
err=0
for j in range(0,len(X)-1):
dx=x[j]-x[j+1]
dy=x[j]-y[j+1]
dx=dx*dx
dy=dy*dy
dl=sqrt(dx+dy)
l=l+dl
return float(l)