def randomInSphere(self): """Gives a random point within a unit sphere. TODO: shitty method""" point = Vector3(2, 2, 2) while point.squaredLength() > 1: point = Vector3(random.random(), random.random(), random.random()) * 2.0 - 1 return point
class Transform: '''This is the transform class with components position, rotation and scale''' position = Vector3(0.0, 0.0, 0.0) rotation = Quaternion(0.0, 0.0, 0.0, 1.0) scale = Vector3(0.0, 0.0, 0.0) def Rotate(self, pitch, yaw, roll): self.rotation.EulerRotation(pitch, yaw, roll)
def colour(ray, roster, depth=0): strike = roster.hit(ray, min, max) if (strike): bounce = strike.medium.material.scatter(strike) if (depth >= bounceDepth): return Vector3() if (not bounce.scatter): return bounce.attenuation return colour(bounce.scatter, roster, depth + 1) * bounce.attenuation else: unitDirection = ray.direction.unit() t = (unitDirection.y + 1.0) / 2 return Vector3(1.0, 1.0, 1.0) * (1.0 - t) + Vector3(0.1, 0.2, 0.8) * t
def toAxisAngleInRadian(self): '''Converts a rotation to axis-angle representation(angle in radian).''' import Vector3 # reference:FreeCAD Rotation.cpp if self.w > -1.0 and self.w < 1.0: t = math.acos(self.w) scale = math.sin(t) if Util.isEqualZero(scale): return Vector3.Vector3(0, 0, 1), 0.0 else: axis = Vector3.Vector3(self.x / scale, self.y / scale, self.z / scale) return axis, 2 * t else: return Vector3.Vector3(0, 0, 1), 0.0
def bounce(par, par2, freq): """Return the value of position z, position z2, time, and separation between two particles""" F1=Force(par) #Make Force class #Define empty matrices z=[] z2=[] t=[] sepo = 0.0 #Initial condition for separation for i in xrange(0,500000): t.append(i*0.0001) #add element i*0.0001 to matrix t par2.pos.z = -500 + 10*math.sin(freq*t[i]) #The oscillating part from the plate netF = F1.GravitationalF(9.8) + F1.NormalF(par2,1000000,1,0.00001) + F1.FrictionF(0.2) #Net force calculation par = Euler(par,netF,0.0001) #Updating particle 1 par2 = Euler(par2,Vector3(0,0,0),0.0001) #Updating particle 2 z.append(par.pos.z-0.5) #add z position of particle 1 to matrix z z2.append(par2.pos.z+500) #add z position of particle 2 to matrix z2 #Calculating the separation between two particles after stable time if t[i] > (0.0001*200000-2*3.14/freq): sepn = (par.pos.z-0.5) - (par2.pos.z+500) if sepn > sepo: sepo = sepn return z,z2,t,sepo
def __init__(self, x, y): self.content = [] for iy in range(y): line = [] for ix in range(x): line.append(Vector3()) self.content.append(line)
def test(): m = Matrix44.xyz_rotation(radians(45), radians(20), radians(0)) n = m.copy() r = Matrix44.z_rotation(radians(32)) m *= r n.fast_mul(r) m.transpose() m.translate = (m.translate[:3]) + Vector3(10, 20, 30) v = (1., 2., 3.) vt = m.transform(v) vit = m.get_inverse().transform(vt) m[1, 2] = 3. m.translate = (1, 2, 3) identity = Matrix44() #identity.set_row(0, (1, 2, 3, 4)) identity[3, 1] = 456 #identity *= Matrix44.scale(20.32, 764.2323, -23) m.translate = (0, 0, 0)
def multiplyVector(self, vec): '''Transforms a direction by this matrix.''' import Vector3 assert isinstance(vec, Vector3.Vector3) v = Vector3.Vector3() v.x = self.m11 * vec.x + self.m12 * vec.y + self.m13 * vec.z v.y = self.m21 * vec.x + self.m22 * vec.y + self.m23 * vec.z v.z = self.m31 * vec.x + self.m32 * vec.y + self.m33 * vec.z return v
def multiplyPoint(self, pnt): '''Transforms a position by this matrix.''' import Vector3 assert isinstance(pnt, Vector3.Vector3) p = Vector3.Vector3() p.x = self.m11 * pnt.x + self.m12 * pnt.y + self.m13 * pnt.z + self.m14 p.y = self.m21 * pnt.x + self.m22 * pnt.y + self.m23 * pnt.z + self.m24 p.z = self.m31 * pnt.x + self.m32 * pnt.y + self.m33 * pnt.z + self.m34 return p
def multiplyPoint(self, pnt): '''Rotates the point pnt by this quaternion.''' import Vector3 assert isinstance(pnt, Vector3.Vector3) x = self.x y = self.y z = self.z w = self.w x2 = self.x * self.x y2 = self.y * self.y z2 = self.z * self.z w2 = self.w * self.w dx = (x2 + w2 - y2 - z2) * pnt.x + 2.0 * ( x * y - z * w) * pnt.y + 2.0 * (x * z + y * w) * pnt.z dy = 2.0 * (x * y + z * w) * pnt.x + ( w2 - x2 + y2 - z2) * pnt.y + 2.0 * (y * z - x * w) * pnt.z dz = 2.0 * (x * z - y * w) * pnt.x + 2.0 * (x * w + y * z) * pnt.y + ( w2 - x2 - y2 + z2) * pnt.z return Vector3.Vector3(dx, dy, dz)
def move(self, forward=None, right=None, up=None): """Changes the translation according to a direction vector. To move in opposite directions (i.e back, left and down), first negate the vector. forward -- Units to move in the 'forward' direction right -- Units to move in the 'right' direction up -- Units to move in the 'up' direction """ if forward is not None: self.translate = Vector3(self.translate) + \ Vector3(self.forward) * forward if right is not None: self.translate = Vector3(self.translate) + \ Vector3(self.right) * right if up is not None: self.translate = Vector3(self.translate) + \ Vector3(self.up) * up
def fromToRotation(f, to): '''Creates a rotation which rotates from from(Vector) to to(Vector).''' from Vector3 import Vector3 assert isinstance(f, Vector3) and isinstance(to, Vector3) # reference:FreeCAD Rotation.cpp u = f.normalized v = to.normalized dot = Vector3.dot(u, v) w = Vector3.cross(u, v) # parallel vectors if w.length == 0: # same direction if dot >= 0: return Quaternion(0.0, 0.0, 0.0, 1.0) else: t = Vector3.cross(u, Vector3(1.0, 0.0, 0.0)) if Util.isEqualZero(t.length): t = Vector3.cross(u, Vector3(0.0, 1.0, 0.0)) return Quaternion(t.x, t.y, t.z, 0.0) else: angleInRad = math.acos(dot) return Quaternion.axisAngleInRadian(w, angleInRad)
if (depth >= bounceDepth): return Vector3() if (not bounce.scatter): return bounce.attenuation return colour(bounce.scatter, roster, depth + 1) * bounce.attenuation else: unitDirection = ray.direction.unit() t = (unitDirection.y + 1.0) / 2 return Vector3(1.0, 1.0, 1.0) * (1.0 - t) + Vector3(0.1, 0.2, 0.8) * t frames = 1 width = 600 height = 600 samples = 2 pic = Picture(width, height * frames) check = Checker(Vector3(0.0, 0.0, 0.3), Vector3(0.9, 0.9, 0.8)) gold = Colour(Vector3(0.8, 0.6, 0.4)) blue = Colour(Vector3(0.8, 0.3, 0.3)) roster = Roster() brick = Lambert(blue) grass = Lambert(check) mirror = Metal(gold) roster.add(Sphere(brick, Vector3(0.0, 0.0, 0.0), 0.5)) roster.add(Sphere(grass, Vector3(0.0, -100.5, 0.0), 100.0)) roster.add(Sphere(mirror, Vector3(1.0, 0.0, 0.0), 0.5)) roster.add(Sphere(mirror, Vector3(-1.0, 0.0, 0.0), 0.5)) for f in range(frames): eye = Eye(Vector3(2.0, 0.0 + float(f) * 0.5, 10.0),
def __init__(self, origin = Vector3(), direction = Vector3()): self.origin = origin self.direction = direction
import Log from Vector3 import * for i in range(0, 10): print("Hello, World (" + str(i) + ")") Log.Error("Some error") Log.IncIndent() Log.Warning("Some warning") Log.Success("Some success") def fib(n): return 1 if n <= 2 else fib(n - 1) + fib(n - 2) a = Vector3(1.0, 2.0, -0.5) + Vector3(3.0, 0.5, 2.0) a.Normalize() b = Vector3(1.5, 2.0, 0.5) print("a.Length() = " + str(a.Length())) print("Dot(a, b) = " + str(Vector3.Dot(a, b))) c = Vector3.Cross(Vector3(0, 0, 1), Vector3(1, 0, 0)) print("Cross(a, b) = { " + str(c.x) + ", " + str(c.y) + ", " + str(c.z) + " }")
import math class PhongMaterial(): def __init__(self, diffuse, specular, shininess, reflectiveness): self.diffuse = diffuse self.specular = specular self.shininess = shininess self.reflectiveness = reflectiveness def sample(self, ray, position, normal): NdotL = normal.dot(lightDir) # Blinn-phong H = lightDir.subtract(ray.direction).normalize() NdotH = normal.dot(H) # phong # R = normal.multiply(lightDir.multiply(2).dot(normal)).subtract(lightDir) # V = ray.direction.negate() # RdotV = R.dot(V) # specularTerm = self.specular.multiply(math.pow(max(RdotV, 0), self.shininess)) diffuseTerm = self.diffuse.multiply(max(NdotL, 0)) specularTerm = self.specular.multiply( math.pow(max(NdotH, 0), self.shininess)) return lighrColor.modulate(diffuseTerm.add(specularTerm)) # global light lightDir = Vector3(1, 1, 1).normalize() lighrColor = col.white
def GravitationalF(self, g): """Calculating gravitational force""" return -self.par.m * Vector3(0, 0, g)
def __init__(self, attenuation=Vector3(), scatter=None): self.attenuation = attenuation self.scatter = scatter
f = open("massa1.1.txt","w") #Open file .txt to be written n=1 freq = [0.15+i*0.01 for i in xrange(0,n)] #The matrix for omega (note we want to take range of omega values) #Creating zero and empty matrices z=[[0 for j in xrange(0,200000)] for k in xrange(0,n)] z2=[[0 for j in xrange(0,200000)] for k in xrange(0,n)] t=[[0 for j in xrange(0,200000)] for k in xrange(0,n)] sep=[] #Iteration for range of omega for i in xrange(0,n): #Initializaition of particle 1 and the particle for plate pos = Vector3(0.0,0.0,0.5) vel = Vector3(0.0,0.0,0.0) acc = Vector3(0,0,0) par = Particle(pos,vel,acc,1,1) pos2=Vector3(0.0,0.0,-500.0) vel2=Vector3(0.0,0.0,0.0) zero=Vector3(0,0,0) par2=Particle(pos2,vel2,zero,1,1000) z[i],z2[i],t[i],sepo=bounce(par,par2,freq[i]) f.write(str(sepo) + "\t" + str(freq[i]) + "\n") #Write to file sep.append(sepo) #add sepo to matrix sep print sepo f.close() #Close the file
def value(self, u, v, point): return Vector3()
import Vector2 from Vector2 import * import Vector3 from Vector3 import * import Vector4 from Vector4 import * Me = Vector2(25, 11) You = Vector2(34, 33) Sex = Vector2(0, 0) Me2 = Vector3(25, 11, 50) You2 = Vector3(34, 33, 42) Sex2 = Vector3(0, 0, 0) Me3 = Vector4(25, 11, 50, 13) def main(): print("2D Vector Math: ") Sex = Me + You print("Adding: ", Sex.x, ",", Sex.y) Sex = Me - You print("Subtracting: ", Sex.x, ",", Sex.y) Sex = Me * You print("Multiplication: ", Sex.x, ",", Sex.y)