Subversion Repositories gelsvn

Rev

Rev 457 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 457 Rev 596
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/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
 
54
namespace
48
namespace
55
{
49
{
56
	
50
	
57
	string vss =
51
	string vss =
58
	"#version 120\n"
52
	"#version 120\n"
59
	"#extension GL_EXT_gpu_shader4 : enable\n"
53
	"#extension GL_EXT_gpu_shader4 : enable\n"
60
	"	\n"
54
	"	\n"
61
	"	\n"
55
	"	\n"
62
	"	attribute float eigenvalue;\n"
56
	"	attribute float eigenvalue;\n"
63
	"	attribute float eigenvalue2;\n"
57
	"	attribute float eigenvalue2;\n"
64
	"	varying vec3 normal;\n"
58
	"	varying vec3 normal;\n"
65
	"	varying float eig;\n"
59
	"	varying float eig;\n"
66
	"	varying float eig2;\n"
60
	"	varying float eig2;\n"
67
	"	\n"
61
	"	\n"
68
	"	void main(void)\n"
62
	"	void main(void)\n"
69
	"	{\n"
63
	"	{\n"
70
	"		gl_Position =  ftransform();\n"
64
	"		gl_Position =  ftransform();\n"
71
	"		normal = normalize(gl_NormalMatrix * gl_Normal);\n"
65
	"		normal = normalize(gl_NormalMatrix * gl_Normal);\n"
72
	"		eig = eigenvalue;\n"
66
	"		eig = eigenvalue;\n"
73
	"		eig2 = eigenvalue2;\n"
67
	"		eig2 = eigenvalue2;\n"
74
	"	}\n";
68
	"	}\n";
75
	
69
	
76
	string fss = 	
70
	string fss = 	
77
	"#version 120\n"
71
	"#version 120\n"
78
	"#extension GL_EXT_gpu_shader4 : enable\n"
72
	"#extension GL_EXT_gpu_shader4 : enable\n"
79
	"	\n"
73
	"	\n"
80
	"	varying vec3 normal;\n"
74
	"	varying vec3 normal;\n"
81
	"	varying float eig;\n"
75
	"	varying float eig;\n"
82
	"	varying float eig2;\n"
76
	"	varying float eig2;\n"
83
	"	uniform float eig_max;\n"
77
	"	uniform float eig_max;\n"
84
	"	uniform float eig_max2;\n"
78
	"	uniform float eig_max2;\n"
85
	"	uniform bool do_highlight;\n"
79
	"	uniform bool do_highlight;\n"
86
	"	uniform bool do_diffuse;\n"
80
	"	uniform bool do_diffuse;\n"
87
	"	const vec3 light_dir = vec3(0,0,1);\n"
81
	"	const vec3 light_dir = vec3(0,0,1);\n"
88
	"	\n"
82
	"	\n"
89
	" float basef(float x) {return max(0.0,min(1.0,2.0-4.0*abs(x)));\n}" 
83
	" float basef(float x) {return max(0.0,min(1.0,2.0-4.0*abs(x)));\n}" 
90
	"	void main()\n"
84
	"	void main()\n"
91
	"	{\n"
85
	"	{\n"
92
	"		float dot_ln = max(0.0,dot(light_dir, normal));\n"
86
	"		float dot_ln = max(0.0,dot(light_dir, normal));\n"
93
	"		\n"
87
	"		\n"
94
	"		float eig_norm = eig/eig_max;\n"
88
	"		float eig_norm = eig/eig_max;\n"
95
	"		float stripe_signal = 250 * eig_norm;\n"
89
	"		float stripe_signal = 250 * eig_norm;\n"
96
	//"		vec4 stripe_col = abs(stripe_signal) < 3.14 ? vec4(1,1,0,0) : vec4(.4,.4,.4,0);\n"
90
	//"		vec4 stripe_col = abs(stripe_signal) < 3.14 ? vec4(1,1,0,0) : vec4(.4,.4,.4,0);\n"
97
	"		vec4 stripe_col = -vec4(.4,.4,.4,0);\n"
91
	"		vec4 stripe_col = -vec4(.4,.4,.4,0);\n"
98
	"		\n"
92
	"		\n"
99
	"       float alpha = (1.0-eig_norm) * 2.0 * 3.1415926;\n"
93
	"       float alpha = (1.0-eig_norm) * 2.0 * 3.1415926;\n"
100
	"       float offs = 2.0*3.1415/3.0;\n"
94
	"       float offs = 2.0*3.1415/3.0;\n"
101
	"		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"
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"
102
	"		if(do_diffuse)   gl_FragColor *= dot_ln;\n"
96
	"		if(do_diffuse)   gl_FragColor *= dot_ln;\n"
103
	"		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"
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"
104
	"		gl_FragColor -= vec4(.4,.4,.4,.4)*smoothstep(0.2,0.6,cos(stripe_signal));\n"
98
	"		gl_FragColor -= vec4(.4,.4,.4,.4)*smoothstep(0.2,0.6,cos(stripe_signal));\n"
105
	"	}\n";
99
	"	}\n";
106
}
100
}
107
 
