[162] | 1 | #include "SceneGraph.h" |
---|
| 2 | #include "Exporter.h" |
---|
| 3 | #include "UnigraphicsParser.h" |
---|
[170] | 4 | #include "X3dParser.h" |
---|
[65] | 5 | #include "Preprocessor.h" |
---|
[235] | 6 | #include "ViewCell.h" |
---|
| 7 | #include "Environment.h" |
---|
[65] | 8 | |
---|
[262] | 9 | Preprocessor::Preprocessor(): |
---|
| 10 | mKdTree(NULL), |
---|
[297] | 11 | mBspTree(NULL), |
---|
[309] | 12 | mViewCellsType(BSP_VIEW_CELLS) |
---|
[262] | 13 | { |
---|
| 14 | } |
---|
[261] | 15 | |
---|
| 16 | Preprocessor::~Preprocessor() |
---|
[308] | 17 | {
|
---|
| 18 | DeleteViewCells();
|
---|
| 19 | |
---|
| 20 | DEL_PTR(mBspTree); |
---|
| 21 | DEL_PTR(mKdTree); |
---|
[261] | 22 | } |
---|
| 23 | |
---|
[162] | 24 | bool |
---|
[261] | 25 | Preprocessor::LoadViewCells(const string filename) |
---|
[162] | 26 | { |
---|
[312] | 27 | X3dParser parser; |
---|
[313] | 28 | int maxViewCells = 0; |
---|
[312] | 29 | environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight); |
---|
[313] | 30 | environment->GetIntValue("ViewCells.maxViewCells", maxViewCells); |
---|
| 31 | |
---|
| 32 | bool loaded = parser.ParseFile(filename, mViewCells); |
---|
| 33 | |
---|
| 34 | if (maxViewCells > 0) |
---|
| 35 | { |
---|
| 36 | while (mViewCells.size() > maxViewCells) |
---|
| 37 | { |
---|
| 38 | ViewCell *vc = mViewCells.back(); |
---|
| 39 | DEL_PTR(vc); |
---|
| 40 | mViewCells.pop_back(); |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | return loaded; |
---|
[162] | 44 | } |
---|
[65] | 45 | |
---|
[310] | 46 | bool Preprocessor::ParseViewCellsOptions() |
---|
| 47 | { |
---|
| 48 | // parse type of view cells |
---|
| 49 | char viewCellsStr[64]; |
---|
| 50 | environment->GetStringValue("ViewCells.hierarchyType", viewCellsStr); |
---|
| 51 | |
---|
| 52 | int vcType = BSP_VIEW_CELLS; |
---|
| 53 | |
---|
| 54 | if (strcmp(viewCellsStr, "bspTree") == 0) |
---|
| 55 | vcType = BSP_VIEW_CELLS; |
---|
| 56 | else if (strcmp(viewCellsStr, "kdTree") == 0) |
---|
| 57 | vcType = KD_VIEW_CELLS; |
---|
| 58 | else if (strcmp(viewCellsStr, "sceneDependent") == 0) |
---|
| 59 | vcType = SCENE_DEPENDENT; |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | cerr<<"Wrong view cells type" << viewCellsStr << endl; |
---|
| 63 | exit(1); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // decide about view cell subdivision type used for preprocessing |
---|
| 67 | switch (vcType) |
---|
| 68 | { |
---|
| 69 | case BSP_VIEW_CELLS: |
---|
| 70 | case KD_VIEW_CELLS: |
---|
| 71 | mViewCellsType = vcType; |
---|
| 72 | break; |
---|
| 73 | case SCENE_DEPENDENT: |
---|
| 74 | mViewCellsType = BSP_VIEW_CELLS; // TODO |
---|
| 75 | break; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | return true; |
---|
| 79 | } |
---|
| 80 | |
---|
[308] | 81 | void Preprocessor::DeleteViewCells() |
---|
| 82 | { |
---|
| 83 | for (int i = 0; i < mViewCells.size(); ++ i) |
---|
| 84 | delete mViewCells[i]->GetMesh(); |
---|
| 85 | |
---|
| 86 | CLEAR_CONTAINER(mViewCells); |
---|
| 87 | } |
---|
| 88 | |
---|
[162] | 89 | bool |
---|
[261] | 90 | Preprocessor::GenerateViewCells() |
---|
[162] | 91 | { |
---|
[263] | 92 | // TODO |
---|
| 93 | // HACK: derive view cells from the scene objects |
---|
| 94 | ObjectContainer objects; |
---|
| 95 | |
---|
| 96 | int maxViewCells = 0; |
---|
[313] | 97 | environment->GetIntValue("ViewCells.maxViewCells", maxViewCells); |
---|
[263] | 98 | |
---|
| 99 | mSceneGraph->CollectObjects(&objects); |
---|
| 100 | ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells); |
---|
| 101 | |
---|
| 102 | return true; |
---|
[162] | 103 | } |
---|
[65] | 104 | |
---|
[162] | 105 | bool |
---|
| 106 | Preprocessor::LoadScene(const string filename) |
---|
| 107 | { |
---|
| 108 | // use leaf nodes of the original spatial hiearrchy as occludees |
---|
| 109 | |
---|
| 110 | mSceneGraph = new SceneGraph; |
---|
[170] | 111 | |
---|
| 112 | |
---|
| 113 | Parser *parser; |
---|
| 114 | |
---|
| 115 | if (strstr(filename.c_str(), ".x3d")) |
---|
| 116 | parser = new X3dParser; |
---|
| 117 | else |
---|
| 118 | parser = new UnigraphicsParser; |
---|
| 119 | |
---|
[162] | 120 | bool result = parser->ParseFile(filename, &mSceneGraph->mRoot); |
---|
[65] | 121 | |
---|
[261] | 122 | delete parser; |
---|
| 123 | |
---|
[162] | 124 | return result; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | bool |
---|
| 128 | Preprocessor::ExportPreprocessedData(const string filename) |
---|
| 129 | { |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | bool |
---|
| 134 | Preprocessor::BuildKdTree() |
---|
| 135 | { |
---|
| 136 | mKdTree = new KdTree; |
---|
| 137 | // add mesh instances of the scene graph to the root of the tree |
---|
| 138 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot(); |
---|
[176] | 139 | mSceneGraph->CollectObjects(&root->mObjects); |
---|
[65] | 140 | |
---|
[162] | 141 | mKdTree->Construct(); |
---|
| 142 | return true; |
---|
| 143 | } |
---|
[65] | 144 | |
---|
[263] | 145 | |
---|
[234] | 146 | bool |
---|
| 147 | Preprocessor::BuildBspTree() |
---|
| 148 | { |
---|
[262] | 149 | DEL_PTR(mBspTree); |
---|
[321] | 150 | |
---|
| 151 | mBspTree = new BspTree(&mUnbounded); |
---|
[235] | 152 | |
---|
[262] | 153 | ObjectContainer objects; |
---|
[327] | 154 | RayContainer *rays = new RayContainer(); |
---|
[308] | 155 | |
---|
[263] | 156 | switch (BspTree::sConstructionMethod) |
---|
[235] | 157 | { |
---|
[310] | 158 | case BspTree::FROM_INPUT_VIEW_CELLS: |
---|
[261] | 159 | mBspTree->Construct(mViewCells); |
---|
[235] | 160 | break; |
---|
[310] | 161 | case BspTree::FROM_SCENE_GEOMETRY: |
---|
[308] | 162 | DeleteViewCells(); // we generate new view cells |
---|
[263] | 163 | mSceneGraph->CollectObjects(&objects); |
---|
[261] | 164 | mBspTree->Construct(objects, &mViewCells); |
---|
[235] | 165 | break; |
---|
[310] | 166 | case BspTree::FROM_RAYS: |
---|
[319] | 167 | DeleteViewCells(); // we generate new view cells |
---|
| 168 | mSceneGraph->CollectObjects(&objects); |
---|
[327] | 169 | mBspTree->Construct(rays, &mViewCells); |
---|
[261] | 170 | break; |
---|
[235] | 171 | default: |
---|
[262] | 172 | Debug << "Error: Method not available\n"; |
---|
[235] | 173 | break; |
---|
| 174 | } |
---|
| 175 | return true; |
---|
[234] | 176 | } |
---|
[162] | 177 | |
---|
[234] | 178 | |
---|
[310] | 179 | |
---|
[162] | 180 | void |
---|
| 181 | Preprocessor::KdTreeStatistics(ostream &s) |
---|
[65] | 182 | { |
---|
[162] | 183 | s<<mKdTree->GetStatistics(); |
---|
[65] | 184 | } |
---|
[162] | 185 | |
---|
[234] | 186 | void |
---|
| 187 | Preprocessor::BspTreeStatistics(ostream &s) |
---|
| 188 | { |
---|
[235] | 189 | s << mBspTree->GetStatistics(); |
---|
[234] | 190 | } |
---|
[162] | 191 | |
---|
| 192 | bool |
---|
| 193 | Preprocessor::Export( const string filename, |
---|
| 194 | const bool scene, |
---|
[242] | 195 | const bool kdtree, |
---|
| 196 | const bool bsptree |
---|
[162] | 197 | ) |
---|
| 198 | { |
---|
| 199 | Exporter *exporter = Exporter::GetExporter(filename); |
---|
| 200 | |
---|
| 201 | if (exporter) { |
---|
| 202 | if (scene) |
---|
| 203 | exporter->ExportScene(mSceneGraph->mRoot); |
---|
| 204 | |
---|
| 205 | if (kdtree) { |
---|
| 206 | exporter->SetWireframe(); |
---|
| 207 | exporter->ExportKdTree(*mKdTree); |
---|
| 208 | } |
---|
| 209 | |
---|
[242] | 210 | if (bsptree) { |
---|
[264] | 211 | //exporter->SetWireframe(); |
---|
[242] | 212 | exporter->ExportBspTree(*mBspTree); |
---|
| 213 | } |
---|
| 214 | |
---|
[162] | 215 | delete exporter; |
---|
| 216 | return true; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | return false; |
---|
[310] | 220 | } |
---|