[3242] | 1 | #include "ViewCellsTree.h"
|
---|
| 2 | #include "Timer/PerfTimer.h"
|
---|
| 3 | #include <stack>
|
---|
[3243] | 4 | #include <string>
|
---|
[3242] | 5 |
|
---|
| 6 | using namespace std;
|
---|
| 7 |
|
---|
| 8 |
|
---|
[3243] | 9 | // MAGIC of all bin exports
|
---|
| 10 | #ifndef MAGIC
|
---|
| 11 | #define MAGIC 0x827923
|
---|
| 12 | #endif
|
---|
| 13 |
|
---|
| 14 | #define VIEWCELLS_VERSION 1.0f
|
---|
| 15 |
|
---|
[3246] | 16 |
|
---|
[3242] | 17 | namespace CHCDemoEngine
|
---|
| 18 | {
|
---|
| 19 |
|
---|
[3243] | 20 |
|
---|
[3242] | 21 | ViewCell *ViewCellsTree::GetViewCell(const Vector3 &point) const
|
---|
| 22 | {
|
---|
[3243] | 23 | ViewCellsTreeNode *node = mRoot;
|
---|
[3242] | 24 |
|
---|
| 25 | while (!node->IsLeaf())
|
---|
| 26 | {
|
---|
[3244] | 27 | //cout << "pt: " << point[node->mAxis] << " " << node->mPosition << endl;
|
---|
[3243] | 28 | if (point[node->mAxis] < node->mPosition)
|
---|
| 29 | node = node->mBack;
|
---|
[3242] | 30 | else
|
---|
[3243] | 31 | node = node->mFront;
|
---|
[3242] | 32 | }
|
---|
| 33 |
|
---|
[3244] | 34 | //cout << "vc: " << node->mViewCell->GetBox() << " pvs " << node->mViewCell->mPvs.Size() << endl;
|
---|
| 35 |
|
---|
[3243] | 36 | return node->mViewCell;
|
---|
[3242] | 37 | }
|
---|
| 38 |
|
---|
| 39 |
|
---|
[3257] | 40 | bool ViewCellsTree::LoadFromFile(const std::string &filename, float scaleFactor)
|
---|
[3242] | 41 | {
|
---|
[3244] | 42 | cout << "Loading view cells from file '" + filename + "'";
|
---|
[3242] | 43 |
|
---|
| 44 | FILE *fr = fopen(filename.c_str(), "rb");
|
---|
| 45 |
|
---|
[3243] | 46 | if (!fr)
|
---|
| 47 | {
|
---|
| 48 | cerr << "Error: Cannot open file for reading" << endl;
|
---|
[3242] | 49 | return false;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[3257] | 52 | bool result = _LoadFromFile(fr, scaleFactor);
|
---|
[3242] | 53 |
|
---|
[3244] | 54 | cout << "finished loading view cells. " << endl;
|
---|
[3242] | 55 | fclose(fr);
|
---|
| 56 |
|
---|
| 57 | return result;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
[3257] | 61 | bool ViewCellsTree::_LoadFromFile(FILE *fr, float scaleFactor)
|
---|
[3242] | 62 | {
|
---|
| 63 | int buffer[256];
|
---|
[3244] | 64 | fread(buffer, sizeof(int), 2, fr);
|
---|
[3242] | 65 |
|
---|
[3243] | 66 | if (buffer[0] != MAGIC)
|
---|
| 67 | {
|
---|
| 68 | cerr << "Error: Wrong file type" << endl;
|
---|
[3242] | 69 | return false;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[3243] | 72 | if (buffer[1] != (int)(1000 * VIEWCELLS_VERSION))
|
---|
| 73 | {
|
---|
| 74 | cerr << "Error: Wrong viewcells version" << endl;
|
---|
[3242] | 75 | return false;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[3246] | 78 | AxisAlignedBox3 box;
|
---|
[3242] | 79 | // get the bounding box
|
---|
[3245] | 80 | // exchange x and y coordinates and recompute box
|
---|
[3246] | 81 | fread(&box, sizeof(AxisAlignedBox3), 1, fr);
|
---|
[3242] | 82 |
|
---|
[3257] | 83 | box.Scale(scaleFactor);
|
---|
[3246] | 84 |
|
---|
| 85 | Vector3 v1(box.Min().x, -box.Min().z, box.Min().y);
|
---|
| 86 | Vector3 v2(box.Max().x, -box.Max().z, box.Max().y);
|
---|
[3244] | 87 |
|
---|
[3245] | 88 | mBox.Initialize();
|
---|
[3244] | 89 |
|
---|
[3245] | 90 | mBox.Include(v1);
|
---|
| 91 | mBox.Include(v2);
|
---|
[3244] | 92 |
|
---|
[3242] | 93 | stack<ViewCellsTreeNode **> nodeStack;
|
---|
[3243] | 94 | nodeStack.push(&mRoot);
|
---|
[3242] | 95 |
|
---|
[3244] | 96 | cout << "view cells bounds " << mBox << endl;
|
---|
| 97 |
|
---|
| 98 | int numViewCells = 0;
|
---|
| 99 |
|
---|
[3245] | 100 | int axes[] = {0, 0, 0};
|
---|
| 101 |
|
---|
[3243] | 102 | while (!nodeStack.empty())
|
---|
| 103 | {
|
---|
[3242] | 104 | ViewCellsTreeNode *&node = *nodeStack.top();
|
---|
| 105 | nodeStack.pop();
|
---|
[3243] | 106 | node = new ViewCellsTreeNode();
|
---|
| 107 |
|
---|
[3244] | 108 | ++ numViewCells;
|
---|
[3243] | 109 |
|
---|
[3244] | 110 | int axis;
|
---|
| 111 | fread(&axis, sizeof(int), 1, fr);
|
---|
| 112 |
|
---|
[3245] | 113 | // exchange y and z axis like for whole scene
|
---|
[3244] | 114 | if (axis == 2) axis = 1;
|
---|
| 115 | else if (axis == 1) axis = 2;
|
---|
| 116 |
|
---|
| 117 | node->mAxis = axis;
|
---|
| 118 |
|
---|
[3243] | 119 | if (!node->IsLeaf())
|
---|
| 120 | {
|
---|
[3245] | 121 | ++ axes[node->mAxis];
|
---|
[3244] | 122 |
|
---|
[3245] | 123 | float mypos;
|
---|
| 124 | fread(&mypos, sizeof(float), 1, fr);
|
---|
[3246] | 125 |
|
---|
[3257] | 126 | mypos *= scaleFactor;
|
---|
[3246] | 127 |
|
---|
[3245] | 128 | // changed coordinate system must be considered also here
|
---|
| 129 | if (node->mAxis == 1)
|
---|
| 130 | {
|
---|
| 131 | node->mPosition = -mypos;
|
---|
| 132 |
|
---|
| 133 | nodeStack.push(&node->mBack);
|
---|
| 134 | nodeStack.push(&node->mFront);
|
---|
| 135 | //cout << mypos << " ";
|
---|
| 136 | }
|
---|
| 137 | else
|
---|
| 138 | {
|
---|
| 139 | node->mPosition = mypos;
|
---|
| 140 |
|
---|
| 141 | nodeStack.push(&node->mFront);
|
---|
| 142 | nodeStack.push(&node->mBack);
|
---|
| 143 | }
|
---|
[3242] | 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return true;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
[3256] | 151 |
|
---|
[3242] | 152 | } |
---|