# pygamekit / mathutils.pyx

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294``` ``` from prerequisites cimport * cimport gkmath cdef class Vector: def __init__(self, ini=None, ini2=None, ini3=None): if ini3!=None: self.v = new gkmath.Vector3(ini,ini2,ini3) elif isinstance(ini, Vector): self.v = new gkmath.Vector3() self.v.assign((ini).v[0]) memcpy((self.v).ptr(),(ini).v.ptr(),sizeof(Real)*3) elif ini!=None: self.v = new gkmath.Vector3(ini[0],ini[1],ini[2]) else: self.v = new gkmath.Vector3() def __dealloc__(self): del self.v cdef Vector assign(self, gkmath.Vector3 v): self.v.assign(v) return self property x: def __get__(self): return self.v.x def __set__(self, v): self.v.x=v property y: def __get__(self): return self.v.y def __set__(self, v): self.v.y=v property z: def __get__(self): return self.v.z def __set__(self, v): self.v.z=v property length: def __get__(self): return self.v.length() def __set__(self, Real v): self.v.normalise() self.v.imul(v) def normalize(self): return self.v.normalise() def normalized(self): cdef Vector v = Vector(self) v.v.normalise() return v def copy(self): return Vector(self) def __len__(self): return 3 def __getitem__(self, i): return self.v.getitem(i) def __add__(self, v): cdef Vector ret = Vector(self) if isinstance(v, Vector): (ret.v).iadd((v).v[0]) else: (ret.v).iadd(gkmath.Vector3(v[0],v[1],v[2])) return ret def __sub__(self, v): cdef Vector ret = Vector(self) if isinstance(v, Vector): (ret.v).isub((v).v[0]) else: (ret.v).isub(gkmath.Vector3(v[0],v[1],v[2])) return ret def __iadd__(self, v): if isinstance(v, Vector): (self.v).iadd((v).v[0]) else: (self.v).iadd(gkmath.Vector3(v[0],v[1],v[2])) return self def __isub__(self, v): cdef Vector ret = Vector(self) if isinstance(v, Vector): (self.v).isub((v).v[0]) else: (self.v).isub(gkmath.Vector3(v[0],v[1],v[2])) return self def __mul__(self, v): cdef Vector ret = Vector(self) if isinstance(v, Vector): (ret.v).imul((v).v[0]) elif getattr(v, '__getitem__', False): (ret.v).imul(gkmath.Vector3(v[0],v[1],v[2])) else: (ret.v).imul(v) return ret def __div__(self, v): cdef Vector ret = Vector(self) if isinstance(v, Vector): (ret.v).idiv((v).v[0]) elif getattr(v, '__getitem__', False): (ret.v).idiv(gkmath.Vector3(v[0],v[1],v[2])) else: (ret.v).idiv(v) return ret def __truediv__(self, v): cdef Vector ret = Vector(self) if isinstance(v, Vector): (ret.v).idiv((v).v[0]) elif getattr(v, '__getitem__', False): (ret.v).idiv(gkmath.Vector3(v[0],v[1],v[2])) else: (ret.v).idiv(v) return ret def __imul__(self, v): if isinstance(v, Vector): (self.v).imul((v).v[0]) elif getattr(v, '__getitem__', False): (self.v).imul(gkmath.Vector3(v[0],v[1],v[2])) else: (self.v).imul(v) return self def __idiv__(self, v): if isinstance(v, Vector): (self.v).idiv((v).v[0]) elif getattr(v, '__getitem__', False): (self.v).idiv(gkmath.Vector3(v[0],v[1],v[2])) else: (self.v).idiv(v) return self def __itruediv__(self, v): if isinstance(v, Vector): (self.v).idiv((v).v[0]) elif getattr(v, '__getitem__', False): (self.v).idiv(gkmath.Vector3(v[0],v[1],v[2])) else: (self.v).idiv(v) return self def __pos__(self): return self def __neg__(self): return self*-1 #optimize this? def __getitem__(self, i): if isinstance(i,slice): return [self.v.x,self.v.y,self.v.z][i] elif i<3: return self.v.ptr()[i%3] else: raise IndexError def __setitem__(self, i, v): if not isinstance(i,slice) and i<3: self.v.ptr()[i%3]=v else: raise IndexError def __repr__(self): return "Vector(%f, %f, %f)"%(self.v.x,self.v.y,self.v.z) def angle_between(self, other): return self.v.angleBetween(Vector(other).v[0]).valueRadians() def get_rotation_to(self, other, fallbackAxis=None): cdef Quaternion q = Quaternion() if fallbackAxis!=None: q.q.assign(self.v.getRotationTo(Vector(other).v[0], Vector(fallbackAxis).v[0])) else: q.q.assign(self.v.getRotationTo(Vector(other).v[0])) return q def lerp(self, other, factor): factor = max(0.0,min(factor,1.0)) invf = 1.0-factor return Vector(self.x*invf + other[0]*factor, self.y*invf + other[1]*factor, self.z*invf + other[2]*factor) #cdef assignv(self, gkmath.Vector3 v): # memcpy((self.v).ptr(),v.ptr(),sizeof(Real)*3) cdef class Quaternion: def __init__(Quaternion self, ini=None, ini2=None, ini3=None, ini4=None): if isinstance(ini, Quaternion): self.q = new gkmath.Quaternion() self.q.assign((ini).q[0]) elif ini4!=None: self.q = new gkmath.Quaternion(ini,ini2,ini3,ini4) elif ini!=None: self.q = new gkmath.Quaternion(ini[0],ini[1],ini[2],ini[3]) else: self.q = new gkmath.Quaternion() def __dealloc__(self): del self.q property w: def __get__(self): return self.q.w def __set__(self, v): self.q.w=v property x: def __get__(self): return self.q.x def __set__(self, v): self.q.x=v property y: def __get__(self): return self.q.y def __set__(self, v): self.q.y=v property z: def __get__(self): return self.q.z def __set__(self, v): self.q.z=v def normalize(Quaternion self): return self.q.normalise() def normalized(Quaternion self): cdef Quaternion v = Quaternion(self) v.v.normalise() return v def copy(Quaternion self): return Quaternion(self) def __len__(self): return 4 def __pos__(Quaternion self): return self def __neg__(Quaternion self): return self*-1 #optimize this? def __getitem__(Quaternion self, i): if isinstance(i,slice): return [self.q.w,self.q.x,self.q.y,self.q.z][i] elif i<4: return self.q.ptr()[i%4] else: raise IndexError def __setitem__(Quaternion self, i, v): if not isinstance(i,slice) and i<4: self.q.ptr()[i%4]=v else: raise IndexError def __repr__(Quaternion self): return "Quaternion(%f, %f, %f, %f)"%(self.q.w,self.q.x,self.q.y,self.q.z) def __mul__(Quaternion self, v): cdef Quaternion retq cdef Vector retv if isinstance(v, Quaternion): retq = Quaternion() (retq.q).assign((retq.q).mul((v).q[0])) return retq if isinstance(v, Vector): retv = Vector() (retv.v).assign((self).q.mul((v).v[0])) return retv retq = Quaternion() (retq.q).assign((retq.q).mul(v)) return retq def inverted(Quaternion self): cdef Quaternion q = self.copy() q.invert() return q def invert(Quaternion self): self.q.x=-(self.q.x) self.q.y=-(self.q.y) self.q.z=-(self.q.z) def lerp(self, Quaternion other, factor): cdef Quaternion q = Quaternion() q.q.assign(gkmath.quat_nlerp(factor, self.q[0], other.q[0], False)) return q def slerp(self, Quaternion other, factor): cdef Quaternion q = Quaternion() q.q.assign(gkmath.quat_Slerp(factor, self.q[0], other.q[0], False)) return q ```