class ZONE: dims = Vector(0.54 * M, 0.48 * M) outlines = ( # F1, F4, F2, F5, F3, F6 *mirrors(Box(dims, Vector(-3.54 * M, 0.55 * M))), *mirrors(Box(dims, Vector(-2.14 * M, -0.59 * M))), *mirrors(Box(dims, Vector(0, 1.795 * M))) )
def hits_barrier(self, barrier: Box): if self.center.distance_to(barrier.center) < ROBOT.outline.radius + barrier.radius and \ (any(barrier.contains(p) for p in self._corners) or any(ROBOT.outline.contains(p.inv_transform(self.center, self.rotation)) for p in barrier.corners)): self.barrier_hits += 1 return True return False
def __init__(self, init_x, init_y, init_z, init_roll, init_pitch, init_yaw,\ init_min_x, init_min_y, init_min_z, init_max_x, init_max_y, init_max_z,\ x=0., y=0., z=0., roll=0., pitch=0., yaw=0.,\ min_x=0., min_y=0., min_z=0., max_x=0., max_y=0., max_z=0.): '''The variables whose names start with the prefix 'init_' represent the pose and the bounding box of an object before an action execution; all the others represent the object's pose and bounding box after an execution. ''' self.init_position = Vector(init_x, init_y, init_z) self.init_rotation = Rotator(init_roll, init_pitch, init_yaw) self.init_bounding_box = Box(init_min_x, init_min_y, init_min_z, init_max_x, init_max_y, init_max_z) self.position = Vector(x, y, z) self.rotation = Rotator(roll, pitch, yaw) self.bounding_box = Box(min_x, min_y, min_z, max_x, max_y, max_z)
def __init__(self, sensors=None, human=False): super().__init__() self.update = self.update_no_track #self.move = self.move_cor self.start_time = time.time() self.human = human # physical car parameters self.width = 10 self.length = 30 self.hwbase = self.length / 2 self.htrack = self.width / 2 self.max_speed = 150 self.rot = 0 self.speed = 0 self.max_steering = PI / 4 self.steering = 0 # for collision detection and box drawings self.quad = Quad(box=Box(-self.width/2, self.length/2, self.width, self.length)) self.corner_distance = Line(self.position, self.quad.top_left).length self.corner_angle = Line(self.position, self.quad.top_right).angle _ = self.corners # to set corner attributes # track info self.track = None self.section = None self.section_idx = -1 self.collided = False self.laps = 0 if not sensors: self.sensors = SensorRig(self, (-PI/2, -3 * self.corner_angle, -self.corner_angle, 0, self.corner_angle, 3 * self.corner_angle, PI/2), (30, 50, 100, 175, 100, 50, 30)) elif sensors == 1: self.sensors = SensorRig(self, (0,), (175,)) else: n = sensors // 2 * 2 + 1 angles = tuple(-PI/2 + i*PI/(n-1) for i in range(n)) distances = (100,) * n self.sensors = SensorRig(self, angles, distances) # neural network if cmodule: self.driver = neural.CDriver(1+self.sensors.size) else: self.driver = neural.Driver(1+self.sensors.size) # graphics stuff self.construct() self.section_batch = pyglet.graphics.Batch() self.section_batch_idx = -1
def make_track_new(self): #points = [(0, 100), (20, 150), (50, 250), (100, 280), (150, 300)] points = [(0, 100), (20, 150), (50, 250), (100, 260), (160, 230), (180, 180), (180, 120), (200, 50), (200, 20), (100, -120), (20, -90), (0, -40)] prev_quad = Quad(box=Box(0, 50, self.width, 50)) #prev_quad = Quad(coords=((0,0),(0,self.width),(0,50),(self.width,50))) for point in points: prev_quad, new_quad = self.make_section_quad(point, prev_quad) self.add_section(quad=prev_quad) prev_quad = new_quad self.add_section(quad=prev_quad) quad = Quad(coords=(prev_quad.top_left, prev_quad.top_right, self.sections[0].quad.bottom_left, self.sections[0].quad.bottom_right)) self.add_section(quad) self.circular = True
class ROBOT: outline = Box(Vector(0.6 * M, 0.5 * M)) _armor_length = 0.14 * M armor_lines = [ LineSegment(*y_mirrors(Vector(outline.dims.x / 2, _armor_length / 2))), LineSegment(*x_mirrors(Vector(_armor_length / 2, outline.dims.y / 2))), LineSegment(*x_mirrors(Vector(_armor_length / 2, -outline.dims.y / 2))), LineSegment(*y_mirrors(Vector(-outline.dims.x / 2, _armor_length / 2))) ] armor_damages = [20, 40, 40, 60] _drive_radius = 0.3 * M _factor = 1 / (math.sqrt(2) * _drive_radius) drive_config = MotionConfig(3 * MS, 6 * MS2, 0.1 * MS2) rotation_config = MotionConfig(_factor * drive_config.top_speed, _factor * drive_config.top_accel, _factor * drive_config.friction_decel) gimbal_yaw_config = MotionConfig(300 * DS, 1000 * DS2, 10 * DS2) zero_tolerance = 0.001 rebound_coeff = 0.4 shot_cooldown = 0.1 * S
from smoke_solver import Smoke_Builder, FlowEmitter from smoke_animation import Smoke_Animation from colliders import RigidBodyCollier, Collider from geometry import Box, Ball res = (512, 512) ti.init(arch=ti.gpu, kernel_profiler=True) gui = ti.GUI("smoke animation", res=res) # build smoke solver smoke = \ Smoke_Builder(res) \ .add_flow_emitter([512//2 , 0] , 512//3 , 2000.0) \ .set_decay(0.995) \ .build() # .set_compute_buoyancy_force(tempreture_factor=600)\ ani = Smoke_Animation(smoke, res) ani.reset() # collider smoke.add_collider(RigidBodyCollier(Box([156, 156], [226, 276]))) smoke.add_collider(RigidBodyCollier(Ball([276, 226], 30))) while gui.running: ani.update() ani.display(gui) ti.kernel_profiler_print()
def __init__(self, PLY = None): if PLY == None: print "PLY nao fornecido" exit(0) self.box = Box() self.vertex = PLY.vertex self.faces = PLY.faces self.colors = [ (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()) ] self.isOpened = False self.isAnimated = False self.useTexture = False self.mapRotate = matrix.identity() self.textureMap = {} self.aux = 0 edges_per_face = [] points_per_face = [] points_per_face_orig = [] polygons = [] for face in self.faces: edges_per_face.append([]) points_per_face.append([]) points_per_face_orig.append([]) for i in xrange(len(face)): points_per_face[-1].append(copy.deepcopy(self.vertex[face[i]])) points_per_face_orig[-1].append(copy.deepcopy(self.vertex[face[i]])) if i != (len(face)-1): edges_per_face[-1].append((face[i],face[i+1])) else: edges_per_face[-1].append((face[i],face[0])) polygons.append(Polygon(points_per_face_orig[-1])) adjacences_list = [] edge_between_faces = {} for i in xrange(len(edges_per_face)): adjacences_list.append([]) for j in xrange(len(edges_per_face[i])): edge = edges_per_face[i][j] for k in xrange(len(edges_per_face)): if k != i: if (((edge[1],edge[0]) in edges_per_face[k])or((edge[0],edge[1]) in edges_per_face[k])) : adjacences_list[i].append(k) edge_between_faces[(i,k)] = edge edge_between_faces[(k,i)] = edge break if len(edges_per_face[i]) != len(adjacences_list[i]): print "Inconsistencia na contrucao do grafico" self.edges_per_face = edges_per_face self.points_per_face = points_per_face self.points_per_face_orig = points_per_face_orig self.adjacences_list = adjacences_list self.polygons = polygons self.edge_between_faces = edge_between_faces self.selected = [0 for elem in edges_per_face] self.face_selected = -1 return
class Polihedron(object): def __init__(self, PLY = None): if PLY == None: print "PLY nao fornecido" exit(0) self.box = Box() self.vertex = PLY.vertex self.faces = PLY.faces self.colors = [ (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()), (random.random(),random.random(),random.random()) ] self.isOpened = False self.isAnimated = False self.useTexture = False self.mapRotate = matrix.identity() self.textureMap = {} self.aux = 0 edges_per_face = [] points_per_face = [] points_per_face_orig = [] polygons = [] for face in self.faces: edges_per_face.append([]) points_per_face.append([]) points_per_face_orig.append([]) for i in xrange(len(face)): points_per_face[-1].append(copy.deepcopy(self.vertex[face[i]])) points_per_face_orig[-1].append(copy.deepcopy(self.vertex[face[i]])) if i != (len(face)-1): edges_per_face[-1].append((face[i],face[i+1])) else: edges_per_face[-1].append((face[i],face[0])) polygons.append(Polygon(points_per_face_orig[-1])) adjacences_list = [] edge_between_faces = {} for i in xrange(len(edges_per_face)): adjacences_list.append([]) for j in xrange(len(edges_per_face[i])): edge = edges_per_face[i][j] for k in xrange(len(edges_per_face)): if k != i: if (((edge[1],edge[0]) in edges_per_face[k])or((edge[0],edge[1]) in edges_per_face[k])) : adjacences_list[i].append(k) edge_between_faces[(i,k)] = edge edge_between_faces[(k,i)] = edge break if len(edges_per_face[i]) != len(adjacences_list[i]): print "Inconsistencia na contrucao do grafico" self.edges_per_face = edges_per_face self.points_per_face = points_per_face self.points_per_face_orig = points_per_face_orig self.adjacences_list = adjacences_list self.polygons = polygons self.edge_between_faces = edge_between_faces self.selected = [0 for elem in edges_per_face] self.face_selected = -1 return def draw(self): points = [] for i,face in enumerate(self.faces): if ( len(face) % 3 == 0): glBegin(GL_TRIANGLES) elif ( len(face) % 4 == 0): glBegin(GL_QUADS) else: glBegin(GL_POLYGON) if not self.useTexture: c = self.colors[i % len(self.colors)] glColor3f(1.0*c[0],1.0*c[1],1.0*c[2]) if (self.isOpened or self.isAnimated): points = self.points_per_face[i] else: points = self.points_per_face_orig[i] count = 0 texturePerFace = [0]*len(points) for point in points: #print(actualPoint) if self.useTexture:# and actualPoint not in self.textureMap.keys(): try: p = self.box.normalize(point.transform(self.mapRotate)) texturePerFace[count] = p texture = p if count == (len(points)-1) and i not in self.textureMap.keys(): self.textureMap[i] = texturePerFace elif i in self.textureMap.keys(): textureInThisFace = self.textureMap[i] texture = textureInThisFace[count] #texture = self.box.normalize(wrongSizedTexture) #p = self.box.normalize(point.transform(self.mapRotate)) #print(self.aux) #print(round(point.x,2), round(point.y, 2), round(point.z,2)) glTexCoord2f(texture.x,texture.y) glVertex3f(point.x,point.y,point.z) except AttributeError as error: pass #elif self.useTexture and actualPoint in self.textureMap.keys(): #texture = self.textureMap[actualPoint] #glTexCoord2f(texture.x, texture.y) #glVertex3f(point.x,point.y,point.z) else: glVertex3f(point.x,point.y,point.z) count += 1 glEnd() def face_intersect(self,ray): face_to_select = -1 smaller_u = 100000000000000000.0 for i,poly in enumerate(self.polygons): result = ray.intersectToPlane(poly) if not(result == None) and (poly.contains(result[0])): if abs(result[1]) < abs(smaller_u): smaller_u = abs(result[1]) face_to_select = i self.last_face_clicked = face_to_select return face_to_select def face_select(self,i): self.selected[self.last_face_clicked] = 1. self.face_selected = self.last_face_clicked def face_unselect(self): self.selected[self.face_selected] = 0. self.face_selected = -1 def open_like_BFS(self,alpha): Q = [] visite1 = [False for i in self.faces] transf_vec = [None for i in self.faces] altura = [0 for i in self.faces] q0 = self.face_selected Q.append(q0) visite1[q0] = True transf_vec[q0] = matrix.identity() altura[q0] = 1. self.points_per_face[q0] = copy.deepcopy(self.points_per_face_orig[q0]) while len(Q) > 0: q1 = Q.pop(0) for v in self.adjacences_list[q1]: if visite1[v] == False : visite1[v] = True Q.append(v) altura[v] = altura[q0] + 1 N1 = self.polygons[q1].normal N2 = self.polygons[v].normal (np1,np2) = self.edge_between_faces[(q1,v)] v0 = self.vertex[np1] v1 = self.vertex[np2] edge = Line(v0,v1) ang = np.rad2deg(math.acos(N1.dotProd(N2))) axis = edge.dir if axis.tripleProd(N1,N2) > 0: ang = -ang ang = alpha*ang R = matrix.translateAndRotate(ang,v1,edge.dir) transf_vec[v] = matrix.dot(transf_vec[q1],R) for i in xrange(len(self.points_per_face[v])): self.points_per_face[v][i] =\ self.points_per_face_orig[v][i].transform(transf_vec[v]) if self.useTexture: axis_z = Point(0.,0.,1.) angle_z = axis_z.dotProd(self.polygons[q0].normal) axis_r = axis_z.crossProd(self.polygons[q0].normal) self.mapRotate = matrix.translateAndRotate(angle_z,axis_r,self.points_per_face[q0][0]) for points in self.points_per_face: for point in points: self.box.add(point.transform(self.mapRotate)) self.box.setParameters() def open(self): self.isOpened = True def close(self): self.isOpened = False def set_texture(self): self.useTexture = True def unset_texture(self): self.useTexture = False def animate(self): self.isAnimated = True def static(self): self.isAnimated = False