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 */
20 
21 
23 
24 namespace WorldBuilder
25 {
26  namespace Types
27  {
28 
29  template <unsigned int dim>
31  :
33  default_value(WorldBuilder::Point<dim>(CoordinateSystem::cartesian))
34 
35  {
37  }
38 
39  template <unsigned int dim>
41  std::string description_)
42  :
43  value(default_value_),
44  default_value(default_value_),
45  description(std::move(description_))
46  {
48  }
49 
50  template <unsigned int dim>
52  const WorldBuilder::Point<dim> &default_value_,
53  std::string description_)
54  :
55  value(value_),
56  default_value(default_value_),
57  description(std::move(description_))
58  {
60  }
61 
62  template <unsigned int dim>
63  Point<dim>::Point(Point const &other)
64  :
65  value(other.value),
68  {
70  }
71 
72  template <unsigned int dim>
74  = default;
75 
76  template<unsigned int dim>
77  void
79  const std::string &name,
80  const std::string &documentation) const
81  {
82  using namespace rapidjson;
83  Document &declarations = prm.declarations;
84  const std::string base = prm.get_full_json_path() + "/" + name;
85 
86  Pointer((base + "/type").c_str()).Set(declarations,"array");
87  Pointer((base + "/minItems").c_str()).Set(declarations,dim);
88  Pointer((base + "/maxItems").c_str()).Set(declarations,dim);
89  Pointer((base + "/description").c_str()).Set(declarations,documentation.c_str());
90  Pointer((base + "/items/type").c_str()).Set(declarations,"number");
91  // todo: default value
92  }
93 
94 
95  template<unsigned int dim>
96  double Point<dim>::operator*(const Point<dim> &point_) const
97  {
98  const std::array<double,dim> array = point_.value.get_array();
99  double dot_product = 0;
100  for (unsigned int i = 0; i < dim; ++i)
101  dot_product += value[i] * array[i];
102  return dot_product;
103  }
104 
105 
106  template<unsigned int dim>
108  Point<dim>::operator*(const double scalar) const
109  {
110  // initialize the array to zero.
111  std::array<double,dim> array = WorldBuilder::Point<dim>(value.get_coordinate_system()).get_array();
112  for (unsigned int i = 0; i < dim; ++i)
113  array[i] += value[i] * scalar;
114  return WorldBuilder::Point<dim>(array,value.get_coordinate_system());
115  }
116 
117  template<unsigned int dim>
119  Point<dim>::operator+(const Point<dim> &point_) const
120  {
121  WorldBuilder::Point<dim> point_tmp(value);
122  point_tmp += point_.value;
123 
124  return point_tmp;
125  }
126 
127  template<unsigned int dim>
129  Point<dim>::operator-(const Point<dim> &point_) const
130  {
131  WorldBuilder::Point<dim> point_tmp(value);
132  point_tmp -= point_.value;
133 
134  return point_tmp;
135  }
136 
140  template<unsigned int dim>
141  double &
142  Point<dim>::operator[](const unsigned int index)
143  {
144  return value[index];
145  }
146 
150  template<unsigned int dim>
151  const double &
152  Point<dim>::operator[](const unsigned int index) const
153  {
154  return value[index];
155  }
156 
161  template<unsigned int dim>
163  operator*(const double scalar, const Point<dim> &point)
164  {
165  return point.value*scalar;
166  }
167 
168  template class Point<2>;
169  template class Point<3>;
170 
175  template WorldBuilder::Point<2> operator*(const double scalar, const Point<2> &point);
176 
177 
182  template WorldBuilder::Point<3> operator*(const double scalar, const Point<3> &point);
183  } // namespace Types
184 } // namespace WorldBuilder
185 
double operator*(const Point< dim > &point) const
Definition: point.cc:96
WorldBuilder::Point< dim > operator-(const Point< dim > &point) const
Definition: point.cc:129
WorldBuilder::Point< dim > operator+(const Point< dim > &point) const
Definition: point.cc:119
~Point() override final
const double & operator[](const unsigned int index) const
Definition: point.cc:152
WorldBuilder::Point< dim > default_value
Definition: point.h:114
std::string get_full_json_path(size_t max_size=std::numeric_limits< size_t >::max()) const
Definition: parameters.cc:1933
std::string description
Definition: point.h:115
WorldBuilder::Point< dim > value
Definition: point.h:113
rapidjson::Document declarations
Definition: parameters.h:248
void write_schema(Parameters &prm, const std::string &name, const std::string &documentation) const override final
Definition: point.cc:78