101
 
108
 
102
 
109
 
103
 
110
#if USE_SPARSE_MATRIX
104
#if USE_SPARSE_MATRIX
111
 
105
 
112
double calculate_laplace_entry(VertexCirculator vc,Vec3d vertex)
106
double calculate_laplace_entry(VertexCirculator vc,Vec3d vertex)
113
{
107
{
114
	//* this piece of code is taken from the orginal make_laplace_operator...
108
	//* this piece of code is taken from the orginal make_laplace_operator...
115
	HalfEdgeIter h = vc.get_halfedge();
109
	HalfEdgeIter h = vc.get_halfedge();
116
	Vec3d nbr(h->vert->pos);
110
	Vec3d nbr(h->vert->pos);
117
	Vec3d left(h->next->vert->pos);
111
	Vec3d left(h->next->vert->pos);
118
	Vec3d right(h->opp->prev->opp->vert->pos);
112
	Vec3d right(h->opp->prev->opp->vert->pos);
119
	
113
	
120
	double d_left = dot(normalize(nbr-left),normalize(vertex-left));
114
	double d_left = dot(normalize(nbr-left),normalize(vertex-left));
121
	double d_right = dot(normalize(nbr-right),normalize(vertex-right));
115
	double d_right = dot(normalize(nbr-right),normalize(vertex-right));
122
	double a_left  = acos(min(1.0, max(-1.0, d_left))); // w positive...?
116
	double a_left  = acos(min(1.0, max(-1.0, d_left))); // w positive...?
123
	double a_right = acos(min(1.0, max(-1.0, d_right)));
117
	double a_right = acos(min(1.0, max(-1.0, d_right)));
124
	
118
	
125
	double w = 1.0/tan(a_left) + 1.0/tan(a_right);
119
	double w = 1.0/tan(a_left) + 1.0/tan(a_right);
126
	return -w;
120
	return -w;
127
}
121
}
128
 
122
 
