Commit a4c3a0e01aef71a8ec579362fd2491f4ed3c0a67
1 parent
f8a38243
added the neccessary changes for better modeling to shview. Some debug code. Iss…
…ue with weird symmetry
Showing
1 changed file
with
113 additions
and
10 deletions
Show diff stats
main.cpp
1 | 1 | #include <iostream> |
2 | +#include <sstream> | |
2 | 3 | |
3 | 4 | #include <GL/glut.h> |
4 | 5 | |
... | ... | @@ -6,6 +7,7 @@ |
6 | 7 | #include <stim/parser/arguments.h> |
7 | 8 | #include <stim/visualization/obj.h> |
8 | 9 | #include <stim/visualization/gl_spharmonics.h> |
10 | +#include <stim/math/constants.h> | |
9 | 11 | |
10 | 12 | #define theta_scale 0.01 |
11 | 13 | #define phi_scale 0.01 |
... | ... | @@ -119,6 +121,60 @@ void mouse_drag(int x, int y){ |
119 | 121 | glutPostRedisplay(); |
120 | 122 | } |
121 | 123 | |
124 | +float uniformRandom() | |
125 | +{ | |
126 | + return ( (float)(rand()))/( (float)(RAND_MAX)); | |
127 | +} | |
128 | + | |
129 | +std::vector<stim::vec3 <float> > | |
130 | +sample_sphere(int num_samples, float radius = 1.0) | |
131 | +{ | |
132 | + | |
133 | + float solidAngle = 2*stim::PI; ///Solid angle to sample over | |
134 | + float PHI[2], Z[2], range; ///Range of angles in cylinderical coordinates | |
135 | + PHI[0] = solidAngle/2; ///project the solid angle into spherical coords | |
136 | + PHI[1] = asin(0); /// | |
137 | + Z[0] = cos(PHI[0]); ///project the z into spherical coordinates | |
138 | + Z[1] = cos(PHI[1]); /// | |
139 | + range = Z[0] - Z[1]; ///the range of all possible z values. | |
140 | + | |
141 | + float z, theta, phi; /// temporary individual | |
142 | + | |
143 | + std::vector<stim::vec3<float> > samples; | |
144 | + | |
145 | + //srand(time(NULL)); ///set random seed | |
146 | + srand(100); ///set random seed | |
147 | + | |
148 | + for(int i = 0; i < num_samples; i++) | |
149 | + { | |
150 | + z = uniformRandom()*range + Z[1]; | |
151 | + theta = uniformRandom()*stim::TAU; | |
152 | + phi = acos(z); | |
153 | + stim::vec3<float> sph(1, theta, phi); | |
154 | + stim::vec3<float> cart = sph.sph2cart(); | |
155 | + sph[0] *= radius; | |
156 | + samples.push_back(cart); | |
157 | + } | |
158 | + samples.push_back(stim::vec3<float>(0.,0.,1.)); | |
159 | + samples.push_back(stim::vec3<float>(0.,1.0,0.)); | |
160 | + samples.push_back(stim::vec3<float>(0.,-1.,0.)); | |
161 | + | |
162 | + | |
163 | + std::stringstream name; | |
164 | + for(int i = 0; i < num_samples; i++) | |
165 | + name << samples[i].str() << std::endl; | |
166 | + name << samples[num_samples].str() << std::endl; | |
167 | + name << samples[num_samples+1].str() << std::endl; | |
168 | + name << samples[num_samples+2].str() << std::endl; | |
169 | + | |
170 | + | |
171 | + std::ofstream outFile; | |
172 | + outFile.open("New_Pos_Vectors.txt"); | |
173 | + outFile << name.str().c_str(); | |
174 | + | |
175 | + return samples; | |
176 | +} | |
177 | + | |
122 | 178 | void process_arguments(int argc, char* argv[]){ |
123 | 179 | |
124 | 180 | args.add("help", "prints this help"); |
... | ... | @@ -128,6 +184,7 @@ void process_arguments(int argc, char* argv[]){ |
128 | 184 | args.add("obj", "approximates a geometric object given as a Wavefront OBJ file", "", "filename"); |
129 | 185 | args.add("out", "filename for outputting spherical harmonics coefficients", "", "filename"); |
130 | 186 | args.add("zaxis", "render the z-axis as a green line"); |
187 | + args.add("pdf", "outputs the PDF if an OBJ files is given"); | |
131 | 188 | |
132 | 189 | //process the command line arguments |
133 | 190 | args.parse(argc, argv); |
... | ... | @@ -212,12 +269,17 @@ void process_arguments(int argc, char* argv[]){ |
212 | 269 | |
213 | 270 | std::string filename = args["obj"].as_string(0); |
214 | 271 | unsigned int l = args["obj"].as_int(1); |
272 | + int p = args["obj"].as_int(2); | |
273 | + std::cout << p << std::endl; | |
274 | + std::vector<stim::vec3<float> > sphere = sample_sphere(p); | |
275 | + p = p+3; | |
215 | 276 | |
216 | 277 | //create an obj object |
217 | 278 | stim::obj<double> object(filename); |
218 | 279 | |
219 | 280 | //get the centroid of the object |
220 | 281 | stim::vec<double> c = object.centroid(); |
282 | + c[0] = 0; c[1] = 0; c[2] = 0; | |
221 | 283 | |
222 | 284 | //get the number of vertices in the model |
223 | 285 | unsigned int nV = object.numV(); |
... | ... | @@ -236,18 +298,59 @@ void process_arguments(int argc, char* argv[]){ |
236 | 298 | //generate the spherical PDF |
237 | 299 | stim::spharmonics<double> P; |
238 | 300 | P.pdf(spherical, l, l); |
301 | + std::vector<float> weights; ///array of weights | |
302 | + if(args["pdf"].is_set()) | |
303 | + { | |
304 | +// S.pdf(spherical, l, l); | |
305 | + for(int i = 0; i < p; i++) ///for each point on the sphere. | |
306 | + { | |
307 | + float val = 0; ///value starts with 0 | |
308 | + for(int j = 0; j < nV; j++) ///for each point on surface | |
309 | + { | |
310 | + stim::vec3<float> star(object.getV(j)[0] - c[0], | |
311 | + object.getV(j)[1] - c[1], | |
312 | + object.getV(j)[2] - c[2]); ///center each point on the model | |
313 | +// val += abs(star.dot(sphere[i])); ///sum the dot product of the centered point and the sphere. | |
314 | + if(star.dot(sphere[i]) > 0) | |
315 | + val += pow(star.dot(sphere[i]),8); ///sum the dot product of the centered point and the sphere. | |
316 | + } | |
317 | + weights.push_back(val); | |
318 | + } | |
319 | + | |
320 | + S.mcBegin(l,l); | |
321 | + for(int i = 0; i < p; i++) | |
322 | + { | |
323 | + if(sphere[i] == stim::vec3<float>(0., 0., 1.)) | |
324 | + { | |
325 | + std::cout << i << sphere[i] << " " << weights[i] << std::endl; | |
326 | + } | |
327 | + if(sphere[i] == stim::vec3<float>(0., 1., 0.)) | |
328 | + { | |
329 | + std::cout << i << sphere[i] << " " << weights[i] << std::endl; | |
330 | + } | |
331 | + if(sphere[i] == stim::vec3<float>(0., -1., 0.)) | |
332 | + { | |
333 | + std::cout << i << sphere[i] << " " << weights[i] << std::endl; | |
334 | + } | |
335 | + stim::vec3<float> sph = sphere[i].cart2sph(); | |
336 | + S.mcSample(sph[1], sph[2], weights[i]); | |
337 | + } | |
338 | + S.mcEnd(); | |
239 | 339 | |
240 | - //begin Monte-Carlo sampling, using the model vertices as samples | |
241 | - S.mcBegin(l, l); | |
242 | - double theta, phi, fx, px; | |
243 | - for(unsigned int i = 0; i < nV; i++){ | |
244 | - theta = spherical[i][1]; | |
245 | - phi = spherical[i][2]; | |
246 | - fx = spherical[i][0]; | |
247 | - px = P(theta, phi); | |
248 | - S.mcSample(theta, phi, fx / px); | |
249 | 340 | } |
250 | - S.mcEnd(); | |
341 | + else{ | |
342 | + //begin Monte-Carlo sampling, using the model vertices as samples | |
343 | + S.mcBegin(l, l); | |
344 | + double theta, phi, fx, px; | |
345 | + for(unsigned int i = 0; i < nV; i++){ | |
346 | + theta = spherical[i][1]; | |
347 | + phi = spherical[i][2]; | |
348 | + fx = spherical[i][0]; | |
349 | + px = P(theta, phi); | |
350 | + S.mcSample(theta, phi, fx / px); | |
351 | + } | |
352 | + S.mcEnd(); | |
353 | + } | |
251 | 354 | } |
252 | 355 | |
253 | 356 | //if the user specifies an SH basis function | ... | ... |