Skip to content

Commit 362f134

Browse files
shalinmehtalgsvlmartins-mozeiko
authored andcommitted
Add vector operators, start fixing scripts for rotated map
1 parent beb078f commit 362f134

12 files changed

+110
-49
lines changed

lgsvl/geometry.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,33 @@ def __repr__(self):
2121
return "Vector({}, {}, {})".format(self.x, self.y, self.z)
2222

2323
def __add__(self, v):
24-
return Vector(self.x + v.x, self.y + v.y, self.z + v.z)
24+
if isinstance(v, Vector):
25+
return Vector(self.x + v.x, self.y + v.y, self.z + v.z)
26+
27+
if isinstance(v, (int, float)):
28+
return Vector(self.x + v, self.y + v, self.z + v)
2529

2630
def __sub__(self, v):
27-
return Vector(self.x - v.x, self.y - v.y, self.z - v.z)
31+
if isinstance(v, Vector):
32+
return Vector(self.x - v.x, self.y - v.y, self.z - v.z)
33+
34+
if isinstance(v, (int, float)):
35+
return Vector(self.x - v, self.y - v, self.z - v)
2836

2937
def __mul__(self, v):
30-
return Vector(self.x * v.x, self.y * v.y, self.z * v.z)
38+
if isinstance(v, Vector):
39+
return Vector(self.x * v.x, self.y * v.y, self.z * v.z)
40+
41+
if isinstance(v, (int, float)):
42+
return Vector(self.x * v, self.y * v, self.z * v)
43+
44+
def __rmul__(self, v):
45+
return self * v
46+
# if isinstance(v, Vector):
47+
# return Vector(self.x * v.x, self.y * v.y, self.z * v.z)
48+
49+
# if isinstance(v, (int, float)):
50+
# return Vector(self.x * v, self.y * v, self.z * v)
3151

3252
class BoundingBox:
3353
def __init__(self, min, max):

lgsvl/utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,38 @@ def transform_to_matrix(tr):
4444
[ px, py, pz, 1.0 ],
4545
]
4646

47+
def transform_to_forward(tr):
48+
ax = tr.rotation.x * math.pi / 180.0
49+
sx, cx = math.sin(ax), math.cos(ax)
50+
51+
ay = tr.rotation.y * math.pi / 180.0
52+
sy, cy = math.sin(ay), math.cos(ay)
53+
54+
return Vector(cx * sy, -sx, cx * cy)
55+
56+
def transform_to_up(tr):
57+
ax = tr.rotation.x * math.pi / 180.0
58+
ay = tr.rotation.y * math.pi / 180.0
59+
az = tr.rotation.z * math.pi / 180.0
60+
61+
sx, cx = math.sin(ax), math.cos(ax)
62+
sy, cy = math.sin(ay), math.cos(ay)
63+
sz, cz = math.sin(az), math.cos(az)
64+
65+
return Vector(sx * sy * cz - cy * sz, cx * cz, sy * sz + sx * cy * cz)
66+
67+
def transform_to_right(tr):
68+
ax = tr.rotation.x * math.pi / 180.0
69+
ay = tr.rotation.y * math.pi / 180.0
70+
az = tr.rotation.z * math.pi / 180.0
71+
72+
sx, cx = math.sin(ax), math.cos(ax)
73+
sy, cy = math.sin(ay), math.cos(ay)
74+
sz, cz = math.sin(az), math.cos(az)
75+
76+
return Vector(sx * sy * sz + cy * cz, cx * sz, sx * cy * sz - sy * cz)
77+
78+
4779

4880
def vector_dot(a, b):
4981
return a.x * b.x + a.y * b.y + a.z * b.z

quickstart/04-ego-drive-straight.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import os
99
import lgsvl
10+
import math
1011

1112
sim = lgsvl.Simulator(os.environ.get("SIMULATOR_HOST", "127.0.0.1"), 8181)
1213
if sim.current_scene == "BorregasAve":
@@ -18,8 +19,11 @@
1819

1920
state = lgsvl.AgentState()
2021
state.transform = spawns[0]
22+
23+
forward = lgsvl.utils.transform_to_forward(spawns[0])
24+
2125
# Agents can be spawned with a velocity. Default is to spawn with 0 velocity
22-
state.velocity = lgsvl.Vector(0, 0, 20)
26+
state.velocity = 20 * forward
2327
a = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2428

