[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 | {
|
---|
| 69 |
|
---|
[1199] | 70 | Debug << "type: sampling" << endl;
|
---|
[487] | 71 |
|
---|
[1199] | 72 | cout<<"Sampling Preprocessor started\n"<<flush;
|
---|
| 73 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[372] | 74 |
|
---|
[1199] | 75 | Randomize(0);
|
---|
[1563] | 76 | const long startTime = GetTime();
|
---|
[372] | 77 | int totalSamples = 0;
|
---|
| 78 |
|
---|
[1199] | 79 | // if not already loaded, construct view cells from file
|
---|
[1715] | 80 | if (!mLoadViewCells) {
|
---|
| 81 | ConstructViewCells();
|
---|
[1199] | 82 | }
|
---|
[1743] | 83 |
|
---|
| 84 | int intersectables, faces;
|
---|
| 85 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
[1966] | 86 | HaltonSequence posHalton;
|
---|
[1743] | 87 |
|
---|
[1199] | 88 | int samples = 0;
|
---|
[1966] | 89 | int rssSamples = 0;
|
---|
| 90 |
|
---|
| 91 | map<ViewCell *, HaltonSequence> dirHalton;
|
---|
[2035] | 92 | int lastEvaluation = 0;
|
---|
| 93 | SimpleRayContainer rays;
|
---|
| 94 | VssRayContainer vssRays;
|
---|
| 95 |
|
---|
[1199] | 96 | while (samples < mTotalSamples) {
|
---|
[2035] | 97 | for (int i=0; i < mSamplesPerPass;) {
|
---|
[1966] | 98 |
|
---|
[2035] | 99 | mRayCaster->InitPass();
|
---|
| 100 |
|
---|
[1966] | 101 | float r[5];
|
---|
| 102 | Vector3 origin, direction;
|
---|
| 103 | posHalton.GetNext(3, r);
|
---|
| 104 |
|
---|
| 105 | mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
|
---|
| 106 | ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
|
---|
| 107 |
|
---|
| 108 | if (!viewcell || !viewcell->GetValid())
|
---|
| 109 | continue;
|
---|
[2035] | 110 |
|
---|
| 111 | rays.clear();
|
---|
| 112 | vssRays.clear();
|
---|
| 113 |
|
---|
[1743] | 114 | for (; rays.size() < 16; ) {
|
---|
[2035] | 115 | if (i%100000 == 0) {
|
---|
[2043] | 116 | cout<<samples/1e6f<<"M rays, progress: "<<(samples*100.0f)/mTotalSamples<<" % \r";
|
---|
[2035] | 117 | }
|
---|
[1743] | 118 |
|
---|
[1966] | 119 | dirHalton[viewcell].GetNext(2, r);
|
---|
| 120 | direction = UniformRandomVector(r[0],r[1]);
|
---|
| 121 | //direction = UniformRandomVector();
|
---|
[1743] | 122 |
|
---|
[1966] | 123 | // cast rays in both directions to make the number of samples comparable
|
---|
| 124 | // with the global sampling method which also casts a "double" ray per sample
|
---|
| 125 | rays.push_back(SimpleRay(origin,
|
---|
| 126 | direction,
|
---|
| 127 | SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
|
---|
| 128 | 1.0f));
|
---|
| 129 | i++;
|
---|
| 130 | samples++;
|
---|
[1743] | 131 | }
|
---|
[1199] | 132 |
|
---|
[1743] | 133 | CastRays(rays,
|
---|
| 134 | vssRays,
|
---|
| 135 | true,
|
---|
[1966] | 136 | true);
|
---|
| 137 |
|
---|
[1743] | 138 |
|
---|
[2130] | 139 | rssSamples += (int)vssRays.size();
|
---|
[1771] | 140 | for (int j=0; j < vssRays.size(); j++)
|
---|
| 141 | if (vssRays[j]->mFlags & VssRay::Valid) {
|
---|
[2022] | 142 | Intersectable *obj = vssRays[j]->mTerminationObject;
|
---|
| 143 |
|
---|
[1771] | 144 | if (obj) {
|
---|
| 145 | // if ray not outside of view space
|
---|
[1743] | 146 | float pdf = 1.0f;
|
---|
[1966] | 147 | // ViewCell *viewcell = viewcells[j/2];
|
---|
[2035] | 148 |
|
---|
[1966] | 149 | ObjectPvs &pvs = viewcell->GetPvs();
|
---|
| 150 | pvs.AddSampleDirtyCheck(obj, pdf);
|
---|
| 151 |
|
---|
| 152 | if (pvs.RequiresResort()) {
|
---|
| 153 | pvs.SimpleSort();
|
---|
| 154 | }
|
---|
[1771] | 155 | }
|
---|
[372] | 156 | }
|
---|
[1199] | 157 |
|
---|
| 158 | if (samples > mTotalSamples)
|
---|
| 159 | break;
|
---|
[372] | 160 | }
|
---|
[1743] | 161 |
|
---|
[2035] | 162 |
|
---|
| 163 | if (samples - lastEvaluation >= mSamplesPerEvaluation) {
|
---|
[2066] | 164 | Real time = TimeDiff(startTime, GetTime());
|
---|
[2035] | 165 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 166 | mStats <<
|
---|
| 167 | "#Pass\n" <<mPass<<endl<<
|
---|
[2046] | 168 | "#Time\n" << time <<endl<<
|
---|
[2035] | 169 | "#TotalSamples\n" <<samples<<endl<<
|
---|
| 170 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 171 | lastEvaluation = samples;
|
---|
[2046] | 172 |
|
---|
[2035] | 173 | if (renderer) {
|
---|
| 174 | ComputeRenderError();
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | }
|
---|
[2049] | 178 |
|
---|
[2066] | 179 | Real time = TimeDiff(startTime, GetTime());
|
---|
[2049] | 180 | if (mTotalTime != -1 && time/1000 > mTotalTime)
|
---|
| 181 | break;
|
---|
| 182 |
|
---|
[1199] | 183 | }
|
---|
[1715] | 184 | if (0) {
|
---|
| 185 | Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
|
---|
| 186 | exporter->SetExportRayDensity(true);
|
---|
| 187 | exporter->ExportKdTree(*mKdTree);
|
---|
| 188 | delete exporter;
|
---|
| 189 | }
|
---|
[2035] | 190 |
|
---|
| 191 |
|
---|
| 192 | // $$JB temporary removed
|
---|
[1715] | 193 | // mViewCellsManager->PostProcess(objects, mSampleRays);
|
---|
| 194 |
|
---|
| 195 | //-- several visualizations and statistics
|
---|
| 196 | Debug << "view cells after post processing: " << endl;
|
---|
| 197 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 198 |
|
---|
[1771] | 199 | EvalViewCellHistogram();
|
---|
| 200 |
|
---|
[1715] | 201 | //-- render simulation after merge
|
---|
| 202 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
| 203 |
|
---|
| 204 | mRenderSimulator->RenderScene();
|
---|
| 205 | SimulationStatistics ss;
|
---|
| 206 | mRenderSimulator->GetStatistics(ss);
|
---|
| 207 |
|
---|
| 208 | cout << " finished" << endl;
|
---|
| 209 | cout << ss << endl;
|
---|
| 210 | Debug << ss << endl;
|
---|
| 211 |
|
---|
| 212 | // $$JB temporary removed
|
---|
[466] | 213 | //mViewCellsManager->Visualize(objects, mSampleRays);
|
---|
[1715] | 214 |
|
---|
| 215 | return true;
|
---|
[362] | 216 | }
|
---|
[349] | 217 |
|
---|
[441] | 218 |
|
---|
[362] | 219 | }
|
---|