Subversion Repositories gelsvn

Rev

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

Rev 388 Rev 391
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
#include <CGLA/Vec3f.h>
11
#include <CGLA/Vec3f.h>
12
#include <CGLA/Vec3d.h>
12
#include <CGLA/Vec3d.h>
13
#include <LinAlg/Matrix.h>
13
#include <LinAlg/Matrix.h>
14
#include <LinAlg/Vector.h>
14
#include <LinAlg/Vector.h>
15
#include <LinAlg/LapackFunc.h>
15
#include <LinAlg/LapackFunc.h>
16
 
16
 
17
#include <GL/glew.h>
17
#include <GL/glew.h>
18
#include <GLGraphics/glsl_shader.h>
18
#include <GLGraphics/glsl_shader.h>
19
 
19
 
20
#include <HMesh/Manifold.h>
20
#include <HMesh/Manifold.h>
21
#include <HMesh/VertexCirculator.h>
21
#include <HMesh/VertexCirculator.h>
22
#include <HMesh/FaceCirculator.h>
22
#include <HMesh/FaceCirculator.h>
23
#include <HMesh/build_manifold.h>
23
#include <HMesh/build_manifold.h>
24
#include <HMesh/mesh_optimization.h>
24
#include <HMesh/mesh_optimization.h>
25
#include <HMesh/triangulate.h>
25
#include <HMesh/triangulate.h>
26
#include <HMesh/load.h>
26
#include <HMesh/load.h>
27
#include <HMesh/x3d_save.h>
27
#include <HMesh/x3d_save.h>
28
 
28
 
29
#include <GLConsole/GLConsole.h>
29
#include <GLConsole/GLConsole.h>
30
 
30
 
31
#include "harmonics.h"
31
#include "harmonics.h"
32
#include "wireframe.h"
32
#include "wireframe.h"
33
 
33
 
34
using namespace CGLA;
34
using namespace CGLA;
35
using namespace std;
35
using namespace std;
36
using namespace HMesh;
36
using namespace HMesh;
37
using namespace Geometry;
37
using namespace Geometry;
38
using namespace GLGraphics;
38
using namespace GLGraphics;
39
using namespace LinAlg;
39
using namespace LinAlg;
40
 
40
 
41
int E = 1; 	
41
bool Harmonics::is_initialized=false;
42
int MAX_E;
42
GLuint Harmonics::prog_P0;
43
 
43
 
44
 
44
 
