Subversion Repositories gelsvn

Rev

Rev 596 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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