[372] | 1 | #include "SceneGraph.h"
|
---|
| 2 | #include "KdTree.h"
|
---|
| 3 | #include "SamplingPreprocessor.h"
|
---|
| 4 | #include "X3dExporter.h"
|
---|
| 5 | #include "Environment.h"
|
---|
| 6 | #include "MutualVisibility.h"
|
---|
| 7 | #include "Polygon3.h"
|
---|
| 8 | #include "ViewCell.h"
|
---|
[439] | 9 | #include "ViewCellsManager.h"
|
---|
[406] | 10 | #include "RenderSimulator.h"
|
---|
[1743] | 11 | #include "VssRay.h"
|
---|
[1883] | 12 | #include "SamplingStrategy.h"
|
---|
[2035] | 13 | #include "RayCaster.h"
|
---|
[372] | 14 |
|
---|
[860] | 15 |
|
---|
[1221] | 16 |
|
---|
[863] | 17 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 18 |
|
---|
| 19 |
|
---|
[1292] | 20 | SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
|
---|
[372] | 21 | {
|
---|
| 22 | // this should increase coherence of the samples
|
---|
[1199] | 23 |
|
---|
[372] | 24 | }
|
---|
| 25 |
|
---|
| 26 | SamplingPreprocessor::~SamplingPreprocessor()
|
---|
| 27 | {
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[444] | 30 |
|
---|
[1520] | 31 |
|
---|
[372] | 32 |
|
---|
[429] | 33 | void
|
---|
[372] | 34 | SamplingPreprocessor::VerifyVisibility(Intersectable *object)
|
---|
| 35 | {
|
---|
[1579] | 36 | #if 0 // 6.10. 2006 due to kdPVS removal from intersectable
|
---|
| 37 | // mail all nodes from the pvs
|
---|
[1199] | 38 | Intersectable::NewMail();
|
---|
| 39 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
|
---|
| 40 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 41 | KdNode *node = (*i).first;
|
---|
| 42 | node->Mail();
|
---|
| 43 | }
|
---|
| 44 | Debug << "Get all neighbours from PVS" << endl;
|
---|
| 45 | vector<KdNode *> invisibleNeighbors;
|
---|
| 46 | // get all neighbors of all PVS nodes
|
---|
| 47 | i = object->mKdPvs.mEntries.begin();
|
---|
| 48 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 49 | KdNode *node = (*i).first;
|
---|
| 50 | mKdTree->FindNeighbors(node, invisibleNeighbors, true);
|
---|
| 51 | AxisAlignedBox3 box = object->GetBox();
|
---|
| 52 | for (int j=0; j < invisibleNeighbors.size(); j++) {
|
---|
| 53 | int visibility = ComputeBoxVisibility(mSceneGraph,
|
---|
| 54 | mKdTree,
|
---|
| 55 | box,
|
---|
| 56 | mKdTree->GetBox(invisibleNeighbors[j]),
|
---|
| 57 | 1e-6f);
|
---|
| 58 | // exit(0);
|
---|
[372] | 59 | }
|
---|
[1199] | 60 | // now rank all the neighbors according to probability that a new
|
---|
| 61 | // sample creates some contribution
|
---|
| 62 | }
|
---|
[1579] | 63 | #endif
|
---|
[372] | 64 | }
|
---|
| 65 |
|
---|
| 66 | bool
|
---|
| 67 | SamplingPreprocessor::ComputeVisibility()
|
---|
| 68 | {
|
---|
[1199] | 69 | Debug << "type: sampling" << endl;
|
---|
| 70 | cout<<"Sampling Preprocessor started\n"<<flush;
|
---|
| 71 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[2653] | 72 |
|
---|
[1199] | 73 | Randomize(0);
|
---|
[1563] | 74 | const long startTime = GetTime();
|
---|
[372] | 75 | int totalSamples = 0;
|
---|
| 76 |
|
---|
[1199] | 77 | // if not already loaded, construct view cells from file
|
---|
[1715] | 78 | if (!mLoadViewCells) {
|
---|
| 79 | ConstructViewCells();
|
---|
[1199] | 80 | }
|
---|
[2653] | 81 |
|
---|
[1743] | 82 | int intersectables, faces;
|
---|
| 83 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
[1966] | 84 | HaltonSequence posHalton;
|
---|
[1743] | 85 |
|
---|
[1199] | 86 | int samples = 0;
|
---|
[1966] | 87 | int rssSamples = 0;
|
---|
| 88 |
|
---|
| 89 | map<ViewCell *, HaltonSequence> dirHalton;
|
---|
[2035] | 90 | int lastEvaluation = 0;
|
---|
| 91 | SimpleRayContainer rays;
|
---|
| 92 | VssRayContainer vssRays;
|
---|
| 93 |
|
---|
[1199] | 94 | while (samples < mTotalSamples) {
|
---|
[2035] | 95 | for (int i=0; i < mSamplesPerPass;) {
|
---|
[1966] | 96 |
|
---|
[2035] | 97 | mRayCaster->InitPass();
|
---|
| 98 |
|
---|
[1966] | 99 | float r[5];
|
---|
| 100 | Vector3 origin, direction;
|
---|
| 101 | posHalton.GetNext(3, r);
|
---|
| 102 |
|
---|
| 103 | mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
|
---|
| 104 | ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
|
---|
| 105 |
|
---|
| 106 | if (!viewcell || !viewcell->GetValid())
|
---|
| 107 | continue;
|
---|
[2035] | 108 |
|
---|
| 109 | rays.clear();
|
---|
| 110 | vssRays.clear();
|
---|
| 111 |
|
---|
[1743] | 112 | for (; rays.size() < 16; ) {
|
---|
[2035] | 113 | if (i%100000 == 0) {
|
---|
[2043] | 114 | cout<<samples/1e6f<<"M rays, progress: "<<(samples*100.0f)/mTotalSamples<<" % \r";
|
---|
[2035] | 115 | }
|
---|
[1743] | 116 |
|
---|
[1966] | 117 | dirHalton[viewcell].GetNext(2, r);
|
---|
| 118 | direction = UniformRandomVector(r[0],r[1]);
|
---|
| 119 | //direction = UniformRandomVector();
|
---|
[1743] | 120 |
|
---|
[1966] | 121 | // cast rays in both directions to make the number of samples comparable
|
---|
| 122 | // with the global sampling method which also casts a "double" ray per sample
|
---|
| 123 | rays.push_back(SimpleRay(origin,
|
---|
| 124 | direction,
|
---|
| 125 | SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
|
---|
| 126 | 1.0f));
|
---|
| 127 | i++;
|
---|
| 128 | samples++;
|
---|
[1743] | 129 | }
|
---|
[1199] | 130 |
|
---|
[1743] | 131 | CastRays(rays,
|
---|
| 132 | vssRays,
|
---|
| 133 | true,
|
---|
[1966] | 134 | true);
|
---|
| 135 |
|
---|
[1743] | 136 |
|
---|
[2130] | 137 | rssSamples += (int)vssRays.size();
|
---|
[1771] | 138 | for (int j=0; j < vssRays.size(); j++)
|
---|
| 139 | if (vssRays[j]->mFlags & VssRay::Valid) {
|
---|
[2022] | 140 | Intersectable *obj = vssRays[j]->mTerminationObject;
|
---|
[1771] | 141 | if (obj) {
|
---|
| 142 | // if ray not outside of view space
|
---|
[1743] | 143 | float pdf = 1.0f;
|
---|
[1966] | 144 | // ViewCell *viewcell = viewcells[j/2];
|
---|
[2035] | 145 |
|
---|
[1966] | 146 | ObjectPvs &pvs = viewcell->GetPvs();
|
---|
| 147 | pvs.AddSampleDirtyCheck(obj, pdf);
|
---|
| 148 |
|
---|
| 149 | if (pvs.RequiresResort()) {
|
---|
| 150 | pvs.SimpleSort();
|
---|
| 151 | }
|
---|
[1771] | 152 | }
|
---|
[372] | 153 | }
|
---|
[1199] | 154 |
|
---|
| 155 | if (samples > mTotalSamples)
|
---|
| 156 | break;
|
---|
[372] | 157 | }
|
---|
[1743] | 158 |
|
---|
[2035] | 159 |
|
---|
| 160 | if (samples - lastEvaluation >= mSamplesPerEvaluation) {
|
---|
[2066] | 161 | Real time = TimeDiff(startTime, GetTime());
|
---|
[2035] | 162 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 163 | mStats <<
|
---|
| 164 | "#Pass\n" <<mPass<<endl<<
|
---|
[2046] | 165 | "#Time\n" << time <<endl<<
|
---|
[2035] | 166 | "#TotalSamples\n" <<samples<<endl<<
|
---|
| 167 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 168 | lastEvaluation = samples;
|
---|
[2046] | 169 |
|
---|
[2035] | 170 | if (renderer) {
|
---|
| 171 | ComputeRenderError();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | }
|
---|
[2049] | 175 |
|
---|
[2066] | 176 | Real time = TimeDiff(startTime, GetTime());
|
---|
[2049] | 177 | if (mTotalTime != -1 && time/1000 > mTotalTime)
|
---|
| 178 | break;
|
---|
| 179 |
|
---|
[1199] | 180 | }
|
---|
[1715] | 181 | if (0) {
|
---|
| 182 | Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
|
---|
| 183 | exporter->SetExportRayDensity(true);
|
---|
| 184 | exporter->ExportKdTree(*mKdTree);
|
---|
| 185 | delete exporter;
|
---|
| 186 | }
|
---|
[2035] | 187 |
|
---|
| 188 |
|
---|
| 189 | // $$JB temporary removed
|
---|
[1715] | 190 | // mViewCellsManager->PostProcess(objects, mSampleRays);
|
---|
| 191 |
|
---|
| 192 | //-- several visualizations and statistics
|
---|
| 193 | Debug << "view cells after post processing: " << endl;
|
---|
| 194 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 195 |
|
---|
[1771] | 196 | EvalViewCellHistogram();
|
---|
| 197 |
|
---|
[1715] | 198 | //-- render simulation after merge
|
---|
| 199 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
| 200 |
|
---|
| 201 | mRenderSimulator->RenderScene();
|
---|
| 202 | SimulationStatistics ss;
|
---|
| 203 | mRenderSimulator->GetStatistics(ss);
|
---|
| 204 |
|
---|
| 205 | cout << " finished" << endl;
|
---|
| 206 | cout << ss << endl;
|
---|
| 207 | Debug << ss << endl;
|
---|
| 208 |
|
---|
| 209 | // $$JB temporary removed
|
---|
[466] | 210 | //mViewCellsManager->Visualize(objects, mSampleRays);
|
---|
[1715] | 211 |
|
---|
| 212 | return true;
|
---|
[362] | 213 | }
|
---|
[349] | 214 |
|
---|
[441] | 215 |
|
---|
[362] | 216 | }
|
---|