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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#ifndef __math_sphere_h__
#define __math_sphere_h__
#include <cmath>
#include <sys/types.h>
#include <type_traits>
#include <Eigen/Core>
#include "math/math.h"
namespace MR
{
namespace Math
{
namespace Sphere
{
//! convert spherical coordinates to Cartesian coordinates
template <class VectorType1, class VectorType2>
inline typename std::enable_if<VectorType1::IsVectorAtCompileTime, void>::type
spherical2cartesian (const VectorType1& az_el_r, VectorType2&& xyz)
{
if (az_el_r.size() == 3) {
xyz[0] = az_el_r[2] * std::sin (az_el_r[1]) * std::cos (az_el_r[0]);
xyz[1] = az_el_r[2] * std::sin (az_el_r[1]) * std::sin (az_el_r[0]);
xyz[2] = az_el_r[2] * cos (az_el_r[1]);
} else {
xyz[0] = std::sin (az_el_r[1]) * std::cos (az_el_r[0]);
xyz[1] = std::sin (az_el_r[1]) * std::sin (az_el_r[0]);
xyz[2] = cos (az_el_r[1]);
}
}
//! convert matrix of spherical coordinates to Cartesian coordinates
template <class MatrixType1, class MatrixType2>
inline typename std::enable_if<!MatrixType1::IsVectorAtCompileTime, void>::type
spherical2cartesian (const MatrixType1& az_el, MatrixType2&& cartesian)
{
cartesian.resize (az_el.rows(), 3);
for (ssize_t dir = 0; dir < az_el.rows(); ++dir)
spherical2cartesian (az_el.row (dir), cartesian.row (dir));
}
//! convert matrix of spherical coordinates to Cartesian coordinates
template <class MatrixType>
inline typename std::enable_if<!MatrixType::IsVectorAtCompileTime, Eigen::MatrixXd>::type
spherical2cartesian (const MatrixType& az_el)
{
Eigen::MatrixXd cartesian (az_el.rows(), 3);
for (ssize_t dir = 0; dir < az_el.rows(); ++dir)
spherical2cartesian (az_el.row (dir), cartesian.row (dir));
return cartesian;
}
//! convert Cartesian coordinates to spherical coordinates
template <class VectorType1, class VectorType2>
inline typename std::enable_if<VectorType1::IsVectorAtCompileTime, void>::type
cartesian2spherical (const VectorType1& xyz, VectorType2&& az_el_r)
{
auto r = std::sqrt (Math::pow2(xyz[0]) + Math::pow2(xyz[1]) + Math::pow2(xyz[2]));
az_el_r[0] = std::atan2 (xyz[1], xyz[0]);
az_el_r[1] = std::acos (xyz[2] / r);
if (az_el_r.size() == 3)
az_el_r[2] = r;
}
//! convert matrix of Cartesian coordinates to spherical coordinates
template <class MatrixType1, class MatrixType2>
inline typename std::enable_if<!MatrixType1::IsVectorAtCompileTime, void>::type
cartesian2spherical (const MatrixType1& cartesian, MatrixType2&& az_el, bool include_r = false)
{
az_el.allocate (cartesian.rows(), include_r ? 3 : 2);
for (ssize_t dir = 0; dir < cartesian.rows(); ++dir)
cartesian2spherical (cartesian.row (dir), az_el.row (dir));
}
//! convert matrix of Cartesian coordinates to spherical coordinates
template <class MatrixType>
inline typename std::enable_if<!MatrixType::IsVectorAtCompileTime, Eigen::MatrixXd>::type
cartesian2spherical (const MatrixType& cartesian, bool include_r = false)
{
Eigen::MatrixXd az_el (cartesian.rows(), include_r ? 3 : 2);
for (ssize_t dir = 0; dir < cartesian.rows(); ++dir)
cartesian2spherical (cartesian.row (dir), az_el.row (dir));
return az_el;
}
//! normalise a set of Cartesian coordinates
template <class MatrixType>
inline void normalise_cartesian (MatrixType& cartesian)
{
assert (cartesian.cols() == 3);
for (ssize_t i = 0; i < cartesian.rows(); i++) {
auto norm = cartesian.row(i).norm();
if (norm)
cartesian.row(i).array() /= norm;
}
}
}
}
}
#endif
|