Beispiel #1
0
def getPartialDerivatives(px, u, v, op, size):
    """
	Returns the partial derivatives of the image at the given pixel
	coordinate.

	Parameters:
		px ([]): A 1D list of pixel values from the original image.
		uv (integer tuple): A tuple containing two zero-indexed 
			values for sampling.
		op (value function): The operation to be used to get a value
			from a pixel.
		size((int)): A tuple containing the width and height of the
		image.
			
	Returns:
		a tuple of two vec3s, each containing the partial derivative
		along its
		axis.
	"""
    dy = (op(getPx(px, u, v, size)) * .01 -
          op(getPx(px, u, v - 1, size)) * .01)
    dx = (op(getPx(px, u, v, size)) * .01 -
          op(getPx(px, u - 1, v, size)) * .01)

    return (vec3(1.0, dx, 0), vec3(0, dy, 1.0))
def GetNumStepBySource(pos,coreconf):
    """
        Retourne le pas de temps où le son direct est censé rejoindre la position pos pour chaque source sonore
    """
    ret_tab={}
    if coreconf.const["ajouter_son_direct"]:
        for src in coreconf.sources_lst:
            if coreconf.const["temporel"]:
                dist=(vec3(pos)-vec3(src.pos)).length()
                ret_tab[src.id]=int(math.floor(dist/(coreconf.const["cel"]*coreconf.const["pasdetemps"])))
            else:
                ret_tab[src.id]=0
    return ret_tab
Beispiel #3
0
def project3D_on_2D(point3D, n, p):
    v = point3D - p
    perp_dist = v.dot(n)
    projected = point3D - perp_dist * n
    x = int(projected.x)
    y = int(projected.y)
    return vec3(x, y, 0)  # or just use projected if no integer casting
Beispiel #4
0
	def _get_local_quat(self):
		rot = self.get_fixed_attribute("r", default=vec3(0,0,0))
		qx = quat().fromAngleAxis(rot[0], (1, 0, 0))
		qy = quat().fromAngleAxis(rot[1], (0, 1, 0))
		qz = quat().fromAngleAxis(rot[2], (0, 0, 1))
		q = qz*qy*qx
		return q
Beispiel #5
0
	def getabsirot(self):
		root = self.getphysmaster().getParent()
		#print("getabsirot checking", root)
		ir = root.gettransformto(None, "inverse_initial_r").decompose()[1]
		if root.flipjoints:
			ir = ir.rotate(math.pi, vec3(1,0,0))
		return ir
Beispiel #6
0
    def __mul__(self, other):
        """Multiplication with a scalar or dot product.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a*2.0
        (2.0000, 1.0000, -3.6000)
        >>> print 2.0*a
        (2.0000, 1.0000, -3.6000)
        >>> print a*b
        -0.825
        """

        T = type(other)
        # vec3*scalar
        if T==types.FloatType or T==types.IntType or T==types.LongType:
            return vec3(self.x*other, self.y*other, self.z*other)
        # vec3*vec3
        if isinstance(other, vec3):
            return self.x*other.x + self.y*other.y + self.z*other.z
        # unsupported
        else:
            # Try to delegate the operation to the other operand
            if getattr(other,"__rmul__",None)!=None:
                return other.__rmul__(self)
            else:
                raise TypeError, "unsupported operand type for *"
Beispiel #7
0
    def __mul__(self, other):
        """Multiplication with a scalar or dot product.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a*2.0
        (2.0000, 1.0000, -3.6000)
        >>> print 2.0*a
        (2.0000, 1.0000, -3.6000)
        >>> print a*b
        -0.825
        """

        T = type(other)
        # vec3*scalar
        if T == types.FloatType or T == types.IntType or T == types.LongType:
            return vec3(self.x * other, self.y * other, self.z * other)
        # vec3*vec3
        if isinstance(other, vec3):
            return self.x * other.x + self.y * other.y + self.z * other.z
        # unsupported
        else:
            # Try to delegate the operation to the other operand
            if getattr(other, "__rmul__", None) != None:
                return other.__rmul__(self)
            else:
                raise TypeError, "unsupported operand type for *"
