#include "ViewCellsTree.h" #include "Timer/PerfTimer.h" #include #include using namespace std; // MAGIC of all bin exports #ifndef MAGIC #define MAGIC 0x827923 #endif #define VIEWCELLS_VERSION 1.0f namespace CHCDemoEngine { ViewCell *ViewCellsTree::GetViewCell(const Vector3 &point) const { ViewCellsTreeNode *node = mRoot; while (!node->IsLeaf()) { //cout << "pt: " << point[node->mAxis] << " " << node->mPosition << endl; if (point[node->mAxis] < node->mPosition) node = node->mBack; else node = node->mFront; } //cout << "vc: " << node->mViewCell->GetBox() << " pvs " << node->mViewCell->mPvs.Size() << endl; return node->mViewCell; } bool ViewCellsTree::LoadFromFile(const std::string &filename, float scaleFactor) { cout << "Loading view cells from file '" + filename + "'"; FILE *fr = fopen(filename.c_str(), "rb"); if (!fr) { cerr << "Error: Cannot open file for reading" << endl; return false; } bool result = _LoadFromFile(fr, scaleFactor); cout << "finished loading view cells. " << endl; fclose(fr); return result; } bool ViewCellsTree::_LoadFromFile(FILE *fr, float scaleFactor) { int buffer[256]; fread(buffer, sizeof(int), 2, fr); if (buffer[0] != MAGIC) { cerr << "Error: Wrong file type" << endl; return false; } if (buffer[1] != (int)(1000 * VIEWCELLS_VERSION)) { cerr << "Error: Wrong viewcells version" << endl; return false; } AxisAlignedBox3 box; // get the bounding box // exchange x and y coordinates and recompute box fread(&box, sizeof(AxisAlignedBox3), 1, fr); box.Scale(scaleFactor); Vector3 v1(box.Min().x, -box.Min().z, box.Min().y); Vector3 v2(box.Max().x, -box.Max().z, box.Max().y); mBox.Initialize(); mBox.Include(v1); mBox.Include(v2); stack nodeStack; nodeStack.push(&mRoot); cout << "view cells bounds " << mBox << endl; int numViewCells = 0; int axes[] = {0, 0, 0}; while (!nodeStack.empty()) { ViewCellsTreeNode *&node = *nodeStack.top(); nodeStack.pop(); node = new ViewCellsTreeNode(); ++ numViewCells; int axis; fread(&axis, sizeof(int), 1, fr); // exchange y and z axis like for whole scene if (axis == 2) axis = 1; else if (axis == 1) axis = 2; node->mAxis = axis; if (!node->IsLeaf()) { ++ axes[node->mAxis]; float mypos; fread(&mypos, sizeof(float), 1, fr); mypos *= scaleFactor; // changed coordinate system must be considered also here if (node->mAxis == 1) { node->mPosition = -mypos; nodeStack.push(&node->mBack); nodeStack.push(&node->mFront); //cout << mypos << " "; } else { node->mPosition = mypos; nodeStack.push(&node->mFront); nodeStack.push(&node->mBack); } } } return true; } }