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 | #if 0
|
---|
50 | object = CastSimpleRay(origin,
|
---|
51 | direction,
|
---|
52 | mKdTree->GetBox(),
|
---|
53 | point,
|
---|
54 | normal
|
---|
55 | );
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | if (mDetectEmptyViewSpace && DotProd(normal, direction) >= 0) {
|
---|
59 | object = NULL;
|
---|
60 | }
|
---|
61 |
|
---|
62 | return object;
|
---|
63 | }
|
---|
64 |
|
---|
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 |
|
---|
78 |
|
---|
79 | void
|
---|
80 | SamplingPreprocessor::VerifyVisibility(Intersectable *object)
|
---|
81 | {
|
---|
82 | #if 0 // 6.10. 2006 due to kdPVS removal from intersectable
|
---|
83 | // mail all nodes from the pvs
|
---|
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);
|
---|
105 | }
|
---|
106 | // now rank all the neighbors according to probability that a new
|
---|
107 | // sample creates some contribution
|
---|
108 | }
|
---|
109 | #endif
|
---|
110 | }
|
---|
111 |
|
---|
112 | bool
|
---|
113 | SamplingPreprocessor::ComputeVisibility()
|
---|
114 | {
|
---|
115 |
|
---|
116 | Debug << "type: sampling" << endl;
|
---|
117 |
|
---|
118 | cout<<"Sampling Preprocessor started\n"<<flush;
|
---|
119 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
120 |
|
---|
121 | Randomize(0);
|
---|
122 | const long startTime = GetTime();
|
---|
123 | int totalSamples = 0;
|
---|
124 |
|
---|
125 | // if not already loaded, construct view cells from file
|
---|
126 | if (!mLoadViewCells)
|
---|
127 | {
|
---|
128 | ConstructViewCells();
|
---|
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 | }
|
---|