Beispiel #8
0
 def __pos__(self):
     """
     >>> a=vec3(3.0, 2.5, -1.8)
     >>> print +a
     (3.0000, 2.5000, -1.8000)
     """
     return vec3(+self.x, +self.y, +self.z)
 def _get_local_quat(self):
     rot = self.get_fixed_attribute("r", default=vec3(0, 0, 0))
     qx = quat().fromAngleAxis(rot[0], (1, 0, 0))
     qy = quat().fromAngleAxis(rot[1], (0, 1, 0))
     qz = quat().fromAngleAxis(rot[2], (0, 0, 1))
     q = qy * qz * qx
     return q
Beispiel #10
0
 def __pos__(self):
     """
     >>> a=vec3(3.0, 2.5, -1.8)
     >>> print +a
     (3.0000, 2.5000, -1.8000)
     """
     return vec3(+self.x, +self.y, +self.z)
Beispiel #11
0
    def __mod__(self, other):
        """Modulo (component wise)

        >>> a=vec3(3.0, 2.5, -1.8)
        >>> print a%2.0
        (1.0000, 0.5000, 0.2000)
        """
        T = type(other)
        # vec3%scalar
        if T == types.FloatType or T == types.IntType or T == types.LongType:
            return vec3(self.x % other, self.y % other, self.z % other)
        # vec3%vec3
        if isinstance(other, vec3):
            return vec3(self.x % other.x, self.y % other.y, self.z % other.z)
        # unsupported
        else:
            raise TypeError, "unsupported operand type for %"
Beispiel #12
0
    def __mod__(self, other):
        """Modulo (component wise)

        >>> a=vec3(3.0, 2.5, -1.8)
        >>> print a%2.0
        (1.0000, 0.5000, 0.2000)
        """
        T = type(other)
        # vec3%scalar
        if T==types.FloatType or T==types.IntType or T==types.LongType:
            return vec3(self.x%other, self.y%other, self.z%other)
        # vec3%vec3
        if isinstance(other, vec3):
            return vec3(self.x%other.x, self.y%other.y, self.z%other.z)
        # unsupported
        else:
            raise TypeError, "unsupported operand type for %"
Beispiel #13
0
    def __neg__(self):
        """Negation

        >>> a=vec3(3.0, 2.5, -1.8)
        >>> print -a
        (-3.0000, -2.5000, 1.8000)
        """
        return vec3(-self.x, -self.y, -self.z)
Beispiel #14
0
    def __neg__(self):
        """Negation

        >>> a=vec3(3.0, 2.5, -1.8)
        >>> print -a
        (-3.0000, -2.5000, 1.8000)
        """
        return vec3(-self.x, -self.y, -self.z)
Beispiel #15
0
 def _fixup(self):
     """Adjust the up vector so that it's orthogonal to the view direction."""
     n = unit(self.center - self.eye)
     v = cross(n, self.up)
     if v == vec3(0, 0, 0):
         print >> sys.stderr, "?? up vector (%g,%g,%g) parallel to view vector (%g,%g,%g)" % (
             self.up.x, self.up.y, self.up.z, n.x, n.y, n.z)
         v = find_basis(n)[0]
     self.up = unit(cross(v, n))
Beispiel #16
0
 def Transform(self, x, y, z):
     for c in self.cells:
         if c.contains(x, y, z):
             x += c.ix
             y += c.iy
             z += c.iz
             p = vec3(x, y, z)
             return (dot(p, self.n1), dot(p, self.n2), dot(p, self.n3))
     raise RuntimeError, "(%g,%g,%g) not contained in any cell" % (x, y, z)
Beispiel #17
0
    def normalize(self):
        """Return normalized vector.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a.normalize()
        (0.4719, 0.2360, -0.8495)
        """

        nlen = 1.0/math.sqrt(self*self)
        return vec3(self.x*nlen, self.y*nlen, self.z*nlen)
Beispiel #18
0
    def normalize(self):
        """Return normalized vector.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a.normalize()
        (0.4719, 0.2360, -0.8495)
        """

        nlen = 1.0 / math.sqrt(self * self)
        return vec3(self.x * nlen, self.y * nlen, self.z * nlen)
Beispiel #19
0
	def isortho(self):
		l = [vec4(1,0,0,1), vec4(0,1,0,1), vec4(0,0,1,1)]
		p = vec3(self.get_world_translation()[0:3])
		t = self.get_world_transform()
		for i in range(3):
			l[i] = t * l[i]
		for i in range(3):
			for j in range(3):
				if i != j:
					k = ((i+1)^(j+1))-1
					u = vec3(l[i][0:3])-p
					v = vec3(l[j][0:3])-p
					w = vec3(l[k][0:3])-p
					s = u.cross(v)
					a = abs((s * w) / (s.length()*w.length()))
					if a <= 0.99:
						#print(i,j,k,u,v,w,s)
						#print("cos(a) of %s is %f." % (self.getFullName(), a))
						return False
		return True
