def __init__(self, position=Vector3D(0, 0, 0), direction=Vector3D(0, 0, 1), right=Vector3D(0, 0, 0), down=Vector3D(0, 0, 0)): self.position = position self.direction = direction self.right = right self.down = down
def get_triangle_points(): """basic triangle vertices""" points = ( Vector3D(-1, 0, 0, 1), Vector3D(0, 1, 0, 1), Vector3D(1, 0, 0, 1), Vector3D(-1, 0, 0, 1), ) return points
def test_rot_matrices(self): m = Matrix3D.get_rot_x_matrix(100) assert m.dot(Matrix3D.identity()) == m m = Matrix3D.get_rot_y_matrix(100) assert m.dot(Matrix3D.identity()) == m m = Matrix3D.get_rot_z_matrix(100) assert m.dot(Matrix3D.identity()) == m # rotate vector only in x-axis around x - nothing should happen v1 = Vector3D(1, 0, 0, 1) assert Matrix3D.get_rot_x_matrix(100).v_dot(v1) == v1 v1 = Vector3D(0, 1, 0, 1) assert Matrix3D.get_rot_y_matrix(100).v_dot(v1) == v1 v1 = Vector3D(0, 0, 1, 1) assert Matrix3D.get_rot_z_matrix(100).v_dot(v1) == v1 # rotate vectors really v1 = Vector3D(1.0, 0.0, 0.0, 1.0) # 90 degrees or pi/2 real_v = Matrix3D.get_rot_z_matrix(math.pi / 2).v_dot(v1) test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 180 degrees real_v = Matrix3D.get_rot_z_matrix(math.pi).v_dot(v1) test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 270 degrees real_v = Matrix3D.get_rot_z_matrix(math.pi + math.pi / 2).v_dot(v1) test_v = Vector3D.from_list([0.000000, -1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 360 degrees real_v = Matrix3D.get_rot_z_matrix(2 * math.pi).v_dot(v1) test_v = Vector3D.from_list([1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # rotate around Y-Axis about 180 degrees real_v = Matrix3D.get_rot_y_matrix(math.pi).v_dot(v1) test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # rotate y:90 and x:90 -> (0, 1, 0, 1) real_v = Matrix3D.get_rot_y_matrix(math.pi / 2).v_dot(v1) test_v = Vector3D.from_list([0.000000, 0.000000, -1.000000, 1.000000]) assert real_v.nearly_equal(test_v) real_v = Matrix3D.get_rot_x_matrix(math.pi / 2).v_dot(real_v) test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # and this is the combined version rot_y = Matrix3D.get_rot_y_matrix(math.pi / 2) print "rotation around y:\n", rot_y rot_x = Matrix3D.get_rot_x_matrix(math.pi / 2) print "rotation around x:\n", rot_x rot_z = Matrix3D.get_rot_z_matrix(math.pi / 2) print "rotation around z:\n", rot_z rot_m = rot_x.dot(rot_y.dot(rot_z)) print "combined rotation matrix:\n", rot_m real_v = rot_m.v_dot(v1) print "resulting vector:", real_v test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v)
def getPosCurve(p, x, y): a = p.getY() * p.getY() - y * y b = (2 * (y - p.getY())) if b != 0.0: x1 = x + p.getX() - 250 h = (p.getX() - x1) * (p.getX() - x1) + a y1 = -h / b return Vector3D(x1, y1) else: return Vector3D(0, 0)
def get_rectangle_points(): """basic rectangle vertices""" points = ( Vector3D(-1, 1, 0, 1), Vector3D(1, 1, 0, 1), Vector3D(1, -1, 0, 1), Vector3D(-1, -1, 0, 1), Vector3D(-1, 1, 0, 1), ) return points
def setup_operation(self, op): ret = Oplist() # South wall loc = Location(self, Vector3D(0, 0, 0)) loc.bbox = Vector3D(2, 0.5, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) return ret
def touch_operation(self, op): retops = Oplist() if self.status<0: return self.status=-1 newloc=self.location.copy() newloc.velocity=Vector3D() retops = retops + Operation("move", Entity(self.id, location=newloc.copy(), mode=self.mode), to=self) retops = retops + Operation("set", Entity(self.id, status=self.status), to=self) for item in ['skull', 'ribcage', 'arm', 'pelvis', 'thigh', 'shin']: newloc.coordinates = newloc.coordinates + Vector3D(uniform(-1,1), uniform(-1,1), uniform(-1,1)) retops = retops + Operation("create", Entity(name=item,parents=[item],location=newloc.copy()), to=self) return retops
def findMinCircle(p1, p2, p3): x1 = p1.point.getX() x1_2 = x1 * x1 x2 = p2.point.getX() x2_2 = x2 * x2 x3 = p3.point.getX() x3_2 = x3 * x3 y1 = p1.point.getY() y1_2 = y1 * y1 y2 = p2.point.getY() y2_2 = y2 * y2 y3 = p3.point.getY() y3_2 = y3 * y3 # First part h = -(x2_2 + y2_2) + (y1_2 + x1_2) b = 2 * (x2 - x1) a = 2 * (y2 - y1) # Second Part f = -(x3_2 + y3_2) + (y1_2 + x1_2) d = 2 * (y3 - y1) e = 2 * (x3 - x1) # Determine Center t = (d * b - a * e) if isNull(t): return None x = 0 y = 0 if isNull(b): y = -h / a #a != 0 car pas deux points identiques x = -(d * y + f) / e #e != 0 car pas 3 points alignés else: y = (-f * b + e * h) / t x = -(a * y + h) / b center = Vector3D(x, y) magn = math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)) minY = Vector3D(x, y + magn) vSite = VSite(minY, center) vSite.sites.append(p1) vSite.sites.append(p2) vSite.sites.append(p3) return vSite
def __init__(self, a=Vector3D(1, 0, 0), b=Vector3D(0, 1, 0), c=Vector3D(0, 0, 1), color=Color(1, 1, 1)): self.a = a self.b = b self.c = c ca = self.c - self.a ba = self.b - self.a self.normal = ca.cross_product(ba).normalize() self.distance = self.normal.dot_product(self.a) self.color = color
def __init__(self, surface, origin): """ pygame surface to draw on center positon of mesh in 2d space """ self.surface = surface (self.origin_x, self.origin_y) = origin self.vector = Vector3D(100, 0, 0, 1) self.model = Models3D.get_cube_mesh() self.center = (surface.get_width() / 2, surface.get_height() / 2) self.angle = 0 self.angle_step = math.pi / 180 self.x_axis = Vector3D(100.0, 0.0, 0.0, 1) self.y_axis = Vector3D(0.0, 100.0, 0.0, 1) self.z_axis = Vector3D(0.0, 0.0, 100.0, 1)
def __init__(self, normal=Vector3D(1, 0, 0), distance=0.0, color=Color(0.5, 0.5, 0.5, 0)): self.normal = normal self.distance = distance self.color = color
def drop_fruit(self): result = atlas.Oplist() if self.fruits > 0: rand = randint(minudrop, maxudrop) rand = min(rand, self.fruits) self.fruits = self.fruits - rand for x in range(rand): # pick a random spot between the 2 extremes of the plant if debug_tree: print "Creating Fruit" randx = uniform( self.location.coordinates.x - cs(x1 * self.height, x2 * self.height, xMax), self.location.coordinates.x + cs(x2 * self.height, x1 * self.height, xMax)) randy = uniform( self.location.coordinates.y - cs(y1 * self.height, y2 * self.height, yMax), self.location.coordinates.y + cs(y2 * self.height, y1 * self.height, yMax)) #randz = uniform ( self.location.coordinates.z - cs( z1 * self.size, z2 * self.size, zMax) , self.location.coordinates.z + cs( z2 * self.size, z1 * self.size, zMax) ) randz = 0 if hasattr(self, "coords_modify"): randx, randy, randz = coords_modify(randx, randy, randz) fruit = Entity(name=self.fruitname, location=Location(server.world, Vector3D(randx, randy, randz)), parents=[self.fruitname]) result = result + Operation("create", fruit, to=self) if debug_tree: print fruit return result
def mouse(event): a = Vector3D(float(event.x) / fac.getX(), float(event.y) / fac.getY()) """ point = len(vec) - 1 max = 5 magn = max + 1.0 while point > -1 and (vec[point] - a).getMagnitude() > max: point -= 1 if point == -1: vec.append(a) print("Add") "" else: print("Mo") vecA = a - mousePos vec[point] += vecA "" fortune.init(vec, False) """ print(a) vec.append(a) fortune.init(vec, False) fortune.beachLine.update( float(fortune.rEvents[len(fortune.rEvents) - 1].point.getY() + 100) / fac.getY()) full(event) start[0] = True
def computeBreakPoint(p1, p2, ly): x1 = p1.point.getX() x1_2 = x1 * x1 x2 = p2.point.getX() x2_2 = x2 * x2 y1 = p1.point.getY() y1_2 = y1 * y1 y2 = p2.point.getY() y2_2 = y2 * y2 ly_2 = ly * ly # First part h = x1_2 + y1_2 - ly_2 e = 2 * (ly - y1) # Second Part a = 2 * (x2 - x1) b = 2 * (y2 - y1) c = x1_2 + y1_2 - x2_2 - y2_2 if isNull(b): if isNull(e): return [] if isNull(a): return [] x = -c / a y = (-x * x + 2 * x * x1 - h) / e return [Vector3D(x, y)] else: d = -(a * e + 2 * b * x1) f = -c * e + h * b delta = (d * d - 4 * b * f) if delta < 0.0: return [] elif delta < 0.00001: x = -d / (2 * b) y = (-c - a * x) / b return [Vector3D(x, y)] else: xa = (-d - math.sqrt(delta)) / (2 * b) xb = (-d + math.sqrt(delta)) / (2 * b) # On prend entre les deux ya = (-c - a * xa) / b yb = (-c - a * xb) / b return [Vector3D(xa, ya), Vector3D(xb, yb)]
def setup_operation(self, op): ret = Oplist() # South counter loc = Location(self, Vector3D(-0.5, 0, 0.5)) loc.bbox = Vector3D(3, 1, -0.5) ret.append( Operation("create", Entity(name='wall', parent='wall', location=loc), to=self)) # North back wall loc = Location(self, Vector3D(-0.5, 0, -1.3)) loc.bbox = Vector3D(3, 3, -0.2) ret.append( Operation("create", Entity(name='wall', parent='wall', location=loc), to=self)) return ret
def setup_operation(self, op): ret = Oplist() # West counter loc = Location(self, Vector3D(-0.5, -0.5, 0)) loc.bbox = Vector3D(0.5, 3, 1) ret.append( Operation("create", Entity(name='wall', parent='wall', location=loc), to=self)) # North back wall loc = Location(self, Vector3D(1.3, -0.5, 0)) loc.bbox = Vector3D(0.2, 3, 3) ret.append( Operation("create", Entity(name='wall', parent='wall', location=loc), to=self)) return ret
def test_init(self): m1 = Vector3D(1, 1, 1, 1) m2 = eval(m1.__repr__()) assert m1 == m2 m2 = m1 + m1 m2 = m2 - m1 assert m2 == m1 m1 *= 2
def test_m_transforms(self): v = Vector3D(1, 1, 0, 1) m = Matrix3D.get_shift_matrix(5, 5, 0) print "shift matrix:\n", m print "shifted vector:", m.v_dot(v) m = Matrix3D.get_scale_matrix(2, 2, 0) print "scale matrix:\n", m print "scaled vector:", m.v_dot(v)
def __init__(self): # Heading Vector along x Axis self._H = Vector3D(1, 0, 0) self._H.setName("Heading") # Left Vector along y Axis self._L = Vector3D(0, 1, 0) self._H.setName("Left") # Up Vector along z Axis self._U = Vector3D(0, 0, 1) self._H.setName("Up") # initial Positon = Origin self._Pos = Vector3D(0, 0, 0) self._H.setName("Pos") # initial rotation angle self._alpha = 90 # initial length of moving forward self._length = 100 self._axiom = ""
def divide(self, seg0, val0, val1): assert (seg0 <= 4 and seg0 >= 0) assert (val0 >= 0.0 and val0 <= 1.0), val0 assert (val1 >= 0.0 and val1 <= 1.0), val1 seg0 = (self.base + seg0) % 2 seg1 = (seg0 + 2) % 4 # Calcul des vecteurs des 2 segments vec0 = Vector3D(self.points[seg0], self.points[(seg0 + 1) % 4]) vec1 = Vector3D(self.points[(seg1 + 1) % 4], self.points[seg1]) # Calcul de la position des points pos0 = vec0 * val0 + self.points[seg0] pos1 = vec1 * val1 + self.points[(seg1 + 1) % 4] # Création des nouvelles formes points0 = [] points1 = [] if seg0 == 1: # Horizontal points0.append(self.points[0]) points0.append(self.points[1]) points0.append(pos0) points0.append(pos1) points1.append(pos1) points1.append(pos0) points1.append(self.points[2]) points1.append(self.points[3]) else: # Vertical points0.append(self.points[0]) points0.append(pos0) points0.append(pos1) points0.append(self.points[3]) points1.append(pos0) points1.append(self.points[1]) points1.append(self.points[2]) points1.append(pos1) return [cForm(points0), cForm(points1)]
def draw(self, screen): vec = Vector3D(100, 100, 0, 0) a = 0 # 1.5 ps = [] for i in range(len(self.points)): ps.append(self.points[i].getRotatedZ(a) + vec) for i in range(len(self.points)): screen.create_line(ps[i].getX(), ps[i].getY(), ps[(i + 1) % 4].getX(), ps[(i + 1) % 4].getY())
def __init__(self, posX, posY, lengthX = 0, lengthY = 0, operations = None): if type(posX) == list: points = posX cellularForm = cForm(points) self.root = Cellular(cellularForm, posY[0]) self.operations = posY self.rootActualisation = [self.root] else: points = [] points.append(Vector3D(posX, posY)) points.append(Vector3D(posX + lengthX, posY)) points.append(Vector3D(posX + lengthX, posY + lengthY)) points.append(Vector3D(posX, posY + lengthY)) cellularForm = cForm(points) self.root = Cellular(cellularForm, operations[0]) self.operations = operations self.rootActualisation = [self.root]
def __matrixMulVector(self, matrix, v): """ multiply 3D Vector with matrix :return: a Vector3D """ x = matrix[0][0] * v.getX() + matrix[0][1] * v.getY( ) + matrix[0][2] * v.getZ() y = matrix[1][0] * v.getX() + matrix[1][1] * v.getY( ) + matrix[1][2] * v.getZ() z = matrix[2][0] * v.getX() + matrix[2][1] * v.getY( ) + matrix[2][2] * v.getZ() return Vector3D(x, y, z)
def normalizeDivide(self, seg0, val0, val1): assert (4 >= seg0 >= 0) assert (0.0 <= val0 <= 1.0) assert (0.0 <= val1 <= 1.0) seg1 = (seg0 + 2) % 4 # Calcul des vecteurs des 2 segments vec0 = Vector3D(self.points[seg0], self.points[(seg0 + 1) % 4]) vec1 = Vector3D(self.points[seg1], self.points[(seg1 + 1) % 4]) # Normalization vec0N = vec0.copy() magn = vec0N.normalize() scalar = vec0N * vec1 # On recherche le plus petit coté par projection assert (scalar >= 0.0) if scalar <= 1.0: max = scalar val1 = magn * val1 / max else: vec1N = vec1.copy() magn2 = vec1N.normalize() vec1N.normalize() max = scalar * magn / magn2 val0 = magn2 * val0 / max # Calcul de la position des points pos0 = vec0 * val0 + self.points[seg0] pos1 = vec1 * val1 + self.points[seg1] # Création des nouvelles formes points0 = [] points1 = [] points0.append(self.points[(seg1 + 1) % 4], self.points[seg0], pos0, pos1) points1.append(self.points[(seg0 + 1) % 4], self.points[seg1], pos1, pos0) return [cForm(points0), cForm(points1)]
def setup_operation(self, op): ret = Oplist() # South wall loc = Location(self, Vector3D(-0.5, 0, -0.5)) loc.bbox = Vector3D(6, 5, -0.5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) loc = Location(self, Vector3D(7.5, 0, -0.5)) loc.bbox = Vector3D(2, 5, -0.5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) # West wall loc = Location(self, Vector3D(-0.5, 0, -0.5)) loc.bbox = Vector3D(0.5, 5, -8) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) # North wall loc = Location(self, Vector3D(-0.5, 0, -8)) loc.bbox = Vector3D(10, 5, -0.5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) # East wall loc = Location(self, Vector3D(9, 0, -0.5)) loc.bbox = Vector3D(0.5, 5, -8) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) return ret
def setup_operation(self, op): ret = Oplist() # South wall with door loc = Location(self, Vector3D(-0.5,-0.5,0)) loc.bbox = Vector3D(2,0.5,5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) loc = Location(self, Vector3D(3.5,-0.5,0)) loc.bbox = Vector3D(4,0.5,5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) # West wall loc = Location(self, Vector3D(-0.5,-0.5,0)) loc.bbox = Vector3D(0.5,8,5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) # North wall loc = Location(self, Vector3D(-0.5,7,0)) loc.bbox = Vector3D(8,0.5,5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) # East wall loc = Location(self, Vector3D(7,-0.5,0)) loc.bbox = Vector3D(0.5,8,5) ret.append(Operation("create",Entity(name='wall',parent='wall',location=loc),to=self)) return ret
def createEdge(p0, p1, pointToAdd=None): vec = None vec = p1.point - p0.point vec.normalize() edge = Edge(Vector3D(-vec.getY(), vec.getX())) p0.edges.append(edge) p1.edges.append(edge) if pointToAdd is not None: edge.addPoint(pointToAdd) return edge
def sound_talk_operation(self, original_op, op): talk_entity = op[0] if hasattr(talk_entity, "say"): say = talk_entity.say if sowee_pattern.match(say): if op.from_.location.parent != self.location.parent: return source = op.from_.location.coordinates distance = (self.location.coordinates - source).mag() if distance > 10: return destination = Location(self.location.parent) destination.velocity = Vector3D(0, 0, 0) return Operation("move", Entity(self.id, location=destination)) return NPCMind.sound_talk_operation(self, original_op, op)
def drawFill(self, screen): vec = Vector3D(100, 100, 0, 0) a = 0 # 1.5 ps = [] for i in range(len(self.points)): ps.append(self.points[i].getRotatedZ(a) + vec) screen.create_polygon(ps[0].getX(), ps[0].getY(), ps[1].getX(), ps[1].getY(), ps[2].getX(), ps[2].getY(), ps[3].getX(), ps[3].getY(), fill='red')
def setup_operation(self, op): ret = Oplist() # South wall with door loc = Location(self, Vector3D(-1, 1, 0)) loc.bbox = Vector3D(6, 0.5, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) loc = Location(self, Vector3D(9, 1, 0)) loc.bbox = Vector3D(4, 0.5, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) # West wall with door loc = Location(self, Vector3D(-1, 1, 0)) loc.bbox = Vector3D(0.5, 2, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) loc = Location(self, Vector3D(-1, 7, 0)) loc.bbox = Vector3D(0.5, 4, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) # North facing section loc = Location(self, Vector3D(-1, 10.5, 0)) loc.bbox = Vector3D(6, 0.5, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) # Remaining west facing section loc = Location(self, Vector3D(5, 7, 0)) loc.bbox = Vector3D(0.5, 10, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) # North wall loc = Location(self, Vector3D(5, 16.5, 0)) loc.bbox = Vector3D(8, 0.5, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) # East wall with door loc = Location(self, Vector3D(12.5, 1, 0)) loc.bbox = Vector3D(0.5, 4, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) loc = Location(self, Vector3D(12.5, 7, 0)) loc.bbox = Vector3D(0.5, 10, 5) ret.append( Operation("create", Entity(name='wall', parents=['wall'], location=loc), to=self)) return ret