Changeset 1196 for GTP/trunk/Lib/Vis/Preprocessing
- Timestamp:
- 08/10/06 17:46:27 (18 years ago)
- Location:
- GTP/trunk/Lib/Vis/Preprocessing/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
r1194 r1196 1125 1125 1126 1126 1127 #define INTERIOR_START_ID-21128 #define LEAF_START_ID-31129 #define LEAF_END_ID -41127 #define TYPE_INTERIOR -2 1128 #define TYPE_LEAF -3 1129 //#define LEAF_END_ID -4 1130 1130 1131 1131 … … 1134 1134 ObjectContainer::const_iterator it, it_end = leaf->mObjects.end(); 1135 1135 1136 int leafStartId = LEAF_START_ID; 1137 stream.write(reinterpret_cast<char *>(&leafStartId), sizeof(int)); 1136 int type = TYPE_LEAF; 1137 int size = (int)leaf->mObjects.size(); 1138 1139 stream.write(reinterpret_cast<char *>(&type), sizeof(int)); 1140 stream.write(reinterpret_cast<char *>(&size), sizeof(int)); 1141 1138 1142 for (it = leaf->mObjects.begin(); it != it_end; ++ it) 1139 1143 { 1140 1144 Intersectable *obj = *it; 1141 1142 1145 int id = obj->mId; 1143 1146 1144 1147 //stream.write(reinterpret_cast<char *>(&origin), sizeof(Vector3)); 1145 1148 stream.write(reinterpret_cast<char *>(&id), sizeof(int)); … … 1147 1150 1148 1151 // end leaf 1149 int leafEndId = LEAF_END_ID; 1150 stream.write(reinterpret_cast<char *>(&leafEndId), sizeof(int)); 1151 } 1152 1153 1154 void KdTree::ImportBinLeaf(ifstream &stream, KdLeaf *leaf) 1155 { 1156 ObjectContainer::const_iterator it, it_end = leaf->mObjects.end(); 1157 1158 int leafId = LEAF_START_ID; 1152 //int leafEndId = LEAF_END_ID; stream.write(reinterpret_cast<char *>(&leafEndId), sizeof(int)); 1153 } 1154 1155 1156 KdLeaf *KdTree::ImportBinLeaf(ifstream &stream, KdInterior *parent) 1157 { 1158 int leafId = TYPE_LEAF; 1159 1159 int objId = leafId; 1160 1161 while (objId != LEAF_END_ID) 1160 int size; 1161 1162 stream.read(reinterpret_cast<char *>(&size), sizeof(int)); 1163 1164 KdLeaf *leaf = new KdLeaf(parent, size); 1165 1166 Debug << "objects size: " << size << endl; 1167 // read object ids 1168 for (int i = 0; i < size; ++ i) 1162 1169 { 1163 1170 stream.read(reinterpret_cast<char *>(&objId), sizeof(int)); 1164 1171 //if (samplesIn.eof()) break; 1165 1172 } 1173 1174 return leaf; 1166 1175 } 1167 1176 … … 1169 1178 void KdTree::ExportBinInterior(ofstream &stream, KdInterior *interior) 1170 1179 { 1171 int interiorid = INTERIOR_START_ID;1180 int interiorid = TYPE_INTERIOR; 1172 1181 stream.write(reinterpret_cast<char *>(&interiorid), sizeof(int)); 1173 1182 1174 1183 int axis = interior->mAxis; 1175 Vector3pos = interior->mPosition;1184 float pos = interior->mPosition; 1176 1185 1177 1186 stream.write(reinterpret_cast<char *>(&axis), sizeof(int)); 1178 stream.write(reinterpret_cast<char *>(&pos), sizeof(Vector3)); 1179 } 1180 1181 1182 void KdTree::ImportBinInterior(ifstream &stream, KdInterior *interior) 1183 { 1184 int interiorid = -2; 1185 stream.read(reinterpret_cast<char *>(&interiorid), sizeof(int)); 1187 stream.write(reinterpret_cast<char *>(&pos), sizeof(float)); 1188 } 1189 1190 1191 KdInterior *KdTree::ImportBinInterior(ifstream &stream, KdInterior *parent) 1192 { 1193 KdInterior *interior = new KdInterior(parent); 1186 1194 1187 1195 int axis = interior->mAxis; 1188 Vector3pos = interior->mPosition;1196 float pos = interior->mPosition; 1189 1197 1190 1198 stream.read(reinterpret_cast<char *>(&axis), sizeof(int)); 1191 stream.read(reinterpret_cast<char *>(&pos), sizeof(Vector3)); 1199 stream.read(reinterpret_cast<char *>(&pos), sizeof(float)); 1200 1201 interior->mAxis = axis; 1202 interior->mPosition = pos; 1203 1204 return interior; 1192 1205 } 1193 1206 … … 1202 1215 // export binary version of mesh 1203 1216 stack<KdNode *> tStack; 1204 1205 1217 tStack.push(mRoot); 1206 1218 … … 1208 1220 { 1209 1221 KdNode *node = tStack.top(); 1210 1222 tStack.pop(); 1223 1211 1224 if (node->IsLeaf()) 1212 1225 { 1226 Debug << "l"; 1213 1227 ExportBinLeaf(stream, dynamic_cast<KdLeaf *>(node)); 1214 1228 } 1215 1229 else 1216 1230 { 1231 Debug << "i"; 1217 1232 KdInterior *interior = dynamic_cast<KdInterior *>(node); 1218 1233 1219 1234 ExportBinInterior(stream, interior); 1220 1235 1236 tStack.push(interior->mBack); 1221 1237 tStack.push(interior->mFront); 1238 } 1239 1240 if (tStack.empty()) 1241 break; 1242 1243 node = tStack.top(); 1244 tStack.pop(); 1245 1246 if (node->IsLeaf()) 1247 { 1248 Debug << "l"; 1249 ExportBinLeaf(stream, dynamic_cast<KdLeaf *>(node)); 1250 } 1251 else 1252 { 1253 Debug << "i"; 1254 KdInterior *interior = dynamic_cast<KdInterior *>(node); 1255 1256 ExportBinInterior(stream, interior); 1257 1222 1258 tStack.push(interior->mBack); 1259 tStack.push(interior->mFront); 1223 1260 } 1224 1261 } … … 1228 1265 1229 1266 1230 bool KdTree::LoadBinTree(const string &filename) 1231 { 1232 ofstream stream(filename.c_str(), ios::binary); 1233 1267 KdNode *KdTree::LoadNextNode(ifstream &stream, KdInterior *parent) 1268 { 1269 int nodeType; 1270 stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int)); 1271 1272 if (nodeType == TYPE_LEAF) 1273 { 1274 return ImportBinLeaf(stream, dynamic_cast<KdInterior *>(parent)); 1275 } 1276 1277 if (nodeType == TYPE_INTERIOR) 1278 { 1279 return ImportBinInterior(stream, dynamic_cast<KdInterior *>(parent)); 1280 } 1281 1282 Debug << "error! loading failed!" << endl; 1283 1284 return NULL; 1285 } 1286 1287 1288 bool KdTree::LoadBinTree(const string &filename, const ObjectContainer &objects) 1289 { 1290 // export binary version of mesh 1291 stack<TraversalData> tStack; 1292 ifstream stream(filename.c_str(), ios::binary); 1293 Debug << "here94" << endl; 1234 1294 if (!stream.is_open()) 1235 1295 return false; 1236 1296 1237 // export binary version of mesh 1238 stack<KdNode *> tStack; 1239 1240 tStack.push(mRoot); 1297 mBox.Initialize(); 1298 1299 ObjectContainer::const_iterator oit, oit_end = objects.end(); 1300 1301 //-- compute bounding box 1302 for (oit = objects.begin(); oit != oit_end; ++ oit) 1303 { 1304 Intersectable *obj = *oit; 1305 1306 // compute bounding box of view space 1307 mBox.Include(obj->GetBox()); 1308 } 1309 1310 DEL_PTR(mRoot); // we make a new root 1311 Debug << "here44" << endl; 1312 KdNode *node = LoadNextNode(stream, NULL); 1313 mRoot = node; 1314 1315 tStack.push(TraversalData(node, mBox, 0)); 1316 mStat.Reset(); 1317 mStat.nodes = 1; 1241 1318 1242 1319 while(!tStack.empty()) 1243 1320 { 1244 KdNode *node = tStack.top(); 1245 1246 if (node->IsLeaf()) 1321 TraversalData tData = tStack.top(); 1322 tStack.pop(); 1323 1324 KdNode *node = tData.mNode; 1325 1326 if (!node->IsLeaf()) 1247 1327 { 1248 ExportBinLeaf(stream, dynamic_cast<KdLeaf *>(node)); 1328 mStat.nodes += 2; 1329 1330 Debug << "i" ; 1331 KdInterior *interior = dynamic_cast<KdInterior *>(node); 1332 interior->mBox = tData.mBox; 1333 1334 KdNode *front = LoadNextNode(stream, interior); 1335 KdNode *back = LoadNextNode(stream, interior); 1336 1337 interior->SetupChildLinks(back, front); 1338 1339 ++ mStat.splits[interior->mAxis]; 1340 1341 Debug << "plane: " << interior->mAxis << " " << interior->mPosition << endl; 1342 Debug << "box: " << tData.mBox << endl; 1343 1344 // compute new bounding box 1345 AxisAlignedBox3 frontBox, backBox; 1346 1347 tData.mBox.Split(interior->mAxis, 1348 interior->mPosition, 1349 frontBox, 1350 backBox); 1351 1352 tStack.push(TraversalData(back, backBox, tData.mDepth + 1)); 1353 tStack.push(TraversalData(front, frontBox, tData.mDepth + 1)); 1249 1354 } 1250 1355 else 1251 1356 { 1252 KdInterior *interior = dynamic_cast<KdInterior *>(node); 1253 1254 ExportBinInterior(stream, interior); 1255 1256 tStack.push(interior->mFront); 1257 tStack.push(interior->mBack); 1357 EvaluateLeafStats(tData); 1358 //dynamic_cast<KdLeaf *>(node)->mBox = tData.mBox; 1359 Debug << "l" << endl; 1258 1360 } 1259 1361 } 1260 1362 1363 Debug << "\nhere85" << endl; 1364 Debug << mStat << endl; 1365 1261 1366 return true; 1262 1367 } -
GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
r1194 r1196 392 392 393 393 bool ExportBinTree(const string &filename); 394 bool LoadBinTree(const string &filename );394 bool LoadBinTree(const string &filename, const ObjectContainer &object); 395 395 396 396 protected: … … 559 559 void ExportBinLeaf(ofstream &stream, KdLeaf *leaf); 560 560 void ExportBinInterior(ofstream &stream, KdInterior *interior); 561 void ImportBinLeaf(ifstream &stream, KdLeaf *leaf); 562 void ImportBinInterior(ifstream &stream, KdInterior *interior); 561 KdLeaf *ImportBinLeaf(ifstream &stream, KdInterior *parent); 562 KdInterior *ImportBinInterior(ifstream &stream, KdInterior *parent); 563 KdNode *LoadNextNode(ifstream &stream, KdInterior *parent); 563 564 564 565 int mTermMaxNodes; -
GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
r1193 r1196 4531 4531 // compute bounding box of view space 4532 4532 mBoundingBox.Include(obj->GetBox()); 4533 mBoundingBox.Include(obj->GetBox());4534 4533 } 4535 4534 } -
GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
r1194 r1196 158 158 159 159 //-- build kd tree from scene geometry 160 161 160 preprocessor->BuildKdTree(); 162 161 preprocessor->KdTreeStatistics(cout); 163 162 preprocessor->mKdTree->ExportBinTree("kd.bin"); 163 164 KdTree *kdTree2 = new KdTree; 165 kdTree2->LoadBinTree("kd.bin", preprocessor->mObjects); 166 Exporter *exporter = Exporter::GetExporter("testkd.x3d"); 167 168 if (exporter) 169 { 170 exporter->SetWireframe(); 171 exporter->ExportKdTree(*kdTree2); 172 173 delete exporter; 174 } 175 176 DEL_PTR(kdTree2); 177 164 178 // parse view cells related options 165 179 preprocessor->PrepareViewCells(); 166 180 167 181 // p->mSceneGraph->Export("soda.x3d"); 168 if (0) { 182 if (0) 183 { 169 184 preprocessor->Export(filename + "-out.x3d", true, false, false); 170 185 preprocessor->Export(filename + "-kdtree.x3d", false, true, false);
Note: See TracChangeset
for help on using the changeset viewer.