Beispiel #20
0
    def angle(self, other):
        """Return angle (in radians) between self and other.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a.angle(b)
        1.99306755584
        """

        other = vec3(other)
        return math.acos((self * other) / (abs(self) * abs(other)))
 def isortho(self):
     l = [vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1)]
     p = vec3(self.get_world_translation()[0:3])
     t = self.get_world_transform()
     for i in range(3):
         l[i] = t * l[i]
     for i in range(3):
         for j in range(3):
             if i != j:
                 k = ((i + 1) ^ (j + 1)) - 1
                 u = vec3(l[i][0:3]) - p
                 v = vec3(l[j][0:3]) - p
                 w = vec3(l[k][0:3]) - p
                 s = u.cross(v)
                 a = abs((s * w) / (s.length() * w.length()))
                 if a <= 0.99:
                     #print(i,j,k,u,v,w,s)
                     #print("cos(a) of %s is %f." % (self.getFullName(), a))
                     return False
     return True
Beispiel #22
0
    def refract(self, N, eta):
        """Return the transmitted vector.

        N is the surface normal which has to be of unit length.
        eta is the relative index of refraction. If the returned
        vector is zero then there is no transmitted light because
        of total internal reflection.
        
        >>> a=vec3(1.0, -1.5, 0.8)
        >>> print a.refract(vec3(0,1,0), 1.33)
        (1.3300, -1.7920, 1.0640)
        """

        N = vec3(N)
        dot = self * N
        k = 1.0 - eta * eta * (1.0 - dot * dot)
        if k < 0:
            return vec3(0.0, 0.0, 0.0)
        else:
            return eta * self - (eta * dot + math.sqrt(k)) * N
Beispiel #23
0
    def refract(self, N, eta):
        """Return the transmitted vector.

        N is the surface normal which has to be of unit length.
        eta is the relative index of refraction. If the returned
        vector is zero then there is no transmitted light because
        of total internal reflection.
        
        >>> a=vec3(1.0, -1.5, 0.8)
        >>> print a.refract(vec3(0,1,0), 1.33)
        (1.3300, -1.7920, 1.0640)
        """

        N = vec3(N)
        dot = self*N
        k   = 1.0 - eta*eta*(1.0 - dot*dot)
        if k<0:
            return vec3(0.0,0.0,0.0)
        else:
            return eta*self - (eta*dot + math.sqrt(k))*N
 def _get_local_ar(self):
     ra = vec3(self.get_fixed_attribute("ra", default=vec3(0, 0, 0)))
     x, y, z = mat4.identity(), mat4.identity(), mat4.identity()
     x[1, 1] = +math.cos(ra.x)
     x[1, 2] = +math.sin(ra.x)
     x[2, 1] = -math.sin(ra.x)
     x[2, 2] = +math.cos(ra.x)
     y[0, 0] = +math.cos(ra.y)
     y[0, 2] = -math.sin(ra.y)
     y[2, 0] = +math.sin(ra.y)
     y[2, 2] = +math.cos(ra.y)
     z[0, 0] = +math.cos(ra.z)
     z[0, 1] = +math.sin(ra.z)
     z[1, 0] = -math.sin(ra.z)
     z[1, 1] = +math.cos(ra.z)
     ro = self.getAttrValue("ro", "ro", "int", default=0)
     rotorder = ["x*y*z", "y*z*x", "z*x*y", "x*z*y", "y*x*z", "z*y*x"]
     mra = eval(rotorder[ro])
     #print("Result of rot:", mra*vec4(0,1,0,0))
     return mra
Beispiel #25
0
    def angle(self, other):
        """Return angle (in radians) between self and other.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a.angle(b)
        1.99306755584
        """

        other = vec3(other)
        return math.acos((self*other) / (abs(self)*abs(other)))
