zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Vector3f.hxx
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ZENI_VECTOR3F_HXX
19 #define ZENI_VECTOR3F_HXX
20 
21 #include <Zeni/Vector3f.h>
22 
23 // HXXed below
24 #include <Zeni/Coordinate.h>
25 #include <Zeni/Vector2f.h>
26 
27 // Not HXXed
28 #include <cassert>
29 #include <cmath>
30 
31 namespace Zeni {
32 
33  Vector3f::Vector3f(const bool &degenerate_)
34  : i(0.0f), j(0.0f), k(0.0f), degenerate(degenerate_)
35  {
36  }
37 
38  Vector3f::Vector3f(const float &i_, const float &j_, const float &k_, const bool &degenerate_)
39  : i(i_), j(j_), k(k_), degenerate(degenerate_)
40  {
41  }
42 
43  Vector3f::Vector3f(const Vector3f &rhs, const bool &degenerate_)
44  : i(rhs.i), j(rhs.j), k(rhs.k), degenerate(rhs.degenerate || degenerate_)
45  {
46  }
47 
49  : i(rhs.x), j(rhs.y), k(rhs.z)
50  {
51  }
52 
54  : i(rhs.x), j(rhs.y), k(0.0f)
55  {
56  }
57 
59  return Vector3f(i + rhs.i,
60  j + rhs.j,
61  k + rhs.k,
62  degenerate || rhs.degenerate);
63  }
64 
66  return Vector3f(i - rhs.i,
67  j - rhs.j,
68  k - rhs.k,
69  degenerate || rhs.degenerate);
70  }
71 
73  i += rhs.i;
74  j += rhs.j;
75  k += rhs.k;
76  degenerate |= rhs.degenerate;
77  return *this;
78  }
79 
81  i -= rhs.i;
82  j -= rhs.j;
83  k -= rhs.k;
84  degenerate |= rhs.degenerate;
85  return *this;
86  }
87 
88  float Vector3f::operator*(const Vector3f &rhs) const {
89  return
90  i * rhs.i +
91  j * rhs.j +
92  k * rhs.k;
93  }
94 
96  return Vector3f(j * rhs.k - rhs.j *k,
97  rhs.i *k - i * rhs.k,
98  i * rhs.j - rhs.i *j,
99  degenerate || rhs.degenerate);
100  }
101 
103  degenerate |= rhs.degenerate;
104  return *this = *this % rhs;
105  }
106 
107  Vector3f Vector3f::operator*(const float &rhs) const {
108  return Vector3f(i * rhs, j * rhs, k * rhs, degenerate);
109  }
110 
111  Vector3f Vector3f::operator/(const float &rhs) const {
112  return Vector3f(i / rhs, j / rhs, k / rhs, degenerate);
113  }
114 
115  Vector3f & Vector3f::operator*=(const float &rhs) {
116  i *= rhs;
117  j *= rhs;
118  k *= rhs;
119  return *this;
120  }
121 
122  Vector3f & Vector3f::operator/=(const float &rhs) {
123  i /= rhs;
124  j /= rhs;
125  k /= rhs;
126  return *this;
127  }
128 
130  return *this * -1;
131  }
132 
133  // Vector Scalar Multiplication Part II of II
134  Vector3f operator*(const float &lhs, const Vector3f &rhs) {
135  return rhs * lhs;
136  }
137 
138  float Vector3f::magnitude2() const {
139  return i * i + j * j + k * k;
140  }
141 
142  float Vector3f::magnitude() const {
143  return float(sqrt(magnitude2()));
144  }
145 
147  return Vector3f(i, 0.0f, 0.0f);
148  }
149 
151  return Vector3f(0.0f, j, 0.0f);
152  }
153 
155  return Vector3f(0.0f, 0.0f, k);
156  }
157 
159  return Vector3f(i, j, 0.0f);
160  }
161 
163  return Vector3f(i, 0.0f, k);
164  }
165 
167  return Vector3f(0.0f, j, k);
168  }
169 
171  return Vector3f(i*rhs.i, j*rhs.j, k*rhs.k, degenerate || rhs.degenerate);
172  }
173 
175  return Vector3f(i/rhs.i, j/rhs.j, k/rhs.k, degenerate || rhs.degenerate);
176  }
177 
178  float Vector3f::angle_between(const Vector3f &rhs) const {
179  const float a = magnitude();
180  const float b = rhs.magnitude();
181  const float c = (rhs - *this).magnitude();
182 
183  return float(acos((a * a + b * b - c * c) / (2 * a * b)));
184  }
185 
186  const float & Vector3f::operator[](const int &index) const {
187  assert(-1 < index && index < 3);
188  const float * const ptr = &i;
189  return ptr[index];
190  }
191 
192  float & Vector3f::operator[](const int &index) {
193  assert(-1 < index && index < 3);
194  float * const ptr = &i;
195  return ptr[index];
196  }
197 
198 }
199 
200 #include <Zeni/Coordinate.hxx>
201 #include <Zeni/Vector2f.hxx>
202 
203 #endif
float magnitude2() const
Get the &#39;magnitude squared&#39; of the vector.
Definition: Vector3f.hxx:138
Vector3f get_ik() const
Get just the i and k parts.
Definition: Vector3f.hxx:162
Vector3f operator/(const float &rhs) const
Get the scalar... something.
Definition: Vector3f.hxx:111
GLclampf f
Definition: glew.h:3390
Vector3f & operator%=(const Vector3f &rhs)
Set equal to the cross-product.
Definition: Vector3f.hxx:102
int32_t k
Definition: e_log.c:102
Vector3f & operator/=(const float &rhs)
Set equal to the scalar something.
Definition: Vector3f.hxx:122
EGLSurface EGLint x
Definition: eglext.h:293
int32_t j
Definition: e_log.c:102
Vector3f get_i() const
Get just the i-part.
Definition: Vector3f.hxx:146
Vector3f divide_by(const Vector3f &rhs) const
Divide corresponding members.
Definition: Vector3f.hxx:174
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8736
Vector3f multiply_by(const Vector3f &rhs) const
Multiply corresponding members.
Definition: Vector3f.hxx:170
#define assert(x)
Definition: SDL_malloc.c:1234
Vector3f get_ij() const
Get just the i and j parts.
Definition: Vector3f.hxx:158
float magnitude() const
Get the magnitude of the vector.
Definition: Vector3f.hxx:142
Vector3f get_j() const
Get just the j-part.
Definition: Vector3f.hxx:150
Vector3f get_jk() const
Get just the j and k parts.
Definition: Vector3f.hxx:166
A 3D Point represented with floats.
Definition: Coordinate.h:133
float operator*(const Vector3f &rhs) const
Get the dot-product.
Definition: Vector3f.hxx:88
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
bool degenerate
Definition: Vector3f.h:123
Vector3f & operator*=(const float &rhs)
Set equal to the scalar multiple.
Definition: Vector3f.hxx:115
const GLfloat * c
Definition: glew.h:14913
Vector3f get_k() const
Get just the k-part.
Definition: Vector3f.hxx:154
GLuint index
Definition: glew.h:1800
Quaternion operator*(const float &lhs, const Quaternion &rhs)
Definition: Quaternion.h:110
const float & operator[](const int &index) const
Get &#39;index&#39;.
Definition: Vector3f.hxx:186
float angle_between(const Vector3f &rhs) const
Find the angle between the Vector3fs.
Definition: Vector3f.hxx:178
Vector3f(const bool &degenerate_=false)
The best way to create a Vector3f.
Definition: Vector3f.hxx:33
Vector3f operator-() const
Get the negation.
Definition: Vector3f.hxx:129
Vector3f & operator-=(const Vector3f &rhs)
Set equal to the difference.
Definition: Vector3f.hxx:80
EGLSurface EGLint EGLint y
Definition: eglext.h:293
Vector3f & operator+=(const Vector3f &rhs)
Set equal to the sum.
Definition: Vector3f.hxx:72
Vector3f operator%(const Vector3f &rhs) const
Get the cross-product.
Definition: Vector3f.hxx:95
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
GLint GLint GLint GLint z
Definition: gl2ext.h:1214
int i
Definition: pngrutil.c:1377
Vector3f operator+(const Vector3f &rhs) const
Get the sum.
Definition: Vector3f.hxx:58
A 2-Space Vector Class.
Definition: Vector2f.h:41