Skip to content

Commit

Permalink
Vector pp() method is now a non-member function
Browse files Browse the repository at this point in the history
No particular reason why this rarely-used debugging tool needs to be part of
the class interface, since it makes no use of private data.
  • Loading branch information
Matthew Mott committed Oct 6, 2021
1 parent 4f3dbca commit 78ec565
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion include/irender.h
Expand Up @@ -214,7 +214,7 @@ typedef std::shared_ptr<RendererLight> RendererLightPtr;
/// Debug stream insertion for RendererLight
inline std::ostream& operator<< (std::ostream& os, const RendererLight& l)
{
return os << "RendererLight(origin=" << l.getLightOrigin().pp()
return os << "RendererLight(origin=" << math::pp(l.getLightOrigin())
<< ", lightAABB=" << l.lightAABB() << ")";
}

Expand Down
26 changes: 13 additions & 13 deletions libs/math/AABB.h
Expand Up @@ -5,15 +5,15 @@
#include "VolumeIntersectionValue.h"

// Forward declaration, include Matrix4.h for definition
class Matrix4;
class Matrix4;

/**
/**
* An Axis Aligned Bounding Box is a simple cuboid which encloses a given set
* of points, such as the vertices of a model. It is defined by an origin,
* located at the centre of the AABB, and symmetrical extents in 3 dimension
* which determine its size.
*
* A valid extents vector has all components >= 0, an assumption allowing for
* A valid extents vector has all components >= 0, an assumption allowing for
* a few optimisations.
*/
class AABB
Expand All @@ -26,18 +26,18 @@ class AABB
Vector3 extents;

/// Construct an AABB with default origin and invalid extents.
AABB() :
origin(0, 0, 0),
AABB() :
origin(0, 0, 0),
extents(-1,-1,-1)
{}

/// Construct an AABB with the provided origin and extents vectors.
AABB(const Vector3& origin_, const Vector3& extents_) :
origin(origin_),
AABB(const Vector3& origin_, const Vector3& extents_) :
origin(origin_),
extents(extents_)
{}

/**
/**
* Static named constructor to create an AABB that encloses the provided
* minimum and maximum points.
*/
Expand Down Expand Up @@ -123,7 +123,7 @@ class AABB

/**
* Classifies the position of this AABB with respect to the given plane.
*
*
* @returns: intersection classification, where VOLUME_INSIDE refers to
* the AABB being completely on the positive side of the plane (where the
* normal vector is pointing to).
Expand All @@ -134,7 +134,7 @@ class AABB
* Like classifyPlane, but uses the given matrix to transform the plane.
*
* TODO: Better documentation. TODO: Use enum as return value
*
*
* @returns: 0 = totally outside, 1 = partially inside, 2 = totally inside
*/
unsigned int classifyOrientedPlane(const Matrix4& transform, const Plane3& plane) const;
Expand Down Expand Up @@ -201,7 +201,7 @@ inline bool AABB::isValid() const
// +/- FLT_MAX, and the extents between 0 and FLT_MAX.
for (int i = 0; i < 3; ++i)
{
if (origin[i] < -FLT_MAX || origin[i] > FLT_MAX ||
if (origin[i] < -FLT_MAX || origin[i] > FLT_MAX ||
extents[i] < 0 || extents[i] > FLT_MAX)
{
return false;
Expand Down Expand Up @@ -284,8 +284,8 @@ inline void AABB::getPlanes(Plane3 planes[6]) const
/// Stream insertion for AABB class.
inline std::ostream& operator<< (std::ostream& os, const AABB& aabb)
{
return os << "AABB(origin=" << aabb.getOrigin().pp()
<< ", extents=" << aabb.getExtents().pp() << ")";
return os << "AABB(origin=" << math::pp(aabb.getOrigin())
<< ", extents=" << math::pp(aabb.getExtents()) << ")";
}

class AABBExtendByPoint
Expand Down
16 changes: 8 additions & 8 deletions libs/math/Vector3.h
Expand Up @@ -102,14 +102,6 @@ class BasicVector3
T y() const { return _v[1]; }
T z() const { return _v[2]; }

/// Return human readable debug string (pretty print)
std::string pp() const
{
std::stringstream ss;
ss << "[" << x() << ", " << y() << ", " << z() << "]";
return ss.str();
}

/// Compare this BasicVector3 against another for equality.
bool operator== (const BasicVector3& other) const {
return (other.x() == x()
Expand Down Expand Up @@ -368,6 +360,14 @@ BasicVector3<T> midPoint(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
return (v1 + v2) * 0.5;
}

/// Return human readable debug string (pretty print)
template<typename T> std::string pp(const BasicVector3<T>& v)
{
std::stringstream ss;
ss << "[" << v.x() << ", " << v.y() << ", " << v.z() << "]";
return ss.str();
}

}

// ==========================================================================================
Expand Down
30 changes: 15 additions & 15 deletions libs/math/Vector4.h
Expand Up @@ -77,21 +77,6 @@ class BasicVector4
const T& z() const { return _v[2]; }
const T& w() const { return _v[3]; }

/**
* \brief Return a readable (pretty-printed) string representation of the
* vector.
*
* We need a dedicated function for this because the standard operator<< is
* already used for serialisation to the less readable space-separated text
* format.
*/
std::string pp() const
{
std::stringstream ss;
ss << "(" << x() << ", " << y() << ", " << z() << ", " << w() << ")";
return ss.str();
}

/// Dot product this BasicVector4 with another vector
T dot(const BasicVector4<T>& other) const {
return x() * other.x()
Expand Down Expand Up @@ -258,4 +243,19 @@ inline bool isNear(const BasicVector4<T>& v1, const BasicVector4<T>& v2, double
&& std::abs(diff.z()) < epsilon && std::abs(diff.w()) < epsilon;
}

/**
* \brief Return a readable (pretty-printed) string representation of a
* BasicVector4.
*
* We need a dedicated function for this because the standard operator<< is
* already used for serialisation to the less readable space-separated text
* format.
*/
template<typename T> std::string pp(const BasicVector4<T>& v)
{
std::stringstream ss;
ss << "(" << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w() << ")";
return ss.str();
}

}

0 comments on commit 78ec565

Please sign in to comment.