[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"
|
---|
[372] | 13 |
|
---|
[860] | 14 |
|
---|
[1221] | 15 |
|
---|
[863] | 16 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 17 |
|
---|
| 18 |
|
---|
[1292] | 19 | SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
|
---|
[372] | 20 | {
|
---|
| 21 | // this should increase coherence of the samples
|
---|
[1199] | 22 |
|
---|
[372] | 23 | }
|
---|
| 24 |
|
---|
| 25 | SamplingPreprocessor::~SamplingPreprocessor()
|
---|
| 26 | {
|
---|
[1199] | 27 | CLEAR_CONTAINER(mSampleRays);
|
---|
| 28 | CLEAR_CONTAINER(mVssSampleRays);
|
---|
[372] | 29 | }
|
---|
| 30 |
|
---|
[444] | 31 |
|
---|
[1520] | 32 |
|
---|
[372] | 33 |
|
---|
[429] | 34 | void
|
---|
[372] | 35 | SamplingPreprocessor::VerifyVisibility(Intersectable *object)
|
---|
| 36 | {
|
---|
[1579] | 37 | #if 0 // 6.10. 2006 due to kdPVS removal from intersectable
|
---|
| 38 | // mail all nodes from the pvs
|
---|
[1199] | 39 | Intersectable::NewMail();
|
---|
| 40 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
|
---|
| 41 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 42 | KdNode *node = (*i).first;
|
---|
| 43 | node->Mail();
|
---|
| 44 | }
|
---|
| 45 | Debug << "Get all neighbours from PVS" << endl;
|
---|
| 46 | vector<KdNode *> invisibleNeighbors;
|
---|
| 47 | // get all neighbors of all PVS nodes
|
---|
| 48 | i = object->mKdPvs.mEntries.begin();
|
---|
| 49 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 50 | KdNode *node = (*i).first;
|
---|
| 51 | mKdTree->FindNeighbors(node, invisibleNeighbors, true);
|
---|
| 52 | AxisAlignedBox3 box = object->GetBox();
|
---|
| 53 | for (int j=0; j < invisibleNeighbors.size(); j++) {
|
---|
| 54 | int visibility = ComputeBoxVisibility(mSceneGraph,
|
---|
| 55 | mKdTree,
|
---|
| 56 | box,
|
---|
| 57 | mKdTree->GetBox(invisibleNeighbors[j]),
|
---|
| 58 | 1e-6f);
|
---|
| 59 | // exit(0);
|
---|
[372] | 60 | }
|
---|
[1199] | 61 | // now rank all the neighbors according to probability that a new
|
---|
| 62 | // sample creates some contribution
|
---|
| 63 | }
|
---|
[1579] | 64 | #endif
|
---|
[372] | 65 | }
|
---|
| 66 |
|
---|
| 67 | bool
|
---|
| 68 | SamplingPreprocessor::ComputeVisibility()
|
---|
| 69 | {
|
---|
| 70 |
|
---|
[1199] | 71 | Debug << "type: sampling" << endl;
|
---|
[487] | 72 |
|
---|
[1199] | 73 | cout<<"Sampling Preprocessor started\n"<<flush;
|
---|
| 74 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[372] | 75 |
|
---|
[1199] | 76 | Randomize(0);
|
---|
[1563] | 77 | const long startTime = GetTime();
|
---|
[372] | 78 | int totalSamples = 0;
|
---|
| 79 |
|
---|
[1199] | 80 | // if not already loaded, construct view cells from file
|
---|
[1715] | 81 | if (!mLoadViewCells) {
|
---|
| 82 | ConstructViewCells();
|
---|
[1199] | 83 | }
|
---|
[1743] | 84 |
|
---|
| 85 | int intersectables, faces;
|
---|
| 86 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
[1966] | 87 | HaltonSequence posHalton;
|
---|
[1743] | 88 |
|
---|
[1199] | 89 | int samples = 0;
|
---|
[1966] | 90 | int rssSamples = 0;
|
---|
| 91 |
|
---|
| 92 | map<ViewCell *, HaltonSequence> dirHalton;
|
---|
| 93 |
|
---|
[1199] | 94 | while (samples < mTotalSamples) {
|
---|
[1966] | 95 | for (int i=0; i < mSamplesPerEvaluation;) {
|
---|
[1743] | 96 | SimpleRayContainer rays;
|
---|
| 97 | VssRayContainer vssRays;
|
---|
[1966] | 98 | // vector<ViewCell *> viewcells;
|
---|
| 99 |
|
---|
| 100 | float r[5];
|
---|
| 101 | Vector3 origin, direction;
|
---|
| 102 | posHalton.GetNext(3, r);
|
---|
| 103 |
|
---|
| 104 | mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
|
---|
| 105 | ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
|
---|
| 106 |
|
---|
| 107 | if (!viewcell || !viewcell->GetValid())
|
---|
| 108 | continue;
|
---|
| 109 |
|
---|
[1743] | 110 | for (; rays.size() < 16; ) {
|
---|
[1966] | 111 | if (i%100000 == 0)
|
---|
[1743] | 112 | cout<<"+";
|
---|
| 113 |
|
---|
[1966] | 114 | // dirHalton.GetNext(2, r);
|
---|
| 115 | dirHalton[viewcell].GetNext(2, r);
|
---|
| 116 | direction = UniformRandomVector(r[0],r[1]);
|
---|
| 117 | //direction = UniformRandomVector();
|
---|
[1743] | 118 |
|
---|
[1966] | 119 | // viewcells.push_back(viewcell);
|
---|
| 120 | // cast rays in both directions to make the number of samples comparable
|
---|
| 121 | // with the global sampling method which also casts a "double" ray per sample
|
---|
| 122 | rays.push_back(SimpleRay(origin,
|
---|
| 123 | direction,
|
---|
| 124 | SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
|
---|
| 125 | 1.0f));
|
---|
| 126 | i++;
|
---|
| 127 | samples++;
|
---|
[1743] | 128 | }
|
---|
[1199] | 129 |
|
---|
[1743] | 130 | CastRays(rays,
|
---|
| 131 | vssRays,
|
---|
| 132 | true,
|
---|
[1966] | 133 | true);
|
---|
| 134 |
|
---|
| 135 | // if (vssRays.size()!=32) {
|
---|
| 136 | // cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
|
---|
| 137 | // exit(1);
|
---|
| 138 | // }
|
---|
[1743] | 139 |
|
---|
[1966] | 140 | rssSamples+=vssRays.size();
|
---|
[1771] | 141 | for (int j=0; j < vssRays.size(); j++)
|
---|
| 142 | if (vssRays[j]->mFlags & VssRay::Valid) {
|
---|
| 143 | Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
|
---|
| 144 | true);
|
---|
| 145 | if (obj) {
|
---|
| 146 | // if ray not outside of view space
|
---|
[1743] | 147 | float pdf = 1.0f;
|
---|
[1966] | 148 | // ViewCell *viewcell = viewcells[j/2];
|
---|
| 149 |
|
---|
| 150 | ObjectPvs &pvs = viewcell->GetPvs();
|
---|
| 151 | pvs.AddSampleDirtyCheck(obj, pdf);
|
---|
| 152 |
|
---|
| 153 | if (pvs.RequiresResort()) {
|
---|
| 154 | pvs.SimpleSort();
|
---|
| 155 | }
|
---|
[1771] | 156 | }
|
---|
[372] | 157 | }
|
---|
[1199] | 158 |
|
---|
[1761] | 159 | CLEAR_CONTAINER(vssRays);
|
---|
[1966] | 160 |
|
---|
[1199] | 161 | if (samples > mTotalSamples)
|
---|
| 162 | break;
|
---|
[372] | 163 | }
|
---|
[1743] | 164 |
|
---|
[1757] | 165 | #if 0
|
---|
[1743] | 166 | Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
| 167 | mViewCellsManager->SetValidity(0, intersectables/2);
|
---|
| 168 | Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
[1757] | 169 | #endif
|
---|
| 170 |
|
---|
[1199] | 171 | // mVssRays.PrintStatistics(mStats);
|
---|
| 172 | mStats <<
|
---|
| 173 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
[1966] | 174 | "#TotalSamples\n" <<samples<<endl<<
|
---|
| 175 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 176 |
|
---|
[1199] | 177 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 178 | // ComputeRenderError();
|
---|
| 179 | }
|
---|
| 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 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | // $$JB temporary removed
|
---|
| 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 | }
|
---|