Beispiel #26
0
	def _get_local_ar(self):
		ra = vec3(self.get_fixed_attribute("ra", default=vec3(0,0,0)))
		x, y, z = mat4.identity(), mat4.identity(), mat4.identity()
		x[1,1] = +math.cos(ra.x)
		x[1,2] = +math.sin(ra.x)
		x[2,1] = -math.sin(ra.x)
		x[2,2] = +math.cos(ra.x)
		y[0,0] = +math.cos(ra.y)
		y[0,2] = -math.sin(ra.y)
		y[2,0] = +math.sin(ra.y)
		y[2,2] = +math.cos(ra.y)
		z[0,0] = +math.cos(ra.z)
		z[0,1] = +math.sin(ra.z)
		z[1,0] = -math.sin(ra.z)
		z[1,1] = +math.cos(ra.z)
		ro = self.getAttrValue("ro", "ro", "int", default=0)
		rotorder = ["x*y*z", "y*z*x", "z*x*y", "x*z*y", "y*x*z", "z*y*x"]
		mra = eval(rotorder[ro])
		#print("Result of rot:", mra*vec4(0,1,0,0))
		return mra
Beispiel #27
0
    def __init__(self,
                 width,
                 height,
                 center=(0, 0, 0),
                 eye=(0, 0, 1),
                 up=(0, 1, 0)):
        self.width = width
        self.height = height
        self.z = 1.0

        if eye == center:
            print >> sys.stderr, "?? eye and center position coincide"
            eye = (center[0], center[1], center[2] + 1)
        self.center = vec3(center)
        self.eye = vec3(eye)
        self.up = vec3(up)
        self._fixup()
        self.mx = None
        self.my = None
        self.rotating_graphic = None
        self.translating_graphic = None
Beispiel #28
0
 def __init__(self,filename, layers):
     fileText = getFileText( filename )
     textLines = getTextLines( fileText )
     self.last_pos = vec3()
     self.layers = layers
     self.layer = None
     self.thread = None
     self.skeinforge = 0
     self.max_z = -9999999999
     for line in textLines:
         self.parseLine( line )
     self.newLayer()
Beispiel #29
0
    def reflect(self, N):
        """Return the reflection vector.

        N is the surface normal which has to be of unit length.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a.reflect(vec3(1,0,1))
        (2.6000, 0.5000, -0.2000)
        """

        N = vec3(N)
        return self - 2.0*(self*N)*N
Beispiel #30
0
    def __add__(self, other):
        """Vector addition.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a+b
        (0.7000, 1.2500, -1.3000)
        """
        if isinstance(other, vec3):
            return vec3(self.x+other.x, self.y+other.y, self.z+other.z)
        else:
            raise TypeError, "unsupported operand type for +"
Beispiel #31
0
 def linearMove( self, splitLine ):
     if self.thread != None:
         pos = vec3().getFromVec3(self.last_pos)
         self.setPointComponent( pos, splitLine )
         if pos.z > self.max_z:
             self.newLayer()
             self.max_z = pos.z
         if pos.z < self.last_pos.z:
             self.newThread()
         if self.skeinforge or pos.z < self.max_z:
             self.thread.append(pos)
         self.last_pos = pos
Beispiel #32
0
    def reflect(self, N):
        """Return the reflection vector.

        N is the surface normal which has to be of unit length.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a.reflect(vec3(1,0,1))
        (2.6000, 0.5000, -0.2000)
        """

        N = vec3(N)
        return self - 2.0 * (self * N) * N
Beispiel #33
0
    def __sub__(self, other):
        """Vector subtraction.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a-b
        (1.3000, -0.2500, -2.3000)
        """
        if isinstance(other, vec3):
            return vec3(self.x - other.x, self.y - other.y, self.z - other.z)
        else:
            raise TypeError, "unsupported operand type for -"
Beispiel #34
0
    def __sub__(self, other):
        """Vector subtraction.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a-b
        (1.3000, -0.2500, -2.3000)
        """
        if isinstance(other, vec3):
            return vec3(self.x-other.x, self.y-other.y, self.z-other.z)
        else:
            raise TypeError, "unsupported operand type for -"
Beispiel #35
0
    def __add__(self, other):
        """Vector addition.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> print a+b
        (0.7000, 1.2500, -1.3000)
        """
        if isinstance(other, vec3):
            return vec3(self.x + other.x, self.y + other.y, self.z + other.z)
        else:
            raise TypeError, "unsupported operand type for +"