129
void Harmonics::make_laplace_operator_sparse()
123
void Harmonics::make_laplace_operator_sparse()
130
{
124
{
131
	bool is_generalized=true;
125
	bool is_generalized=true;
132
	CSCMatrixBuilder<double> mb_M;
126
	CSCMatrixBuilder<double> mb_M;
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);
144
			double a_sum = 0;
138
			double a_sum = 0;
145
			for(VertexCirculator vc(v); !vc.end(); ++vc)
139
			for(VertexCirculator vc(v); !vc.end(); ++vc)
146
			{
140
			{
147
				int j = vc.get_vertex()->touched;
141
				int j = vc.get_vertex()->touched;
148
				double entry = calculate_laplace_entry(vc,vertex);
142
				double entry = calculate_laplace_entry(vc,vertex);
149
				if(!is_generalized)
143
				if(!is_generalized)
150
				{
144
				{
151
					double area_j = voronoi_area(vc.get_vertex());
145
					double area_j = voronoi_area(vc.get_vertex());
152
					entry /= sqrt(area_i*area_j);
146
					entry /= sqrt(area_i*area_j);
153
				}
147
				}
154
				if(j > i)
148
				if(j > i)
155
					mb_M.insert_entry(j,entry);
149
					mb_M.insert_entry(j,entry);
156
				a_sum += entry;
150
				a_sum += entry;
157
			}
151
			}
158
			//cout << a_sum << " ";
152
			//cout << a_sum << " ";
159
			mb_M.insert_entry(i,-a_sum);
153
			mb_M.insert_entry(i,-a_sum);
160
			mb_M.next_column();
154
			mb_M.next_column();
161
			
155
			
162
			if(!is_generalized)
156
			if(!is_generalized)
163
				area_i = 1; // if standard S is an identity matrix;
157
				area_i = 1; // if standard S is an identity matrix;
164
			mb_S.insert_entry(i,area_i);
158
			mb_S.insert_entry(i,area_i);
165
			mb_S.next_column_nonsort();
159
			mb_S.next_column_nonsort();
166
			S[i] = area_i;
160
			S[i] = area_i;
167
		}     
161
		}     
168
	
162
	
169
	cout << "solving generalized problem... i = " << mani.no_vertices()<< endl;
163
	cout << "solving generalized problem... i = " << mani.no_vertices()<< endl;
170
	
164
	
171
	
165
	
172
	//+STANDARD
166
	//+STANDARD
173
	//ArpackPP::ARluSymStdEig<double> dprob(50L, mb_M.get_Matrix(), "SA");
167
	//ArpackPP::ARluSymStdEig<double> dprob(50L, mb_M.get_Matrix(), "SA");
174
	//+GENERALIZED (shifted inv mode)
168
	//+GENERALIZED (shifted inv mode)
175
	ARluSymGenEig<double> dprob('S',maximum_eigenvalue, mb_M.get_Matrix(), mb_S.get_Matrix(), 0.0);
169
	ARluSymGenEig<double> dprob('S',maximum_eigenvalue, mb_M.get_Matrix(), mb_S.get_Matrix(), 0.0);
176
	//ArpackPP::ARluSymGenEig<double> dprob(number_of_eigenvalues, mb_M.get_Matrix(), mb_S.get_Matrix());
170
	//ArpackPP::ARluSymGenEig<double> dprob(number_of_eigenvalues, mb_M.get_Matrix(), mb_S.get_Matrix());
177
	
171
	
178
	dprob.FindEigenvectors();
172
	dprob.FindEigenvectors();
179
	int conv = dprob.ConvergedEigenvalues();
173
	int conv = dprob.ConvergedEigenvalues();
180
	cout << conv << " eigenvectors found" << endl;
174
	cout << conv << " eigenvectors found" << endl;
181
	Q.Resize(conv, dprob.GetN());
175
	Q.Resize(conv, dprob.GetN());
182
	V.Resize(conv);
176
	V.Resize(conv);
183
	
177
	
184
	qnorm.Resize(conv);
178
	qnorm.Resize(conv);
185
	for(int i = 0; i < conv; i++)
179
	for(int i = 0; i < conv; i++)