45
namespace
45
namespace
46
{
46
{
47
	GLuint prog_P0;
-
 
48
	
-
 
49
	vector<double> projx;
-
 
50
	vector<double> projy;
-
 
51
	vector<double> projz;
-
 
52
	
-
 
53
	CMatrix Q;
-
 
54
	CVector V;
-
 
55
	vector<float> max_eig_values;
-
 
56
	vector<Vec3f> vertex_positions;
-
 
57
	
47
	
58
	double voronoi_area(VertexIter v)
48
	double voronoi_area(VertexIter v)
59
	{
49
	{
60
	double area_mixed = 0;
50
	double area_mixed = 0;
61
	//For each triangle T from the 1-ring neighborhood of x
51
	//For each triangle T from the 1-ring neighborhood of x
62
	for(VertexCirculator vc(v); !vc.end(); ++vc)
52
	for(VertexCirculator vc(v); !vc.end(); ++vc)
63
	{
53
	{
64
		FaceIter f = vc.get_face();
54
		FaceIter f = vc.get_face();
65
		double f_area = area(f);
55
		double f_area = area(f);
66
		
56
		
67
		HalfEdgeIter he = vc.get_halfedge();
57
		HalfEdgeIter he = vc.get_halfedge();
68
		Vec3d v1(he->vert->pos);
58
		Vec3d v1(he->vert->pos);
69
		Vec3d v2(he->next->vert->pos);
59
		Vec3d v2(he->next->vert->pos);
70
		Vec3d v0(he->next->next->vert->pos);
60
		Vec3d v0(he->next->next->vert->pos);
71
		
61
		
72
		double a0 = acos(dot(v1-v0, v2-v0)/(length(v1-v0)*length(v2-v0)));
62
		double a0 = acos(dot(v1-v0, v2-v0)/(length(v1-v0)*length(v2-v0)));
73
		double a1 = acos(dot(v2-v1, v0-v1)/(length(v2-v1)*length(v0-v1)));
63
		double a1 = acos(dot(v2-v1, v0-v1)/(length(v2-v1)*length(v0-v1)));
74
		double a2 = acos(dot(v0-v2, v1-v2)/(length(v0-v2)*length(v1-v2)));
64
		double a2 = acos(dot(v0-v2, v1-v2)/(length(v0-v2)*length(v1-v2)));
75
		
65
		
76
		if(a0>(M_PI/2.0) && a1>(M_PI/2.0) && a2>(M_PI/2.0)) // f is non-obtuse
66
		if(a0>(M_PI/2.0) && a1>(M_PI/2.0) && a2>(M_PI/2.0)) // f is non-obtuse
77
		{
67
		{
78
			// Add Voronoi formula (see Section 3.3)
68
			// Add Voronoi formula (see Section 3.3)
79
			area_mixed += (1.0/8) * 
69
			area_mixed += (1.0/8) * 
80
			((1.0/tan(a1)) * sqr_length(v2-v0) + 
70
			((1.0/tan(a1)) * sqr_length(v2-v0) + 
81
			 (1.0/tan(a2)) * sqr_length(v1-v0));
71
			 (1.0/tan(a2)) * sqr_length(v1-v0));
82
		}
72
		}
83
		else // Voronoi inappropriate
73
		else // Voronoi inappropriate
84
		{
74
		{
85
			// Add either area(f)/4 or area(f)/2
75
			// Add either area(f)/4 or area(f)/2
86
			if(a0>M_PI/2.0)// the angle of f at x is obtuse
76
			if(a0>M_PI/2.0)// the angle of f at x is obtuse
87
				area_mixed += f_area/2;
77
				area_mixed += f_area/2;
88
			else
78
			else
89
				area_mixed += f_area/4;
79
				area_mixed += f_area/4;
90
		}
80
		}
91
	}
81
	}
92
	return area_mixed;
82
	return area_mixed;
93
}
83
}
94
 
84
 
95
	double barycentric_area(VertexIter v)
85
	double barycentric_area(VertexIter v)
96
	{
86
	{
97
	double barea = 0;
87
	double barea = 0;
98
	//For each triangle T from the 1-ring neighborhood of x
88
	//For each triangle T from the 1-ring neighborhood of x
99
	for(VertexCirculator vc(v); !vc.end(); ++vc)
89
	for(VertexCirculator vc(v); !vc.end(); ++vc)
100
	{
90
	{
101
		FaceIter f = vc.get_face();
91
		FaceIter f = vc.get_face();
102
		barea += area(f)/3.0;
92
		barea += area(f)/3.0;
103
	}
93
	}
104
	return barea;
94
	return barea;
105
}
95
}
106
 
96
 
107
}
97
}
108
 
98
 
