| 1 |
#ifndef _PROTO_SPACE |
| 2 |
#define _PROTO_SPACE |
| 3 |
|
| 4 |
|
| 5 |
#include "protoTree.h" |
| 6 |
#include <sys/types.h> // for BYTE_ORDER macro |
| 7 |
|
| 8 |
/** |
| 9 |
* @class ProtoSpace |
| 10 |
* |
| 11 |
* @brief For now, this maintains a set of "Nodes" in n-dimensional |
| 12 |
* space. Note that the "space" is destroyed, the Nodes themselves |
| 13 |
* are not destroyed. |
| 14 |
* (TBD - support n-dimensions) |
| 15 |
*/ |
| 16 |
class ProtoSpace |
| 17 |
{ |
| 18 |
public: |
| 19 |
ProtoSpace(); |
| 20 |
~ProtoSpace(); |
| 21 |
|
| 22 |
unsigned int GetDimensions() const |
| 23 |
{return num_dimensions;} |
| 24 |
|
| 25 |
/** |
| 26 |
* @class Node |
| 27 |
* |
| 28 |
* @brief This base class let's us associate ordinate position |
| 29 |
* information with any class that derives from it. |
| 30 |
*/ |
| 31 |
class Node |
| 32 |
{ |
| 33 |
public: |
| 34 |
virtual ~Node(); |
| 35 |
|
| 36 |
virtual unsigned int GetDimensions() const = 0; |
| 37 |
virtual double GetOrdinate(unsigned int dim) const = 0; |
| 38 |
|
| 39 |
protected: |
| 40 |
Node(); |
| 41 |
|
| 42 |
}; // end class ProtoSpace::Node |
| 43 |
|
| 44 |
bool InsertNode(Node& node); |
| 45 |
bool RemoveNode(Node& node); // returns false if Node not in space |
| 46 |
bool ContainsNode(Node& node); // returns false if Node not in space |
| 47 |
void Empty(); |
| 48 |
|
| 49 |
unsigned int GetNodeCount() const |
| 50 |
{return node_count;} |
| 51 |
|
| 52 |
class Ordinate : public ProtoSortedTree::Item |
| 53 |
{ |
| 54 |
public: |
| 55 |
Ordinate(); |
| 56 |
~Ordinate(); |
| 57 |
|
| 58 |
const char* GetKey() const |
| 59 |
{return key;} |
| 60 |
enum {KEYBYTES = (sizeof(double)+sizeof(Node*))}; |
| 61 |
enum {KEYBITS = 8*KEYBYTES}; |
| 62 |
|
| 63 |
// ProtoSortedTree::Item overrides |
| 64 |
unsigned int GetKeysize() const |
| 65 |
{return KEYBITS;} |
| 66 |
bool UseSignBit() const |
| 67 |
{return true;} |
| 68 |
bool UseComplement2() const |
| 69 |
{return false;} |
| 70 |
|
| 71 |
#if BYTE_ORDER == LITTLE_ENDIAN |
| 72 |
ProtoTree::Endian GetEndian() const |
| 73 |
{return ProtoTree::ENDIAN_LITTLE;} |
| 74 |
void SetNode(Node* theNode) |
| 75 |
{memcpy(key, &theNode, sizeof(Node*));} |
| 76 |
Node* GetNode() const |
| 77 |
{ |
| 78 |
Node* node; |
| 79 |
memcpy(&node, key, sizeof(Node*)); |
| 80 |
return node; |
| 81 |
} |
| 82 |
void SetValue(double theValue) |
| 83 |
{memcpy(key+sizeof(Node*), &theValue, sizeof(double));} |
| 84 |
double GetValue() const |
| 85 |
{ |
| 86 |
double value; |
| 87 |
memcpy(&value, key+sizeof(Node*), sizeof(double)); |
| 88 |
return value; |
| 89 |
} |
| 90 |
#else |
| 91 |
ProtoTree::Endian GetEndian() const |
| 92 |
{return ProtoTree::ENDIAN_BIG;} |
| 93 |
void SetNode(Node* theNode) |
| 94 |
{memcpy(key+sizeof(double), &theNode, sizeof(Node*));} |
| 95 |
Node* GetNode() const |
| 96 |
{ |
| 97 |
Node* node; |
| 98 |
memcpy(&node, key+sizeof(double), sizeof(Node*)); |
| 99 |
return node; |
| 100 |
} |
| 101 |
void SetValue(double theValue) |
| 102 |
{memcpy(key, &theValue, sizeof(double));} |
| 103 |
double GetValue() const |
| 104 |
{ |
| 105 |
double value; |
| 106 |
memcpy(&value, key, sizeof(double)); |
| 107 |
return value; |
| 108 |
} |
| 109 |
#endif // end if/else (BYTE_ORDER == LITTLE_ENDIAN) |
| 110 |
|
| 111 |
private: |
| 112 |
char key[sizeof(double)+sizeof(Node*)]; |
| 113 |
}; // end class ProtoSpace::Ordinate |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* @class Iterator |
| 118 |
* |
| 119 |
* @brief This class starts at a origin point in the "space" and |
| 120 |
* iterates through the nodes from the closest node(s) |
| 121 |
* to the farthest |
| 122 |
*/ |
| 123 |
class Iterator |
| 124 |
{ |
| 125 |
public: |
| 126 |
Iterator(ProtoSpace& theSpace); |
| 127 |
~Iterator(); |
| 128 |
|
| 129 |
bool Init(const double* originOrdinates = NULL); |
| 130 |
void Destroy(); |
| 131 |
|
| 132 |
void Reset(const double* originOrdinates = NULL); |
| 133 |
|
| 134 |
Node* GetNextNode(double* distance = NULL); |
| 135 |
|
| 136 |
private: |
| 137 |
ProtoSpace& space; |
| 138 |
|
| 139 |
// This array contains the ordinates |
| 140 |
// of the "origin" |
| 141 |
double* orig; |
| 142 |
double bbox_radius; |
| 143 |
double x_factor; |
| 144 |
// The iterations define an expanding |
| 145 |
// bounding n-dimensional sub-space. |
| 146 |
ProtoSortedTree::Iterator** pos_it; |
| 147 |
ProtoSortedTree::Iterator** neg_it; |
| 148 |
ProtoSortedTree ord_tree; |
| 149 |
|
| 150 |
}; // end class Iterator() |
| 151 |
friend class Iterator; |
| 152 |
|
| 153 |
|
| 154 |
private: |
| 155 |
Ordinate* GetOrdinateFromPool() |
| 156 |
{return static_cast<Ordinate*>(ord_pool.Get());} |
| 157 |
void ReturnOrdinateToPool(Ordinate& ord) |
| 158 |
{ord_pool.Put(ord);} |
| 159 |
|
| 160 |
unsigned int num_dimensions; |
| 161 |
ProtoSortedTree* ord_tree; |
| 162 |
ProtoSortedTree::ItemPool ord_pool; |
| 163 |
unsigned node_count; |
| 164 |
|
| 165 |
}; // end class ProtoSpace |
| 166 |
|
| 167 |
|
| 168 |
#endif // _PROTO_SPACE |