2529
# The bounding box of an agent are 2 points (min and max) such that the box formed from those 2 points completely encases the agent

quickstart/05-ego-drive-in-circle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
state = lgsvl.AgentState()
2020
state.transform = spawns[0]
21-
state.transform.position.z += 5 # 5m forwards
21+
forward = lgsvl.utils.transform_to_forward(spawns[0])
22+
state.transform.position += 5 * forward# 5m forwards
2223
a = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2324

2425
print("Current time = ", sim.current_time)

quickstart/08-create-npc.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121
state.transform = spawns[0]
2222
a = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2323

24-
# Spawn NPC vehicles 10 meters ahead of the EGO
25-
sx = spawns[0].position.x
26-
sz = spawns[0].position.z + 10.0
24+
forward = lgsvl.utils.transform_to_forward(spawns[0])
25+
right = lgsvl.utils.transform_to_right(spawns[0])
2726

2827
# Spawns one of each of the listed types of NPCS
2928
# The first will be created in front of the EGO and then they will be created to the left
3029
# The available types of NPCs can be found in NPCManager prefab
3130
for i, name in enumerate(["Sedan", "SUV", "Jeep", "Hatchback"]):
3231
state = lgsvl.AgentState()
33-
state.transform = spawns[0]
3432

35-
state.transform.position.x = sx - 4.0 * i
36-
state.transform.position.z = sz
33+
# Spawn NPC vehicles 10 meters ahead of the EGO
34+
state.transform.position = spawns[0].position + (10 * forward) - (4.0 * i * right)
35+
state.transform.rotation = spawns[0].rotation
3736
sim.add_agent(name, lgsvl.AgentType.NPC, state)

quickstart/09-reset-scene.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@
2525
state.transform = spawns[0]
2626
a = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2727

28-
# 10 meters ahead
29-
sx = state.transform.position.x
30-
sz = state.transform.position.z + 10.0
28+
forward = lgsvl.utils.transform_to_forward(spawns[0])
29+
right = lgsvl.utils.transform_to_right(spawns[0])
30+
3131

3232
for i, name in enumerate(["Sedan", "SUV", "Jeep", "Hatchback"]):
3333
state = lgsvl.AgentState()
34-
state.transform = spawns[0]
35-
36-
state.transform.position.x = sx - 4.0 * i
37-
state.transform.position.z = sz
34+
# 10 meters ahead
35+
state.transform.position = spawns[0].position + (10 * forward) - (4.0 * i * right)
36+
state.transform.rotation = spawns[0].rotation
3837
sim.add_agent(name, lgsvl.AgentType.NPC, state)
3938

4039
input("Press Enter to reset")

quickstart/10-npc-follow-the-lane.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,21 @@
2121
a = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2222

2323
state = lgsvl.AgentState()
24-
state.transform = spawns[0]
2524

26-
sx = spawns[0].position.x
27-
sz = spawns[0].position.z
25+
forward = lgsvl.utils.transform_to_forward(spawns[0])
26+
right = lgsvl.utils.transform_to_right(spawns[0])
2827

2928
# 10 meters ahead, on left lane
30-
state.transform.position.z = sz + 10.0
29+
state.transform.position = spawns[0].position + 10.0 * forward
30+
state.transform.rotation = spawns[0].rotation
3131

3232
npc1 = sim.add_agent("Sedan", lgsvl.AgentType.NPC, state)
3333

3434
state = lgsvl.AgentState()
35-
state.transform = spawns[0]
3635

3736
# 10 meters ahead, on right lane
38-
state.transform.position.x = sx + 4.0
39-
state.transform.position.z = sz + 10.0
37+
state.transform.position = spawns[0].position + 4.0 * right + 10.0 * forward
38+
state.transform.rotation = spawns[0].rotation
4039

4140
npc2 = sim.add_agent("SUV", lgsvl.AgentType.NPC, state)
4241

quickstart/11-collision-callbacks.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@
2626
state.transform = spawns[0]
2727
ego = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2828

29+
forward = lgsvl.utils.transform_to_forward(spawns[0])
30+
right = lgsvl.utils.transform_to_right(spawns[0])
31+
2932
# school bus, 20m ahead, perpendicular to road, stopped
3033

