-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (145 loc) · 7 KB
/
main.py
File metadata and controls
177 lines (145 loc) · 7 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
from manim import *
# Mobject.set_default(color=BLACK)
# config.background_color = WHITE
class HiMo_Static(Scene):
def get_line_intersection(self, p1, p2, p3, p4):
A1 = p2[1] - p1[1]
B1 = p1[0] - p2[0]
C1 = A1 * p1[0] + B1 * p1[1]
A2 = p4[1] - p3[1]
B2 = p3[0] - p4[0]
C2 = A2 * p3[0] + B2 * p3[1]
determinant = A1 * B2 - A2 * B1
if determinant == 0:
return None # The lines are parallel or coincident
x = (B2 * C1 - B1 * C2) / determinant
y = (A1 * C2 - A2 * C1) / determinant
if (
min(p1[0], p2[0]) <= x <= max(p1[0], p2[0])
and min(p1[1], p2[1]) <= y <= max(p1[1], p2[1])
and min(p3[0], p4[0]) <= x <= max(p3[0], p4[0])
and min(p3[1], p4[1]) <= y <= max(p3[1], p4[1])
):
return np.array([x, y, 0])
else:
return None
def lidar_ray(self, lidar, inner_rect_points, start=0, end=360):
rays = []
dots = []
lidar_position = lidar.get_center()
for angle in range(start, end, 10): # Every 10 degrees, a ray is emitted
ray_dir = np.array([np.cos(angle * DEGREES), np.sin(angle * DEGREES), 0])
intersections = []
for i in range(len(inner_rect_points)):
p1 = inner_rect_points[i]
p2 = inner_rect_points[(i + 1) % len(inner_rect_points)]
intersection_point = self.get_line_intersection(lidar_position, lidar_position + 10 * ray_dir, p1, p2)
if intersection_point is not None:
intersections.append(intersection_point)
if intersections:
# Find the closest intersection point
closest_point = min(intersections, key=lambda point: np.linalg.norm(point - lidar_position))
ray = Line(start=lidar_position, end=closest_point, color=YELLOW, buff=0).add_tip(tip_length=0.1).set_opacity(0.5)
dot = Dot(closest_point, color=BLUE)
rays.append(ray)
dots.append(dot)
return rays, dots
def scan_looks(self, rec, lidar, dots, rays, relative_obj):
rec.set_stroke(opacity=0.3)
rec.set_fill(opacity=0)
copy_dots = [dot.copy() for dot in dots]
copy_dots2 = [dot.copy() for dot in dots]
group2 = VGroup(rec, lidar, *copy_dots, *(rays.copy()))
group1 = VGroup(rec.copy(), lidar.copy(), *[dot.scale(1.5) for dot in copy_dots2])
self.play(Create(group2))
self.play(group2.animate.scale(0.3).next_to(relative_obj, DOWN))
self.play(Create(group1))
self.play(group1.animate.scale(0.3).next_to(group2, DOWN))
return group1
def construct(self):
# Create objects
ego_car = Rectangle(width=1, height=2, color=DARK_BROWN)#.scale(0.5)
lidar = Circle(radius=0.1, color=BLUE).move_to(ego_car.get_center()) # .scale(0.5)
rec1 = Rectangle(width=7, height=7)
rec2 = Rectangle(width=6, height=6) # color=BLACK
static_wall = Difference(rec1, rec2, color=GREY, fill_opacity=0.9)
# Simulate the moving ego car and lidar
o_ego_car = ego_car.copy().move_to(ORIGIN)
o_lidar = lidar.copy().move_to(ORIGIN)
o_staticwall = static_wall.copy().move_to(ORIGIN)
o_rec2 = rec2.copy().move_to(ORIGIN)
# Initial Setup
#
# self.add(ego_car)
# self.add(lidar)
text= Text("This is our ego car", font_size = 18)
text.next_to(ego_car, UR)
self.play(Create(ego_car), Write(text))
self.wait(0.5)
self.remove(text)
text = Text("We have a lidar sensor", font_size = 18)
text.next_to(ego_car, RIGHT)
self.play(Write(text), Create(lidar))
self.play(VGroup(ego_car, lidar).animate.scale(0.5))
self.remove(text)
text = Text("Now we add static environment", font_size = 18).next_to(ego_car, UP)
self.play(Write(text))
self.remove(text)
self.add(static_wall)
# # Generate and animate the rays and dots in the static case
text = Text("Static Case", font_size=18).to_corner(UP, buff=0.5)
self.play(Create(text))
rays, dots = self.lidar_ray(lidar, rec2.get_vertices())
for ray, dot in zip(rays, dots):
self.play(Create(ray), Create(dot), run_time=0.05)
copy_dots = [dot.copy() for dot in dots]
copy_rays = [ray.copy() for ray in rays]
group = VGroup(static_wall, ego_car, lidar, *[dot for dot in dots], *[ray for ray in rays])
self.play(group.animate.scale(0.3).to_corner(UL))
self.remove(text)
text = Text("One Scan Point Cloud", font_size=18).to_corner(UP)
self.play(Create(text))
static_ego = self.scan_looks(o_rec2.copy(), o_lidar.copy(), copy_dots, copy_rays, group)
self.remove(text)
m_staticwall = o_staticwall.copy()
moving_group = VGroup(o_ego_car.copy(), o_lidar.copy())
self.add(m_staticwall)
total_rays, total_dots = [], []
moving_raysdots = VGroup(o_rec2.copy().set_stroke(opacity=0.3).set_fill(opacity=0), o_lidar.copy())
moving_dots = VGroup(o_rec2.copy().set_stroke(opacity=0.3).set_fill(opacity=0), o_lidar.copy())
text = Text("If we are moving", font_size=18).to_corner(UP)
self.play(Create(text))
for inter_ in range(0, 360, 30):
rays, dots = self.lidar_ray(moving_group[1], o_rec2.get_vertices(), start=inter_, end=inter_+30)
self.add(moving_group)
tmp_raydots = VGroup()
tmp_dots = VGroup()
for ray, dot in zip(rays, dots):
self.play(Create(ray), Create(dot), run_time=0.05)
total_rays.append(ray)
total_dots.append(dot)
tmp_raydots.add(ray.copy(), dot.copy())
tmp_dots.add(dot.copy().scale(1.5))
lidar_start_position = moving_group[1].get_center()
tmp_raydots.shift(-lidar_start_position)
tmp_dots.shift(-lidar_start_position)
moving_raysdots.add(tmp_raydots)
moving_dots.add(tmp_dots)
moving_group.move_to(UP*inter_/360)
self.wait(0.1)
self.remove(text)
text = Text("One Scan Point Cloud", font_size=18).to_corner(UP)
self.play(Create(text))
moving_group.add(*total_rays, *total_dots, m_staticwall)
self.play(moving_group.animate.scale(0.3).to_corner(UR))
self.play(Create(moving_raysdots))
self.play(moving_raysdots.animate.scale(0.3).next_to(moving_group, DOWN))
self.play(Create(moving_dots))
self.play(moving_dots.animate.scale(0.3).next_to(moving_raysdots, DOWN))
self.remove(text)
# text = Text("Comparison", font_size=18).to_corner(RIGHT)
# self.play(Create(text))
self.play(static_ego.animate.scale(1.8).move_to(UP*2))
self.play(moving_dots.animate.scale(1.8).next_to(static_ego, DOWN))
# self.remove(text)
self.wait(1)