Subversion Repositories gelsvn

Rev

Rev 457 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 457 Rev 596
Line 13... Line 13...
13
 
13
 
14
#if USE_SPARSE_MATRIX
14
#if USE_SPARSE_MATRIX
15
#include <arlgsym.h>
15
#include <arlgsym.h>
16
#endif
16
#endif
17
 
17
 
18
#include <CGLA/Vec3f.h>
-
 
19
#include <CGLA/Vec3d.h>
18
#include <CGLA/Vec3d.h>
20
#include <CGLA/Mat2x2d.h>
19
#include <CGLA/Mat2x2d.h>
21
#include <LinAlg/Matrix.h>
20
#include <LinAlg/Matrix.h>
22
#include <LinAlg/Vector.h>
21
#include <LinAlg/Vector.h>
23
#include <LinAlg/LapackFunc.h>
22
#include <LinAlg/LapackFunc.h>
24
 
23
 
25
#include <GL/glew.h>
24
#include <GL/glew.h>
26
#include <GLGraphics/glsl_shader.h>
25
#include <GLGraphics/glsl_shader.h>
27
 
26
 
28
#include <HMesh/Manifold.h>
-
 
29
#include <HMesh/VertexCirculator.h>
-
 
30
#include <HMesh/FaceCirculator.h>
-
 
31
#include <HMesh/build_manifold.h>
-
 
32
#include <HMesh/mesh_optimization.h>
27
#include <HMesh/mesh_optimization.h>
-
 
28
#include <HMesh/curvature.h>
33
#include <HMesh/triangulate.h>
29
#include <HMesh/triangulate.h>
34
#include <HMesh/load.h>
30
#include <HMesh/load.h>
35
#include <HMesh/x3d_save.h>
31
#include <HMesh/x3d_save.h>
36
 
32
 
37
#include <GLConsole/GLConsole.h>
33
#include <GLGraphics/Console.h>
38
 
34
 
39
#include "curvature.h"
-
 
40
#include "CSCMatrixBuilder.h"
35
//#include "CSCMatrixBuilder.h"
41
 
36
 
42
using namespace CGLA;
37
using namespace CGLA;
43
using namespace std;
38
using namespace std;
44
using namespace HMesh;
39
using namespace HMesh;
45
using namespace Geometry;
40
using namespace Geometry;
46
using namespace GLGraphics;
41
using namespace GLGraphics;
47
using namespace LinAlg;
42
using namespace LinAlg;
48
using namespace CVarUtils;
-
 
49
 
43
 
50
bool Harmonics::is_initialized=false;
44
bool Harmonics::is_initialized=false;
51
GLuint Harmonics::prog_P0;
45
GLuint Harmonics::prog_P0;
52
 
46
 
53
 
47
 
Line 133... Line 127...
133
	//+GENERALIZED
127
	//+GENERALIZED
134
	CSCMatrixBuilder<double> mb_S;
128
	CSCMatrixBuilder<double> mb_S;
135
	// S has the same type as V
129
	// S has the same type as V
136
	S.Resize(mani.no_vertices());
130
	S.Resize(mani.no_vertices());
137
	
131
	
138
	for(VertexIter v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
132
	for(VertexIter v = mani.vhandles_begin(); v != mani.vhandles_end(); ++v)
139
		if(!is_boundary(v))
133
		if(!is_boundary(v))
140
		{
134
		{
141
			int i = v->touched;
135
			int i = v->touched;
142
			double area_i = voronoi_area(v);
136
			double area_i = voronoi_area(v);
143
			Vec3d vertex(v->pos);
137
			Vec3d vertex(v->pos);
Line 221... Line 215...
221
	
215
	
222
	cout << endl << "Proj" << endl;
216
	cout << endl << "Proj" << endl;
223
	for(int es=0; es<maximum_eigenvalue; ++es)  //o(n^2)
217
	for(int es=0; es<maximum_eigenvalue; ++es)  //o(n^2)
224
	{
218
	{
225
		proj[es] = Vec3d(0.0);
219
		proj[es] = Vec3d(0.0);
226
		for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
220
		for(VertexIter v=mani.vhandles_begin(); v != mani.vhandles_end(); ++v)
227
		{
221
		{
228
			proj[es] +=  Vec3d(v->pos) * Q[es][v->touched] * S[v->touched];
222
			proj[es] +=  Vec3d(v->pos) * Q[es][v->touched] * S[v->touched];
229
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][v->touched])));     
223
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][v->touched])));     
230
		}     
224
		}     
231
	}
225
	}
Line 235... Line 229...
235
#else
229
#else
236
void Harmonics::make_laplace_operator()
230
void Harmonics::make_laplace_operator()
237
{
231
{
238
	Q.Resize(mani.no_vertices(), mani.no_vertices());
232
	Q.Resize(mani.no_vertices(), mani.no_vertices());
239
	
233
	
240
	for(VertexIter v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
234
	for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
241
		if(!is_boundary(v))
235
		if(!boundary(mani, *v)){
242
		{
-
 
243
			int i = v->touched;
236
			int i = vtouched[*v];
244
			double area_i = voronoi_area(v);
237
			double area_i = voronoi_area(mani, *v);
245
			Vec3d vertex(v->pos);
238
			Vec3d vertex(mani.pos(*v));
246
			Vec3d curv_normal(0);
239
			Vec3d curv_normal(0);
247
			double a_sum = 0;
240
			double a_sum = 0;
248
			for(VertexCirculator vc(v); !vc.end(); ++vc)
241
            for(Walker wv = mani.walker(*v); !wv.full_circle(); wv = wv.circulate_vertex_cw())
249
			{
242
			{
250
				int j = vc.get_vertex()->touched;
243
				int j = vtouched[wv.vertex()];
251
				double area_j = voronoi_area(vc.get_vertex());
244
				double area_j = voronoi_area(mani, wv.vertex());
252
				HalfEdgeIter h = vc.get_halfedge();
245
				
253
				Vec3d nbr(h->vert->pos);
246
				Vec3d nbr(mani.pos(wv.vertex()));
254
				Vec3d left(h->next->vert->pos);
247
				Vec3d left(mani.pos(wv.next().vertex()));
255
				Vec3d right(h->opp->prev->opp->vert->pos);
248
				Vec3d right(mani.pos(wv.opp().prev().opp().vertex()));
256
				
249
				
257
				double d_left = dot(normalize(nbr-left),
250
				double d_left = dot(normalize(nbr-left),
258
									normalize(vertex-left));
251
									normalize(vertex-left));
259
				double d_right = dot(normalize(nbr-right),
252
				double d_right = dot(normalize(nbr-right),
260
									 normalize(vertex-right));
253
									 normalize(vertex-right));
Line 270... Line 263...
270
			Q[i][i] = -a_sum;
263
			Q[i][i] = -a_sum;
271
		}
264
		}
272
	EigenSolutionsSym(Q,V);
265
	EigenSolutionsSym(Q,V);
273
}
266
}
274
 
267
 
275
Harmonics::Harmonics(Manifold& _mani):mani(_mani)
268
Harmonics::Harmonics(Manifold& _mani):mani(_mani), vtouched(_mani.allocated_vertices(), 0) 
276
{
269
{
277
	assert(is_initialized);
270
	assert(is_initialized);
278
	
271
	
279
	triangulate(mani);
272
	triangulate_by_edge_face_split(mani);
-
 
273
 
-
 
274
    int i = 0;
-
 
275
    for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v, ++i)
280
	mani.enumerate_vertices();
276
        vtouched[*v] = i;
281
	maximum_eigenvalue = mani.no_vertices()-1;
277
	maximum_eigenvalue = mani.no_vertices()-1;
282
	make_laplace_operator();
278
	make_laplace_operator();
283
	
279
	
284
	proj.resize(maximum_eigenvalue);
280
	proj.resize(maximum_eigenvalue);
285
	max_eig_values.resize(maximum_eigenvalue, 1e-10f);
281
	max_eig_values.resize(maximum_eigenvalue, 1e-10f);
286
	
282
	
287
	cout << "Projection magnitude" << endl;
283
	cout << "Projection magnitude" << endl;
288
	for(int es=0; es<maximum_eigenvalue; ++es)
284
	for(int es=0; es<maximum_eigenvalue; ++es)
289
	{
285
	{
290
		proj[es] = Vec3d(0.0);
286
		proj[es] = Vec3d(0.0);
291
		for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
287
		for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
292
		{
288
		{
293
			proj[es] +=  Vec3d(v->pos) * Q[es][v->touched];
289
			proj[es] +=  Vec3d(mani.pos(*v)) * Q[es][vtouched[*v]];
294
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][v->touched])));
290
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][vtouched[*v]])));
295
		}
291
		}
296
	}
292
	}
297
}
293
}
298
 
294
 
299
#endif
295
#endif
300
 
296
 
301
void Harmonics::add_frequency(int es, float scale)
297
void Harmonics::add_frequency(int es, float scale)
302
{
298
{
303
	if(es<maximum_eigenvalue)
299
	if(es<maximum_eigenvalue)
304
		for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
300
		for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
305
		{
-
 
306
			Vec3f p = Vec3f(proj[es]);
301
			Vec3d p = Vec3d(proj[es]);
307
			double Qval = Q[es][v->touched];
302
			double Qval = Q[es][vtouched[*v]];
308
			
303
			
309
			v->pos += p * Qval * scale; 	
304
			mani.pos(*v) += p * Qval * scale; 	
310
		}
305
		}
311
}
306
}
312
 
307
 
313
void Harmonics::reset_shape()
308
void Harmonics::reset_shape()
314
{
309
{
315
	for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
310
	for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
316
		v->pos = Vec3f(0);	
311
		mani.pos(*v) = Vec3d(0);	
317
}
312
}
318
void Harmonics::partial_reconstruct(int E0, int E1, float scale)
313
void Harmonics::partial_reconstruct(int E0, int E1, float scale)
319
{
314
{
320
	for(int es=E0;es<=E1;++es)
315
	for(int es=E0;es<=E1;++es)
321
		add_frequency(es, scale);
316
		add_frequency(es, scale);
322
}
317
}
323
 
318
 
-
 
319
Console::variable<float> display_harmonics_time;
-
 
320
Console::variable<int> display_harmonics_diffuse;
-
 
321
Console::variable<int> display_harmonics_highlight;
-
 
322
 
324
void Harmonics::parse_key(unsigned char key)
323
void Harmonics::parse_key(unsigned char key)
325
{
324
{
326
	float& display_t = GetCVarRef<float>("display.harmonics.time");
-
 
327
	int& display_diffuse = GetCVarRef<int>("display.harmonics.diffuse");
-
 
328
	int& display_highlight = GetCVarRef<int>("display.harmonics.highlight");
-
 
329
	switch(key) {
325
	switch(key) {
330
		case '+': 
326
		case '+': 
331
			display_t = display_t+0.001; 
327
			display_harmonics_time = display_harmonics_time+0.001; 
332
			break;
328
			break;
333
		case '-': 
329
		case '-': 
334
			display_t = display_t-0.001; 
330
			display_harmonics_time = display_harmonics_time-0.001; 
335
			break;
331
			break;
336
		case 'd':	
332
		case 'd':	
337
			display_diffuse = !display_diffuse; 
333
			display_harmonics_diffuse = !display_harmonics_diffuse; 
338
			break;
334
			break;
339
		case 'h':
335
		case 'h':
340
			display_highlight = !display_highlight;
336
			display_harmonics_highlight = !display_harmonics_highlight;
341
			break;			
337
			break;			
342
	}
338
	}
343
	
339
	
344
}
340
}
345
 
341
 
346
 
342
 
347
void Harmonics::draw_adf()
343
void Harmonics::draw_adf()
348
{
344
{
349
	float& t = GetCVarRef<float>("display.harmonics.time");
-
 
350
	int& do_diffuse = GetCVarRef<int>("display.harmonics.diffuse");
-
 
351
	int& do_highlight = GetCVarRef<int>("display.harmonics.highlight");
-
 
352
	
-
 
353
	vector<double> F(mani.no_vertices(),0);
345
	vector<double> F(mani.no_vertices(),0);
354
	double F_max = 0;
346
	double F_max = 0;
355
	for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
347
	for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
356
	{
-
 
357
		int v_idx = v->touched;
348
		int v_idx = vtouched[*v];
358
		for(int e=1;e<V.Length();++e)
349
		for(int e = 1; e < V.Length(); ++e)
359
			F[v_idx] += sqr(Q[e][v_idx])*exp(-(t)*V[e]/V[1]);
350
			F[v_idx] += sqr(Q[e][v_idx]) * exp(-(display_harmonics_time)*V[e]/V[1]);
360
		F_max = max(F[v_idx], F_max);
351
		F_max = max(F[v_idx], F_max);
361
	}
352
	}
362
	cout << "F max" <<  F_max << endl;
353
	cout << "F max" <<  F_max << endl;
363
	glUseProgram(prog_P0);
354
	glUseProgram(prog_P0);
364
	glUniform1f(glGetUniformLocation(prog_P0,"eig_max"),F_max);//2*M_PI);
355
	glUniform1f(glGetUniformLocation(prog_P0,"eig_max"),F_max);//2*M_PI);
365
	glUniform1i(glGetUniformLocation(prog_P0,"do_diffuse"),do_diffuse);
356
	glUniform1i(glGetUniformLocation(prog_P0,"do_diffuse"),display_harmonics_diffuse);
366
   	glUniform1i(glGetUniformLocation(prog_P0,"do_highlight"),do_highlight);
357
   	glUniform1i(glGetUniformLocation(prog_P0,"do_highlight"),display_harmonics_highlight);
367
	GLuint attrib = glGetAttribLocationARB(prog_P0, "eigenvalue");
358
	GLuint attrib = glGetAttribLocationARB(prog_P0, "eigenvalue");
368
	
359
	
369
	glFrontFace(GL_CW);
360
	glFrontFace(GL_CW);
370
	for(FaceIter f=mani.faces_begin(); f != mani.faces_end(); ++f)
361
	for(FaceIDIterator f = mani.faces_begin(); f != mani.faces_end(); ++f){
371
	{
-
 
372
		FaceCirculator fc(f);
-
 
373
		glBegin(GL_TRIANGLES);
362
		glBegin(GL_TRIANGLES);
374
		while(!fc.end())
363
        for(Walker w = mani.walker(*f); !w.full_circle(); w = w.circulate_face_cw()){
375
		{
-
 
376
			int i = fc.get_vertex()->touched;
364
			int i = vtouched[w.vertex()];
377
			glVertexAttrib1f(attrib,F[i]);
365
			glVertexAttrib1f(attrib,F[i]);
378
			glNormal3fv(normal(fc.get_vertex()).get());
366
			glNormal3dv(normal(mani, w.vertex()).get());
379
			glVertex3fv(fc.get_vertex()->pos.get());
367
			glVertex3dv(mani.pos(w.vertex()).get());
380
			++fc;
-
 
381
		}
368
		}
382
		glEnd();
369
		glEnd();
383
	}
370
	}
384
	glFrontFace(GL_CCW);
371
	glFrontFace(GL_CCW);
385
	glUseProgram(0);
372
	glUseProgram(0);
386
}
373
}
387
 
374
 
388
void Harmonics::init()
375
void Harmonics::init(Console& cs)
389
{
376
{
390
	is_initialized = true;
377
	is_initialized = true;
391
	string shader_path = "/Users/jab/GEL/apps/MeshEdit/";
378
	string shader_path = "/Users/jab/GEL/apps/MeshEdit/";
392
	GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
379
	GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
393
	GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
380
	GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
Line 404... Line 391...
404
	print_glsl_program_log(prog_P0);
391
	print_glsl_program_log(prog_P0);
405
	
392
	
406
	// Install program object as part of current state
393
	// Install program object as part of current state
407
	glUseProgram(0);
394
	glUseProgram(0);
408
	
395
	
409
	CreateCVar("display.harmonics.time",0.0f);
396
    display_harmonics_diffuse.reg(cs, "display.harmonics.diffuse", "");    
410
	CreateCVar("display.harmonics.eigenvalue2",0);
397
    display_harmonics_time.reg(cs, "display.harmonics.time", ""); 
411
	CreateCVar("display.harmonics.highlight",1);
398
    display_harmonics_highlight.reg(cs, "display.harmonics.highlight", "");
412
	CreateCVar("display.harmonics.diffuse",1);
-
 
413
	CreateCVar("display.harmonics.diffusion_distance_init_vertex",0);
-
 
-
 
399
    
414
	
400
 
415
}
401
}