// cOctree.h
// 3/14/04
// An octree that stores each triangle. Generates a collision list of possible triangles
// that could intersect with the ball. 
// Made one failed attempt, then heavily borrowed from
// code by Kurt Miller and Tomas Akenine-Möller 
// http://kurtm.flipcode.com/devlog-OctreesforPotentialColliders.shtml    

#ifndef octree_h
#define octree_h

#include "ForwardDeclarations.h"
#include "cBoundingBox.h"
#include "cVector.h"
#include "cVertex.h"
#include "cTriangle.h"
#include "cEdge.h"

#include <vector>

class cOctree
{
public:
    cOctree();
    ~cOctree();
    
    // build the octree structor from the triangles in tripool and 
    bool build(const vector<cTriangle*>& tripool, const cBoundingBox& box);
    
    // builds a list of triangles that could collide with the box without any duplicates
    bool buildCollisionList(vector<cTriangle*>& triangles, cBoundingBox& box);
    
    void draw();
    
private:
    // build the 8 octant boxes inside me
    void createChildBoxes(cBoundingBox* boxes);

    const static int maxTrianglePerNode = 10;  
    float minBoxWidth;

    cBoundingBox box_;
    cOctree* children_[8];
    vector<cTriangle*> triangles_; 
};

#endif

