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     friend inline Vector makeCCWTriangleNormal (
 68         const Vector& p0, const Vector& p1, const Vector& p2     )
;
 69         //. Computes ((p0-p1)*(p1-p2)).normalized, i.e. a vector that is
 70         //. perpendicular the the plane defined by the three vertices.
 71
 72     //- operator<<, operator>>
 73     friend  ostream& operator<<(ostream&, const Vector&);
 74     friend  istream& operator>>(istream&, Vector&);
 75
 76     //- rep
 77     double* rep() const;
 78         //. Direct data access. It is guaranteed that the
 79         //. vector components are stored continuously.
 80    
 81
 82 private:
 83     double v_[3];
 84 };
 85
 86 inline Vector::Vector(double x, double y, double z) {
 87     v_[0] = x; v_[1] = y; v_[2] = z;
 88 }
 89
 90 inline double Vector::operator[](int i) const { return v_[i]; }
 91
 92 inline double& Vector::operator[](int i) { return v_[i]; }
 93
 94 inline bool Vector::operator==(const Vector& u) const {
 95     return(v_[0]==u.v_[0] && v_[1]==u.v_[1] && v_[2]==u.v_[2]);
 96 }
 97
 98 inline bool Vector::operator!=(const Vector& u) const {
 99     return(v_[0]!=u.v_[0] || v_[1]!=u.v_[1] || v_[2]!=u.v_[2]);
100 }
101
102 inline Vector& Vector::operator+=(const Vector& u) {
103     v_[0] += u.v_[0];
104     v_[1] += u.v_[1];
105     v_[2] += u.v_[2];
106     return *this;
107 }
108
109 inline Vector& Vector::operator-=(const Vector& u) {
110     v_[0] -= u.v_[0];
111     v_[1] -= u.v_[1];
112     v_[2] -= u.v_[2];
113     return *this;
114 }
115
116 inline Vector& Vector::operator*=(double t) {
117     v_[0] *= t;
118     v_[1] *= t;
119     v_[2] *= t;
120     return *this;
121 }
122
123 inline Vector Vector::operator+(const Vector& u) const {
124     return Vector(v_[0]+u.v_[0],v_[1]+u.v_[1],v_[2]+u.v_[2]);
125 }
126
127 inline Vector Vector::operator-(const Vector& u) const {
128     return Vector(v_[0]-u.v_[0],v_[1]-u.v_[1],v_[2]-u.v_[2]);
129 }
130
131 inline Vector Vector::operator*(const Vector& w) const {
132     return Vector(v_[1]*w.v_[2]-v_[2]*w.v_[1],
133                    v_[2]*w.v_[0]-v_[0]*w.v_[2],
134                    v_[0]*w.v_[1]-v_[1]*w.v_[0])
;
135 }
136
137 inline Vector Vector::operator/(double s) const {
138     return Vector(v_[0]/s, v_[1]/s, v_[2]/s);
139 }
140
141 inline Vector Vector::operator-() const {
142     return Vector(-v_[0], -v_[1], -v_[2]);
143 }
144
145 inline Vector Vector::operator*(double f) const {
146     return Vector(v_[0]*f, v_[1]*f, v_[2]*f);
147 }
148
149 inline Vector operator*(double s, const Vector& u) {
150     return Vector(u[0]*s, u[1]*s, u[2]*s);
151 }
152
153 inline double dotProduct(const Vector& u1, const Vector& u2) {
154     return(u1[0]*u2[0] + u1[1]*u2[1] + u1[2]*u2[2]);
155 }
156
157 inline double abs(const Vector& u) { return(sqrt(dotProduct(u,u))); }
158 inline double abs2(const Vector& u) { return dotProduct(u,u); }
159
160 inline Vector bezier(const Vector* v, int nPoints, double t)
161 {
162     Vector pts[10];
163
164     for (int n=0; n<nPoints; n++)
165         pts[n] = v[n];
166    
167     for(int outer=1; outer< nPoints; outer++)
168         for(int inner=0; inner<= (nPoints-outer); inner++)
169             pts[inner]+= t* (pts[inner+1]-pts[inner]);
170
171     return pts[0];
172 }
173
174 inline Vector makeCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
175     return(((p0 - p1) * (p1 - p2)).normalized());
176 }
177
178 inline Vector makeCCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
179     return(makeCWTriangleNormal(p2, p1, p0));
180 }
181
182 inline double* Vector::rep() const {
183     return (double*)(&(v_[0]));
184 }
185
186 #endif // _VECTOR_H
187
188