1 """Generate simple JSON scenario.
3 The scenario contains at least:
5 - `init` -- the init car position,
7 - `slot` -- the parking slot,
9 - `obst` -- the list of (convex polygon) obstacles.
11 from json import dumps
12 from math import cos, pi, sin
13 from random import random, uniform
20 MTR = ((CTC / 2)**2 - WB**2)**0.5 - W / 2
29 """Generate car init position."""
30 # TODO if changed, change ``gen_slot`` accordingly
34 def gen_slot_at(x, y, h, parallel=True):
35 """Generate slot at specified coordinates."""
45 x + length/2 * cos(h - pi/2 * right),
46 y + length/2 * sin(h - pi/2 * right),
49 x + width * cos(h) + length/2 * cos(h - pi/2 * right),
50 y + width * sin(h) + length/2 * sin(h - pi/2 * right),
53 x + width * cos(h) + length/2 * cos(h + pi/2 * right),
54 y + width * sin(h) + length/2 * sin(h + pi/2 * right),
57 x + length/2 * cos(h + pi/2 * right),
58 y + length/2 * sin(h + pi/2 * right),
64 x + width/2 * cos(h - pi/2 * right),
65 y + width/2 * sin(h - pi/2 * right),
68 x + length * cos(h) + width/2 * cos(h - pi/2 * right),
69 y + length * sin(h) + width/2 * sin(h - pi/2 * right),
72 x + length * cos(h) + width/2 * cos(h + pi/2 * right),
73 y + length * sin(h) + width/2 * sin(h + pi/2 * right),
76 x + width/2 * cos(h + pi/2 * right),
77 y + width/2 * sin(h + pi/2 * right),
82 def gen_slot(l=5.3, w=2.4):
83 """Generate parking slot."""
84 parallel = True if random() < 0.5 else False
85 if parallel and l == 5.3 and w == 2.4:
92 right = 1.0 if random() < 0.5 else -1.0
94 angl = uniform(0, 2 * pi)
97 h = uniform(0, 2 * pi)
101 x + l/2 * cos(h - pi/2 * right),
102 y + l/2 * sin(h - pi/2 * right),
105 x + w * cos(h) + l/2 * cos(h - pi/2 * right),
106 y + w * sin(h) + l/2 * sin(h - pi/2 * right),
109 x + w * cos(h) + l/2 * cos(h + pi/2 * right),
110 y + w * sin(h) + l/2 * sin(h + pi/2 * right),
113 x + l/2 * cos(h + pi/2 * right),
114 y + l/2 * sin(h + pi/2 * right),
120 x + w/2 * cos(h - pi/2 * right),
121 y + w/2 * sin(h - pi/2 * right),
124 x + l * cos(h) + w/2 * cos(h - pi/2 * right),
125 y + l * sin(h) + w/2 * sin(h - pi/2 * right),
128 x + l * cos(h) + w/2 * cos(h + pi/2 * right),
129 y + l * sin(h) + w/2 * sin(h + pi/2 * right),
132 x + w/2 * cos(h + pi/2 * right),
133 y + w/2 * sin(h + pi/2 * right),
138 def gen_obst_at(x, y, h):
139 """Generate obstacle at specific coordinates."""
144 x + width/2 * cos(h - pi/2) + length/2 * cos(h),
145 y + width/2 * sin(h - pi/2) + length/2 * sin(h),
148 x + width/2 * cos(h - pi/2) - length/2 * cos(h),
149 y + width/2 * sin(h - pi/2) - length/2 * sin(h),
152 x + width/2 * cos(h + pi/2) - length/2 * cos(h),
153 y + width/2 * sin(h + pi/2) - length/2 * sin(h),
156 x + width/2 * cos(h + pi/2) + length/2 * cos(h),
157 y + width/2 * sin(h + pi/2) + length/2 * sin(h),
160 x + width/2 * cos(h - pi/2) + length/2 * cos(h),
161 y + width/2 * sin(h - pi/2) + length/2 * sin(h),
166 """Generate obstacles array."""
168 min_r = ((W/2)**2 + ((WB+L)/2)**2)**0.5 + (OBST_W**2 + OBST_L**2)**0.5 / 2
169 for i in range(OBST_COUNT):
172 angl = uniform(0, 2 * pi)
173 r = uniform(min_r**2, (SLOT_RADI - 5)**2)**0.5
176 h = uniform(0, 2 * pi)
179 x + w/2 * cos(h - pi/2) + l/2 * cos(h),
180 y + w/2 * sin(h - pi/2) + l/2 * sin(h),
183 x + w/2 * cos(h - pi/2) - l/2 * cos(h),
184 y + w/2 * sin(h - pi/2) - l/2 * sin(h),
187 x + w/2 * cos(h + pi/2) - l/2 * cos(h),
188 y + w/2 * sin(h + pi/2) - l/2 * sin(h),
191 x + w/2 * cos(h + pi/2) + l/2 * cos(h),
192 y + w/2 * sin(h + pi/2) + l/2 * sin(h),
195 x + w/2 * cos(h - pi/2) + l/2 * cos(h),
196 y + w/2 * sin(h - pi/2) + l/2 * sin(h),
205 "slot": gen_slot_at(15, 0, 0, True),
206 "obst": [gen_obst_at(p) for p in [[], [], []]]}}
208 if __name__ == "__main__":
210 if len(sys.argv) == 2:
213 print(dumps(SCENARIOS()[sc]))
218 "obst": gen_obst()}))