109
void make_laplace_operator(Manifold& mani)
99
void Harmonics::make_laplace_operator()
110
{
100
{
111
	Q.Resize(mani.no_vertices(), mani.no_vertices());
101
	Q.Resize(mani.no_vertices(), mani.no_vertices());
112
	
102
	
113
	for(VertexIter v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
103
	for(VertexIter v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
114
		if(!is_boundary(v))
104
		if(!is_boundary(v))
115
		{
105
		{
116
			int i = v->touched;
106
			int i = v->touched;
117
			double area_i = voronoi_area(v);
107
			double area_i = voronoi_area(v);
118
			Vec3d vertex(v->pos);
108
			Vec3d vertex(v->pos);
119
			Vec3d curv_normal(0);
109
			Vec3d curv_normal(0);
120
			double a_sum = 0;
110
			double a_sum = 0;
121
			for(VertexCirculator vc(v); !vc.end(); ++vc)
111
			for(VertexCirculator vc(v); !vc.end(); ++vc)
122
			{
112
			{
123
				int j = vc.get_vertex()->touched;
113
				int j = vc.get_vertex()->touched;
124
				double area_j = voronoi_area(vc.get_vertex());
114
				double area_j = voronoi_area(vc.get_vertex());
125
				HalfEdgeIter h = vc.get_halfedge();
115
				HalfEdgeIter h = vc.get_halfedge();
126
				Vec3d nbr(h->vert->pos);
116
				Vec3d nbr(h->vert->pos);
127
				Vec3d left(h->next->vert->pos);
117
				Vec3d left(h->next->vert->pos);
128
				Vec3d right(h->opp->prev->opp->vert->pos);
118
				Vec3d right(h->opp->prev->opp->vert->pos);
129
				
119
				
130
				double d_left = dot(normalize(nbr-left),
120
				double d_left = dot(normalize(nbr-left),
131
									normalize(vertex-left));
121
									normalize(vertex-left));
132
				double d_right = dot(normalize(nbr-right),
122
				double d_right = dot(normalize(nbr-right),
133
									 normalize(vertex-right));
123
									 normalize(vertex-right));
134
				double a_left  = acos(min(1.0, max(-1.0, d_left)));
124
				double a_left  = acos(min(1.0, max(-1.0, d_left)));
135
				double a_right = acos(min(1.0, max(-1.0, d_right)));
125
				double a_right = acos(min(1.0, max(-1.0, d_right)));
136
				
126
				
137
				double w = 1.0/tan(a_left) + 1.0/tan(a_right);
127
				double w = 1.0/tan(a_left) + 1.0/tan(a_right);
138
				
128
				
139
				Q[i][j] = -w/sqrt(area_i*area_j);						
129
				Q[i][j] = -w/sqrt(area_i*area_j);						
140
				//Q[i][j] = -1;						
130
				//Q[i][j] = -1;						
141
				a_sum += Q[i][j];
131
				a_sum += Q[i][j];
142
			}
132
			}
143
			Q[i][i] = -a_sum;
133
			Q[i][i] = -a_sum;
144
		}
134
		}
145
	EigenSolutionsSym(Q,V);
135
	EigenSolutionsSym(Q,V);
146
	
136
	
147
}
137
}
148
 
138
 
149
 
139
 
150
void analyze_mesh(Manifold& mani)
140
Harmonics::Harmonics(Manifold& _mani):mani(_mani)
151
{
141
{
152
	static bool was_here = false;
142
	assert(is_initialized);
153
	if(!was_here)
-
 
154
	{
143
		
155
		triangulate(mani);
144
	triangulate(mani);
156
		mani.enumerate_vertices();
145
	mani.enumerate_vertices();
157
		MAX_E = mani.no_vertices()-1;
146
	maximum_eigenvalue = mani.no_vertices()-1;
158
		E = min(E, MAX_E);
-
 
159
		was_here = true;
-
 
160
		make_laplace_operator(mani);
147
	make_laplace_operator();
161
		vertex_positions.resize(mani.no_vertices());
-
 
162
	}
148
	
163
	projx.resize(MAX_E);
-
 
164
	projy.resize(MAX_E);
-
 
165
	projz.resize(MAX_E);
149
	proj.resize(maximum_eigenvalue);
166
	max_eig_values.resize(MAX_E, 1e-10);
150
	max_eig_values.resize(maximum_eigenvalue, 1e-10);
167
	for(int es=0; es<MAX_E; ++es)
151
	for(int es=0; es<maximum_eigenvalue; ++es)
168
	{
152
	{
169
		for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
153
		for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
170
		{
154
		{
171
			projx[es] += v->pos[0] * Q[es][v->touched];
-
 
172
			projy[es] += v->pos[1] * Q[es][v->touched];
155
			int i= v->touched;
173
			projz[es] += v->pos[2] * Q[es][v->touched];
156
			proj[es] += Vec3d(v->pos) * Q[es][i];
174
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][v->touched])));
157
			max_eig_values[es] = max(max_eig_values[es], static_cast<float>(abs(Q[es][i])));
175
		}
158
		}
176
	}
159
	}
177
	
-
 
178
	reconstruct(mani, MAX_E);
-
 
179
}
160
}
180
 
161
 
181
void add_frequency(Manifold& mani, int es, float scale)
162
void Harmonics::add_frequency(int es, float scale)
182
{
163
{
183
	if(es<MAX_E)
164
	if(es<maximum_eigenvalue)
184
		for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
165
		for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
185
		{
166
		{
186
			Vec3f& p = vertex_positions[v->touched]; 
167
			Vec3f& p = v->pos; 
187
			p[0] += projx[es]* Q[es][v->touched] * scale;
168
			v->pos += Vec3f(proj[es] * Q[es][v->touched] * scale);
188
			p[1] += projy[es]* Q[es][v->touched] * scale;
-
 
189
			p[2] += projz[es]* Q[es][v->touched] * scale;
-
 
190
		}
169
		}
191
	
170
	
192
}
171
}
193
 
