1 | #include "SceneGraph.h"
|
---|
2 | #include "KdTree.h"
|
---|
3 | #include "VssPreprocessor.h"
|
---|
4 | #include "X3dExporter.h"
|
---|
5 | #include "Environment.h"
|
---|
6 | #include "MutualVisibility.h"
|
---|
7 | #include "Polygon3.h"
|
---|
8 | #include "ViewCell.h"
|
---|
9 | #include "VssRay.h"
|
---|
10 |
|
---|
11 | VssPreprocessor::VssPreprocessor():
|
---|
12 | mPass(0),
|
---|
13 | mVssRays()
|
---|
14 | {
|
---|
15 | // this should increase coherence of the samples
|
---|
16 | environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
17 | environment->GetIntValue("VssPreprocessor.totalSamples", mTotalSamples);
|
---|
18 | mStats.open("stats.log");
|
---|
19 | }
|
---|
20 |
|
---|
21 | VssPreprocessor::~VssPreprocessor()
|
---|
22 | {
|
---|
23 | CLEAR_CONTAINER(mVssRays);
|
---|
24 | }
|
---|
25 |
|
---|
26 | void
|
---|
27 | VssPreprocessor::SetupRay(Ray &ray,
|
---|
28 | const Vector3 &point,
|
---|
29 | const Vector3 &direction
|
---|
30 | )
|
---|
31 | {
|
---|
32 | ray.intersections.clear();
|
---|
33 | // do not store anything else then intersections at the ray
|
---|
34 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
35 | }
|
---|
36 |
|
---|
37 | VssRay *
|
---|
38 | VssPreprocessor::CastRay(
|
---|
39 | Vector3 &viewPoint,
|
---|
40 | Vector3 &direction
|
---|
41 | )
|
---|
42 | {
|
---|
43 | static Ray ray;
|
---|
44 | AxisAlignedBox3 box = mKdTree->GetBox();
|
---|
45 |
|
---|
46 | SetupRay(ray, viewPoint, direction);
|
---|
47 | // cast ray to KD tree to find intersection with other objects
|
---|
48 | Intersectable *objectA, *objectB;
|
---|
49 | Vector3 pointA, pointB;
|
---|
50 |
|
---|
51 | if (mKdTree->CastRay(ray)) {
|
---|
52 | objectA = ray.intersections[0].mObject;
|
---|
53 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
54 | } else {
|
---|
55 | objectA = NULL;
|
---|
56 | // compute intersection with the scene bounding box
|
---|
57 | float tmin, tmax;
|
---|
58 | box.ComputeMinMaxT(ray, &tmin, &tmax);
|
---|
59 | pointA = ray.Extrap(tmax);
|
---|
60 | }
|
---|
61 |
|
---|
62 | SetupRay(ray, viewPoint, -direction);
|
---|
63 |
|
---|
64 | if (mKdTree->CastRay(ray)) {
|
---|
65 | objectB = ray.intersections[0].mObject;
|
---|
66 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
67 | } else {
|
---|
68 | objectB = NULL;
|
---|
69 | float tmin, tmax;
|
---|
70 | box.ComputeMinMaxT(ray, &tmin, &tmax);
|
---|
71 | pointB = ray.Extrap(tmax);
|
---|
72 | }
|
---|
73 |
|
---|
74 | VssRay *vssRay = NULL;
|
---|
75 |
|
---|
76 | if (objectA || objectB) {
|
---|
77 | vssRay = new VssRay(pointA,
|
---|
78 | pointB,
|
---|
79 | objectA,
|
---|
80 | objectB);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return vssRay;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | Vector3
|
---|
88 | VssPreprocessor::GetViewpoint()
|
---|
89 | {
|
---|
90 | AxisAlignedBox3 box = mKdTree->GetBox();
|
---|
91 |
|
---|
92 | // shrink the box in the y direction
|
---|
93 | Vector3 diff(0, -box.Size().y*0.4f, 0);
|
---|
94 | box.Enlarge(diff);
|
---|
95 |
|
---|
96 | return box.GetRandomPoint();
|
---|
97 | }
|
---|
98 |
|
---|
99 | Vector3
|
---|
100 | VssPreprocessor::GetDirection(const Vector3 &viewpoint)
|
---|
101 | {
|
---|
102 | int i = RandomValue(0, mObjects.size());
|
---|
103 | Intersectable *object = mObjects[i];
|
---|
104 | Vector3 point, normal;
|
---|
105 | object->GetRandomSurfacePoint(point, normal);
|
---|
106 | return point - viewpoint;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | bool
|
---|
112 | VssPreprocessor::ComputeVisibility()
|
---|
113 | {
|
---|
114 |
|
---|
115 | mSceneGraph->CollectObjects(&mObjects);
|
---|
116 |
|
---|
117 |
|
---|
118 | long startTime = GetTime();
|
---|
119 |
|
---|
120 | int i;
|
---|
121 | int totalSamples = 0;
|
---|
122 |
|
---|
123 |
|
---|
124 | while (totalSamples < mTotalSamples) {
|
---|
125 | int passContributingSamples = 0;
|
---|
126 | int passSampleContributions = 0;
|
---|
127 | int passSamples = 0;
|
---|
128 | int index = 0;
|
---|
129 |
|
---|
130 | int sampleContributions;
|
---|
131 |
|
---|
132 |
|
---|
133 | for (int k=0; k < mSamplesPerPass; k++) {
|
---|
134 |
|
---|
135 | Vector3 viewpoint = GetViewpoint();
|
---|
136 | Vector3 direction = GetDirection(viewpoint);
|
---|
137 |
|
---|
138 | VssRay *vssRay = CastRay(viewpoint, direction);
|
---|
139 |
|
---|
140 | if (vssRay) {
|
---|
141 | sampleContributions = vssRay->HitCount();
|
---|
142 | mVssRays.push_back(vssRay);
|
---|
143 | }
|
---|
144 |
|
---|
145 | //-- CORR matt: put block inside loop
|
---|
146 | if (sampleContributions) {
|
---|
147 | passContributingSamples ++;
|
---|
148 | passSampleContributions += sampleContributions;
|
---|
149 | }
|
---|
150 | passSamples++;
|
---|
151 | totalSamples++;
|
---|
152 | }
|
---|
153 |
|
---|
154 | mPass++;
|
---|
155 |
|
---|
156 | int pvsSize = 0;
|
---|
157 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
158 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
159 |
|
---|
160 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
161 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
162 | << "k #SampleContributions=" << passSampleContributions << " ("
|
---|
163 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
164 | << pvsSize/(float)mObjects.size() << endl
|
---|
165 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
166 |
|
---|
167 | mStats <<
|
---|
168 | "#Pass\n" <<mPass<<endl<<
|
---|
169 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
170 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
171 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
172 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
173 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
174 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
175 | }
|
---|
176 |
|
---|
177 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
178 | cout << "#totalRayStackSize=" << mVssRays.size() << endl;
|
---|
179 |
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|