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
|
/*
common/vector3f.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
// project headers
#include "math/vector3f.h"
// C++ headers
#include <cmath>
namespace math
{
Vector3f::Vector3f() :
x(coord[0]), y(coord[1]), z(coord[2])
{
for (int i=0; i < 3; i++)
coord[i] = 0;
}
Vector3f::Vector3f(const Vector3f &other) :
x(coord[0]), y(coord[1]), z(coord[2])
{
for (int i=0; i < 3; i++)
coord[i] = other.coord[i];
}
Vector3f::Vector3f(const float xv, const float yv, const float zv) :
x(coord[0]), y(coord[1]), z(coord[2])
{
coord[0] = xv;
coord[1] = yv;
coord[2] = zv;
}
Vector3f::~Vector3f()
{
}
Vector3f & Vector3f::operator=(const Vector3f & other)
{
for (int i=0; i < 3; i++)
coord[i] = other.coord[i];
return (*this);
}
Vector3f & Vector3f::operator*=(const float scalar)
{
for (int i=0; i < 3; i++)
coord[i] *= scalar;
return (*this);
}
Vector3f & Vector3f::operator/=(const float scalar)
{
for (int i=0; i < 3; i++)
coord[i] /= scalar;
return (*this);
}
Vector3f &Vector3f::operator-=(const Vector3f & other)
{
for (int i=0; i < 3; i++)
coord[i] -= other[i];
return (*this);
}
Vector3f &Vector3f::operator+=(const Vector3f &other)
{
for (int i=0; i < 3; i++)
coord[i] += other[i];
return (*this);
}
Vector3f Vector3f::operator*(const float scalar) const
{
Vector3f r(*this);
for (int i=0; i < 3; i++)
r.coord[i] *= scalar;
return (r);
}
Vector3f Vector3f::operator/(const float scalar) const
{
Vector3f r(*this);
for (int i=0; i < 3; i++)
r.coord[i] /= scalar;
return (r);
}
Vector3f Vector3f::operator-(const Vector3f& other) const
{
Vector3f r(*this);
for (int i=0; i < 3; i++)
r.coord[i] -= other.coord[i];
return (r);
}
Vector3f Vector3f::operator+(const Vector3f& other) const
{
Vector3f r(*this);
for (int i=0; i < 3; i++)
r.coord[i] += other.coord[i];
return (r);
}
bool Vector3f::operator==(const Vector3f& other) const
{
for (int i=0; i < 3; i++)
if (coord[i] != other.coord[i])
return (false);
return (true);
}
float Vector3f::lengthsquared() const
{
double r = 0;
for (int i=0; i < 3; i++)
r += coord[i]*coord[i];
return ((float) r);
}
float Vector3f::length() const
{
double r = 0;
for (int i=0; i < 3; i++)
r += coord[i]*coord[i];
return ((float) sqrt(r));
}
void Vector3f::normalize()
{
(*this) /= this->length();
}
Vector3f operator*(float scalar, const Vector3f& vector)
{
return vector * scalar;
}
std::ostream &operator<<(std::ostream & os, Vector3f const & vector)
{
os << vector[0] << " " << vector[1] << " " << vector[2];
return os;
}
std::istream &operator>>(std::istream & is, Vector3f & vector)
{
for (int i=0; i < 3; i++)
is >> vector[i];
return is;
}
const Vector3f crossproduct(Vector3f const & first, Vector3f const& second)
{
float x = first[1]*second[2] - first[2]*second[1];
float y = first[2]*second[0] - first[0]*second[2];
float z = first[0]*second[1] - first[1]*second[0];
return(Vector3f(x,y,z));
}
float dotproduct(const Vector3f& first, const Vector3f& second)
{
float r = 0;
for (int i=0; i < 3; i++)
r += first[i] * second[i];
return (r);
}
} // namespace math
|