forked from BoostGSoC19/astronomy
/
base_differential.hpp
169 lines (139 loc) · 5.32 KB
/
base_differential.hpp
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
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
168
/*=============================================================================
Copyright 2018-2020 Pranam Lashkari < lpranam - plashkari628@gmail.com >
Copyright 2019-2020 Sarthak Singhal < sarthak2007 - singhalsarthak2007@gmail.com >
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef BOOST_ASTRONOMY_COORDINATE_BASE_DIFFERENTIAL_HPP
#define BOOST_ASTRONOMY_COORDINATE_BASE_DIFFERENTIAL_HPP
#include <cstddef>
#include <type_traits>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/algorithms/transform.hpp>
#include <boost/geometry/core/cs.hpp>
#include <boost/static_assert.hpp>
#include <boost/astronomy/detail/is_base_template_of.hpp>
namespace boost { namespace astronomy { namespace coordinate {
namespace bg = boost::geometry;
typedef bg::degree degree;
typedef bg::radian radian;
//! structure which is the base for all the representation
template
<
std::size_t DimensionCount,
typename CoordinateSystem,
typename CoordinateType=double
>
struct base_differential
{
///@cond INTERNAL
BOOST_STATIC_ASSERT_MSG((DimensionCount == 2 || DimensionCount == 3),
"DimensionCount is expected to be 2 or 3");
///@endcond
protected:
bg::model::point<CoordinateType, DimensionCount, CoordinateSystem> diff;
public:
typedef CoordinateSystem system;
typedef CoordinateType type;
//! returns the unit vector of current differential
template <typename ReturnType>
ReturnType unit_vector() const
{
/*!given coordinates/vectors are converted into cartesian and
unit vector of it is returned by converting it into requested type*/
/*checking return type if they both are not subclass of
base_representaion then compile time erorr is generated*/
BOOST_STATIC_ASSERT_MSG((boost::astronomy::detail::is_base_template_of
<boost::astronomy::coordinate::base_differential, ReturnType>::value),
"return type is expected to be a differential class");
bg::model::point<CoordinateType, 3, bg::cs::cartesian> tempPoint;
double mag = this->magnitude(); //magnitude of vector stored in current object
//converting coordinate/vector into cartesian
bg::transform(this->diff, tempPoint);
//performing calculations to find unit vector
bg::set<0>(tempPoint, (bg::get<0>(tempPoint) / mag));
bg::set<1>(tempPoint, (bg::get<1>(tempPoint) / mag));
bg::set<2>(tempPoint, (bg::get<2>(tempPoint) / mag));
return ReturnType(tempPoint);
}
//! magnitude of the current class is returned
double magnitude() const
{
double result = 0.0;
bg::model::point<CoordinateType, 3, bg::cs::cartesian> tempPoint;
bg::transform(this->diff, tempPoint);
switch (DimensionCount)
{
case 2:
result += std::pow(bg::get<0>(tempPoint), 2);
result += std::pow(bg::get<1>(tempPoint), 2);
break;
case 3:
result += std::pow(bg::get<0>(tempPoint), 2);
result += std::pow(bg::get<1>(tempPoint), 2);
result += std::pow(bg::get<2>(tempPoint), 2);
break;
default:
return -1;
}
return std::sqrt(result);
}
//! converts current representation into specified representation
template <typename ReturnType>
ReturnType to_differential() const
{
/*checking return type if they both are not subclass of
base_representaion then compile time erorr is generated*/
BOOST_STATIC_ASSERT_MSG((boost::astronomy::detail::is_base_template_of
<boost::astronomy::coordinate::base_differential, ReturnType>::value),
"return type is expected to be a differential class");
return ReturnType(this->diff);
}
//! returns the differential of calling object
bg::model::point
<
CoordinateType,
DimensionCount,
CoordinateSystem
>
get_differential() const
{
return this->diff;
}
template
<
std::size_t OtherDimensionCount,
typename OtherCoordinateSystem,
typename OtherCoordinateType
>
bool operator==
(
base_differential
<
OtherDimensionCount,
OtherCoordinateSystem,
OtherCoordinateType
> const& other
) const
{
/*converting both coordinates/vector into cartesian system*/
bg::model::point
<
typename std::conditional
<
sizeof(OtherCoordinateType) >= sizeof(CoordinateType),
OtherCoordinateType,
CoordinateType
>::type,
3,
bg::cs::cartesian
> tempPoint1, tempPoint2;
bg::transform(this->diff, tempPoint1);
bg::transform(other.get_differential(), tempPoint2);
return (bg::get<0>(tempPoint1) == bg::get<0>(tempPoint2)) &&
(bg::get<1>(tempPoint1) == bg::get<1>(tempPoint2)) &&
(bg::get<2>(tempPoint1) == bg::get<2>(tempPoint2));
}
}; //base_differential
}}} //namespace boost::astronomy::coordinate
#endif // !BOOST_ASTRONOMY_COORDINATE_BASE_DIFFERENTIAL_HPP