studies/grafik2/Computergrafik-Code4/Aufgabe10/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 );
67 friend inline Vector makeCCWTriangleNormal (
68 const Vector& p0, const Vector& p1, const Vector& p2
69 );
70
71
72
73
74 friend ostream& operator<<(ostream&, const Vector&);
75 friend istream& operator>>(istream&, Vector&);
76
77
78 double* rep() const;
79
80
81
82
83 private:
84 double v_[3];
85 };
86
87 inline Vector::Vector(double x, double y, double z) {
88 v_[0] = x; v_[1] = y; v_[2] = z;
89 }
90
91 inline double Vector::operator[](int i) const { return v_[i]; }
92
93 inline double& Vector::operator[](int i) { return v_[i]; }
94
95 inline bool Vector::operator==(const Vector& u) const {
96 return(v_[0]==u.v_[0] && v_[1]==u.v_[1] && v_[2]==u.v_[2]);
97 }
98
99 inline bool Vector::operator!=(const Vector& u) const {
100 return(v_[0]!=u.v_[0] || v_[1]!=u.v_[1] || v_[2]!=u.v_[2]);
101 }
102
103 inline Vector& Vector::operator+=(const Vector& u) {
104 v_[0] += u.v_[0];
105 v_[1] += u.v_[1];
106 v_[2] += u.v_[2];
107 return *this;
108 }
109
110 inline Vector& Vector::operator-=(const Vector& u) {
111 v_[0] -= u.v_[0];
112 v_[1] -= u.v_[1];
113 v_[2] -= u.v_[2];
114 return *this;
115 }
116
117 inline Vector& Vector::operator*=(double t) {
118 v_[0] *= t;
119 v_[1] *= t;
120 v_[2] *= t;
121 return *this;
122 }
123
124 inline Vector Vector::operator+(const Vector& u) const {
125 return Vector(v_[0]+u.v_[0],v_[1]+u.v_[1],v_[2]+u.v_[2]);
126 }
127
128 inline Vector Vector::operator-(const Vector& u) const {
129 return Vector(v_[0]-u.v_[0],v_[1]-u.v_[1],v_[2]-u.v_[2]);
130 }
131
132 inline Vector Vector::operator*(const Vector& w) const {
133 return Vector(v_[1]*w.v_[2]-v_[2]*w.v_[1],
134 v_[2]*w.v_[0]-v_[0]*w.v_[2],
135 v_[0]*w.v_[1]-v_[1]*w.v_[0]);
136 }
137
138 inline Vector Vector::operator/(double s) const {
139 return Vector(v_[0]/s, v_[1]/s, v_[2]/s);
140 }
141
142 inline Vector Vector::operator-() const {
143 return Vector(-v_[0], -v_[1], -v_[2]);
144 }
145
146 inline Vector Vector::operator*(double f) const {
147 return Vector(v_[0]*f, v_[1]*f, v_[2]*f);
148 }
149
150 inline Vector operator*(double s, const Vector& u) {
151 return Vector(u[0]*s, u[1]*s, u[2]*s);
152 }
153
154 inline double dotProduct(const Vector& u1, const Vector& u2) {
155 return(u1[0]*u2[0] + u1[1]*u2[1] + u1[2]*u2[2]);
156 }
157
158 inline double abs(const Vector& u) { return(sqrt(dotProduct(u,u))); }
159 inline double abs2(const Vector& u) { return dotProduct(u,u); }
160
161 inline Vector makeCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
162 return(((p0 - p1) * (p1 - p2)).normalized());
163 }
164
165 inline Vector makeCCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
166 return(makeCWTriangleNormal(p2, p1, p0));
167 }
168
169 inline double* Vector::rep() const {
170 return (double*)(&(v_[0]));
171 }
172
173 #endif // _VECTOR_H
174