rename eclipse project
[MicroTrace.git] / Vec3f.cxx
1 #include <cassert>
2
3 #include "Vec3f.hxx"
4
5 Vec3f::Vec3f() {
6 }
7
8 Vec3f::Vec3f(float x, float y, float z) {
9 m_values[0] = x;
10 m_values[1] = y;
11 m_values[2] = z;
12 }
13
14 Vec3f::~Vec3f() {
15 }
16
17 Vec3f::Vec3f(const Vec3f& o) {
18 for(size_t i = 0; i < DIM; i++) {
19 m_values[i] = o.m_values[i];
20 }
21 }
22
23 Vec3f& Vec3f::operator=(const Vec3f& o) {
24 for(size_t i = 0; i < DIM; i++) {
25 m_values[i] = o.m_values[i];
26 }
27 return *this;
28 }
29
30 float Vec3f::operator|(const Vec3f& o) {
31
32 return dot(o);
33 }
34
35 float Vec3f::dot(const Vec3f& o) {
36 float prod = 0;
37 for(size_t i = 0; i < DIM; i++) {
38 prod += m_values[i] * o.m_values[i];
39 }
40 return prod;
41 }
42
43 Vec3f Vec3f::operator%(const Vec3f& o) {
44 return cross(o);
45 }
46
47 Vec3f Vec3f::cross(const Vec3f& o) {
48 return Vec3f(m_values[1] * o.m_values[2] - m_values[2] * o.m_values[1],
49 m_values[2] * o.m_values[0] - m_values[0] * o.m_values[2], m_values[0]
50 * o.m_values[1] - m_values[1] * o.m_values[0]);
51 }
52
53 void Vec3f::normalize() {
54 *this /= norm();
55 }
56
57 float Vec3f::norm() const {
58 return sqrt(m_values[0] * m_values[0] + m_values[1] * m_values[1]
59 + m_values[2] * m_values[2]);
60 }
61
62 Vec3f Vec3f::operator*(const float t) const {
63 Vec3f v(*this);
64 return v *= t;
65 }
66
67 Vec3f& Vec3f::operator*=(const float t) {
68 for(unsigned int i = 0; i < 3; ++i) {
69 m_values[i] *= t;
70 }
71 return *this;
72 }
73
74 Vec3f Vec3f::operator/(const float t) const {
75 Vec3f v(*this);
76 return v /= t;
77 }
78
79 Vec3f& Vec3f::operator/=(const float t) {
80 for(unsigned int i = 0; i < 3; ++i) {
81 m_values[i] /= t;
82 }
83 return *this;
84 }
85
86 // example implementation of a standard operator
87 Vec3f Vec3f::operator+(const Vec3f& o) const {
88 Vec3f v(*this);
89 return v += o;
90 }
91
92 // example implementation of an in-place operator
93 Vec3f& Vec3f::operator+=(const Vec3f& o) {
94 for(unsigned int i = 0; i < 3; ++i) {
95 m_values[i] += o.m_values[i];
96 }
97 return *this;
98 }
99
100 Vec3f Vec3f::operator-(const Vec3f& o) const {
101 Vec3f v(*this);
102 return v -= o;
103 }
104
105 Vec3f& Vec3f::operator-=(const Vec3f& o) {
106 for(unsigned int i = 0; i < 3; ++i) {
107 m_values[i] -= o.m_values[i];
108 }
109 return *this;
110 }
111
112 Vec3f Vec3f::operator*(const Vec3f& o) const {
113 Vec3f v(*this);
114 return v *= o;
115 }
116
117 Vec3f& Vec3f::operator*=(const Vec3f& o) {
118 for(unsigned int i = 0; i < 3; ++i) {
119 m_values[i] *= o.m_values[i];
120 }
121 return *this;
122 }
123
124 Vec3f Vec3f::operator/(const Vec3f& o) const {
125 Vec3f v(*this);
126 return v /= o;
127 }
128
129 Vec3f& Vec3f::operator/=(const Vec3f& o) {
130 for(unsigned int i = 0; i < 3; ++i) {
131 m_values[i] /= o.m_values[i];
132 }
133 return *this;
134 }
135
136 float Vec3f::operator[](unsigned int i) const {
137 assert(i < DIM);
138 return m_values[i];
139 }
140
141 float& Vec3f::operator[](unsigned int i) {
142 assert(i < DIM);
143 return m_values[i];
144 }
This page took 0.060751 seconds and 5 git commands to generate.