Gazebo Math

API Reference

9.3.0
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 <limits>
23#include <sstream>
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#include <gz/math/detail/Error.hh>
31
32namespace gz::math
33{
34 // Inline bracket to help doxygen filtering.
35 inline namespace GZ_MATH_VERSION_NAMESPACE {
36 //
40
59 template<typename ScalarField3T, typename ScalarT>
61 {
68
70 public: PiecewiseScalarField3() = default;
71
76 : pieces(_pieces)
77 {
78 for (size_t i = 0; i < pieces.size(); ++i)
79 {
80 if (pieces[i].region.Empty())
81 {
83 errStream << "Region #" << i << " (" << pieces[i].region
84 << ") in piecewise scalar field definition is empty.";
85 detail::LogErrorMessage(errStream.str());
86 }
87 for (size_t j = i + 1; j < pieces.size(); ++j)
88 {
89 if (pieces[i].region.Intersects(pieces[j].region))
90 {
92 errStream << "Detected overlap between regions in "
93 << "piecewise scalar field definition: "
94 << "region #" << i << " (" << pieces[i].region
95 << ") overlaps with region #" << j << " ("
96 << pieces[j].region << "). Region #" << i
97 << " will take precedence when overlapping.";
98 detail::LogErrorMessage(errStream.str());
99 }
100 }
101 }
102 }
103
112
117 public: ScalarT Evaluate(const Vector3<ScalarT> &_p) const
118 {
119 auto it = std::find_if(
120 this->pieces.begin(), this->pieces.end(),
121 [&](const Piece &piece)
122 {
123 return piece.region.Contains(_p);
124 });
125 if (it == this->pieces.end())
126 {
128 }
129 return it->field(_p);
130 }
131
137 public: ScalarT operator()(const Vector3<ScalarT> &_p) const
138 {
139 return this->Evaluate(_p);
140 }
141
153 {
154 // NaN until a non-empty piece is found, so that a field with
155 // no pieces, or only empty-region pieces, is reported as undefined.
158 for (const Piece &piece : this->pieces)
159 {
160 if (!piece.region.Empty())
161 {
163 const ScalarT y = piece.field.Minimum(piece.region, p);
164 if (std::isnan(yMin) || y < yMin)
165 {
166 _pMin = p;
167 yMin = y;
168 }
169 }
170 }
171 return yMin;
172 }
173
178 public: ScalarT Minimum() const
179 {
181 return this->Minimum(pMin);
182 }
183
188 public: friend std::ostream &operator<<(
192 {
193 if (_field.pieces.empty())
194 {
195 return _out << "undefined";
196 }
197 for (size_t i = 0; i < _field.pieces.size() - 1; ++i)
198 {
199 _out << _field.pieces[i].field << " if (x, y, z) in "
200 << _field.pieces[i].region << "; ";
201 }
202 return _out << _field.pieces.back().field
203 << " if (x, y, z) in "
204 << _field.pieces.back().region;
205 }
206
208 private: std::vector<Piece> pieces;
209 };
210
211 template<typename ScalarField3T>
213 template<typename ScalarField3T>
215 } // namespace GZ_MATH_VERSION_NAMESPACE
216} // namespace gz::math
217#endif // GZ_MATH_PIECEWISE_SCALAR_FIELD3_HH_