Nebula
Loading...
Searching...
No Matches
polar.h
Go to the documentation of this file.
1#pragma once
2#ifndef MATH_POLAR_H
3#define MATH_POLAR_H
4//------------------------------------------------------------------------------
20#include "math/scalar.h"
21#include "math/vec3.h"
22#include "math/vec2.h"
23
24//------------------------------------------------------------------------------
25namespace Math
26{
27class polar
28{
29public:
31 polar();
33 polar(scalar t, scalar r);
35 polar(const vec3& v);
37 polar(const polar& src);
39 void operator=(const polar& rhs);
41 vec3 get_cartesian() const;
43 void set(const polar& p);
45 void set(scalar t, scalar r);
47 void set(const vec3& v);
48
51};
52
53//------------------------------------------------------------------------------
56inline
58 theta(0.0f),
59 rho(0.0f)
60{
61 // empty
62}
63
64//------------------------------------------------------------------------------
67inline
69 theta(t),
70 rho(r)
71{
72 // empty
73}
74
75//------------------------------------------------------------------------------
78inline
80{
81 this->set(v);
82}
83
84//------------------------------------------------------------------------------
87inline
88polar::polar(const polar& src) :
89 theta(src.theta),
90 rho(src.rho)
91{
92 // empty
93}
94
95//------------------------------------------------------------------------------
98inline void
100{
101 this->theta = rhs.theta;
102 this->rho = rhs.rho;
103}
104
105//------------------------------------------------------------------------------
108inline void
110{
111 this->theta = p.theta;
112 this->rho = p.rho;
113}
114
115//------------------------------------------------------------------------------
118inline void
120{
121 this->theta = t;
122 this->rho = r;
123}
124
125//------------------------------------------------------------------------------
129inline void
130polar::set(const vec3& vec)
131{
132 vec3 normVec3d = normalize(vec);
133 this->theta = Math::acos(normVec3d.y);
134
135 // build a normalized 2d vector of the xz component
136 vec2 normVec2d(normVec3d.x, normVec3d.z);
137 if (normVec2d.length() > TINY)
138 {
139 normVec2d = vec2::normalize(normVec2d);
140 }
141 else
142 {
143 normVec2d.set(1.0f, 0.0f);
144 }
145
146 // adjust dRho based on the quadrant we are in
147 if ((normVec2d.x >= 0.0f) && (normVec2d.y >= 0.0f))
148 {
149 // quadrant 1
150 this->rho = Math::asin(normVec2d.x);
151 }
152 else if ((normVec2d.x < 0.0f) && (normVec2d.y >= 0.0f))
153 {
154 // quadrant 2
155 this->rho = Math::asin(normVec2d.y) + Math::deg2rad(270.0f);
156 }
157 else if ((normVec2d.x < 0.0f) && (normVec2d.y < 0.0f))
158 {
159 // quadrant 3
160 this->rho = Math::asin(-normVec2d.x) + Math::deg2rad(180.0f);
161 }
162 else
163 {
164 // quadrant 4
165 this->rho = Math::asin(-normVec2d.y) + Math::deg2rad(90.0f);
166 }
167}
168
169//------------------------------------------------------------------------------
173inline vec3
175{
176 scalar sinTheta = Math::sin(this->theta);
177 scalar cosTheta = Math::cos(this->theta);
178 scalar sinRho = Math::sin(this->rho);
179 scalar cosRho = Math::cos(this->rho);
180 vec3 v(sinTheta * sinRho, cosTheta, sinTheta * cosRho);
181 return v;
182}
183
184} // namespace Math
185//------------------------------------------------------------------------------
186#endif
A polar coordinate inline class, consisting of 2 angles theta (latitude) and rho (longitude).
Definition polar.h:28
void set(const polar &p)
set to polar object
Definition polar.h:109
scalar theta
Definition polar.h:49
polar()
the default constructor
Definition polar.h:57
vec3 get_cartesian() const
convert to normalized cartesian coords
Definition polar.h:174
void operator=(const polar &rhs)
the assignment operator
Definition polar.h:99
scalar rho
Definition polar.h:50
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline constexpr scalar deg2rad(scalar d)
Definition scalar.h:468
__forceinline scalar asin(scalar x)
Definition scalar.h:209
__forceinline plane normalize(const plane &p)
Definition plane.h:255
float scalar
Definition scalar.h:45
__forceinline scalar cos(scalar x)
Definition scalar.h:191
__forceinline scalar acos(scalar x)
Definition scalar.h:218
__forceinline scalar sin(scalar x)
Definition scalar.h:182
Nebula's scalar datatype.
#define TINY
Definition scalar.h:41
A 2-component float vector class.
Definition vec2.h:21
void set(scalar x, scalar y)
set content
Definition vec2.h:198
scalar x
Definition vec2.h:86
static vec2 normalize(const vec2 &v)
return normalized version of vector
Definition vec2.h:321
scalar y
Definition vec2.h:87
scalar length() const
return length of vector
Definition vec2.h:228
A 3D vector.
Definition vec3.h:40
float x
Definition vec3.h:94
float z
Definition vec3.h:94
float y
Definition vec3.h:94