35#ifdef OO_DEBUG_MONITOR_ENABLED
36#define OctreeDebugLog(format, ...) \
38 if (EXPECT_NOT(gDebugFlags & DEBUG_OCTREE_LOGGING)) \
39 OOLog(@"octree.debug", format, ##__VA_ARGS__); \
42#define OctreeDebugLog(...) \
54@interface Octree (Private)
56#ifndef OODEBUGLDRAWING_DISABLE
58- (void)drawOctreeFromLocation:(uint32_t)loc scale:(GLfloat)scale offset:(
Vector)offset;
59- (void)drawOctreeCollisionFromLocation:(uint32_t)loc scale:(GLfloat)scale offset:(
Vector)offset;
62- (void)setHasCollision:(BOOL)value;
70static const int change_oct[] = { 0, 1, 2, 4, 3, 5, 6, 7 };
84 [
NSException raise:NSInternalInconsistencyException format:@"Call of invalid initializer %s", __FUNCTION__];
89- (id)initWithData:(NSData*)data
90 radius:(GLfloat)radius
92 if ((
self = [super
init])) {
96 NSUInteger nodeCount = [_data length] /
sizeof *
_octree;
97 NSParameterAssert(nodeCount < UINT32_MAX);
112- (id)initWithDictionary:(NSDictionary*)dict
114 NSData* data = [
dict objectForKey:@"octree"];
115 if (![data isKindOfClass:[NSData
class]] || ([data length] %
sizeof(
int)) != 0) {
137- (void)setHasCollision:(BOOL)value
147 .octree_collision = _collisionOctree
155 return [[[
Octree alloc] initWithData:_data radius:_radius * factor] autorelease];
160 return make_vector((0.5f - (GLfloat)((oct >> 2) & 1)) * r, (0.5f - (GLfloat)((oct >> 1) & 1)) * r, (0.5f - (GLfloat)(oct & 1)) * r);
163#ifndef OODEBUGLDRAWING_DISABLE
167 OODebugWFState state = OODebugBeginWireframe(NO);
170 OOGL(glEnable(GL_BLEND));
172 glColor4f(0.4f, 0.4f, 0.4f, 0.5f);
175 [
self drawOctreeFromLocation:0 scale:_radius offset:kZeroVector];
179 OODebugEndWireframe(state);
184#define OCTREE_COLOR(r, g, b, a) glColor4f(r, g, b, a)
186#define OCTREE_COLOR(r, g, b, a) \
191- (void)drawOctreeFromLocation:(uint32_t)loc scale:(GLfloat)scale offset:(
Vector)offset
193 if (_octree[loc] == 0) {
199 if (_octree[loc] == -1)
231 }
else if (_octree[loc] > 0) {
232 GLfloat sc = 0.5f * scale;
233 OCTREE_COLOR(0.4f, 0.4f, 0.4f, 0.5f);
234 [
self drawOctreeFromLocation:loc + _octree[loc] + 0 scale:sc offset:make_vector(offset.x - sc, offset.y - sc, offset.z - sc)];
235 OCTREE_COLOR(0.0f, 0.0f, 1.0f, 0.5f);
236 [
self drawOctreeFromLocation:loc + _octree[loc] + 1 scale:sc offset:make_vector(offset.x - sc, offset.y - sc, offset.z + sc)];
237 OCTREE_COLOR(0.0f, 1.0f, 0.0f, 0.5f);
238 [
self drawOctreeFromLocation:loc + _octree[loc] + 2 scale:sc offset:make_vector(offset.x - sc, offset.y + sc, offset.z - sc)];
239 OCTREE_COLOR(0.0f, 1.0f, 1.0f, 0.5f);
240 [
self drawOctreeFromLocation:loc + _octree[loc] + 3 scale:sc offset:make_vector(offset.x - sc, offset.y + sc, offset.z + sc)];
241 OCTREE_COLOR(1.0f, 0.0f, 0.0f, 0.5f);
242 [
self drawOctreeFromLocation:loc + _octree[loc] + 4 scale:sc offset:make_vector(offset.x + sc, offset.y - sc, offset.z - sc)];
243 OCTREE_COLOR(1.0f, 0.0f, 1.0f, 0.5f);
244 [
self drawOctreeFromLocation:loc + _octree[loc] + 5 scale:sc offset:make_vector(offset.x + sc, offset.y - sc, offset.z + sc)];
245 OCTREE_COLOR(1.0f, 1.0f, 0.0f, 0.5f);
246 [
self drawOctreeFromLocation:loc + _octree[loc] + 6 scale:sc offset:make_vector(offset.x + sc, offset.y + sc, offset.z - sc)];
247 OCTREE_COLOR(1.0f, 1.0f, 1.0f, 0.5f);
248 [
self drawOctreeFromLocation:loc + _octree[loc] + 7 scale:sc offset:make_vector(offset.x + sc, offset.y + sc, offset.z + sc)];
252static BOOL drawTestForCollisions;
254- (void)drawOctreeCollisions
256 OODebugWFState state = OODebugBeginWireframe(NO);
259 drawTestForCollisions = NO;
261 [
self drawOctreeCollisionFromLocation:0 scale:_radius offset:kZeroVector];
263 _hasCollision = drawTestForCollisions;
265 OODebugEndWireframe(state);
269- (void)drawOctreeCollisionFromLocation:(uint32_t)loc scale:(GLfloat)scale offset:(
Vector)offset
271 if (_octree[loc] == 0) {
275 if ((_octree[loc] != 0) && (_collisionOctree[loc] != (
unsigned char)0))
279 GLfloat red = (GLfloat)(_collisionOctree[loc]) / 255.0;
280 glColor4f(1.0, 0.0, 0.0, red);
282 drawTestForCollisions = YES;
283 _collisionOctree[loc]--;
286 OOGL(glDisable(GL_CULL_FACE));
288 OOGL(glDisable(GL_TEXTURE_2D));
320 OOGL(glEnable(GL_CULL_FACE));
322 if (_octree[loc] > 0) {
323 GLfloat sc = 0.5f * scale;
324 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 0 scale:sc offset:make_vector(offset.x - sc, offset.y - sc, offset.z - sc)];
325 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 1 scale:sc offset:make_vector(offset.x - sc, offset.y - sc, offset.z + sc)];
326 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 2 scale:sc offset:make_vector(offset.x - sc, offset.y + sc, offset.z - sc)];
327 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 3 scale:sc offset:make_vector(offset.x - sc, offset.y + sc, offset.z + sc)];
328 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 4 scale:sc offset:make_vector(offset.x + sc, offset.y - sc, offset.z - sc)];
329 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 5 scale:sc offset:make_vector(offset.x + sc, offset.y - sc, offset.z + sc)];
330 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 6 scale:sc offset:make_vector(offset.x + sc, offset.y + sc, offset.z - sc)];
331 [
self drawOctreeCollisionFromLocation:loc + _octree[loc] + 7 scale:sc offset:make_vector(offset.x + sc, offset.y + sc, offset.z + sc)];
336static BOOL
isHitByLineSub(
const int* octbuffer,
unsigned char* collbuffer,
int nextLevel, GLfloat rad, GLfloat rd2,
Vector v0,
Vector v1,
int octantMask)
338 if (octbuffer[nextLevel + octantMask]) {
340 return isHitByLine(octbuffer, collbuffer, nextLevel + octantMask, rd2, v0, v1, moveLine, 0);
353 OctreeDebugLog(
@"DEBUG octant: [%d] radius: %.2f vs. line: (%.2f, %.2f, %.2f) - (%.2f, %.2f, %.2f)",
354 level, rad, u0.
x, u0.
y, u0.
z, u1.
x, u1.
y, u1.
z);
356 if (octbuffer[level] == 0) {
361 if (octbuffer[level] == -1) {
363 collbuffer[
level] = 2;
368 int faces = face_hit;
377 int octantIntersected = 0;
383 octantIntersected = ((vi.
x < 0.0) ? 1 : 5) + ((vi.
y < 0.0) ? 0 : 2);
385 octantIntersected = ((vi.
x < 0.0) ? 0 : 4) + ((vi.
y < 0.0) ? 0 : 2);
388 octantIntersected = ((vi.
y < 0.0) ? 4 : 6) + ((vi.
z < 0.0) ? 0 : 1);
390 octantIntersected = ((vi.
y < 0.0) ? 0 : 2) + ((vi.
z < 0.0) ? 0 : 1);
393 octantIntersected = ((vi.
x < 0.0) ? 2 : 6) + ((vi.
z < 0.0) ? 0 : 1);
395 octantIntersected = ((vi.
x < 0.0) ? 0 : 4) + ((vi.
z < 0.0) ? 0 : 1);
397 OctreeDebugLog(
@"----> found intersection with face 0x%2x of cube of radius %.2f at (%.2f, %.2f, %.2f) octant:%d",
398 faces, rad, vi.
x, vi.
y, vi.
z, octantIntersected);
400 OctreeDebugLog(
@"----> inside cube of radius %.2f octant:%d", rad, octantIntersected);
405 collbuffer[
level] = 1;
409 int nextLevel = level + octbuffer[
level];
411 GLfloat rd2 = 0.5f * rad;
414 int oct0, oct1, oct2, oct3;
415 oct0 = octantIntersected;
421 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct0))
426 OctreeDebugLog(
@"----> testing next three octants [+%d] [+%d] [+%d]", oct1, oct2, oct3);
427 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct1))
429 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct2))
431 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct3))
441 OctreeDebugLog(
@"----> testing back three octants [+%d] [+%d] [+%d]", oct1, oct2, oct3);
442 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct1))
444 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct2))
446 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct3))
451 if (
isHitByLineSub(octbuffer, collbuffer, nextLevel, rad, rd2, u0, u1, oct0))
476 const int* axialBuffer = axialDetails.
octree;
477 const int* otherBuffer = otherDetails.
octree;
479 if (axialBuffer[0] == 0) {
489 if (otherBuffer[0] == 0) {
494 GLfloat axialRadius = axialDetails.
radius;
495 GLfloat otherRadius = otherDetails.
radius;
497 if (otherRadius < axialRadius)
500 if ((otherPosition.
x + otherRadius < -axialRadius) || (otherPosition.
x - otherRadius > axialRadius) || (otherPosition.
y + otherRadius < -axialRadius) || (otherPosition.
y - otherRadius > axialRadius) || (otherPosition.
z + otherRadius < -axialRadius) || (otherPosition.
z - otherRadius > axialRadius)) {
501 OctreeDebugLog(
@"%@",
@"----> Other sphere does not intersect axial cube");
508 if ((axialPosition.
x + axialRadius < -otherRadius) || (axialPosition.
x - axialRadius > otherRadius) || (axialPosition.
y + axialRadius < -otherRadius) || (axialPosition.
y - axialRadius > otherRadius) || (axialPosition.
z + axialRadius < -otherRadius) || (axialPosition.
z - axialRadius > otherRadius)) {
509 OctreeDebugLog(
@"%@",
@"----> Axial sphere does not intersect other cube");
517 if (axialBuffer[0] == -1) {
519 if (otherBuffer[0] == -1) {
521 axialCollisionBuffer[0] = (
unsigned char)255;
522 otherCollisionBuffer[0] = (
unsigned char)255;
535 int nearest_oct = ((otherPosition.
x > 0.0) ? 0 : 4) | ((otherPosition.
y > 0.0) ? 0 : 2) | ((otherPosition.
z > 0.0) ? 0 : 1);
537 int nextLevel = otherBuffer[0];
538 const int* nextBuffer = &otherBuffer[
nextLevel];
539 unsigned char* nextCollisionBuffer = &otherCollisionBuffer[
nextLevel];
541 Vector voff, nextPosition;
544 nextDetails.
radius = 0.5f * otherRadius;
545 for (i = 0; i < 8; i++) {
554 if (
isHitByOctree(axialDetails, nextDetails, nextPosition, other_ijk))
570 int nearest_oct = ((otherPosition.
x > 0.0) ? 4 : 0) | ((otherPosition.
y > 0.0) ? 2 : 0) | ((otherPosition.
z > 0.0) ? 1 : 0);
572 int nextLevel = axialBuffer[0];
573 const int* nextBuffer = &axialBuffer[
nextLevel];
574 unsigned char* nextCollisionBuffer = &axialCollisionBuffer[
nextLevel];
579 nextDetails.
radius = 0.5f * axialRadius;
580 for (i = 0; i < 8; i++) {
588 if (
isHitByOctree(nextDetails, otherDetails, nextPosition, other_ijk)) {
630 [
NSNumber numberWithFloat:_radius], @"radius",
636 const int* octBuffer = octree_details.
octree;
637 GLfloat octRadius = octree_details.
radius;
639 if (octBuffer[0] == 0) {
643 if (octBuffer[0] == -1 || depthLimit == 0) {
644 return octRadius * octRadius * octRadius;
650 GLfloat sumVolume = 0.0f;
652 int nextLevel = octBuffer[0];
653 const int* nextBuffer = &octBuffer[
nextLevel];
656 nextDetails.
radius = 0.5f * octRadius;
659 for (i = 0; i < 8; i++) {
662 nextDetails.
octree = &nextBuffer[
i];
685 const int* octBuffer = details.
octree;
686 GLfloat octRadius = details.
radius;
688 if (octBuffer[0] == 0)
690 if (octBuffer[0] == -1)
696 int nextLevel = octBuffer[0];
697 const int* nextBuffer = &octBuffer[
nextLevel];
700 nextDetails.
radius = 0.5f * octRadius;
704 for (i = 0; i < 8; i++) {
705 int octant = oct ^ i;
706 if (nextBuffer[octant])
721#ifdef OO_DEBUG_MONITOR_ENABLED
724 return [
self oo_objectSize] + _nodeCount * [_data oo_objectSize] + [_data length] + _nodeCount * sizeof *_collisionOctree;
740@implementation OOOctreeBuilder
745 return &
self->_stateStack[
self->
_level];
753 self->_octree[
index] = value;
762 NSCAssert(
State(
self)->remaining > 0,
@"Attempt to add node to a full parent in octree builder.");
763 State(
self)->remaining--;
770 if ((
self = [super
init])) {
783 State(
self)->remaining = 1;
795- (
Octree*)buildOctreeWithRadius:(GLfloat)radius
797 NSAssert(
State(
self)->remaining == 0 &&
_level == 0,
@"Attempt to produce octree from an octree builder in an incomplete state.");
800 int* resized = realloc(
_octree, dataSize);
807 NSData* data = [
NSData dataWithBytesNoCopy:resized
815 return [[[
Octree alloc] initWithData:data radius:radius] autorelease];
847 State(
self)->insertionPoint = newInsertionPoint;
848 State(
self)->remaining = 8;
853 NSAssert(
State(
self)->remaining == 0,
@"Attempt to end an inner octree node with fewer than eight children.");
854 NSAssert1(
_level > 0,
@"Unbalanced call to %s", __FUNCTION__);
869 NSAssert(
_nodeCount > 8,
@"After ending an inner node, there must be at least eight nodes in buffer.");
884 newCapacity =
MAX(newCapacity, index + 1);
886 int* newBuffer = realloc(
self->_octree, newCapacity *
sizeof *newBuffer);
888 [
NSException raise:NSMallocException format:@"Failed to allocate memory for octree."];
891 self->_octree = newBuffer;
892 self->_capacity = newCapacity;
893 self->_octree[
index] = value;
#define OO_ENTER_OPENGL()
BOOL OOCheckOpenGLErrors(NSString *format,...)
OOINLINE Vector resolveVectorInIJK(Vector v0, Triangle ijk)
OOINLINE Vector vector_flip(Vector v) INLINE_CONST_FUNC
OOINLINE Vector vector_add(Vector a, Vector b) INLINE_CONST_FUNC
OOINLINE Vector vector_subtract(Vector a, Vector b) INLINE_CONST_FUNC
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Vector lineIntersectionWithFace(Vector p1, Vector p2, long mask, GLfloat rd) CONST_FUNC
int lineCubeIntersection(Vector v0, Vector v1, GLfloat rd) CONST_FUNC
static const int change_oct[]
@ kMinimumBuilderCapacity
static BOOL isHitByLine(const int *octbuffer, unsigned char *collbuffer, int level, GLfloat rad, Vector v0, Vector v1, Vector off, int face_hit)
#define OctreeDebugLog(...)
static GLfloat volumeOfOctree(Octree_details octree_details, unsigned depthLimit)
static BOOL isHitByOctree(Octree_details axialDetails, Octree_details otherDetails, Vector delta, Triangle other_ijk)
static Vector randomFullNodeFrom(Octree_details details, Vector offset)
Octree_details octreeDetails()
static void SetNode_slow(OOOctreeBuilder *self, uint32_t index, int value) NO_INLINE_FUNC
OOINLINE struct OOOctreeBuildState * State(OOOctreeBuilder *self)
BOOL suppressClangStuff()
OOINLINE void InsertNode(OOOctreeBuilder *self, int value)
struct OOOctreeBuilder::OOOctreeBuildState _stateStack[kMaxOctreeDepth+1]
OOINLINE void SetNode(OOOctreeBuilder *self, uint32_t index, int value)
unsigned char * _collisionOctree
Octree_details octreeDetails()
void setHasCollision:(BOOL value)
static BOOL isHitByLine(const int *octbuffer, unsigned char *collbuffer, int level, GLfloat rad, Vector v0, Vector v1, Vector off, int face_hit)
NSDictionary * dictionaryRepresentation()
static BOOL isHitByOctree(Octree_details axialDetails, Octree_details otherDetails, Vector otherPosition, Triangle other_ijk)
static BOOL isHitByLineSub(const int *octbuffer, unsigned char *collbuffer, int nextLevel, GLfloat rad, GLfloat rd2, Vector v0, Vector v1, int octantMask)
static Vector randomFullNodeFrom(Octree_details details, Vector offset)
static GLfloat volumeOfOctree(Octree_details octree_details, unsigned depthLimit)
id initWithData:radius:(NSData *data, [radius] GLfloat radius)
static Vector offsetForOctant(int oct, GLfloat r)
unsigned char * octree_collision