2 """Generate simple JSON scenario.
4 The scenario contains at least:
6 - `init` -- the init car position,
8 - `slot` -- the parking slot,
10 - `obst` -- the list of (convex polygon) obstacles.
12 from json import dumps
13 from math import cos, pi, sin
14 from random import random, uniform
21 MTR = ((CTC / 2)**2 - WB**2)**0.5 - W / 2
30 """Generate car init position."""
31 # TODO if changed, change ``gen_slot`` accordingly
35 def gen_slot_at(x, y, h, parallel=True):
36 """Generate slot at specified coordinates."""
46 x + length/2 * cos(h - pi/2 * right),
47 y + length/2 * sin(h - pi/2 * right),
50 x + width * cos(h) + length/2 * cos(h - pi/2 * right),
51 y + width * sin(h) + length/2 * sin(h - pi/2 * right),
54 x + width * cos(h) + length/2 * cos(h + pi/2 * right),
55 y + width * sin(h) + length/2 * sin(h + pi/2 * right),
58 x + length/2 * cos(h + pi/2 * right),
59 y + length/2 * sin(h + pi/2 * right),
65 x + width/2 * cos(h - pi/2 * right),
66 y + width/2 * sin(h - pi/2 * right),
69 x + length * cos(h) + width/2 * cos(h - pi/2 * right),
70 y + length * sin(h) + width/2 * sin(h - pi/2 * right),
73 x + length * cos(h) + width/2 * cos(h + pi/2 * right),
74 y + length * sin(h) + width/2 * sin(h + pi/2 * right),
77 x + width/2 * cos(h + pi/2 * right),
78 y + width/2 * sin(h + pi/2 * right),
83 def gen_slot(l=5.3, w=2.4):
84 """Generate parking slot."""
85 parallel = True if random() < 0.5 else False
86 if parallel and l == 5.3 and w == 2.4:
93 right = 1.0 if random() < 0.5 else -1.0
95 angl = uniform(0, 2 * pi)
98 h = uniform(0, 2 * pi)
102 x + l/2 * cos(h - pi/2 * right),
103 y + l/2 * sin(h - pi/2 * right),
106 x + w * cos(h) + l/2 * cos(h - pi/2 * right),
107 y + w * sin(h) + l/2 * sin(h - pi/2 * right),
110 x + w * cos(h) + l/2 * cos(h + pi/2 * right),
111 y + w * sin(h) + l/2 * sin(h + pi/2 * right),
114 x + l/2 * cos(h + pi/2 * right),
115 y + l/2 * sin(h + pi/2 * right),
121 x + w/2 * cos(h - pi/2 * right),
122 y + w/2 * sin(h - pi/2 * right),
125 x + l * cos(h) + w/2 * cos(h - pi/2 * right),
126 y + l * sin(h) + w/2 * sin(h - pi/2 * right),
129 x + l * cos(h) + w/2 * cos(h + pi/2 * right),
130 y + l * sin(h) + w/2 * sin(h + pi/2 * right),
133 x + w/2 * cos(h + pi/2 * right),
134 y + w/2 * sin(h + pi/2 * right),
139 def gen_obst_at(x, y, h):
140 """Generate obstacle at specific coordinates."""
145 x + width/2 * cos(h - pi/2) + length/2 * cos(h),
146 y + width/2 * sin(h - pi/2) + length/2 * sin(h),
149 x + width/2 * cos(h - pi/2) - length/2 * cos(h),
150 y + width/2 * sin(h - pi/2) - length/2 * sin(h),
153 x + width/2 * cos(h + pi/2) - length/2 * cos(h),
154 y + width/2 * sin(h + pi/2) - length/2 * sin(h),
157 x + width/2 * cos(h + pi/2) + length/2 * cos(h),
158 y + width/2 * sin(h + pi/2) + length/2 * sin(h),
161 x + width/2 * cos(h - pi/2) + length/2 * cos(h),
162 y + width/2 * sin(h - pi/2) + length/2 * sin(h),
167 """Generate obstacles array."""
169 min_r = ((W/2)**2 + ((WB+L)/2)**2)**0.5 + (OBST_W**2 + OBST_L**2)**0.5 / 2
170 for i in range(OBST_COUNT):
173 angl = uniform(0, 2 * pi)
174 r = uniform(min_r**2, (SLOT_RADI - 5)**2)**0.5
177 h = uniform(0, 2 * pi)
180 x + w/2 * cos(h - pi/2) + l/2 * cos(h),
181 y + w/2 * sin(h - pi/2) + l/2 * sin(h),
184 x + w/2 * cos(h - pi/2) - l/2 * cos(h),
185 y + w/2 * sin(h - pi/2) - l/2 * sin(h),
188 x + w/2 * cos(h + pi/2) - l/2 * cos(h),
189 y + w/2 * sin(h + pi/2) - l/2 * sin(h),
192 x + w/2 * cos(h + pi/2) + l/2 * cos(h),
193 y + w/2 * sin(h + pi/2) + l/2 * sin(h),
196 x + w/2 * cos(h - pi/2) + l/2 * cos(h),
197 y + w/2 * sin(h - pi/2) + l/2 * sin(h),
206 "slot": gen_slot_at(15, 0, 0, True),
207 "obst": [gen_obst_at(*p) for p in [
208 [15.25, -3.5, 0], [15.25, 3.5, 0],
209 [12.5, -2.5, 0], [10, 0, 0], [7.5, 2.5, 0]]]}}
211 if __name__ == "__main__":
213 if len(sys.argv) == 2:
216 print(dumps(SCENARIOS[sc]))
221 "obst": gen_obst()}))