3134
state = lgsvl.AgentState()
32-
state.transform = spawns[0]
33-
state.transform.position.z = sz + 20.0
34-
state.transform.rotation.y = 90.0
35+
state.transform.position = spawns[0].position + 20.0 * forward
36+
state.transform.rotation.y = spawns[0].rotation.y + 90.0
3537
bus = sim.add_agent("SchoolBus", lgsvl.AgentType.NPC, state)
3638

3739
# sedan, 10m ahead, driving forward
3840
state = lgsvl.AgentState()
39-
state.transform = spawns[0]
40-
state.transform.position.z = sz + 10.0
41-
state.transform.rotation.y = ry
41+
state.transform.position = spawns[0].position + 10.0 * forward
42+
state.transform.rotation = spawns[0].rotation
4243
sedan = sim.add_agent("Sedan", lgsvl.AgentType.NPC, state)
4344
# Even though the sedan is commanded to follow the lane, obstacle avoidance takes precedence and it will not drive into the bus
4445
sedan.follow_closest_lane(True, 11.1) # 11.1 m/s is ~40 km/h

quickstart/13-npc-follow-waypoints.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
sim.load("BorregasAve")
1818

1919
spawns = sim.get_spawn()
20-
20+
forward = lgsvl.utils.transform_to_forward(spawns[0])
21+
right = lgsvl.utils.transform_to_right(spawns[0])
2122
# EGO
2223

2324
state = lgsvl.AgentState()
2425
state.transform = spawns[0]
2526
state2 = copy.deepcopy(state)
26-
state2.transform.position.z += 50
27+
state2.transform.position += 50 * forward
2728
a = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state2)
2829

2930
# NPC, 10 meters ahead
@@ -33,9 +34,8 @@
3334
sz = spawns[0].position.z + 10.0
3435

3536
state = lgsvl.AgentState()
36-
state.transform = spawns[0]
37-
state.transform.position.x = sx
38-
state.transform.position.z = sz
37+
state.transform.position = spawns[0].position + 10 * forward
38+
state.transform.rotation = spawns[0].rotation
3939
npc = sim.add_agent("Sedan", lgsvl.AgentType.NPC, state)
4040

4141
vehicles = {
@@ -66,9 +66,9 @@ def on_collision(agent1, agent2, contact):
6666
px = 0
6767
pz = (i + 1) * z_delta
6868
# Waypoint angles are input as Euler angles (roll, pitch, yaw)
69-
angle = lgsvl.Vector(0, 0, 0)
69+
angle = spawns[0].rotation
7070
# Raycast the points onto the ground because BorregasAve is not flat
71-
hit = sim.raycast(lgsvl.Vector(sx + px, sy, sz + pz), lgsvl.Vector(0,-1,0), layer_mask)
71+
hit = sim.raycast(spawns[0].position + pz * forward, lgsvl.Vector(0,-1,0), layer_mask)
7272

7373
# NPC will wait for 1 second at each waypoint
7474
wp = lgsvl.DriveWaypoint(hit.point, speed, angle, 1, 0)

quickstart/14-create-pedestrians.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
state = lgsvl.AgentState()
2222
state.transform = spawns[0]
23+
forward = lgsvl.utils.transform_to_forward(spawns[0])
24+
right = lgsvl.utils.transform_to_right(spawns[0])
2325
a = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2426

2527
sx = state.transform.position.x
@@ -33,10 +35,9 @@
3335
i = 0
3436
while True:
3537
state = lgsvl.AgentState()
36-
state.transform = spawns[0]
3738

38-
state.transform.position.x = sx + 5 - (1.0 * (i % 16))
39-
state.transform.position.z = sz + 5 + (1.0 * (i//16))
39+
state.transform.position = spawns[0].position + (5 + (1.0 * (i//16))) * forward + (5 - (1.0 * (i % 16))) * right
40+
state.transform.rotation = spawns[0].rotation
4041
name = random.choice(names)
4142
print("({}) adding {}".format(i+1, name))
4243
p = sim.add_agent(name, lgsvl.AgentType.PEDESTRIAN, state)

0 commit comments

Comments
 (0)