Oolite
Loading...
Searching...
No Matches
Octree.h
Go to the documentation of this file.
1/*
2
3Octree.h
4
5Octtree class for collision detection.
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
27#import "OOFoundation.h"
28#import "OOMaths.h"
29#import "OOOpenGL.h"
30
31#define OCTREE_MIN_HALF_WIDTH 1.0
32
33#if !defined(OODEBUGLDRAWING_DISABLE) && !defined(OO_DEBUG_MONITOR_ENABLED)
34#define OODEBUGLDRAWING_DISABLE 1
35#endif
36
37@interface Octree : NSObject {
38@private
39 GLfloat _radius;
40 uint32_t _nodeCount;
41 const int* _octree;
43
44 unsigned char* _collisionOctree;
45
46 NSData* _data;
47}
48
49/*
50- (id) initWithDictionary:
51
52 Deserialize an octree from cache representation.
53 (To make a new octree, build it with OOOctreeBuilder.)
54*/
55- (id)initWithDictionary:(NSDictionary*)dictionary;
56
57- (Octree*)octreeScaledBy:(GLfloat)factor;
58
59#ifndef OODEBUGLDRAWING_DISABLE
60- (void)drawOctree;
61- (void)drawOctreeCollisions;
62#endif
63
64- (GLfloat)isHitByLine:(Vector)v0 v1:(Vector)v1;
65
66- (BOOL)isHitByOctree:(Octree*)other withOrigin:(Vector)origin andIJK:(Triangle)ijk;
67- (BOOL)isHitByOctree:(Octree*)other withOrigin:(Vector)origin andIJK:(Triangle)ijk s1:(GLfloat)s1 s2:(GLfloat)s2;
68
69- (NSDictionary*)dictionaryRepresentation;
70
71- (GLfloat)volume;
72
73- (Vector)randomPoint;
74
75#ifdef OO_DEBUG_MONITOR_ENABLED
76- (size_t)totalSize;
77#endif
78
79@end
80
81enum {
82 kMaxOctreeDepth = 7 // 128x128x128
83};
84
85@interface OOOctreeBuilder : NSObject {
86@private
87 int* _octree;
91 uint32_t remaining;
92 } _stateStack[kMaxOctreeDepth + 1];
93 uint8_t _level;
94}
95
96/*
97 -buildOctreeWithRadius:(GLfloat)radius
98
99 Generate an octree with the current data in the builder and the specified
100 radius, and clear the builder. If NDEBUG is undefined, throws an exception
101 if the structure of the octree is invalid.
102*/
103- (Octree*)buildOctreeWithRadius:(GLfloat)radius;
104
105/*
106 Append nodes to the octree.
107
108 There are three types of nodes: solid, empty, and inner nodes.
109 An inner node must have exactly eight children, which may be any type of
110 node. Exactly one node must be added at root level.
111
112 The order of child nodes is defined as follows: the index of a child node
113 is a three bit number. The highest bit represents x, the middle bit
114 represents y and the low bit represents z. A set bit indicates the high-
115 coordinate half of the parent node, and a clear bit indicates the low-
116 coordinate half.
117
118 For instance, if the parent node is a cube ranging from -1 to 1 on each
119 axis, the child 101 (5) represents x 0..1, y -1..0, z 0..1.
120*/
121- (void)writeSolid;
122- (void)writeEmpty;
123- (void)beginInnerNode;
124- (void)endInnerNode;
125
126@end
@ kMaxOctreeDepth
Definition Octree.h:82
static BOOL isHitByLine(const int *octbuffer, unsigned char *collbuffer, int level, GLfloat rad, Vector v0, Vector v1, Vector off, int face_hit)
static BOOL isHitByOctree(Octree_details axialDetails, Octree_details otherDetails, Vector delta, Triangle other_ijk)
uint8_t _level
Definition Octree.h:93
uint32_t _capacity
Definition Octree.h:88
uint32_t _nodeCount
Definition Octree.h:88
struct OOOctreeBuilder::OOOctreeBuildState _stateStack[kMaxOctreeDepth+1]
int * _octree
Definition Octree.h:87
unsigned char * _collisionOctree
Definition Octree.h:44
GLfloat _radius
Definition Octree.h:39
const int * _octree
Definition Octree.h:41
uint32_t _nodeCount
Definition Octree.h:40
BOOL _hasCollision
Definition Octree.h:42
NSData * _data
Definition Octree.h:46