def __init__(self): ''' Plane class constructor. ''' self.normal = vector.Vector(3, data=[0.0, 0.0, 0.0]) self.a = 0 self.b = 0 self.c = 0 self.d = 0
def __init__(self, startVector, dirVector): ''' Initiated a ray with the start vector and direction vector. ''' self.start = startVector self.dir = dirVector self.distance = self.dir.magnitude() self.dir.i_normalize() # The end is used when intersections are added so # we can know where the ray stops. self.end = vec.Vector(3)
def matrix_vector_multiply(matrix, vec): ''' Multiply matrix with vector ''' matSize = len(matrix) vecSize = vec.size vecOut = vector.Vector(vecSize) crange = list(sm.range(matSize)) for i in crange: for j in crange: vecOut.vector[j] += vec.vector[i] * matrix[i][j] return vecOut
def quat_rotate(origin, axis, theta): ''' Returns a vector that is rotated around an axis. ''' thetaOver2 = theta * 0.5 sinThetaOver2 = math.sin(math.radians(thetaOver2)) cosThetaOver2 = math.cos(math.radians(thetaOver2)) quat = Quaternion(data=[ cosThetaOver2, axis[0] * sinThetaOver2, axis[1] * sinThetaOver2, axis[2] * sinThetaOver2 ]) rotation = (quat * origin) * quat.conjugate() return vector.Vector( 3, data=[rotation.data[1], rotation.data[2], rotation.data[3]])
def normalize(pdata): ''' Return the normalized plane.''' vec = vector.Vector(3, data=pdata) vecN = vec.normalize() length = vecN.magnitude() if length is not 0: return vecN.vector[0], vecN.vector[1], vecN.vector[ 2], pdata[3] / length else: print("Plane fail to normalize due to zero division.") return 0.0, 0.0, 0.0, 0.0
def bestFitNormal(self, vecList): ''' Pass in a list of vectors to find the best fit normal. ''' output = vector.Vector(3).zero() for i in sm.range(len(vecList)): output.vector[0] += ( vecList[i].vector[2] + vecList[i + 1].vector[2]) * ( vecList[i].vector[1] - vecList[i + 1].vector[1]) output.vector[1] += ( vecList[i].vector[0] + vecList[i + 1].vector[0]) * ( vecList[i].vector[2] - vecList[i + 1].vector[2]) output.vector[2] += ( vecList[i].vector[1] + vecList[i + 1].vector[1]) * ( vecList[i].vector[0] - vecList[i + 1].vector[0]) return output.normalize()
def unproject(winx, winy, winz, modelview, projection, viewport): ''' Unproject a point from the screen and return the object coordinates. ''' m = Matrix(4) IN = vector.Vector(4).zero() objCoord = vector.Vector(3).zero() A = projection * modelview m = A.inverse() IN.vector[0] = (winx - viewport[0]) / viewport[2] * 2.0 - 1.0 IN.vector[1] = (winy - viewport[1]) / viewport[3] * 2.0 - 1.0 IN.vector[2] = 2.0 * winz - 1.0 IN.vector[3] = 1.0 OUT = m * IN if (OUT.vector[3] == 0.0): return vector.Vector(3).zero() OUT.vector[3] = 1.0 / OUT.vector[3] objCoord.vector[0] = OUT.vector[0] * OUT.vector[3] objCoord.vector[1] = OUT.vector[1] * OUT.vector[3] objCoord.vector[2] = OUT.vector[2] * OUT.vector[3] return objCoord
def GenerateSamples(sqrtNumSamples, numBands): x = 0 y = 0 theta = 0 phi = 0 ii = 0 index = 0 numSamples = sqrtNumSamples * sqrtNumSamples numFunctions = numBands * numBands invertedNumSamples = 1.0 / sqrtNumSamples # Create the array to hold the samples samples = [] for x in sm.range(numSamples): samples.append( SPHSample(0.0, 0.0, vector.Vector(3, data=[0.0, 0.0, 0.0]), numFunctions)) print("Generating Samples...") # Loop through a grid of numSamples X numSamples for i in sm.range(sqrtNumSamples): for j in sm.range(sqrtNumSamples): x = (i + random.random()) * invertedNumSamples y = (j + random.random()) * invertedNumSamples # Spherical Angles theta = 2.0 * math.acos(math.sqrt(1.0 - x)) phi = 2.0 * math.PI * y samples[ii].theta = theta samples[ii].phi = phi # Vec is an array and dir is a vector samples[ii].dir.vec = [ math.sin(theta) * math.cos(phi), math.sin(theta) * math.sin(phi), math.cos(theta) ] # Calculate SH coefficients of current sample for l in sm.range(numBands): for m in sm.range(-l, l + 1): index = l * (l + 1) + m samples[ii].values[index] = sph.SPH(l, m, theta, phi) ii += 1 # Return the samples array return samples
def __init__(self, theta, phi, dirc, sampleNumber): # Spherical coordinates self.theta = theta self.phi = phi # Values of SH function at this point self.values = [] for _ in sm.range(sampleNumber): self.values.append(0.0) # Direction (Vector3D) if isinstance(dirc, vector.Vector): self.dir = dirc else: self.dir = vector.Vector(3, data=[0.0, 0.0, 0.0])
def project(obj, model, proj, viewport): ''' The most hacked together project code in the world. :| It works tho. :3 ''' projM = common.list_2d_to_1d(proj) modelM = common.list_2d_to_1d(model) T = Matrix(4) for r in sm.range(4): for c in sm.range(4): T[r][c] = 0.0 for i in sm.range(4): T[r][c] += projM[r + i * 4] * modelM[i + c * 4] result = vector.Vector(4) for r in sm.range(4): result.vector[r] = vector.Vector(4, data=T[r]).dot(obj) rhw = 1.0 / result.vector[3] return vector.Vector( 3, data=[(1 + result.vector[0] * rhw) * viewport[2] / 2.0 + viewport[0], (1 + result.vector[1] * rhw) * viewport[3] / 2.0 + viewport[1], (result.vector[2] * rhw) * (1 - 0) + 0, rhw])
def getLeft(self): ''' Returns the left vector. ''' return quat_rotate_vector(self, vector.Vector(3, data=[-1.0, 0.0, 0.0]))
def getDown(self): ''' Returns the down vector. ''' return quat_rotate_vector(self, vector.Vector(3, data=[0.0, -1.0, 0.0]))
def getUp(self): ''' Returns the up vector. ''' return quat_rotate_vector(self, vector.Vector(3, data=[0.0, 1.0, 0.0]))
def getRight(self): ''' Returns the right vector. ''' return quat_rotate_vector(self, vector.Vector(3, data=[1.0, 0.0, 0.0]))
def getBack(self): ''' Returns the backwards vector. ''' return quat_rotate_vector(self, vector.Vector(3, data=[0.0, 0.0, -1.0]))
def getForward(self): ''' Returns the forward vector. ''' return quat_rotate_vector(self, vector.Vector(3, data=[0.0, 0.0, 1.0]))
def quat_rotate_vector(quat, vec): ''' Rotates a vector by a quaternion, returns a vector. ''' outQuat = (quat * vec) * quat.conjugate() return vector.Vector( 3, data=[outQuat.data[1], outQuat.data[2], outQuat.data[3]])
import six.moves as sm from gem import matrix from gem import vector # Some vector operations examples vectorA = vector.Vector(3, data=[1, 2, 3]) vectorB = vector.Vector(3, data=[4, 5, 6]) vectorC = vectorA + vectorB print("Vector C output:" , vectorC.vector) vectorD = vectorA * 2 print("Vector A * 2:", vectorD.vector) print("Vector A magnitude:", vectorA.magnitude()) # Some matrix operations examples matrixA = matrix.Matrix(4, data=[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) matrixB = matrix.Matrix(4, data=[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) matrixC = matrixA * matrixB print("Matrix C output:") for i in sm.range(matrixC.size): print(matrixC.matrix[i]) print("End of Matrix C output")