studies/grafik2/Computergrafik-Code9/Aufgabe23/vector.h
⇒
download file
1
2
3
4
5
6
7
8
9 #ifndef _VECTOR_H
10 #define _VECTOR_H
11
12 #include <iostream.h>
13 #include <math.h>
14
15
16 class Vector {
17
18 public:
19
20 Vector(double x = 0, double y = 0, double z = 0);
21
22
23 bool operator==(const Vector&) const;
24 bool operator!=(const Vector&) const;
25 bool operator<(const Vector&) const;
26
27
28
29 double& operator[](int i);
30 double operator[](int i) const;
31
32
33
34
35
36 Vector& operator+=(const Vector&);
37 Vector& operator-=(const Vector&);
38 Vector& operator*=(const Vector&);
39 Vector& operator*=(double);
40
41
42 Vector operator+(const Vector&) const;
43 Vector operator-(const Vector&) const;
44 Vector operator*(const Vector&) const;
45 Vector operator/(double) const;
46 Vector operator-() const;
47 Vector operator*(double) const;
48 friend inline Vector operator*(double, const Vector&);
49
50
51 Vector normalized() const;
52 bool normalize();
53
54
55
56
57
58 friend inline double abs(const Vector&);
59 friend inline double abs2(const Vector&);
60 friend inline double dotProduct(const Vector&, const Vector&);
61
62
63
64 friend inline Vector makeCWTriangleNormal (
65 const Vector& p0, const Vector& p1, const Vector& p2
);
66 friend inline Vector makeCCWTriangleNormal (
67 const Vector& p0, const Vector& p1, const Vector& p2
);
68
69
70
71
72 friend ostream& operator<<(ostream&, const Vector&);
73 friend istream& operator>>(istream&, Vector&);
74
75
76 double* rep() const;
77
78
79
80
81 private:
82 double v_[3];
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
173