Changeset 176 for trunk/VUT/GtpVisibilityPreprocessor
- Timestamp:
- 07/15/05 18:21:11 (19 years ago)
- Location:
- trunk/VUT/GtpVisibilityPreprocessor/src
- Files:
-
- 25 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp
r162 r176 30 30 for (x = 0; x < mWidth; x++) { 31 31 SetupRay(ray, x, y); 32 MeshInstance::NewMail();33 32 if (tree->CastRay(ray)) 34 33 if (ray.intersections.size()) { … … 50 49 51 50 long t2 = GetTime(); 52 cout<<"#RAY_CAST_TIME"<<endl<<TimeDiff(t1, t2)<<endl; 53 54 cerr<<"Saving image"<<endl; 55 image.save(filename, "PNG"); 51 cout<<"#RAY_CAST_TIME\n"; 52 cout<<TimeDiff(t1, t2)<<"\n"; 53 54 cout<<"Saving image"<<endl; 55 image.save(filename.c_str(), "PNG"); 56 56 57 57 Exporter *exporter = NULL; … … 72 72 if (0) 73 73 for (i= 0; i < ray->meshes.size(); i++) 74 exporter->Export MeshInstance(ray->meshes[i]);74 exporter->ExportIntersectable(ray->meshes[i]); 75 75 76 76 } -
trunk/VUT/GtpVisibilityPreprocessor/src/Containers.h
r162 r176 5 5 using namespace std; 6 6 7 class Mesh;8 class MeshInstance;9 7 class ViewCell; 10 8 class HierarchyNode; 11 9 class SceneGraphNode; 10 class Intersectable; 12 11 13 12 /** Container for Mesh pointers primarily for the use within the kDTree and 14 13 BSP hierarchies */ 15 typedef vector< MeshInstance *> MeshContainer;14 typedef vector<Intersectable *> ObjectContainer; 16 15 17 16 /** Container for ViewCell pointers primarily for the use within the kDTree and -
trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
r170 r176 6 6 using namespace std; 7 7 8 #include "Material.h" 9 8 10 class KdTree; 9 11 class SceneGraphNode; 10 12 class Ray; 11 13 class AxisAlignedBox3; 12 class MeshInstance;14 class Intersectable; 13 15 14 16 class Exporter … … 17 19 string mFilename; 18 20 bool wireframe; 21 bool mUseForcedMaterial; 22 Material mForcedMaterial; 19 23 20 24 public: 21 25 22 Exporter(const string filename):mFilename(filename),wireframe(false) 26 Exporter(const string filename):mFilename(filename), 27 wireframe(false), 28 mUseForcedMaterial(false) 23 29 { 24 30 } … … 34 40 35 41 virtual bool 36 ExportRays(const vector<Ray> &rays, const float length=1000) = 0; 42 ExportRays(const vector<Ray> &rays, 43 const float length=1000, 44 const RgbColor &color = RgbColor(1,1,1) 45 ) = 0; 46 37 47 38 48 virtual void 39 Export MeshInstance(MeshInstance *mesh) = 0;40 49 ExportIntersectable(Intersectable *object) = 0; 50 41 51 void SetWireframe() { wireframe = true; } 42 52 void SetFilled() { wireframe = false; } 43 53 54 void SetForcedMaterial(const Material &m) { 55 mForcedMaterial = m; 56 mUseForcedMaterial = true; 57 } 58 void ResetForcedMaterial() { 59 mUseForcedMaterial = false; 60 } 61 44 62 static Exporter * 45 63 GetExporter(const string filename); -
trunk/VUT/GtpVisibilityPreprocessor/src/Intersectable.h
r162 r176 2 2 #define __INTERSECTABLE_H 3 3 4 #include "AxisAlignedBox3.h" 5 #include "Pvs.h" 6 4 7 5 8 class Intersectable { 9 public: 10 static int mailID; 11 int mailbox; 12 13 KdPvs mKdPvs; 6 14 15 enum { MESH_INSTANCE, TRANSFORMED_MESH_INSTANCE, SPHERE }; 7 16 17 Intersectable():mailbox(0) {} 18 19 void Mail() { mailbox = mailID; } 20 static void NewMail() { mailID++; } 21 bool Mailed() const { return mailbox == mailID; } 22 23 virtual AxisAlignedBox3 GetBox() = 0; 24 25 virtual int CastRay(Ray &ray) = 0; 26 27 virtual bool IsConvex() = 0; 28 virtual bool IsWatertight() = 0; 29 virtual float IntersectionComplexity() = 0; 30 31 virtual int Type() const = 0; 32 33 virtual void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal) = 0; 34 8 35 }; 9 36 -
trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
r170 r176 60 60 mBox.Initialize(); 61 61 62 MeshContainer::const_iterator mi;62 ObjectContainer::const_iterator mi; 63 63 for ( mi = leaf->mObjects.begin(); 64 64 mi != leaf->mObjects.end(); … … 239 239 frontBBox.SetMin(axis, position); 240 240 241 MeshContainer::const_iterator mi;241 ObjectContainer::const_iterator mi; 242 242 243 243 for ( mi = leaf->mObjects.begin(); … … 391 391 392 392 // insert all queries 393 for( MeshContainer::const_iterator mi = node->mObjects.begin();394 mi <node->mObjects.end();393 for(ObjectContainer::const_iterator mi = node->mObjects.begin(); 394 mi != node->mObjects.end(); 395 395 mi++) { 396 396 AxisAlignedBox3 box = (*mi)->GetBox(); 397 397 398 splitCandidates->push_back(SortableEntry(SortableEntry:: MESH_MIN,398 splitCandidates->push_back(SortableEntry(SortableEntry::BOX_MIN, 399 399 box.Min(axis), 400 (void *)*mi)400 *mi) 401 401 ); 402 402 403 403 404 splitCandidates->push_back(SortableEntry(SortableEntry:: MESH_MAX,404 splitCandidates->push_back(SortableEntry(SortableEntry::BOX_MAX, 405 405 box.Max(axis), 406 (void *)*mi)406 *mi) 407 407 ); 408 408 } … … 429 429 // C = ct_div_ci + (ol + or)/queries 430 430 431 int totalFaces = 0;431 float totalIntersections = 0.0f; 432 432 vector<SortableEntry>::const_iterator ci; 433 433 … … 435 435 ci < splitCandidates->end(); 436 436 ci++) 437 if ((*ci).type == SortableEntry::MESH_MIN) { 438 Mesh *mesh = ((MeshInstance *)(*ci).data)->GetMesh(); 439 totalFaces += mesh->mFaces.size(); 440 } 441 442 int facesLeft = 0; 443 int facesRight = totalFaces; 437 if ((*ci).type == SortableEntry::BOX_MIN) { 438 totalIntersections += (*ci).intersectable->IntersectionComplexity(); 439 } 440 441 float intersectionsLeft = 0; 442 float intersectionsRight = totalIntersections; 444 443 445 444 int objectsLeft = 0, objectsRight = node->mObjects.size(); … … 457 456 ci < splitCandidates->end(); 458 457 ci++) { 459 Mesh *mesh = ((MeshInstance *)(*ci).data)->GetMesh();460 458 switch ((*ci).type) { 461 case SortableEntry:: MESH_MIN:459 case SortableEntry::BOX_MIN: 462 460 objectsLeft++; 463 facesLeft += mesh->mFaces.size();461 intersectionsLeft += (*ci).intersectable->IntersectionComplexity(); 464 462 break; 465 case SortableEntry:: MESH_MAX:463 case SortableEntry::BOX_MAX: 466 464 objectsRight--; 467 facesRight -= mesh->mFaces.size();465 intersectionsRight -= (*ci).intersectable->IntersectionComplexity(); 468 466 break; 469 467 } … … 477 475 float sum; 478 476 if (mSahUseFaces) 479 sum = facesLeft*lbox.SurfaceArea() + facesRight*rbox.SurfaceArea();477 sum = intersectionsLeft*lbox.SurfaceArea() + intersectionsRight*rbox.SurfaceArea(); 480 478 else 481 479 sum = objectsLeft*lbox.SurfaceArea() + objectsRight*rbox.SurfaceArea(); … … 494 492 } 495 493 496 float oldCost = mSahUseFaces ? total Faces : node->mObjects.size();494 float oldCost = mSahUseFaces ? totalIntersections : node->mObjects.size(); 497 495 float newCost = mCt_div_ci + minSum/boxArea; 498 496 float ratio = newCost/oldCost; … … 518 516 float mint = 0; 519 517 520 518 Intersectable::NewMail(); 519 521 520 if (!mBox.GetMinMaxT(ray, &mint, &maxt)) 522 521 return 0; … … 574 573 ray.leaves.push_back(leaf); 575 574 576 MeshContainer::const_iterator mi;575 ObjectContainer::const_iterator mi; 577 576 for ( mi = leaf->mObjects.begin(); 578 577 mi != leaf->mObjects.end(); 579 578 mi++) { 580 MeshInstance *mesh= *mi;581 if (! mesh->Mailed() ) {582 mesh->Mail();579 Intersectable *object = *mi; 580 if (!object->Mailed() ) { 581 object->Mail(); 583 582 //ray.meshes.push_back(mesh); 584 hits += mesh->CastRay(ray);583 hits += object->CastRay(ray); 585 584 } 586 585 } … … 608 607 } 609 608 609 void 610 KdTree::CollectObjects(KdNode *n, ObjectContainer &objects) 611 { 612 stack<KdNode *> nodeStack; 613 614 nodeStack.push(n); 615 616 while (!nodeStack.empty()) { 617 KdNode *node = nodeStack.top(); 618 nodeStack.pop(); 619 if (node->IsLeaf()) { 620 KdLeaf *leaf = (KdLeaf *)node; 621 for (int j=0; j < leaf->mObjects.size(); j++) { 622 Intersectable *object = leaf->mObjects[j]; 623 if (!object->Mailed()) { 624 object->Mail(); 625 objects.push_back(object); 626 } 627 } 628 } else { 629 KdInterior *interior = (KdInterior *)node; 630 nodeStack.push(interior->mFront); 631 nodeStack.push(interior->mBack); 632 } 633 } 634 } -
trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h
r170 r176 13 13 class KdLeaf; 14 14 class KdInterior; 15 class Intersectable; 15 16 16 17 … … 156 157 157 158 /** pointers to occluders contained in this node */ 158 MeshContainer mObjects;159 ObjectContainer mObjects; 159 160 160 161 /** pointers to viewcells contained in this node */ … … 220 221 KdTree(); 221 222 222 /** Insert occluder into the tree */223 virtual void InsertOccluder(Mesh *occluder) {224 // mRoot->mOccluders.push_back(occluder);225 }226 227 /** Insert occludee into the tree */228 virtual void InsertOccludee(Mesh *occludee) {229 // mRoot->mOccludees.push_back(occludee);230 }231 223 232 224 /** Insert view cell into the tree */ … … 266 258 } 267 259 260 void 261 CollectObjects(KdNode *n, ObjectContainer &objects); 262 268 263 AxisAlignedBox3 GetBox(const KdNode *node) const { 269 264 KdInterior *parent = node->mParent; … … 383 378 { 384 379 enum { 385 MESH_MIN,386 MESH_MAX380 BOX_MIN, 381 BOX_MAX 387 382 }; 388 383 389 384 int type; 390 385 float value; 391 void *data;386 Intersectable *intersectable; 392 387 393 388 SortableEntry() {} 394 SortableEntry(const int t, const float v, void *d):type(t),395 396 data(d) {}389 SortableEntry(const int t, const float v, Intersectable *i):type(t), 390 value(v), 391 intersectable(i) {} 397 392 398 393 bool operator<(const SortableEntry &b) const { -
trunk/VUT/GtpVisibilityPreprocessor/src/Matrix4x4.cpp
r162 r176 300 300 { 301 301 Vector3 vec = CrossProd(vecStart, vecTo); 302 302 303 303 if (Magnitude(vec) > Limits::Small) { 304 304 // vector exist, compute angle -
trunk/VUT/GtpVisibilityPreprocessor/src/Matrix4x4.h
r162 r176 47 47 // create the rotation matrix that rotates 'vecFrom' to 'vecTo' 48 48 friend Matrix4x4 RotationVectorsMatrix(const Vector3 &vecFrom, 49 const Vector3 &vecTo);49 const Vector3 &vecTo); 50 50 51 51 friend Matrix4x4 ScaleMatrix(float X, float Y, float Z); -
trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
r170 r176 8 8 Mesh::Preprocess() 9 9 { 10 mBo undingBox.Initialize();10 mBox.Initialize(); 11 11 12 12 VertexContainer::const_iterator vi = mVertices.begin(); 13 13 for (; vi != mVertices.end(); vi++) { 14 mBoundingBox.Include(*vi); 15 } 16 14 mBox.Include(*vi); 15 } 16 17 /** true if it is a watertight convex mesh */ 17 18 mIsConvex = false; 18 19 if (mFaces.size() > 2*MeshKdTree::mTermMinCost) {19 20 if (mFaces.size() > MeshKdTree::mTermMinCost) { 20 21 mKdTree = new MeshKdTree(this); 21 22 MeshKdLeaf *root = (MeshKdLeaf *)mKdTree->GetRoot(); … … 24 25 cout<<"KD"; 25 26 mKdTree->Construct(); 27 28 if (mKdTree->GetRoot()->IsLeaf()) { 29 cout<<"d"; 30 delete mKdTree; 31 } 26 32 } 27 33 } … … 252 258 253 259 260 void 261 Mesh::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal) 262 { 263 int faceIndex = RandomValue(0, mFaces.size()-1); 264 265 // assume the face is convex and generate a convex combination 266 // 267 Face *face = mFaces[faceIndex]; 268 point = Vector3(0,0,0); 269 float sum = 0.0f; 270 for (int i = 0; i < face->mVertexIndices.size(); i++) { 271 float r = RandomValue(0,1); 272 sum += r; 273 point += mVertices[face->mVertexIndices[i]]*r; 274 } 275 point *= 1.0f/sum; 276 normal = GetFacePlane(faceIndex).mNormal; 277 } 278 279 254 280 int 255 281 MeshInstance::CastRay( … … 271 297 272 298 299 300 void 301 MeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal) 302 { 303 mMesh->GetRandomSurfacePoint(point, normal); 304 } 305 306 void 307 TransformedMeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal) 308 { 309 mMesh->GetRandomSurfacePoint(point, normal); 310 point = mWorldTransform*point; 311 normal = TransformNormal(mWorldTransform, normal); 312 } 313 273 314 Plane3 274 315 Mesh::GetFacePlane(const int faceIndex) … … 281 322 282 323 int 283 MeshTransformedInstance::CastRay(324 TransformedMeshInstance::CastRay( 284 325 Ray &ray 285 326 ) -
trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.h
r170 r176 50 50 class Mesh { 51 51 52 53 52 public: 54 53 … … 58 57 /// Constructor with container preallocation 59 58 Mesh(const int vertices, 60 const int faces):mVertices(), 61 mFaces(), 62 mMaterial(NULL) 59 const int faces): 60 mFaces(), 61 mMaterial(NULL), 62 mKdTree(NULL), 63 mVertices(), 64 mIsConvex(false), 65 mIsWatertight(false) 63 66 { 64 67 mVertices.reserve(vertices); … … 78 81 void Preprocess(); 79 82 80 /** Axis aligned bounding box of the mesh */81 AxisAlignedBox3 mBo undingBox;83 /** Axis aligned bounding box of the mesh in local mesh coordinates */ 84 AxisAlignedBox3 mBox; 82 85 83 86 /** Vertices forming the mesh */ … … 92 95 /** true if the mesh is a convex mesh */ 93 96 bool mIsConvex; 97 98 /** true if the mesh is a convex mesh */ 99 bool mIsWatertight; 94 100 95 101 MeshKdTree *mKdTree; … … 128 134 129 135 AxisAlignedBox3 GetFaceBox(const int faceIndex); 130 136 137 void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); 138 131 139 }; 132 140 133 141 class MeshInstance : public Intersectable { 134 142 protected: 135 static int mailID;136 int mailbox;137 float mVisibility;138 143 Mesh *mMesh; 139 144 140 145 public: 141 MeshInstance(Mesh *mesh):mMesh(mesh), mVisibility(1.0f), mailbox(0) 142 { 143 } 144 145 void Mail() { mailbox = mailID; } 146 static void NewMail() { mailID++; } 147 bool Mailed() const { return mailbox == mailID; } 148 146 MeshInstance(Mesh *mesh):Intersectable(), mMesh(mesh) 147 { 148 } 149 150 void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); 151 149 152 150 153 Mesh *GetMesh() { return mMesh; } 151 154 152 155 virtual AxisAlignedBox3 GetBox() { 153 return mMesh->mBo undingBox;156 return mMesh->mBox; 154 157 } 155 158 … … 158 161 Ray &ray 159 162 ); 160 163 164 virtual bool IsConvex() { return mMesh->mIsConvex; } 165 virtual bool IsWatertight() { return mMesh->mIsWatertight; } 166 virtual float IntersectionComplexity() { return mMesh->mFaces.size(); } 167 168 virtual int Type() const { return MESH_INSTANCE; } 169 161 170 virtual int 162 171 CastRay( … … 169 178 170 179 171 class MeshTransformedInstance : public MeshInstance180 class TransformedMeshInstance : public MeshInstance 172 181 { 173 182 public: 174 MeshTransformedInstance(Mesh *mesh):MeshInstance(mesh)183 TransformedMeshInstance(Mesh *mesh):MeshInstance(mesh) 175 184 { 176 185 mWorldTransform = IdentityMatrix(); … … 178 187 179 188 virtual AxisAlignedBox3 GetBox() { 180 return Transform(mMesh->mBo undingBox,189 return Transform(mMesh->mBox, 181 190 mWorldTransform); 182 191 } 183 192 184 193 virtual int 185 194 CastRay( 186 195 Ray &ray 187 196 ); 197 198 virtual int Type() const { return TRANSFORMED_MESH_INSTANCE; } 199 200 void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); 188 201 189 202 private: -
trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
r170 r176 54 54 // add mesh instances of the scene graph to the root of the tree 55 55 KdLeaf *root = (KdLeaf *)mKdTree->GetRoot(); 56 mSceneGraph->Collect MeshInstaces(&root->mObjects);56 mSceneGraph->CollectObjects(&root->mObjects); 57 57 58 58 mKdTree->Construct(); -
trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
r162 r176 84 84 85 85 /// list of all loaded occluders 86 MeshContainer mOccluders;86 ObjectContainer mOccluders; 87 87 /// list of all loaded occludees 88 MeshContainer mOccludees;88 ObjectContainer mOccludees; 89 89 /// list of all loaded/generated viewcells 90 90 ViewCellContainer mViewcells; -
trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
r162 r176 28 28 // the point of intersection 29 29 float mT; 30 30 31 // can be either mesh or a viewcell 31 32 Intersectable *mObject; 33 32 34 // the face of the intersectable 33 35 int mFace; 36 34 37 RayIntersection(const float t, 35 38 Intersectable *object, 36 39 const int face):mT(t), mObject(object), mFace(face) {} 40 37 41 RayIntersection() {} 38 42 … … 44 48 b.mT; 45 49 } 46 47 48 50 49 51 }; 52 53 // I should have some abstract cell data type !!! here 54 // corresponds to the spatial elementary cell 55 /** intersection with the source object if any */ 56 RayIntersection sourceObject; 50 57 51 58 vector<RayIntersection> intersections; … … 61 68 dir = Normalize(whichdir); 62 69 mType = _type; 63 origin = _originCell;64 termination = NULL;65 70 depth = 0; 66 71 Init(); … … 72 77 // Inititalize the ray again when already constructed 73 78 void Init(const Vector3 &wherefrom, const Vector3 &whichdir, 74 const int _type, const void *_originCell = NULL,79 const int _type, 75 80 bool dirNormalized = false) { 76 81 loc = wherefrom; 77 82 dir = (dirNormalized) ? whichdir: Normalize(whichdir) ; 78 83 mType = _type; 79 origin = _originCell;80 termination = NULL;81 84 depth = 0; 82 85 Init(); … … 146 149 } 147 150 148 // the cell in the ASDS, where ray starts from149 void SetOrigin(const void *c) {origin = c;}150 const void *GetOrigin() const { return origin; }151 152 // the cell in the ASDS, where ray finishes the walk153 void SetTermination(const void *c) {termination = c; }154 const void* GetTermination() const { return termination;}155 151 156 152 // the object on which the ray starts at … … 183 179 Vector3 loc, dir; // Describes ray origin and vector 184 180 185 186 181 // The inverted direction of the ray components. It is computed optionally 187 182 // by the ray traversal algorithm using function ComputeInvertedDir(); … … 191 186 int mType; 192 187 193 // I should have some abstract cell data type !!! here194 // corresponds to the spatial elementary cell195 const void *origin;196 const void *termination;197 188 198 189 // unique ID of a ray for the use in the mailboxes 199 190 int ID; 191 200 192 // unique ID of a ray for the use with a transformations - this one 201 193 // never can be changed that allows the nesting of transformations -
trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
r162 r176 1 #include "SceneGraph.h" 2 #include "KdTree.h" 1 3 #include "SamplingPreprocessor.h" 4 #include "X3dExporter.h" 2 5 6 SamplingPreprocessor::SamplingPreprocessor() 7 { 8 // this should increase coherence of the samples 9 mSamplesPerPass = 1; 10 mTotalSamples = 1e8; 11 mKdPvsDepth = 10; 3 12 13 } 14 15 void 16 SamplingPreprocessor::SetupRay(Ray &ray, const Vector3 &point, const Vector3 &direction) 17 { 18 ray.intersections.clear(); 19 ray.leaves.clear(); 20 ray.meshes.clear(); 21 // cout<<point<<" "<<direction<<endl; 22 ray.Init(point, direction, Ray::LOCAL_RAY); 23 } 24 25 KdNode * 26 SamplingPreprocessor::GetNodeForPvs(KdLeaf *leaf) 27 { 28 KdNode *node = leaf; 29 while (node->mParent && node->mDepth > mKdPvsDepth) 30 node = node->mParent; 31 return node; 32 } 33 34 int 35 SamplingPreprocessor::AddNodeSamples(Intersectable *object, const Ray &ray) 36 { 37 int contributingSamples = 0; 38 39 for (int j=0; j < ray.leaves.size(); j++) { 40 KdNode *node = GetNodeForPvs( ray.leaves[j] ); 41 contributingSamples += object->mKdPvs.AddNodeSample(node); 42 } 4 43 44 return contributingSamples; 45 } 46 5 47 bool 6 48 SamplingPreprocessor::ComputeVisibility() 7 49 { 8 return true; 9 }; 50 51 // pickup an object 52 ObjectContainer objects; 53 54 mSceneGraph->CollectObjects(&objects); 55 56 Vector3 point, normal, direction; 57 Ray ray; 58 59 long startTime = GetTime(); 60 61 int i; 62 int pass = 0; 63 int totalSamples = 0; 10 64 11 65 66 int pvsOut = Min((int)objects.size(), 10); 67 68 vector<Ray> rays[10]; 69 70 while (totalSamples < mTotalSamples) { 71 72 int passContributingSamples = 0; 73 int passSampleContributions = 0; 74 int passSamples = 0; 75 76 for (i =0; i < objects.size(); i++) { 77 for (int k=0; k < mSamplesPerPass; k++) { 78 Intersectable *object = objects[i]; 79 object->GetRandomSurfacePoint(point, normal); 80 direction = UniformRandomVector(normal); 81 // construct a ray 82 SetupRay(ray, point, direction); 83 mKdTree->CastRay(ray); 84 85 if (i < pvsOut) 86 rays[i].push_back(ray); 87 88 int sampleContributions = 0; 89 90 if (ray.leaves.size()) { 91 sampleContributions += AddNodeSamples(object, ray); 92 93 if (ray.intersections.size()) { 94 sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray); 95 // check whether we can add this to the rays 96 for (int j = 0; j < pvsOut; j++) { 97 if (objects[j] == ray.intersections[0].mObject) { 98 rays[j].push_back(ray); 99 } 100 } 101 } 102 passSamples++; 103 if (sampleContributions) { 104 passContributingSamples++; 105 passSampleContributions += sampleContributions; 106 } 107 } 108 } 109 } 110 totalSamples += passSamples; 111 pass++; 112 113 int pvsSize = 0; 114 for (i=0; i < objects.size(); i++) { 115 Intersectable *object = objects[i]; 116 pvsSize += object->mKdPvs.mEntries.size(); 117 } 118 119 cout<<"pass "<<pass<<" : t = "<<TimeDiff(startTime, GetTime())*1e-3<<"s"<<endl; 120 cout<<"#totalSamples="<<totalSamples/1000<< 121 "k #sampleContributions="<<passSampleContributions<< 122 " ("<<100*passContributingSamples/passSamples<<"%)"<< 123 " avgPVS="<<pvsSize/(float)objects.size()<<endl; 124 } 125 bool exportRays = false; 126 if (exportRays) { 127 Exporter *exporter = NULL; 128 exporter = Exporter::GetExporter("sample-rays.x3d"); 129 exporter->SetWireframe(); 130 exporter->ExportKdTree(*mKdTree); 131 for (i=0; i < pvsOut; i++) 132 exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0)); 133 exporter->SetFilled(); 134 delete exporter; 135 } 136 137 if (1) { 138 for (int k=0; k < pvsOut; k++) { 139 Intersectable *object = objects[k]; 140 char s[64]; 141 sprintf(s, "sample-pvs%04d.x3d", k); 142 Exporter *exporter = Exporter::GetExporter(s); 143 exporter->SetWireframe(); 144 KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 145 Intersectable::NewMail(); 146 // avoid adding the object to the list 147 object->Mail(); 148 ObjectContainer visibleObjects; 149 for (; i != object->mKdPvs.mEntries.end(); i++) { 150 KdNode *node = (*i).first; 151 exporter->ExportBox(mKdTree->GetBox(node)); 152 mKdTree->CollectObjects(node, visibleObjects); 153 } 154 155 exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0)); 156 exporter->SetFilled(); 157 158 for (int j = 0; j < visibleObjects.size(); j++) 159 exporter->ExportIntersectable(visibleObjects[j]); 160 161 Material m; 162 m.mDiffuseColor = RgbColor(1, 0, 0); 163 exporter->SetForcedMaterial(m); 164 exporter->ExportIntersectable(object); 165 166 delete exporter; 167 } 168 169 } 170 171 return true; 172 } 173 174 175 176 -
trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
r162 r176 9 9 class SamplingPreprocessor : public Preprocessor { 10 10 public: 11 int mSamplesPerPass; 12 int mTotalSamples; 13 int mKdPvsDepth; 14 15 SamplingPreprocessor(); 16 11 17 virtual bool ComputeVisibility(); 18 19 void 20 SetupRay(Ray &ray, const Vector3 &point, const Vector3 &direction); 21 22 KdNode * 23 GetNodeForPvs(KdLeaf *leaf); 24 25 int 26 AddNodeSamples(Intersectable *object, const Ray &ray); 27 12 28 }; 13 29 -
trunk/VUT/GtpVisibilityPreprocessor/src/SceneGraph.cpp
r162 r176 26 26 27 27 int 28 SceneGraph::Collect MeshInstaces(MeshContainer *instances)28 SceneGraph::CollectObjects(ObjectContainer *instances) 29 29 { 30 30 int number = 0; 31 31 stack<SceneGraphNode *> nodeStack; 32 32 33 33 nodeStack.push(mRoot); 34 34 … … 37 37 nodeStack.pop(); 38 38 39 MeshContainer::const_iterator mi = node->mGeometry.begin();39 ObjectContainer::const_iterator mi = node->mGeometry.begin(); 40 40 for (; mi != node->mGeometry.end(); mi++) 41 41 instances->push_back(*mi); -
trunk/VUT/GtpVisibilityPreprocessor/src/SceneGraph.h
r162 r176 13 13 class SceneGraphNode { 14 14 public: 15 16 17 MeshContainer mGeometry; 15 ObjectContainer mGeometry; 18 16 SceneGraphNodeContainer mChildren; 19 17 AxisAlignedBox3 mBox; … … 28 26 bool Export(const string filename); 29 27 30 int Collect MeshInstaces(MeshContainer *instances);28 int CollectObjects(ObjectContainer *instances); 31 29 32 30 protected: -
trunk/VUT/GtpVisibilityPreprocessor/src/Vector3.cpp
r162 r176 1 #include "Matrix4x4.h" 1 2 #include "Vector3.h" 2 3 … … 188 189 return; 189 190 } 191 192 Vector3 193 UniformRandomVector(const Vector3 &normal) 194 { 195 float r1 = RandomValue(0.0f, 1.0f); 196 float r2 = RandomValue(0.0f, 1.0f); 197 float cosTheta = 1.0f - r1; 198 float sinTheta = sqrt(1 - sqr(cosTheta)); 199 float fi = 2.0f*M_PI*r2; 200 201 Vector3 dir(sinTheta*sin(fi), 202 cosTheta, 203 sinTheta*cos(fi)); 204 205 206 // return Normalize(dir); 207 208 Matrix4x4 m = RotationVectorsMatrix( 209 normal, 210 Vector3(0,1,0)); 211 Matrix4x4 mi = Invert(m); 212 m = m*RotationVectorsMatrix( 213 Vector3(0,1,0), 214 Normalize(dir))*mi; 215 216 return TransformNormal(m, normal); 217 218 // return TransformNormal( 219 // RotationVectorsMatrix( 220 // Vector3(0,1,0), 221 // Normalize(dir) 222 // ), 223 // normal 224 // ); 225 } -
trunk/VUT/GtpVisibilityPreprocessor/src/Vector3.h
r162 r176 226 226 friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr); 227 227 friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2); 228 229 friend Vector3 UniformRandomVector(const Vector3 &normal); 228 230 }; 229 231 … … 411 413 { 412 414 return Vector3(A.y * B.z - A.z * B.y, 413 414 415 A.z * B.x - A.x * B.z, 416 A.x * B.y - A.y * B.x); 415 417 } 416 418 -
trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
r162 r176 23 23 } 24 24 25 25 26 bool 26 X3dExporter::ExportRays(const vector<Ray> &rays, const float length) 27 X3dExporter::ExportRays(const vector<Ray> &rays, 28 const float length, 29 const RgbColor &color) 27 30 { 28 31 vector<Ray>::const_iterator ri = rays.begin(); 29 32 stream<<"<Shape>"<<endl; 33 stream<<"<Appearance>"<<endl; 34 stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<< 35 "\" />"<<endl; 36 stream<<"</Appearance>"<<endl; 37 30 38 stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl; 31 39 … … 43 51 for (; ri != rays.end(); ri++) { 44 52 Vector3 a = (*ri).GetLoc(); 45 Vector3 b = (*ri).GetLoc() + length*(*ri).GetDir(); 53 54 Vector3 b; 55 if (length < 0) 56 b = (*ri).GetLoc() - length*(*ri).GetDir(); 57 else 58 if ((*ri).intersections.size()==0) 59 b = (*ri).GetLoc() + length*(*ri).GetDir(); 60 else 61 b = (*ri).Extrap((*ri).intersections[0].mT); 46 62 47 63 stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,"; … … 66 82 67 83 68 MeshContainer::const_iterator mi = node->mGeometry.begin();84 ObjectContainer::const_iterator mi = node->mGeometry.begin(); 69 85 for (; mi != node->mGeometry.end(); mi++) { 70 86 // export the transform... 71 Export Mesh((*mi)->GetMesh());87 ExportIntersectable(*mi); 72 88 } 73 89 74 90 stream<<"</Group>"<<endl; 75 91 92 } 93 void 94 X3dExporter::ExportIntersectable(Intersectable *object) 95 { 96 switch (object->Type()) { 97 case Intersectable::MESH_INSTANCE: 98 case Intersectable::TRANSFORMED_MESH_INSTANCE: 99 ExportMeshInstance((MeshInstance *)object); 100 break; 101 default: 102 cerr<<"Sorry the export for object not yet defined"<<endl; 103 break; 104 } 105 } 106 107 void 108 X3dExporter::ExportMeshInstance(MeshInstance *object) 109 { 110 // $$JB$$ 111 // in the future check whether the mesh was not already exportet 112 // and use a reference to the that mesh instead 113 ExportMesh(object->GetMesh()); 76 114 } 77 115 … … 82 120 stream<<"<Shape>"<<endl; 83 121 stream<<"<Appearance>"<<endl; 84 122 85 123 // $$ tmp -> random material 86 124 87 125 float r, g, b; 88 126 89 if (mesh->mMaterial) { 90 r = mesh->mMaterial->mDiffuseColor.r; 91 g = mesh->mMaterial->mDiffuseColor.g; 92 b = mesh->mMaterial->mDiffuseColor.b; 93 } else { 94 r = RandomValue(0.5, 1.0); 95 g = RandomValue(0.5, 1.0); 96 b = RandomValue(0.5, 1.0); 97 } 127 if (mUseForcedMaterial) { 128 r = mForcedMaterial.mDiffuseColor.r; 129 g = mForcedMaterial.mDiffuseColor.g; 130 b = mForcedMaterial.mDiffuseColor.b; 131 132 } else 133 if (mesh->mMaterial) { 134 r = mesh->mMaterial->mDiffuseColor.r; 135 g = mesh->mMaterial->mDiffuseColor.g; 136 b = mesh->mMaterial->mDiffuseColor.b; 137 } else { 138 r = RandomValue(0.5, 1.0); 139 g = RandomValue(0.5, 1.0); 140 b = RandomValue(0.5, 1.0); 141 } 98 142 stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<< 99 143 "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl; … … 205 249 206 250 207 void208 X3dExporter::ExportMeshInstance(MeshInstance *mi)209 {210 ExportMesh(mi->GetMesh());211 } -
trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
r162 r176 12 12 class SceneGraphNode; 13 13 class Mesh; 14 15 14 class Intersectable; 15 class MeshInstance; 16 16 17 17 class X3dExporter : public Exporter … … 27 27 28 28 bool 29 ExportRays(const vector<Ray> &rays, const float length=1000); 29 ExportRays(const vector<Ray> &rays, 30 const float length=1000, 31 const RgbColor &color = RgbColor(1,1,1)); 30 32 31 33 bool … … 39 41 } 40 42 41 bool43 virtual bool 42 44 ExportBox(const AxisAlignedBox3 &box); 43 45 … … 45 47 ExportMeshInstance(MeshInstance *mi); 46 48 47 void 49 virtual void 50 ExportIntersectable(Intersectable *object); 51 52 virtual void 48 53 ExportMesh(Mesh *mesh); 49 54 55 50 56 protected: 51 v oid57 virtual void 52 58 ExportSceneNode(SceneGraphNode *node); 53 59 -
trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp
r170 r176 27 27 #include "Mesh.h" 28 28 #include "SceneGraph.h" 29 30 31 29 32 30 // --------------------------------------------------------------------------- … … 62 60 // --------------------------------------------------------------------------- 63 61 X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root) : 64 fElementCount(0)65 , fAttrCount(0)66 , fCharacterCount(0)67 , fSpaceCount(0)68 { 69 currentNode = root;62 mElementCount(0) 63 , mAttrCount(0) 64 , mCharacterCount(0) 65 , mSpaceCount(0) 66 { 67 mCurrentNode = root; 70 68 } 71 69 … … 81 79 { 82 80 StrX lname(name); 83 string element(lname. localForm());81 string element(lname.LocalForm()); 84 82 if (element == "Shape") 85 83 EndShape(); … … 89 87 X3dParseHandlers::EndShape() 90 88 { 91 currentMesh->Preprocess(); 92 // make an instance of this mesh 93 MeshInstance *mi = new MeshInstance(currentMesh); 94 currentNode->mGeometry.push_back(mi); 95 currentMesh = NULL; 89 if (mCurrentMesh->mFaces.size()) { 90 mCurrentMesh->Preprocess(); 91 // make an instance of this mesh 92 MeshInstance *mi = new MeshInstance(mCurrentMesh); 93 mCurrentNode->mGeometry.push_back(mi); 94 } else { 95 cout<<"X"; 96 delete mCurrentMesh; 97 } 98 mCurrentMesh = NULL; 96 99 } 97 100 … … 105 108 106 109 for (i=0; i < len; i++) { 107 string attrName(StrX(attributes.getName(i)). localForm());110 string attrName(StrX(attributes.getName(i)).LocalForm()); 108 111 if (attrName == "coordIndex") { 109 112 StrX attrValue(attributes.getValue(i)); 110 113 // handle coordIndex 111 114 vertices.clear(); 112 const char *ptr = attrValue. localForm();115 const char *ptr = attrValue.LocalForm(); 113 116 char *endptr; 114 117 while(1) { … … 117 120 if (vertices.size() > 2) { 118 121 Face *face = new Face(vertices); 119 currentMesh->mFaces.push_back(face);122 mCurrentMesh->mFaces.push_back(face); 120 123 } 121 124 vertices.clear(); … … 137 140 int len = attributes.getLength(); 138 141 int i; 139 if (! currentMesh->mMaterial)140 currentMesh->mMaterial = new Material;142 if (!mCurrentMesh->mMaterial) 143 mCurrentMesh->mMaterial = new Material; 141 144 for (i=0; i < len; i++) { 142 string attrName(StrX(attributes.getName(i)). localForm());145 string attrName(StrX(attributes.getName(i)).LocalForm()); 143 146 StrX attrValue(attributes.getValue(i)); 144 const char *ptr = attrValue. localForm();147 const char *ptr = attrValue.LocalForm(); 145 148 if (attrName == "diffuseColor") { 146 149 float r, g, b; 147 150 if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3) 148 currentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b);151 mCurrentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b); 149 152 } 150 153 } … … 159 162 VertexContainer vertices; 160 163 for (i=0; i < len; i++) { 161 string attrName(StrX(attributes.getName(i)). localForm());164 string attrName(StrX(attributes.getName(i)).LocalForm()); 162 165 if (attrName == "point") { 163 166 StrX attrValue(attributes.getValue(i)); 164 const char *ptr = attrValue. localForm();167 const char *ptr = attrValue.LocalForm(); 165 168 char *endptr; 166 169 while(1) { … … 182 185 vertices.push_back(v); 183 186 } 184 currentMesh->mVertices = vertices;187 mCurrentMesh->mVertices = vertices; 185 188 } 186 189 } … … 193 196 { 194 197 StrX lname(name); 195 string element(lname. localForm());198 string element(lname.LocalForm()); 196 199 197 200 if (element == "IndexedFaceSet") { … … 202 205 if (element == "Shape") { 203 206 cout<<"+"; 204 currentMesh = new Mesh;207 mCurrentMesh = new Mesh; 205 208 } 206 209 207 210 if (element == "Coordinate") { 208 if ( currentMesh)211 if (mCurrentMesh) 209 212 StartCoordinate(attributes); 210 213 } … … 214 217 } 215 218 216 fElementCount++;217 fAttrCount += attributes.getLength();219 mElementCount++; 220 mAttrCount += attributes.getLength(); 218 221 } 219 222 … … 222 225 const unsigned int length) 223 226 { 224 fCharacterCount += length;227 mCharacterCount += length; 225 228 } 226 229 … … 229 232 const unsigned int length) 230 233 { 231 fSpaceCount += length;234 mSpaceCount += length; 232 235 } 233 236 … … 235 238 X3dParseHandlers::resetDocument() 236 239 { 237 fAttrCount = 0;238 fCharacterCount = 0;239 fElementCount = 0;240 fSpaceCount = 0;240 mAttrCount = 0; 241 mCharacterCount = 0; 242 mElementCount = 0; 243 mSpaceCount = 0; 241 244 } 242 245 … … 345 348 return false; 346 349 } 350 347 351 348 352 // Print out the stats that we collected and time taken 349 353 if (!errorCount) { 350 354 XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms (" 351 << handler. getElementCount() << " elems, "352 << handler. getAttrCount() << " attrs, "353 << handler. getSpaceCount() << " spaces, "354 << handler. getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;355 << handler.GetElementCount() << " elems, " 356 << handler.GetAttrCount() << " attrs, " 357 << handler.GetSpaceCount() << " spaces, " 358 << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl; 355 359 } 356 360 } -
trunk/VUT/GtpVisibilityPreprocessor/src/default.env
r170 r176 6 6 Scene { 7 7 8 # filename glasgow1.x3d 9 # filename vienna.x3d 8 10 # filename atlanta2.x3d 9 11 filename soda.dat … … 19 21 20 22 Unigraphics { 21 meshGrouping 1 00023 meshGrouping 1 22 24 } 23 25 … … 48 50 splitBorder 0.01 49 51 } 52 53 54 SamplingPreprocessor { 55 56 57 } -
trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
r170 r176 31 31 32 32 // p->mSceneGraph->Export("soda.x3d"); 33 if ( 1) {33 if (0) { 34 34 p->Export(filename + "-out.x3d", true, false); 35 35 p->Export(filename + "-kdtree.x3d", false, true); … … 37 37 38 38 // p->LoadViewcells("viewcells.wrl"); 39 p->ComputeVisibility(); 40 p->ExportPreprocessedData("scene.vis"); 39 if (1) { 40 p->ComputeVisibility(); 41 p->ExportPreprocessedData("scene.vis"); 42 } 41 43 42 if ( 1) {44 if (0) { 43 45 Camera camera; 44 46 camera.LookAtBox(p->mKdTree->GetBox()); -
trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro
r170 r176 31 31 UnigraphicsParser.cpp X3dExporter.cpp SceneGraph.cpp Material.cpp \ 32 32 Matrix4x4.cpp Vector3.cpp AxisAlignedBox3.cpp Ray.cpp main.cpp Mesh.cpp \ 33 Exporter.cpp Camera.cpp X3dParser.cpp MeshKdTree.cpp 33 Exporter.cpp Camera.cpp X3dParser.cpp MeshKdTree.cpp Pvs.cpp 34 34 35 35
Note: See TracChangeset
for help on using the changeset viewer.