Skip to content

Commit aa5de3e

Browse files
committed
laser simulation is not good..
1 parent 45d6953 commit aa5de3e

File tree

3 files changed

+149
-30
lines changed

3 files changed

+149
-30
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
"""
2+
3+
Object shape recognition with circle fitting
4+
5+
author: Atsushi Sakai (@Atsushi_twi)
6+
7+
"""
8+
9+
10+
import matplotlib.pyplot as plt
11+
import math
12+
import random
13+
import numpy as np
14+
15+
16+
def circle_fitting(x, y):
17+
"""
18+
Circle Fitting with least squared
19+
input: point x-y positions
20+
output cxe x center position
21+
cye y center position
22+
re radius of circle
23+
error: prediction error
24+
"""
25+
26+
sumx = sum(x)
27+
sumy = sum(y)
28+
sumx2 = sum([ix ** 2 for ix in x])
29+
sumy2 = sum([iy ** 2 for iy in y])
30+
sumxy = sum([ix * iy for (ix, iy) in zip(x, y)])
31+
32+
F = np.array([[sumx2, sumxy, sumx],
33+
[sumxy, sumy2, sumy],
34+
[sumx, sumy, len(x)]])
35+
36+
G = np.array([[-sum([ix ** 3 + ix * iy ** 2 for (ix, iy) in zip(x, y)])],
37+
[-sum([ix ** 2 * iy + iy ** 3 for (ix, iy) in zip(x, y)])],
38+
[-sum([ix ** 2 + iy ** 2 for (ix, iy) in zip(x, y)])]])
39+
40+
# try:
41+
T = np.linalg.inv(F).dot(G)
42+
# except:
43+
# return (0, 0, float("inf"))
44+
45+
cxe = float(T[0] / -2)
46+
cye = float(T[1] / -2)
47+
# print (cxe,cye,T)
48+
# try:
49+
re = math.sqrt(cxe**2 + cye**2 - T[2])
50+
# except:
51+
# return (cxe, cye, float("inf"))
52+
53+
error = sum([np.hypot(cxe - ix, cye - iy) - re for (ix, iy) in zip(x, y)])
54+
# print(error)
55+
56+
return (cxe, cye, re, error)
57+
58+
59+
def get_sample_points(cx, cy, r, angle_reso):
60+
x, y, angle, ran = [], [], [], []
61+
62+
for theta in np.arange(0.0, 2.0 * math.pi, angle_reso):
63+
rn = r * random.uniform(1.0, 1.0)
64+
nx = cx + rn * math.cos(theta)
65+
ny = cy + rn * math.sin(theta)
66+
nangle = math.atan2(ny, nx)
67+
nr = math.hypot(nx, ny)
68+
69+
occluded = False
70+
for i in range(len(angle)):
71+
if abs(angle[i] - nangle) <= angle_reso:
72+
if nr >= ran[i]:
73+
occluded = True
74+
break
75+
76+
if not occluded:
77+
x.append(nx)
78+
y.append(ny)
79+
angle.append(nangle)
80+
ran.append(nr)
81+
82+
return x, y
83+
84+
85+
def plot_circle(x, y, size, color="-b"):
86+
deg = list(range(0, 360, 5))
87+
deg.append(0)
88+
xl = [x + size * math.cos(math.radians(d)) for d in deg]
89+
yl = [y + size * math.sin(math.radians(d)) for d in deg]
90+
plt.plot(xl, yl, color)
91+
92+
93+
def main1():
94+
print(__file__ + " start!!")
95+
96+
tcx = 1.0
97+
tcy = 2.0
98+
tr = 3.0
99+
np = 10
100+
101+
x, y = get_sample_points(tcx, tcy, tr, np)
102+
103+
cx, cy, r, error = circle_fitting(x, y)
104+
print("Error:", error)
105+
106+
plot_circle(tcx, tcy, tr)
107+
plot_circle(cx, cy, r, color="-xr")
108+
plt.plot(x, y, "gx")
109+
110+
plt.axis("equal")
111+
plt.show()
112+
113+
114+
def main():
115+
116+
time = 0.0
117+
simtime = 10.0
118+
dt = 1.0
119+
120+
cx = -3.0
121+
cy = -5.0
122+
theta = math.radians(30.0)
123+
124+
cr = 1.0
125+
angle_reso = math.radians(30.0)
126+
127+
while time <= simtime:
128+
time += dt
129+
130+
cx += math.cos(theta)
131+
cy += math.cos(theta)
132+
133+
x, y = get_sample_points(cx, cy, cr, angle_reso)
134+
135+
ex, ey, er, error = circle_fitting(x, y)
136+
print("Error:", error)
137+
138+
plt.cla()
139+
plt.axis("equal")
140+
plt.plot(0.0, 0.0, "*r")
141+
plot_circle(cx, cy, cr)
142+
plt.plot(x, y, "xr")
143+
plot_circle(ex, ey, er, "-r")
144+
plt.pause(dt)
145+
146+
147+
if __name__ == '__main__':
148+
# main1()
149+
main()

Mapping/object_shape_recognition/convex_circle_fitting.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

Mapping/object_shape_recognition/object_shape_recognition.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)