def __init__(self, path): self.model = Model(True) self.path = path self.fileName = os.path.basename(path) self._parsedJson: dict = None self._build() logger.log("Preset: %s" % self.fileName)
import utils.cgal.geometry as Geom from model.entity import Entity from model.modelService import Model from model.vertex import Vertex from utils.cgal.drawing import CreatePolygon from utils.cgal.types import Polygon from utils.vertexUtils import ptToStringId, convertToPoint OBSTACLE_COLOR = "Grey" mode = Model() class Obstacle(Entity): def __init__(self, name, pts): """ canvas: tk.Canvas color: color string name: str pts: [utils.cgal.types.Point] """ super().__init__(color=OBSTACLE_COLOR, name=name) self.vertices = [] self._vertexByLocation = {} self._pts = pts self.polygon = Polygon(self._pts) self.createVertices(pts) def createVertices(self, pts):
from model.entity import Entity from model.modelService import Model from model.vertex import Vertex from utils.cgal.drawing import CreateLine from utils.cgal.types import Segment COLOR = "Black" SECONDARY_COLOR = "Red" DASH_PATTERN = (3, 3) model = Model() class DebugEdge(Entity): def __init__(self, name, pts, isConstraint=False, isDirected=False): """ The shape is not created by default. params === canvas: Canvas object (not tk.Canvas) color: color string name: str pts: A list of 2 utils.cgal.types.Point or a utils.cgal.types.Segment """ super().__init__(color=SECONDARY_COLOR if isConstraint else COLOR, name=name) if isinstance(pts, list):
class Preset(object): def __init__(self, path): self.model = Model(True) self.path = path self.fileName = os.path.basename(path) self._parsedJson: dict = None self._build() logger.log("Preset: %s" % self.fileName) def _build(self): if (not os.path.isfile(self.path)): raise FileNotFoundError("%s does not exist" % self.path) with open(self.path, 'r') as jsonFile: self._parsedJson = json.load(jsonFile) self._populateModel() processReducedVisibilityGraph() def _populateModel(self): for e in self._parsedJson: if (e == 'cable'): self._createCableAndRobots(self._parsedJson[e]) elif (e == 'cableLength'): self.model.setMaxCable(float(self._parsedJson[e])) elif (e == 'destinations'): self._createDestinations(self._parsedJson[e]) elif (e == 'obstacles'): self._createObstacles(self._parsedJson[e]) else: logger.log('unexpected json parameter %s' % e) def _parsePt(self, ptString): if "," in ptString: return Point(*[float(c) for c in ptString.split(",")]) pt = self.model.entities[ptString].loc return Point(pt.x(), pt.y()) def _createCableAndRobots(self, ptList): self._createRobots([ptList[0], ptList[-1]]) self.model.cable.append(self.model.robots[0]) for pt in ptList[1:-1]: p = self._parsePt(pt) self.model.cable.append( self.model.getVertexByLocation(p.x(), p.y())) self.model.cable.append(self.model.robots[1]) c = Cable(name="CABLE-O", pts=self.model.cable, isOrigin=True) self.model.entities[c.name] = c def _createRobots(self, ptList): i = 1 colors = ['Red', 'Blue'] for pt in ptList: coords = [float(c) for c in pt.split(',')] r = Robot(color=colors[i - 1], name="R%d" % i, loc=Point(*coords)) self.model.entities[r.name] = r self.model.addVertexByLocation(r) self.model.robots.append(r) i += 1 def _createDestinations(self, ptList): i = 0 for p in ptList: coords = [float(c) for c in p.split(',')] d = Destination(robot=self.model.robots[i], loc=Point(*coords)) self.model.robots[i].destination = d self.model.entities[d.name] = d self.model.addVertexByLocation(d) i += 1 def _createObstacles(self, obsArr): i = 0 for obsVerts in obsArr: pts = [Point(*[float(c) for c in v.split(',')]) for v in obsVerts] o = Obstacle(name='O%s' % i, pts=pts) self.model.entities[o.name] = o self.model.obstacles.append(o) numVerts = len(o.vertices) i += 1 for j in range(numVerts): v = o.vertices[j] prevV = o.vertices[j - 1] nextV = o.vertices[j + 1 if j + 1 < numVerts else 0] v.adjacentOnObstacle = {prevV, nextV} self.model.entities[v.name] = v self.model.vertices.append(v) self.model.addVertexByLocation(v)