~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to NuxCore/Math/Trigonometry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "../NuxCore.h"
 
24
#include "Trigonometry.h"
 
25
#include "Constants.h"
 
26
 
 
27
namespace nux
 
28
{
 
29
 
 
30
// Assume the spherical coordinate system relatively to a right handed xyz,
 
31
// with Z pointing up.
 
32
// 0 <= phi < 180
 
33
// 0 <= theta < 360 ->>> along X axis, theta = 0.
 
34
  Vector3 SphericalToCartesianXBaseDeg (float r, float theta, float phi)
 
35
  {
 
36
    Vector3 v;
 
37
    v.x = r * std::cos (theta * Const::pi / 180.0f) * std::sin (phi * Const::pi / 180.0f);
 
38
    v.y = r * std::sin (theta * Const::pi / 180.0f) * std::sin (phi * Const::pi / 180.0f);
 
39
    v.z = r * std::cos (phi * Const::pi / 180.0f);
 
40
    return v;
 
41
  }
 
42
 
 
43
  Vector3 SphericalToCartesianXBaseRad (float r, float theta, float phi)
 
44
  {
 
45
    Vector3 v;
 
46
    v.x = r * std::cos (theta) * std::sin (phi);
 
47
    v.y = r * std::sin (theta) * std::sin (phi);
 
48
    v.z = r * std::cos (phi);
 
49
    return v;
 
50
  }
 
51
 
 
52
// Assume the spherical coordinate system relatively to a right handed xyz,
 
53
// with Y pointing up.
 
54
// 0 <= phi < 180
 
55
// 0 <= theta < 360 ->>> along Z axis, theta = 0.
 
56
  Vector3 SphericalToCartesianZBaseDeg (float r, float theta, float phi)
 
57
  {
 
58
    Vector3 v;
 
59
    v.z = r * std::cos (theta * Const::pi / 180.0f) * std::sin (phi * Const::pi / 180.0f);
 
60
    v.x = r * std::sin (theta * Const::pi / 180.0f) * std::sin (phi * Const::pi / 180.0f);
 
61
    v.y = r * std::cos (phi * Const::pi / 180.0f);
 
62
    return v;
 
63
  }
 
64
 
 
65
  Vector3 SphericalToCartesianZBaseRad (float r, float theta, float phi)
 
66
  {
 
67
    Vector3 v;
 
68
    v.z = r * std::cos (theta) * std::sin (phi);
 
69
    v.x = r * std::sin (theta) * std::sin (phi);
 
70
    v.y = r * std::cos (phi);
 
71
    return v;
 
72
  }
 
73
 
 
74
  Vector3 CartesianToSphericalXBaseRad (float x, float y, float z)
 
75
  {
 
76
    float r, theta, phi;
 
77
    r = std::sqrt (x * x + y * y + z * z);
 
78
    theta = std::atan (y / x);
 
79
    phi = std::acos (z / r);
 
80
    return Vector3 (r, theta, phi);
 
81
  }
 
82
 
 
83
  Vector3 CartesianToSphericalZBaseDeg (float x, float y, float z)
 
84
  {
 
85
    float r, theta, phi;
 
86
    r = std::sqrt (x * x + y * y + z * z);
 
87
    theta = std::atan (x / z);
 
88
    phi = std::acos (y / r);
 
89
    return Vector3 (r, theta, phi);
 
90
  }
 
91
 
 
92
}
 
93