vertex.cpp (2253B)
1 #include "all_includes.hh" 2 3 Vertex::Vertex() {} 4 5 Vertex::Vertex(float _x, float _y, float _z): x(_x), y(_y), z(_z) { 6 /* if (!(std::isfinite(x) && std::isfinite(y) && std::isfinite(z))) 7 std::cerr << "Attempted to create vertex with bad data !" << std::endl; */ 8 } 9 10 float Vertex::norm() const { return std::sqrt(x*x + y*y + z*z); } 11 12 Vertex Vertex::projectOn(Vertex v) const { 13 // http://www.developpez.net/forums/d202580/applications/developpement-2d-3d-jeux/contribuez/faq-mat-quat-ajout-calculs-vectoriels/ 14 float scalaire = (this->x)*(v.x) + (this->y)*(v.y) + (this->z)*(v.z); 15 float norme = v.norm(); 16 return Vertex(v.x, v.y, v.z) * scalaire / (norme * norme); 17 } 18 19 Vertex Vertex::setNorm(float n) const { 20 return (*this * n / norm()); 21 } 22 23 Vertex Vertex::normalize() const { 24 return (*this / norm()); 25 } 26 27 float Vertex::cosAngle(Vertex v) const { 28 // http://www.developpez.net/forums/d202580/applications/developpement-2d-3d-jeux/contribuez/faq-mat-quat-ajout-calculs-vectoriels/ 29 return ((this->x*v.x + this->y*v.y + this->z*v.z) / (norm()*v.norm())); 30 } 31 32 float Vertex::angle(Vertex v) const { 33 return std::acos(cosAngle(v)); 34 } 35 36 std::ostream& operator<<(std::ostream& os, const Vertex& v) { 37 return os << "(" << v.x << "," << v.y << "," << v.z << ")"; 38 } 39 40 41 Vertex operator+(const Vertex& u, const Vertex& v) { 42 return Vertex(u.x + v.x, u.y + v.y, u.z + v.z); 43 } 44 45 Vertex operator-(const Vertex& u, const Vertex& v) { 46 return Vertex(u.x - v.x, u.y - v.y, u.z - v.z); 47 } 48 49 Vertex operator-(const Vertex& v) { 50 return Vertex(-v.x, -v.y, -v.z); 51 } 52 53 Vertex operator*(const Vertex& v, const float n) { 54 return Vertex(v.x * n, v.y * n, v.z * n); 55 } 56 57 Vertex operator*(const Vertex& u, const Vertex& v) { 58 return Vertex( 59 (u.y * v.z) - (u.z * v.y), 60 (u.z * v.x) - (u.x * v.z), 61 (u.x * v.y) - (u.y * v.x) 62 ); 63 } 64 65 Vertex operator/(const Vertex& v, const float f) { 66 return Vertex(v.x / f, v.y / f, v.z / f); 67 } 68 69 Vertex Vertex::fromSpherical(float r, float xAngle, float yAngle) { 70 // http://electron9.phys.utk.edu/vectors/3dcoordinates.htm 71 return Vertex( 72 r * std::sin(xAngle / 180.f * Angle::Pi) * std::cos(yAngle / 180.f * Angle::Pi), 73 r * std::sin(xAngle / 180.f * Angle::Pi) * std::sin(yAngle / 180.f * Angle::Pi), 74 r * std::cos(xAngle / 180.f * Angle::Pi) 75 ); 76 }