zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Vector3f.cpp
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 #include <zeni.h>
19 
20 #include <cmath>
21 
22 #include <Zeni/Define.h>
23 
24 namespace Zeni {
25 
26  namespace Global {
27  const float pi = float(acos(-1.0f));
28  const float pi_over_two = float(acos(-1.0f)) / 2.0f;
29  const float three_pi_over_two = 3.0f * float(acos(-1.0f)) / 2.0f;
30  const float over_three = 1.0f / 3.0f;
31  const float sqrt_two = float(sqrt(2.0f));
32  const float sqrt_three = float(sqrt(3.0f));
33 
34  const Vector3f vector_i(1, 0, 0);
35  const Vector3f vector_j(0, 1, 0);
36  const Vector3f vector_k(0, 0, 1);
37  }
38 
40  float mplier = magnitude();
41 
42  if(INFINTESSIMAL(mplier)) {
43  degenerate = true;
44  return *this;
45  }
46  else
47  degenerate = false;
48 
49  mplier = 1.0f / mplier;
50 
51  i *= mplier;
52  j *= mplier;
53  k *= mplier;
54 
55  return *this;
56  }
57 
59  float mplier = magnitude();
60 
61  if(INFINTESSIMAL(mplier))
62  return Vector3f(*this, true);
63 
64  mplier = 1.0f / mplier;
65 
66  return Vector3f(i * mplier, j * mplier, k * mplier);
67  }
68 
69  float Vector3f::theta() const {
70  if(i > 0)
71  return float(atan(j/i));
72  else if(i < 0)
73  return float(atan(j/i)) + Global::pi;
74  else if(j > 0)
75  return Global::pi_over_two;
76  else if(j < 0)
78  return 0;
79  }
80 
81  float Vector3f::phi() const {
82  const float xy_mag = float(sqrt(pow(i, 2) + pow(j, 2)));
83 
84  if(xy_mag > 0.0f)
85  return Global::pi_over_two + float(atan(-k / xy_mag));
86 
87  if(k < 0.0f)
88  return Global::pi;
89 
90  return 0.0f;
91  }
92 
93  void Vector3f::set_spherical(const float &theta, const float &phi, const float &magnitude) {
94  i = float(sin(phi)) * magnitude;
95  j = float(sin(theta)) * i;
96  i *= float(cos(theta));
97  k = float(cos(phi)) * magnitude;
98  degenerate = false;
99  }
100 
101  std::ostream & serialize(std::ostream &os, const Vector3f &value) {
102  return serialize(serialize(serialize(os, value.i), value.j), value.k);
103  }
104 
105  std::istream & unserialize(std::istream &is, Vector3f &value) {
106  return unserialize(unserialize(unserialize(is, value.i), value.j), value.k);
107  }
108 
109 }
110 
111 #include <Zeni/Undefine.h>
Vector3f & normalize()
Normalize the vector.
Definition: Vector3f.cpp:39
GLclampf f
Definition: glew.h:3390
void set_spherical(const float &theta, const float &phi, const float &magnitude=1)
Set the vector using spherical coordinates.
Definition: Vector3f.cpp:93
const float sqrt_three
sqrt(3.0f)
Definition: Vector3f.cpp:32
std::istream & unserialize(std::istream &is, Color &value)
Definition: Color.cpp:72
std::ostream & serialize(std::ostream &os, const Color &value)
Definition: Color.cpp:68
float magnitude() const
Get the magnitude of the vector.
Definition: Vector3f.hxx:142
const Vector3f vector_k(0, 0, 1)
k == Vector3f(0, 0, 1)
Definition: Vector3f.h:55
const float sqrt_two
sqrt(2.0f)
Definition: Vector3f.cpp:31
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
bool degenerate
Definition: Vector3f.h:123
const float pi
pi == 3.1415926...
Definition: Vector3f.cpp:27
float theta() const
theta == radians north of vector i
Definition: Vector3f.cpp:69
Vector3f(const bool &degenerate_=false)
The best way to create a Vector3f.
Definition: Vector3f.hxx:33
const float pi_over_two
pi/23
Definition: Vector3f.cpp:28
double sin(double x)
Definition: s_sin.c:56
EGLSurface EGLint void ** value
Definition: eglext.h:301
float phi() const
phi == radians down from k
Definition: Vector3f.cpp:81
const Vector3f vector_i(1, 0, 0)
i == Vector3f(1, 0, 0)
Definition: Vector3f.h:53
const float three_pi_over_two
3*pi/2
Definition: Vector3f.cpp:29
double atan(double x)
Definition: s_atan.c:67
const Vector3f vector_j(0, 1, 0)
j == Vector3f(0, 1, 0)
Definition: Vector3f.h:54
double cos(double x)
Definition: s_cos.c:56
const float over_three
1/3
Definition: Vector3f.cpp:30
Vector3f normalized() const
Get the normalized vector.
Definition: Vector3f.cpp:58
#define INFINTESSIMAL(x)
Definition: Define.h:134