5 #define _USE_MATH_DEFINES 9 #include "../tools/errorhandler.hpp" 38 #ifdef ASSERT_NUMERIC_RANGES 51 #ifdef ASSERT_NUMERIC_RANGES 66 #ifdef ASSERT_NUMERIC_RANGES 74 #ifdef ASSERT_NUMERIC_RANGES 100 value_type XCos = deltaX * dCos, XSin = deltaX * dSin;
101 value_type YCos = deltaY * dCos, YSin = deltaY * dSin;
108 poly[1] =
m_center + Point2D( XCos + YSin, XSin - YCos);
109 poly[2] =
m_center + Point2D( - XCos + YSin, - XSin - YCos);
110 poly[3] =
m_center + Point2D( - XCos - YSin, - XSin + YCos);
127 value_type xmax = deltaX * dCos + deltaY * dSin;
128 value_type ymax = deltaX * dSin + deltaY * dCos;
131 Point2D size(2.0f * xmax, 2.0f * ymax);
137 std::pair<Box2D::value_type, Box2D::value_type>
150 return std::make_pair(-
PI,
PI);
159 for (
unsigned k = 1; k < 4; ++k)
163 if (pointangle < minangle)
164 minangle = pointangle;
166 if (pointangle > maxangle)
167 maxangle = pointangle;
170 return std::make_pair(minangle, maxangle);
183 return (std::abs(pointX) <= 0.5f *
m_size.
getX())
184 && (std::abs(pointY) <= 0.5f *
m_size.
getY());
200 if (std::max(std::abs(deltaX), std::abs(deltaY)) > half_sizeX + half_sizeY)
206 value_type pointX = dCos * deltaX + dSin * deltaY;
207 value_type pointY = -dSin * deltaX + dCos * deltaY;
210 return ((std::abs(pointX) <= half_sizeX)
211 && (std::abs(pointY) <= half_sizeY));
218 template<
typename floatT>
224 floatT pointXAbs = std::abs(pointX);
225 floatT pointYAbs = std::abs(pointY);
228 if (pointXAbs > boxSizeX && pointYAbs > boxSizeY)
231 return hypot(pointXAbs - boxSizeX, pointYAbs - boxSizeY);
234 if (pointXAbs > boxSizeX)
236 return pointXAbs - boxSizeX;
238 if (pointYAbs > boxSizeY)
240 return pointYAbs - boxSizeY;
243 return std::min(boxSizeX - pointXAbs, boxSizeY - pointYAbs);
247 template<
class Po
intT>
274 -dSin * deltaX + dCos * deltaY,
282 template<
class IteratorT>
286 std::vector<Point2D>::size_type numPoints = 0;
288 for (IteratorT iter = begin; iter != end; ++iter)
310 template<
class Po
intT>
311 static inline double rotate_x(
const PointT& p,
double dCos,
double dSin)
313 return dCos * p.getX() + dSin * p.getY();
318 template <
class Po
intT>
319 static inline double rotate_y(
const PointT& p,
double dCos,
double dSin)
321 return -dSin * p.getX() + dCos * p.getY();
328 template<
class IterT>
static Box2D 333 float dCos = cos(orientation);
334 float dSin = sin(orientation);
338 assert(begin != end);
345 float minX =
rotate_x(*begin, dCos, dSin);
347 float minY =
rotate_y(*begin, dCos, dSin);
352 for (IterT iter = begin + 1; iter != end; iter++)
354 float x =
rotate_x(*iter, dCos, dSin);
355 float y =
rotate_y(*iter, dCos, dSin);
356 if (x < minX) minX = x;
357 if (x > maxX) maxX = x;
358 if (y < minY) minY = y;
359 if (y > maxY) maxY = y;
363 float rotated_center_x = 0.5 * (maxX + minX);
364 float rotated_center_y = 0.5 * (maxY + minY);
367 result.
setCenter(dCos * rotated_center_x - dSin * rotated_center_y,
368 dSin * rotated_center_x + dCos * rotated_center_y);
386 const std::vector<Point2D>::const_iterator& begin,
387 const std::vector<Point2D>::const_iterator& end)
394 const std::vector<Point2D>& points)
423 bool __attribute__((unused)) correctRange =
true;
425 bool correctRange =
true;
430 printWarning(
"Size of Box2D was given as negative value (" +
432 ") - silently using the absolute value instead.");
435 correctRange =
false;
443 printWarning(
"Size.getX() of Box2D was given as NaN value - silently using Zero instead.");
444 correctRange =
false;
449 printWarning(
"Size.getY() of Box2D was given as NaN value - silently using Zero instead.");
450 correctRange =
false;
455 printWarning(
"Rotation of Box2D was given as NaN value - silently using Zero instead.");
456 correctRange =
false;
461 printWarning(
"Rotation of Box2D (" + ::
toString(
m_rotation, 2) +
") was outside of its [-pi,pi] definition range - silently normalizing it back into that range (" +
464 correctRange =
false;
466 #ifdef ASSERT_NUMERIC_RANGES 467 assert(correctRange);
value_type getRotation() const
std::string toString() const
std::string toString(UINT16 digits=2) const
Text output for debugging.
const Point2D & getCenter() const
Returns the center point of this Box.
const Point2D & getSize() const
Returns the size of this Box.
A rotated 2-dimensional box in the plane.
void setSize(const Point2D &p)
Sets the size of this Box. Must be non-negative.
std::pair< value_type, value_type > getBoundingAngles() const
Returns boundary angles for this box.
static double rotate_y(const PointT &p, double dCos, double dSin)
Box2D movedBy(const Point2D ¢erMovement) const
Returns a Box that is copied from this one but with its center point moved.
Polygon2D toPolygon() const
Converts this Box2D to a closed polygon.
static Box2D orientatedBox(value_type orientation_rad, const Polygon2D &poly)
Returns an orientated bounding box for the given list of points.
static double rotate_x(const PointT &p, double dCos, double dSin)
Point2D::value_type distanceFromOutline(const Point2D &point) const
Returns the distance from the outline of this Box2D to the given point.
Point2D::value_type value_type
The type of the stored x, y coordinates, and the rotation.
static floatT distanceFromStraightBox(floatT pointX, floatT pointY, floatT boxSizeX, floatT boxSizeY)
void setRotation(value_type r)
Sets the rotation angle of this Box in [radians], counter clock wise.
void setCenter(const Point2D &p)
Sets the center point of this Box2D.
void setXY(value_type x, value_type y)
Box2D toBoundingBox() const
Returns a Box in parallel to the coordinate system that bounds this box.
Box2D()
Constructor for an all-zero Box2D.
bool containsPoint(const Point2D &point) const
Returns true if the given Point2D is inside this box or on its outline.
static Box2D calcOrientatedBox(double orientation, const IterT &begin, const IterT &end)
void printWarning(std::string message)
void moveBy(const Point2D ¢erMovement)
Move the center point of this box by the given point values.
void verifyNumericRanges()