[算法][包围盒]AABB简单类

头文件:

#pragma once
#include <iostream>
//一个假的点类型
struct Vector3
{
    float x;
    float y;
    float z;
};

 
class AABB
{
public:
    AABB();
    AABB(const AABB &aabb);
    ~AABB();
    void add(const Vector3 &v);
    void clear();
    void makeAABB(Vector3 V[], int n);
 
    Vector3 min, max, center;
};
 
void aabbPrint(AABB &ab);

 

cpp文件

#include "AABB.h"


AABB::AABB()
{
}
 
AABB::AABB(const AABB &aabb)
{
    min = aabb.min;
    max = aabb.max;
    center = aabb.center;
}
 
void AABB::clear()
{
    min.x = min.y = min.z = FLT_MAX;
    //careful: This is not FLT_MIN, 'cause FLT_MIN is a little larger than 0,
    //hence not a minus value.
    max.x = max.y = max.z = -FLT_MAX;
}
 
void AABB::add(const Vector3 &v)
{
    if (v.x < min.x) min.x = v.x;
    if (v.y < min.y) min.y = v.y;
    if (v.z < min.z) min.z = v.z;
    if (v.x > max.x) max.x = v.x;
    if (v.y > max.y) max.y = v.y;
    if (v.z > max.z) max.z = v.z;
}
 
//make AABB out of Vector3 points
void AABB::makeAABB(Vector3 V[], int n)
{
    if (!V) return;
 
    for (int i = 0; i < n; i++)
        add(V[i]);
 
    center.x = (min.x + max.x) * 0.5f;
    center.y = (min.y + max.y) * 0.5f;
    center.z = (min.z + max.z) * 0.5f;
}
 
AABB::~AABB()
{
}
 
void aabbPrint(AABB &ab)
{
    std::cout<<"AABB min is: ";
    ab.min.x;
    ab.min.y;
    ab.min.z;
    std::cout<<"AABB max is: ";
    ab.max.x;
    ab.max.y;
    ab.max.z;
    std::cout<<std::endl; 
}

 

打印什么的自己看着办,没时间搞

posted @ 2016-04-13 11:12  南水之源  阅读(1303)  评论(0编辑  收藏  举报