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
9 class Vector {
10
11 public:
12
13 Vector();
14 Vector(double x, double y, double z);
15
16
17 bool operator==(const Vector&) const;
18 bool operator!=(const Vector&) const;
19 bool operator<(const Vector&) const;
20
21
22
23 double& operator[](int i);
24 double operator[](int i) const;
25
26
27
28
29
30 Vector& operator+=(const Vector&);
31 Vector& operator-=(const Vector&);
32 Vector& operator*=(const Vector&);
33 Vector& operator*=(double);
34
35
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
45 Vector normalized() const;
46 bool normalize();
47
48
49
50
51
52 friend inline double abs(const Vector&);
53 friend inline double abs2(const Vector&);
54 friend inline double dotProduct(const Vector&, const Vector&);
55
56
57
58 friend inline Vector makeCWTriangleNormal (
59 const Vector& p0, const Vector& p1, const Vector& p2
60 );
61 friend inline Vector makeCCWTriangleNormal (
62 const Vector& p0, const Vector& p1, const Vector& p2
63 );
64
65
66
67
68 friend ostream& operator<<(ostream&, const Vector&);
69 friend istream& operator>>(istream&, Vector&);
70
71
72 double* rep() const;
73
74
75
76
77 private:
78 double v_[3];
79 };
80
81 inline Vector::Vector() {
82 v_[0] = v_[1] = v_[2] = 0;
83 }
84
85 inline Vector::Vector(double x, double y, double z) {
86 v_[0] = x; v_[1] = y; v_[2] = z;
87 }
88
89 inline double Vector::operator[](int i) const { return v_[i]; }
90
91 inline double& Vector::operator[](int i) { return v_[i]; }
92
93 inline bool Vector::operator==(const Vector& u) const {
94 return(v_[0]==u.v_[0] && v_[1]==u.v_[1] && v_[2]==u.v_[2]);
95 }
96
97 inline bool Vector::operator!=(const Vector& u) const {
98 return(v_[0]!=u.v_[0] || v_[1]!=u.v_[1] || v_[2]!=u.v_[2]);
99 }
100
101 inline Vector& Vector::operator+=(const Vector& u) {
102 v_[0] += u.v_[0];
103 v_[1] += u.v_[1];
104 v_[2] += u.v_[2];
105 return *this;
106 }
107
108 inline Vector& Vector::operator-=(const Vector& u) {
109 v_[0] -= u.v_[0];
110 v_[1] -= u.v_[1];
111 v_[2] -= u.v_[2];
112 return *this;
113 }
114
115 inline Vector& Vector::operator*=(double t) {
116 v_[0] *= t;
117 v_[1] *= t;
118 v_[2] *= t;
119 return *this;
120 }
121
122 inline Vector Vector::operator+(const Vector& u) const {
123 return Vector(v_[0]+u.v_[0],v_[1]+u.v_[1],v_[2]+u.v_[2]);
124 }
125
126 inline Vector Vector::operator-(const Vector& u) const {
127 return Vector(v_[0]-u.v_[0],v_[1]-u.v_[1],v_[2]-u.v_[2]);
128 }
129
130 inline Vector Vector::operator*(const Vector& w) const {
131 return Vector(v_[1]*w.v_[2]-v_[2]*w.v_[1],
132 v_[2]*w.v_[0]-v_[0]*w.v_[2],
133 v_[0]*w.v_[1]-v_[1]*w.v_[0]);
134 }
135
136 inline Vector Vector::operator/(double s) const {
137 return Vector(v_[0]/s, v_[1]/s, v_[2]/s);
138 }
139
140 inline Vector Vector::operator-() const {
141 return Vector(-v_[0], -v_[1], -v_[2]);
142 }
143
144 inline Vector Vector::operator*(double f) const {
145 return Vector(v_[0]*f, v_[1]*f, v_[2]*f);
146 }
147
148 inline Vector operator*(double s, const Vector& u) {
149 return Vector(u[0]*s, u[1]*s, u[2]*s);
150 }
151
152 inline double dotProduct(const Vector& u1, const Vector& u2) {
153 return(u1[0]*u2[0] + u1[1]*u2[1] + u1[2]*u2[2]);
154 }
155
156 inline double abs(const Vector& u) { return(sqrt(dotProduct(u,u))); }
157 inline double abs2(const Vector& u) { return dotProduct(u,u); }
158
159 inline Vector makeCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
160 return(((p0 - p1) * (p1 - p2)).normalized());
161 }
162
163 inline Vector makeCCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
164 return(makeCWTriangleNormal(p2, p1, p0));
165 }
166
167 inline double* Vector::rep() const {
168 return (double*)(&(v_[0]));
169 }
170
171 #endif // _VECTOR_H
172