Beispiel #36
0
def make_images(layers):
    palette = []
    for i in range(256):
        #resistor colour codes
        if i == 1:
            palette.extend((134, 100, 57))  # brown
        elif i == 2:
            palette.extend((255, 0, 0))  # red
        elif i == 3:
            palette.extend((218, 90, 35))  # orange
        elif i == 4:
            palette.extend((255, 255, 0))  # yellow
        elif i == 5:
            palette.extend((0, 255, 0))  # green
        elif i == 6:
            palette.extend((0, 0, 255))  # blue
        elif i == 7:
            palette.extend((255, 0, 255))  # purple
        else:
            palette.extend((i, i, i))  # shades of grey
    cube = bounding_cube(layers)
    scale = 10
    x0 = int(cube[0].x) - 1
    y0 = int(cube[0].y) - 1
    width = int(round(cube[1].x - x0) + 1) * scale
    height = int(round(cube[1].y - y0) + 1) * scale
    last_pos = None
    images = []
    for layer in layers:
        image = Image.new('P', (width, height), 255)
        image.putpalette(palette)
        draw = ImageDraw.Draw(image)
        segment = 0
        for thread in layer:
            if last_pos != None:
                draw.line((((last_pos.x - x0) * scale, height -
                            (last_pos.y - y0) * scale),
                           ((thread[0].x - x0) * scale, height -
                            (thread[0].y - y0) * scale)),
                          fill=128)
            last_pos = vec3().getFromVec3(thread[0])
            for point in thread[1:]:
                draw.line((((last_pos.x - x0) * scale, height -
                            (last_pos.y - y0) * scale),
                           ((point.x - x0) * scale, height -
                            (point.y - y0) * scale)),
                          fill=segment % 8)
                last_pos.getFromVec3(point)
            segment = segment + 1
        images.append(image)
    return images
Beispiel #37
0
    def __div__(self, other):
        """Division by scalar

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a/2.0
        (0.5000, 0.2500, -0.9000)
        """
        T = type(other)
        # vec3/scalar
        if T==types.FloatType or T==types.IntType or T==types.LongType:
            return vec3(self.x/other, self.y/other, self.z/other)
        # unsupported
        else:
            raise TypeError, "unsupported operand type for /"
Beispiel #38
0
    def ortho(self):
        """Returns an orthogonal vector.

        Returns a vector that is orthogonal to self (where
        self*self.ortho()==0).

        >>> a=vec3(1.0, -1.5, 0.8)
        >>> print round(a*a.ortho(),8)
        0.0
        """

        x=abs(self.x)
        y=abs(self.y)
        z=abs(self.z)
        # Is z the smallest element? Then use x and y
        if z<=x and z<=y:
            return vec3(-self.y, self.x, 0.0)
        # Is y smallest element? Then use x and z
        elif y<=x and y<=z:
            return vec3(-self.z, 0.0, self.x)
        # x is smallest
        else:
            return vec3(0.0, -self.z, self.y)
Beispiel #39
0
    def ortho(self):
        """Returns an orthogonal vector.

        Returns a vector that is orthogonal to self (where
        self*self.ortho()==0).

        >>> a=vec3(1.0, -1.5, 0.8)
        >>> print round(a*a.ortho(),8)
        0.0
        """

        x = abs(self.x)
        y = abs(self.y)
        z = abs(self.z)
        # Is x the smallest element?
        if x < y and x < z:
            return vec3(0.0, -self.z, self.y)
        # Is y smallest element?
        elif y < z:
            return vec3(-self.z, 0.0, self.x)
        # z is smallest
        else:
            return vec3(-self.y, self.x, 0.0)
Beispiel #40
0
def bounding_cube(layers):
    min_x = 999999
    min_y = 999999
    min_z = 999999
    max_x = -999999
    max_y = -999999
    max_z = -999999
    for layer in layers:
        for thread in layer:
            for point in thread:
                if point.x > max_x:
                    max_x = point.x
                if point.y > max_y:
                    max_y = point.y
                if point.z > max_z:
                    max_z = point.z
                if point.x < min_x:
                    min_x = point.x
                if point.y < min_y:
                    min_y = point.y
                if point.z < min_z:
                    min_z = point.z
    return vec3().getFromXYZ(min_x, min_y, min_z), vec3().getFromXYZ(max_x, max_y, max_z)
Beispiel #41
0
    def __truediv__(self, other):
        """Division by scalar

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a/2.0
        (0.5000, 0.2500, -0.9000)
        """
        T = type(other)
        # vec3/scalar
        if T in [int, float, long]:
            return vec3(self.x / other, self.y / other, self.z / other)
        # unsupported
        else:
            raise TypeError("unsupported operand type for /")
