minor PEP8 cleanup (" -> ')
This commit is contained in:
parent
6416b66f6e
commit
61fb3b1044
18 changed files with 456 additions and 456 deletions
|
@ -8,7 +8,7 @@ import sys
|
|||
try:
|
||||
from .cython_m import mat4, vec3
|
||||
except ImportError:
|
||||
print("!!! [%s] unable to load native implementation, using python instead" %(__name__), file=sys.stderr)
|
||||
print('!!! [%s] unable to load native implementation, using python instead' %(__name__), file=sys.stderr)
|
||||
from .python_m import mat4, vec3
|
||||
|
||||
del sys
|
||||
|
|
110
m/cython_m.pyx
110
m/cython_m.pyx
|
@ -1,11 +1,11 @@
|
|||
#! /bin/env python3
|
||||
# encoding: utf-8
|
||||
|
||||
"""
|
||||
'''
|
||||
Vector And Matrix Math
|
||||
|
||||
TODO some nice description
|
||||
"""
|
||||
'''
|
||||
|
||||
from libc.stdlib cimport malloc, realloc, free
|
||||
from libc.stdint cimport uint8_t, uint16_t, uint64_t
|
||||
|
@ -26,7 +26,7 @@ class GeneralError(Exception):
|
|||
return self.description
|
||||
|
||||
cdef class mat4:
|
||||
"""
|
||||
'''
|
||||
A float 4x4 matrix.
|
||||
|
||||
All arrays are column-major, i.e. OpenGL style:
|
||||
|
@ -37,30 +37,30 @@ cdef class mat4:
|
|||
3 7 11 15
|
||||
|
||||
The matrix implements stacking useful for graphics.
|
||||
"""
|
||||
'''
|
||||
|
||||
def __cinit__(mat4 self):
|
||||
to_alloc = 16 * sizeof(float)
|
||||
self.stack = <float *>malloc(to_alloc)
|
||||
|
||||
if not self.stack:
|
||||
raise MemoryError("Unable to malloc %d B for mat4." %(to_alloc))
|
||||
raise MemoryError('Unable to malloc %d B for mat4.' %(to_alloc))
|
||||
|
||||
self.m = self.stack
|
||||
self.size = 1
|
||||
|
||||
def _debug(mat4 self):
|
||||
print("--- self.stack = %d" %(<uint64_t>self.stack))
|
||||
print("--- self.m = %d (+%d)" %(<uint64_t>self.m, self.m - self.stack))
|
||||
print("--- self.size = %d" %(self.size))
|
||||
print('--- self.stack = %d' %(<uint64_t>self.stack))
|
||||
print('--- self.m = %d (+%d)' %(<uint64_t>self.m, self.m - self.stack))
|
||||
print('--- self.size = %d' %(self.size))
|
||||
|
||||
def __init__(mat4 self, *args):
|
||||
"""
|
||||
'''
|
||||
Create a ma4t.
|
||||
|
||||
Accepts any number of parameters between 0 and 16 to fill the
|
||||
matrix from the upper left corner going down (column-wise).
|
||||
"""
|
||||
'''
|
||||
|
||||
length = len(args)
|
||||
|
||||
|
@ -69,7 +69,7 @@ cdef class mat4:
|
|||
length = len(args)
|
||||
|
||||
if length > 16:
|
||||
raise MathError("Attempt to initialize a mat4 with %d arguments." %(length))
|
||||
raise MathError('Attempt to initialize a mat4 with %d arguments.' %(length))
|
||||
|
||||
self.load_from(args)
|
||||
|
||||
|
@ -89,7 +89,7 @@ cdef class mat4:
|
|||
matrices = length//16
|
||||
|
||||
if not matrices*16 == length:
|
||||
raise GeneralError("mat4 __setstate__ got %d floats as a state" %(length))
|
||||
raise GeneralError('mat4 __setstate__ got %d floats as a state' %(length))
|
||||
|
||||
self.m = self.stack
|
||||
|
||||
|
@ -104,20 +104,20 @@ cdef class mat4:
|
|||
|
||||
def __getitem__(mat4 self, int i):
|
||||
if i > 16 or i < 0:
|
||||
raise IndexError("element index out of range(16)")
|
||||
raise IndexError('element index out of range(16)')
|
||||
|
||||
return self.m[i]
|
||||
|
||||
def __setitem__(self, int i, value):
|
||||
if i > 16 or i < 0:
|
||||
raise IndexError("element index out of range(16)")
|
||||
raise IndexError('element index out of range(16)')
|
||||
|
||||
self.m[i] = value
|
||||
|
||||
def push(mat4 self):
|
||||
"""
|
||||
'''
|
||||
Push the current matrix into the stack and load up an empty one (a zero matrix)
|
||||
"""
|
||||
'''
|
||||
|
||||
# self.m points to the current matrix
|
||||
# self.stack points to the first matrix
|
||||
|
@ -136,7 +136,7 @@ cdef class mat4:
|
|||
if tmp:
|
||||
self.stack = tmp
|
||||
else:
|
||||
raise MemoryError("Unable to malloc %d B for mat4." %(to_alloc))
|
||||
raise MemoryError('Unable to malloc %d B for mat4.' %(to_alloc))
|
||||
|
||||
# advance the pointer to the new one
|
||||
self.m = self.stack + 16 * used
|
||||
|
@ -149,12 +149,12 @@ cdef class mat4:
|
|||
self.m[i] = old_m[i]
|
||||
|
||||
def pop(mat4 self):
|
||||
"""
|
||||
'''
|
||||
Pop a matrix from the stack.
|
||||
"""
|
||||
'''
|
||||
|
||||
if self.m == self.stack:
|
||||
raise IndexError("pop from an empty stack")
|
||||
raise IndexError('pop from an empty stack')
|
||||
|
||||
self.m -= 16
|
||||
|
||||
|
@ -167,14 +167,14 @@ cdef class mat4:
|
|||
return L
|
||||
|
||||
def load_from(mat4 self, L):
|
||||
"""
|
||||
'''
|
||||
Fill the current matrix from a either a list of values, column-major,
|
||||
or another matrix. This method doesn't modify the stack, only the
|
||||
current matrix is read and modified.
|
||||
|
||||
If the number of values isn't 16, it will be padded to 16 by zeros.
|
||||
If it's larger, GeneralError will be raised.
|
||||
"""
|
||||
'''
|
||||
|
||||
if isinstance(L, mat4):
|
||||
L = L.get_list()
|
||||
|
@ -183,7 +183,7 @@ cdef class mat4:
|
|||
length = len(L)
|
||||
|
||||
if length > 16:
|
||||
raise GeneralError("supplied list is longer than 16")
|
||||
raise GeneralError('supplied list is longer than 16')
|
||||
|
||||
for i in range(16):
|
||||
if i < length:
|
||||
|
@ -192,13 +192,13 @@ cdef class mat4:
|
|||
self.m[i] = 0.0
|
||||
|
||||
def zero(mat4 self):
|
||||
"""Fill the matrix with zeroes."""
|
||||
'''Fill the matrix with zeroes.'''
|
||||
|
||||
for i in range(16):
|
||||
self.m[i] = 0.0
|
||||
|
||||
def identity(mat4 self):
|
||||
"""Make the matrix an identity."""
|
||||
'''Make the matrix an identity.'''
|
||||
|
||||
self.zero()
|
||||
|
||||
|
@ -208,7 +208,7 @@ cdef class mat4:
|
|||
self.m[15] = 1.0
|
||||
|
||||
def transpose(mat4 self):
|
||||
"""Transpose the matrix."""
|
||||
'''Transpose the matrix.'''
|
||||
|
||||
cdef float tmp
|
||||
|
||||
|
@ -237,7 +237,7 @@ cdef class mat4:
|
|||
self.m[9] = tmp
|
||||
|
||||
def invert(mat4 self):
|
||||
"""Invert the matrix."""
|
||||
'''Invert the matrix.'''
|
||||
|
||||
cdef float tmp[16]
|
||||
cdef float det
|
||||
|
@ -251,7 +251,7 @@ cdef class mat4:
|
|||
|
||||
# epsilon pulled straight out of Uranus
|
||||
if det < 0.00005 and det > -0.00005:
|
||||
print("det=%.1f" %(det))
|
||||
print('det=%.1f' %(det))
|
||||
return
|
||||
|
||||
tmp[1] = -self.m[1]*self.m[10]*self.m[15] + self.m[1]*self.m[11]*self.m[14] + self.m[9]*self.m[2]*self.m[15] - self.m[9]*self.m[3]*self.m[14] - self.m[13]*self.m[2]*self.m[11] + self.m[13]*self.m[3]*self.m[10]
|
||||
|
@ -286,11 +286,11 @@ cdef class mat4:
|
|||
self.m[15] = tmp[15] * det
|
||||
|
||||
def mulm(mat4 self, mat4 B, bint inplace=False):
|
||||
"""
|
||||
'''
|
||||
Return a matrix that is the result of multiplying this matrix by another.
|
||||
|
||||
M = self * mat4 B
|
||||
"""
|
||||
'''
|
||||
|
||||
cdef uint8_t i
|
||||
cdef mat4 tmp = mat4()
|
||||
|
@ -319,11 +319,11 @@ cdef class mat4:
|
|||
return tmp
|
||||
|
||||
def mulv(mat4 self, vec3 v):
|
||||
"""
|
||||
'''
|
||||
Return a vec3 that is the result of multiplying this matrix by a vec3.
|
||||
|
||||
u = self * v
|
||||
"""
|
||||
'''
|
||||
|
||||
cdef mat4 tmp = vec3()
|
||||
|
||||
|
@ -334,11 +334,11 @@ cdef class mat4:
|
|||
return tmp
|
||||
|
||||
def mulf(mat4 self, f):
|
||||
"""
|
||||
'''
|
||||
Return a matrix that is the result of multiplying this matrix by a scalar.
|
||||
|
||||
M = self * f
|
||||
"""
|
||||
'''
|
||||
|
||||
cdef mat4 tmp = mat4()
|
||||
cdef int i
|
||||
|
@ -351,15 +351,15 @@ cdef class mat4:
|
|||
def __repr__(mat4 self):
|
||||
lines = []
|
||||
|
||||
lines.append("mat4(%.1f %.1f %.1f %.1f" %(self.m[0], self.m[4], self.m[8], self.m[12]))
|
||||
lines.append(" %.1f %.1f %.1f %.1f" %(self.m[1], self.m[5], self.m[9], self.m[13]))
|
||||
lines.append(" %.1f %.1f %.1f %.1f" %(self.m[2], self.m[6], self.m[10], self.m[14]))
|
||||
lines.append(" %.1f %.1f %.1f %.1f)" %(self.m[3], self.m[7], self.m[11], self.m[15]))
|
||||
lines.append('mat4(%.1f %.1f %.1f %.1f' %(self.m[0], self.m[4], self.m[8], self.m[12]))
|
||||
lines.append(' %.1f %.1f %.1f %.1f' %(self.m[1], self.m[5], self.m[9], self.m[13]))
|
||||
lines.append(' %.1f %.1f %.1f %.1f' %(self.m[2], self.m[6], self.m[10], self.m[14]))
|
||||
lines.append(' %.1f %.1f %.1f %.1f)' %(self.m[3], self.m[7], self.m[11], self.m[15]))
|
||||
|
||||
return "\n".join(lines)
|
||||
return '\n'.join(lines)
|
||||
|
||||
cdef class vec3:
|
||||
"""
|
||||
'''
|
||||
A float 3D vector.
|
||||
|
||||
>>> v = vec3(1, 1, 0)
|
||||
|
@ -375,14 +375,14 @@ cdef class vec3:
|
|||
>>> w - v
|
||||
vec4(-1.00, 0.00, 1.00)
|
||||
|
||||
"""
|
||||
'''
|
||||
|
||||
def __init__(vec3 self, *args):
|
||||
"""
|
||||
'''
|
||||
Create a vec3.
|
||||
|
||||
Accepts any number of parameters between 0 and 3 to fill the vector from the left.
|
||||
"""
|
||||
'''
|
||||
|
||||
length = len(args)
|
||||
|
||||
|
@ -391,7 +391,7 @@ cdef class vec3:
|
|||
length = len(args)
|
||||
|
||||
if length > 3:
|
||||
raise MathError("Attempt to initialize a vec3 with %d arguments." %(length))
|
||||
raise MathError('Attempt to initialize a vec3 with %d arguments.' %(length))
|
||||
|
||||
for i in range(3):
|
||||
if i < length:
|
||||
|
@ -401,18 +401,18 @@ cdef class vec3:
|
|||
|
||||
def __getitem__(vec3 self, int i):
|
||||
if i >= 3 or i < 0:
|
||||
raise IndexError("element index out of range(3)")
|
||||
raise IndexError('element index out of range(3)')
|
||||
|
||||
return self.v[i]
|
||||
|
||||
def __setitem__(vec3 self, int i, float value):
|
||||
if i >= 3 or i < 0:
|
||||
raise IndexError("element index out of range(3)")
|
||||
raise IndexError('element index out of range(3)')
|
||||
|
||||
self.v[i] = value
|
||||
|
||||
def __repr__(vec3 self):
|
||||
return "vec3(%.2f, %.2f, %.2f)" %(self.v[0], self.v[1], self.v[2])
|
||||
return 'vec3(%.2f, %.2f, %.2f)' %(self.v[0], self.v[1], self.v[2])
|
||||
|
||||
def __getstate__(vec3 self):
|
||||
return (self.v[0], self.v[1], self.v[2])
|
||||
|
@ -424,12 +424,12 @@ cdef class vec3:
|
|||
|
||||
@property
|
||||
def length(vec3 self):
|
||||
"""Contains the geometric length of the vector."""
|
||||
'''Contains the geometric length of the vector.'''
|
||||
|
||||
return sqrt(self.v[0]**2 + self.v[1]**2 + self.v[2]**2)
|
||||
|
||||
def normalized(vec3 self):
|
||||
"""Returns this vector, normalized."""
|
||||
'''Returns this vector, normalized.'''
|
||||
|
||||
length = self.length
|
||||
|
||||
|
@ -445,29 +445,29 @@ cdef class vec3:
|
|||
return vec3(-self.v[0], -self.v[1], -self.v[2])
|
||||
|
||||
def dot(vec3 L, vec3 R):
|
||||
"""
|
||||
'''
|
||||
Returns the dot product of the two vectors.
|
||||
|
||||
E.g. u.dot(v) -> u . v
|
||||
"""
|
||||
'''
|
||||
|
||||
return L.v[0] * R.v[0] + L.v[1] * R.v[1] + L.v[2] * R.v[2]
|
||||
|
||||
def cross(vec3 L, vec3 R):
|
||||
"""
|
||||
'''
|
||||
Returns the cross product of the two vectors.
|
||||
|
||||
E.g. u.cross(v) -> u x v
|
||||
|
||||
"""
|
||||
'''
|
||||
|
||||
return vec3(L.v[1]*R.v[2] - L.v[2]*R.v[1], L.v[0]*R.v[2] - L.v[2]*R.v[0], L.v[0]*R.v[1] - L.v[1]*R.v[0])
|
||||
|
||||
def __mul__(vec3 L, R):
|
||||
"""
|
||||
'''
|
||||
Multiplication of a vec3 by a float.
|
||||
|
||||
The float has to be on the right.
|
||||
"""
|
||||
'''
|
||||
|
||||
return vec3(L.v[0] * R, L.v[1] * R, L.v[2] * R)
|
||||
|
|
110
m/python_m.py
110
m/python_m.py
|
@ -1,16 +1,16 @@
|
|||
#! /bin/env python3
|
||||
# encoding: utf-8
|
||||
|
||||
"""
|
||||
'''
|
||||
Vector And Matrix Math
|
||||
|
||||
Pure Python implementation.
|
||||
"""
|
||||
'''
|
||||
|
||||
from math import sin, cos, sqrt
|
||||
|
||||
class mat4:
|
||||
"""
|
||||
'''
|
||||
A float 4x4 matrix.
|
||||
|
||||
All arrays are column-major, i.e. OpenGL style:
|
||||
|
@ -21,30 +21,30 @@ class mat4:
|
|||
3 7 11 15
|
||||
|
||||
The matrix implements stacking useful for graphics.
|
||||
"""
|
||||
'''
|
||||
|
||||
def __cinit__(mat4 self):
|
||||
to_alloc = 16 * sizeof(float)
|
||||
self.stack = <float *>malloc(to_alloc)
|
||||
|
||||
if not self.stack:
|
||||
raise MemoryError("Unable to malloc %d B for mat4." %(to_alloc))
|
||||
raise MemoryError('Unable to malloc %d B for mat4.' %(to_alloc))
|
||||
|
||||
self.m = self.stack
|
||||
self.size = 1
|
||||
|
||||
def _debug(mat4 self):
|
||||
print("--- self.stack = %d" %(<uint64_t>self.stack))
|
||||
print("--- self.m = %d (+%d)" %(<uint64_t>self.m, self.m - self.stack))
|
||||
print("--- self.size = %d" %(self.size))
|
||||
print('--- self.stack = %d' %(<uint64_t>self.stack))
|
||||
print('--- self.m = %d (+%d)' %(<uint64_t>self.m, self.m - self.stack))
|
||||
print('--- self.size = %d' %(self.size))
|
||||
|
||||
def __init__(mat4 self, *args):
|
||||
"""
|
||||
'''
|
||||
Create a ma4t.
|
||||
|
||||
Accepts any number of parameters between 0 and 16 to fill the
|
||||
matrix from the upper left corner going down (column-wise).
|
||||
"""
|
||||
'''
|
||||
|
||||
self.m =
|
||||
|
||||
|
@ -55,7 +55,7 @@ class mat4:
|
|||
length = len(args)
|
||||
|
||||
if length > 16:
|
||||
raise MathError("Attempt to initialize a mat4 with %d arguments." %(length))
|
||||
raise MathError('Attempt to initialize a mat4 with %d arguments.' %(length))
|
||||
|
||||
self.load_from(args)
|
||||
|
||||
|
@ -75,7 +75,7 @@ class mat4:
|
|||
matrices = length//16
|
||||
|
||||
if not matrices*16 == length:
|
||||
raise GeneralError("mat4 __setstate__ got %d floats as a state" %(length))
|
||||
raise GeneralError('mat4 __setstate__ got %d floats as a state' %(length))
|
||||
|
||||
self.m = self.stack
|
||||
|
||||
|
@ -90,20 +90,20 @@ class mat4:
|
|||
|
||||
def __getitem__(mat4 self, int i):
|
||||
if i > 16 or i < 0:
|
||||
raise IndexError("element index out of range(16)")
|
||||
raise IndexError('element index out of range(16)')
|
||||
|
||||
return self.m[i]
|
||||
|
||||
def __setitem__(self, int i, value):
|
||||
if i > 16 or i < 0:
|
||||
raise IndexError("element index out of range(16)")
|
||||
raise IndexError('element index out of range(16)')
|
||||
|
||||
self.m[i] = value
|
||||
|
||||
def push(mat4 self):
|
||||
"""
|
||||
'''
|
||||
Push the current matrix into the stack and load up an empty one (a zero matrix)
|
||||
"""
|
||||
'''
|
||||
|
||||
# self.m points to the current matrix
|
||||
# self.stack points to the first matrix
|
||||
|
@ -122,7 +122,7 @@ class mat4:
|
|||
if tmp:
|
||||
self.stack = tmp
|
||||
else:
|
||||
raise MemoryError("Unable to malloc %d B for mat4." %(to_alloc))
|
||||
raise MemoryError('Unable to malloc %d B for mat4.' %(to_alloc))
|
||||
|
||||
# advance the pointer to the new one
|
||||
self.m = self.stack + 16 * used
|
||||
|
@ -135,12 +135,12 @@ class mat4:
|
|||
self.m[i] = old_m[i]
|
||||
|
||||
def pop(mat4 self):
|
||||
"""
|
||||
'''
|
||||
Pop a matrix from the stack.
|
||||
"""
|
||||
'''
|
||||
|
||||
if self.m == self.stack:
|
||||
raise IndexError("pop from an empty stack")
|
||||
raise IndexError('pop from an empty stack')
|
||||
|
||||
self.m -= 16
|
||||
|
||||
|
@ -153,14 +153,14 @@ class mat4:
|
|||
return L
|
||||
|
||||
def load_from(mat4 self, L):
|
||||
"""
|
||||
'''
|
||||
Fill the current matrix from a either a list of values, column-major,
|
||||
or another matrix. This method doesn't modify the stack, only the
|
||||
current matrix is read and modified.
|
||||
|
||||
If the number of values isn't 16, it will be padded to 16 by zeros.
|
||||
If it's larger, GeneralError will be raised.
|
||||
"""
|
||||
'''
|
||||
|
||||
if isinstance(L, mat4):
|
||||
L = L.get_list()
|
||||
|
@ -169,7 +169,7 @@ class mat4:
|
|||
length = len(L)
|
||||
|
||||
if length > 16:
|
||||
raise GeneralError("supplied list is longer than 16")
|
||||
raise GeneralError('supplied list is longer than 16')
|
||||
|
||||
for i in range(16):
|
||||
if i < length:
|
||||
|
@ -178,13 +178,13 @@ class mat4:
|
|||
self.m[i] = 0.0
|
||||
|
||||
def zero(mat4 self):
|
||||
"""Fill the matrix with zeroes."""
|
||||
'''Fill the matrix with zeroes.'''
|
||||
|
||||
for i in range(16):
|
||||
self.m[i] = 0.0
|
||||
|
||||
def identity(mat4 self):
|
||||
"""Make the matrix an identity."""
|
||||
'''Make the matrix an identity.'''
|
||||
|
||||
self.zero()
|
||||
|
||||
|
@ -194,7 +194,7 @@ class mat4:
|
|||
self.m[15] = 1.0
|
||||
|
||||
def transpose(mat4 self):
|
||||
"""Transpose the matrix."""
|
||||
'''Transpose the matrix.'''
|
||||
|
||||
cdef float tmp
|
||||
|
||||
|
@ -223,7 +223,7 @@ class mat4:
|
|||
self.m[9] = tmp
|
||||
|
||||
def invert(mat4 self):
|
||||
"""Invert the matrix."""
|
||||
'''Invert the matrix.'''
|
||||
|
||||
cdef float tmp[16]
|
||||
cdef float det
|
||||
|
@ -237,7 +237,7 @@ class mat4:
|
|||
|
||||
# epsilon pulled straight out of Uranus
|
||||
if det < 0.00005 and det > -0.00005:
|
||||
print("det=%.1f" %(det))
|
||||
print('det=%.1f' %(det))
|
||||
return
|
||||
|
||||
tmp[1] = -self.m[1]*self.m[10]*self.m[15] + self.m[1]*self.m[11]*self.m[14] + self.m[9]*self.m[2]*self.m[15] - self.m[9]*self.m[3]*self.m[14] - self.m[13]*self.m[2]*self.m[11] + self.m[13]*self.m[3]*self.m[10]
|
||||
|
@ -272,11 +272,11 @@ class mat4:
|
|||
self.m[15] = tmp[15] * det
|
||||
|
||||
def mulm(mat4 self, mat4 B, bint inplace=False):
|
||||
"""
|
||||
'''
|
||||
Return a matrix that is the result of multiplying this matrix by another.
|
||||
|
||||
M = self * mat4 B
|
||||
"""
|
||||
'''
|
||||
|
||||
cdef uint8_t i
|
||||
cdef mat4 tmp = mat4()
|
||||
|
@ -305,11 +305,11 @@ class mat4:
|
|||
return tmp
|
||||
|
||||
def mulv(mat4 self, vec3 v):
|
||||
"""
|
||||
'''
|
||||
Return a vec3 that is the result of multiplying this matrix by a vec3.
|
||||
|
||||
u = self * v
|
||||
"""
|
||||
'''
|
||||
|
||||
cdef mat4 tmp = vec3()
|
||||
|
||||
|
@ -320,11 +320,11 @@ class mat4:
|
|||
return tmp
|
||||
|
||||
def mulf(mat4 self, f):
|
||||
"""
|
||||
'''
|
||||
Return a matrix that is the result of multiplying this matrix by a scalar.
|
||||
|
||||
M = self * f
|
||||
"""
|
||||
'''
|
||||
|
||||
cdef mat4 tmp = mat4()
|
||||
cdef int i
|
||||
|
@ -337,15 +337,15 @@ class mat4:
|
|||
def __repr__(mat4 self):
|
||||
lines = []
|
||||
|
||||
lines.append("mat4(%.1f %.1f %.1f %.1f" %(self.m[0], self.m[4], self.m[8], self.m[12]))
|
||||
lines.append(" %.1f %.1f %.1f %.1f" %(self.m[1], self.m[5], self.m[9], self.m[13]))
|
||||
lines.append(" %.1f %.1f %.1f %.1f" %(self.m[2], self.m[6], self.m[10], self.m[14]))
|
||||
lines.append(" %.1f %.1f %.1f %.1f)" %(self.m[3], self.m[7], self.m[11], self.m[15]))
|
||||
lines.append('mat4(%.1f %.1f %.1f %.1f' %(self.m[0], self.m[4], self.m[8], self.m[12]))
|
||||
lines.append(' %.1f %.1f %.1f %.1f' %(self.m[1], self.m[5], self.m[9], self.m[13]))
|
||||
lines.append(' %.1f %.1f %.1f %.1f' %(self.m[2], self.m[6], self.m[10], self.m[14]))
|
||||
lines.append(' %.1f %.1f %.1f %.1f)' %(self.m[3], self.m[7], self.m[11], self.m[15]))
|
||||
|
||||
return "\n".join(lines)
|
||||
return '\n'.join(lines)
|
||||
|
||||
cdef class vec3:
|
||||
"""
|
||||
'''
|
||||
A float 3D vector.
|
||||
|
||||
>>> v = vec3(1, 1, 0)
|
||||
|
@ -361,14 +361,14 @@ cdef class vec3:
|
|||
>>> w - v
|
||||
vec4(-1.00, 0.00, 1.00)
|
||||
|
||||
"""
|
||||
'''
|
||||
|
||||
def __init__(vec3 self, *args):
|
||||
"""
|
||||
'''
|
||||
Create a vec3.
|
||||
|
||||
Accepts any number of parameters between 0 and 3 to fill the vector from the left.
|
||||
"""
|
||||
'''
|
||||
|
||||
length = len(args)
|
||||
|
||||
|
@ -377,7 +377,7 @@ cdef class vec3:
|
|||
length = len(args)
|
||||
|
||||
if length > 3:
|
||||
raise MathError("Attempt to initialize a vec3 with %d arguments." %(length))
|
||||
raise MathError('Attempt to initialize a vec3 with %d arguments.' %(length))
|
||||
|
||||
for i in range(3):
|
||||
if i < length:
|
||||
|
@ -387,18 +387,18 @@ cdef class vec3:
|
|||
|
||||
def __getitem__(vec3 self, int i):
|
||||
if i >= 3 or i < 0:
|
||||
raise IndexError("element index out of range(3)")
|
||||
raise IndexError('element index out of range(3)')
|
||||
|
||||
return self.v[i]
|
||||
|
||||
def __setitem__(vec3 self, int i, float value):
|
||||
if i >= 3 or i < 0:
|
||||
raise IndexError("element index out of range(3)")
|
||||
raise IndexError('element index out of range(3)')
|
||||
|
||||
self.v[i] = value
|
||||
|
||||
def __repr__(vec3 self):
|
||||
return "vec3(%.2f, %.2f, %.2f)" %(self.v[0], self.v[1], self.v[2])
|
||||
return 'vec3(%.2f, %.2f, %.2f)' %(self.v[0], self.v[1], self.v[2])
|
||||
|
||||
def __getstate__(vec3 self):
|
||||
return (self.v[0], self.v[1], self.v[2])
|
||||
|
@ -410,12 +410,12 @@ cdef class vec3:
|
|||
|
||||
@property
|
||||
def length(vec3 self):
|
||||
"""Contains the geometric length of the vector."""
|
||||
'''Contains the geometric length of the vector.'''
|
||||
|
||||
return sqrt(self.v[0]**2 + self.v[1]**2 + self.v[2]**2)
|
||||
|
||||
def normalized(vec3 self):
|
||||
"""Returns this vector, normalized."""
|
||||
'''Returns this vector, normalized.'''
|
||||
|
||||
length = self.length
|
||||
|
||||
|
@ -431,29 +431,29 @@ cdef class vec3:
|
|||
return vec3(-self.v[0], -self.v[1], -self.v[2])
|
||||
|
||||
def dot(vec3 L, vec3 R):
|
||||
"""
|
||||
'''
|
||||
Returns the dot product of the two vectors.
|
||||
|
||||
E.g. u.dot(v) -> u . v
|
||||
"""
|
||||
'''
|
||||
|
||||
return L.v[0] * R.v[0] + L.v[1] * R.v[1] + L.v[2] * R.v[2]
|
||||
|
||||
def cross(vec3 L, vec3 R):
|
||||
"""
|
||||
'''
|
||||
Returns the cross product of the two vectors.
|
||||
|
||||
E.g. u.cross(v) -> u x v
|
||||
|
||||
"""
|
||||
'''
|
||||
|
||||
return vec3(L.v[1]*R.v[2] - L.v[2]*R.v[1], L.v[0]*R.v[2] - L.v[2]*R.v[0], L.v[0]*R.v[1] - L.v[1]*R.v[0])
|
||||
|
||||
def __mul__(vec3 L, R):
|
||||
"""
|
||||
'''
|
||||
Multiplication of a vec3 by a float.
|
||||
|
||||
The float has to be on the right.
|
||||
"""
|
||||
'''
|
||||
|
||||
return vec3(L.v[0] * R, L.v[1] * R, L.v[2] * R)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue