RedWoft

To be or not to be, it is a question.
Axis-aligned bounding box

 

#ifndef BOUNDING_BOX_H
#define BOUNDING_BOX_H

#include 
"BoundingVolume.h"

namespace Math
{
    
// Axis-aligned bounding box.
    class BoundingBox:public BoundingVolume
    {
    
public:

        
// @desc: None.
        BoundingBox();

        
// @desc: Set member variable to the values that the parameters specified.
        BoundingBox(const Vector3& p_vec3Min, const Vector3& p_vec3Max);

        
// @desc: None.
        virtual ~BoundingBox();

        
// @desc: Check the visibility of bounding box.
        virtual bool CheckVisible(const Frustum& p_Frustum);

        
// @desc: Append new point to the bounding box.
        void AppendPoint(const Vector3& p_vec3Vertex);

        
// @desc: The combine of bounding box.
        void AppendBoundingBox(const BoundingBox& p_BoundingBox)
        {
            AppendPoint(p_BoundingBox.m_vec3Min);
            AppendPoint(p_BoundingBox.m_vec3Max);
        }

        
// @desc: Calculate the center of bounding box.
        const Vector3 CalculateCenter() const
        {
            
return MiddlePoint(m_vec3Min, m_vec3Max);
        }

        
// The corner point of the bounding box.
        Vector3 m_vec3Min;
        Vector3 m_vec3Max;
    };

    
// @desc: None.
    BoundingBox::BoundingBox():m_vec3Min(),m_vec3Max(){}

    
// @desc: Set member variable to the values that the parameters specified.
    BoundingBox::BoundingBox(const Vector3& p_vec3Min, const Vector3& p_vec3Max):m_vec3Min(p_vec3Min),m_vec3Max(p_vec3Max){}

    
// @desc: None.
    BoundingBox::~BoundingBox(){}

    
// @desc: Check the visibility of bounding box.
    bool BoundingBox::CheckVisible(const Frustum& p_Frustum)
    {
        
const Plane* pClipPlane = p_Frustum.GetClipPlane();
        
for (unsigned i = 0U; i < Frustum::s_uiPlaneNumber; ++i)
        {
            
if (PointToPlane(pClipPlane[i], m_vec3Min.x, m_vec3Min.y, m_vec3Min.z) > 0.0fcontinue;
            
if (PointToPlane(pClipPlane[i], m_vec3Max.x, m_vec3Min.y, m_vec3Min.z) > 0.0fcontinue;   
            
if (PointToPlane(pClipPlane[i], m_vec3Min.x, m_vec3Max.y, m_vec3Min.z) > 0.0fcontinue;   
            
if (PointToPlane(pClipPlane[i], m_vec3Max.x, m_vec3Max.y, m_vec3Min.z) > 0.0fcontinue;   
            
if (PointToPlane(pClipPlane[i], m_vec3Min.x, m_vec3Min.y, m_vec3Max.z) > 0.0fcontinue;   
            
if (PointToPlane(pClipPlane[i], m_vec3Max.x, m_vec3Min.y, m_vec3Max.z) > 0.0fcontinue;   
            
if (PointToPlane(pClipPlane[i], m_vec3Min.x, m_vec3Max.y, m_vec3Max.z) > 0.0fcontinue;   
            
if (PointToPlane(pClipPlane[i], m_vec3Max.x, m_vec3Max.y, m_vec3Max.z) > 0.0fcontinue;
            
return false;
        }
        
return true;
    }

    
// @desc: Append new point to the bounding box.
    void BoundingBox::AppendPoint(const Vector3& p_vec3Vertex)
    {
        
if (p_vec3Vertex.x < m_vec3Min.x) { m_vec3Min.x = p_vec3Vertex.x; }
        
if (p_vec3Vertex.x > m_vec3Max.x) { m_vec3Max.x = p_vec3Vertex.x; }
        
if (p_vec3Vertex.y < m_vec3Min.y) { m_vec3Min.y = p_vec3Vertex.y; }
        
if (p_vec3Vertex.y > m_vec3Max.y) { m_vec3Max.y = p_vec3Vertex.y; }
        
if (p_vec3Vertex.z < m_vec3Min.z) { m_vec3Min.z = p_vec3Vertex.z; }
        
if (p_vec3Vertex.z > m_vec3Max.z) { m_vec3Max.z = p_vec3Vertex.z; }
    }
}

#endif

 

 

posted on 2010-07-01 05:26  RedWoft  阅读(1056)  评论(0)    收藏  举报