sources:
attribute.cpp (1.1k)
attribute.h (1.5k)
cgapplication.cpp (4.1k)
cgapplication.h (4.8k)
cgrobot.cpp (7.9k)
cgrobot.h (716 bytes)
node.cpp (2.4k)
node.h (2.3k)
shape.cpp (3.2k)
shape.h (966 bytes)
transformation.cpp (2.9k)
transformation.h (2.4k)
vector.cpp (1.4k)
vector.h (5.3k)


website:
more info here


screenshot:
studies/grafik/Computergrafik-Code8/vector.h
download file

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