-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial import of library header and implementation files.
- Loading branch information
1 parent
22a9935
commit 8d945aa
Showing
28 changed files
with
2,645 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,3 @@ | ||
# Compiled Object files | ||
*.slo | ||
*.lo | ||
*.o | ||
|
||
# Compiled Dynamic libraries | ||
*.so | ||
|
||
# Compiled Static libraries | ||
*.lai | ||
*.la | ||
*.a | ||
Debug/* | ||
Release/* | ||
*.vcxproj.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "Precompiled.hpp" | ||
#include "BoundingBox.hpp" | ||
|
||
#include <algorithm> | ||
|
||
namespace | ||
{ | ||
Math::BoundingBox::BoxCorner operator ++ (Math::BoundingBox::BoxCorner& corner, int) | ||
{ | ||
Math::BoundingBox::BoxCorner old_corner = corner; | ||
corner = static_cast<Math::BoundingBox::BoxCorner>(corner + 1); | ||
return old_corner; | ||
} | ||
} | ||
|
||
namespace Math | ||
{ | ||
BoundingBox::BoundingBox() | ||
{ | ||
} | ||
|
||
BoundingBox::BoundingBox(const Vector3& minimum, const Vector3& maximum) : | ||
mMinimum(Vector3::minimise(minimum, maximum)), | ||
mMaximum(Vector3::maximise(minimum, maximum)) | ||
{ | ||
} | ||
|
||
bool BoundingBox::contains(const Vector3& point) const | ||
{ | ||
return | ||
(mMinimum.x <= point.x && point.x <= mMaximum.x) && | ||
(mMinimum.y <= point.y && point.y <= mMaximum.y) && | ||
(mMinimum.z <= point.z && point.z <= mMaximum.z); | ||
} | ||
|
||
void BoundingBox::set(const Vector3& minimum, const Vector3& maximum) | ||
{ | ||
mMinimum = Vector3::minimise(minimum, maximum); | ||
mMaximum = Vector3::maximise(minimum, maximum); | ||
} | ||
|
||
Vector3 BoundingBox::get_corner(BoxCorner corner) const | ||
{ | ||
const float x = (corner & 1) ? mMaximum.x : mMinimum.x; | ||
const float y = (corner & 2) ? mMaximum.y : mMinimum.y; | ||
const float z = (corner & 4) ? mMaximum.z : mMinimum.z; | ||
return Vector3(x, y, z); | ||
} | ||
|
||
BoundingBox::CornerArray BoundingBox::get_all_corners() const | ||
{ | ||
CornerArray result; | ||
BoxCorner corner = NEAR_BOTTOM_LEFT; | ||
|
||
std::generate_n(std::begin(result), result.size(), [this, &corner]() { return get_corner(corner++); }); | ||
|
||
return result; | ||
} | ||
|
||
BoundingBox BoundingBox::compute_containing_box(const BoundingBox& a, const BoundingBox& b) | ||
{ | ||
return BoundingBox(Vector3::minimise(a.minimum_corner(), b.minimum_corner()), Vector3::maximise(a.maximum_corner(), b.maximum_corner())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#pragma once | ||
#ifndef __BOUNDINGBOX_HPP__ | ||
#define __BOUNDINGBOX_HPP__ | ||
|
||
#include "Vector3.hpp" | ||
|
||
#include <type_traits> | ||
#include <iterator> | ||
#include <numeric> | ||
#include <functional> | ||
#include <array> | ||
|
||
namespace Math | ||
{ | ||
|
||
class BoundingBox | ||
{ | ||
public: | ||
enum BoxCorner | ||
{ | ||
NEAR_BOTTOM_LEFT, | ||
NEAR_BOTTOM_RIGHT, | ||
NEAR_TOP_LEFT, | ||
NEAR_TOP_RIGHT, | ||
FAR_BOTTOM_LEFT, | ||
FAR_BOTTOM_RIGHT, | ||
FAR_TOP_LEFT, | ||
FAR_TOP_RIGHT | ||
}; | ||
|
||
typedef std::array<Vector3, 8> CornerArray; | ||
|
||
private: | ||
Vector3 mMinimum; | ||
Vector3 mMaximum; | ||
|
||
public: | ||
//-------------------------------------------------------------------------- | ||
// Constructors | ||
// | ||
|
||
BoundingBox(); | ||
|
||
BoundingBox(const Vector3& minimum, const Vector3& maximum); | ||
|
||
BoundingBox(const BoundingBox& box) : mMinimum(box.mMinimum), mMaximum(box.mMaximum) | ||
{ | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Assignment | ||
// | ||
|
||
BoundingBox& operator = (const BoundingBox& box) | ||
{ | ||
mMinimum = box.mMinimum; | ||
mMaximum = box.mMaximum; | ||
return *this; | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Comparison | ||
// | ||
|
||
bool operator == (const BoundingBox& box) const | ||
{ | ||
return mMinimum == box.mMinimum && mMaximum == box.mMaximum; | ||
} | ||
|
||
bool operator != (const BoundingBox& box) const | ||
{ | ||
return mMinimum != box.mMinimum || mMaximum != box.mMaximum; | ||
} | ||
|
||
bool contains(const Vector3& point) const; | ||
|
||
bool is_empty() const | ||
{ | ||
return mMinimum == mMaximum; | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Accessors | ||
// | ||
|
||
void set(const Vector3& minimum, const Vector3& maximum); | ||
|
||
const Vector3& minimum_corner() const | ||
{ | ||
return mMinimum; | ||
} | ||
|
||
const Vector3& maximum_corner() const | ||
{ | ||
return mMaximum; | ||
} | ||
|
||
Vector3 get_corner(BoxCorner corner) const; | ||
|
||
CornerArray get_all_corners() const; | ||
|
||
//-------------------------------------------------------------------------- | ||
// Computation | ||
// | ||
|
||
static BoundingBox compute_containing_box(const BoundingBox& a, const BoundingBox& b); | ||
|
||
template <class Iterator> | ||
typename std::enable_if<std::is_same<typename std::iterator_traits<Iterator>::value_type, BoundingBox>::value, BoundingBox>::type | ||
static compute_containing_box(Iterator begin, Iterator end) | ||
{ | ||
if (begin != end) | ||
{ | ||
BoundingBox bounds = *begin++; | ||
return std::accumulate(begin, end, bounds, std::ptr_fun(&compute_containing_box)); | ||
} | ||
|
||
return BoundingBox(); | ||
} | ||
}; | ||
|
||
} // namespace Math | ||
|
||
|
||
#endif // __BOUNDINGBOX_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "Precompiled.hpp" | ||
#include "BoundingSphere.hpp" | ||
#include "Intersect.hpp" | ||
#include "Line.hpp" | ||
|
||
namespace Math | ||
{ | ||
|
||
BoundingSphere BoundingSphere::compute_containing_sphere(const BoundingSphere& a, const BoundingSphere& b) | ||
{ | ||
Intersect::VolumeResult result = Intersect::test(a, b); | ||
|
||
if (result == Intersect::VOLUME_CONTAINS) // A contains B | ||
{ | ||
return a; | ||
} | ||
|
||
if (result == Intersect::VOLUME_CONTAINED) // B contains A | ||
{ | ||
return b; | ||
} | ||
|
||
Line line(a.center(), b.center()); | ||
line.extend(a.radius(), b.radius()); | ||
return BoundingSphere(line.mid_point(), line.length() / 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#pragma once | ||
#ifndef __MATHS_BOUNDINGSPHERE_HPP__ | ||
#define __MATHS_BOUNDINGSPHERE_HPP__ | ||
|
||
#include "Vector3.hpp" | ||
|
||
#include <type_traits> | ||
#include <iterator> | ||
#include <algorithm> | ||
#include <functional> | ||
#include <numeric> | ||
|
||
namespace Math | ||
{ | ||
|
||
class BoundingSphere | ||
{ | ||
private: | ||
Vector3 mCenter; | ||
float mRadius; | ||
|
||
public: | ||
//-------------------------------------------------------------------------- | ||
// Constructors | ||
// | ||
|
||
BoundingSphere() : mRadius(0.0f) | ||
{ | ||
} | ||
|
||
BoundingSphere(const Vector3& center, float radius) : mCenter(center), mRadius(std::abs(radius)) | ||
{ | ||
} | ||
|
||
BoundingSphere(const BoundingSphere& sphere) : mCenter(sphere.mCenter), mRadius(sphere.mRadius) | ||
{ | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Assignment | ||
// | ||
|
||
BoundingSphere& operator = (const BoundingSphere& sphere) | ||
{ | ||
mCenter = sphere.mCenter; | ||
mRadius = sphere.mRadius; | ||
return *this; | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Comparison | ||
// | ||
|
||
bool operator == (const BoundingSphere& sphere) const | ||
{ | ||
return mCenter == sphere.mCenter && mRadius == sphere.mRadius; | ||
} | ||
|
||
bool operator != (const BoundingSphere& sphere) const | ||
{ | ||
return mCenter != sphere.mCenter || mRadius != sphere.mRadius; | ||
} | ||
|
||
bool contains(const Vector3& point) const | ||
{ | ||
return Vector3(mCenter - point).length_squared() <= mRadius * mRadius; | ||
} | ||
|
||
bool is_empty() | ||
{ | ||
return mRadius <= FLT_EPSILON; | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Accessors | ||
// | ||
|
||
void set(const Vector3& center, float radius) | ||
{ | ||
mCenter = center; | ||
mRadius = std::abs(radius); | ||
} | ||
|
||
void center(const Vector3& center) | ||
{ | ||
mCenter = center; | ||
} | ||
|
||
const Vector3& center() const | ||
{ | ||
return mCenter; | ||
} | ||
|
||
void radius(float radius) | ||
{ | ||
mRadius = std::abs(radius); | ||
} | ||
|
||
float radius() const | ||
{ | ||
return mRadius; | ||
} | ||
|
||
static BoundingSphere compute_containing_sphere(const BoundingSphere& a, const BoundingSphere& b); | ||
|
||
template <class Iterator> | ||
typename std::enable_if<std::is_same<typename std::iterator_traits<Iterator>::value_type, BoundingSphere>::value, BoundingSphere>::type | ||
static compute_containing_sphere(Iterator begin, Iterator end) | ||
{ | ||
if (begin != end) | ||
{ | ||
BoundingSphere bounds = *begin++; | ||
return std::accumulate(begin, end, bounds, std::ptr_fun(&compute_containing_sphere)); | ||
} | ||
|
||
return BoundingSphere(); | ||
} | ||
}; | ||
|
||
} // namespace Math | ||
|
||
#endif // __MATHS_BOUNDINGSPHERE_H__ |
Oops, something went wrong.