Changeset 225 for trunk/VUT/GtpVisibilityPreprocessor/src
- Timestamp:
- 08/10/05 01:50:13 (19 years ago)
- Location:
- trunk/VUT/GtpVisibilityPreprocessor/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h
r209 r225 41 41 float *t = NULL, 42 42 bool *coplanar = NULL 43 ) const ;43 ) const {return Vector3(0,0,0);} // TODO 44 44 45 45 friend bool -
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
r224 r225 86 86 87 87 /****************************************************************/ 88 /* class Polygon implementation */ 89 /****************************************************************/ 90 91 Polygon::Polygon() 92 {} 93 94 Polygon::Polygon(const VertexContainer &vertices): mVertices(vertices) 95 {} 96 97 Polygon::Polygon(Face *face, Mesh *parent) 98 { 99 VertexIndexContainer::const_iterator it; 100 101 for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it) 102 { 103 mVertices.push_back(parent->mVertices[*it]); 104 } 105 } 106 107 Plane3 Polygon::GetSupportingPlane() 108 { 109 return Plane3(mVertices[0], mVertices[1], mVertices[2]); 110 } 111 112 void Polygon::DeletePolygons(PolygonQueue *polys) 113 { 114 // don't need to store polygon information = delete polygons 115 while(!polys->empty()) 116 { 117 Polygon *poly = polys->front(); 118 polys->pop(); 119 DEL_PTR(poly); 120 } 121 122 } 123 124 void Polygon::Split(Plane3 *partition, Polygon *front, Polygon *back) 125 { 126 Vector3 ptA = mVertices[mVertices.size() - 1];; 127 128 int sideA = partition->Side(ptA); 129 130 VertexContainer::const_iterator it; 131 132 // find line - plane intersections 133 for (it = mVertices.begin(); it != mVertices.end(); ++ it) 134 { 135 Vector3 ptB = (*it); 136 int sideB = partition->Side(ptB); 137 138 // vertices on different sides => split 139 if ((sideA != 0) && (sideB != 0) && (sideA != sideB)) 140 { 141 Vector3 v = ptB - ptA; // line from A to B 142 float dv = DotProd(partition->mNormal, v); 143 float t = 0; 144 145 if (dv) 146 { 147 t = - partition->Distance(ptA) / dv; 148 } 149 } 150 if (sideB >= 0) 151 { 152 back->mVertices.push_back(ptB); 153 } 154 else if (sideB <= 0) 155 { 156 front->mVertices.push_back(ptB); 157 } 158 159 ptA = ptB; 160 sideA = sideB; 161 } 162 } 163 164 int Polygon::Side(Plane3 *plane) 165 { 166 VertexContainer::const_iterator it; 167 168 bool onFrontSide = false; 169 bool onBackSide = false; 170 171 // find line - plane intersections 172 for (it = mVertices.begin(); it != mVertices.end(); ++ it) 173 { 174 int side = plane->Side(*it); 175 176 if (side > 0) 177 { 178 onFrontSide = true; 179 } 180 else if (side < 0) 181 { 182 onBackSide = true; 183 } 184 185 if (onFrontSide && onBackSide) // splits 186 return SPLIT; 187 } 188 189 if (onBackSide) 190 { 191 return BACK_SIDE; 192 } 193 else if (onFrontSide) 194 { 195 return FRONT_SIDE; 196 } 197 198 return COINCIDENT; 199 } 200 201 /****************************************************************/ 88 202 /* class BspTree implementation */ 89 203 /****************************************************************/ … … 128 242 } 129 243 130 delete node; 131 node = NULL; 244 DEL_PTR(node); 132 245 } 133 246 } … … 136 249 void BspTree::InsertViewCell(ViewCell *viewCell) 137 250 { 138 BspNode *currentNode = mRoot; 251 std::stack<BspTraversalData> tStack; 252 253 PolygonQueue polys; 254 // copy polygon information to guide the split process 255 CopyMesh2Polygon(viewCell->GetMesh(), polys); 256 257 tStack.push(BspTraversalData(mRoot, NULL, &polys, 0)); 258 259 while (!tStack.empty()) 260 { 261 /*BspNode *node = tStack.top(); 262 263 tStack.pop(); 264 265 if (!node->IsLeaf()) 266 { 267 BspInterior *interior = dynamic_cast<BspInterior *>(node); 268 269 // push the children on the stack (there are always two children) 270 interior->GetBack()->mParent = NULL; 271 interior->GetFront()->mParent = NULL; 272 273 tStack.push(interior->GetBack()); 274 tStack.push(interior->GetFront()); 275 } 276 277 DEL_PTR(node);*/ 278 } 279 /* BspNode *currentNode = mRoot; 139 280 140 281 std::stack<BspTraversalData> tStack; … … 170 311 } 171 312 } 172 } 313 }*/ 173 314 } 174 315 … … 177 318 } 178 319 179 void BspTree::Copy2PolygonSoup(const ObjectContainer &objects, Mesh &mesh) 320 void BspTree::CopyMesh2Polygon(Mesh *mesh, PolygonQueue &polys) 321 { 322 FaceContainer::const_iterator fi; 323 // copy the face data to polygons 324 for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi) 325 { 326 Polygon *poly = new Polygon((*fi), mesh); 327 polys.push(poly); 328 } 329 } 330 331 void BspTree::Copy2PolygonSoup(const ObjectContainer &objects, PolygonQueue &polys) 180 332 { 181 333 ObjectContainer::const_iterator it, it_end = objects.end(); … … 183 335 for (it = objects.begin(); it != it_end; ++ it) 184 336 { 185 FaceContainer::const_iterator fi;186 337 Intersectable *object = *it; 187 338 … … 191 342 MeshInstance *inst = dynamic_cast<MeshInstance *>(object); 192 343 193 Mesh *polys = inst->GetMesh(); 194 195 // copy the face data 196 for (fi = polys->mFaces.begin(); fi != polys->mFaces.end(); ++ fi) 197 { 198 Face *face = new Face((*fi)->mVertexIndices); 199 mesh.AddFace(face); 200 } 344 Mesh *mesh = inst->GetMesh(); 345 346 // copy the mesh data to polygons 347 CopyMesh2Polygon(mesh, polys); 201 348 } 202 349 } … … 206 353 { 207 354 std::stack<BspTraversalData> tStack; 208 Meshpolys;355 PolygonQueue polys; 209 356 210 357 // copy mesh instance polygons into one big polygon soup … … 220 367 tStack.pop(); 221 368 222 Mesh backPolys;223 Mesh frontPolys;369 PolygonQueue *backPolys = new PolygonQueue(); 370 PolygonQueue *frontPolys = new PolygonQueue(); 224 371 225 372 BspNode *node = SubdivideNode(dynamic_cast<BspLeaf *>(data.mNode), … … 235 382 236 383 // push the children on the stack (there are always two children) 237 tStack.push(BspTraversalData(interior->GetBack(), interior, &backPolys, data.mDepth + 1));238 tStack.push(BspTraversalData(interior->GetFront(), interior, &frontPolys, data.mDepth + 1));239 } 240 } 241 } 242 243 Plane3 BspTree::SelectPlane( Mesh *polys)384 tStack.push(BspTraversalData(interior->GetBack(), interior, backPolys, data.mDepth + 1)); 385 tStack.push(BspTraversalData(interior->GetFront(), interior, frontPolys, data.mDepth + 1)); 386 } 387 } 388 } 389 390 Plane3 BspTree::SelectPlane(PolygonQueue *polyQueue) 244 391 { 245 392 // TODO: more sophisticated criteria 246 return poly s->GetFacePlane(0);393 return polyQueue->front()->GetSupportingPlane(); 247 394 } 248 395 249 396 BspNode *BspTree::SubdivideNode(BspLeaf *leaf, BspInterior *parent, 250 Mesh*polys, const int depth,251 Mesh &frontPolys, Mesh &backPolys)397 PolygonQueue *polys, const int depth, 398 PolygonQueue *frontPolys, PolygonQueue *backPolys) 252 399 { 253 400 // terminate traversal if no more faces in mesh 254 if ((polys->mFaces.size() <= mMaxPolys) || (depth >= mMaxDepth)) 255 { 256 // don't need to store polygon information 257 polys->mFaces->clear(); // HACK: faces were deleted before 258 DEL_PTR(polys); // we can savely delete mesh 259 401 if ((polys->size() <= mMaxPolys) || (depth >= mMaxDepth)) 402 { 403 // don't need to store polygon information = delete polygons 404 Polygon::DeletePolygons(polys); 405 260 406 return leaf; 261 407 } … … 264 410 BspInterior *node = new BspInterior(SelectPlane(polys)); 265 411 266 FaceContainer::const_iterator fi;267 268 for (fi = polys->mFaces.begin(); fi != polys->mFaces.end(); ++ fi)269 { 270 Face *face = (*fi);412 while (!polys->empty()) 413 { 414 Polygon *poly = polys->front(); 415 416 polys->pop(); 271 417 272 418 int result = 0;// = node->GetPlane()->Side(node->GetPlane()); 273 419 274 Face *front_piece, *back_piece; 420 Polygon *front_piece = NULL; 421 Polygon *back_piece = NULL; 275 422 276 423 switch (result) 277 424 { 278 case 1: 279 frontPolys.AddFace(face); 425 case Polygon::COINCIDENT: 426 case Polygon::FRONT_SIDE: 427 frontPolys->push(poly); 280 428 break; 281 case -1:282 backPolys .AddFace(face);429 case Polygon::BACK_SIDE: 430 backPolys->push(poly); 283 431 break; 284 case 0: 285 //Split_Polygon (face, tree->partition, front_piece, back_piece); 286 backPolys.AddFace(back_piece); 287 frontPolys.AddFace(front_piece); 288 289 // face not needed anymore 290 DEL_PTR(face); 291 432 case Polygon::SPLIT: 433 front_piece = new Polygon(); 434 back_piece = new Polygon(); 435 436 //-- split polygon 437 poly->Split(node->GetPlane(), front_piece, back_piece); 438 439 440 backPolys->push(back_piece); 441 frontPolys->push(front_piece); 442 443 // don't need polygon anymore 444 DEL_PTR(poly); 292 445 break; 293 446 default: 447 frontPolys->push(poly); 294 448 break; 295 449 } … … 309 463 node->SetupChildLinks(back, front); 310 464 311 // don't need to store polygon information312 polys->mFaces->clear(); // HACK: faces were deleted before313 DEL_PTR(polys); // we can savely delete mesh314 315 465 DEL_PTR(leaf); 316 466 -
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
r224 r225 4 4 #include "Mesh.h" 5 5 #include "Containers.h" 6 6 #include <queue> 7 7 8 8 class ViewCell; … … 13 13 class BspTree; 14 14 class BspInterior; 15 15 class Polygon; 16 16 17 17 /** … … 91 91 }; 92 92 93 typedef std::queue<Polygon *> PolygonQueue; 94 95 /** Class representing a polygon. 96 */ 97 class Polygon 98 { 99 public: 100 Polygon(); 101 Polygon(const VertexContainer &vertices); 102 103 /** Copies all the vertices of the face. 104 */ 105 Polygon(Face *face, Mesh *parent); 106 107 /** Returns supporting plane of this polygon. 108 */ 109 Plane3 GetSupportingPlane(); 110 111 /** Splits polygon. 112 @param partition the split plane 113 @param front the front polygon 114 @param back the back polygon 115 */ 116 void Split(Plane3 *partition, Polygon *front, Polygon *back); 117 118 enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT}; 119 120 /** Side of the plane where the polygon is located. 121 @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT 122 */ 123 int Side(Plane3 *plane); 124 125 static void DeletePolygons(PolygonQueue *polys); 126 127 /// vertices are connected in counterclockwise order. 128 VertexContainer mVertices; 129 }; 130 93 131 /** Implementation of the ViewCell BSP tree. */ 94 132 class BspTree 95 133 { 96 134 public: 135 136 /** Additional data which is passed down the BSP tree during traversal. 137 */ 97 138 struct BspTraversalData 98 139 { … … 102 143 BspInterior *mParent; 103 144 /// polygonal data for splitting 104 Mesh*mPolys;145 PolygonQueue *mPolys; 105 146 /// current depth 106 147 int mDepth; … … 108 149 BspTraversalData() {} 109 150 110 BspTraversalData(BspNode *node, BspInterior *parent, Mesh*polys, const int depth):151 BspTraversalData(BspNode *node, BspInterior *parent, PolygonQueue *polys, const int depth): 111 152 mNode(node), mParent(parent), mPolys(polys), mDepth(depth) {} 112 153 }; 113 154 155 114 156 /** Constructs BSP tree from list of view cells. 115 157 */ … … 127 169 /** Selects a splitting plane from the given polygons. 128 170 */ 129 Plane3 SelectPlane( Mesh *polys);171 Plane3 SelectPlane(PolygonQueue *polyQueue); 130 172 131 173 /** Filters next viewcell down the tree and inserts it into the appropriate leaves … … 150 192 */ 151 193 BspNode *SubdivideNode(BspLeaf *leaf, BspInterior *parent, 152 Mesh*polys, const int depth,153 Mesh &frontPolys, Mesh &backPolys);194 PolygonQueue *polys, const int depth, 195 PolygonQueue *frontPolys, PolygonQueue *backPolys); 154 196 155 197 /** Extracts the mesh instances of the objects and copies them into the mesh. 156 198 */ 157 static void Copy2PolygonSoup(const ObjectContainer &objects, Mesh &mesh); 158 199 static void Copy2PolygonSoup(const ObjectContainer &objects, PolygonQueue &polys); 200 201 static void CopyMesh2Polygon(Mesh *mesh, PolygonQueue &polys); 159 202 160 203 /// Pointer to the root of the tree
Note: See TracChangeset
for help on using the changeset viewer.