- Timestamp:
- 11/22/05 20:51:37 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
r403 r427 10 10 #include "VssTree.h" 11 11 12 13 bool useViewSpaceBox = true; 14 bool use2dSampling = false; 15 bool useViewspacePlane = false; 16 12 17 VssPreprocessor::VssPreprocessor(): 13 18 mPass(0), … … 18 23 environment->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples); 19 24 environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples); 25 environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass); 20 26 environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling); 21 27 … … 49 55 static Ray ray; 50 56 AxisAlignedBox3 box = mKdTree->GetBox(); 57 58 AxisAlignedBox3 sbox = box; 59 sbox.Enlarge(Vector3(-Limits::Small)); 60 if (!sbox.IsInside(viewPoint)) 61 return 0; 51 62 52 63 SetupRay(ray, viewPoint, direction); … … 145 156 146 157 Vector3 147 VssPreprocessor::GetDirection(const Vector3 &viewpoint) 148 { 149 int i = RandomValue(0, mObjects.size()-1); 150 Intersectable *object = mObjects[i]; 151 Vector3 point, normal; 152 object->GetRandomSurfacePoint(point, normal); 158 VssPreprocessor::GetDirection(const Vector3 &viewpoint, 159 AxisAlignedBox3 *viewSpaceBox 160 ) 161 { 162 Vector3 point; 163 if (!use2dSampling) { 164 Vector3 normal; 165 int i = RandomValue(0, mObjects.size()-1); 166 Intersectable *object = mObjects[i]; 167 object->GetRandomSurfacePoint(point, normal); 168 } else { 169 AxisAlignedBox3 box; 170 171 if (viewSpaceBox) 172 box =*viewSpaceBox; 173 else 174 box = mKdTree->GetBox(); 175 176 point = box.GetRandomPoint(); 177 point.y = viewpoint.y; 178 } 179 153 180 return point - viewpoint; 154 181 } 155 182 156 183 int 157 VssPreprocessor::RandomizedImportanceSampling(VssTree *vssTree, 158 const int desiredSamples) 159 { 160 float minRayContribution; 161 float maxRayContribution; 162 float avgRayContribution; 163 164 vssTree->GetRayContributionStatistics(minRayContribution, 165 maxRayContribution, 166 avgRayContribution); 167 168 cout<< 169 "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<< 170 "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<< 171 "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl; 172 173 float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves()); 174 SimpleRayContainer rays; 175 int num = vssTree->GenerateRays(p, rays); 184 VssPreprocessor::GenerateImportanceRays(VssTree *vssTree, 185 const int desiredSamples, 186 SimpleRayContainer &rays 187 ) 188 { 189 int num; 190 if (0) { 191 float minRayContribution; 192 float maxRayContribution; 193 float avgRayContribution; 194 195 vssTree->GetRayContributionStatistics(minRayContribution, 196 maxRayContribution, 197 avgRayContribution); 198 199 cout<< 200 "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<< 201 "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<< 202 "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl; 203 204 float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves()); 205 num = vssTree->GenerateRays(p, rays); 206 } else { 207 int leaves = vssTree->stat.Leaves()/2; 208 num = vssTree->GenerateRays(desiredSamples, leaves, rays); 209 } 210 176 211 cout<<"Generated "<<num<<" rays."<<endl; 177 212 178 VssRayContainer vssRays;179 180 for (int i=0; i < rays.size(); i++)181 CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);182 183 vssTree->AddRays(vssRays);184 213 return num; 185 214 } 186 215 216 217 bool 218 VssPreprocessor::ExportRays(const char *filename, 219 const VssRayContainer &vssRays, 220 const int number 221 ) 222 { 223 cout<<"Exporting vss rays..."<<endl<<flush; 224 225 float prob = number/(float)vssRays.size(); 226 227 228 Exporter *exporter = NULL; 229 exporter = Exporter::GetExporter(filename); 230 // exporter->SetWireframe(); 231 // exporter->ExportKdTree(*mKdTree); 232 exporter->SetFilled(); 233 exporter->ExportScene(mSceneGraph->mRoot); 234 exporter->SetWireframe(); 235 236 if (mViewSpaceBox) { 237 exporter->SetForcedMaterial(RgbColor(1,0,0)); 238 exporter->ExportBox(*mViewSpaceBox); 239 exporter->ResetForcedMaterial(); 240 } 241 242 VssRayContainer rays; for (int i=0; i < vssRays.size(); i++) 243 if (RandomValue(0,1) < prob) 244 rays.push_back(vssRays[i]); 245 246 exporter->ExportRays(rays, RgbColor(1, 0, 0)); 247 248 delete exporter; 249 250 cout<<"done."<<endl<<flush; 251 252 return true; 253 } 254 255 256 bool 257 VssPreprocessor::ExportVssTreeLeaf(char *filename, 258 VssTree *tree, 259 VssTreeLeaf *leaf) 260 { 261 Exporter *exporter = NULL; 262 exporter = Exporter::GetExporter(filename); 263 exporter->SetWireframe(); 264 exporter->ExportKdTree(*mKdTree); 265 266 if (mViewSpaceBox) { 267 exporter->SetForcedMaterial(RgbColor(1,0,0)); 268 exporter->ExportBox(*mViewSpaceBox); 269 exporter->ResetForcedMaterial(); 270 } 271 272 exporter->SetForcedMaterial(RgbColor(0,0,1)); 273 exporter->ExportBox(tree->GetBBox(leaf)); 274 exporter->ResetForcedMaterial(); 275 276 VssRayContainer rays[4]; 277 for (int i=0; i < leaf->rays.size(); i++) { 278 int k = leaf->rays[i].GetRayClass(); 279 rays[k].push_back(leaf->rays[i].mRay); 280 } 281 282 // SOURCE RAY 283 exporter->ExportRays(rays[0], RgbColor(1, 0, 0)); 284 // TERMINATION RAY 285 exporter->ExportRays(rays[1], RgbColor(1, 1, 1)); 286 // PASSING_RAY 287 exporter->ExportRays(rays[2], RgbColor(1, 1, 0)); 288 // CONTAINED_RAY 289 exporter->ExportRays(rays[3], RgbColor(0, 0, 1)); 290 291 delete exporter; 292 return true; 293 } 294 295 void 296 VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number) 297 { 298 vector<VssTreeLeaf *> leaves; 299 tree->CollectLeaves(leaves); 300 301 int num = 0; 302 int i; 303 float p = number / (float)leaves.size(); 304 for (i=0; i < leaves.size(); i++) { 305 if (RandomValue(0,1) < p) { 306 char filename[64]; 307 sprintf(filename, "vss-leaf-%04d.x3d", num); 308 ExportVssTreeLeaf(filename, tree, leaves[i]); 309 num++; 310 } 311 if (num >= number) 312 break; 313 } 314 } 187 315 188 316 bool … … 196 324 int totalSamples = 0; 197 325 198 AxisAlignedBox3 *viewSpaceBox = NULL; 199 200 AxisAlignedBox3 box = mKdTree->GetBox(); 201 202 203 if (1) 204 box.Enlarge(box.Size()*-Vector3(0.45, 0.45, 0.45)); 205 else { 326 327 AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox()); 328 329 if (!useViewspacePlane) { 330 float size = 0.01f; 331 float s = 0.5f - size; 332 float olds = Magnitude(box->Size()); 333 box->Enlarge(box->Size()*Vector3(-s)); 334 Vector3 translation = Vector3(-olds*0.1f, 0, 0); 335 box->SetMin(box->Min() + translation); 336 box->SetMax(box->Max() + translation); 337 } else { 338 206 339 // sample city like heights 207 box.SetMin(1, box.Min(1) + box.Size(1)*0.1); 208 box.SetMax(1, box.Min(1) + box.Size(1)*0.2); 209 } 210 211 bool useViewSpaceBox = false; 340 box->SetMin(1, box->Min(1) + box->Size(1)*0.1); 341 box->SetMax(1, box->Min(1) + box->Size(1)*0.2); 342 } 343 344 if (use2dSampling) 345 box->SetMax(1, box->Min(1)); 346 212 347 if (useViewSpaceBox) 213 viewSpaceBox = &box; 348 mViewSpaceBox = box; 349 else 350 mViewSpaceBox = NULL; 351 214 352 215 353 VssTree *vssTree = NULL; … … 223 361 int sampleContributions; 224 362 225 226 for (int k=0; k < mSamplesPerPass; k++) {363 int s = Min(mSamplesPerPass, mInitialSamples); 364 for (int k=0; k < s; k++) { 227 365 228 Vector3 viewpoint = GetViewpoint( viewSpaceBox);229 Vector3 direction = GetDirection(viewpoint );366 Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 367 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 230 368 231 369 sampleContributions = CastRay(viewpoint, direction, mVssRays); … … 271 409 cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush; 272 410 273 cout<<"Exporting vss rays..."<<endl<<flush; 274 275 int exportRays = 10000; 276 277 if (exportRays) { 278 float prob = exportRays/(float)mVssRays.size(); 279 280 Exporter *exporter = NULL; 281 exporter = Exporter::GetExporter("vss-rays.x3d"); 282 exporter->SetWireframe(); 283 exporter->ExportKdTree(*mKdTree); 284 if (viewSpaceBox) { 285 exporter->SetForcedMaterial(RgbColor(1,0,0)); 286 exporter->ExportBox(*viewSpaceBox); 287 exporter->ResetForcedMaterial(); 288 } 289 VssRayContainer rays; 290 for (int i=0; i < mVssRays.size(); i++) 291 if (RandomValue(0,1) < prob) 292 rays.push_back(mVssRays[i]); 293 exporter->ExportRays(rays, RgbColor(1, 0, 0)); 294 295 delete exporter; 296 } 297 cout<<"done."<<endl<<flush; 411 412 // int numExportRays = 10000; 413 414 int numExportRays = 0; 415 416 if (numExportRays) { 417 char filename[64]; 418 sprintf(filename, "vss-rays-initial.x3d"); 419 ExportRays(filename, mVssRays, numExportRays); 420 } 298 421 299 422 … … 302 425 vssTree = new VssTree; 303 426 304 vssTree->Construct(mVssRays, viewSpaceBox);427 vssTree->Construct(mVssRays, mViewSpaceBox); 305 428 306 429 cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 307 430 431 ExportVssTreeLeaves(vssTree, 10); 308 432 309 433 int samples = 0; 434 int pass = 0; 310 435 while (1) { 311 int num = mSamplesPerPass; 436 int num = mVssSamplesPerPass; 437 SimpleRayContainer rays; 438 VssRayContainer vssRays; 439 312 440 if (!mUseImportanceSampling) { 313 VssRayContainer vssRays;314 441 for (int j=0; j < num; j++) { 315 Vector3 viewpoint = GetViewpoint( viewSpaceBox);316 Vector3 direction = GetDirection(viewpoint );317 CastRay(viewpoint, direction, vssRays);442 Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 443 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 444 rays.push_back(SimpleRay(viewpoint, direction)); 318 445 } 319 vssTree->AddRays(vssRays);320 446 } else { 321 num = RandomizedImportanceSampling(vssTree, num); 322 } 447 num = GenerateImportanceRays(vssTree, num, rays); 448 } 449 450 451 for (int i=0; i < rays.size(); i++) 452 CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays); 453 454 vssTree->AddRays(vssRays); 455 456 if (1) { 457 int subdivided = vssTree->UpdateSubdivision(); 458 cout<<"subdivided leafs = "<<subdivided<<endl; 459 } 460 461 if (numExportRays) { 462 char filename[64]; 463 if (mUseImportanceSampling) 464 sprintf(filename, "vss-rays-i%04d.x3d", pass); 465 else 466 sprintf(filename, "vss-rays-%04d.x3d", pass); 467 468 ExportRays(filename, vssRays, numExportRays); 469 } 470 471 323 472 samples+=num; 324 473 float pvs = vssTree->GetAvgPvsSize(); 474 cout<<"*****************************\n"; 325 475 cout<<samples<<" avgPVS ="<<pvs<<endl; 476 cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 477 cout<<"*****************************\n"; 326 478 if (samples >= mVssSamples) 327 479 break; 480 pass++; 328 481 } 329 482
Note: See TracChangeset
for help on using the changeset viewer.