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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*
* Copyright (C) 2012 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.
*
*/
#include <cmath>
#include <ignition/math/AxisAlignedBox.hh>
using namespace ignition;
using namespace math;
// Private data for AxisAlignedBox class
class ignition::math::AxisAlignedBoxPrivate
{
/// \brief Minimum corner of the box
public: Vector3d min = Vector3d(MAX_D, MAX_D, MAX_D);
/// \brief Maximum corner of the box
public: Vector3d max = Vector3d(LOW_D, LOW_D, LOW_D);
};
//////////////////////////////////////////////////
AxisAlignedBox::AxisAlignedBox()
: dataPtr(new AxisAlignedBoxPrivate)
{
}
//////////////////////////////////////////////////
AxisAlignedBox::AxisAlignedBox(double _vec1X, double _vec1Y, double _vec1Z,
double _vec2X, double _vec2Y, double _vec2Z)
: dataPtr(new AxisAlignedBoxPrivate)
{
this->dataPtr->min.Set(_vec1X, _vec1Y, _vec1Z);
this->dataPtr->max.Set(_vec2X, _vec2Y, _vec2Z);
this->dataPtr->min.Min(math::Vector3d(_vec2X, _vec2Y, _vec2Z));
this->dataPtr->max.Max(math::Vector3d(_vec1X, _vec1Y, _vec1Z));
}
//////////////////////////////////////////////////
AxisAlignedBox::AxisAlignedBox(const Vector3d &_vec1, const Vector3d &_vec2)
: dataPtr(new AxisAlignedBoxPrivate)
{
this->dataPtr->min = _vec1;
this->dataPtr->min.Min(_vec2);
this->dataPtr->max = _vec2;
this->dataPtr->max.Max(_vec1);
}
//////////////////////////////////////////////////
AxisAlignedBox::AxisAlignedBox(const AxisAlignedBox &_b)
: dataPtr(new AxisAlignedBoxPrivate)
{
this->dataPtr->min = _b.dataPtr->min;
this->dataPtr->max = _b.dataPtr->max;
}
//////////////////////////////////////////////////
AxisAlignedBox::~AxisAlignedBox()
{
delete this->dataPtr;
this->dataPtr = NULL;
}
//////////////////////////////////////////////////
double AxisAlignedBox::XLength() const
{
return std::max(0.0, this->dataPtr->max.X() - this->dataPtr->min.X());
}
//////////////////////////////////////////////////
double AxisAlignedBox::YLength() const
{
return std::max(0.0, this->dataPtr->max.Y() - this->dataPtr->min.Y());
}
//////////////////////////////////////////////////
double AxisAlignedBox::ZLength() const
{
return std::max(0.0, this->dataPtr->max.Z() - this->dataPtr->min.Z());
}
//////////////////////////////////////////////////
math::Vector3d AxisAlignedBox::Size() const
{
return math::Vector3d(this->XLength(),
this->YLength(),
this->ZLength());
}
//////////////////////////////////////////////////
math::Vector3d AxisAlignedBox::Center() const
{
return 0.5 * this->dataPtr->min + 0.5 * this->dataPtr->max;
}
//////////////////////////////////////////////////
void AxisAlignedBox::Merge(const AxisAlignedBox &_box)
{
this->dataPtr->min.Min(_box.dataPtr->min);
this->dataPtr->max.Max(_box.dataPtr->max);
}
//////////////////////////////////////////////////
AxisAlignedBox &AxisAlignedBox::operator =(const AxisAlignedBox &_b)
{
this->dataPtr->max = _b.dataPtr->max;
this->dataPtr->min = _b.dataPtr->min;
return *this;
}
//////////////////////////////////////////////////
AxisAlignedBox AxisAlignedBox::operator+(const AxisAlignedBox &_b) const
{
AxisAlignedBox result(*this);
result += _b;
return result;
}
//////////////////////////////////////////////////
const AxisAlignedBox &AxisAlignedBox::operator+=(const AxisAlignedBox &_b)
{
this->dataPtr->min.Min(_b.dataPtr->min);
this->dataPtr->max.Max(_b.dataPtr->max);
return *this;
}
//////////////////////////////////////////////////
bool AxisAlignedBox::operator==(const AxisAlignedBox &_b) const
{
return this->dataPtr->min == _b.dataPtr->min &&
this->dataPtr->max == _b.dataPtr->max;
}
//////////////////////////////////////////////////
bool AxisAlignedBox::operator!=(const AxisAlignedBox &_b) const
{
return !(*this == _b);
}
//////////////////////////////////////////////////
AxisAlignedBox AxisAlignedBox::operator-(const Vector3d &_v)
{
return AxisAlignedBox(this->dataPtr->min - _v, this->dataPtr->max - _v);
}
//////////////////////////////////////////////////
AxisAlignedBox AxisAlignedBox::operator+(const Vector3d &_v)
{
return AxisAlignedBox(this->dataPtr->min + _v, this->dataPtr->max + _v);
}
//////////////////////////////////////////////////
bool AxisAlignedBox::Intersects(const AxisAlignedBox &_box) const
{
// Check the six separating planes.
if (this->Max().X() < _box.Min().X())
return false;
if (this->Max().Y() < _box.Min().Y())
return false;
if (this->Max().Z() < _box.Min().Z())
return false;
if (this->Min().X() > _box.Max().X())
return false;
if (this->Min().Y() > _box.Max().Y())
return false;
if (this->Min().Z() > _box.Max().Z())
return false;
// Otherwise the two boxes must intersect.
return true;
}
//////////////////////////////////////////////////
const Vector3d &AxisAlignedBox::Min() const
{
return this->dataPtr->min;
}
//////////////////////////////////////////////////
const Vector3d &AxisAlignedBox::Max() const
{
return this->dataPtr->max;
}
//////////////////////////////////////////////////
Vector3d &AxisAlignedBox::Min()
{
return this->dataPtr->min;
}
//////////////////////////////////////////////////
Vector3d &AxisAlignedBox::Max()
{
return this->dataPtr->max;
}
//////////////////////////////////////////////////
bool AxisAlignedBox::Contains(const Vector3d &_p) const
{
return _p.X() >= this->dataPtr->min.X() && _p.X() <= this->dataPtr->max.X() &&
_p.Y() >= this->dataPtr->min.Y() && _p.Y() <= this->dataPtr->max.Y() &&
_p.Z() >= this->dataPtr->min.Z() && _p.Z() <= this->dataPtr->max.Z();
}
//////////////////////////////////////////////////
bool AxisAlignedBox::ClipLine(const int _d, const Line3d &_line,
double &_low, double &_high) const
{
// dimLow and dimHigh are the results we're calculating for this
// current dimension.
double dimLow, dimHigh;
// Find the point of intersection in this dimension only as a fraction of
// the total vector http://youtu.be/USjbg5QXk3g?t=3m12s
dimLow = (this->dataPtr->min[_d] - _line[0][_d]) /
(_line[1][_d] - _line[0][_d]);
dimHigh = (this->dataPtr->max[_d] - _line[0][_d]) /
(_line[1][_d] - _line[0][_d]);
// Make sure low is less than high
if (dimHigh < dimLow)
std::swap(dimHigh, dimLow);
// If this dimension's high is less than the low we got then we definitely
// missed. http://youtu.be/USjbg5QXk3g?t=7m16s
if (dimHigh < _low)
return false;
// Likewise if the low is less than the high.
if (dimLow > _high)
return false;
// Add the clip from this dimension to the previous results
// http://youtu.be/USjbg5QXk3g?t=5m32s
if (std::isfinite(dimLow))
_low = std::max(dimLow, _low);
if (std::isfinite(dimHigh))
_high = std::min(dimHigh, _high);
return true;
}
/////////////////////////////////////////////////
bool AxisAlignedBox::IntersectCheck(
const Vector3d &_origin, const Vector3d &_dir,
const double _min, const double _max) const
{
return std::get<0>(this->Intersect(_origin, _dir, _min, _max));
}
/////////////////////////////////////////////////
std::tuple<bool, double> AxisAlignedBox::IntersectDist(const Vector3d &_origin,
const Vector3d &_dir, const double _min, const double _max) const
{
return std::make_tuple(
std::get<0>(this->Intersect(_origin, _dir, _min, _max)),
std::get<1>(this->Intersect(_origin, _dir, _min, _max)));
}
/////////////////////////////////////////////////
std::tuple<bool, double, Vector3d> AxisAlignedBox::Intersect(
const Vector3d &_origin, const Vector3d &_dir,
const double _min, const double _max) const
{
Vector3d dir = _dir;
dir.Normalize();
return this->Intersect(Line3d(_origin + dir * _min, _origin + dir * _max));
}
/////////////////////////////////////////////////
double AxisAlignedBox::Volume() const
{
return this->XLength() * this->YLength() * this->ZLength();
}
/////////////////////////////////////////////////
// Find the intersection of a line from v0 to v1 and an
// axis-aligned bounding box http://www.youtube.com/watch?v=USjbg5QXk3g
std::tuple<bool, double, Vector3d> AxisAlignedBox::Intersect(
const Line3d &_line) const
{
// low and high are the results from all clipping so far.
// We'll write our results back out to those parameters.
double low = 0;
double high = 1;
if (!this->ClipLine(0, _line, low, high))
return std::make_tuple(false, 0, Vector3d::Zero);
if (!this->ClipLine(1, _line, low, high))
return std::make_tuple(false, 0, Vector3d::Zero);
if (!this->ClipLine(2, _line, low, high))
return std::make_tuple(false, 0, Vector3d::Zero);
// The formula for I: http://youtu.be/USjbg5QXk3g?t=6m24s
Vector3d intersection = _line[0] + ((_line[1] - _line[0]) * low);
return std::make_tuple(true, _line[0].Distance(intersection), intersection);
}
|