Beispiel #42
0
    def __div__(self, other):
        """Division by scalar

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a/2.0
        (0.5000, 0.2500, -0.9000)
        """
        T = type(other)
        # vec3/scalar
        if T == types.FloatType or T == types.IntType or T == types.LongType:
            return vec3(self.x / other, self.y / other, self.z / other)
        # unsupported
        else:
            raise TypeError, "unsupported operand type for /"
Beispiel #43
0
    def __truediv__(self, other):
        """Division by scalar

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> print a/2.0
        (0.5000, 0.2500, -0.9000)
        """
        T = type(other)
        # vec3/scalar
        if T in [int, float, long]:
            return vec3(self.x/other, self.y/other, self.z/other)
        # unsupported
        else:
            raise TypeError("unsupported operand type for /")
Beispiel #44
0
    def ortho(self):
        """Returns an orthogonal vector.

        Returns a vector that is orthogonal to self (where
        self*self.ortho()==0).

        >>> a=vec3(1.0, -1.5, 0.8)
        >>> print round(a*a.ortho(),8)
        0.0
        """

        x=abs(self.x)
        y=abs(self.y)
        z=abs(self.z)
        # Is x the smallest element? 
        if x<y and x<z:
            return vec3(0.0, -self.z, self.y)
        # Is y smallest element?
        elif y<z:
            return vec3(-self.z, 0.0, self.x)
        # z is smallest
        else:
            return vec3(-self.y, self.x, 0.0)
Beispiel #45
0
    def ortho(self):
        """Returns an orthogonal vector.

        Returns a vector that is orthogonal to self (where
        self*self.ortho()==0).

        >>> a=vec3(1.0, -1.5, 0.8)
        >>> print round(a*a.ortho(),8)
        0.0
        """

        x = abs(self.x)
        y = abs(self.y)
        z = abs(self.z)
        # Is z the smallest element? Then use x and y
        if z <= x and z <= y:
            return vec3(-self.y, self.x, 0.0)
        # Is y smallest element? Then use x and z
        elif y <= x and y <= z:
            return vec3(-self.z, 0.0, self.x)
        # x is smallest
        else:
            return vec3(0.0, -self.z, self.y)
Beispiel #46
0
def bounding_cube(layers):
    min_x = 999999
    min_y = 999999
    min_z = 999999
    max_x = -999999
    max_y = -999999
    max_z = -999999
    for layer in layers:
        for thread in layer:
            for point in thread:
                if point.x > max_x:
                    max_x = point.x
                if point.y > max_y:
                    max_y = point.y
                if point.z > max_z:
                    max_z = point.z
                if point.x < min_x:
                    min_x = point.x
                if point.y < min_y:
                    min_y = point.y
                if point.z < min_z:
                    min_z = point.z
    return vec3().getFromXYZ(min_x, min_y,
                             min_z), vec3().getFromXYZ(max_x, max_y, max_z)
Beispiel #47
0
    def cross(self, other):
        """Cross product.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> c=a.cross(b)
        >>> print c
        (1.6000, 0.0400, 0.9000)
        """

        if isinstance(other, vec3):
            return vec3(self.y * other.z - self.z * other.y,
                        self.z * other.x - self.x * other.z,
                        self.x * other.y - self.y * other.x)
        else:
            raise TypeError, "unsupported operand type for cross()"
Beispiel #48
0
def centerverts(group, bodies, verbose):
	for shape in group:
		vtx = shape.get_fixed_attribute("rgvtx", optional=True)
		if not vtx:
			continue
		mesh = shape.getParent();
		if not mesh.get_fixed_attribute("center_vertices", optional=True):
			continue
		# Center around physics position, by removing translational offset for each vertex.
		phys,physidx,q,p,s = mesh.get_final_mesh_transform(None, bodies, verbose)
		p = quat(q).rotateVec(p)
		if verbose:
			print("Offsetting %s's %i vertices around %s, moving them %s." % (mesh.getName(), len(vtx)/3, phys.getName(), str(p)))
		for idx in range(0, len(vtx), 3):
			v = vec3(vtx[idx:idx+3]) + p
			vtx[idx:idx+3] = v[:3]
