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