Bubba-3D  0.9.0
Awesome game engine!
Octree.h
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #pragma once
18 
19 #include <functional>
20 #include <linmath/float3.h>
21 #include <vector>
22 #include <set>
23 #include <ctime>
24 #include <math.h>
25 #include "AABB2.h"
26 
27 class Triangle;
28 
29 const int MAX_DEPTH = 8;
30 const int MAX_CHILDREN = 16;
31 
32 class Octree {
33 public:
39  Octree();
40 
49  Octree(chag::float3 origin, chag::float3 halfVector);
50 
51  ~Octree();
52 
53  void insertTriangle(Triangle* t);
54  void insertAll(std::vector<Triangle*> &triangles);
55 
59  bool hasChildren();
65  void getChildren(std::vector<Octree*>* octs);
69  void clearChildren();
70 
71  int getOctantContainingPoint(const chag::float3& point);
72  int getTriangleCount();
80  std::vector<Triangle*> *getTriangles();
81 
85  int getNumberOfSubTrees();
86 
87 
93  void getTrianglesInsersectedByRayCast(chag::float3 rayOrigin, chag::float3 rayVector,
94  std::vector<Triangle*> *triangleList);
95 
99  AABB* getAABB();
100 
101 private:
102  Octree(chag::float3 origin, chag::float3 halfVector, int depth);
103 
104  void setupAABB(chag::float3 origin, chag::float3 halfVector);
105 
109  void createChildren();
110 
116  chag::float3 combineTwoPointsByComparator(chag::float3 p1, chag::float3 p2,
117  std::function<bool(float, float)> comparator);
118 
119  bool rayCastIntersectsAABB(chag::float3 rayOrigin, chag::float3 rayVector);
120  void putTrianglesToList(std::vector<Triangle *> *triangleList);
121 
122  chag::float3 origin;
123  chag::float3 halfVector;
124 
125  Octree *children[8];
126  std::vector<Triangle*> ts;
127  int depth;
128 
129 
130  AABB aabb;
131 };
int getTriangleCountRecursively()
Definition: Octree.cpp:153
std::vector< Triangle * > * getTriangles()
Definition: Octree.cpp:83
void getTrianglesInsersectedByRayCast(chag::float3 rayOrigin, chag::float3 rayVector, std::vector< Triangle * > *triangleList)
Definition: Octree.cpp:222
AABB * getAABB()
Definition: Octree.cpp:149
Definition: Octree.h:32
void clearChildren()
Definition: Octree.cpp:50
void getChildren(std::vector< Octree * > *octs)
Definition: Octree.cpp:128
bool hasChildren()
Definition: Octree.cpp:124
int getNumberOfSubTrees()
Definition: Octree.cpp:165
Definition: AABB2.h:23
Octree()
Definition: Octree.cpp:24