Ignition Math

API Reference

6.4.0
OrientedBox.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 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 IGNITION_MATH_ORIENTEDBOX_HH_
18 #define IGNITION_MATH_ORIENTEDBOX_HH_
19 
20 #include <iostream>
21 #include <ignition/math/Helpers.hh>
24 #include <ignition/math/Matrix4.hh>
25 #include <ignition/math/Pose3.hh>
26 #include <ignition/math/Vector3.hh>
27 #include <ignition/math/config.hh>
28 
29 namespace ignition
30 {
31  namespace math
32  {
33  // Inline bracket to help doxygen filtering.
34  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
35  //
38  template<typename T>
40  {
42  public: OrientedBox() : size(Vector3<T>::Zero), pose(Pose3<T>::Zero)
43  {
44  }
45 
50  public: OrientedBox(const Vector3<T> &_size, const Pose3<T> &_pose)
51  : size(_size.Abs()), pose(_pose)
52  {
53  }
54 
60  public: OrientedBox(const Vector3<T> &_size, const Pose3<T> &_pose,
61  const Material &_mat)
62  : size(_size.Abs()), pose(_pose), material(_mat)
63  {
64  }
65 
69  public: explicit OrientedBox(const Vector3<T> &_size)
70  : size(_size.Abs()), pose(Pose3<T>::Zero)
71  {
72  }
73 
78  public: explicit OrientedBox(const Vector3<T> &_size,
79  const Material &_mat)
80  : size(_size.Abs()), pose(Pose3<T>::Zero), material(_mat)
81  {
82  }
83 
86  public: OrientedBox(const OrientedBox<T> &_b)
87  : size(_b.size), pose(_b.pose), material(_b.material)
88  {
89  }
90 
92  public: virtual ~OrientedBox()
93  {
94  }
95 
98  public: T XLength() const
99  {
100  return this->size.X();
101  }
102 
105  public: T YLength() const
106  {
107  return this->size.Y();
108  }
109 
112  public: T ZLength() const
113  {
114  return this->size.Z();
115  }
116 
119  public: const Vector3<T> &Size() const
120  {
121  return this->size;
122  }
123 
126  public: const Pose3<T> &Pose() const
127  {
128  return this->pose;
129  }
130 
134  public: void Size(Vector3<T> &_size)
135  {
136  // Enforce non-negative size
137  this->size = _size.Abs();
138  }
139 
142  public: void Pose(Pose3<T> &_pose)
143  {
144  this->pose = _pose;
145  }
146 
151  {
152  this->size = _b.size;
153  this->pose = _b.pose;
154  this->material = _b.material;
155  return *this;
156  }
157 
161  public: bool operator==(const OrientedBox<T> &_b) const
162  {
163  return this->size == _b.size && this->pose == _b.pose &&
164  this->material == _b.material;
165  }
166 
170  public: bool operator!=(const OrientedBox<T> &_b) const
171  {
172  return this->size != _b.size || this->pose != _b.pose ||
173  this->material != _b.material;
174  }
175 
180  public: friend std::ostream &operator<<(std::ostream &_out,
181  const OrientedBox<T> &_b)
182  {
183  _out << "Size[" << _b.Size() << "] Pose[" << _b.Pose() << "] "
184  << "Material[" << _b.Material().Name() << "]";
185  return _out;
186  }
187 
191  public: bool Contains(const Vector3d &_p) const
192  {
193  // Move point to box frame
194  auto t = Matrix4<T>(this->pose).Inverse();
195  auto p = t *_p;
196 
197  return p.X() >= -this->size.X()*0.5 && p.X() <= this->size.X()*0.5 &&
198  p.Y() >= -this->size.Y()*0.5 && p.Y() <= this->size.Y()*0.5 &&
199  p.Z() >= -this->size.Z()*0.5 && p.Z() <= this->size.Z()*0.5;
200  }
201 
204  public: const ignition::math::Material &Material() const
205  {
206  return this->material;
207  }
208 
211  public: void SetMaterial(const ignition::math::Material &_mat)
212  {
213  this->material = _mat;
214  }
215 
218  public: T Volume() const
219  {
220  return this->size.X() * this->size.Y() * this->size.Z();
221  }
222 
231  public: T DensityFromMass(const T _mass) const
232  {
233  if (this->size.Min() <= 0|| _mass <= 0)
234  return -1.0;
235 
236  return _mass / this->Volume();
237  }
238 
251  public: bool SetDensityFromMass(const T _mass)
252  {
253  T newDensity = this->DensityFromMass(_mass);
254  if (newDensity > 0)
255  this->material.SetDensity(newDensity);
256  return newDensity > 0;
257  }
258 
265  public: bool MassMatrix(MassMatrix3<T> &_massMat) const
266  {
267  return _massMat.SetFromBox(this->material, this->size);
268  }
269 
271  private: Vector3<T> size;
272 
274  private: Pose3<T> pose;
275 
277  private: ignition::math::Material material;
278  };
279 
283  }
284  }
285 }
286 #endif
OrientedBox< float > OrientedBoxf
Definition: OrientedBox.hh:282
OrientedBox(const Vector3< T > &_size, const Pose3< T > &_pose)
Constructor which takes size and pose.
Definition: OrientedBox.hh:50
OrientedBox & operator=(const OrientedBox< T > &_b)
Assignment operator. Set this box to the parameter.
Definition: OrientedBox.hh:150
const Pose3< T > & Pose() const
Get the box pose, which is the pose of its center.
Definition: OrientedBox.hh:126
OrientedBox(const Vector3< T > &_size, const Pose3< T > &_pose, const Material &_mat)
Constructor which takes size, pose, and material.
Definition: OrientedBox.hh:60
OrientedBox< double > OrientedBoxd
Definition: OrientedBox.hh:281
A class for inertial information about a rigid body consisting of the scalar mass and a 3x3 symmetric...
Definition: MassMatrix3.hh:45
bool Contains(const Vector3d &_p) const
Check if a point lies inside the box.
Definition: OrientedBox.hh:191
Encapsulates a position and rotation in three space.
Definition: Pose3.hh:34
A 4x4 matrix class.
Definition: Matrix4.hh:38
bool operator==(const OrientedBox< T > &_b) const
Equality test operator.
Definition: OrientedBox.hh:161
Contains information about a single material.
Definition: Material.hh:65
T X() const
Get the x value.
Definition: Vector3.hh:648
OrientedBox(const Vector3< T > &_size)
Constructor which takes only the size.
Definition: OrientedBox.hh:69
bool SetDensityFromMass(const T _mass)
Set the density of this box based on a mass value. Density is computed using double DensityFromMass(c...
Definition: OrientedBox.hh:251
const Material & Material() const
Get the material associated with this box.
Definition: OrientedBox.hh:204
OrientedBox(const Vector3< T > &_size, const Material &_mat)
Constructor which takes only the size.
Definition: OrientedBox.hh:78
void Size(Vector3< T > &_size)
Set the box size.
Definition: OrientedBox.hh:134
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:223
OrientedBox(const OrientedBox< T > &_b)
Copy constructor.
Definition: OrientedBox.hh:86
T YLength() const
Get the length along the y dimension.
Definition: OrientedBox.hh:105
friend std::ostream & operator<<(std::ostream &_out, const OrientedBox< T > &_b)
Output operator.
Definition: OrientedBox.hh:180
void Pose(Pose3< T > &_pose)
Set the box pose.
Definition: OrientedBox.hh:142
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:40
bool SetFromBox(const Material &_mat, const Vector3< T > &_size, const Quaternion< T > &_rot=Quaternion< T >::Identity)
Set inertial properties based on a Material and equivalent box.
Definition: MassMatrix3.hh:1054
bool MassMatrix(MassMatrix3< T > &_massMat) const
Get the mass matrix for this box. This function is only meaningful if the box&#39;s size and material hav...
Definition: OrientedBox.hh:265
bool operator!=(const OrientedBox< T > &_b) const
Inequality test operator.
Definition: OrientedBox.hh:170
Mathematical representation of a box which can be arbitrarily positioned and rotated.
Definition: OrientedBox.hh:39
OrientedBox()
Default constructor.
Definition: OrientedBox.hh:42
T DensityFromMass(const T _mass) const
Compute the box&#39;s density given a mass value. The box is assumed to be solid with uniform density...
Definition: OrientedBox.hh:231
Definition: Angle.hh:42
OrientedBox< int > OrientedBoxi
Definition: OrientedBox.hh:280
STL class.
const Vector3< T > & Size() const
Get the size of the box.
Definition: OrientedBox.hh:119
T ZLength() const
Get the length along the z dimension.
Definition: OrientedBox.hh:112
virtual ~OrientedBox()
Destructor.
Definition: OrientedBox.hh:92
T XLength() const
Get the length along the x dimension.
Definition: OrientedBox.hh:98
void SetMaterial(const Material &_mat)
Set the material associated with this box.
Definition: OrientedBox.hh:211
T Volume() const
Get the volume of the box in m^3.
Definition: OrientedBox.hh:218