kick
/Users/morten/Programmering/cpp/kick/src/kick/math/spherical.cpp
00001 //
00002 // Created by Morten Nobel-Jørgensen on 29/10/14.
00003 //
00004 
00005 #include "spherical.h"
00006 #include <glm/gtc/constants.hpp>
00007 
00008 namespace kick{
00009 
00010 glm::vec3 Spherical::toCartesian() {
00011     return sphericalToCartesian(radius, polar, elevation);
00012 }
00013 
00014 Spherical Spherical::cartesianToSpherical(glm::vec3 cartCoords) {
00015     if (cartCoords.x == 0)
00016         cartCoords.x = FLT_EPSILON;
00017     Spherical res;
00018     res.radius = (float) sqrt((cartCoords.x * cartCoords.x)
00019                 + (cartCoords.y * cartCoords.y)
00020                 + (cartCoords.z * cartCoords.z));
00021     res.polar = (float) atan(cartCoords.z / cartCoords.x);
00022     if (cartCoords.x < 0)
00023                 res.polar += glm::pi<float>();
00024     res.elevation = (float) asin(cartCoords.y / res.radius);
00025     return res;
00026 }
00027 
00028 glm::vec3 Spherical::sphericalToCartesian(float radius, float polar, float elevation) {
00029     float a = (float) (radius * cos(elevation));
00030     glm::vec3 outCart;
00031     outCart.x = (float) (a * cos(polar));
00032     outCart.y = (float) (radius * sin(elevation));
00033     outCart.z = (float) (a * sin(polar));
00034     return outCart;
00035 }
00036 
00037 }
 All Classes Functions Variables