2007-06-14
This commit is contained in:
1 parent
e20673c2cd
commit
1ae34ae340
96 files changed
+5680
-1020
No files matched your search
+82
-10
@@ -1,4 +1,46 @@
|
||||
/* Derived from code written by Jonathan Kreuzer.
|
||||
*
|
||||
* See: http://www.3dkingdoms.com/weekly/weekly.php?a=21
|
||||
*
|
||||
* basically the same as bbox.h/.cpp but using coldet math
|
||||
*
|
||||
* -- quote from a mail of the author --
|
||||
*
|
||||
* You're free to continue using my CBBox code however you want.
|
||||
* ... [snip] ...
|
||||
* The only thing I ask is a note about where it came from ( I think
|
||||
* you said you added a link to the article, so that's fine. )
|
||||
*
|
||||
*/
|
||||
|
||||
/************************************************************************
|
||||
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
|
||||
* *
|
||||
* This software is provided as-is, without any express or implied *
|
||||
* warranty. In no event will the authors be held liable for any *
|
||||
* damages arising from the use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute *
|
||||
* it freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must *
|
||||
* not claim that you wrote the original software. If you use this *
|
||||
* software in a product, an acknowledgment in the product documentation *
|
||||
* would be appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must *
|
||||
* not be misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source *
|
||||
* distribution. *
|
||||
************************************************************************/
|
||||
|
||||
#include <cassert>
|
||||
#include "log.h"
|
||||
#include "obox.h"
|
||||
|
||||
#include "plane.h"
|
||||
// --------------------------
|
||||
//
|
||||
// Oriented Bounding Box Class
|
||||
@@ -8,7 +50,7 @@
|
||||
//
|
||||
// Check if a point is in this bounding box
|
||||
//
|
||||
bool OBox::IsPointInBox(const Vector3D &InP) const
|
||||
bool OBox::isPointInBox(const Vector3D &InP) const
|
||||
{
|
||||
// Rotate the point into the box's coordinates
|
||||
Vector3D P = Transform(InP, m_M.Inverse());
|
||||
@@ -23,7 +65,7 @@ bool OBox::IsPointInBox(const Vector3D &InP) const
|
||||
//
|
||||
// Check if a sphere overlaps any part of this bounding box
|
||||
//
|
||||
bool OBox::IsSphereInBox( const Vector3D &InP, float fRadius) const
|
||||
bool OBox::isSphereInBox( const Vector3D &InP, float fRadius) const
|
||||
{
|
||||
float fDist;
|
||||
float fDistSq = 0;
|
||||
@@ -44,7 +86,7 @@ bool OBox::IsSphereInBox( const Vector3D &InP, float fRadius) const
|
||||
//
|
||||
// Check if the bounding box is completely behind a plane( defined by a normal and a point )
|
||||
//
|
||||
bool OBox::BoxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP ) const
|
||||
bool OBox::boxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP ) const
|
||||
{
|
||||
// Plane Normal in Box Space
|
||||
Vector3D Norm = rotateVector(InNorm, m_M.Inverse() );
|
||||
@@ -52,7 +94,7 @@ bool OBox::BoxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP ) const
|
||||
|
||||
float Extent = Norm * m_Extent; //Norm.Dot( m_Extent ); // Box Extent along the plane normal
|
||||
//float Distance = InNorm.Dot( GetCenterPoint() - InP ); // Distance from Box Center to the Plane
|
||||
float Distance = InNorm * (GetCenterPoint() - InP);
|
||||
float Distance = InNorm * (getCenterPoint() - InP);
|
||||
|
||||
// If Box Centerpoint is behind the plane further than its extent, the Box is outside the plane
|
||||
if ( Distance < -Extent ) return true;
|
||||
@@ -62,7 +104,7 @@ bool OBox::BoxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP ) const
|
||||
//
|
||||
// Does the Line (L1, L2) intersect the Box?
|
||||
//
|
||||
bool OBox::IsLineInBox( const Vector3D& L1, const Vector3D& L2 ) const
|
||||
bool OBox::isLineInBox( const Vector3D& L1, const Vector3D& L2 ) const
|
||||
{
|
||||
// Put line in box space
|
||||
Matrix3D MInv = m_M.Inverse();
|
||||
@@ -87,10 +129,40 @@ bool OBox::IsLineInBox( const Vector3D& L1, const Vector3D& L2 ) const
|
||||
return true;
|
||||
}
|
||||
|
||||
void OBox::lineCrossBox(const Vector3D& L1, const Vector3D& L2, Vector3D & isecLocal) const {
|
||||
// Put line in box space
|
||||
Matrix3D MInv = m_M.Inverse();
|
||||
Vector3D LB1 = Transform(L1, MInv);
|
||||
Vector3D LB2 = Transform(L2, MInv);
|
||||
float small_t = 2.0f;
|
||||
Vector3D p_copy(0, 0, 0);
|
||||
|
||||
//i = 0: -x,-z <-> -x,z
|
||||
//i = 1: -x,-z <-> x,-z
|
||||
//i = 2: x,-z, <-> x,z
|
||||
//i = 3: -x,z <-> x,z
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Vector3D s1((i <= 1 || i == 3 ? -m_Extent.x : m_Extent.x), 0, (i < 3 ? -m_Extent.z : m_Extent.z));
|
||||
Vector3D s2((i == 0 ? -m_Extent.x : m_Extent.x), 0, (i == 1 ? -m_Extent.z : m_Extent.z));
|
||||
Vector3D p;
|
||||
float dt = Math::intersection_segments(s1, s2, LB1, LB2, p);
|
||||
if ((dt >= 0.0f) && (dt < small_t)) {
|
||||
p_copy = p;
|
||||
small_t = dt;
|
||||
}
|
||||
}
|
||||
if (small_t >= 0.0f && small_t <= 1.0f) {
|
||||
isecLocal = p_copy;
|
||||
return;
|
||||
}
|
||||
ERROR << "Did not find intersection when OBB says there is one :-(" << std::endl;
|
||||
isecLocal = L1;
|
||||
}
|
||||
|
||||
//
|
||||
// Returns a 3x3 rotation matrix as vectors
|
||||
//
|
||||
inline void OBox::GetInvRot( Vector3D *pvRot ) const
|
||||
inline void OBox::getInvRot( Vector3D *pvRot ) const
|
||||
{
|
||||
pvRot[0] = Vector3D( m_M.m[0][0], m_M.m[0][1], m_M.m[0][2] );
|
||||
pvRot[1] = Vector3D( m_M.m[1][0], m_M.m[1][1], m_M.m[1][2] );
|
||||
@@ -101,13 +173,13 @@ inline void OBox::GetInvRot( Vector3D *pvRot ) const
|
||||
// Check if any part of a box is inside any part of another box
|
||||
// Uses the separating axis test.
|
||||
//
|
||||
bool OBox::IsBoxInBox( OBox &BBox ) const
|
||||
bool OBox::isBoxInBox( OBox &BBox ) const
|
||||
{
|
||||
Vector3D SizeA = m_Extent;
|
||||
Vector3D SizeB = BBox.m_Extent;
|
||||
Vector3D RotA[3], RotB[3];
|
||||
GetInvRot( RotA );
|
||||
BBox.GetInvRot( RotB );
|
||||
getInvRot( RotA );
|
||||
BBox.getInvRot( RotB );
|
||||
|
||||
float R[3][3]; // Rotation from B to A
|
||||
float AR[3][3]; // absolute values of R matrix, to use with box extents
|
||||
@@ -123,7 +195,7 @@ bool OBox::IsBoxInBox( OBox &BBox ) const
|
||||
}
|
||||
|
||||
// Vector separating the centers of Box B and of Box A
|
||||
Vector3D vSepWS = BBox.GetCenterPoint() - GetCenterPoint();
|
||||
Vector3D vSepWS = BBox.getCenterPoint() - getCenterPoint();
|
||||
// Rotated into Box A's coordinates
|
||||
Vector3D vSepA( vSepWS * RotA[0], vSepWS * RotA[1], vSepWS * RotA[2] );
|
||||
|
||||
|
||||
+53
-17
@@ -1,43 +1,79 @@
|
||||
/* Derived from code written by Jonathan Kreuzer.
|
||||
*
|
||||
* See: http://www.3dkingdoms.com/weekly/weekly.php?a=21
|
||||
*
|
||||
* basically the same as bbox.h/.cpp but using coldet math
|
||||
*
|
||||
* -- quote from a mail of the author --
|
||||
*
|
||||
* You're free to continue using my CBBox code however you want.
|
||||
* ... [snip] ...
|
||||
* The only thing I ask is a note about where it came from ( I think
|
||||
* you said you added a link to the article, so that's fine. )
|
||||
*
|
||||
*/
|
||||
|
||||
/************************************************************************
|
||||
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
|
||||
* *
|
||||
* This software is provided as-is, without any express or implied *
|
||||
* warranty. In no event will the authors be held liable for any *
|
||||
* damages arising from the use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute *
|
||||
* it freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must *
|
||||
* not claim that you wrote the original software. If you use this *
|
||||
* software in a product, an acknowledgment in the product documentation *
|
||||
* would be appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must *
|
||||
* not be misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source *
|
||||
* distribution. *
|
||||
************************************************************************/
|
||||
|
||||
#include "math3d.h"
|
||||
|
||||
// see: from: http://www.3dkingdoms.com/weekly/weekly.php?a=21
|
||||
|
||||
// basically the same as bbox.h/.cpp but using coldet math
|
||||
|
||||
class OBox
|
||||
{
|
||||
public:
|
||||
OBox() {}
|
||||
OBox( const Matrix3D & m, const Vector3D & extent )
|
||||
{ Set( m, extent ); }
|
||||
{ set( m, extent ); }
|
||||
OBox( const Matrix3D & m, const Vector3D & low, const Vector3D & high )
|
||||
{ Set( m, low, high ); }
|
||||
{ set( m, low, high ); }
|
||||
OBox( const OBox & other)
|
||||
{ Set( other.m_M, other.m_Extent ); }
|
||||
{ set( other.m_M, other.m_Extent ); }
|
||||
|
||||
void Set( const Matrix3D & m, const Vector3D & extent )
|
||||
void set( const Matrix3D & m, const Vector3D & extent )
|
||||
{
|
||||
m_M = m;
|
||||
m_Extent = extent;
|
||||
}
|
||||
void Set( const Matrix3D & m, const Vector3D & low, const Vector3D & high )
|
||||
void set( const Matrix3D & m, const Vector3D & low, const Vector3D & high )
|
||||
{
|
||||
m_M = m;
|
||||
m_M.Translate( 0.5f * (low + high) );
|
||||
m_Extent = 0.5f * (high - low);
|
||||
}
|
||||
|
||||
Vector3D GetSize() const
|
||||
Vector3D getSize() const
|
||||
{ return 2.0f * m_Extent; }
|
||||
Vector3D GetCenterPoint() const
|
||||
Vector3D getCenterPoint() const
|
||||
{ return m_M.GetTranslate(); }
|
||||
void GetInvRot( Vector3D *pvRot ) const;
|
||||
void getInvRot( Vector3D *pvRot ) const;
|
||||
|
||||
bool IsPointInBox( const Vector3D & p ) const ;
|
||||
bool IsBoxInBox( OBox & box ) const ;
|
||||
bool IsSphereInBox( const Vector3D & p, float fRadius ) const ;
|
||||
bool IsLineInBox( const Vector3D & l1, const Vector3D & l2 ) const ;
|
||||
bool BoxOutsidePlane( const Vector3D & normal, const Vector3D & p ) const ;
|
||||
bool isPointInBox( const Vector3D & p ) const ;
|
||||
bool isBoxInBox( OBox & box ) const ;
|
||||
bool isSphereInBox( const Vector3D & p, float fRadius ) const ;
|
||||
bool isLineInBox( const Vector3D & l1, const Vector3D & l2 ) const ;
|
||||
bool boxOutsidePlane( const Vector3D & normal, const Vector3D & p ) const ;
|
||||
|
||||
void lineCrossBox(const Vector3D & l1, const Vector3D & l2, Vector3D & isecLocalSpace) const;
|
||||
|
||||
// Data
|
||||
Matrix3D m_M;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
#include "plane.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace Math {
|
||||
|
||||
// see: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/example.cpp
|
||||
float intersection_segments(const Vector3D & s1, const Vector3D & s2,
|
||||
const Vector3D & l1, const Vector3D & l2, Vector3D & hit) {
|
||||
float denom = (l2.z - l1.z) * (s2.x - s1.x) -
|
||||
(l2.x - l1.x) * (s2.z - s1.z);
|
||||
|
||||
float d_a = (l2.x - l1.x) * (s1.z - l1.z) -
|
||||
(l2.z - l1.z) * (s1.x - l1.x);
|
||||
|
||||
float d_b = (s2.x - s1.x) * (s1.z - l1.z) -
|
||||
(s2.z - s1.z) * (s1.x - l1.x);
|
||||
|
||||
float ua = d_a / denom; // ratio on s1 -> s2
|
||||
float ub = d_b / denom; // ratio on l1 -> l2
|
||||
|
||||
if (denom == 0.0f) {
|
||||
/*
|
||||
if (d_a == 0.0f && d_b == 0.0f)
|
||||
INFO << "COINCIDENT" << std::endl;
|
||||
else
|
||||
INFO << "PARALLEL" << std::endl;
|
||||
*/
|
||||
return -1.0f;
|
||||
}
|
||||
|
||||
if (ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f) {
|
||||
hit.x = s1.x + ua * (s2.x - s1.x);
|
||||
hit.z = s1.z + ua * (s2.z - s1.z);
|
||||
//INFO << "INTERSECTING " << hit.x << " " << hit.z << std::endl;
|
||||
//INFO << ua << " | " << ub << std::endl;
|
||||
return ub;
|
||||
}
|
||||
//INFO << "NOT INTERSECTING" << std::endl;
|
||||
return -1.0f;
|
||||
}
|
||||
|
||||
int Plane::segmentIntersect(const Vector3D & p1, const Vector3D & p2, Vector3D & p_p) {
|
||||
float d = - normal.x * pop.x - normal.y * pop.y - normal.z * pop.z;
|
||||
float denom = normal.x * (p2.x - p1.x) + normal.y * (p2.y - p1.y) +
|
||||
normal.z * (p2.z - p1.z);
|
||||
if (fabs(denom) < 0.001f)
|
||||
return false;
|
||||
float mu = - (d + normal.x * p1.x + normal.y * p1.y + normal.z * p1.z) / denom;
|
||||
p_p = Vector3D(p2 - p1) * mu + p1;
|
||||
if (mu < 0 || mu > 1)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
float Plane::distance(const Vector3D & p) {
|
||||
float d = normal * p;
|
||||
d += - normal.x * pop.x - normal.y * pop.y - normal.z * pop.z;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
int main() {
|
||||
Vector3D p(0, 0, 0);
|
||||
Vector3D n(-1, 0, 0);
|
||||
Math::Plane plane(p, n);
|
||||
Vector3D p1(-1, 0, 0);
|
||||
Vector3D p2(1, 0, 0);
|
||||
Vector3D col;
|
||||
if (plane.segmentIntersect(p1, p2, col))
|
||||
std::cout << "intersect at: " << col.x << " " << col.y << " " << col.z << std::endl;
|
||||
std::cout << plane.distance(p1) << std::endl;
|
||||
std::cout << plane.distance(p2) << std::endl;
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef MATH_PLANE_H
|
||||
#define MATH_PLANE_H
|
||||
|
||||
#include "math3d.h"
|
||||
|
||||
namespace Math {
|
||||
|
||||
float intersection_segments(const Vector3D & s1, const Vector3D & s2,
|
||||
const Vector3D & l1, const Vector3D & l2, Vector3D & hit);
|
||||
|
||||
struct Plane {
|
||||
Plane(const Vector3D & p, const Vector3D & n) :
|
||||
pop(p), normal(n) {}
|
||||
Plane(const Vector3D & p1, const Vector3D & p2, const Vector3D & p3) :
|
||||
pop(p1), normal() {
|
||||
const Vector3D pa(p2 - p1);
|
||||
const Vector3D pb(p3 - p1);
|
||||
normal = CrossProduct(pa, pb).Normalized();
|
||||
}
|
||||
Vector3D pop;
|
||||
Vector3D normal;
|
||||
int segmentIntersect(const Vector3D & p1, const Vector3D & p2, Vector3D & p_p);
|
||||
float distance(const Vector3D & p);
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user