def createVertices(self, pts): for i in range(0, len(pts)): v = Vertex(name="%s-%d" % (self.name, i), loc=pts[i], ownerObs=self) self.vertices.append(v) self._vertexByLocation[ptToStringId(v)] = v
def __init__(self, x, y, dim, id, path): self._identity_mat = identity_mat44() self._x = x self._y = y self._v1 = Vertex(x-dim/2, y-dim/2, None) self._v2 = Vertex(x+dim/2, y-dim/2, None) self._v3 = Vertex(x+dim/2, y+dim/2, None) self._v4 = Vertex(x-dim/2, y+dim/2, None) self._id = id self._path = path self._texId = None
def getPartialMotion(oldCable, newCable, isRobotA, debug) -> list: me = 0 if isRobotA else -1 other = -1 if isRobotA else 0 src = oldCable[me] dst = newCable[me] start = 0 end = 1 maxFrac = nan maxFracCable = None maxVert = None lastDistance = nan searchThreshold = 1e-2 distanceThreshold = 1 # Binary search for an approximation of the fraction while (isnan(lastDistance) or lastDistance > distanceThreshold ) and fabs(start - end) > searchThreshold: frac = (start + end) / 2 newDst = Geom.getPointOnLineSegment(src, dst, frac) if debug: inObstacle = False for o in model.obstacles: if o.enclosesPoint(newDst): inObstacle = True break if inObstacle: raise RuntimeError("WHUT?") v = model.getVertexByLocation(newDst.x(), newDst.y()) # if not v: v = getClosestVertex(newDst) if not v: v = Vertex(name="tmp-vert", loc=newDst, ownerObs=None) model.addTempVertex(v, isRobotA) tight = tightenCable(oldCable, v, newCable[other]) if isRobotA else tightenCable( oldCable, newCable[other], v) l = Geom.lengthOfCurve(tight) if l <= model.MAX_CABLE: maxFrac = frac maxFracCable = tight lastDistance = Geom.vertexDistance(v, maxVert if maxVert else dst) maxVert = v start = (start + end) / 2 else: end = (start + end) / 2 model.removeTempVertex(v) # Save this vertex for future searches if maxVert: model.addTempVertex(maxVert, isRobotA) maxVert.gaps.add(dst) return (maxFrac, maxFracCable)
def __init__(self): self._identity_mat = identity_mat44() self._vertices = [ Vertex(0, 0, 0), Vertex(-3, 0, 0), Vertex(0, 3, 0), Vertex(0, 0, -3), Vertex(3, 0, 0), Vertex(0, -3, 0), Vertex(0, 0, 3), ] self._edges = [ (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), ]
square = Square(sq_vert, sq_edges, (1, 1, 0)) bt_list = [ Button(SCREEN_SIZE[0] - 50, 50, 50, 1, 'test_image.png'), Button(SCREEN_SIZE[0] - 110, 50, 50, 2, 'test_image.png'), Button(SCREEN_SIZE[0] - 50, 110, 50, 3, 'test_image.png'), Button(SCREEN_SIZE[0] - 110, 110, 50, 4, 'test_image.png'), Button(SCREEN_SIZE[0] - 50, 170, 50, 5, 'test_image.png'), Button(SCREEN_SIZE[0] - 110, 170, 50, 6, 'test_image.png'), Button(SCREEN_SIZE[0] - 50, 230, 50, 7, 'test_image.png'), Button(SCREEN_SIZE[0] - 110, 230, 50, 8, 'test_image.png'), Button(SCREEN_SIZE[0] - 50, 290, 50, 9, 'test_image.png'), Button(SCREEN_SIZE[0] - 110, 290, 50, 10, 'test_image.png'), ] vertex0 = Vertex(1, -1, -1) vertex1 = Vertex(1, 1, -1) vertex2 = Vertex(-1, 1, -1) vertex3 = Vertex(-1, -1, -1) vertex4 = Vertex(1, -1, 1) vertex5 = Vertex(1, 1, 1) vertex6 = Vertex(-1, -1, 1) vertex7 = Vertex(-1, 1, 1) edges = ( Edge(vertex0, vertex1), Edge(vertex0, vertex3), Edge(vertex0, vertex4), Edge(vertex2, vertex1), Edge(vertex2, vertex3), Edge(vertex2, vertex7),
def initialize_graph(self, graph_size): vertex_count = 0 candidate_sinks_number = 0 candidate_controllers_number = 0 edges_pair_list = [] if graph_size == 1: vertex_count = 20 candidate_sinks_number = 6 candidate_controllers_number = 9 elif graph_size == 2: vertex_count = 40 candidate_sinks_number = vertex_count / 5 candidate_controllers_number = vertex_count / 10 elif graph_size == 3: vertex_count = 80 candidate_sinks_number = vertex_count / 5 candidate_controllers_number = vertex_count / 10 elif graph_size == 4: vertex_count = 150 candidate_sinks_number = 50 candidate_controllers_number = 40 for i in range(vertex_count): ith_node_neighbors_count = vertex_count / 2 neighbors_number_set = set() while len(neighbors_number_set) < ith_node_neighbors_count: next_int = random.randint(0, vertex_count - 1) if next_int != i: neighbors_number_set.add(next_int) for neighborNumber in neighbors_number_set: edges_pair_list.append( ("Edge_" + str(i) + "_To_" + str(neighborNumber), (i, neighborNumber))) for j in range(vertex_count): location = Vertex("Node_" + str(j), "Node_" + str(j), self.SINK_LOAD, self.CONTROLLER_LOAD) self.nodes_list.append(location) for edge in edges_pair_list: self.add_lane(edge[0], (edge[1])[0], (edge[1])[0]) candidate_sinks_number_set = set() candidate_controller_number_set = set() while len(candidate_sinks_number_set) < candidate_sinks_number: next_int = random.randint(0, vertex_count - 1) candidate_sinks_number_set.add(next_int) while len(candidate_controller_number_set ) < candidate_controllers_number: next_int = random.randint(0, vertex_count - 1) candidate_controller_number_set.add(next_int) for candidateControllerNumber in candidate_controller_number_set: self.candidate_controllers_list.append( self.nodes_list[candidateControllerNumber]) for candidate_sinks_number in candidate_sinks_number_set: self.candidate_sinks_list.append( self.nodes_list[candidate_sinks_number]) graph = Graph(self.nodes_list, self.edges_list) return graph
class Button: def __init__(self, x, y, dim, id, path): self._identity_mat = identity_mat44() self._x = x self._y = y self._v1 = Vertex(x-dim/2, y-dim/2, None) self._v2 = Vertex(x+dim/2, y-dim/2, None) self._v3 = Vertex(x+dim/2, y+dim/2, None) self._v4 = Vertex(x-dim/2, y+dim/2, None) self._id = id self._path = path self._texId = None def draw(self): glMatrixMode(GL_MODELVIEW) glPushMatrix() glMultMatrixf(self._identity_mat) glColor3f(1,1,1) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self._texId) glBegin(GL_QUADS) glTexCoord2f(0.0, 1.0) self._v1.draw() glTexCoord2f(1.0, 1.0) self._v2.draw() glTexCoord2f(1.0, 0.0) self._v3.draw() glTexCoord2f(0.0, 0.0) self._v4.draw() glEnd() glDisable(GL_TEXTURE_2D) glFlush() glPopMatrix() def load_texture(self, path): textureSurface = pygame.image.load(path) textureData = pygame.image.tostring(textureSurface, "RGBA", 1) width = textureSurface.get_width() height = textureSurface.get_height() texid = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texid) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) self._texId = texid def is_pressed(self, pos): if (pos[0]>self._v1._x) and (pos[0]<self._v2._x) and (pos[1]<self._v3._y) and (pos[1]>self._v1._y): return True return False def __str__(self): return "Button"+str(self._id)
def add_lane(lane_id, source_location_number, destination_location_number): lane_1_edge = Edge(lane_id, nodes_vertex_list[source_location_number], nodes_vertex_list[destination_location_number], 1) edges_vertex_list.append(lane_1_edge) lane_2_edge = Edge(lane_id, nodes_vertex_list[destination_location_number], nodes_vertex_list[source_location_number], 1) edges_vertex_list.append(lane_2_edge) if __name__ == '__main__': nodes_vertex_list = [] edges_vertex_list = [] for i in range(0, 11): location_vertex = Vertex("Node_" + str(i), "Node_" + str(i), i, i) nodes_vertex_list.append(location_vertex) add_lane("Edge_0", 0, 1) add_lane("Edge_1", 0, 2) add_lane("Edge_2", 0, 4) add_lane("Edge_3", 2, 6) add_lane("Edge_4", 2, 7) add_lane("Edge_5", 3, 7) add_lane("Edge_6", 5, 8) add_lane("Edge_7", 8, 9) add_lane("Edge_8", 7, 9) add_lane("Edge_9", 4, 9) add_lane("Edge_10", 9, 10) add_lane("Edge_11", 1, 10)
def parse_first_line(self, line: str): v_no, e_no = line.split(' ') self.n, self.m = int(v_no), int(e_no) self.vs = [Vertex(idx) for idx in range(self.n)]