/***************************************************************************** * Copyright 2005 Kevin Hobbs * * * * This file is part of Balloon. * * * * Balloon 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. * * * * Balloon 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 Balloon; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *****************************************************************************/ #include #include #include "mesh.h" #include "utility.h" struct point * new_empty_point(const size_t ndims, size_t id) { int i; struct point * n_e_p = xmalloc(sizeof(struct point)); n_e_p ->id = id; n_e_p -> x_in = xmalloc(sizeof(double) * ndims); n_e_p -> s = xmalloc(sizeof(double) * ndims); n_e_p -> neighbor_count = 0; n_e_p -> neighbors = xmalloc(sizeof(struct point *) * ndims); n_e_p -> neighbor_buffer_length = ndims; for (i = 0; i < ndims; i++) n_e_p -> neighbors[i] = NULL; return n_e_p; } void free_point(struct point * p) { free(p -> neighbors); free(p -> x_in); free(p -> s); free(p); } void connect_points(struct point * p1, struct point * p2) { size_t i; /*************************/ /* Don't connect to self */ /*************************/ if (p1 == p2) return; /************************/ /* Don't double connect */ /************************/ for ( i = 0; i < p1 -> neighbor_count; i++) if ( p1 -> neighbors[i] == p2 ) return; for ( i = 0; i < p2 -> neighbor_count; i++) if ( p2 -> neighbors[i] == p1 ) return; /************************************************/ /* Make room for more connections if we need it */ /************************************************/ if ( p1 -> neighbor_count == p1 -> neighbor_buffer_length ) { p1 -> neighbor_buffer_length = p1 -> neighbor_buffer_length * 2; p1 -> neighbors = xrealloc(p1 -> neighbors, sizeof(struct point *) * p1 -> neighbor_buffer_length); for ( i = p1 -> neighbor_count; i < p1 -> neighbor_buffer_length; i++) p1 -> neighbors[i] = NULL; } /***********************/ /* Make the connection */ /***********************/ p1 -> neighbors[p1 -> neighbor_count] = p2; p1 -> neighbor_count++; /*******************************************/ /* Do it all again for the other direction */ /*******************************************/ /************************************************/ /* Make room for more connections if we need it */ /************************************************/ if ( p2 -> neighbor_count == p2 -> neighbor_buffer_length ) { p2 -> neighbor_buffer_length = p2 -> neighbor_buffer_length * 2; p2 -> neighbors = xrealloc(p2 -> neighbors, sizeof(struct point *) * p2 -> neighbor_buffer_length); for ( i = p2 -> neighbor_count; i < p2 -> neighbor_buffer_length; i++) p2 -> neighbors[i] = NULL; } /***********************/ /* Make the connection */ /***********************/ p2 -> neighbors[p2 -> neighbor_count] = p1; p2 -> neighbor_count++; } void insert_point( struct point * p1, struct point * p2, struct point * p3 ) { size_t i, j; /***************************/ /* Copy common connections */ /***************************/ for (i = 0; i < p1 -> neighbor_count; i++) for (j = 0; j < p2 -> neighbor_count; j++) if (p1 -> neighbors[i] == p2 -> neighbors[j]) connect_points( p1 -> neighbors[i], p3); /***********************************/ /* Make room for connections in p3 */ /***********************************/ if ( ( p3 -> neighbor_count + 2 ) >= p3 -> neighbor_buffer_length ) { p3 -> neighbor_buffer_length = p3 -> neighbor_buffer_length * 2; p3 -> neighbors = xrealloc(p3 -> neighbors, sizeof(struct point *) * p3 -> neighbor_buffer_length); for ( i = p3 -> neighbor_count; i < p3 -> neighbor_buffer_length; i++) p3 -> neighbors[i] = NULL; } /*******************************/ /* Replace p1 to p2 connection */ /*******************************/ for ( i = 0; i < p1 -> neighbor_count; i++ ) if ( p1 -> neighbors[i] == p2 ) p1 -> neighbors[i] = p3; p3 -> neighbors[p3 -> neighbor_count] = p1; p3 -> neighbor_count++; /*******************************/ /* Replace p2 to p1 connection */ /*******************************/ for ( j = 0; j < p2 -> neighbor_count; j++ ) if ( p2 -> neighbors[j] == p1 ) p2 -> neighbors[j] = p3; p3 -> neighbors[p3 -> neighbor_count] = p2; p3 -> neighbor_count++; } void list_conections(const struct point * p) { size_t i; printf("from [%lu %p] to ",p -> id, p); for ( i = 0; i < p -> neighbor_count; i++) printf( "[%lu %p]", p -> neighbors[i] -> id, p -> neighbors[i] ); printf("\n"); }