1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/*
* Copyright (C) 2017 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_MATH_ORIENTEDBOX_HH_
#define IGNITION_MATH_ORIENTEDBOX_HH_
#include <iostream>
#include <ignition/math/Helpers.hh>
#include <ignition/math/MassMatrix3.hh>
#include <ignition/math/Material.hh>
#include <ignition/math/Matrix4.hh>
#include <ignition/math/Pose3.hh>
#include <ignition/math/Vector3.hh>
#include <ignition/math/config.hh>
namespace ignition
{
namespace math
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_MATH_VERSION_NAMESPACE {
//
/// \brief Mathematical representation of a box which can be arbitrarily
/// positioned and rotated.
template<typename T>
class OrientedBox
{
/// \brief Default constructor
public: OrientedBox() : size(Vector3<T>::Zero), pose(Pose3<T>::Zero)
{
}
/// \brief Constructor which takes size and pose.
/// \param[in] _size Box size, in its own coordinate frame. Its absolute
/// value will be taken, so the size is non-negative.
/// \param[in] _pose Box pose.
public: OrientedBox(const Vector3<T> &_size, const Pose3<T> &_pose)
: size(_size.Abs()), pose(_pose)
{
}
/// \brief Constructor which takes size, pose, and material.
/// \param[in] _size Box size, in its own coordinate frame. Its absolute
/// value will be taken, so the size is non-negative.
/// \param[in] _pose Box pose.
/// \param[in] _mat Material property for the box.
public: OrientedBox(const Vector3<T> &_size, const Pose3<T> &_pose,
const Material &_mat)
: size(_size.Abs()), pose(_pose), material(_mat)
{
}
/// \brief Constructor which takes only the size.
/// \param[in] _size Box size, in its own coordinate frame. Its absolute
/// value will be taken, so the size is non-negative.
public: explicit OrientedBox(const Vector3<T> &_size)
: size(_size.Abs()), pose(Pose3<T>::Zero)
{
}
/// \brief Constructor which takes only the size.
/// \param[in] _size Box size, in its own coordinate frame. Its absolute
/// value will be taken, so the size is non-negative.
/// \param[in] _mat Material property for the box.
public: explicit OrientedBox(const Vector3<T> &_size,
const Material &_mat)
: size(_size.Abs()), pose(Pose3<T>::Zero), material(_mat)
{
}
/// \brief Copy constructor.
/// \param[in] _b OrientedBox to copy.
public: OrientedBox(const OrientedBox<T> &_b)
: size(_b.size), pose(_b.pose), material(_b.material)
{
}
/// \brief Destructor
public: virtual ~OrientedBox()
{
}
/// \brief Get the length along the x dimension
/// \return Value of the length in the x dimension
public: T XLength() const
{
return this->size.X();
}
/// \brief Get the length along the y dimension
/// \return Value of the length in the y dimension
public: T YLength() const
{
return this->size.Y();
}
/// \brief Get the length along the z dimension
/// \return Value of the length in the z dimension
public: T ZLength() const
{
return this->size.Z();
}
/// \brief Get the size of the box
/// \return Size of the box
public: const Vector3<T> &Size() const
{
return this->size;
}
/// \brief Get the box pose, which is the pose of its center.
/// \return The pose of the box.
public: const Pose3<T> &Pose() const
{
return this->pose;
}
/// \brief Set the box size.
/// \param[in] _size Box size, in its own coordinate frame. Its absolute
/// value will be taken, so the size is non-negative.
public: void Size(Vector3<T> &_size)
{
// Enforce non-negative size
this->size = _size.Abs();
}
/// \brief Set the box pose.
/// \param[in] _pose Box pose.
public: void Pose(Pose3<T> &_pose)
{
this->pose = _pose;
}
/// \brief Assignment operator. Set this box to the parameter
/// \param[in] _b OrientedBox to copy
/// \return The new box.
public: OrientedBox &operator=(const OrientedBox<T> &_b)
{
this->size = _b.size;
this->pose = _b.pose;
this->material = _b.material;
return *this;
}
/// \brief Equality test operator
/// \param[in] _b OrientedBox to test
/// \return True if equal
public: bool operator==(const OrientedBox<T> &_b) const
{
return this->size == _b.size && this->pose == _b.pose &&
this->material == _b.material;
}
/// \brief Inequality test operator
/// \param[in] _b OrientedBox to test
/// \return True if not equal
public: bool operator!=(const OrientedBox<T> &_b) const
{
return this->size != _b.size || this->pose != _b.pose ||
this->material != _b.material;
}
/// \brief Output operator
/// \param[in] _out Output stream
/// \param[in] _b OrientedBox to output to the stream
/// \return The stream
public: friend std::ostream &operator<<(std::ostream &_out,
const OrientedBox<T> &_b)
{
_out << "Size[" << _b.Size() << "] Pose[" << _b.Pose() << "] "
<< "Material[" << _b.Material().Name() << "]";
return _out;
}
/// \brief Check if a point lies inside the box.
/// \param[in] _p Point to check.
/// \return True if the point is inside the box.
public: bool Contains(const Vector3d &_p) const
{
// Move point to box frame
auto t = Matrix4<T>(this->pose).Inverse();
auto p = t *_p;
return p.X() >= -this->size.X()*0.5 && p.X() <= this->size.X()*0.5 &&
p.Y() >= -this->size.Y()*0.5 && p.Y() <= this->size.Y()*0.5 &&
p.Z() >= -this->size.Z()*0.5 && p.Z() <= this->size.Z()*0.5;
}
/// \brief Get the material associated with this box.
/// \return The material assigned to this box.
public: const ignition::math::Material &Material() const
{
return this->material;
}
/// \brief Set the material associated with this box.
/// \param[in] _mat The material assigned to this box.
public: void SetMaterial(const ignition::math::Material &_mat)
{
this->material = _mat;
}
/// \brief Get the volume of the box in m^3.
/// \return Volume of the box in m^3.
public: T Volume() const
{
return this->size.X() * this->size.Y() * this->size.Z();
}
/// \brief Compute the box's density given a mass value. The
/// box is assumed to be solid with uniform density. This
/// function requires the box's size to be set to
/// values greater than zero. The Material of the box is ignored.
/// \param[in] _mass Mass of the box, in kg. This value should be
/// greater than zero.
/// \return Density of the box in kg/m^3. A negative value is
/// returned if the size or _mass is <= 0.
public: T DensityFromMass(const T _mass) const
{
if (this->size.Min() <= 0|| _mass <= 0)
return -1.0;
return _mass / this->Volume();
}
/// \brief Set the density of this box based on a mass value.
/// Density is computed using
/// double DensityFromMass(const double _mass) const. The
/// box is assumed to be solid with uniform density. This
/// function requires the box's size to be set to
/// values greater than zero. The existing Material density value is
/// overwritten only if the return value from this true.
/// \param[in] _mass Mass of the box, in kg. This value should be
/// greater than zero.
/// \return True if the density was set. False is returned if the
/// box's size or the _mass value are <= 0.
/// \sa double DensityFromMass(const double _mass) const
public: bool SetDensityFromMass(const T _mass)
{
T newDensity = this->DensityFromMass(_mass);
if (newDensity > 0)
this->material.SetDensity(newDensity);
return newDensity > 0;
}
/// \brief Get the mass matrix for this box. This function
/// is only meaningful if the box's size and material
/// have been set.
/// \param[out] _massMat The computed mass matrix will be stored here.
/// \return False if computation of the mass matrix failed, which
/// could be due to an invalid size (<=0) or density (<=0).
public: bool MassMatrix(MassMatrix3<T> &_massMat) const
{
return _massMat.SetFromBox(this->material, this->size);
}
/// \brief The size of the box in its local frame.
private: Vector3<T> size;
/// \brief The pose of the center of the box.
private: Pose3<T> pose;
/// \brief The box's material.
private: ignition::math::Material material;
};
typedef OrientedBox<int> OrientedBoxi;
typedef OrientedBox<double> OrientedBoxd;
typedef OrientedBox<float> OrientedBoxf;
}
}
}
#endif
|