[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"
|
---|
[372] | 11 |
|
---|
[860] | 12 |
|
---|
[1221] | 13 |
|
---|
[863] | 14 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 15 |
|
---|
| 16 |
|
---|
[1292] | 17 | SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
|
---|
[372] | 18 | {
|
---|
| 19 | // this should increase coherence of the samples
|
---|
[1199] | 20 | Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 21 | Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples);
|
---|
| 22 |
|
---|
[372] | 23 | mStats.open("stats.log");
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | SamplingPreprocessor::~SamplingPreprocessor()
|
---|
| 27 | {
|
---|
[1199] | 28 | CLEAR_CONTAINER(mSampleRays);
|
---|
| 29 | CLEAR_CONTAINER(mVssSampleRays);
|
---|
[372] | 30 | }
|
---|
| 31 |
|
---|
[1199] | 32 | Intersectable *
|
---|
| 33 | SamplingPreprocessor::CastRay(
|
---|
| 34 | const Vector3 &origin,
|
---|
| 35 | const Vector3 &direction
|
---|
| 36 | )
|
---|
[372] | 37 | {
|
---|
[1199] | 38 | AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
|
---|
[440] | 39 |
|
---|
[1199] | 40 | AxisAlignedBox3 sbox = box;
|
---|
| 41 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
| 42 | if (!sbox.IsInside(origin))
|
---|
| 43 | return 0;
|
---|
[1292] | 44 |
|
---|
| 45 | Vector3 point, normal;
|
---|
| 46 | Intersectable *object;
|
---|
[444] | 47 |
|
---|
[1199] | 48 | // cast ray to KD tree to find intersection with other objects
|
---|
[1520] | 49 | #if 0
|
---|
[1292] | 50 | object = CastSimpleRay(origin,
|
---|
| 51 | direction,
|
---|
| 52 | mKdTree->GetBox(),
|
---|
| 53 | point,
|
---|
| 54 | normal
|
---|
| 55 | );
|
---|
[1520] | 56 | #endif
|
---|
| 57 |
|
---|
[1292] | 58 | if (mDetectEmptyViewSpace && DotProd(normal, direction) >= 0) {
|
---|
| 59 | object = NULL;
|
---|
[1199] | 60 | }
|
---|
[1292] | 61 |
|
---|
| 62 | return object;
|
---|
[372] | 63 | }
|
---|
| 64 |
|
---|
[429] | 65 | void
|
---|
| 66 | SamplingPreprocessor::HoleSamplingPass()
|
---|
| 67 | {
|
---|
| 68 | vector<KdLeaf *> leaves;
|
---|
| 69 | mKdTree->CollectLeaves(leaves);
|
---|
| 70 |
|
---|
| 71 | // go through all the leaves and evaluate their passing contribution
|
---|
| 72 | for (int i=0 ; i < leaves.size(); i++) {
|
---|
| 73 | KdLeaf *leaf = leaves[i];
|
---|
| 74 | cout<<leaf->mPassingRays<<endl;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[372] | 78 |
|
---|
| 79 | void
|
---|
| 80 | SamplingPreprocessor::VerifyVisibility(Intersectable *object)
|
---|
| 81 | {
|
---|
| 82 | // mail all nodes from the pvs
|
---|
[1199] | 83 | Intersectable::NewMail();
|
---|
| 84 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
|
---|
| 85 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 86 | KdNode *node = (*i).first;
|
---|
| 87 | node->Mail();
|
---|
| 88 | }
|
---|
| 89 | Debug << "Get all neighbours from PVS" << endl;
|
---|
| 90 | vector<KdNode *> invisibleNeighbors;
|
---|
| 91 | // get all neighbors of all PVS nodes
|
---|
| 92 | i = object->mKdPvs.mEntries.begin();
|
---|
| 93 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 94 | KdNode *node = (*i).first;
|
---|
| 95 | mKdTree->FindNeighbors(node, invisibleNeighbors, true);
|
---|
| 96 | AxisAlignedBox3 box = object->GetBox();
|
---|
| 97 | for (int j=0; j < invisibleNeighbors.size(); j++) {
|
---|
| 98 | int visibility = ComputeBoxVisibility(mSceneGraph,
|
---|
| 99 | mKdTree,
|
---|
| 100 | box,
|
---|
| 101 | mKdTree->GetBox(invisibleNeighbors[j]),
|
---|
| 102 | 1e-6f);
|
---|
| 103 | // exit(0);
|
---|
[372] | 104 | }
|
---|
[1199] | 105 | // now rank all the neighbors according to probability that a new
|
---|
| 106 | // sample creates some contribution
|
---|
| 107 | }
|
---|
[372] | 108 | }
|
---|
| 109 |
|
---|
| 110 | bool
|
---|
| 111 | SamplingPreprocessor::ComputeVisibility()
|
---|
| 112 | {
|
---|
| 113 |
|
---|
[1199] | 114 | Debug << "type: sampling" << endl;
|
---|
[487] | 115 |
|
---|
[1199] | 116 | cout<<"Sampling Preprocessor started\n"<<flush;
|
---|
| 117 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[372] | 118 |
|
---|
[1199] | 119 | Randomize(0);
|
---|
[1563] | 120 | const long startTime = GetTime();
|
---|
[372] | 121 | int totalSamples = 0;
|
---|
| 122 |
|
---|
[1199] | 123 | // if not already loaded, construct view cells from file
|
---|
| 124 | if (!mLoadViewCells)
|
---|
| 125 | {
|
---|
[1563] | 126 | ConstructViewCells();
|
---|
[1199] | 127 | }
|
---|
[1563] | 128 |
|
---|
[1199] | 129 | int samples = 0;
|
---|
| 130 | int i=0;
|
---|
| 131 | while (samples < mTotalSamples) {
|
---|
| 132 | for (i=0; i < mSamplesPerPass; i++, samples++) {
|
---|
| 133 |
|
---|
| 134 | if (i%10000 == 0)
|
---|
| 135 | cout<<"+";
|
---|
| 136 |
|
---|
| 137 | Vector3 origin, direction;
|
---|
| 138 | mViewCellsManager->GetViewPoint(origin);
|
---|
| 139 | direction = UniformRandomVector();
|
---|
[372] | 140 |
|
---|
[1199] | 141 |
|
---|
| 142 | ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
|
---|
| 143 |
|
---|
| 144 | if (viewcell && viewcell->GetValid()) {
|
---|
| 145 | // cast rays in both directions to make the number of samples comparable
|
---|
| 146 | // with the global sampling method which also casts a "double" ray per sample
|
---|
| 147 | for (int j=0; j < 2; j++) {
|
---|
| 148 | Intersectable *object = CastRay(origin, direction);
|
---|
| 149 | if (object) {
|
---|
| 150 | // if ray not outside of view space
|
---|
| 151 | float contribution;
|
---|
| 152 | int pvsContribution = 0;
|
---|
| 153 | float relativePvsContribution = 0;
|
---|
| 154 | if (object) {
|
---|
| 155 | float pdf = 1.0f;
|
---|
| 156 | if (viewcell->GetPvs().GetSampleContribution(object,
|
---|
| 157 | pdf,
|
---|
| 158 | contribution))
|
---|
| 159 | ++pvsContribution;
|
---|
| 160 | relativePvsContribution += contribution;
|
---|
| 161 | viewcell->GetPvs().AddSample(object, pdf);
|
---|
[372] | 162 | }
|
---|
[1199] | 163 | }
|
---|
| 164 | direction = -direction;
|
---|
[372] | 165 | }
|
---|
[1199] | 166 | }
|
---|
| 167 |
|
---|
| 168 | if (samples > mTotalSamples)
|
---|
| 169 | break;
|
---|
[372] | 170 | }
|
---|
| 171 |
|
---|
[1199] | 172 | // mVssRays.PrintStatistics(mStats);
|
---|
| 173 | mStats <<
|
---|
| 174 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 175 | "#TotalSamples\n" <<samples<<endl;
|
---|
| 176 |
|
---|
| 177 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 178 | // ComputeRenderError();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[372] | 181 | // HoleSamplingPass();
|
---|
| 182 | if (0) {
|
---|
[1199] | 183 | Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
|
---|
| 184 | exporter->SetExportRayDensity(true);
|
---|
| 185 | exporter->ExportKdTree(*mKdTree);
|
---|
| 186 | delete exporter;
|
---|
[422] | 187 | }
|
---|
[452] | 188 |
|
---|
[1199] | 189 |
|
---|
[466] | 190 | // $$JB temporary removed
|
---|
| 191 | // mViewCellsManager->PostProcess(objects, mSampleRays);
|
---|
[452] | 192 |
|
---|
| 193 | //-- several visualizations and statistics
|
---|
[475] | 194 | Debug << "view cells after post processing: " << endl;
|
---|
[452] | 195 | mViewCellsManager->PrintStatistics(Debug);
|
---|
[1199] | 196 |
|
---|
[448] | 197 | //-- render simulation after merge
|
---|
| 198 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
| 199 |
|
---|
[1199] | 200 | mRenderSimulator->RenderScene();
|
---|
| 201 | SimulationStatistics ss;
|
---|
| 202 | mRenderSimulator->GetStatistics(ss);
|
---|
[468] | 203 |
|
---|
[1199] | 204 | cout << " finished" << endl;
|
---|
| 205 | cout << ss << endl;
|
---|
| 206 | Debug << ss << endl;
|
---|
| 207 |
|
---|
[466] | 208 | // $$JB temporary removed
|
---|
| 209 | //mViewCellsManager->Visualize(objects, mSampleRays);
|
---|
[1199] | 210 |
|
---|
[441] | 211 | return true;
|
---|
[362] | 212 | }
|
---|
[349] | 213 |
|
---|
[441] | 214 |
|
---|
[362] | 215 | }
|
---|