WUnitSphereCoordinates.h 4.14 KB
Newer Older
Stefan Philips's avatar
Stefan Philips committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WUNITSPHERECOORDINATES_H
#define WUNITSPHERECOORDINATES_H

28
#include <cmath>
Stefan Philips's avatar
Stefan Philips committed
29 30
#include <vector>

31
#include "../../common/math/linearAlgebra/WLinearAlgebra.h"
Mathias Goldau's avatar
Mathias Goldau committed
32

Stefan Philips's avatar
Stefan Philips committed
33 34 35
/**
 * This class stores coordinates on the unit sphere.
 */
36 37

template< typename T > class WUnitSphereCoordinates // NOLINT
Stefan Philips's avatar
Stefan Philips committed
38 39 40 41 42 43 44 45 46 47
{
// TODO(all): implement test
// friend class WUnitSphereCoordinatesTest;
public:
    /**
     * Default constructor.
     */
    WUnitSphereCoordinates();

    /**
48 49 50
     * Constructor for unit sphere angles.
     * \param theta coordinate
     * \param phi coordinate
Stefan Philips's avatar
Stefan Philips committed
51
     */
52
    WUnitSphereCoordinates( T theta, T phi );
Stefan Philips's avatar
Stefan Philips committed
53 54

    /**
Alexander Wiebel's avatar
Alexander Wiebel committed
55 56
     * Constructor for Euclidean coordinates.
     * \param vector Euclidean coordinates
Stefan Philips's avatar
Stefan Philips committed
57
     */
58
    explicit WUnitSphereCoordinates( const WMatrixFixed< T, 3, 1 >& vector );
Stefan Philips's avatar
Stefan Philips committed
59 60 61 62 63 64 65 66

    /**
     * Destructor.
     */
    virtual ~WUnitSphereCoordinates();

    /**
     * Return the theta angle.
67 68
     *
     * \return theta angle
Stefan Philips's avatar
Stefan Philips committed
69
     */
70
    T getTheta() const;
Stefan Philips's avatar
Stefan Philips committed
71 72 73

    /**
     * Return the phi angle.
74 75
     *
     * \return phi angle
Stefan Philips's avatar
Stefan Philips committed
76
     */
77
    T getPhi() const;
Stefan Philips's avatar
Stefan Philips committed
78 79 80

    /**
     * Set theta angle.
81
     * \param theta Value for theta.
Stefan Philips's avatar
Stefan Philips committed
82
     */
83
    void setTheta( T theta );
Stefan Philips's avatar
Stefan Philips committed
84 85 86 87 88

    /**
     * Set phi angle.
     * \param phi Value for phi.
     */
89
    void setPhi( T phi );
Stefan Philips's avatar
Stefan Philips committed
90 91

    /**
Alexander Wiebel's avatar
Alexander Wiebel committed
92
     * Returns the stored sphere coordinates as Euclidean coordinates.
93 94
     *
     * \return sphere coordinates in euclidean space
Stefan Philips's avatar
Stefan Philips committed
95
     */
96
    WMatrixFixed< T, 3, 1 > getEuclidean() const;
Stefan Philips's avatar
Stefan Philips committed
97

98 99 100 101 102 103 104
    /**
     * Returns the stored sphere coordinates as Euclidean coordinates.
     *
     * \param sphere coordinates in euclidean space
     */
    void setEuclidean( WMatrixFixed< T, 3, 1 > );

Stefan Philips's avatar
Stefan Philips committed
105 106
protected:
private:
107
    /** coordinate */
108
    T m_theta;
109
    /** coordinate */
110
    T m_phi;
Stefan Philips's avatar
Stefan Philips committed
111 112
};

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
template< typename T >
WUnitSphereCoordinates< T >::WUnitSphereCoordinates()
    : m_theta( 0.0 ),
      m_phi( 0.0 )
{
}

template< typename T >
WUnitSphereCoordinates< T >::WUnitSphereCoordinates( T theta, T phi )
    : m_theta( theta ),
      m_phi( phi )
{
}

template< typename T >
128
WUnitSphereCoordinates< T >::WUnitSphereCoordinates( const WMatrixFixed< T, 3, 1 >& vector )
129
{
130
    setEuclidean( vector );
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
}

template< typename T >
WUnitSphereCoordinates< T >::~WUnitSphereCoordinates()
{
}

template< typename T >
T WUnitSphereCoordinates< T >::getTheta() const
{
    return m_theta;
}

template< typename T >
T WUnitSphereCoordinates< T >::getPhi() const
{
    return m_phi;
}

template< typename T >
void WUnitSphereCoordinates< T >::setTheta( T theta )
{
    m_theta = theta;
}

template< typename T >
void WUnitSphereCoordinates< T >::setPhi( T phi )
{
    m_phi = phi;
}

template< typename T >
WMatrixFixed< T, 3, 1 > WUnitSphereCoordinates< T >::getEuclidean() const
{
    return WMatrixFixed< T, 3, 1 >( std::sin( m_theta )*std::cos( m_phi ), std::sin( m_theta )*std::sin( m_phi ), std::cos( m_theta ) );
}

168 169 170 171 172 173 174 175 176
template< typename T >
void WUnitSphereCoordinates< T >::setEuclidean( WMatrixFixed< T, 3, 1 > vector )
{
    vector = normalize( vector );
    // calculate angles
    m_theta = std::acos( vector[2] );
    m_phi = std::atan2( vector[1], vector[0] );
}

Stefan Philips's avatar
Stefan Philips committed
177
#endif  // WUNITSPHERECOORDINATES_H