Gazebo Math

API Reference

7.7.0
gz/math/PiecewiseScalarField3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_MATH_PIECEWISE_SCALAR_FIELD3_HH_
18 #define GZ_MATH_PIECEWISE_SCALAR_FIELD3_HH_
19 
20 #include <algorithm>
21 #include <cmath>
22 #include <iostream>
23 #include <limits>
24 #include <utility>
25 #include <vector>
26 
27 #include <gz/math/Region3.hh>
28 #include <gz/math/Vector3.hh>
29 #include <gz/math/config.hh>
30 
31 namespace gz::math
32 {
33  // Inline bracket to help doxygen filtering.
34  inline namespace GZ_MATH_VERSION_NAMESPACE {
35  //
39  template<typename ScalarField3T, typename ScalarT>
60  {
63  public: struct Piece {
65  ScalarField3T field;
66  };
67 
69  public: PiecewiseScalarField3() = default;
70 
74  public: explicit PiecewiseScalarField3(const std::vector<Piece> &_pieces)
75  : pieces(_pieces)
76  {
77  for (size_t i = 0; i < pieces.size(); ++i)
78  {
79  if (pieces[i].region.Empty())
80  {
81  std::cerr << "Region #" << i << " (" << pieces[i].region
82  << ") in piecewise scalar field definition is empty."
83  << std::endl;
84  }
85  for (size_t j = i + 1; j < pieces.size(); ++j)
86  {
87  if (pieces[i].region.Intersects(pieces[j].region))
88  {
89  std::cerr << "Detected overlap between regions in "
90  << "piecewise scalar field definition: "
91  << "region #" << i << " (" << pieces[i].region
92  << ") overlaps with region #" << j << " ("
93  << pieces[j].region << "). Region #" << i
94  << " will take precedence when overlapping."
95  << std::endl;
96  }
97  }
98  }
99  }
100 
104  public: static PiecewiseScalarField3 Throughout(ScalarField3T _field)
105  {
108  }
109 
114  public: ScalarT Evaluate(const Vector3<ScalarT> &_p) const
115  {
116  auto it = std::find_if(
117  this->pieces.begin(), this->pieces.end(),
118  [&](const Piece &piece)
119  {
120  return piece.region.Contains(_p);
121  });
122  if (it == this->pieces.end())
123  {
125  }
126  return it->field(_p);
127  }
128 
134  public: ScalarT operator()(const Vector3<ScalarT> &_p) const
135  {
136  return this->Evaluate(_p);
137  }
138 
149  public: ScalarT Minimum(Vector3<ScalarT> &_pMin) const
150  {
151  // NaN until a non-empty piece is found, so that a field with
152  // no pieces, or only empty-region pieces, is reported as undefined.
153  _pMin = Vector3<ScalarT>::NaN;
155  for (const Piece &piece : this->pieces)
156  {
157  if (!piece.region.Empty())
158  {
160  const ScalarT y = piece.field.Minimum(piece.region, p);
161  if (std::isnan(yMin) || y < yMin)
162  {
163  _pMin = p;
164  yMin = y;
165  }
166  }
167  }
168  return yMin;
169  }
170 
175  public: ScalarT Minimum() const
176  {
177  Vector3<ScalarT> pMin;
178  return this->Minimum(pMin);
179  }
180 
185  public: friend std::ostream &operator<<(
186  std::ostream &_out,
188  ScalarField3T, ScalarT> &_field)
189  {
190  if (_field.pieces.empty())
191  {
192  return _out << "undefined";
193  }
194  for (size_t i = 0; i < _field.pieces.size() - 1; ++i)
195  {
196  _out << _field.pieces[i].field << " if (x, y, z) in "
197  << _field.pieces[i].region << "; ";
198  }
199  return _out << _field.pieces.back().field
200  << " if (x, y, z) in "
201  << _field.pieces.back().region;
202  }
203 
205  private: std::vector<Piece> pieces;
206  };
207 
208  template<typename ScalarField3T>
210  template<typename ScalarField3T>
212  } // namespace GZ_MATH_VERSION_NAMESPACE
213 } // namespace gz::math
214 #endif // GZ_MATH_PIECEWISE_SCALAR_FIELD3_HH_