World Builder  1.1.0-pre
A geodynamic initial conditions generator
point.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018-2024 by the authors of the World Builder code.
3 
4  This file is part of the World Builder.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19 
20 #include "world_builder/point.h"
21 
22 #include <limits>
23 
24 namespace WorldBuilder
25 {
26 
27  template<>
28  Point<2>::Point(const double x, const double y, const CoordinateSystem coordinate_system_)
29  :
30  point({{x,y}}),
31  coordinate_system(coordinate_system_)
32  {}
33 
34  template<>
35  Point<3>::Point(const double /*x*/, const double /*y*/, CoordinateSystem coordinate_system_)
36  :
37  point({{std::numeric_limits<double>::signaling_NaN(),std::numeric_limits<double>::signaling_NaN(), std::numeric_limits<double>::signaling_NaN()}}),
38  coordinate_system(coordinate_system_)
39  {
40  WBAssertThrow(false,"Can't use the 2d constructor in 3d.");
41  }
42 
43 
44  template<>
45  Point<2>::Point(const double /*x*/, const double /*y*/, const double /*z*/, CoordinateSystem coordinate_system_)
46  :
47  point({{std::numeric_limits<double>::signaling_NaN(),std::numeric_limits<double>::signaling_NaN()}}),
48  coordinate_system(coordinate_system_)
49  {
50  WBAssertThrow(false,"Can't use the 3d constructor in 2d.");
51  }
52 
53 
54  template<>
55  Point<3>::Point(const double x, const double y, const double z, CoordinateSystem coordinate_system_)
56  :
57  point({{x,y,z}}),
58  coordinate_system(coordinate_system_)
59  {}
60 
61 
62 
63 
64 
65 
66 
67  template<unsigned int dim>
68  double
69  Point<dim>::distance(const Point<2> &two) const
70  {
71  if (this->coordinate_system == spherical)
72  {
73  // spherical
74  const double d_longitude = two[0] - this->point[0];
75  const double d_latitude = two[1] - this->point[1];
76  const double sin_d_lat = std::sin(d_latitude * 0.5);
77  const double sin_d_long = std::sin(d_longitude * 0.5);
78  return 2.0 * asin(sqrt((sin_d_lat * sin_d_lat) + (sin_d_long*sin_d_long) * std::cos(this->point[1]) * std::cos(two[1])));
79  }
80 
81  // cartesian
82  const double x_distance_to_reference_point = point[0]-two[0];
83  const double y_distance_to_reference_point = point[1]-two[1];
84  return sqrt((x_distance_to_reference_point*x_distance_to_reference_point) + (y_distance_to_reference_point*y_distance_to_reference_point));
85 
86  }
87 
91  template class Point<2>;
92 
93 
97  template class Point<3>;
98 
102  template Point<2> operator*(const double scalar, const Point<2> &point);
103 
107  template Point<3> operator*(const double scalar, const Point<3> &point);
108 } // namespace WorldBuilder
template WorldBuilder::Point< 3 > operator*(const double scalar, const Point< 3 > &point)
double cos(const double angle)
Definition: point.h:97
#define WBAssertThrow(condition, message)
Definition: assert.h:40
double sin(const double raw_angle)
Definition: point.h:81
Point(CoordinateSystem coordinate_system_)
Definition: point.h:119