-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathVector.hpp
185 lines (129 loc) · 3.55 KB
/
Vector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
////////////////////////////////////////////////////////////////////////////////
//
// Source code for the paper
//
// "Fast Ray--Tetrahedron Intersection"
//
// (c) Nikos Platis, Theoharis Theoharis 2003
//
// Department of Informatics and Telecommunications,
// University of Athens, Greece
// {nplatis|theotheo}@di.uoa.gr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef _Vector_hpp_
#define _Vector_hpp_
#include "Util.h"
#include <cmath>
#include <iostream>
// Vector //////////////////////////////////////////////////////////////////////
class Vector
{
public:
double x;
double y;
double z;
Vector();
Vector(double xx, double yy, double zz);
double Measure2() const;
double Measure() const;
void MakeUnitVector();
// Some operators for vectors
Vector& operator+=(const Vector& vec); // Vector addition
Vector& operator-=(const Vector& vec); // Vector subtraction
bool operator==(const Vector& vec) const; // Equality test
};
// Vector negation
Vector operator-(const Vector& vec);
// Vector addition
Vector operator+(const Vector& vec1, const Vector& vec2);
// Vector subtraction
Vector operator-(const Vector& vec1, const Vector& vec2);
// Dot product
double operator*(const Vector& vec1, const Vector& vec2);
// Scalar product
Vector operator*(double c, const Vector& vec);
// Cross product
Vector operator^(const Vector& vec1, const Vector& vec2);
// Iut/Output of vectors
std::istream& operator>>(std::istream& istr, Vector& vec);
std::ostream& operator<<(std::ostream& ostr, const Vector& vec);
// Inline functions ////////////////////////////////////////////////////////////
inline Vector::Vector()
: x(0), y(0), z(0)
{
}
inline Vector::Vector(double xx, double yy, double zz)
: x(xx), y(yy), z(zz)
{
}
inline double Vector::Measure2() const
{
return (x*x + y*y + z*z);
}
inline double Vector::Measure() const
{
return sqrt(Measure2());
}
inline void Vector::MakeUnitVector()
{
double measure = Measure();
x /= measure;
y /= measure;
z /= measure;
}
inline Vector& Vector::operator+=(const Vector& vec)
{
x += vec.x;
y += vec.y;
z += vec.z;
return *this;
}
inline Vector& Vector::operator-=(const Vector& vec)
{
x -= vec.x;
y -= vec.y;
z -= vec.z;
return *this;
}
inline bool Vector::operator==(const Vector& vec) const
{
return ( (x == vec.x) && (y == vec.y) && (z == vec.z) );
}
inline Vector operator-(const Vector& vec)
{
return Vector(-vec.x, -vec.y, -vec.z);
}
inline Vector operator+(const Vector& vec1, const Vector& vec2)
{
return Vector(vec1.x+vec2.x, vec1.y+vec2.y, vec1.z+vec2.z);
}
inline Vector operator-(const Vector& vec1, const Vector& vec2)
{
return Vector(vec1.x-vec2.x, vec1.y-vec2.y, vec1.z-vec2.z);
}
inline double operator*(const Vector& vec1, const Vector& vec2)
{
return (vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z);
}
inline Vector operator*(double c, const Vector& vec)
{
return Vector(c*vec.x, c*vec.y, c*vec.z);
}
inline Vector operator^(const Vector& vec1, const Vector& vec2)
{
return Vector(vec1.y*vec2.z - vec2.y*vec1.z,
vec1.z*vec2.x - vec2.z*vec1.x,
vec1.x*vec2.y - vec2.x*vec1.y);
}
inline std::istream& operator>>(std::istream& istr, Vector& vec)
{
istr >> vec.x >> vec.y >> vec.z;
return istr;
}
inline std::ostream& operator<<(std::ostream& ostr, const Vector& vec)
{
ostr << vec.x << " " << vec.y << " " << vec.z;
return ostr;
}
#endif // _Vector_hpp_