World Builder  0.1.0-pre
A geodyanmic initial conditions generator
point.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 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 <limits>
21 #include <iostream>
22 
23 #include <world_builder/point.h>
24 #include <world_builder/assert.h>
25 
26 namespace WorldBuilder
27 {
28  template<>
30  :
31  point({0,0,0}),
32  coordinate_system(CoordinateSystem::cartesian)
33  {}
34 
35  template<>
37  :
38  point({0,0}),
39  coordinate_system(CoordinateSystem::cartesian)
40  {}
41 
42  template<int dim>
43  Point<dim>::Point(const std::array<double,dim> &location, CoordinateSystem coordinate_system_)
44  :
45  point(location),
46  coordinate_system(coordinate_system_)
47  {}
48 
49  template<int dim>
50  Point<dim>::Point(const Point<dim> &point_, CoordinateSystem coordinate_system_)
51  :
52  point(point_.get_array()),
53  coordinate_system(coordinate_system_)
54  {}
55 
56 
57  template<>
58  Point<2>::Point(const double x, const double y, CoordinateSystem coordinate_system_)
59  :
60  point({x,y}),
61  coordinate_system(coordinate_system_)
62  {}
63 
64  template<>
65  Point<3>::Point(const double /*x*/, const double /*y*/, CoordinateSystem coordinate_system_)
66  :
67  point({std::numeric_limits<double>::signaling_NaN(),std::numeric_limits<double>::signaling_NaN(), std::numeric_limits<double>::signaling_NaN()}),
68  coordinate_system(coordinate_system_)
69  {
70  WBAssertThrow(false,"Can't use the 2d constructor in 3d.");
71  }
72 
73 
74  template<>
75  Point<2>::Point(const double /*x*/, const double /*y*/, const double /*z*/, CoordinateSystem coordinate_system_)
76  :
77  point({std::numeric_limits<double>::signaling_NaN(),std::numeric_limits<double>::signaling_NaN()}),
78  coordinate_system(coordinate_system_)
79  {
80  WBAssertThrow(false,"Can't use the 3d constructor in 2d.");
81  }
82 
83 
84  template<>
85  Point<3>::Point(const double x, const double y, const double z, CoordinateSystem coordinate_system_)
86  :
87  point({x,y,z}),
88  coordinate_system(coordinate_system_)
89  {}
90 
91 
92  template<int dim>
94  {}
95 
96  template<int dim>
98  {
99  for (unsigned int i = 0; i < dim; ++i)
100  point[i] = point_[i];
101  return *this;
102  }
103 
104  template<int dim>
105  double Point<dim>::operator*(const Point<dim> &point_) const
106  {
107  const std::array<double,dim> array = point_.get_array();
108  double dot_product = 0;
109  for (unsigned int i = 0; i < dim; ++i)
110  dot_product += point[i] * array[i];
111  return dot_product;
112  }
113 
114 
115  template<int dim>
116  Point<dim> Point<dim>::operator*(const double scalar) const
117  {
118  // initialize the array to zero.
119  std::array<double,dim> array = Point<dim>().get_array();
120  for (unsigned int i = 0; i < dim; ++i)
121  array[i] += point[i] * scalar;
122  return Point<dim>(array);
123  }
124 
125  template<int dim>
126  Point<dim>
127  Point<dim>::operator+(const Point<dim> &point_) const
128  {
129  Point<dim> point_tmp(point);
130  point_tmp += point_;
131 
132  return point_tmp;
133  }
134 
135  template<int dim>
136  Point<dim>
137  Point<dim>::operator-(const Point<dim> &point_) const
138  {
139  Point<dim> point_tmp(point);
140  point_tmp -= point_;
141 
142  return point_tmp;
143  }
144 
145 
146  template<int dim>
147  Point<dim> &
148  Point<dim>::operator*=(const double scalar)
149  {
150  for (unsigned int i = 0; i < dim; ++i)
151  point[i] *= scalar;
152  return *this;
153  }
154 
155  template<int dim>
156  Point<dim> &
158  {
159  for (unsigned int i = 0; i < dim; ++i)
160  point[i] += point_[i];
161  return *this;
162  }
163 
164 
165  template<int dim>
166  Point<dim> &
168  {
169  for (unsigned int i = 0; i < dim; ++i)
170  point[i] -= point_[i];
171  return *this;
172  }
173 
174 
178  template<int dim>
179  const double &
180  Point<dim>::operator[](const unsigned int index) const
181  {
182  WBAssertThrow(index <= dim, "Can't ask for element " << index << " in an point with dimension " << dim << ".");
183  return point[index];
184  }
185 
186 
190  template<int dim>
191  double &
192  Point<dim>::operator[](const unsigned int index)
193  {
194  WBAssertThrow(index <= dim, "Can't ask for element " << index << " in an point with dimension " << dim << ".");
195  return point[index];
196  }
197 
198 
199  template<int dim>
200  const std::array<double,dim> &
202  {
203  return point;
204  }
205 
206 
207  template<int dim>
210  {
211  return coordinate_system;
212  }
213 
214 
215  template<int dim>
216  double
218  {
219  return std::sqrt(this->norm_square());
220  }
221 
222 
223  template<>
224  double
226  {
227  return (point[0] * point[0]) + (point[1] * point[1]);
228  }
229 
230  template<>
231  double
233  {
234  return (point[0] * point[0]) + (point[1] * point[1]) + (point[2] * point[2]);
235  }
236 
237 
238  template<int dim>
239  Point<dim>
240  operator*(const double scalar, const Point<dim> &point)
241  {
242  return point*scalar;
243  }
244 
245 
246  template class Point<2>;
247  template class Point<3>;
248  template Point<2> operator*(const double scalar, const Point<2> &point);
249  template Point<3> operator*(const double scalar, const Point<3> &point);
250 }
const double & operator[](const unsigned int index) const
Definition: point.cc:180
Point< dim > & operator+=(const Point< dim > &point)
Definition: point.cc:157
double norm_square() const
Point< dim > & operator=(const Point< dim > &point)
Definition: point.cc:97
CoordinateSystem get_coordinate_system() const
Definition: point.cc:209
Point< dim > operator+(const Point< dim > &point) const
Definition: point.cc:127
const std::array< double, dim > & get_array() const
Definition: point.cc:201
double norm() const
Definition: point.cc:217
double operator*(const Point< dim > &point) const
Definition: point.cc:105
#define WBAssertThrow(condition, message)
Definition: assert.h:41
Point< dim > & operator*=(const double scalar)
Definition: point.cc:148
Point< dim > operator-(const Point< dim > &point) const
Definition: point.cc:137
Point< dim > & operator-=(const Point< dim > &point)
Definition: point.cc:167