Skip to content

Commit

Permalink
arch: Update the SceneObject contructor
Browse files Browse the repository at this point in the history
Redefine the cosntructor for easier instantion of objects that we load
from a loader class
  • Loading branch information
dervelakos committed Jan 2, 2025
1 parent 44e9e79 commit 113faa4
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 26 deletions.
2 changes: 2 additions & 0 deletions models/car.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
width: 80.0
length: 150.0

resizable: False

mass: 20.0
friction: 25.0

Expand Down
6 changes: 6 additions & 0 deletions models/wall.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
color: [255, 100, 50, 255] # Compressed format

width: 10.0
length: 100.0

resizable: True
2 changes: 2 additions & 0 deletions models/white-truck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
width: 93.75
length: 421.5

resizable: False

mass: 100.0
friction: 200.0

Expand Down
104 changes: 96 additions & 8 deletions scenarios/campain-1.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,104 @@
aliases:
- name: Wall
model: null
- model: models/wall.yaml
name: Wall
render: RectangleRender
type: Wall
- name: Vehicle
model: null
type: SceneObject
- model: models/car.yaml
name: Car
render: SimpleVehicleRender
type: Vehicle
- name: WhiteTruck
model: "models/white-truck.yaml"
- model: models/white-truck.yaml
name: WhiteTruck
render: SimpleVehicleRender
type: Vehicle
objects:
dynamic: []
static: []
static:
- alias: Wall
angle: -0.06200839517698959
dim:
- 10
- 924.0005411253827
loc:
- 1050.0
- 197.0
- alias: Wall
angle: 89.83582916128299
dim:
- 10
- 698.0028653236318
loc:
- 1508.0
- 542.0
- alias: Wall
angle: -179.72716501693193
dim:
- 10
- 1050.0119046944183
loc:
- 987.0
- 863.0
- alias: Wall
angle: -90.22872445172517
dim:
- 10
- 501.0039920000638
loc:
- 465.0
- 613.0
- alias: Car
angle: -90.94695144677347
dim:
- 80.0
- 150.0
loc:
- 1467.0
- 764.0
- alias: Car
angle: -93.46822925891715
dim:
- 80.0
- 150.0
loc:
- 1469.0
- 336.0
- alias: Car
angle: -179.11859600341788
dim:
- 80.0
- 150.0
loc:
- 1280.0
- 804.0
- alias: Car
angle: 180.0
dim:
- 80.0
- 150.0
loc:
- 1013.0
- 806.0
- alias: Car
angle: 179.38394009160078
dim:
- 80.0
- 150.0
loc:
- 685.0
- 796.0
- alias: WhiteTruck
angle: 179.30130561701654
dim:
- 93.75
- 421.5
loc:
- 1130.0
- 531.0
- alias: WhiteTruck
angle: 179.83440572259718
dim:
- 93.75
- 421.5
loc:
- 999.0
- 368.0
5 changes: 3 additions & 2 deletions src/GraphicalWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def createObject(self):
if self.editMode:
alias = self.aliases[self.selectedListObject]
tmp = alias.genObject([center_x, center_y],
angle+90,
[length, width])
angle)
if tmp.isResizable():
tmp.setDimensions(width, length)
tmp.setAlias(alias)
self.simEngine.registerStaticObject(tmp)
self.renderEngine.registerObject(alias.genRender(tmp))
Expand Down
1 change: 0 additions & 1 deletion src/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from PyQt5.QtWidgets import QApplication

from VehicleRender import SimpleVehicleRender, RectangleRender
from SceneObjects import Wall
from Vehicle import Vehicle
from VehicleImporter import easyImport
from Sensors import Lidar
Expand Down
9 changes: 5 additions & 4 deletions src/ScenarioLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def __init__(self, classType, model, render, name):
else:
self.data = None

def genObject(self, loc, angle, dim):
def genObject(self, loc, angle):
if self.data:
return globals()[self.classType](loc, angle, dim, data=self.data)
return globals()[self.classType](loc, angle, dim)
return globals()[self.classType](loc, angle, data=self.data)
return globals()[self.classType](loc, angle)

def genRender(self, obj):
if self.data:
Expand Down Expand Up @@ -108,7 +108,8 @@ def instantiateScenario(self, simEngine, renderEngine):
for obj in self.data['objects']['static']:
print (obj)
alias = self.aliases[obj['alias']]
tmp = alias.genObject(obj['loc'], obj['angle'], obj['dim'])
tmp = alias.genObject(obj['loc'], obj['angle'])
tmp.setDimensions(obj['dim'][0],obj['dim'][1])
tmp.setAlias(alias)
simEngine.registerStaticObject(tmp)
if renderEngine is None:
Expand Down
28 changes: 22 additions & 6 deletions src/SceneObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
from Utils import Vector2D

class SceneObject:
def __init__(self, initialPos, rotation, dimensions, data=None):
def __init__(self, initialPos, rotation, data):

self.pos = Vector2D(initialPos[0], initialPos[1])
self.angle = rotation

#We support only rectanglular objects :P
self.width = dimensions[0]
self.length = dimensions[1]
if 'width' in data:
self.width = data['width']
if 'length' in data:
self.width = data['length']

if 'resizable' in data:
self.resizable = data['resizable']
else:
self.resizable = False

self.boundOffset = Vector2D(0.0, 0.0)

Expand All @@ -32,12 +39,21 @@ def setAngle(self, angle):
def getAngle(self):
return self.angle

def isResizable(self):
return self.resizable

def setAlias(self, alias):
self.alias = alias

def getAlias(self):
return self.alias

def setDimensions(self, width=None, length=None):
if width:
self.width = width
if length:
self.length = length

def getCorners(self):
"""
Calculate the coordinates of the rectangle's four corners.
Expand Down Expand Up @@ -196,6 +212,6 @@ def checkCollision(self, sceneObject):
def __str__(self):
return f"x:{self.x}, y: {self.y}, angle: {self.angle}, width: {self.width}, length: {self.length}"

class Wall(SceneObject):
def __init__(self, initialPos, rotation, dimensions=[100.0, 10.0]):
super().__init__(initialPos, rotation, dimensions)
#class Wall(SceneObject):
# def __init__(self, initialPos, rotation, dimensions=[100.0, 10.0]):
# super().__init__(initialPos, rotation, dimensions)
10 changes: 5 additions & 5 deletions src/VehicleRender.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class BasicRender:
def drawMain(self, painter):
pass

class RectangleRender:
def __init__(self, parent, color=QColor(0, 255, 0), data=None):
class RectangleRender(BasicRender):
def __init__(self, parent, data=None):
self.parent = parent
self.color = color

if 'color' in data:
self.color = QColor(*data['color'])

if data and 'image' in data:
self.image = QImage(data["image"]).scaled(int(self.parent.length),
Expand Down Expand Up @@ -53,8 +55,6 @@ def drawImage(self, painter):
painter.drawImage(int(-self.parent.length/2),
int(-self.parent.width/2),
self.image)
#sw=int(self.parent.length),
#sh=int(self.parent.width))

painter.restore()

Expand Down

0 comments on commit 113faa4

Please sign in to comment.