// // Computergraphik II // Prof. Dr. Juergen Doellner // Wintersemester 2001/02 // // Rahmenprogramm zu Aufgabenzettel 5 // #ifndef _VECTOR_H #define _VECTOR_H #include #include //- Vector class Vector { //. 3D Vector Class. public: //- Vector Vector(double x = 0, double y = 0, double z = 0); //- operator==, operator!=, operator< bool operator==(const Vector&) const; bool operator!=(const Vector&) const; bool operator<(const Vector&) const; //. The comparison is based on the lexiographic order. //- operator[] double& operator[](int i); double operator[](int i) const; //. Indices 0,1, and 2 correspond to the x,y,z coordinates //. of the vector. //. CAUTION: For performance reasons, indices are not checked. //- operator+=, operator-=, operator*= Vector& operator+=(const Vector&); Vector& operator-=(const Vector&); Vector& operator*=(const Vector&); Vector& operator*=(double); //- operator+, operator-, operator*, operator/, operator-, operator* Vector operator+(const Vector&) const; Vector operator-(const Vector&) const; Vector operator*(const Vector&) const; Vector operator/(double) const; Vector operator-() const; Vector operator*(double) const; friend inline Vector operator*(double, const Vector&); //- normalized, normalize Vector normalized() const; bool normalize(); //. `normalized' returns a normalized copy of the vector object. //. `normalize' normalizes the vector object. If the length //. of the vector was > 0, it returns 1 for success, otherwise 0. //- abs, abs2, dotProduct friend inline double abs(const Vector&); friend inline double abs2(const Vector&); friend inline double dotProduct(const Vector&, const Vector&); //. Vector length and scalar product. //- makeCWTriangleNormal, makeCCWTriangleNormal friend inline Vector makeCWTriangleNormal ( const Vector& p0, const Vector& p1, const Vector& p2 ); friend inline Vector makeCCWTriangleNormal ( const Vector& p0, const Vector& p1, const Vector& p2 ); //. Computes ((p0-p1)*(p1-p2)).normalized, i.e. a vector that is //. perpendicular the the plane defined by the three vertices. //- operator<<, operator>> friend ostream& operator<<(ostream&, const Vector&); friend istream& operator>>(istream&, Vector&); //- rep double* rep() const; //. Direct data access. It is guaranteed that the //. vector components are stored continuously. private: double v_[3]; }; inline Vector::Vector(double x, double y, double z) { v_[0] = x; v_[1] = y; v_[2] = z; } inline double Vector::operator[](int i) const { return v_[i]; } inline double& Vector::operator[](int i) { return v_[i]; } inline bool Vector::operator==(const Vector& u) const { return(v_[0]==u.v_[0] && v_[1]==u.v_[1] && v_[2]==u.v_[2]); } inline bool Vector::operator!=(const Vector& u) const { return(v_[0]!=u.v_[0] || v_[1]!=u.v_[1] || v_[2]!=u.v_[2]); } inline Vector& Vector::operator+=(const Vector& u) { v_[0] += u.v_[0]; v_[1] += u.v_[1]; v_[2] += u.v_[2]; return *this; } inline Vector& Vector::operator-=(const Vector& u) { v_[0] -= u.v_[0]; v_[1] -= u.v_[1]; v_[2] -= u.v_[2]; return *this; } inline Vector& Vector::operator*=(double t) { v_[0] *= t; v_[1] *= t; v_[2] *= t; return *this; } inline Vector Vector::operator+(const Vector& u) const { return Vector(v_[0]+u.v_[0],v_[1]+u.v_[1],v_[2]+u.v_[2]); } inline Vector Vector::operator-(const Vector& u) const { return Vector(v_[0]-u.v_[0],v_[1]-u.v_[1],v_[2]-u.v_[2]); } inline Vector Vector::operator*(const Vector& w) const { return Vector(v_[1]*w.v_[2]-v_[2]*w.v_[1], v_[2]*w.v_[0]-v_[0]*w.v_[2], v_[0]*w.v_[1]-v_[1]*w.v_[0]); } inline Vector Vector::operator/(double s) const { return Vector(v_[0]/s, v_[1]/s, v_[2]/s); } inline Vector Vector::operator-() const { return Vector(-v_[0], -v_[1], -v_[2]); } inline Vector Vector::operator*(double f) const { return Vector(v_[0]*f, v_[1]*f, v_[2]*f); } inline Vector operator*(double s, const Vector& u) { return Vector(u[0]*s, u[1]*s, u[2]*s); } inline double dotProduct(const Vector& u1, const Vector& u2) { return(u1[0]*u2[0] + u1[1]*u2[1] + u1[2]*u2[2]); } inline double abs(const Vector& u) { return(sqrt(dotProduct(u,u))); } inline double abs2(const Vector& u) { return dotProduct(u,u); } inline Vector makeCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) { return(((p0 - p1) * (p1 - p2)).normalized()); } inline Vector makeCCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) { return(makeCWTriangleNormal(p2, p1, p0)); } inline double* Vector::rep() const { return (double*)(&(v_[0])); } #endif // _VECTOR_H