1
2
3
4
5
6
7 #ifndef _VECTOR_H
8 #define _VECTOR_H
9
10 #include <iostream.h>
11 #include <math.h>
12
13
14 class Vector {
15
16 public:
17
18 Vector(double x = 0, double y = 0, double z = 0);
19
20
21 bool operator==(const Vector&) const;
22 bool operator!=(const Vector&) const;
23 bool operator<(const Vector&) const;
24
25
26
27 double& operator[](int i);
28 double operator[](int i) const;
29
30
31
32
33
34 Vector& operator+=(const Vector&);
35 Vector& operator-=(const Vector&);
36 Vector& operator*=(const Vector&);
37 Vector& operator*=(double);
38
39
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
49 Vector normalized() const;
50 bool normalize();
51
52
53
54
55
56 friend inline double abs(const Vector&);
57 friend inline double abs2(const Vector&);
58 friend inline double dotProduct(const Vector&, const Vector&);
59
60
61
62 friend inline Vector bezier(const Vector* v, int nPoints, double t);
63
64
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
72
73
74
75 friend ostream& operator<<(ostream&, const Vector&);
76 friend istream& operator>>(istream&, Vector&);
77
78
79 double* rep() const;
80
81
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