Beispiel #49
0
    def cross(self, other):
        """Cross product.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> c=a.cross(b)
        >>> print c
        (1.6000, 0.0400, 0.9000)
        """
        
        if isinstance(other, vec3):
            return vec3(self.y*other.z-self.z*other.y,
                        self.z*other.x-self.x*other.z,
                        self.x*other.y-self.y*other.x)
        else:
            raise TypeError, "unsupported operand type for cross()"
Beispiel #50
0
    def crossProduct(first, second):
        """Cross product.

        >>> a=vec3(1.0, 0.5, -1.8)
        >>> b=vec3(-0.3, 0.75, 0.5)
        >>> c=vec3.cross(a, b)
        >>> print c
        (1.6000, 0.0400, 0.9000)
        """
        
        if isinstance(first, vec3) and isinstance(second, vec3):
            return vec3(first.y*second.z-first.z*second.y,
                        first.z*second.x-first.x*second.z,
                        first.x*second.y-first.y*second.x)
        else:
            raise TypeError("unsupported operand type for cross()")
Beispiel #51
0
def make_images(layers):
    palette = []
    for i in range(256):
        #resistor colour codes
        if i == 1:
            palette.extend((134, 100,  57)) # brown
        elif i == 2:
            palette.extend((255,   0,   0)) # red
        elif i == 3:
            palette.extend((218,  90,  35)) # orange
        elif i == 4:
            palette.extend((255, 255,   0)) # yellow
        elif i == 5:
            palette.extend((  0, 255,   0)) # green
        elif i == 6:
            palette.extend((  0,   0, 255)) # blue
        elif i == 7:
            palette.extend((255,   0, 255)) # purple
        else:
            palette.extend((i, i, i))       # shades of grey
    cube = bounding_cube(layers)
    scale = 10
    x0 = int(cube[0].x) - 1
    y0 = int(cube[0].y) - 1
    width  = int(round(cube[1].x - x0) + 1) * scale
    height = int(round(cube[1].y - y0) + 1) * scale
    last_pos = None
    images = []
    for layer in layers:
        image = Image.new('P', (width, height), 255)
        image.putpalette(palette)
        draw = ImageDraw.Draw(image)
        segment = 0
        for thread in layer:
            if last_pos != None:
                draw.line(((( last_pos.x - x0) * scale, height - ( last_pos.y - y0) * scale),
                           ((thread[0].x - x0) * scale, height - (thread[0].y - y0) * scale)), fill = 128)
            last_pos = vec3().getFromVec3(thread[0])
            for point in thread[1:]:
                draw.line((((last_pos.x - x0) * scale, height - (last_pos.y - y0) * scale),
                          ( (point.x    - x0) * scale, height - (point.y    - y0) * scale)), fill = segment % 8)
                last_pos.getFromVec3(point)
            segment = segment + 1
        images.append(image)
    return images
Beispiel #52
0
 def on_mouse_press(self, mx, my, button=mouse.LEFT, modifiers=0):
     x, y, z, r = self._getbasis()
     shift, ctrl = (modifiers & key.MOD_SHIFT, modifiers & key.MOD_CTRL)
     if button == mouse.LEFT:
         self.rotating_graphic = [
             vec3(self.center),
             vec3(x),
             vec3(y),
             vec3(z),
             float(r)
         ]
     elif button == mouse.RIGHT:
         self.translating_graphic = [
             vec3(self.center),
             vec3(x),
             vec3(y),
             vec3(z),
             float(r)
         ]
     self.mx = mx
     self.my = my
Beispiel #53
0
	def _get_local_s(self):
		return self.get_fixed_attribute("s", default=vec3(1,1,1))
