[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 | {
|
---|
[1579] | 82 | #if 0 // 6.10. 2006 due to kdPVS removal from intersectable
|
---|
| 83 | // mail all nodes from the pvs
|
---|
[1199] | 84 | Intersectable::NewMail();
|
---|
| 85 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
|
---|
| 86 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 87 | KdNode *node = (*i).first;
|
---|
| 88 | node->Mail();
|
---|
| 89 | }
|
---|
| 90 | Debug << "Get all neighbours from PVS" << endl;
|
---|
| 91 | vector<KdNode *> invisibleNeighbors;
|
---|
| 92 | // get all neighbors of all PVS nodes
|
---|
| 93 | i = object->mKdPvs.mEntries.begin();
|
---|
| 94 | for (; i != object->mKdPvs.mEntries.end(); i++) {
|
---|
| 95 | KdNode *node = (*i).first;
|
---|
| 96 | mKdTree->FindNeighbors(node, invisibleNeighbors, true);
|
---|
| 97 | AxisAlignedBox3 box = object->GetBox();
|
---|
| 98 | for (int j=0; j < invisibleNeighbors.size(); j++) {
|
---|
| 99 | int visibility = ComputeBoxVisibility(mSceneGraph,
|
---|
| 100 | mKdTree,
|
---|
| 101 | box,
|
---|
| 102 | mKdTree->GetBox(invisibleNeighbors[j]),
|
---|
| 103 | 1e-6f);
|
---|
| 104 | // exit(0);
|
---|
[372] | 105 | }
|
---|
[1199] | 106 | // now rank all the neighbors according to probability that a new
|
---|
| 107 | // sample creates some contribution
|
---|
| 108 | }
|
---|
[1579] | 109 | #endif
|
---|
[372] | 110 | }
|
---|
| 111 |
|
---|
| 112 | bool
|
---|
| 113 | SamplingPreprocessor::ComputeVisibility()
|
---|
| 114 | {
|
---|
| 115 |
|
---|
[1199] | 116 | Debug << "type: sampling" << endl;
|
---|
[487] | 117 |
|
---|
[1199] | 118 | cout<<"Sampling Preprocessor started\n"<<flush;
|
---|
| 119 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[372] | 120 |
|
---|
[1199] | 121 | Randomize(0);
|
---|
[1563] | 122 | const long startTime = GetTime();
|
---|
[372] | 123 | int totalSamples = 0;
|
---|
| 124 |
|
---|
[1199] | 125 | // if not already loaded, construct view cells from file
|
---|
[1715] | 126 | if (!mLoadViewCells) {
|
---|
| 127 | ConstructViewCells();
|
---|
[1199] | 128 | }
|
---|
[1563] | 129 |
|
---|
[1199] | 130 | int samples = 0;
|
---|
| 131 | int i=0;
|
---|
| 132 | while (samples < mTotalSamples) {
|
---|
| 133 | for (i=0; i < mSamplesPerPass; i++, samples++) {
|
---|
| 134 |
|
---|
| 135 | if (i%10000 == 0)
|
---|
| 136 | cout<<"+";
|
---|
| 137 |
|
---|
| 138 | Vector3 origin, direction;
|
---|
| 139 | mViewCellsManager->GetViewPoint(origin);
|
---|
| 140 | direction = UniformRandomVector();
|
---|
[372] | 141 |
|
---|
[1199] | 142 |
|
---|
| 143 | ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
|
---|
| 144 |
|
---|
| 145 | if (viewcell && viewcell->GetValid()) {
|
---|
| 146 | // cast rays in both directions to make the number of samples comparable
|
---|
| 147 | // with the global sampling method which also casts a "double" ray per sample
|
---|
| 148 | for (int j=0; j < 2; j++) {
|
---|
| 149 | Intersectable *object = CastRay(origin, direction);
|
---|
| 150 | if (object) {
|
---|
| 151 | // if ray not outside of view space
|
---|
| 152 | float contribution;
|
---|
| 153 | int pvsContribution = 0;
|
---|
| 154 | float relativePvsContribution = 0;
|
---|
| 155 | if (object) {
|
---|
| 156 | float pdf = 1.0f;
|
---|
| 157 | if (viewcell->GetPvs().GetSampleContribution(object,
|
---|
| 158 | pdf,
|
---|
| 159 | contribution))
|
---|
| 160 | ++pvsContribution;
|
---|
| 161 | relativePvsContribution += contribution;
|
---|
| 162 | viewcell->GetPvs().AddSample(object, pdf);
|
---|
[372] | 163 | }
|
---|
[1199] | 164 | }
|
---|
| 165 | direction = -direction;
|
---|
[372] | 166 | }
|
---|
[1199] | 167 | }
|
---|
| 168 |
|
---|
| 169 | if (samples > mTotalSamples)
|
---|
| 170 | break;
|
---|
[372] | 171 | }
|
---|
| 172 |
|
---|
[1199] | 173 | // mVssRays.PrintStatistics(mStats);
|
---|
| 174 | mStats <<
|
---|
| 175 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 176 | "#TotalSamples\n" <<samples<<endl;
|
---|
| 177 |
|
---|
| 178 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 179 | // ComputeRenderError();
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[1715] | 182 | if (0) {
|
---|
| 183 | Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
|
---|
| 184 | exporter->SetExportRayDensity(true);
|
---|
| 185 | exporter->ExportKdTree(*mKdTree);
|
---|
| 186 | delete exporter;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | // $$JB temporary removed
|
---|
| 191 | // mViewCellsManager->PostProcess(objects, mSampleRays);
|
---|
| 192 |
|
---|
| 193 | //-- several visualizations and statistics
|
---|
| 194 | Debug << "view cells after post processing: " << endl;
|
---|
| 195 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 196 |
|
---|
| 197 | //-- render simulation after merge
|
---|
| 198 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
| 199 |
|
---|
| 200 | mRenderSimulator->RenderScene();
|
---|
| 201 | SimulationStatistics ss;
|
---|
| 202 | mRenderSimulator->GetStatistics(ss);
|
---|
| 203 |
|
---|
| 204 | cout << " finished" << endl;
|
---|
| 205 | cout << ss << endl;
|
---|
| 206 | Debug << ss << endl;
|
---|
| 207 |
|
---|
| 208 | // $$JB temporary removed
|
---|
[466] | 209 | //mViewCellsManager->Visualize(objects, mSampleRays);
|
---|
[1715] | 210 |
|
---|
| 211 | return true;
|
---|
[362] | 212 | }
|
---|
[349] | 213 |
|
---|
[441] | 214 |
|
---|
[362] | 215 | }
|
---|