172
 
194
void reset_shape(Manifold& mani)
173
void Harmonics::reset_shape()
195
{
174
{
196
	for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
175
	for(VertexIter v=mani.vertices_begin(); v != mani.vertices_end(); ++v)
197
		vertex_positions[v->touched] = Vec3f(0);	
176
		v->pos = Vec3f(0);	
198
}
-
 
199
 
-
 
200
void reconstruct(Manifold& mani, int E)
-
 
201
{
-
 
202
	reset_shape(mani);
-
 
203
	for(int es=0;es<=E;++es)
-
 
204
		add_frequency(mani,es);
-
 
205
}
177
}
206
 
-
 
207
void partial_reconstruct(Manifold& mani, int E0, int E1, float scale)
178
void Harmonics::partial_reconstruct(int E0, int E1, float scale)
208
{
179
{
209
	for(int es=E0;es<=E1;++es)
180
	for(int es=E0;es<=E1;++es)
210
		add_frequency(mani,es, scale);
181
		add_frequency(es, scale);
211
}
182
}
212
 
183
 
213
 
184
 
214
template<typename T>
185
template<typename T>
215
T& get_CVar_ref(const std::string& s)
186
T& get_CVar_ref(const std::string& s)
216
{
187
{
217
	return *reinterpret_cast<T*> (GetCVarData(s));
188
	return *reinterpret_cast<T*> (GetCVarData(s));
218
}
189
}
219
 
190
 
220
void draw_eigenvalues(Manifold& m)
191
void Harmonics::parse_key(unsigned char key)
-
 
192
{
-
 
193
		int& display_eigenvalue = get_CVar_ref<int>("display.harmonics.eigenvalue");
-
 
194
		int& display_diffuse = get_CVar_ref<int>("display.harmonics.diffuse");
-
 
195
		int& display_highlight = get_CVar_ref<int>("display.harmonics.highlight");
-
 
196
		switch(key) {
-
 
197
			case '+': 
-
 
198
				display_eigenvalue = min(display_eigenvalue+1, maximum_eigenvalue); 
-
 
199
				break;
-
 
200
			case '-': 
-
 
201
				display_eigenvalue = max(display_eigenvalue-1, 0); 
-
 
202
				break;
-
 
203
			case 'd':	
-
 
204
				display_diffuse = !display_diffuse; 
-
 
205
				break;
-
 
206
			case 'h':
-
 
207
				display_highlight = !display_highlight;
-
 
208
				break;			
-
 
209
		}
-
 
210
 
-
 
211
}
-
 
212
 
-
 
