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 friend inline Vector makeCCWTriangleNormal (
68 const Vector& p0, const Vector& p1, const Vector& p2
);
69
70
71
72
73 friend ostream& operator<<(ostream&, const Vector&);
74 friend istream& operator>>(istream&, Vector&);
75
76
77 double* rep() const;
78
79
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