Oolite
Loading...
Searching...
No Matches
OOOctreeBuilder Class Reference

#include <Octree.h>

Inheritance diagram for OOOctreeBuilder:
Collaboration diagram for OOOctreeBuilder:

Classes

struct  OOOctreeBuildState

Instance Methods

(Octree *) - buildOctreeWithRadius:
(void) - writeSolid
(void) - writeEmpty
(void) - beginInnerNode
(void) - endInnerNode
(OOINLINE struct OOOctreeBuildState *) - State [implementation]
(OOINLINE void) - SetNode [implementation]
(OOINLINE void) - InsertNode [implementation]
(id) - init [implementation]
(void) - dealloc [implementation]
(BOOL) - suppressClangStuff [implementation]

Class Methods

(static void) + SetNode_slow [implementation]

Private Attributes

int * _octree
uint32_t _nodeCount
uint32_t _capacity
struct OOOctreeBuilder::OOOctreeBuildState _stateStack [kMaxOctreeDepth+1]
uint8_t _level

Detailed Description

Definition at line 85 of file Octree.h.

Method Documentation

◆ beginInnerNode

- (void) beginInnerNode

Definition at line 828 of file Octree.m.

829{
830 NSAssert(_level < kMaxOctreeDepth, @"Attempt to build octree exceeding maximum depth.");
831
832 // Insert relative offset to next free space.
833 uint32_t newInsertionPoint = _nodeCount;
834 InsertNode(self, (int)_nodeCount - State(self)->insertionPoint);
835
836 /*
837 Leave space for eight nodes.
838
839 NOTE: this may leave memory uninitialized or leave _nodeCount pointing
840 past the end of the buffer. A valid sequence of eight child insertions
841 will fix both these problems by writing to all of the new slots.
842 */
843 _nodeCount += 8;
844
845 // Push state and set up new "stack frame".
846 _level++;
847 State(self)->insertionPoint = newInsertionPoint;
848 State(self)->remaining = 8;
849}
@ kMaxOctreeDepth
Definition Octree.h:82
uint8_t _level
Definition Octree.h:93
OOINLINE struct OOOctreeBuildState * State(OOOctreeBuilder *self)
Definition Octree.m:743
OOINLINE void InsertNode(OOOctreeBuilder *self, int value)
Definition Octree.m:760
uint32_t _nodeCount
Definition Octree.h:88

References _level, _nodeCount, beginInnerNode, InsertNode, kMaxOctreeDepth, and State.

Referenced by beginInnerNode, and BuildSubOctree().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ buildOctreeWithRadius:

- (Octree *) buildOctreeWithRadius: (GLfloat) radius

Definition at line 795 of file Octree.m.

795 :(GLfloat)radius
796{
797 NSAssert(State(self)->remaining == 0 && _level == 0, @"Attempt to produce octree from an octree builder in an incomplete state.");
798
799 size_t dataSize = _nodeCount * sizeof *_octree;
800 int* resized = realloc(_octree, dataSize);
801 if (resized == NULL)
802 resized = _octree;
803
804 /* Hand over the bytes to the data object, which will be used directly by
805 the Octree.
806 */
807 NSData* data = [NSData dataWithBytesNoCopy:resized
808 length:dataSize
809 freeWhenDone:YES];
810
811 _octree = NULL;
812 _nodeCount = 0;
813 _capacity = 0;
814
815 return [[[Octree alloc] initWithData:data radius:radius] autorelease];
816}
uint32_t _capacity
Definition Octree.h:88
int * _octree
Definition Octree.h:87

References _capacity, _level, _nodeCount, _octree, and State.

Referenced by OOMeshToOctreeConverter::findOctreeToDepth:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dealloc

- (void) dealloc
implementation

Definition at line 788 of file Octree.m.

789{
790 free(_octree);
791
792 [super dealloc];
793}

References _octree, and dealloc.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ endInnerNode

- (void) endInnerNode

Definition at line 851 of file Octree.m.

852{
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__);
855
856 _level--;
857
858 /*
859 Check if the last eight nodes are solid. If so, we just inserted an
860 entirely solid subtree and can fold it into a single solid node. An
861 entirely solid subtree will always use the last eight nodes (any solid
862 subtrees within a child node will have been folded already), so this
863 simple approach to folding will produce an optimal result.
864
865 We could do the same for empty nodes, but OOMeshToOctreeConverter will
866 never recurse into an empty subtree.
867 */
868
869 NSAssert(_nodeCount > 8, @"After ending an inner node, there must be at least eight nodes in buffer.");
870 for (uint32_t node = _nodeCount - 8; node < _nodeCount; node++) {
871 if (_octree[node] != -1)
872 return;
873 }
874
875 // If we got here, subtree is solid; fold it into a solid node.
876 _octree[State(self)->insertionPoint - 1] = -1;
877 _nodeCount -= 8;
878}

References _level, _nodeCount, _octree, endInnerNode, and State.

Referenced by BuildSubOctree(), and endInnerNode.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init

