Gazebo Math
API Reference
9.3.0
insert_drive_file
Tutorials
library_books
Classes
toc
Namespaces
insert_drive_file
Files
launch
Gazebo Website
Index
List
Hierarchy
Members: All
Members: Functions
Members: Variables
Members: Typedefs
Members: Enumerations
Members: Enumerator
List
Members
Functions
Typedefs
Variables
Enumerations
Enumerator
src
gz-math
include
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 <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
32
namespace
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>
60
class
PiecewiseScalarField3
61
{
64
public
:
struct
Piece
{
65
Region3<ScalarT>
region
;
66
ScalarField3T
field
;
67
};
68
70
public
:
PiecewiseScalarField3
() =
default
;
71
75
public
:
explicit
PiecewiseScalarField3
(
const
std::vector<Piece>
&
_pieces
)
76
: pieces(
_pieces
)
77
{
78
for
(
size_t
i
= 0;
i
< pieces.size(); ++
i
)
79
{
80
if
(pieces[
i
].region.Empty())
81
{
82
std::ostringstream
errStream
;
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
{
91
std::ostringstream
errStream
;
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
107
public
:
static
PiecewiseScalarField3
Throughout
(
ScalarField3T
_field
)
108
{
109
return
PiecewiseScalarField3<ScalarField3T, ScalarT>
({
110
{
Region3<ScalarT>::Unbounded
,
std::move
(
_field
)}});
111
}
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
{
127
return
std::numeric_limits<ScalarT>::quiet_NaN
();
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
152
public
:
ScalarT
Minimum
(
Vector3<ScalarT>
&
_pMin
)
const
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.
156
_pMin
=
Vector3<ScalarT>::NaN
;
157
ScalarT
yMin
=
std::numeric_limits<ScalarT>::quiet_NaN
();
158
for
(
const
Piece
&
piece
: this->pieces)
159
{
160
if
(!
piece
.region.Empty())
161
{
162
Vector3<ScalarT>
p;
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
{
180
Vector3<ScalarT>
pMin
;
181
return
this->Minimum(
pMin
);
182
}
183
188
public
:
friend
std::ostream
&
operator<<
(
189
std::ostream
&
_out
,
190
const
gz::math::PiecewiseScalarField3
<
191
ScalarField3T
,
ScalarT
> &
_field
)
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>
212
using
PiecewiseScalarField3f
=
PiecewiseScalarField3<ScalarField3T, float>
;
213
template
<
typename
ScalarField3T>
214
using
PiecewiseScalarField3d
=
PiecewiseScalarField3<ScalarField3T, double>
;
215
}
// namespace GZ_MATH_VERSION_NAMESPACE
216
}
// namespace gz::math
217
#endif
// GZ_MATH_PIECEWISE_SCALAR_FIELD3_HH_