Subversion Repositories gelsvn

Rev

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