213
void Harmonics::draw()
221
{
214
{
222
	int& display_eigen = get_CVar_ref<int>("display.harmonics.eigenvalue");
215
	int& display_eigen = get_CVar_ref<int>("display.harmonics.eigenvalue");
223
	int& display_eigen2 = get_CVar_ref<int>("display.harmonics.eigenvalue");
216
	int& display_eigen2 = get_CVar_ref<int>("display.harmonics.eigenvalue");
224
	int& do_diffuse = get_CVar_ref<int>("display.harmonics.diffuse");
217
	int& do_diffuse = get_CVar_ref<int>("display.harmonics.diffuse");
225
	int& do_highlight = get_CVar_ref<int>("display.harmonics.highlight");
218
	int& do_highlight = get_CVar_ref<int>("display.harmonics.highlight");
226
 
219
 
227
 
220
 
228
	glUseProgram(prog_P0);
221
	glUseProgram(prog_P0);
229
	glUniform1f(glGetUniformLocation(prog_P0,"eig_max"),max_eig_values[display_eigen]);
222
	glUniform1f(glGetUniformLocation(prog_P0,"eig_max"),max_eig_values[display_eigen]);
230
	glUniform1f(glGetUniformLocation(prog_P0,"eig_max2"),max_eig_values[display_eigen2]);
223
	glUniform1f(glGetUniformLocation(prog_P0,"eig_max2"),max_eig_values[display_eigen2]);
231
	glUniform1i(glGetUniformLocation(prog_P0,"do_diffuse"),do_diffuse);
224
	glUniform1i(glGetUniformLocation(prog_P0,"do_diffuse"),do_diffuse);
232
   	glUniform1i(glGetUniformLocation(prog_P0,"do_highlight"),do_highlight);
225
   	glUniform1i(glGetUniformLocation(prog_P0,"do_highlight"),do_highlight);
233
	GLuint attrib = glGetAttribLocationARB(prog_P0, "eigenvalue");
226
	GLuint attrib = glGetAttribLocationARB(prog_P0, "eigenvalue");
234
	GLuint attrib2 = glGetAttribLocationARB(prog_P0, "eigenvalue2");
227
	GLuint attrib2 = glGetAttribLocationARB(prog_P0, "eigenvalue2");
235
	
228
	
236
	glFrontFace(GL_CW);
229
	glFrontFace(GL_CW);
237
	for(FaceIter f=m.faces_begin(); f != m.faces_end(); ++f)
230
	for(FaceIter f=mani.faces_begin(); f != mani.faces_end(); ++f)
238
	{
231
	{
239
		FaceCirculator fc(f);
232
		FaceCirculator fc(f);
240
		glBegin(GL_TRIANGLES);
233
		glBegin(GL_TRIANGLES);
241
		while(!fc.end())
234
		while(!fc.end())
242
		{
235
		{
243
			int i = fc.get_vertex()->touched;
236
			int i = fc.get_vertex()->touched;
244
			glVertexAttrib1f(attrib,Q[display_eigen][i]);
237
			glVertexAttrib1f(attrib,Q[display_eigen][i]);
245
			glVertexAttrib1f(attrib2,Q[display_eigen2][i]);
238
			glVertexAttrib1f(attrib2,Q[display_eigen2][i]);
246
			glNormal3fv(normal(fc.get_vertex()).get());
239
			glNormal3fv(normal(fc.get_vertex()).get());
247
			glVertex3fv(vertex_positions[i].get());
240
			glVertex3fv(fc.get_vertex()->pos.get());
248
			++fc;
241
			++fc;
249
		}
242
		}
250
		glEnd();
243
		glEnd();
251
	}
244
	}
252
	glFrontFace(GL_CCW);
245
	glFrontFace(GL_CCW);
253
	glUseProgram(0);
246
	glUseProgram(0);
254
}
247
}
255
 
248
 
256
void init_harmonics()
249
void Harmonics::init()
257
{
250
{
-
 
251
	is_initialized = true;
258
	string shader_path = "/Users/jab/GEL/apps/MeshEdit/";
252
	string shader_path = "/Users/jab/GEL/apps/MeshEdit/";
259
	GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, shader_path, "tri.vert");
253
	GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, shader_path, "tri.vert");
260
	GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, shader_path, "tri.frag");
254
	GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, shader_path, "tri.frag");
261
	
255
	
262
	// Create the program
256
	// Create the program
263
	prog_P0 = glCreateProgram();
257
	prog_P0 = glCreateProgram();
264
	
258
	
265
	// Attach all shaders
259
	// Attach all shaders
266
	if(vs) glAttachShader(prog_P0, vs);
260
	if(vs) glAttachShader(prog_P0, vs);
267
	if(fs) glAttachShader(prog_P0, fs);
261
	if(fs) glAttachShader(prog_P0, fs);
268
	
262
	
269
	// Link the program object and print out the info log
263
	// Link the program object and print out the info log
270
	glLinkProgram(prog_P0);
264
	glLinkProgram(prog_P0);
271
	print_glsl_program_log(prog_P0);
265
	print_glsl_program_log(prog_P0);
272
	
266
	
273
	// Install program object as part of current state
267
	// Install program object as part of current state
274
	glUseProgram(prog_P0);
268
	glUseProgram(prog_P0);
275
	
269
	
276
	static CVar<int> display_eigen("display.harmonics.eigenvalue",0);
270
	static CVar<int> display_eigen("display.harmonics.eigenvalue",0);
277
	static CVar<int> display_eigen2("display.harmonics.eigenvalue2",0);
271
	static CVar<int> display_eigen2("display.harmonics.eigenvalue2",0);
278
	static CVar<int> do_highlight("display.harmonics.highlight",1);
272
	static CVar<int> do_highlight("display.harmonics.highlight",1);
279
	static CVar<int> do_diffuse("display.harmonics.diffuse",1);
273
	static CVar<int> do_diffuse("display.harmonics.diffuse",1);
280
 
274
 
281
}
275
}
282
 
276