- (id) init
implementation

Definition at line 768 of file Octree.m.

769{
770 if ((self = [super init])) {
772 _octree = malloc(_capacity * sizeof *_octree);
773 if (_octree == NULL) {
774 [self release];
775 return nil;
776 }
777
778 /*
779 We're initially inserting into the root slot, which must exist and
780 takes a single node.
781 */
782 _nodeCount = 1;
783 State(self)->remaining = 1;
784 }
785 return self;
786}
return nil
@ kMinimumBuilderCapacity
Definition Octree.m:737

References _capacity, _nodeCount, _octree, init, kMinimumBuilderCapacity, nil, and State.

Referenced by init.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InsertNode

- (OOINLINE void) InsertNode (OOOctreeBuilder *) self
(int) value 
implementation

Definition at line 760 of file Octree.m.

761{
762 NSCAssert(State(self)->remaining > 0, @"Attempt to add node to a full parent in octree builder.");
763 State(self)->remaining--;
764
765 SetNode(self, State(self)->insertionPoint++, value);
766}
OOINLINE void SetNode(OOOctreeBuilder *self, uint32_t index, int value)
Definition Octree.m:750

References OOINLINE, SetNode, and State.

Referenced by beginInnerNode, writeEmpty, and writeSolid.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetNode

- (OOINLINE void) SetNode (OOOctreeBuilder *) self
(uint32_t) index
(int) value 
implementation

Definition at line 750 of file Octree.m.

751{
752 if (index < self->_capacity) {
753 self->_octree[index] = value;
754 } else {
755 SetNode_slow(self, index, value);
756 }
757}
static void SetNode_slow(OOOctreeBuilder *self, uint32_t index, int value) NO_INLINE_FUNC
Definition Octree.m:881

References _capacity, OOINLINE, and SetNode_slow.

Referenced by InsertNode.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetNode_slow

+ (void) SetNode_slow (OOOctreeBuilder *) self
(uint32_t) index
(int) value 
implementation

Definition at line 881 of file Octree.m.

882{
883 uint32_t newCapacity = MAX(self->_capacity * 2, (uint32_t)kMinimumBuilderCapacity);
884 newCapacity = MAX(newCapacity, index + 1);
885
886 int* newBuffer = realloc(self->_octree, newCapacity * sizeof *newBuffer);
887 if (EXPECT_NOT(newBuffer == NULL)) {
888 [NSException raise:NSMallocException format:@"Failed to allocate memory for octree."];
889 }
890
891 self->_octree = newBuffer;
892 self->_capacity = newCapacity;
893 self->_octree[index] = value;
894}
#define EXPECT_NOT(x)
#define MAX(A, B)
Definition OOMaths.h:94
return self

References EXPECT_NOT, kMinimumBuilderCapacity, MAX, self, and SetNode_slow.

Referenced by SetNode, and SetNode_slow.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ State

- (OOINLINE struct OOOctreeBuildState *) State (OOOctreeBuilder *) self
implementation

Definition at line 743 of file Octree.m.

744{
745 return &self->_stateStack[self->_level];
746}

References _level, and OOINLINE.

Referenced by beginInnerNode, buildOctreeWithRadius:, endInnerNode, init, and InsertNode.

Here is the caller graph for this function:

◆ suppressClangStuff

- (BOOL) suppressClangStuff
implementation

Definition at line 900 of file Octree.m.

901{
902 return &_stateStack && 0;
903}
struct OOOctreeBuilder::OOOctreeBuildState _stateStack[kMaxOctreeDepth+1]

References _stateStack, and suppressClangStuff.

Referenced by suppressClangStuff.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeEmpty

- (void) writeEmpty

Definition at line 823 of file Octree.m.

824{
825 InsertNode(self, 0);
826}

References InsertNode, and writeEmpty.

Referenced by BuildSubOctree(), and writeEmpty.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeSolid

- (void) writeSolid

Definition at line 818 of file Octree.m.

819{
820 InsertNode(self, -1);
821}

References InsertNode, and writeSolid.

Referenced by BuildSubOctree(), and writeSolid.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _capacity

- (uint32_t) _capacity
private

Definition at line 88 of file Octree.h.

Referenced by buildOctreeWithRadius:, init, and SetNode.

◆ _level

- (uint8_t) _level
private

Definition at line 93 of file Octree.h.

Referenced by beginInnerNode, buildOctreeWithRadius:, endInnerNode, and State.

◆ _nodeCount

- (uint32_t) _nodeCount
private

Definition at line 88 of file Octree.h.

Referenced by beginInnerNode, buildOctreeWithRadius:, endInnerNode, and init.

◆ _octree

- (int*) _octree
private

Definition at line 87 of file Octree.h.

Referenced by buildOctreeWithRadius:, dealloc, endInnerNode, and init.

◆ _stateStack

- (struct OOOctreeBuildState) OOOctreeBuilder:
private

Referenced by suppressClangStuff.


The documentation for this class was generated from the following files: