Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
386 jab 1
/*
2
 *  harmonics.cpp
3
 *  GEL
4
 *
5
 *  Created by J. Andreas Bærentzen on 01/09/08.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
7
 *
8
 */
9
 
10
#include <iostream>
457 jab 11
 
12
#include "harmonics.h"
13
 
14
#if USE_SPARSE_MATRIX
15
#include <arlgsym.h>
16
#endif
17
 
386 jab 18
#include <CGLA/Vec3d.h>
457 jab 19
#include <CGLA/Mat2x2d.h>
386 jab 20
#include <LinAlg/Matrix.h>
21
#include <LinAlg/Vector.h>
22
#include <LinAlg/LapackFunc.h>
23
 
24
#include <GL/glew.h>
25
#include <GLGraphics/glsl_shader.h>
26
 
27
#include <HMesh/mesh_optimization.h>
596 jab 28
#include <HMesh/curvature.h>
386 jab 29
#include <HMesh/triangulate.h>
30
#include <HMesh/load.h>
31
#include <HMesh/x3d_save.h>
32
 
596 jab 33
#include <GLGraphics/Console.h>
386 jab 34
 
596 jab 35
//#include "CSCMatrixBuilder.h"
386 jab 36
 
37
using namespace CGLA;
38
using namespace std;
39
using namespace HMesh;
40
using namespace Geometry;
41
using namespace GLGraphics;
42
using namespace LinAlg;
43
 
44
namespace
45
{
457 jab 46
 
397 jab 47
	string vss =
457 jab 48
	"#version 120\n"
49
	"#extension GL_EXT_gpu_shader4 : enable\n"
50
	"	\n"
51
	"	\n"
52
	"	attribute float eigenvalue;\n"
53
	"	attribute float eigenvalue2;\n"
54
	"	varying vec3 normal;\n"
55
	"	varying float eig;\n"
56
	"	varying float eig2;\n"
57
	"	\n"
58
	"	void main(void)\n"
59
	"	{\n"
60
	"		gl_Position =  ftransform();\n"
61
	"		normal = normalize(gl_NormalMatrix * gl_Normal);\n"
62
	"		eig = eigenvalue;\n"
63
	"		eig2 = eigenvalue2;\n"
64
	"	}\n";
386 jab 65
 
641 janba 66
	string fss =
457 jab 67
	"#version 120\n"
68
	"#extension GL_EXT_gpu_shader4 : enable\n"
69
	"	\n"
70
	"	varying vec3 normal;\n"
71
	"	varying float eig;\n"
72
	"	varying float eig2;\n"
73
	"	uniform float eig_max;\n"
74
	"	uniform float eig_max2;\n"
75
	"	uniform bool do_highlight;\n"
76
	"	uniform bool do_diffuse;\n"
77
	"	const vec3 light_dir = vec3(0,0,1);\n"
78
	"	\n"
641 janba 79
	" float basef(float x) {return max(0.0,min(1.0,2.0-4.0*abs(x)));\n}"
457 jab 80
	"	void main()\n"
81
	"	{\n"
82
	"		float dot_ln = max(0.0,dot(light_dir, normal));\n"
83
	"		\n"
84
	"		float eig_norm = eig/eig_max;\n"
85
	"		float stripe_signal = 250 * eig_norm;\n"
86
	//"		vec4 stripe_col = abs(stripe_signal) < 3.14 ? vec4(1,1,0,0) : vec4(.4,.4,.4,0);\n"
87
	"		vec4 stripe_col = -vec4(.4,.4,.4,0);\n"
88
	"		\n"
89
	"       float alpha = (1.0-eig_norm) * 2.0 * 3.1415926;\n"
90
	"       float offs = 2.0*3.1415/3.0;\n"
91
	"		gl_FragColor = vec4(0,0,1,0)*basef(eig_norm)+vec4(0,1,0,0)*basef(eig_norm-0.5)+vec4(1,0,0,0)* basef(eig_norm-1.0);\n"
92
	"		if(do_diffuse)   gl_FragColor *= dot_ln;\n"
93
	"		if(do_highlight) gl_FragColor += dot_ln*dot_ln*dot_ln*dot_ln*dot_ln*dot_ln*dot_ln*vec4(.5,.5,.5,0);\n"
94
	"		gl_FragColor -= vec4(.4,.4,.4,.4)*smoothstep(0.2,0.6,cos(stripe_signal));\n"
95
	"	}\n";
96
}
97
 
98
 
99
 
100
#if USE_SPARSE_MATRIX
101
 
102
double calculate_laplace_entry(VertexCirculator vc,Vec3d vertex)
103
{
104
	//* this piece of code is taken from the orginal make_laplace_operator...
105
	HalfEdgeIter h = vc.get_halfedge();
106
	Vec3d nbr(h->vert->pos);
107
	Vec3d left(h->next->vert->pos);
108
	Vec3d right(h->opp->prev->opp->vert->pos);
397 jab 109
 
457 jab 110
	double d_left = dot(normalize(nbr-left),normalize(vertex-left));
111
	double d_right = dot(normalize(nbr-right),normalize(vertex-right));
112
	double a_left  = acos(min(1.0, max(-1.0, d_left))); // w positive...?
113
	double a_right = acos(min(1.0, max(-1.0, d_right)));
114
 
115
	double w = 1.0/tan(a_left) + 1.0/tan(a_right);
116
	return -w;
117
}
118
 
119
void Harmonics::make_laplace_operator_sparse()
120
{
121
	bool is_generalized=true;
122
	CSCMatrixBuilder<double> mb_M;
123
	//+GENERALIZED
124
	CSCMatrixBuilder<double> mb_S;
125
	// S has the same type as V
126
	S.Resize(mani.no_vertices());
127
 
596 jab 128
	for(VertexIter v = mani.vhandles_begin(); v != mani.vhandles_end(); ++v)
457 jab 129
		if(!is_boundary(v))
130
		{
131
			int i = v->touched;
132
			double area_i = voronoi_area(v);
133
			Vec3d vertex(v->pos);
134
			double a_sum = 0;
135
			for(VertexCirculator vc(v); !vc.end(); ++vc)
136
			{
137
				int j = vc.get_vertex()->touched;
138
				double entry = calculate_laplace_entry(vc,vertex);
139
				if(!is_generalized)
140
				{
141
					double area_j = voronoi_area(vc.get_vertex());
142
					entry /= sqrt(area_i*area_j);
143
				}
144
				if(j > i)
145
					mb_M.insert_entry(j,entry);
146
				a_sum += entry;
147
			}
148
			//cout << a_sum << " ";
149
			mb_M.insert_entry(i,-a_sum);
150
			mb_M.next_column();
151
 
152
			if(!is_generalized)
153
				area_i = 1; // if standard S is an identity matrix;
154
			mb_S.insert_entry(i,area_i);
155
			mb_S.next_column_nonsort();
156
			S[i] = area_i;
641 janba 157
		}
457 jab 158
 
159
	cout << "solving generalized problem... i = " << mani.no_vertices()<< endl;
160
 
161
 
162
	//+STANDARD
163
	//ArpackPP::ARluSymStdEig<double> dprob(50L, mb_M.get_Matrix(), "SA");
164
	//+GENERALIZED (shifted inv mode)
165
	ARluSymGenEig<double> dprob('S',maximum_eigenvalue, mb_M.get_Matrix(), mb_S.get_Matrix(), 0.0);
166
	//ArpackPP::ARluSymGenEig<double> dprob(number_of_eigenvalues, mb_M.get_Matrix(), mb_S.get_Matrix());
167
 
168
	dprob.FindEigenvectors();
169
	int conv = dprob.ConvergedEigenvalues();
170
	cout << conv << " eigenvectors found" << endl;
171
	Q.Resize(conv, dprob.GetN());
172
	V.Resize(conv);
173
 
174
	qnorm.Resize(conv);
175
	for(int i = 0; i < conv; i++)
386 jab 176
	{
457 jab 177
		V[i] = dprob.Eigenvalue(i);
178
		qnorm[i] = 0;
179
		for(int j = 0; j < dprob.GetN(); j++)
386 jab 180
		{
457 jab 181
			Q[i][j] = dprob.Eigenvector(i,j);
182
			qnorm[i] += Q[i][j]*Q[i][j];
386 jab 183
		}
457 jab 184
 
185
		cout << "(" << i << ":" << sqrt(V[i]/V[1]
186
										) << ")" << V[i]/V[1] << "V= "<< V[i] << " exp " << exp(-0.01*V[i]/V[1]) << " Qnorm " << qnorm[i]<< endl;
386 jab 187
	}
457 jab 188
 
189
	cout  << endl;
386 jab 190
}
191
 
457 jab 192
Harmonics::Harmonics(Manifold& _mani):mani(_mani)
193
{
194
	assert(is_initialized);
195
 
196
	maximum_eigenvalue = min(size_t(500),mani.no_vertices());
197
 
198
	triangulate(mani);
199
	mani.enumerate_vertices();
200
	make_laplace_operator_sparse();
201
 
202
	if(maximum_eigenvalue == -1)
386 jab 203
	{
457 jab 204
		cout << "not found" << endl;
205
		return;
206
	}
207
 
208
	proj.resize(maximum_eigenvalue);
209
 
210
	max_eig_values.resize(maximum_eigenvalue, 1e-10f);
211
 
212
	cout << endl << "Proj" << endl;
213
	for(int es=0; es<maximum_eigenvalue; ++es)  //o(n^2)
386 jab 214
	{
457 jab 215
		proj[es] = Vec3d(0.0);
596 jab 216
		for(VertexIter v=mani.vhandles_begin(); v != mani.vhandles_end(); ++v)
457 jab 217
		{
218
			proj[es] +=  Vec3d(v->pos) * Q[es][v->touched] * S[v->touched];
641 janba 219
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][v->touched])));
220
		}
386 jab 221
	}
457 jab 222
	cout << endl;
386 jab 223
}
224
 
457 jab 225
#else
391 jab 226
void Harmonics::make_laplace_operator()
386 jab 227
{
228
	Q.Resize(mani.no_vertices(), mani.no_vertices());
229
 
596 jab 230
	for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
231
		if(!boundary(mani, *v)){
232
			int i = vtouched[*v];
233
			double area_i = voronoi_area(mani, *v);
234
			Vec3d vertex(mani.pos(*v));
386 jab 235
			Vec3d curv_normal(0);
236
			double a_sum = 0;
596 jab 237
            for(Walker wv = mani.walker(*v); !wv.full_circle(); wv = wv.circulate_vertex_cw())
386 jab 238
			{
596 jab 239
				int j = vtouched[wv.vertex()];
240
				double area_j = voronoi_area(mani, wv.vertex());
386 jab 241
 
596 jab 242
				Vec3d nbr(mani.pos(wv.vertex()));
243
				Vec3d left(mani.pos(wv.next().vertex()));
244
				Vec3d right(mani.pos(wv.opp().prev().opp().vertex()));
245
 
386 jab 246
				double d_left = dot(normalize(nbr-left),
247
									normalize(vertex-left));
248
				double d_right = dot(normalize(nbr-right),
249
									 normalize(vertex-right));
250
				double a_left  = acos(min(1.0, max(-1.0, d_left)));
251
				double a_right = acos(min(1.0, max(-1.0, d_right)));
252
 
253
				double w = 1.0/tan(a_left) + 1.0/tan(a_right);
254
 
641 janba 255
				Q[i][j] = -w/sqrt(area_i*area_j);
256
				//Q[i][j] = -1;
386 jab 257
				a_sum += Q[i][j];
258
			}
259
			Q[i][i] = -a_sum;
260
		}
261
	EigenSolutionsSym(Q,V);
262
}
263
 
641 janba 264
Harmonics::Harmonics(HMesh::Manifold& _mani):mani(_mani), vtouched(_mani.allocated_vertices(), 0)
386 jab 265
{
641 janba 266
 
596 jab 267
    int i = 0;
268
    for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v, ++i)
269
        vtouched[*v] = i;
391 jab 270
	maximum_eigenvalue = mani.no_vertices()-1;
271
	make_laplace_operator();
272
 
273
	proj.resize(maximum_eigenvalue);
409 jrf 274
	max_eig_values.resize(maximum_eigenvalue, 1e-10f);
457 jab 275
 
276
	cout << "Projection magnitude" << endl;
391 jab 277
	for(int es=0; es<maximum_eigenvalue; ++es)
386 jab 278
	{
399 jab 279
		proj[es] = Vec3d(0.0);
596 jab 280
		for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
386 jab 281
		{
596 jab 282
			proj[es] +=  Vec3d(mani.pos(*v)) * Q[es][vtouched[*v]];
283
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][vtouched[*v]])));
386 jab 284
		}
285
	}
286
}
287
 
457 jab 288
#endif
289
 
391 jab 290
void Harmonics::add_frequency(int es, float scale)
386 jab 291
{
391 jab 292
	if(es<maximum_eigenvalue)
596 jab 293
		for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
294
			Vec3d p = Vec3d(proj[es]);
295
			double Qval = Q[es][vtouched[*v]];
399 jab 296
 
641 janba 297
			mani.pos(*v) += p * Qval * scale;
386 jab 298
		}
299
}
300
 
391 jab 301
void Harmonics::reset_shape()
386 jab 302
{
596 jab 303
	for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
641 janba 304
		mani.pos(*v) = Vec3d(0);
386 jab 305
}
391 jab 306
void Harmonics::partial_reconstruct(int E0, int E1, float scale)
386 jab 307
{
308
	for(int es=E0;es<=E1;++es)
391 jab 309
		add_frequency(es, scale);
386 jab 310
}
311
 
641 janba 312
 
313
double Harmonics::compute_adf(HMesh::VertexAttributeVector<double>& F, double t)
386 jab 314
{
641 janba 315
	double F_max = 0;
316
	for(VertexID vid : mani.vertices()){
317
		for(int e = 1; e < V.Length(); ++e)
318
			F[vid] += sqr(Q[e][vtouched[vid]]) * exp(-t*V[e]/V[1]);
319
		F_max = max(F[vid], F_max);
320
	}
321
    return F_max;
322
}
323
 
324
 
325
GLuint HarmonicsRenderer::prog_P0 = 0;
326
GLGraphics::Console::variable<float> HarmonicsRenderer::display_harmonics_time;
327
GLGraphics::Console::variable<int> HarmonicsRenderer::display_harmonics_diffuse;
328
GLGraphics::Console::variable<int> HarmonicsRenderer::display_harmonics_highlight;
329
 
330
 
331
HarmonicsRenderer::HarmonicsRenderer(HMesh::Manifold& _m, Harmonics& _h, GLGraphics::Console& cs): m(&_m), h(&_h)
332
{
333
    if (prog_P0 == 0) {
334
        string shader_path = "/Users/jab/GEL/apps/MeshEdit/";
335
        GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
336
        GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
337
 
338
        // Create the program
339
        prog_P0 = glCreateProgram();
340
 
341
        // Attach all shaders
342
        if(vs) glAttachShader(prog_P0, vs);
343
        if(fs) glAttachShader(prog_P0, fs);
344
 
345
        // Link the program object and print out the info log
346
        glLinkProgram(prog_P0);
347
        print_glsl_program_log(prog_P0);
348
 
349
        // Install program object as part of current state
350
        glUseProgram(0);
351
 
352
 
353
        display_harmonics_diffuse.reg(cs, "display.harmonics.diffuse", "");
354
        display_harmonics_time.reg(cs, "display.harmonics.time", "");
355
        display_harmonics_highlight.reg(cs, "display.harmonics.highlight", "");
356
    }
357
    draw_adf();
358
}
359
 
360
 
361
void HarmonicsRenderer::parse_key(unsigned char key)
362
{
457 jab 363
	switch(key) {
641 janba 364
		case '+':
365
			display_harmonics_time = display_harmonics_time+0.001;
457 jab 366
			break;
641 janba 367
		case '-':
368
			display_harmonics_time = display_harmonics_time-0.001;
457 jab 369
			break;
641 janba 370
		case 'd':
371
			display_harmonics_diffuse = !display_harmonics_diffuse;
457 jab 372
			break;
373
		case 'h':
596 jab 374
			display_harmonics_highlight = !display_harmonics_highlight;
641 janba 375
			break;
457 jab 376
	}
377
 
391 jab 378
}
379
 
457 jab 380
 
641 janba 381
 
382
 
383
void HarmonicsRenderer::draw_adf()
391 jab 384
{
641 janba 385
    VertexAttributeVector<double> F;
386
    double F_max = h->compute_adf(F, display_harmonics_time);
457 jab 387
	cout << "F max" <<  F_max << endl;
641 janba 388
 
389
    glNewList(display_list, GL_COMPILE);
386 jab 390
	glUseProgram(prog_P0);
457 jab 391
	glUniform1f(glGetUniformLocation(prog_P0,"eig_max"),F_max);//2*M_PI);
596 jab 392
	glUniform1i(glGetUniformLocation(prog_P0,"do_diffuse"),display_harmonics_diffuse);
393
   	glUniform1i(glGetUniformLocation(prog_P0,"do_highlight"),display_harmonics_highlight);
386 jab 394
	GLuint attrib = glGetAttribLocationARB(prog_P0, "eigenvalue");
395
 
396
	glFrontFace(GL_CW);
641 janba 397
	for(FaceIDIterator f = m->faces_begin(); f != m->faces_end(); ++f){
386 jab 398
		glBegin(GL_TRIANGLES);
641 janba 399
        for(Walker w = m->walker(*f); !w.full_circle(); w = w.circulate_face_cw()){
400
			glVertexAttrib1f(attrib,F[w.vertex()]);
401
			glNormal3dv(normal(*m, w.vertex()).get());
402
			glVertex3dv(m->pos(w.vertex()).get());
386 jab 403
		}
404
		glEnd();
405
	}
406
	glFrontFace(GL_CCW);
407
	glUseProgram(0);
641 janba 408
    glEndList();
386 jab 409
}
410