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
use crate::geometry::Point;
use crate::geometry::Pose;
use crate::properties::Motion;
use crate::properties::Size;
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct BCar {
pub pose: Pose,
pub size: Size,
pub motion: Motion,
}
impl BCar {
pub fn new(pose: Pose, size: Size, motion: Motion) -> BCar {
BCar {pose, size, motion}
}
pub fn new_xyh(x: f64, y: f64, h: f64) -> BCar {
BCar {
pose: Pose::new(x, y, h),
size: Size::default(),
motion: Motion::default(),
}
}
pub fn default() -> BCar {
BCar {
pose: Pose::default(),
size: Size::default(),
motion: Motion::default(),
}
}
pub fn size(&self) -> Size {
self.size
}
pub fn set_size(&mut self, s: Size) {
self.size = s;
}
pub fn pose(&self) -> Pose {
self.pose
}
pub fn set_pose(&mut self, p: Pose) {
self.pose = p
}
pub fn motion(&self) -> Motion {
self.motion
}
pub fn set_motion(&mut self, m: Motion) {
self.motion = m
}
pub fn drivable(&self, pose: Pose) -> bool {
let pi = std::f64::consts::PI;
let pi2 = pi / 2.0;
let mut a_1 = (pose.y - self.pose.y).atan2(pose.x - self.pose.x);
a_1 -= self.pose.h;
while a_1 < -pi { a_1 += 2.0 * pi; }
while a_1 > pi { a_1 -= 2.0 * pi; }
let mut h_d = pose.h - self.pose.h;
while h_d < -pi { h_d += 2.0 * pi; }
while h_d > pi { h_d -= 2.0 * pi; }
let mut a_2 = 0.0;
let mut zb = self.pose;
if h_d == 0.0 && (a_1 == 0.0 || a_2 == pi || a_2 == -pi) {
return true;
} else if 0.0 < a_1 && a_1 <= pi2 {
zb.rotate(self.ccl(), h_d);
if pose.y == zb.y && pose.x == zb.x {
return true;
}
a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
while a_2 < -pi { a_2 += 2.0 * pi; }
while a_2 > pi { a_2 -= 2.0 * pi; }
if zb.h >= a_2 && a_2 >= self.pose.h {
return true;
}
} else if pi2 < a_1 && a_1 <= pi {
zb.rotate(self.ccl(), h_d);
if pose.y == zb.y && pose.x == zb.x {
return true;
}
a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
a_2 -= pi;
while a_2 < -pi { a_2 += 2.0 * pi; }
while a_2 > pi { a_2 -= 2.0 * pi; }
if self.pose.h >= a_2 && a_2 >= zb.h {
return true;
}
} else if 0.0 > a_1 && a_1 >= -pi2 {
zb.rotate(self.ccr(), h_d);
if pose.y == zb.y && pose.x == zb.x {
return true;
}
a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
while a_2 < -pi { a_2 += 2.0 * pi; }
while a_2 > pi { a_2 -= 2.0 * pi; }
if self.pose.h >= a_2 && a_2 >= zb.h {
return true;
}
} else if -pi2 > a_1 && a_1 >= -pi {
zb.rotate(self.ccr(), h_d);
if pose.y == zb.y && pose.x == zb.x {
return true;
}
a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
a_2 -= pi;
while a_2 < -pi { a_2 += 2.0 * pi; }
while a_2 > pi { a_2 -= 2.0 * pi; }
if zb.h >= a_2 && a_2 >= self.pose.h {
return true;
}
}
false
}
pub fn mtr(&self) -> f64 {
(
(self.size.curb_to_curb / 2.0).powi(2)
- self.size.wheelbase.powi(2)
).sqrt()
- self.size.width / 2.0
}
pub fn ccl(&self) -> Point {
let x = self.pose.x;
let y = self.pose.y;
let h = self.pose.h;
Point::new(
x + self.mtr() * (h + std::f64::consts::PI / 2.0).cos(),
y + self.mtr() * (h + std::f64::consts::PI / 2.0).sin(),
)
}
pub fn ccr(&self) -> Point {
let x = self.pose.x;
let y = self.pose.y;
let h = self.pose.h;
Point::new(
x + self.mtr() * (h - std::f64::consts::PI / 2.0).cos(),
y + self.mtr() * (h - std::f64::consts::PI / 2.0).sin(),
)
}
pub fn cf(&self) -> Point {
let mut x = self.pose.x;
x += self.size.distance_to_front * self.pose.h.cos();
let mut y = self.pose.y;
y += self.size.distance_to_front * self.pose.h.sin();
Point {x, y}
}
pub fn lf(&self) -> Point {
let pi = std::f64::consts::PI;
let mut x = self.pose.x;
x += self.size.width/2.0 * (self.pose.h + pi/2.0).cos();
x += self.size.distance_to_front * self.pose.h.cos();
let mut y = self.pose.y;
y += self.size.width/2.0 * (self.pose.h + pi/2.0).sin();
y += self.size.distance_to_front * self.pose.h.sin();
Point {x, y}
}
pub fn la(&self) -> Point {
let pi = std::f64::consts::PI;
let mut x = self.pose.x;
x += self.size.width/2.0 * (self.pose.h + pi/2.0).cos();
let mut y = self.pose.y;
y += self.size.width/2.0 * (self.pose.h + pi/2.0).sin();
Point {x, y}
}
pub fn lr(&self) -> Point {
let pi = std::f64::consts::PI;
let distance_to_rear = self.size.length - self.size.distance_to_front;
let mut x = self.pose.x;
x += self.size.width/2.0 * (self.pose.h + pi/2.0).cos();
x += -distance_to_rear * self.pose.h.cos();
let mut y = self.pose.y;
y += self.size.width/2.0 * (self.pose.h + pi/2.0).sin();
y += -distance_to_rear * self.pose.h.sin();
Point {x, y}
}
pub fn rr(&self) -> Point {
let pi = std::f64::consts::PI;
let distance_to_rear = self.size.length - self.size.distance_to_front;
let mut x = self.pose.x;
x += self.size.width/2.0 * (self.pose.h - pi/2.0).cos();
x += -distance_to_rear * self.pose.h.cos();
let mut y = self.pose.y;
y += self.size.width/2.0 * (self.pose.h - pi/2.0).sin();
y += -distance_to_rear * self.pose.h.sin();
Point {x, y}
}
pub fn ra(&self) -> Point {
let pi = std::f64::consts::PI;
let mut x = self.pose.x;
x += self.size.width/2.0 * (self.pose.h - pi/2.0).cos();
let mut y = self.pose.y;
y += self.size.width/2.0 * (self.pose.h - pi/2.0).sin();
Point {x, y}
}
pub fn rf(&self) -> Point {
let pi = std::f64::consts::PI;
let mut x = self.pose.x;
x += self.size.width/2.0 * (self.pose.h - pi/2.0).cos();
x += self.size.distance_to_front * self.pose.h.cos();
let mut y = self.pose.y;
y += self.size.width/2.0 * (self.pose.h - pi/2.0).sin();
y += self.size.distance_to_front * self.pose.h.sin();
Point {x, y}
}
}
impl Iterator for BCar {
type Item = BCar;
fn next(&mut self) -> Option<Self::Item> {
self.pose.x += self.motion.speed * self.pose.h.cos();
self.pose.y += self.motion.speed * self.pose.h.sin();
self.pose.h += self.motion.speed
/ self.size.wheelbase
* self.motion.steer.tan();
Some(*self)
}
}