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