1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <octree>
#include <iostream>
int main()
{
// Construct a small 2-d binary tree.
double center[2] = { 0.5, 0.5 };
octree<int,2> foo( center, 1. );
foo.root()->add_children();
foo.root()->value() = 42;
*(*foo.root())[0] = 25;
*(*foo.root())[1] = 19;
(*foo.root())[1].add_children();
(*foo.root())[3].add_children();
*(*foo.root())[1][0] = 38;
*(*foo.root())[1][1] = 5;
*(*foo.root())[1][2] = -19;
*(*foo.root())[1][3] = 1;
*(*foo.root())[2] = 8;
*(*foo.root())[3] = 3;
*(*foo.root())[3][0] = 15;
(*foo.root())[3].remove_children();
cout << "Root is " << foo.root()->value() << "\n";
cout << "Child 0 is " << (*foo.root())[0].value() << "\n";
cout << "Child 1 is " << (*foo.root())[1].value() << "\n";
// Now test an iterator
octree<int,2>::iterator it;
for ( it = foo.begin(); it != foo.end(); ++it )
{
/*
const double* bds = it->center();
double he = it->size() / 2.;
*/
cout
<< "Node 0x" << hex << (&*it)
<< " (" << (it.level()) << ") "
<< " = " << it->value()
/*
<< " [" << (bds[0] - he) << "->" << (bds[0] + he)
<< ", " << (bds[1] - he) << "->" << (bds[1] + he) << "]"
*/
<< "\n";
}
cout << "\n\n";
for ( it = foo.begin( false ); it != foo.end( false ); ++it )
{
/*
const double* bds = it->center();
double he = it->size() / 2.;
*/
cout
<< "Node 0x" << hex << (&*it)
<< " (" << (it.level()) << ") "
<< " = " << it->value()
/*
<< " [" << (bds[0] - he) << "->" << (bds[0] + he)
<< ", " << (bds[1] - he) << "->" << (bds[1] + he) << "]"
*/
<< "\n";
}
cout << "\n\n";
it = foo.end();
do
{
--it;
cout
<< "Node 0x" << hex << (&*it)
<< " (" << (it.level()) << ") "
<< " = " << it->value() << "\n";
}
while ( it != foo.begin() );
cout << "\n\n";
it = foo.end( false );
do
{
--it;
cout
<< "Node 0x" << hex << (&*it)
<< " (" << (it.level()) << ") "
<< " = " << it->value() << "\n";
}
while ( it != foo.begin( false ) );
cout << "\n\n";
// Now test the "immediate family" mode.
it = foo.begin();
++it;
it.immediate_family( true );
for ( ; it != foo.end(); ++it )
{
/*
const double* bds = it->center();
double he = it->size() / 2.;
*/
cout
<< "Node 0x" << hex << (&*it)
<< " (" << (it.level()) << ") "
<< " = " << it->value()
/*
<< " [" << (bds[0] - he) << "->" << (bds[0] + he)
<< ", " << (bds[1] - he) << "->" << (bds[1] + he) << "]"
*/
<< "\n";
}
cout << "\n\n";
it = foo.begin( false );
++it;
it.immediate_family( true );
for ( ; it != foo.end( false ); ++it )
{
/*
const double* bds = it->center();
double he = it->size() / 2.;
*/
cout
<< "Node 0x" << hex << (&*it)
<< " (" << (it.level()) << ") "
<< " = " << it->value()
/*
<< " [" << (bds[0] - he) << "->" << (bds[0] + he)
<< ", " << (bds[1] - he) << "->" << (bds[1] + he) << "]"
*/
<< "\n";
}
cout << "\n\n";
// Test octree cursors:
octree<int,2>::cursor curs( &foo );
curs.down( 0 );
curs.over( 1 );
cout << "Initial L2Node: " << "level " << curs.level() << " where " << curs.where() << " val " << curs->value() << "\n";
curs.axis_partner( 1 );
cout << "Axis 1 partner: " << "level " << curs.level() << " where " << curs.where() << " val " << curs->value() << "\n";
curs.over( 1 );
curs.axis_partner( 0 );
cout << "Axis 0 partner: " << "level " << curs.level() << " where " << curs.where() << " val " << curs->value() << "\n";
curs.over( 1 );
curs.down( 3 );
cout << "Down to level2: " << "level " << curs.level() << " where " << curs.where() << " val " << curs->value() << "\n";
// Copy an iterator's position
curs = foo.begin();
cout << "level " << curs.level() << " where " << curs.where() << " val " << curs->value() << "\n";
return 0;
}
|