186
	{
180
	{
187
		V[i] = dprob.Eigenvalue(i);
181
		V[i] = dprob.Eigenvalue(i);
188
		qnorm[i] = 0;
182
		qnorm[i] = 0;
189
		for(int j = 0; j < dprob.GetN(); j++)
183
		for(int j = 0; j < dprob.GetN(); j++)
190
		{
184
		{
191
			Q[i][j] = dprob.Eigenvector(i,j);
185
			Q[i][j] = dprob.Eigenvector(i,j);
192
			qnorm[i] += Q[i][j]*Q[i][j];
186
			qnorm[i] += Q[i][j]*Q[i][j];
193
		}
187
		}
194
		
188
		
195
		cout << "(" << i << ":" << sqrt(V[i]/V[1]
189
		cout << "(" << i << ":" << sqrt(V[i]/V[1]
196
										) << ")" << V[i]/V[1] << "V= "<< V[i] << " exp " << exp(-0.01*V[i]/V[1]) << " Qnorm " << qnorm[i]<< endl;
190
										) << ")" << V[i]/V[1] << "V= "<< V[i] << " exp " << exp(-0.01*V[i]/V[1]) << " Qnorm " << qnorm[i]<< endl;
197
	}
191
	}
198
	
192
	
199
	cout  << endl;
193
	cout  << endl;
200
}
194
}
201
 
195
 
202
Harmonics::Harmonics(Manifold& _mani):mani(_mani)
196
Harmonics::Harmonics(Manifold& _mani):mani(_mani)
203
{
197
{
204
	assert(is_initialized);
198
	assert(is_initialized);
205
	
199
	
206
	maximum_eigenvalue = min(size_t(500),mani.no_vertices());
200
	maximum_eigenvalue = min(size_t(500),mani.no_vertices());
207
	
201
	
208
	triangulate(mani);
202
	triangulate(mani);
209
	mani.enumerate_vertices();
203
	mani.enumerate_vertices();
210
	make_laplace_operator_sparse();
204
	make_laplace_operator_sparse();
211
	
205
	
212
	if(maximum_eigenvalue == -1)
206
	if(maximum_eigenvalue == -1)
213
	{
207
	{
214
		cout << "not found" << endl;
208
		cout << "not found" << endl;
215
		return;
209
		return;
216
	}
210
	}
217
	
211
	
218
	proj.resize(maximum_eigenvalue);
212
	proj.resize(maximum_eigenvalue);
219
	
213
	
220
	max_eig_values.resize(maximum_eigenvalue, 1e-10f);
214
	max_eig_values.resize(maximum_eigenvalue, 1e-10f);
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
	}
232
	cout << endl;
226
	cout << endl;
233
}
227
}
234
 
228
 
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));
261
				double a_left  = acos(min(1.0, max(-1.0, d_left)));
254
				double a_left  = acos(min(1.0, max(-1.0, d_left)));
262
				double a_right = acos(min(1.0, max(-1.0, d_right)));
255
				double a_right = acos(min(1.0, max(-1.0, d_right)));
263
				
256
				
264
				double w = 1.0/tan(a_left) + 1.0/tan(a_right);
257
				double w = 1.0/tan(a_left) + 1.0/tan(a_right);
265
				
258
				
266
				Q[i][j] = -w/sqrt(area_i*area_j);						
259
				Q[i][j] = -w/sqrt(area_i*area_j);						
267
				//Q[i][j] = -1;						
260
				//Q[i][j] = -1;						
268
				a_sum += Q[i][j];
261
				a_sum += Q[i][j];
269
			}
262
			}
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);
394
	
381
	
395
	// Create the program
382
	// Create the program
396
	prog_P0 = glCreateProgram();
383
	prog_P0 = glCreateProgram();
397
	
384
	
398
	// Attach all shaders
385
	// Attach all shaders
399
	if(vs) glAttachShader(prog_P0, vs);
386
	if(vs) glAttachShader(prog_P0, vs);
400
	if(fs) glAttachShader(prog_P0, fs);
387
	if(fs) glAttachShader(prog_P0, fs);
401
	
388
	
402
	// Link the program object and print out the info log
389
	// Link the program object and print out the info log
403
	glLinkProgram(prog_P0);
390
	glLinkProgram(prog_P0);
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
}
416
 
402