iceaabb.cpp
来自「opcode是功能强大」· C++ 代码 · 共 406 行 · 第 1/2 页
CPP
406 行
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Contains AABB-related code.
* \file IceAABB.cpp
* \author Pierre Terdiman
* \date January, 29, 2000
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* AABB class.
* \class AABB
* \author Pierre Terdiman
* \version 1.0
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Precompiled Header
#include "Opcode/Stdafx.h"
using namespace IceMaths;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Computes the sum of two AABBs.
* \param aabb [in] the other AABB
* \return Self-Reference
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AABB& AABB::Add(const AABB& aabb)
{
// Compute new min & max values
Point Min; GetMin(Min);
Point Tmp; aabb.GetMin(Tmp);
Min.Min(Tmp);
Point Max; GetMax(Max);
aabb.GetMax(Tmp);
Max.Max(Tmp);
// Update this
SetMinMax(Min, Max);
return *this;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Makes a cube from the AABB.
* \param cube [out] the cube AABB
* \return cube edge length
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float AABB::MakeCube(AABB& cube) const
{
Point Ext; GetExtents(Ext);
float Max = Ext.Max();
Point Cnt; GetCenter(Cnt);
cube.SetCenterExtents(Cnt, Point(Max, Max, Max));
return Max;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Makes a sphere from the AABB.
* \param sphere [out] sphere containing the AABB
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AABB::MakeSphere(Sphere& sphere) const
{
GetExtents(sphere.mCenter);
sphere.mRadius = sphere.mCenter.Magnitude() * 1.00001f; // To make sure sphere::Contains(*this) succeeds
GetCenter(sphere.mCenter);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Checks a box is inside another box.
* \param box [in] the other AABB
* \return true if current box is inside input box
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool AABB::IsInside(const AABB& box) const
{
if(box.GetMin(0)>GetMin(0)) return false;
if(box.GetMin(1)>GetMin(1)) return false;
if(box.GetMin(2)>GetMin(2)) return false;
if(box.GetMax(0)<GetMax(0)) return false;
if(box.GetMax(1)<GetMax(1)) return false;
if(box.GetMax(2)<GetMax(2)) return false;
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Computes the AABB planes.
* \param planes [out] 6 planes surrounding the box
* \return true if success
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool AABB::ComputePlanes(Plane* planes) const
{
// Checkings
if(!planes) return false;
Point Center, Extents;
GetCenter(Center);
GetExtents(Extents);
// Writes normals
planes[0].n = Point(1.0f, 0.0f, 0.0f);
planes[1].n = Point(-1.0f, 0.0f, 0.0f);
planes[2].n = Point(0.0f, 1.0f, 0.0f);
planes[3].n = Point(0.0f, -1.0f, 0.0f);
planes[4].n = Point(0.0f, 0.0f, 1.0f);
planes[5].n = Point(0.0f, 0.0f, -1.0f);
// Compute a point on each plane
Point p0 = Point(Center.x+Extents.x, Center.y, Center.z);
Point p1 = Point(Center.x-Extents.x, Center.y, Center.z);
Point p2 = Point(Center.x, Center.y+Extents.y, Center.z);
Point p3 = Point(Center.x, Center.y-Extents.y, Center.z);
Point p4 = Point(Center.x, Center.y, Center.z+Extents.z);
Point p5 = Point(Center.x, Center.y, Center.z-Extents.z);
// Compute d
planes[0].d = -(planes[0].n|p0);
planes[1].d = -(planes[1].n|p1);
planes[2].d = -(planes[2].n|p2);
planes[3].d = -(planes[3].n|p3);
planes[4].d = -(planes[4].n|p4);
planes[5].d = -(planes[5].n|p5);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Computes the aabb points.
* \param pts [out] 8 box points
* \return true if success
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool AABB::ComputePoints(Point* pts) const
{
// Checkings
if(!pts) return false;
// Get box corners
Point min; GetMin(min);
Point max; GetMax(max);
// 7+------+6 0 = ---
// /| /| 1 = +--
// / | / | 2 = ++-
// / 4+---/--+5 3 = -+-
// 3+------+2 / y z 4 = --+
// | / | / | / 5 = +-+
// |/ |/ |/ 6 = +++
// 0+------+1 *---x 7 = -++
// Generate 8 corners of the bbox
pts[0] = Point(min.x, min.y, min.z);
pts[1] = Point(max.x, min.y, min.z);
pts[2] = Point(max.x, max.y, min.z);
pts[3] = Point(min.x, max.y, min.z);
pts[4] = Point(min.x, min.y, max.z);
pts[5] = Point(max.x, min.y, max.z);
pts[6] = Point(max.x, max.y, max.z);
pts[7] = Point(min.x, max.y, max.z);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Gets vertex normals.
* \param pts [out] 8 box points
* \return true if success
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const Point* AABB::GetVertexNormals() const
{
static float VertexNormals[] =
{
-INVSQRT3, -INVSQRT3, -INVSQRT3,
INVSQRT3, -INVSQRT3, -INVSQRT3,
INVSQRT3, INVSQRT3, -INVSQRT3,
-INVSQRT3, INVSQRT3, -INVSQRT3,
-INVSQRT3, -INVSQRT3, INVSQRT3,
INVSQRT3, -INVSQRT3, INVSQRT3,
INVSQRT3, INVSQRT3, INVSQRT3,
-INVSQRT3, INVSQRT3, INVSQRT3
};
return (const Point*)VertexNormals;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Returns edges.
* \return 24 indices (12 edges) indexing the list returned by ComputePoints()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?