1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/*
* Pure Data Packet module. mesh implementation
* Copyright (c) by Tom Schouten <tom@zwizwa.be>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/* a very naive approach to triangular meshes */
// $$TODO: some serious memory corruption in this file our the list implementation
#include <math.h>
#include "pdp.h"
#include "pdp_mesh.h"
/* VERTEX methods */
void vertex_add_triangle(t_vertex *v, t_triangle *t)
{
pdp_list_add_pointer(v->trilist, t);
}
void vertex_remove_triangle(t_vertex *v, t_triangle *t)
{
pdp_list_remove_pointer(v->trilist, t);
}
void vertex_add_neighbour(t_vertex *v, t_vertex *neighbour)
{
pdp_list_add_pointer_to_set(v->vertlist, neighbour);
};
/* constructor/destructors are "private"
they may only be called by the mesh object to ensure
the vector list stays sound (i.e. without duplicates) */
void _vertex_free(t_vertex *v)
{
if (!v->trilist) post("WARNING: vertex %x has empty trilist", v);
else{
pdp_list_free(v->trilist);
v->trilist = 0;
}
if (!v->vertlist) post("WARNING: vertex %x has empty vertlist", v);
{
pdp_list_free(v->vertlist);
v->vertlist = 0;
}
pdp_dealloc(v);
}
t_vertex *_vertex_new(float *c, float *n)
{
int k;
t_vertex *v = (t_vertex *) pdp_alloc(sizeof(t_vertex));
I3(k) v->c[k] = c[k];
I3(k) v->n[k] = n[k];
v->trilist = pdp_list_new(0);
v->vertlist = pdp_list_new(0);
return v;
}
void vertex_compute_normal_random(t_vertex *v){int k; I3(k) v->n[k] = _rand();}
void vertex_compute_normal_sphere(t_vertex *v){int k; I3(k) v->n[k] = v->c[k];}
void vertex_compute_normal_prism(t_vertex *v)
{
float scale = 0.0f;
float sum[] = {0.0f, 0.0f, 0.0f};
int k;
t_pdp_atom* i;
t_pdp_list *vl = v->vertlist;
t_vertex *vtx;
PDP_POINTER_IN(vl, i, vtx) {
I3(k) sum[k] += vtx->c[k];
scale = scale + 1.0f;
}
scale = 1.0f / scale;
I3(k) sum[k] *= scale;
I3(k) v->n[k] = v->c[k] - sum[k];
//post("computed normal (%f, %f, %f) of vertex (%f, %f, %f)", v->n[0], v->n[1], v->n[2], v->c[0], v->c[1], v->c[2]);
};
void vertex_compute_normal_average(t_vertex *v)
{
int triangles = pdp_list_size(v->trilist);
float scale = 1.0f / ((float)triangles);
t_pdp_atom* i;
int k;
t_triangle *t;
I3(k) v->n[k] = 0; //reset normal
PDP_POINTER_IN(v->trilist, i, t){
I3(k) v->n[k] += t->n[k];
}
_vector3_scale(v->n, scale);
}
float vertex_normalize(t_vertex *v)
{
return _vector3_normalize(v->c);
}
/* TRIANGLE methods */
/* create triangle (a connection between 3 vertices):
counterclockwize with facing front
this method is "private"
you can only create triangles as part of a mesh */
t_triangle *_triangle_new(t_vertex *v0, t_vertex *v1, t_vertex *v2)
{
int k;
t_triangle *t = (t_triangle *)pdp_alloc(sizeof(t_triangle));
/* store vertex references */
t->v[0] = v0;
t->v[1] = v1;
t->v[2] = v2;
/* reset median vertices */
I3(k) t->m[k] = 0;
/* connect triangle to vertices */
vertex_add_triangle(v0, t);
vertex_add_triangle(v1, t);
vertex_add_triangle(v2, t);
/* connect vertices to vertices */
vertex_add_neighbour(v0, v1);
vertex_add_neighbour(v0, v2);
vertex_add_neighbour(v1, v0);
vertex_add_neighbour(v1, v2);
vertex_add_neighbour(v2, v0);
vertex_add_neighbour(v2, v1);
return t;
}
/* delete a triangle, disconnecting the vertices */
void _triangle_free(t_triangle *t)
{
int k;
/* remove the triangle reference of the vertices */
I3(k) vertex_remove_triangle(t->v[k], t);
/* set references to zero (bug catcher) */
I3(k) t->v[k] = 0;
I3(k) t->m[k] = 0;
/* free struct */
pdp_dealloc(t);
}
/* get triangle that shares the link between v0 and v1 */
t_triangle *triangle_neighbour(t_triangle *t, t_vertex *v0, t_vertex *v1)
{
t_pdp_atom* it;
t_triangle *tri;
PDP_POINTER_IN(v1->trilist, it, tri){
if (tri != t && pdp_list_contains_pointer(v0->trilist, tri)) return tri;
}
return 0;
}
/* add a median vector to a link in a triangle
note: vertices must be in triangle, or behaviour is undefined */
void triangle_add_median(t_triangle *t, t_vertex *v0, t_vertex *v1, t_vertex *median)
{
/* link 0 1 */
if (!((v0 == t->v[2]) || (v1 == t->v[2]))) t->m[0] = median;
/* link 1 2 */
else if (!((v0 == t->v[0]) || (v1 == t->v[0]))) t->m[1] = median;
/* link 2 0 */
else t->m[2] = median;
}
void triangle_compute_normal(t_triangle *t)
{
int k;
float v0[3];
float v1[3];
I3(k) v0[k] = t->v[1]->c[k] - t->v[0]->c[k];
I3(k) v1[k] = t->v[2]->c[k] - t->v[0]->c[k];
_vector3_cross(v0,v1,t->n);
}
void triangle_compute_unit_normal(t_triangle *t)
{
triangle_compute_normal(t);
_vector3_normalize(t->n);
}
/* MESH methods */
/* add and remove methods for vertices and triangles */
t_vertex *mesh_vertex_add(t_mesh *m, float *c, float *n)
{
t_vertex *v = _vertex_new(c, n);
pdp_list_add_pointer(m->vertices, v);
return v;
}
void mesh_vertex_remove(t_mesh *m, t_vertex *v)
{
pdp_list_remove_pointer(m->vertices, v);
_vertex_free(v);
}
t_triangle *mesh_triangle_add(t_mesh *m, t_vertex *v0, t_vertex *v1, t_vertex *v2)
{
t_triangle *t = _triangle_new(v0,v1,v2);
pdp_list_add_pointer(m->triangles, t);
return t;
}
void mesh_triangle_remove(t_mesh *m, t_triangle *t)
{
pdp_list_remove_pointer(m->triangles, t);
_triangle_free(t);
}
/* calculate normals */
void mesh_calculate_normals(t_mesh *m)
{
t_pdp_atom* it;
t_pdp_atom* it_tri;
t_pdp_list *l = m->vertices;
t_pdp_list *l_tri = m->triangles;
t_vertex *v;
t_triangle *t;
//while (v = pdp_list_getnext_pointer(l, &it)) vertex_compute_normal_sphere(v);
switch(m->normal_type){
default:
case MESH_NORMAL_SPHERE: PDP_POINTER_IN(l, it, v) vertex_compute_normal_sphere(v); break;
case MESH_NORMAL_PRISM: PDP_POINTER_IN(l, it, v) vertex_compute_normal_prism(v); break;
case MESH_NORMAL_RANDOM: PDP_POINTER_IN(l, it, v) vertex_compute_normal_random(v); break;
case MESH_NORMAL_AVERAGE:
PDP_POINTER_IN(l_tri, it_tri, t) triangle_compute_unit_normal(t);
PDP_POINTER_IN(l, it, v) vertex_compute_normal_average(v);
break;
}
}
/* split a triangle in 4, using the intermedia median vertex storage */
void mesh_split_four(t_mesh *m, t_triangle *old_t)
{
int k;
t_vertex *v[6];
/* some intermediates */
t_triangle *neighbour;
t_float newv[] = {0,0,0};
t_float nullvect[] = {0,0,0};
/* get main vertices */
I3(k) v[k] = old_t->v[k];
/* get median vertices inserted by neighbouring triangles */
I3(k) v[k+3] = old_t->m[k];
#define GET_MEDIAN(v, v0, v1) \
if (!v){ \
I3(k) newv[k] = 0.5f * (v0->c[k] + v1->c[k]); \
v = mesh_vertex_add(m, newv, nullvect); \
/*vertex_normalize(v);*/ \
if (neighbour = triangle_neighbour(old_t, v0, v1)){ \
triangle_add_median(neighbour, v0, v1, v); \
} \
}
GET_MEDIAN(v[3], v[0], v[1])
GET_MEDIAN(v[4], v[1], v[2])
GET_MEDIAN(v[5], v[2], v[0])
#undef GET_MEDIAN
/* remove the old triangle */
mesh_triangle_remove(m, old_t);
/* create 4 new triangles */
mesh_triangle_add(m, v[0], v[3], v[5]);
mesh_triangle_add(m, v[1], v[4], v[3]);
mesh_triangle_add(m, v[2], v[5], v[4]);
mesh_triangle_add(m, v[3], v[4], v[5]);
}
/* split a triangle in 3 */
void mesh_split_three(t_mesh *m, t_triangle *old_t)
{
int k, l;
t_vertex *v[4];
t_float newv[] = {0,0,0};
t_float nullvect[] = {0,0,0};
/* get vertices */
I3(k) v[k] = old_t->v[k];
/* remove a triangle */
mesh_triangle_remove(m, old_t);
/* compute new vertex coordinates */
I3(k) I3(l) newv[k] += 0.33333f * v[l]->c[k];
/* create new vertex */
v[3] = mesh_vertex_add(m, newv, nullvect);
//vertex_normalize(v[3]);
/* create 3 new triangles */
mesh_triangle_add(m, v[0], v[1], v[3]);
mesh_triangle_add(m, v[1], v[2], v[3]);
mesh_triangle_add(m, v[2], v[0], v[3]);
}
void mesh_split_all_four(t_mesh *m)
{
t_triangle *t;
t_pdp_list *l = pdp_list_copy(m->triangles);
//post("split_all_four: nb triangles %d", pdp_list_size(m->triangles));
while (l->elements){
t = pdp_list_pop(l).w_pointer;
mesh_split_four(m, t);
}
mesh_calculate_normals(m);
pdp_list_free(l);
}
void mesh_split_all_three(t_mesh *m)
{
t_triangle *t;
t_pdp_list *l = pdp_list_copy(m->triangles);
//post("split_all_three: nb triangles %d", pdp_list_size(m->triangles));
while (l->elements){
t = pdp_list_pop(l).w_pointer;
mesh_split_three(m, t);
}
mesh_calculate_normals(m);
pdp_list_free(l);
}
void mesh_split_random_three(t_mesh *m)
{
int size = pdp_list_size(m->triangles);
t_triangle *t = pdp_list_index(m->triangles, (random() % size)).w_pointer;
mesh_split_three(m, t);
mesh_calculate_normals(m);
}
void mesh_free(t_mesh *m)
{
t_pdp_list *l;
t_triangle *t;
t_vertex *v;
/* delete all triangles */
while (m->triangles->elements){
t = pdp_list_pop(m->triangles).w_pointer;
//post("freeing triangle %x", t);
_triangle_free(t);
}
pdp_list_free(m->triangles);
m->triangles = 0;
/* delete all vertices */
while (m->vertices->elements){
v = pdp_list_pop(m->vertices).w_pointer;
//post("freeing vertex %x", v);
_vertex_free(v);
}
pdp_list_free(m->vertices);
m->vertices = 0;
pdp_dealloc(m);
}
t_mesh *_mesh_new(void)
{
t_mesh *m = (t_mesh *)pdp_alloc(sizeof(t_mesh));
/* create main vertex and triangle lists */
m->triangles = pdp_list_new(0);
m->vertices = pdp_list_new(0);
/* set normal type */
m->normal_type = MESH_NORMAL_PRISM;
return m;
}
/* init tetra */
t_mesh *mesh_new_tetra(void)
{
int k;
t_triangle *t[4];
t_vertex *v[4];
t_pdp_atom* it;
t_triangle *tri;
t_mesh *m = _mesh_new();
float n[] = {0,0,0};
float fv[4][3] = {{2,0,0},{0,2,0},{0,0,2}, {-1,-1,-1}};
/* add vertices */
I4(k) v[k] = mesh_vertex_add(m, &fv[k][0], n);
I4(k) vertex_normalize(v[k]);
/* add triangles */
mesh_triangle_add(m, v[0], v[1], v[2]);
mesh_triangle_add(m, v[1], v[0], v[3]);
mesh_triangle_add(m, v[0], v[2], v[3]);
mesh_triangle_add(m, v[1], v[3], v[2]);
/* compute normals */
mesh_calculate_normals(m);
return m;
}
void _mesh_relax_compute_resultant_spring(t_mesh *m, float *center, float d0, float r0)
{
int k;
t_pdp_atom *i, *j;
t_vertex *v, *w;
PDP_POINTER_IN(m->vertices, i, v){
float scale = 0.0f;
float r;
/* compute contribution of origin link */
I3(k) v->n[k] = v->c[k] - center[k];
r = _vector3_normalize(v->n);
I3(k) v->n[k] *= (r0 - r);
PDP_POINTER_IN(v->vertlist, j, w){
int k;
float f[3];
float d, l;
/* compute force contribution of one link (model: spring with rest length == d0) */
I3(k) f[k] = w->c[k] - v->c[k]; // PC: f == distance vector
d = _vector3_normalize(f); // PC: d == distance, vector == unit norm
I3(k) v->n[k] += (d - d0) * f[k]; // PC: n == n_prev + fource resultant
}
}
}
void _mesh_relax_apply_force(t_mesh *m, float k)
{
t_pdp_atom* it;
t_vertex *v;
PDP_POINTER_IN(m->vertices, it, v){
int i;
/* apply fource vector with step */
I3(i) v->c[i] += k * v->n[i];
}
}
void mesh_compute_center(t_mesh *m, float *c)
{
t_pdp_atom*(it);
t_vertex *v;
float scale;
int k;
I3(k) c[k] = 0;
PDP_POINTER_IN(m->vertices, it, v){
I3(k) c[k] += v->c[k];
}
scale = 1.0f / ((float)pdp_list_size(m->vertices));
I3(k) c[k] *= scale;
}
void mesh_translate(t_mesh *m, float *c)
{
t_pdp_atom *it;
t_vertex *v;
int k;
PDP_POINTER_IN(m->vertices, it, v){
I3(k) v->c[k] += c[k];
}
}
/* relax a mesh (move toward equal link length) */
void mesh_relax(t_mesh *m, float step, float d0, float r0)
{
int k;
float c[3];
mesh_compute_center(m, c);
I3(k) c[k] = -c[k];
mesh_translate(m, c);
I3(k) c[k] = 0;
_mesh_relax_compute_resultant_spring(m, c, d0, r0); /* compute force resultant */
_mesh_relax_apply_force(m, step); /* apply "time step towards desired distance" */
mesh_calculate_normals(m); /* restore normals */
}
/* print some debug information */
void mesh_debug(t_mesh *m)
{
int k;
int boundary_edges = 0;
t_pdp_atom* it;
t_triangle *t;
post("mesh info");
post("\tnumber of vertices = %d", pdp_list_size(m->vertices));
post("\tnumber of triangles = %d", pdp_list_size(m->triangles));
PDP_POINTER_IN(m->triangles, it, t){
I3(k) if (!triangle_neighbour(t, t->v[k], t->v[(k+1)%3])) boundary_edges++;
}
post("\tnumber of boundaray edges = %d", boundary_edges);
}
|