Beispiel #54
0
    def __init__(self, *args):
        """Constructor.

        There are several possibilities how to initialize a vector:

        v = vec3()       -> v = <0,0,0>
        v = vec3(a)      -> v = <a,a,a>
        v = vec3(x,y)    -> v = <x,y,0>
        v = vec3(x,y,z)  -> v = <x,y,z>

        Note that specifying just one value sets all three components to
        that value (except when that single value is a another vec3, then
        that vector is copied).

        Additionally you can wrap those values in a list or a tuple or
        specify them as a string:

        v = vec3([1,2,3]) -> v = <1,2,3>
        v = vec3("4,5")   -> v = <4,5,0>        
        """

        if len(args) == 0:
            self.x, self.y, self.z = (0.0, 0.0, 0.0)

        elif len(args) == 1:
            T = type(args[0])
            # scalar
            if T == types.FloatType or T == types.IntType or T == types.LongType:
                self.x, self.y, self.z = (args[0], args[0], args[0])
            # vec3
            elif isinstance(args[0], vec3):
                self.x, self.y, self.z = args[0]
            # Tuple/List
            elif T == types.TupleType or T == types.ListType:
                if len(args[0]) == 0:
                    self.x = self.y = self.z = 0.0
                elif len(args[0]) == 1:
                    self.x = self.y = self.z = args[0][0]
                elif len(args[0]) == 2:
                    self.x, self.y = args[0]
                    self.z = 0.0
                elif len(args[0]) == 3:
                    self.x, self.y, self.z = args[0]
                else:
                    raise TypeError, "vec3() takes at most 3 arguments"
            # String
            elif T == types.StringType:
                s = args[0].replace(",", " ").replace("  ",
                                                      " ").strip().split(" ")
                if s == [""]:
                    s = []
                f = map(lambda x: float(x), s)
                dummy = vec3(f)
                self.x, self.y, self.z = dummy
            # error
            else:
                raise TypeError, "vec3() arg can't be converted to vec3"

        elif len(args) == 2:
            self.x, self.y, self.z = (args[0], args[1], 0.0)

        elif len(args) == 3:
            self.x, self.y, self.z = args

        else:
            raise TypeError, "vec3() takes at most 3 arguments"
Beispiel #55
0
	def _pointup_adjust_mat(self):
		if hasattr(self, "pointup") and self.pointup:
			# Some primitives have different orientation in the editor compared to
			# the runtime environment (Maya along Y-axis, RGE along Z-axis).
			return mat4.rotation(-math.pi/2, vec3(1,0,0))
		return mat4(1)
Beispiel #56
0
	def _get_local_rpivot(self):
		return self.get_fixed_attribute("rp", default=vec3(0,0,0))
Beispiel #57
0
 def ang0(self):
     C = vec3(1.0, 0.0, 0.0)
     try:
         return self.angle(C)
     except:
         return 0.0
Beispiel #58
0
    def __init__(self, *args):
        """Constructor.

        There are several possibilities how to initialize a vector:

        v = vec3()       -> v = <0,0,0>
        v = vec3(a)      -> v = <a,a,a>
        v = vec3(x,y)    -> v = <x,y,0>
        v = vec3(x,y,z)  -> v = <x,y,z>

        Note that specifying just one value sets all three components to
        that value (except when that single value is a another vec3, then
        that vector is copied).

        Additionally you can wrap those values in a list or a tuple or
        specify them as a string:

        v = vec3([1,2,3]) -> v = <1,2,3>
        v = vec3("4,5")   -> v = <4,5,0>        
        """
        
        if len(args)==0:
            self.x, self.y, self.z = (0.0, 0.0, 0.0)

        elif len(args)==1:
            T = type(args[0])
            # scalar
            if T==types.FloatType or T==types.IntType or T==types.LongType:
                f = float(args[0])
                self.x, self.y, self.z = (f, f, f)
            # vec3  
            elif isinstance(args[0], vec3):
                self.x, self.y, self.z = args[0]
            # Tuple/List
            elif T==types.TupleType or T==types.ListType:
                if len(args[0])==0:
                    self.x = self.y = self.z = 0.0
                elif len(args[0])==1:
                    self.x = self.y = self.z = float(args[0][0])
                elif len(args[0])==2:
                    self.x = float(args[0][0])
                    self.y = float(args[0][1])
                    self.z = 0.0
                elif len(args[0])==3:
                    x,y,z = args[0]
                    self.x = float(x)
                    self.y = float(y)
                    self.z = float(z)
                else:
                    raise TypeError, "vec3() takes at most 3 arguments"
            # String
            elif T==types.StringType:
                s=args[0].replace(","," ").replace("  "," ").strip().split(" ")
                if s==[""]:
                    s=[]
                f=map(lambda x: float(x), s)
                dummy = vec3(f)
                self.x, self.y, self.z = dummy
            # error
            else:
                raise TypeError,"vec3() arg can't be converted to vec3"

        elif len(args)==2:
            self.x, self.y, self.z = (float(args[0]), float(args[1]), 0.0)
            
        elif len(args)==3:
            x,y,z = args
            self.x = float(x)
            self.y = float(y)
            self.z = float(z)

        else:
            raise TypeError, "vec3() takes at most 3 arguments"