[372] | 1 | #include "SceneGraph.h"
|
---|
| 2 | #include "Exporter.h"
|
---|
| 3 | #include "UnigraphicsParser.h"
|
---|
| 4 | #include "X3dParser.h"
|
---|
| 5 | #include "Preprocessor.h"
|
---|
| 6 | #include "ViewCell.h"
|
---|
| 7 | #include "Environment.h"
|
---|
[409] | 8 | #include "RenderSimulator.h"
|
---|
[372] | 9 |
|
---|
| 10 | Preprocessor::Preprocessor():
|
---|
| 11 | mKdTree(NULL),
|
---|
[409] | 12 | mBspTree(NULL),
|
---|
| 13 | mRenderSimulator(NULL)
|
---|
[308] | 14 | {
|
---|
[372] | 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | Preprocessor::~Preprocessor()
|
---|
| 19 | {
|
---|
[308] | 20 | DeleteViewCells();
|
---|
[372] | 21 |
|
---|
| 22 | DEL_PTR(mBspTree);
|
---|
| 23 | DEL_PTR(mKdTree);
|
---|
[409] | 24 |
|
---|
| 25 | DEL_PTR(mRenderSimulator);
|
---|
[372] | 26 | }
|
---|
| 27 |
|
---|
| 28 | bool
|
---|
| 29 | Preprocessor::LoadViewCells(const string filename)
|
---|
| 30 | {
|
---|
| 31 | X3dParser parser;
|
---|
| 32 | int maxViewCells = 0;
|
---|
| 33 | environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
|
---|
| 34 | environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
|
---|
| 35 |
|
---|
| 36 | bool loaded = parser.ParseFile(filename, mViewCells);
|
---|
| 37 |
|
---|
| 38 | if (maxViewCells > 0)
|
---|
| 39 | {
|
---|
| 40 | while (mViewCells.size() > maxViewCells)
|
---|
| 41 | {
|
---|
| 42 | ViewCell *vc = mViewCells.back();
|
---|
| 43 | DEL_PTR(vc);
|
---|
| 44 | mViewCells.pop_back();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | return loaded;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | bool
|
---|
| 51 | Preprocessor::ParseViewCellsOptions()
|
---|
| 52 | {
|
---|
| 53 | // parse type of view cells
|
---|
| 54 | char viewCellsStr[64];
|
---|
| 55 | environment->GetStringValue("ViewCells.hierarchy", viewCellsStr);
|
---|
| 56 |
|
---|
| 57 | if (strcmp(viewCellsStr, "bspTree") == 0)
|
---|
[409] | 58 | {
|
---|
[372] | 59 | ViewCell::sHierarchy = ViewCell::BSP;
|
---|
[409] | 60 | }
|
---|
[372] | 61 | else if (strcmp(viewCellsStr, "kdTree") == 0)
|
---|
[409] | 62 | {
|
---|
[372] | 63 | ViewCell::sHierarchy = ViewCell::KD;
|
---|
[409] | 64 | }
|
---|
[372] | 65 | else if (strcmp(viewCellsStr, "sceneDependent") == 0)
|
---|
| 66 | {
|
---|
| 67 | //TODO
|
---|
| 68 | }
|
---|
| 69 | else
|
---|
| 70 | {
|
---|
| 71 | cerr<<"Wrong view cells type" << viewCellsStr << endl;
|
---|
| 72 | exit(1);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[409] | 78 | RenderSimulator *Preprocessor::GetRenderSimulator()
|
---|
| 79 | {
|
---|
| 80 | if (mRenderSimulator)
|
---|
| 81 | return mRenderSimulator;
|
---|
| 82 |
|
---|
| 83 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
| 84 |
|
---|
| 85 | environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
| 86 | environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
| 87 | environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
| 88 |
|
---|
[410] | 89 | Debug << "render simulator using render cost=" << objRenderCost << ", vc overhead=" << vcOverhead << ", move speed=" << moveSpeed << endl;
|
---|
[409] | 90 | if (ViewCell::sHierarchy = ViewCell::BSP)
|
---|
| 91 | mRenderSimulator = new BspViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mBspTree);
|
---|
| 92 | else // KD view cells
|
---|
| 93 | mRenderSimulator = new KdViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mKdTree);
|
---|
| 94 |
|
---|
| 95 | return mRenderSimulator;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[372] | 98 | void Preprocessor::DeleteViewCells()
|
---|
| 99 | {
|
---|
[409] | 100 | for (int i = 0; i < (int)mViewCells.size(); ++ i)
|
---|
| 101 | {
|
---|
| 102 | Mesh *mesh = mViewCells[i]->GetMesh();
|
---|
| 103 | DEL_PTR(mesh);
|
---|
| 104 | }
|
---|
[372] | 105 | CLEAR_CONTAINER(mViewCells);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | bool
|
---|
| 109 | Preprocessor::GenerateViewCells()
|
---|
| 110 | {
|
---|
| 111 | // TODO
|
---|
| 112 | // HACK: derive view cells from the scene objects
|
---|
| 113 | ObjectContainer objects;
|
---|
| 114 |
|
---|
| 115 | int maxViewCells = 0;
|
---|
| 116 | environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
|
---|
| 117 |
|
---|
| 118 | mSceneGraph->CollectObjects(&objects);
|
---|
| 119 | ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells);
|
---|
| 120 |
|
---|
| 121 | return true;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[387] | 124 | int
|
---|
| 125 | SplitFilenames(const string str,
|
---|
| 126 | vector<string> &filenames)
|
---|
| 127 | {
|
---|
| 128 | int pos = 0;
|
---|
| 129 |
|
---|
| 130 | while(1) {
|
---|
| 131 | int npos = str.find(';', pos);
|
---|
| 132 |
|
---|
| 133 | if (npos < 0 || npos - pos < 1)
|
---|
| 134 | break;
|
---|
| 135 | filenames.push_back(string(str, pos, npos - pos));
|
---|
| 136 | pos = npos + 1;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
| 140 | return filenames.size();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[372] | 143 | bool
|
---|
| 144 | Preprocessor::LoadScene(const string filename)
|
---|
| 145 | {
|
---|
| 146 | // use leaf nodes of the original spatial hiearrchy as occludees
|
---|
| 147 |
|
---|
| 148 | mSceneGraph = new SceneGraph;
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | Parser *parser;
|
---|
[387] | 152 | vector<string> filenames;
|
---|
| 153 | int files = SplitFilenames(filename, filenames);
|
---|
| 154 | cout<<files<<endl;
|
---|
| 155 | bool result = false;
|
---|
| 156 | if (files == 1) {
|
---|
| 157 |
|
---|
| 158 | if (strstr(filename.c_str(), ".x3d"))
|
---|
| 159 | parser = new X3dParser;
|
---|
| 160 | else
|
---|
| 161 | parser = new UnigraphicsParser;
|
---|
[372] | 162 |
|
---|
[387] | 163 | cout<<filename<<endl;
|
---|
| 164 | result = parser->ParseFile(filename, &mSceneGraph->mRoot);
|
---|
[372] | 165 |
|
---|
[387] | 166 | delete parser;
|
---|
[372] | 167 |
|
---|
[387] | 168 | } else {
|
---|
| 169 | // root for different files
|
---|
| 170 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
| 171 | for (int i= 0; i < filenames.size(); i++) {
|
---|
| 172 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
| 173 | parser = new X3dParser;
|
---|
| 174 | else
|
---|
| 175 | parser = new UnigraphicsParser;
|
---|
| 176 |
|
---|
| 177 | SceneGraphNode *node;
|
---|
| 178 | if (parser->ParseFile(filenames[i], &node)) {
|
---|
| 179 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
| 180 | // at least one file parsed
|
---|
| 181 | result = true;
|
---|
| 182 | }
|
---|
| 183 | delete parser;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
[372] | 186 |
|
---|
| 187 |
|
---|
[387] | 188 | if (result) {
|
---|
[372] | 189 | mSceneGraph->AssignObjectIds();
|
---|
[387] | 190 | int intersectables, faces;
|
---|
| 191 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
| 192 | cout<<filename<<" parsed successfully."<<endl;
|
---|
| 193 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
| 194 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[372] | 197 |
|
---|
| 198 | return result;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | bool
|
---|
| 202 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
| 203 | {
|
---|
| 204 | return false;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | bool
|
---|
| 208 | Preprocessor::BuildKdTree()
|
---|
| 209 | {
|
---|
| 210 | mKdTree = new KdTree;
|
---|
| 211 | // add mesh instances of the scene graph to the root of the tree
|
---|
| 212 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
| 213 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
| 214 |
|
---|
| 215 | mKdTree->Construct();
|
---|
| 216 | return true;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | void
|
---|
| 220 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
| 221 | {
|
---|
| 222 | s<<mKdTree->GetStatistics();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | void
|
---|
| 226 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
| 227 | {
|
---|
| 228 | s << mBspTree->GetStatistics();
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | bool
|
---|
| 232 | Preprocessor::Export( const string filename,
|
---|
| 233 | const bool scene,
|
---|
| 234 | const bool kdtree,
|
---|
| 235 | const bool bsptree
|
---|
| 236 | )
|
---|
| 237 | {
|
---|
| 238 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 239 |
|
---|
| 240 | if (exporter) {
|
---|
| 241 | if (scene)
|
---|
| 242 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 243 |
|
---|
| 244 | if (kdtree) {
|
---|
| 245 | exporter->SetWireframe();
|
---|
| 246 | exporter->ExportKdTree(*mKdTree);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if (bsptree) {
|
---|
| 250 | //exporter->SetWireframe();
|
---|
| 251 | exporter->ExportBspTree(*mBspTree);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | delete exporter;
|
---|
| 255 | return true;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | return false;
|
---|
| 259 | }
|
---|