Oolite 1.91.0.7604-240417-a536cbe
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 "OOCocoa.h"
28#import "OOOpenGL.h"
29#import "OOMaths.h"
30
31#define OCTREE_MIN_HALF_WIDTH 1.0
32
33
34#if !defined(OODEBUGLDRAWING_DISABLE) && defined(NDEBUG)
35#define OODEBUGLDRAWING_DISABLE 1
36#endif
37
38
39@interface Octree: NSObject
40{
41@private
42 GLfloat _radius;
43 uint32_t _nodeCount;
44 const int *_octree;
46
47 unsigned char *_collisionOctree;
48
49 NSData *_data;
50}
51
52/*
53 - (id) initWithDictionary:
54
55 Deserialize an octree from cache representation.
56 (To make a new octree, build it with OOOctreeBuilder.)
57*/
58- (id) initWithDictionary:(NSDictionary *)dictionary;
59
60- (Octree *) octreeScaledBy:(GLfloat)factor;
61
62#ifndef OODEBUGLDRAWING_DISABLE
63- (void) drawOctree;
64- (void) drawOctreeCollisions;
65#endif
66
67- (GLfloat) isHitByLine:(Vector)v0 :(Vector)v1;
68
69- (BOOL) isHitByOctree:(Octree *)other withOrigin:(Vector)origin andIJK:(Triangle)ijk;
70- (BOOL) isHitByOctree:(Octree *)other withOrigin:(Vector)origin andIJK:(Triangle)ijk andScales:(GLfloat)s1 :(GLfloat)s2;
71
72- (NSDictionary *) dictionaryRepresentation;
73
74- (GLfloat) volume;
75
76- (Vector) randomPoint;
77
78
79#ifndef NDEBUG
80- (size_t) totalSize;
81#endif
82
83@end
84
85
86enum
87{
88 kMaxOctreeDepth = 7 // 128x128x128
89};
90
91
92@interface OOOctreeBuilder: NSObject
93{
94@private
95 int *_octree;
96 uint_fast32_t _nodeCount, _capacity;
98 {
100 uint32_t remaining;
101 } _stateStack[kMaxOctreeDepth + 1];
102 uint_fast8_t _level;
103}
104
105/*
106 -buildOctreeWithRadius:(GLfloat)radius
107
108 Generate an octree with the current data in the builder and the specified
109 radius, and clear the builder. If NDEBUG is undefined, throws an exception
110 if the structure of the octree is invalid.
111*/
112- (Octree *) buildOctreeWithRadius:(GLfloat)radius;
113
114/*
115 Append nodes to the octree.
116
117 There are three types of nodes: solid, empty, and inner nodes.
118 An inner node must have exactly eight children, which may be any type of
119 node. Exactly one node must be added at root level.
120
121 The order of child nodes is defined as follows: the index of a child node
122 is a three bit number. The highest bit represents x, the middle bit
123 represents y and the low bit represents z. A set bit indicates the high-
124 coordinate half of the parent node, and a clear bit indicates the low-
125 coordinate half.
126
127 For instance, if the parent node is a cube ranging from -1 to 1 on each
128 axis, the child 101 (5) represents x 0..1, y -1..0, z 0..1.
129*/
130- (void) writeSolid;
131- (void) writeEmpty;
132- (void) beginInnerNode;
133- (void) endInnerNode;
134
135@end
@ kMaxOctreeDepth
Definition Octree.h:88
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)
uint_fast32_t _nodeCount
Definition Octree.h:96
uint_fast32_t _capacity
Definition Octree.h:96
uint_fast8_t _level
Definition Octree.h:102
struct OOOctreeBuilder::OOOctreeBuildState _stateStack[kMaxOctreeDepth+1]
int * _octree
Definition Octree.h:95
unsigned char * _collisionOctree
Definition Octree.h:47
GLfloat _radius
Definition Octree.h:42
const int * _octree
Definition Octree.h:44
uint32_t _nodeCount
Definition Octree.h:43
BOOL _hasCollision
Definition Octree.h:45
NSData * _data
Definition Octree.h:49