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