sources:


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code6/Aufgabe17/VECTOR.H
download file

  1 #ifndef _VECTOR_H
  2 #define _VECTOR_H
  3
  4 #include <assert.h>
  5 #include <iostream.h>
  6 #include <math.h>
  7
  8 //- Vector
  9 class Vector {
 10 //. 3D Vector Class.
 11 public:
 12     //- Vector
 13     Vector();
 14     Vector(double x, double y, double z);
 15
 16     //- operator==, operator!=, operator<
 17     bool operator==(const Vector&) const;
 18     bool operator!=(const Vector&) const;
 19     bool operator<(const Vector&) const;
 20         //. The comparison is based on the lexiographic order.
 21
 22     //- operator[]
 23     double& operator[](int i);
 24     double operator[](int i) const;
 25         //. Indices 0,1, and 2 correspond to the x,y,z coordinates
 26         //. of the vector.
 27         //. CAUTION: For performance reasons, indices are not checked.
 28
 29     //- operator+=, operator-=, operator*=
 30     Vector& operator+=(const Vector&);
 31     Vector& operator-=(const Vector&);
 32     Vector& operator*=(const Vector&);
 33     Vector& operator*=(double);
 34
 35     //- operator+, operator-, operator*, operator/, operator-, operator*
 36     Vector operator+(const Vector&) const;
 37     Vector operator-(const Vector&) const;
 38     Vector operator*(const Vector&) const;
 39     Vector operator/(double) const;
 40     Vector operator-() const;
 41     Vector operator*(double) const;
 42     friend inline Vector operator*(double, const Vector&);
 43
 44     //- normalized, normalize
 45     Vector normalized() const;
 46     bool normalize();
 47         //. `normalized' returns a normalized copy of the vector object.
 48         //. `normalize' normalizes the vector object. If the length
 49         //. of the vector was > 0, it returns 1 for success, otherwise 0.
 50
 51     //- abs, abs2, dotProduct
 52     friend inline double abs(const Vector&);
 53     friend inline double abs2(const Vector&);
 54     friend inline double dotProduct(const Vector&, const Vector&);
 55         //. Vector length and scalar product.
 56
 57     //- makeCWTriangleNormal, makeCCWTriangleNormal
 58     friend inline Vector makeCWTriangleNormal (
 59         const Vector& p0, const Vector& p1, const Vector& p2     )
;
 60     friend inline Vector makeCCWTriangleNormal (
 61         const Vector& p0, const Vector& p1, const Vector& p2     )
;
 62         //. Computes ((p0-p1)*(p1-p2)).normalized, i.e. a vector that is
 63         //. perpendicular the the plane defined by the three vertices.
 64
 65     //- operator<<, operator>>
 66     friend  ostream& operator<<(ostream&, const Vector&);
 67     friend  istream& operator>>(istream&, Vector&);
 68
 69     //- rep
 70     double* rep() const;
 71         //. Direct data access. It is guaranteed that the
 72         //. vector components are stored continuously.
 73    
 74
 75 private:
 76     double v_[3];
 77 };
 78
 79 inline Vector::Vector() {
 80     v_[0] = v_[1] = v_[2] = 0;
 81 }
 82
 83 inline Vector::Vector(double x, double y, double z) {
 84     v_[0] = x; v_[1] = y; v_[2] = z;
 85 }
 86
 87 inline double Vector::operator[](int i) const { return v_[i]; }
 88
 89 inline double& Vector::operator[](int i) { return v_[i]; }
 90
 91 inline bool Vector::operator==(const Vector& u) const {
 92     return(v_[0]==u.v_[0] && v_[1]==u.v_[1] && v_[2]==u.v_[2]);
 93 }
 94
 95 inline bool Vector::operator!=(const Vector& u) const {
 96     return(v_[0]!=u.v_[0] || v_[1]!=u.v_[1] || v_[2]!=u.v_[2]);
 97 }
 98
 99 inline Vector& Vector::operator+=(const Vector& u) {
100     v_[0] += u.v_[0];
101     v_[1] += u.v_[1];
102     v_[2] += u.v_[2];
103     return *this;
104 }
105
106 inline Vector& Vector::operator-=(const Vector& u) {
107     v_[0] -= u.v_[0];
108     v_[1] -= u.v_[1];
109     v_[2] -= u.v_[2];
110     return *this;
111 }
112
113 inline Vector& Vector::operator*=(double t) {
114     v_[0] *= t;
115     v_[1] *= t;
116     v_[2] *= t;
117     return *this;
118 }
119
120 inline Vector Vector::operator+(const Vector& u) const {
121     return Vector(v_[0]+u.v_[0],v_[1]+u.v_[1],v_[2]+u.v_[2]);
122 }
123
124 inline Vector Vector::operator-(const Vector& u) const {
125     return Vector(v_[0]-u.v_[0],v_[1]-u.v_[1],v_[2]-u.v_[2]);
126 }
127
128 inline Vector Vector::operator*(const Vector& w) const {
129     return Vector(v_[1]*w.v_[2]-v_[2]*w.v_[1],
130                    v_[2]*w.v_[0]-v_[0]*w.v_[2],
131                    v_[0]*w.v_[1]-v_[1]*w.v_[0])
;
132 }
133
134 inline Vector Vector::operator/(double s) const {
135     return Vector(v_[0]/s, v_[1]/s, v_[2]/s);
136 }
137
138 inline Vector Vector::operator-() const {
139     return Vector(-v_[0], -v_[1], -v_[2]);
140 }
141
142 inline Vector Vector::operator*(double f) const {
143     return Vector(v_[0]*f, v_[1]*f, v_[2]*f);
144 }
145
146 inline Vector operator*(double s, const Vector& u) {
147     return Vector(u[0]*s, u[1]*s, u[2]*s);
148 }
149
150 inline double dotProduct(const Vector& u1, const Vector& u2) {
151     return(u1[0]*u2[0] + u1[1]*u2[1] + u1[2]*u2[2]);
152 }
153
154 inline double abs(const Vector& u) { return(sqrt(dotProduct(u,u))); }
155 inline double abs2(const Vector& u) { return dotProduct(u,u); }
156
157 inline Vector makeCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
158     return(((p0 - p1) * (p1 - p2)).normalized());
159 }
160
161 inline Vector makeCCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
162     return(makeCWTriangleNormal(p2, p1, p0));
163 }
164
165 inline double* Vector::rep() const {
166     return (double*)(&(v_[0]));
167 }
168
169 #endif // _VECTOR_H
170
171