[1844] | 1 | /* GNUPLOT - contour.c */
|
---|
| 2 |
|
---|
| 3 | /*[
|
---|
| 4 | * Copyright 1986 - 1993, 1998 Thomas Williams, Colin Kelley
|
---|
| 5 | *
|
---|
| 6 | * Permission to use, copy, and distribute this software and its
|
---|
| 7 | * documentation for any purpose with or without fee is hereby granted,
|
---|
| 8 | * provided that the above copyright notice appear in all copies and
|
---|
| 9 | * that both that copyright notice and this permission notice appear
|
---|
| 10 | * in supporting documentation.
|
---|
| 11 | *
|
---|
| 12 | * Permission to modify the software is granted, but not the right to
|
---|
| 13 | * distribute the complete modified source code. Modifications are to
|
---|
| 14 | * be distributed as patches to the released version. Permission to
|
---|
| 15 | * distribute binaries produced by compiling modified sources is granted,
|
---|
| 16 | * provided you
|
---|
| 17 | * 1. distribute the corresponding source modifications from the
|
---|
| 18 | * released version in the form of a patch file along with the binaries,
|
---|
| 19 | * 2. add special version identification to distinguish your version
|
---|
| 20 | * in addition to the base release version number,
|
---|
| 21 | * 3. provide your name and address as the primary contact for the
|
---|
| 22 | * support of your modified version, and
|
---|
| 23 | * 4. retain our contact information in regard to use of the base
|
---|
| 24 | * software.
|
---|
| 25 | * Permission to distribute the released version of the source code along
|
---|
| 26 | * with corresponding source modifications in the form of a patch file is
|
---|
| 27 | * granted with same provisions 2 through 4 for binary distributions.
|
---|
| 28 | *
|
---|
| 29 | * This software is provided "as is" without express or implied warranty
|
---|
| 30 | * to the extent permitted by applicable law.
|
---|
| 31 | ]*/
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | /*
|
---|
| 35 | * AUTHORS
|
---|
| 36 | *
|
---|
| 37 | * Original Software:
|
---|
| 38 | * Gershon Elber
|
---|
| 39 | *
|
---|
| 40 | * Improvements to the numerical algorithms:
|
---|
| 41 | * Hans-Martin Keller, 1995,1997 (hkeller@gwdg.de)
|
---|
| 42 | *
|
---|
| 43 | */
|
---|
| 44 |
|
---|
| 45 | #include "gp_contour.h"
|
---|
| 46 |
|
---|
| 47 | #include "gp_alloc.h"
|
---|
| 48 | #include "gp_axis.h"
|
---|
| 49 | /* #include "setshow.h" */
|
---|
| 50 |
|
---|
| 51 | /* exported variables (to be handled by the 'set' and friends): */
|
---|
| 52 |
|
---|
| 53 | char contour_format[32] = "%8.3g"; /* format for contour key entries */
|
---|
| 54 | t_contour_kind contour_kind = CONTOUR_KIND_LINEAR;
|
---|
| 55 | t_contour_levels_kind contour_levels_kind = LEVELS_AUTO;
|
---|
| 56 | int contour_levels = DEFAULT_CONTOUR_LEVELS;
|
---|
| 57 | int contour_order = DEFAULT_CONTOUR_ORDER;
|
---|
| 58 | int contour_pts = DEFAULT_NUM_APPROX_PTS;
|
---|
| 59 |
|
---|
| 60 | dynarray dyn_contour_levels_list;/* storage for z levels to draw contours at */
|
---|
| 61 | double * contour_levels_list=NULL;
|
---|
| 62 |
|
---|
| 63 | /* position of edge in mesh */
|
---|
| 64 | typedef enum en_edge_position {
|
---|
| 65 | INNER_MESH=1,
|
---|
| 66 | BOUNDARY,
|
---|
| 67 | DIAGONAL
|
---|
| 68 | } t_edge_position;
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /* Valeur de zero - Reza 21/12/2001 - Pourquoi zero = 0. ??? */
|
---|
| 72 | static double zero = 0.;
|
---|
| 73 |
|
---|
| 74 | /* FIXME HBB 2000052: yet another local copy of 'epsilon'. Why? */
|
---|
| 75 | #define EPSILON 1e-5 /* Used to decide if two float are equal. */
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | #ifndef TRUE
|
---|
| 79 | #define TRUE -1
|
---|
| 80 | #define FALSE 0
|
---|
| 81 | #endif
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | #define MAX_POINTS_PER_CNTR 100
|
---|
| 85 |
|
---|
| 86 | #define SQR(x) ((x) * (x))
|
---|
| 87 |
|
---|
| 88 | /*
|
---|
| 89 | * struct vrtx_struct {
|
---|
| 90 | * double X, Y, Z;
|
---|
| 91 | * struct vrtx_struct *next;
|
---|
| 92 | * };
|
---|
| 93 | *
|
---|
| 94 | * replaced by 'struct coordinate ', see plot.h (HMK 1997)
|
---|
| 95 | */
|
---|
| 96 |
|
---|
| 97 | struct edge_struct {
|
---|
| 98 | struct poly_struct *poly[2]; /* Each edge belongs to up to 2 polygons */
|
---|
| 99 | struct coordinate *vertex[2]; /* The two extreme points of this edge. */
|
---|
| 100 | struct edge_struct *next; /* To chain lists */
|
---|
| 101 | TBOOLEAN is_active; /* is edge is 'active' at certain Z level? */
|
---|
| 102 | t_edge_position position; /* position of edge in mesh */
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | struct poly_struct {
|
---|
| 106 | struct edge_struct *edge[3]; /* As we do triangolation here... */
|
---|
| 107 | struct poly_struct *next; /* To chain lists. */
|
---|
| 108 | };
|
---|
| 109 |
|
---|
| 110 | struct cntr_struct { /* Contours are saved using this struct list. */
|
---|
| 111 | double X, Y; /* The coordinates of this vertex. */
|
---|
| 112 | struct cntr_struct *next; /* To chain lists. */
|
---|
| 113 | };
|
---|
| 114 |
|
---|
| 115 | static struct gnuplot_contours *contour_list = NULL;
|
---|
| 116 | static double crnt_cntr[MAX_POINTS_PER_CNTR * 2];
|
---|
| 117 | static int crnt_cntr_pt_index = 0;
|
---|
| 118 | static double contour_level = 0.0;
|
---|
| 119 |
|
---|
| 120 | /* Linear, Cubic interp., Bspline: */
|
---|
| 121 | static t_contour_kind interp_kind = CONTOUR_KIND_LINEAR;
|
---|
| 122 |
|
---|
| 123 | static double x_min, y_min, z_min; /* Minimum values of x, y, and z */
|
---|
| 124 | static double x_max, y_max, z_max; /* Maximum values of x, y, and z */
|
---|
| 125 |
|
---|
| 126 | static void add_cntr_point (double x, double y);
|
---|
| 127 | static void end_crnt_cntr (void);
|
---|
| 128 | static void gen_contours(struct edge_struct * p_edges, double z_level,
|
---|
| 129 | double xx_min, double xx_max, double yy_min, double yy_max);
|
---|
| 130 | static int update_all_edges(struct edge_struct * p_edges,
|
---|
| 131 | double z_level);
|
---|
| 132 | static struct cntr_struct *gen_one_contour (
|
---|
| 133 | struct edge_struct * p_edges, double
|
---|
| 134 | z_level, TBOOLEAN *contr_isclosed,
|
---|
| 135 | int *num_active);
|
---|
| 136 | static struct cntr_struct *trace_contour (
|
---|
| 137 | struct edge_struct * pe_start, double
|
---|
| 138 | z_level, int *num_active,
|
---|
| 139 | TBOOLEAN contr_isclosed);
|
---|
| 140 | static struct cntr_struct *update_cntr_pt (struct edge_struct * p_edge,
|
---|
| 141 | double z_level);
|
---|
| 142 | static int fuzzy_equal (struct cntr_struct * p_cntr1,
|
---|
| 143 | struct cntr_struct * p_cntr2);
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | static void gen_triangle (int num_isolines,
|
---|
| 147 | struct iso_curve * iso_lines, struct poly_struct ** p_polys,
|
---|
| 148 | struct edge_struct ** p_edges);
|
---|
| 149 | static void calc_min_max (int num_isolines,
|
---|
| 150 | struct iso_curve * iso_lines, double *xx_min, double *yy_min,
|
---|
| 151 | double *zz_min,
|
---|
| 152 | double *xx_max, double *yy_max, double *zz_max);
|
---|
| 153 | static struct edge_struct *add_edge (struct coordinate * point0,
|
---|
| 154 | struct coordinate * point1, struct edge_struct
|
---|
| 155 | ** p_edge,
|
---|
| 156 | struct edge_struct ** pe_tail);
|
---|
| 157 | static struct poly_struct *add_poly (struct edge_struct * edge0,
|
---|
| 158 | struct edge_struct * edge1, struct edge_struct * edge2,
|
---|
| 159 | struct poly_struct ** p_poly, struct poly_struct ** pp_tail);
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | static void put_contour (struct cntr_struct * p_cntr, double z_level,
|
---|
| 163 | double xx_min, double xx_max, double yy_min, double yy_max,
|
---|
| 164 | TBOOLEAN contr_isclosed);
|
---|
| 165 | static void put_contour_nothing (struct cntr_struct * p_cntr);
|
---|
| 166 | static int chk_contour_kind (struct cntr_struct * p_cntr,
|
---|
| 167 | TBOOLEAN contr_isclosed);
|
---|
| 168 | static void put_contour_cubic (struct cntr_struct * p_cntr,
|
---|
| 169 | double z_level, double xx_min, double xx_max, double
|
---|
| 170 | yy_min, double yy_max,
|
---|
| 171 | TBOOLEAN contr_isclosed);
|
---|
| 172 | static void put_contour_bspline (struct cntr_struct * p_cntr,
|
---|
| 173 | double z_level, double xx_min, double xx_max, double
|
---|
| 174 | yy_min, double yy_max,
|
---|
| 175 | TBOOLEAN contr_isclosed);
|
---|
| 176 | static void free_contour (struct cntr_struct * p_cntr);
|
---|
| 177 | static int count_contour (struct cntr_struct * p_cntr);
|
---|
| 178 | static int gen_cubic_spline (int num_pts, struct cntr_struct * p_cntr,
|
---|
| 179 | double d2x[], double d2y[], double delta_t[], TBOOLEAN contr_isclosed,
|
---|
| 180 | double unit_x, double unit_y);
|
---|
| 181 | static void intp_cubic_spline (int n, struct cntr_struct * p_cntr,
|
---|
| 182 | double d2x[], double d2y[], double delta_t[], int n_intpol);
|
---|
| 183 | static int solve_cubic_1 (tri_diag m[], int n);
|
---|
| 184 | static void solve_cubic_2 (tri_diag m[], double x[], int n);
|
---|
| 185 | static void gen_bspline_approx (struct cntr_struct * p_cntr,
|
---|
| 186 | int num_of_points, int order, TBOOLEAN contr_isclosed);
|
---|
| 187 | static void eval_bspline (double t, struct cntr_struct * p_cntr,
|
---|
| 188 | int num_of_points, int order, int j, TBOOLEAN contr_isclosed, double *x,
|
---|
| 189 | double *y);
|
---|
| 190 | static double fetch_knot (TBOOLEAN contr_isclosed, int num_of_points,
|
---|
| 191 | int order, int i);
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | static int num_of_z_levels;/*_____ OP ___________*/ /* # Z contour levels. */
|
---|
| 195 |
|
---|
| 196 | int Get_Num_Of_Z_Levels(){
|
---|
| 197 | return num_of_z_levels;
|
---|
| 198 | } /* OP __________ */
|
---|
| 199 |
|
---|
| 200 | /*
|
---|
| 201 | * Entry routine to this whole set of contouring module.
|
---|
| 202 | */
|
---|
| 203 | struct gnuplot_contours *
|
---|
| 204 | contour(num_isolines, iso_lines)
|
---|
| 205 | int num_isolines;
|
---|
| 206 | struct iso_curve *iso_lines;
|
---|
| 207 | {
|
---|
| 208 | int i;
|
---|
| 209 | /*OP int num_of_z_levels;*/ /* # Z contour levels. */
|
---|
| 210 | struct poly_struct *p_polys, *p_poly;
|
---|
| 211 | struct edge_struct *p_edges, *p_edge;
|
---|
| 212 | double z = 0, dz = 0;
|
---|
| 213 | struct gnuplot_contours *save_contour_list;
|
---|
| 214 |
|
---|
| 215 | num_of_z_levels = contour_levels;
|
---|
| 216 | interp_kind = contour_kind;
|
---|
| 217 |
|
---|
| 218 | contour_list = NULL;
|
---|
| 219 |
|
---|
| 220 | /*
|
---|
| 221 | * Calculate min/max values :
|
---|
| 222 | */
|
---|
| 223 | calc_min_max(num_isolines, iso_lines,
|
---|
| 224 | &x_min, &y_min, &z_min, &x_max, &y_max, &z_max);
|
---|
| 225 |
|
---|
| 226 | dz = fabs(z_max - z_min);
|
---|
| 227 | /*
|
---|
| 228 | * printf(" contour z_max %g z_min %g dz=%g kind %d \n",z_max, z_min , dz,contour_levels_kind);
|
---|
| 229 | * Generate list of edges (p_edges) and list of triangles (p_polys):
|
---|
| 230 | */
|
---|
| 231 |
|
---|
| 232 | gen_triangle(num_isolines, iso_lines, &p_polys, &p_edges);
|
---|
| 233 | crnt_cntr_pt_index = 0;
|
---|
| 234 | /*AJOUT OP */
|
---|
| 235 | if (contour_levels_kind == LEVELS_NUM) {
|
---|
| 236 | dz = fabs(z_max - z_min)/(num_of_z_levels);
|
---|
| 237 | z = z_min - dz/2.;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | if (contour_levels_kind == LEVELS_AUTO) {
|
---|
| 241 | dz = fabs(z_max - z_min);
|
---|
| 242 | /*printf(" contour z_max %g z_min %g dz=%g\n",z_max, z_min , dz);*/
|
---|
| 243 | if (dz == 0)
|
---|
| 244 | return NULL; /* empty z range ? */
|
---|
| 245 | /* what is the deeper sense of this ? (joze) */
|
---|
| 246 | dz = set_tic(log10(dz), ((int) contour_levels + 1) * 2);
|
---|
| 247 | z = floor(z_min / dz) * dz;
|
---|
| 248 | num_of_z_levels = (int) floor((z_max - z) / dz);
|
---|
| 249 | /*printf("contour() : num_of_z_levels %d\n",num_of_z_levels);*/
|
---|
| 250 | }
|
---|
| 251 | for (i = 0; i < num_of_z_levels; i++) {
|
---|
| 252 | switch (contour_levels_kind) {
|
---|
| 253 | case LEVELS_AUTO:
|
---|
| 254 | case LEVELS_NUM:
|
---|
| 255 | z += dz;
|
---|
| 256 | break;
|
---|
| 257 | case LEVELS_INCREMENTAL:
|
---|
| 258 | z = contour_levels_list[0] + i * contour_levels_list[1];
|
---|
| 259 | break;
|
---|
| 260 | case LEVELS_DISCRETE:
|
---|
| 261 | /*printf("????? contour z=%f\n",contour_levels_list[i] );*/
|
---|
| 262 | /*z = AXIS_LOG_VALUE(FIRST_Z_AXIS, contour_levels_list[i]); PAS BESOIN ? OP */
|
---|
| 263 | z = contour_levels_list[i];
|
---|
| 264 | break;
|
---|
| 265 | }
|
---|
| 266 | contour_level = z;
|
---|
| 267 | /*printf(" contour z=%f\n",z);*/
|
---|
| 268 | save_contour_list = contour_list;
|
---|
| 269 | gen_contours(p_edges, z, x_min, x_max, y_min, y_max);
|
---|
| 270 | if (contour_list != save_contour_list) {
|
---|
| 271 | contour_list->isNewLevel = 1;
|
---|
| 272 | sprintf(contour_list->label, contour_format, AXIS_DE_LOG_VALUE(FIRST_Z_AXIS,z));
|
---|
| 273 | #ifdef PM3D
|
---|
| 274 | contour_list->z = AXIS_DE_LOG_VALUE(FIRST_Z_AXIS, z);
|
---|
| 275 | #endif
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /* Free all contouring related temporary data. */
|
---|
| 280 | while (p_polys) {
|
---|
| 281 | p_poly = p_polys->next;
|
---|
| 282 | free(p_polys);
|
---|
| 283 | p_polys = p_poly;
|
---|
| 284 | }
|
---|
| 285 | while (p_edges) {
|
---|
| 286 | p_edge = p_edges->next;
|
---|
| 287 | free(p_edges);
|
---|
| 288 | p_edges = p_edge;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | return contour_list;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | /*
|
---|
| 295 | * Adds another point to the currently build contour.
|
---|
| 296 | */
|
---|
| 297 | static void
|
---|
| 298 | add_cntr_point(x, y)
|
---|
| 299 | double x, y;
|
---|
| 300 | {
|
---|
| 301 | int index;
|
---|
| 302 |
|
---|
| 303 | if (crnt_cntr_pt_index >= MAX_POINTS_PER_CNTR - 1) {
|
---|
| 304 | index = crnt_cntr_pt_index - 1;
|
---|
| 305 | end_crnt_cntr();
|
---|
| 306 | crnt_cntr[0] = crnt_cntr[index * 2];
|
---|
| 307 | crnt_cntr[1] = crnt_cntr[index * 2 + 1];
|
---|
| 308 | crnt_cntr_pt_index = 1; /* Keep the last point as first of this one. */
|
---|
| 309 | }
|
---|
| 310 | crnt_cntr[crnt_cntr_pt_index * 2] = x;
|
---|
| 311 | crnt_cntr[crnt_cntr_pt_index * 2 + 1] = y;
|
---|
| 312 | crnt_cntr_pt_index++;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /*
|
---|
| 316 | * Done with current contour - create gnuplot data structure for it.
|
---|
| 317 | */
|
---|
| 318 | static void
|
---|
| 319 | end_crnt_cntr()
|
---|
| 320 | {
|
---|
| 321 | int i;
|
---|
| 322 | struct gnuplot_contours *cntr = (struct gnuplot_contours *)
|
---|
| 323 | gp_alloc(sizeof(struct gnuplot_contours), "gnuplot_contour");
|
---|
| 324 | cntr->coords = (struct coordinate *)
|
---|
| 325 | gp_alloc(sizeof(struct coordinate) * crnt_cntr_pt_index,
|
---|
| 326 | "contour coords");
|
---|
| 327 |
|
---|
| 328 | for (i = 0; i < crnt_cntr_pt_index; i++) {
|
---|
| 329 | cntr->coords[i].x = crnt_cntr[i * 2];
|
---|
| 330 | cntr->coords[i].y = crnt_cntr[i * 2 + 1];
|
---|
| 331 | cntr->coords[i].z = contour_level;
|
---|
| 332 | }
|
---|
| 333 | cntr->num_pts = crnt_cntr_pt_index;
|
---|
| 334 |
|
---|
| 335 | cntr->next = contour_list;
|
---|
| 336 | contour_list = cntr;
|
---|
| 337 | contour_list->isNewLevel = 0;
|
---|
| 338 |
|
---|
| 339 | crnt_cntr_pt_index = 0;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /*
|
---|
| 343 | * Generates all contours by tracing the intersecting triangles.
|
---|
| 344 | */
|
---|
| 345 | static void
|
---|
| 346 | gen_contours(p_edges, z_level, xx_min, xx_max, yy_min, yy_max)
|
---|
| 347 | struct edge_struct *p_edges;
|
---|
| 348 | double z_level, xx_min, xx_max, yy_min, yy_max;
|
---|
| 349 | {
|
---|
| 350 | int num_active; /* Number of edges marked ACTIVE. */
|
---|
| 351 | TBOOLEAN contr_isclosed; /* Is this contour a closed line? */
|
---|
| 352 | struct cntr_struct *p_cntr;
|
---|
| 353 |
|
---|
| 354 | num_active = update_all_edges(p_edges, z_level); /* Do pass 1. */
|
---|
| 355 |
|
---|
| 356 | contr_isclosed = FALSE; /* Start to look for contour on boundaries. */
|
---|
| 357 | /*printf("<gen_contour> z=%g num_active %d \n",z_level,num_active);*/
|
---|
| 358 | while (num_active > 0) { /* Do Pass 2. */
|
---|
| 359 | /* Generate One contour (and update MumActive as needed): */
|
---|
| 360 | p_cntr = gen_one_contour(p_edges, z_level, &contr_isclosed, &num_active);
|
---|
| 361 | if (p_cntr ==NULL) printf("<gen_contour> gen_one_contour retourne NULL \n");
|
---|
| 362 | /* Emit it in requested format: */
|
---|
| 363 | put_contour(p_cntr, z_level, xx_min, xx_max, yy_min, yy_max, contr_isclosed);
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | /*
|
---|
| 368 | * Does pass 1, or marks the edges which are active (crosses this z_level)
|
---|
| 369 | * Returns number of active edges (marked ACTIVE).
|
---|
| 370 | */
|
---|
| 371 | static int
|
---|
| 372 | update_all_edges(p_edges, z_level)
|
---|
| 373 | struct edge_struct *p_edges;
|
---|
| 374 | double z_level;
|
---|
| 375 | {
|
---|
| 376 | int count = 0;
|
---|
| 377 |
|
---|
| 378 | while (p_edges) {
|
---|
| 379 | /* use the same test at both vertices to avoid roundoff errors */
|
---|
| 380 |
|
---|
| 381 | if ((p_edges->vertex[0]->z >= z_level) !=
|
---|
| 382 | (p_edges->vertex[1]->z >= z_level)) {
|
---|
| 383 | p_edges->is_active = TRUE;
|
---|
| 384 | count++;
|
---|
| 385 | } else
|
---|
| 386 | p_edges->is_active = FALSE;
|
---|
| 387 | p_edges = p_edges->next;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | return count;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /*
|
---|
| 394 | * Does pass 2, or find one complete contour out of the triangulation
|
---|
| 395 | * data base:
|
---|
| 396 | *
|
---|
| 397 | * Returns a pointer to the contour (as linked list), contr_isclosed
|
---|
| 398 | * tells if the contour is a closed line or not, and num_active is
|
---|
| 399 | * updated.
|
---|
| 400 | */
|
---|
| 401 | static struct cntr_struct *
|
---|
| 402 | gen_one_contour(p_edges, z_level, contr_isclosed, num_active)
|
---|
| 403 | struct edge_struct *p_edges; /* list of edges input */
|
---|
| 404 | double z_level; /* Z level of contour input */
|
---|
| 405 | TBOOLEAN *contr_isclosed; /* open or closed contour, in/out */
|
---|
| 406 | int *num_active; /* number of active edges in/out */
|
---|
| 407 | {
|
---|
| 408 | struct edge_struct *pe_temp;
|
---|
| 409 |
|
---|
| 410 | if (! *contr_isclosed) {
|
---|
| 411 | /*printf("<gen_one_contour> contr_isclosed FALSE \n");*/
|
---|
| 412 | /* Look for something to start with on boundary: */
|
---|
| 413 | pe_temp = p_edges;
|
---|
| 414 | while (pe_temp) {
|
---|
| 415 | if (pe_temp->is_active && (pe_temp->position == BOUNDARY))
|
---|
| 416 | break;
|
---|
| 417 | pe_temp = pe_temp->next;
|
---|
| 418 | }
|
---|
| 419 | if (!pe_temp)
|
---|
| 420 | *contr_isclosed = TRUE; /* No more contours on boundary. */
|
---|
| 421 | else {
|
---|
| 422 | /*printf("<gen_one_contour> contr_isclosed FALSE return \n");*/
|
---|
| 423 | return trace_contour(pe_temp, z_level, num_active, *contr_isclosed);
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
| 426 | if (*contr_isclosed) {
|
---|
| 427 | /* Look for something to start with inside: */
|
---|
| 428 | /*printf("<gen_one_contour> contr_isclosed TRUE \n");*/
|
---|
| 429 | pe_temp = p_edges;
|
---|
| 430 | while (pe_temp) {
|
---|
| 431 | if (pe_temp->is_active && (pe_temp->position != BOUNDARY))
|
---|
| 432 | break;
|
---|
| 433 | pe_temp = pe_temp->next;
|
---|
| 434 | }
|
---|
| 435 | if (!pe_temp) {
|
---|
| 436 | *num_active = 0;
|
---|
| 437 | fprintf(stderr, "gen_one_contour: no contour found\n");
|
---|
| 438 | return NULL;
|
---|
| 439 | } else {
|
---|
| 440 | *contr_isclosed = TRUE;
|
---|
| 441 | return trace_contour(pe_temp, z_level, num_active, *contr_isclosed);
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 | printf("<gen_one_contour> We should never be here, but lint... \n");
|
---|
| 445 | return NULL; /* We should never be here, but lint... */
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | /*
|
---|
| 449 | * Search the data base along a contour starts at the edge pe_start until
|
---|
| 450 | * a boundary edge is detected or until we close the loop back to pe_start.
|
---|
| 451 | * Returns a linked list of all the points on the contour
|
---|
| 452 | * Also decreases num_active by the number of points on contour.
|
---|
| 453 | */
|
---|
| 454 | static struct cntr_struct *
|
---|
| 455 | trace_contour(pe_start, z_level, num_active, contr_isclosed)
|
---|
| 456 | struct edge_struct *pe_start; /* edge to start contour input */
|
---|
| 457 | double z_level; /* Z level of contour input */
|
---|
| 458 | int *num_active; /* number of active edges in/out */
|
---|
| 459 | TBOOLEAN contr_isclosed; /* open or closed contour line (input) */
|
---|
| 460 | {
|
---|
| 461 | struct cntr_struct *p_cntr, *pc_tail;
|
---|
| 462 | struct edge_struct *p_edge, *p_next_edge;
|
---|
| 463 | struct poly_struct *p_poly, *PLastpoly = NULL;
|
---|
| 464 | int i;
|
---|
| 465 |
|
---|
| 466 | p_edge = pe_start; /* first edge to start contour */
|
---|
| 467 |
|
---|
| 468 | /* Generate the header of the contour - the point on pe_start. */
|
---|
| 469 | if (! contr_isclosed) {
|
---|
| 470 | pe_start->is_active = FALSE;
|
---|
| 471 | (*num_active)--;
|
---|
| 472 | }
|
---|
| 473 | if (p_edge->poly[0] || p_edge->poly[1]) { /* more than one point */
|
---|
| 474 |
|
---|
| 475 | p_cntr = pc_tail = update_cntr_pt(pe_start, z_level); /* first point */
|
---|
| 476 |
|
---|
| 477 | do {
|
---|
| 478 | /* Find polygon to continue (Not where we came from - PLastpoly): */
|
---|
| 479 | if (p_edge->poly[0] == PLastpoly)
|
---|
| 480 | p_poly = p_edge->poly[1];
|
---|
| 481 | else
|
---|
| 482 | p_poly = p_edge->poly[0];
|
---|
| 483 | p_next_edge = NULL; /* In case of error, remains NULL. */
|
---|
| 484 | for (i = 0; i < 3; i++) /* Test the 3 edges of the polygon: */
|
---|
| 485 | if (p_poly->edge[i] != p_edge)
|
---|
| 486 | if (p_poly->edge[i]->is_active)
|
---|
| 487 | p_next_edge = p_poly->edge[i];
|
---|
| 488 | if (!p_next_edge) { /* Error exit */
|
---|
| 489 | pc_tail->next = NULL;
|
---|
| 490 | free_contour(p_cntr);
|
---|
| 491 | fprintf(stderr, "trace_contour: unexpected end of contour\n");
|
---|
| 492 | return NULL;
|
---|
| 493 | }
|
---|
| 494 | p_edge = p_next_edge;
|
---|
| 495 | PLastpoly = p_poly;
|
---|
| 496 | p_edge->is_active = FALSE;
|
---|
| 497 | (*num_active)--;
|
---|
| 498 |
|
---|
| 499 | /* Do not allocate contour points on diagonal edges */
|
---|
| 500 | if (p_edge->position != DIAGONAL) {
|
---|
| 501 |
|
---|
| 502 | pc_tail->next = update_cntr_pt(p_edge, z_level);
|
---|
| 503 |
|
---|
| 504 | /* Remove nearby points */
|
---|
| 505 | if (fuzzy_equal(pc_tail, pc_tail->next)) {
|
---|
| 506 |
|
---|
| 507 | free((char *) pc_tail->next);
|
---|
| 508 | } else
|
---|
| 509 | pc_tail = pc_tail->next;
|
---|
| 510 | }
|
---|
| 511 | } while ((p_edge != pe_start) && (p_edge->position != BOUNDARY));
|
---|
| 512 |
|
---|
| 513 | pc_tail->next = NULL;
|
---|
| 514 |
|
---|
| 515 | /* For closed contour the first and last point should be equal */
|
---|
| 516 | if (pe_start == p_edge) {
|
---|
| 517 | (p_cntr->X) = (pc_tail->X);
|
---|
| 518 | (p_cntr->Y) = (pc_tail->Y);
|
---|
| 519 | }
|
---|
| 520 | } else { /* only one point, forget it */
|
---|
| 521 | p_cntr = NULL;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | return p_cntr;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | /*
|
---|
| 528 | * Allocates one contour location and update it to to correct position
|
---|
| 529 | * according to z_level and edge p_edge.
|
---|
| 530 | */
|
---|
| 531 | static struct cntr_struct *
|
---|
| 532 | update_cntr_pt(p_edge, z_level)
|
---|
| 533 | struct edge_struct *p_edge;
|
---|
| 534 | double z_level;
|
---|
| 535 | {
|
---|
| 536 | double t;
|
---|
| 537 | struct cntr_struct *p_cntr;
|
---|
| 538 |
|
---|
| 539 | t = (z_level - p_edge->vertex[0]->z) /
|
---|
| 540 | (p_edge->vertex[1]->z - p_edge->vertex[0]->z);
|
---|
| 541 |
|
---|
| 542 | /* test if t is out of interval [0:1] (should not happen but who knows ...) */
|
---|
| 543 | /*if(t>1) printf(" <update_cntr_pt> t >1 !\n");*/
|
---|
| 544 | /*if(t<0) printf(" <update_cntr_pt> t negatif !\n");*/
|
---|
| 545 | t = (t < 0.0 ? 0.0 : t);
|
---|
| 546 | t = (t > 1.0 ? 1.0 : t);
|
---|
| 547 | /*printf(" <update_cntr_pt> Point 0 %g %g %g \n",p_edge->vertex[0]->x,p_edge->vertex[0]->y,p_edge->vertex[0]->z);*/
|
---|
| 548 | /*printf(" <update_cntr_pt> Point 1 %g %g %g \n",p_edge->vertex[1]->x,p_edge->vertex[1]->y,p_edge->vertex[1]->z);*/
|
---|
| 549 | p_cntr = (struct cntr_struct *)
|
---|
| 550 | gp_alloc(sizeof(struct cntr_struct), "contour cntr_struct");
|
---|
| 551 |
|
---|
| 552 | p_cntr->X = p_edge->vertex[1]->x * t +
|
---|
| 553 | p_edge->vertex[0]->x * (1 - t);
|
---|
| 554 | p_cntr->Y = p_edge->vertex[1]->y * t +
|
---|
| 555 | p_edge->vertex[0]->y * (1 - t);
|
---|
| 556 | /*printf(" <update_cntr_pt> p_cntr X %g Y %g \n",p_cntr->X,p_cntr->Y);*/
|
---|
| 557 | return p_cntr;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | /* Simple routine to decide if two contour points are equal by
|
---|
| 561 | * calculating the relative error (< EPSILON). */
|
---|
| 562 | /* HBB 20010121: don't use absolute value 'zero' to compare to data
|
---|
| 563 | * values. */
|
---|
| 564 | static int
|
---|
| 565 | fuzzy_equal(p_cntr1, p_cntr2)
|
---|
| 566 | struct cntr_struct *p_cntr1, *p_cntr2;
|
---|
| 567 | {
|
---|
| 568 | double unit_x, unit_y;
|
---|
| 569 | unit_x = fabs(x_max - x_min); /* reference */
|
---|
| 570 | unit_y = fabs(y_max - y_min);
|
---|
| 571 | return ((fabs(p_cntr1->X - p_cntr2->X) < unit_x * EPSILON)
|
---|
| 572 | && (fabs(p_cntr1->Y - p_cntr2->Y) < unit_y * EPSILON));
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | /*
|
---|
| 576 | * Generate the triangles.
|
---|
| 577 | * Returns the lists (edges & polys) via pointers to their heads.
|
---|
| 578 | */
|
---|
| 579 | static void
|
---|
| 580 | gen_triangle(num_isolines, iso_lines, p_polys, p_edges)
|
---|
| 581 | int num_isolines; /* number of iso-lines input */
|
---|
| 582 | struct iso_curve *iso_lines; /* iso-lines input */
|
---|
| 583 | struct poly_struct **p_polys; /* list of polygons output */
|
---|
| 584 | struct edge_struct **p_edges; /* list of edges output */
|
---|
| 585 | {
|
---|
| 586 | int i, j, grid_x_max = iso_lines->p_count;
|
---|
| 587 | struct edge_struct *p_edge1, *p_edge2, *edge0, *edge1, *edge2, *pe_tail,
|
---|
| 588 | *pe_tail2, *pe_temp;
|
---|
| 589 | struct poly_struct *pp_tail, *lower_tri, *upper_tri;
|
---|
| 590 | /* HBB 980308: need to tag *each* of them as ! */
|
---|
| 591 | struct coordinate *p_vrtx1, * p_vrtx2;
|
---|
| 592 |
|
---|
| 593 | (*p_polys) = pp_tail = NULL; /* clear lists */
|
---|
| 594 | (*p_edges) = pe_tail = NULL;
|
---|
| 595 |
|
---|
| 596 | p_vrtx1 = iso_lines->points; /* first row of vertices */
|
---|
| 597 | p_edge1 = pe_tail = NULL; /* clear list of edges */
|
---|
| 598 |
|
---|
| 599 | /* Generate edges of first row */
|
---|
| 600 | for (j = 0; j < grid_x_max - 1; j++)
|
---|
| 601 | add_edge(p_vrtx1 + j, p_vrtx1 + j + 1, &p_edge1, &pe_tail);
|
---|
| 602 |
|
---|
| 603 | (*p_edges) = p_edge1; /* update main list */
|
---|
| 604 |
|
---|
| 605 |
|
---|
| 606 | /*
|
---|
| 607 | * Combines vertices to edges and edges to triangles:
|
---|
| 608 | * ==================================================
|
---|
| 609 | * The edges are stored in the edge list, referenced by p_edges
|
---|
| 610 | * (pe_tail points on last edge).
|
---|
| 611 | *
|
---|
| 612 | * Temporary pointers:
|
---|
| 613 | * 1. p_edge2: Top horizontal edge list: +-----------------------+ 2
|
---|
| 614 | * 2. p_tail : end of middle edge list: |\ |\ |\ |\ |\ |\ |
|
---|
| 615 | * | \| \| \| \| \| \|
|
---|
| 616 | * 3. p_edge1: Bottom horizontal edge list: +-----------------------+ 1
|
---|
| 617 | *
|
---|
| 618 | * pe_tail2 : end of list beginning at p_edge2
|
---|
| 619 | * pe_temp : position inside list beginning at p_edge1
|
---|
| 620 | * p_edges : head of the master edge list (part of our output)
|
---|
| 621 | * p_vrtx1 : start of lower row of input vertices
|
---|
| 622 | * p_vrtx2 : start of higher row of input vertices
|
---|
| 623 | *
|
---|
| 624 | * The routine generates two triangle Lower Upper 1
|
---|
| 625 | * upper one and lower one: | \ ----
|
---|
| 626 | * (Nums. are edges order in polys) 0| \1 0\ |2
|
---|
| 627 | * The polygons are stored in the polygon ---- \ |
|
---|
| 628 | * list (*p_polys) (pp_tail points on 2
|
---|
| 629 | * last polygon).
|
---|
| 630 | * 1
|
---|
| 631 | * -----------
|
---|
| 632 | * In addition, the edge lists are updated - | \ 0 |
|
---|
| 633 | * each edge has two pointers on the two | \ |
|
---|
| 634 | * (one active if boundary) polygons which 0|1 0\1 0|1
|
---|
| 635 | * uses it. These two pointer to polygons | \ |
|
---|
| 636 | * are named: poly[0], poly[1]. The diagram | 1 \ |
|
---|
| 637 | * on the right show how they are used for the -----------
|
---|
| 638 | * upper and lower polygons (INNER_MESH polygons only). 0
|
---|
| 639 | */
|
---|
| 640 |
|
---|
| 641 | for (i = 1; i < num_isolines; i++) {
|
---|
| 642 | /* Read next column and gen. polys. */
|
---|
| 643 | iso_lines = iso_lines->next;
|
---|
| 644 |
|
---|
| 645 | p_vrtx2 = iso_lines->points; /* next row of vertices */
|
---|
| 646 | p_edge2 = pe_tail2 = NULL; /* clear top horizontal list */
|
---|
| 647 | pe_temp = p_edge1; /* pointer in bottom list */
|
---|
| 648 |
|
---|
| 649 | /*
|
---|
| 650 | * Generate edges and triagles for next row:
|
---|
| 651 | */
|
---|
| 652 |
|
---|
| 653 | /* generate first vertical edge */
|
---|
| 654 | edge2 = add_edge(p_vrtx1, p_vrtx2, p_edges, &pe_tail);
|
---|
| 655 |
|
---|
| 656 | for (j = 0; j < grid_x_max - 1; j++) {
|
---|
| 657 |
|
---|
| 658 | /* copy vertical edge for lower triangle */
|
---|
| 659 | edge0 = edge2;
|
---|
| 660 |
|
---|
| 661 | if (pe_temp && pe_temp->vertex[0] == p_vrtx1 + j) {
|
---|
| 662 | /* test lower edge */
|
---|
| 663 | edge2 = pe_temp;
|
---|
| 664 | pe_temp = pe_temp->next;
|
---|
| 665 | } else {
|
---|
| 666 | edge2 = NULL; /* edge is undefined */
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | /* generate diagonal edge */
|
---|
| 670 | edge1 = add_edge(p_vrtx1 + j + 1, p_vrtx2 + j, p_edges, &pe_tail);
|
---|
| 671 | if (edge1)
|
---|
| 672 | edge1->position = DIAGONAL;
|
---|
| 673 |
|
---|
| 674 | /* generate lower triangle */
|
---|
| 675 | lower_tri = add_poly(edge0, edge1, edge2, p_polys, &pp_tail);
|
---|
| 676 |
|
---|
| 677 | /* copy diagonal edge for upper triangle */
|
---|
| 678 | edge0 = edge1;
|
---|
| 679 |
|
---|
| 680 | /* generate upper edge */
|
---|
| 681 | edge1 = add_edge(p_vrtx2 + j, p_vrtx2 + j + 1, &p_edge2, &pe_tail2);
|
---|
| 682 |
|
---|
| 683 | /* generate vertical edge */
|
---|
| 684 | edge2 = add_edge(p_vrtx1 + j + 1, p_vrtx2 + j + 1, p_edges, &pe_tail);
|
---|
| 685 |
|
---|
| 686 | /* generate upper triangle */
|
---|
| 687 | upper_tri = add_poly(edge0, edge1, edge2, p_polys, &pp_tail);
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | if (p_edge2) {
|
---|
| 691 | /* HBB 19991130 bugfix: if p_edge2 list is empty,
|
---|
| 692 | * don't change p_edges list! Crashes by access
|
---|
| 693 | * to NULL pointer pe_tail, the second time through,
|
---|
| 694 | * otherwise */
|
---|
| 695 | if ((*p_edges)) { /* Chain new edges to main list. */
|
---|
| 696 | pe_tail->next = p_edge2;
|
---|
| 697 | pe_tail = pe_tail2;
|
---|
| 698 | } else {
|
---|
| 699 | (*p_edges) = p_edge2;
|
---|
| 700 | pe_tail = pe_tail2;
|
---|
| 701 | }
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | /* this row finished, move list heads up one row: */
|
---|
| 705 | p_edge1 = p_edge2;
|
---|
| 706 | p_vrtx1 = p_vrtx2;
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | /* Update the boundary flag, saved in each edge, and update indexes: */
|
---|
| 710 |
|
---|
| 711 | pe_temp = (*p_edges);
|
---|
| 712 |
|
---|
| 713 | while (pe_temp) {
|
---|
| 714 | if ((!(pe_temp->poly[0])) || (!(pe_temp->poly[1])))
|
---|
| 715 | (pe_temp->position) = BOUNDARY;
|
---|
| 716 | pe_temp = pe_temp->next;
|
---|
| 717 | }
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | /*
|
---|
| 721 | * Calculate minimum and maximum values
|
---|
| 722 | */
|
---|
| 723 | static void
|
---|
| 724 | calc_min_max(num_isolines, iso_lines, xx_min, yy_min, zz_min, xx_max, yy_max, zz_max)
|
---|
| 725 | int num_isolines; /* number of iso-lines input */
|
---|
| 726 | struct iso_curve *iso_lines; /* iso-lines input */
|
---|
| 727 | double *xx_min, *yy_min, *zz_min, *xx_max, *yy_max, *zz_max; /* min/max values in/out */
|
---|
| 728 | {
|
---|
| 729 | int i, j, grid_x_max;
|
---|
| 730 | struct coordinate *vertex;
|
---|
| 731 | /*printf("<calc_min_max> : iso_lines->p_count %ld \n",iso_lines->p_count);*/
|
---|
| 732 | grid_x_max = iso_lines->p_count; /* number of vertices per iso_line */
|
---|
| 733 |
|
---|
| 734 | (*xx_min) = (*yy_min) = (*zz_min) = VERYLARGE; /* clear min/max values */
|
---|
| 735 | (*xx_max) = (*yy_max) = (*zz_max) = -VERYLARGE;
|
---|
| 736 | /*printf(" <calc_min_max> %d \n",num_isolines);*/
|
---|
| 737 | for (j = 0; j < num_isolines; j++) {
|
---|
| 738 | /*printf(" <calc_min_max> iso_lines %lx %lx %d min %g max %g\n", */
|
---|
| 739 | /* iso_lines,iso_lines->points,grid_x_max,(*zz_min),(*zz_max)); */
|
---|
| 740 | vertex = iso_lines->points;
|
---|
| 741 |
|
---|
| 742 | for (i = 0; i < grid_x_max; i++) {
|
---|
| 743 | if (vertex[i].type != UNDEFINED) {
|
---|
| 744 | if (vertex[i].x > (*xx_max))
|
---|
| 745 | (*xx_max) = vertex[i].x;
|
---|
| 746 | if (vertex[i].y > (*yy_max))
|
---|
| 747 | (*yy_max) = vertex[i].y;
|
---|
| 748 | if (vertex[i].z > (*zz_max))
|
---|
| 749 | (*zz_max) = vertex[i].z;
|
---|
| 750 | if (vertex[i].x < (*xx_min))
|
---|
| 751 | (*xx_min) = vertex[i].x;
|
---|
| 752 | if (vertex[i].y < (*yy_min))
|
---|
| 753 | (*yy_min) = vertex[i].y;
|
---|
| 754 | if (vertex[i].z < (*zz_min))
|
---|
| 755 | (*zz_min) = vertex[i].z;
|
---|
| 756 |
|
---|
| 757 | }
|
---|
| 758 | }
|
---|
| 759 | iso_lines = iso_lines->next;
|
---|
| 760 | /*printf(" End of loop calc_min_max %d \n", j);*/
|
---|
| 761 | }
|
---|
| 762 | /* HBB 20000426: this code didn't take into account that axes might
|
---|
| 763 | * be logscaled... */
|
---|
| 764 | #if 0
|
---|
| 765 | /* HBB 20001220: DON'T. The values are actually already stored
|
---|
| 766 | * logarithmized, as should be! */
|
---|
| 767 | axis_unlog_interval(FIRST_X_AXIS, xx_min, xx_max, 0);
|
---|
| 768 | axis_unlog_interval(FIRST_Y_AXIS, yy_min, yy_max, 0);
|
---|
| 769 | axis_unlog_interval(FIRST_Z_AXIS, zz_min, zz_max, 0);
|
---|
| 770 | #endif
|
---|
| 771 |
|
---|
| 772 | /*
|
---|
| 773 | * fprintf(stderr," x: %g, %g\n", (*xx_min), (*xx_max));
|
---|
| 774 | * fprintf(stderr," y: %g, %g\n", (*yy_min), (*yy_max));
|
---|
| 775 | * fprintf(stderr," z: %g, %g\n", (*zz_min), (*zz_max));
|
---|
| 776 | */
|
---|
| 777 | }
|
---|
| 778 |
|
---|
| 779 | /*
|
---|
| 780 | * Generate new edge and append it to list, but only if both vertices are
|
---|
| 781 | * defined. The list is referenced by p_edge and pe_tail (p_edge points on
|
---|
| 782 | * first edge and pe_tail on last one).
|
---|
| 783 | * Note, the list may be empty (pe_edge==pe_tail==NULL) on entry and exit.
|
---|
| 784 | */
|
---|
| 785 | static struct edge_struct *
|
---|
| 786 | add_edge(point0, point1, p_edge, pe_tail)
|
---|
| 787 | struct coordinate *point0; /* 2 vertices input */
|
---|
| 788 | struct coordinate *point1;
|
---|
| 789 | struct edge_struct **p_edge, **pe_tail; /* pointers to edge list in/out */
|
---|
| 790 | {
|
---|
| 791 | struct edge_struct *pe_temp = NULL;
|
---|
| 792 |
|
---|
| 793 | #if 1
|
---|
| 794 | if (point0->type == INRANGE && point1->type == INRANGE) {
|
---|
| 795 | #else
|
---|
| 796 | if (point0->type != UNDEFINED && point1->type != UNDEFINED) {
|
---|
| 797 | #endif
|
---|
| 798 |
|
---|
| 799 | pe_temp = (struct edge_struct *)
|
---|
| 800 | gp_alloc(sizeof(struct edge_struct), "contour edge");
|
---|
| 801 |
|
---|
| 802 | pe_temp->poly[0] = NULL; /* clear links */
|
---|
| 803 | pe_temp->poly[1] = NULL;
|
---|
| 804 | pe_temp->vertex[0] = point0; /* First vertex of edge. */
|
---|
| 805 | pe_temp->vertex[1] = point1; /* Second vertex of edge. */
|
---|
| 806 | pe_temp->next = NULL;
|
---|
| 807 | pe_temp->position = INNER_MESH; /* default position in mesh */
|
---|
| 808 |
|
---|
| 809 | if ((*pe_tail)) {
|
---|
| 810 | (*pe_tail)->next = pe_temp; /* Stick new record as last one. */
|
---|
| 811 | } else {
|
---|
| 812 | (*p_edge) = pe_temp; /* start new list if empty */
|
---|
| 813 | }
|
---|
| 814 | (*pe_tail) = pe_temp; /* continue to last record. */
|
---|
| 815 |
|
---|
| 816 | }
|
---|
| 817 | return pe_temp; /* returns NULL, if no edge allocated */
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | /*
|
---|
| 821 | * Generate new triangle and append it to list, but only if all edges are defined.
|
---|
| 822 | * The list is referenced by p_poly and pp_tail (p_poly points on first ploygon
|
---|
| 823 | * and pp_tail on last one).
|
---|
| 824 | * Note, the list may be empty (pe_ploy==pp_tail==NULL) on entry and exit.
|
---|
| 825 | */
|
---|
| 826 | static struct poly_struct *
|
---|
| 827 | add_poly(edge0, edge1, edge2, p_poly, pp_tail)
|
---|
| 828 | struct edge_struct *edge0, *edge1, *edge2; /* 3 edges input */
|
---|
| 829 | struct poly_struct **p_poly, **pp_tail; /* pointers to polygon list in/out */
|
---|
| 830 | {
|
---|
| 831 | struct poly_struct *pp_temp = NULL;
|
---|
| 832 |
|
---|
| 833 | if (edge0 && edge1 && edge2) {
|
---|
| 834 |
|
---|
| 835 | pp_temp = (struct poly_struct *)
|
---|
| 836 | gp_alloc(sizeof(struct poly_struct), "contour polygon");
|
---|
| 837 |
|
---|
| 838 | pp_temp->edge[0] = edge0; /* First edge of triangle */
|
---|
| 839 | pp_temp->edge[1] = edge1; /* Second one */
|
---|
| 840 | pp_temp->edge[2] = edge2; /* Third one */
|
---|
| 841 | pp_temp->next = NULL;
|
---|
| 842 |
|
---|
| 843 | if (edge0->poly[0]) /* update edge0 */
|
---|
| 844 | edge0->poly[1] = pp_temp;
|
---|
| 845 | else
|
---|
| 846 | edge0->poly[0] = pp_temp;
|
---|
| 847 |
|
---|
| 848 | if (edge1->poly[0]) /* update edge1 */
|
---|
| 849 | edge1->poly[1] = pp_temp;
|
---|
| 850 | else
|
---|
| 851 | edge1->poly[0] = pp_temp;
|
---|
| 852 |
|
---|
| 853 | if (edge2->poly[0]) /* update edge2 */
|
---|
| 854 | edge2->poly[1] = pp_temp;
|
---|
| 855 | else
|
---|
| 856 | edge2->poly[0] = pp_temp;
|
---|
| 857 |
|
---|
| 858 | if ((*pp_tail)) /* Stick new record as last one. */
|
---|
| 859 | (*pp_tail)->next = pp_temp;
|
---|
| 860 | else
|
---|
| 861 | (*p_poly) = pp_temp; /* start new list if empty */
|
---|
| 862 |
|
---|
| 863 | (*pp_tail) = pp_temp; /* continue to last record. */
|
---|
| 864 |
|
---|
| 865 | }
|
---|
| 866 | return pp_temp; /* returns NULL, if no edge allocated */
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 |
|
---|
| 870 |
|
---|
| 871 | /*
|
---|
| 872 | * Calls the (hopefully) desired interpolation/approximation routine.
|
---|
| 873 | */
|
---|
| 874 | static void
|
---|
| 875 | put_contour(p_cntr, z_level, xx_min, xx_max, yy_min, yy_max, contr_isclosed)
|
---|
| 876 | struct cntr_struct *p_cntr; /* contour structure input */
|
---|
| 877 | double z_level; /* Z level of contour input */
|
---|
| 878 | double xx_min, xx_max, yy_min, yy_max; /* minimum/maximum values input */
|
---|
| 879 | TBOOLEAN contr_isclosed; /* contour line closed? (input) */
|
---|
| 880 | {
|
---|
| 881 |
|
---|
| 882 | if (!p_cntr)
|
---|
| 883 | return; /* Nothing to do if it is empty contour. */
|
---|
| 884 |
|
---|
| 885 | switch (interp_kind) {
|
---|
| 886 | case CONTOUR_KIND_LINEAR: /* No interpolation/approximation. */
|
---|
| 887 | put_contour_nothing(p_cntr);
|
---|
| 888 | break;
|
---|
| 889 | case CONTOUR_KIND_CUBIC_SPL: /* Cubic spline interpolation. */
|
---|
| 890 | put_contour_cubic(p_cntr, z_level, xx_min, xx_max, yy_min, yy_max,
|
---|
| 891 | chk_contour_kind(p_cntr, contr_isclosed));
|
---|
| 892 |
|
---|
| 893 | break;
|
---|
| 894 | case CONTOUR_KIND_BSPLINE: /* Bspline approximation. */
|
---|
| 895 | put_contour_bspline(p_cntr, z_level, xx_min, xx_max, yy_min, yy_max,
|
---|
| 896 | chk_contour_kind(p_cntr, contr_isclosed));
|
---|
| 897 | break;
|
---|
| 898 | }
|
---|
| 899 | free_contour(p_cntr);
|
---|
| 900 | }
|
---|
| 901 |
|
---|
| 902 | /*
|
---|
| 903 | * Simply puts contour coordinates in order with no interpolation or
|
---|
| 904 | * approximation.
|
---|
| 905 | */
|
---|
| 906 | static void
|
---|
| 907 | put_contour_nothing(p_cntr)
|
---|
| 908 | struct cntr_struct *p_cntr;
|
---|
| 909 | {
|
---|
| 910 | while (p_cntr) {
|
---|
| 911 | add_cntr_point(p_cntr->X, p_cntr->Y);
|
---|
| 912 | p_cntr = p_cntr->next;
|
---|
| 913 | }
|
---|
| 914 | end_crnt_cntr();
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | /*
|
---|
| 918 | * for some reason contours are never flagged as 'isclosed'
|
---|
| 919 | * if first point == last point, set flag accordingly
|
---|
| 920 | *
|
---|
| 921 | */
|
---|
| 922 |
|
---|
| 923 | static int
|
---|
| 924 | chk_contour_kind(p_cntr, contr_isclosed)
|
---|
| 925 | struct cntr_struct *p_cntr;
|
---|
| 926 | TBOOLEAN contr_isclosed;
|
---|
| 927 | {
|
---|
| 928 | struct cntr_struct *pc_tail = NULL;
|
---|
| 929 | TBOOLEAN current_contr_isclosed;
|
---|
| 930 |
|
---|
| 931 | /*fprintf(stderr, "check_contour_kind: current contour_kind value is %d\n", contour_kind);*/
|
---|
| 932 |
|
---|
| 933 | current_contr_isclosed = contr_isclosed;
|
---|
| 934 |
|
---|
| 935 | if (! contr_isclosed) {
|
---|
| 936 | pc_tail = p_cntr;
|
---|
| 937 | while (pc_tail->next)
|
---|
| 938 | pc_tail = pc_tail->next; /* Find last point. */
|
---|
| 939 |
|
---|
| 940 | /* test if first and last point are equal */
|
---|
| 941 | if (fuzzy_equal(pc_tail, p_cntr)) {
|
---|
| 942 | current_contr_isclosed = TRUE;
|
---|
| 943 | fprintf(stderr, "check_contour_kind: contr_isclosed changed to %d\n", current_contr_isclosed);
|
---|
| 944 | }
|
---|
| 945 | }
|
---|
| 946 | return (current_contr_isclosed);
|
---|
| 947 | }
|
---|
| 948 |
|
---|
| 949 | /*
|
---|
| 950 | * Generate a cubic spline curve through the points (x_i,y_i) which are
|
---|
| 951 | * stored in the linked list p_cntr.
|
---|
| 952 | * The spline is defined as a 2d-function s(t) = (x(t),y(t)), where the
|
---|
| 953 | * parameter t is the length of the linear stroke.
|
---|
| 954 | */
|
---|
| 955 | static void
|
---|
| 956 | put_contour_cubic(p_cntr, z_level, xx_min, xx_max, yy_min, yy_max, contr_isclosed)
|
---|
| 957 | struct cntr_struct *p_cntr;
|
---|
| 958 | double z_level, xx_min, xx_max, yy_min, yy_max;
|
---|
| 959 | TBOOLEAN contr_isclosed;
|
---|
| 960 | {
|
---|
| 961 | int num_pts, num_intpol;
|
---|
| 962 | double unit_x, unit_y; /* To define norm (x,y)-plane */
|
---|
| 963 | double *delta_t; /* Interval length t_{i+1}-t_i */
|
---|
| 964 | double *d2x, *d2y; /* Second derivatives x''(t_i), y''(t_i) */
|
---|
| 965 | struct cntr_struct *pc_tail;
|
---|
| 966 |
|
---|
| 967 | num_pts = count_contour(p_cntr); /* Number of points in contour. */
|
---|
| 968 |
|
---|
| 969 | pc_tail = p_cntr; /* Find last point. */
|
---|
| 970 | while (pc_tail->next)
|
---|
| 971 | pc_tail = pc_tail->next;
|
---|
| 972 |
|
---|
| 973 | if (contr_isclosed) {
|
---|
| 974 | /* Test if first and last point are equal (should be) */
|
---|
| 975 | if (!fuzzy_equal(pc_tail, p_cntr)) {
|
---|
| 976 | pc_tail->next = p_cntr; /* Close contour list - make it circular. */
|
---|
| 977 | num_pts++;
|
---|
| 978 | }
|
---|
| 979 | }
|
---|
| 980 | delta_t = (double *) gp_alloc(num_pts * sizeof(double), "contour delta_t");
|
---|
| 981 | d2x = (double *) gp_alloc(num_pts * sizeof(double), "contour d2x");
|
---|
| 982 | d2y = (double *) gp_alloc(num_pts * sizeof(double), "contour d2y");
|
---|
| 983 |
|
---|
| 984 | /* Width and height of the grid is used as a unit length (2d-norm) */
|
---|
| 985 | unit_x = xx_max - x_min;
|
---|
| 986 | unit_y = yy_max - y_min;
|
---|
| 987 | /* FIXME HBB 20010121: 'zero' should not be used as an absolute
|
---|
| 988 | * figure to compare to data */
|
---|
| 989 | unit_x = (unit_x > zero ? unit_x : zero); /* should not be zero */
|
---|
| 990 | unit_y = (unit_y > zero ? unit_y : zero);
|
---|
| 991 |
|
---|
| 992 | if (num_pts > 2) {
|
---|
| 993 | /*
|
---|
| 994 | * Calculate second derivatives d2x[], d2y[] and interval lengths delta_t[]:
|
---|
| 995 | */
|
---|
| 996 | if (!gen_cubic_spline(num_pts, p_cntr, d2x, d2y, delta_t,
|
---|
| 997 | contr_isclosed, unit_x, unit_y)) {
|
---|
| 998 | free((char *) delta_t);
|
---|
| 999 | free((char *) d2x);
|
---|
| 1000 | free((char *) d2y);
|
---|
| 1001 | if (contr_isclosed)
|
---|
| 1002 | pc_tail->next = NULL; /* Un-circular list */
|
---|
| 1003 | return;
|
---|
| 1004 | }
|
---|
| 1005 | }
|
---|
| 1006 | /* If following (num_pts > 1) is TRUE then exactly 2 points in contour. */
|
---|
| 1007 | else if (num_pts > 1) {
|
---|
| 1008 | /* set all second derivatives to zero, interval length to 1 */
|
---|
| 1009 | d2x[0] = 0.;
|
---|
| 1010 | d2y[0] = 0.;
|
---|
| 1011 | d2x[1] = 0.;
|
---|
| 1012 | d2y[1] = 0.;
|
---|
| 1013 | delta_t[0] = 1.;
|
---|
| 1014 | } else { /* Only one point ( ?? ) - ignore it. */
|
---|
| 1015 | free((char *) delta_t);
|
---|
| 1016 | free((char *) d2x);
|
---|
| 1017 | free((char *) d2y);
|
---|
| 1018 | if (contr_isclosed)
|
---|
| 1019 | pc_tail->next = NULL; /* Un-circular list */
|
---|
| 1020 | return;
|
---|
| 1021 | }
|
---|
| 1022 |
|
---|
| 1023 | /* Calculate "num_intpol" interpolated values */
|
---|
| 1024 | num_intpol = 1 + (num_pts - 1) * contour_pts; /* global: contour_pts */
|
---|
| 1025 | intp_cubic_spline(num_pts, p_cntr, d2x, d2y, delta_t, num_intpol);
|
---|
| 1026 |
|
---|
| 1027 | free((char *) delta_t);
|
---|
| 1028 | free((char *) d2x);
|
---|
| 1029 | free((char *) d2y);
|
---|
| 1030 |
|
---|
| 1031 | if (contr_isclosed)
|
---|
| 1032 | pc_tail->next = NULL; /* Un-circular list */
|
---|
| 1033 |
|
---|
| 1034 | end_crnt_cntr();
|
---|
| 1035 | }
|
---|
| 1036 |
|
---|
| 1037 |
|
---|
| 1038 | /*
|
---|
| 1039 | * Find Bspline approximation for this data set.
|
---|
| 1040 | * Uses global variable contour_pts to determine number of samples per
|
---|
| 1041 | * interval, where the knot vector intervals are assumed to be uniform, and
|
---|
| 1042 | * global variable contour_order for the order of Bspline to use.
|
---|
| 1043 | */
|
---|
| 1044 | static void
|
---|
| 1045 | put_contour_bspline(p_cntr, z_level, xx_min, xx_max, yy_min, yy_max, contr_isclosed)
|
---|
| 1046 | struct cntr_struct *p_cntr;
|
---|
| 1047 | double z_level, xx_min, xx_max, yy_min, yy_max;
|
---|
| 1048 | TBOOLEAN contr_isclosed;
|
---|
| 1049 | {
|
---|
| 1050 | int num_pts;
|
---|
| 1051 | int order = contour_order - 1;
|
---|
| 1052 |
|
---|
| 1053 | num_pts = count_contour(p_cntr); /* Number of points in contour. */
|
---|
| 1054 | if (num_pts < 2)
|
---|
| 1055 | return; /* Can't do nothing if empty or one points! */
|
---|
| 1056 | /* Order must be less than number of points in curve - fix it if needed. */
|
---|
| 1057 | if (order > num_pts - 1)
|
---|
| 1058 | order = num_pts - 1;
|
---|
| 1059 |
|
---|
| 1060 | gen_bspline_approx(p_cntr, num_pts, order, contr_isclosed);
|
---|
| 1061 | end_crnt_cntr();
|
---|
| 1062 | }
|
---|
| 1063 |
|
---|
| 1064 | /*
|
---|
| 1065 | * Free all elements in the contour list.
|
---|
| 1066 | */
|
---|
| 1067 | static void
|
---|
| 1068 | free_contour(p_cntr)
|
---|
| 1069 | struct cntr_struct *p_cntr;
|
---|
| 1070 | {
|
---|
| 1071 | struct cntr_struct *pc_temp;
|
---|
| 1072 |
|
---|
| 1073 | while (p_cntr) {
|
---|
| 1074 | pc_temp = p_cntr;
|
---|
| 1075 | p_cntr = p_cntr->next;
|
---|
| 1076 | free((char *) pc_temp);
|
---|
| 1077 | }
|
---|
| 1078 | }
|
---|
| 1079 |
|
---|
| 1080 | /*
|
---|
| 1081 | * Counts number of points in contour.
|
---|
| 1082 | */
|
---|
| 1083 | static int
|
---|
| 1084 | count_contour(p_cntr)
|
---|
| 1085 | struct cntr_struct *p_cntr;
|
---|
| 1086 | {
|
---|
| 1087 | int count = 0;
|
---|
| 1088 |
|
---|
| 1089 | while (p_cntr) {
|
---|
| 1090 | count++;
|
---|
| 1091 | p_cntr = p_cntr->next;
|
---|
| 1092 | }
|
---|
| 1093 | return count;
|
---|
| 1094 | }
|
---|
| 1095 |
|
---|
| 1096 | /*
|
---|
| 1097 | * Find second derivatives (x''(t_i),y''(t_i)) of cubic spline interpolation
|
---|
| 1098 | * through list of points (x_i,y_i). The parameter t is calculated as the
|
---|
| 1099 | * length of the linear stroke. The number of points must be at least 3.
|
---|
| 1100 | * Note: For closed contours the first and last point must be equal.
|
---|
| 1101 | */
|
---|
| 1102 | static int
|
---|
| 1103 | gen_cubic_spline(num_pts, p_cntr, d2x, d2y, delta_t, contr_isclosed, unit_x, unit_y)
|
---|
| 1104 | int num_pts; /* Number of points (num_pts>=3), input */
|
---|
| 1105 | struct cntr_struct *p_cntr; /* List of points (x(t_i),y(t_i)), input */
|
---|
| 1106 | double d2x[], d2y[], /* Second derivatives (x''(t_i),y''(t_i)), output */
|
---|
| 1107 | delta_t[]; /* List of interval lengths t_{i+1}-t_{i}, output */
|
---|
| 1108 | TBOOLEAN contr_isclosed; /* Closed or open contour?, input */
|
---|
| 1109 | double unit_x, unit_y; /* Unit length in x and y (norm=1), input */
|
---|
| 1110 | {
|
---|
| 1111 | int n, i;
|
---|
| 1112 | double norm;
|
---|
| 1113 | tri_diag *m; /* The tri-diagonal matrix is saved here. */
|
---|
| 1114 | struct cntr_struct *pc_temp;
|
---|
| 1115 |
|
---|
| 1116 | m = (tri_diag *) gp_alloc(num_pts * sizeof(tri_diag), "contour tridiag m");
|
---|
| 1117 |
|
---|
| 1118 | /*
|
---|
| 1119 | * Calculate first differences in (d2x[i], d2y[i]) and interval lengths
|
---|
| 1120 | * in delta_t[i]:
|
---|
| 1121 | */
|
---|
| 1122 | pc_temp = p_cntr;
|
---|
| 1123 | for (i = 0; i < num_pts - 1; i++) {
|
---|
| 1124 | d2x[i] = pc_temp->next->X - pc_temp->X;
|
---|
| 1125 | d2y[i] = pc_temp->next->Y - pc_temp->Y;
|
---|
| 1126 | /*
|
---|
| 1127 | * The norm of a linear stroke is calculated in "normal coordinates"
|
---|
| 1128 | * and used as interval length:
|
---|
| 1129 | */
|
---|
| 1130 | delta_t[i] = sqrt(SQR(d2x[i] / unit_x) + SQR(d2y[i] / unit_y));
|
---|
| 1131 |
|
---|
| 1132 | d2x[i] /= delta_t[i]; /* first difference, with unit norm: */
|
---|
| 1133 | d2y[i] /= delta_t[i]; /* || (d2x[i], d2y[i]) || = 1 */
|
---|
| 1134 |
|
---|
| 1135 | pc_temp = pc_temp->next;
|
---|
| 1136 | }
|
---|
| 1137 |
|
---|
| 1138 | /*
|
---|
| 1139 | * Setup linear system: m * x = b
|
---|
| 1140 | */
|
---|
| 1141 | n = num_pts - 2; /* Without first and last point */
|
---|
| 1142 | if (contr_isclosed) {
|
---|
| 1143 | /* First and last points must be equal for closed contours */
|
---|
| 1144 | delta_t[num_pts - 1] = delta_t[0];
|
---|
| 1145 | d2x[num_pts - 1] = d2x[0];
|
---|
| 1146 | d2y[num_pts - 1] = d2y[0];
|
---|
| 1147 | n++; /* Add last point (= first point) */
|
---|
| 1148 | }
|
---|
| 1149 | for (i = 0; i < n; i++) {
|
---|
| 1150 | /* Matrix M, mainly tridiagonal with cyclic second index ("j = j+n mod n") */
|
---|
| 1151 | m[i][0] = delta_t[i]; /* Off-diagonal element M_{i,i-1} */
|
---|
| 1152 | m[i][1] = 2. * (delta_t[i] + delta_t[i + 1]); /* M_{i,i} */
|
---|
| 1153 | m[i][2] = delta_t[i + 1]; /* Off-diagonal element M_{i,i+1} */
|
---|
| 1154 |
|
---|
| 1155 | /* Right side b_x and b_y */
|
---|
| 1156 | d2x[i] = (d2x[i + 1] - d2x[i]) * 6.;
|
---|
| 1157 | d2y[i] = (d2y[i + 1] - d2y[i]) * 6.;
|
---|
| 1158 |
|
---|
| 1159 | /*
|
---|
| 1160 | * If the linear stroke shows a cusps of more than 90 degree, the right
|
---|
| 1161 | * side is reduced to avoid oscillations in the spline:
|
---|
| 1162 | */
|
---|
| 1163 | norm = sqrt(SQR(d2x[i] / unit_x) + SQR(d2y[i] / unit_y)) / 8.5;
|
---|
| 1164 |
|
---|
| 1165 | if (norm > 1.) {
|
---|
| 1166 | d2x[i] /= norm;
|
---|
| 1167 | d2y[i] /= norm;
|
---|
| 1168 | /* The first derivative will not be continuous */
|
---|
| 1169 | }
|
---|
| 1170 | }
|
---|
| 1171 |
|
---|
| 1172 | if (!contr_isclosed) {
|
---|
| 1173 | /* Third derivative is set to zero at both ends */
|
---|
| 1174 | m[0][1] += m[0][0]; /* M_{0,0} */
|
---|
| 1175 | m[0][0] = 0.; /* M_{0,n-1} */
|
---|
| 1176 | m[n - 1][1] += m[n - 1][2]; /* M_{n-1,n-1} */
|
---|
| 1177 | m[n - 1][2] = 0.; /* M_{n-1,0} */
|
---|
| 1178 | }
|
---|
| 1179 | /* Solve linear systems for d2x[] and d2y[] */
|
---|
| 1180 |
|
---|
| 1181 |
|
---|
| 1182 | if (solve_cubic_1(m, n)) { /* Calculate Cholesky decomposition */
|
---|
| 1183 | solve_cubic_2(m, d2x, n); /* solve M * d2x = b_x */
|
---|
| 1184 | solve_cubic_2(m, d2y, n); /* solve M * d2y = b_y */
|
---|
| 1185 |
|
---|
| 1186 | } else { /* Should not happen, but who knows ... */
|
---|
| 1187 | free((char *) m);
|
---|
| 1188 | return FALSE;
|
---|
| 1189 | }
|
---|
| 1190 |
|
---|
| 1191 | /* Shift all second derivatives one place right and abdate end points */
|
---|
| 1192 | for (i = n; i > 0; i--) {
|
---|
| 1193 | d2x[i] = d2x[i - 1];
|
---|
| 1194 | d2y[i] = d2y[i - 1];
|
---|
| 1195 | }
|
---|
| 1196 | if (contr_isclosed) {
|
---|
| 1197 | d2x[0] = d2x[n];
|
---|
| 1198 | d2y[0] = d2y[n];
|
---|
| 1199 | } else {
|
---|
| 1200 | d2x[0] = d2x[1]; /* Third derivative is zero in */
|
---|
| 1201 | d2y[0] = d2y[1]; /* first and last interval */
|
---|
| 1202 | d2x[n + 1] = d2x[n];
|
---|
| 1203 | d2y[n + 1] = d2y[n];
|
---|
| 1204 | }
|
---|
| 1205 |
|
---|
| 1206 | free((char *) m);
|
---|
| 1207 | return TRUE;
|
---|
| 1208 | }
|
---|
| 1209 |
|
---|
| 1210 | /*
|
---|
| 1211 | * Calculate interpolated values of the spline function (defined via p_cntr
|
---|
| 1212 | * and the second derivatives d2x[] and d2y[]). The number of tabulated
|
---|
| 1213 | * values is n. On an equidistant grid n_intpol values are calculated.
|
---|
| 1214 | */
|
---|
| 1215 | static void
|
---|
| 1216 | intp_cubic_spline(n, p_cntr, d2x, d2y, delta_t, n_intpol)
|
---|
| 1217 | int n;
|
---|
| 1218 | struct cntr_struct *p_cntr;
|
---|
| 1219 | double d2x[], d2y[], delta_t[];
|
---|
| 1220 | int n_intpol;
|
---|
| 1221 | {
|
---|
| 1222 | double t, t_skip, t_max;
|
---|
| 1223 | double x0, x1, x, y0, y1, y;
|
---|
| 1224 | double d, hx, dx0, dx01, hy, dy0, dy01;
|
---|
| 1225 | int i;
|
---|
| 1226 |
|
---|
| 1227 | /* The length of the total interval */
|
---|
| 1228 | t_max = 0.;
|
---|
| 1229 | for (i = 0; i < n - 1; i++)
|
---|
| 1230 | t_max += delta_t[i];
|
---|
| 1231 |
|
---|
| 1232 | /* The distance between interpolated points */
|
---|
| 1233 | t_skip = (1. - 1e-7) * t_max / (n_intpol - 1);
|
---|
| 1234 |
|
---|
| 1235 | t = 0.; /* Parameter value */
|
---|
| 1236 | x1 = p_cntr->X;
|
---|
| 1237 | y1 = p_cntr->Y;
|
---|
| 1238 | add_cntr_point(x1, y1); /* First point. */
|
---|
| 1239 | t += t_skip;
|
---|
| 1240 |
|
---|
| 1241 | for (i = 0; i < n - 1; i++) {
|
---|
| 1242 | p_cntr = p_cntr->next;
|
---|
| 1243 |
|
---|
| 1244 | d = delta_t[i]; /* Interval length */
|
---|
| 1245 | x0 = x1;
|
---|
| 1246 | y0 = y1;
|
---|
| 1247 | x1 = p_cntr->X;
|
---|
| 1248 | y1 = p_cntr->Y;
|
---|
| 1249 | hx = (x1 - x0) / d;
|
---|
| 1250 | hy = (y1 - y0) / d;
|
---|
| 1251 | dx0 = (d2x[i + 1] + 2 * d2x[i]) / 6.;
|
---|
| 1252 | dy0 = (d2y[i + 1] + 2 * d2y[i]) / 6.;
|
---|
| 1253 | dx01 = (d2x[i + 1] - d2x[i]) / (6. * d);
|
---|
| 1254 | dy01 = (d2y[i + 1] - d2y[i]) / (6. * d);
|
---|
| 1255 | while (t <= delta_t[i]) { /* t in current interval ? */
|
---|
| 1256 | x = x0 + t * (hx + (t - d) * (dx0 + t * dx01));
|
---|
| 1257 | y = y0 + t * (hy + (t - d) * (dy0 + t * dy01));
|
---|
| 1258 | add_cntr_point(x, y); /* next point. */
|
---|
| 1259 | t += t_skip;
|
---|
| 1260 | }
|
---|
| 1261 | t -= delta_t[i]; /* Parameter t relative to start of next interval */
|
---|
| 1262 | }
|
---|
| 1263 | }
|
---|
| 1264 |
|
---|
| 1265 | /*
|
---|
| 1266 | * The following two procedures solve the special linear system which arise
|
---|
| 1267 | * in cubic spline interpolation. If x is assumed cyclic ( x[i]=x[n+i] ) the
|
---|
| 1268 | * equations can be written as (i=0,1,...,n-1):
|
---|
| 1269 | * m[i][0] * x[i-1] + m[i][1] * x[i] + m[i][2] * x[i+1] = b[i] .
|
---|
| 1270 | * In matrix notation one gets M * x = b, where the matrix M is tridiagonal
|
---|
| 1271 | * with additional elements in the upper right and lower left position:
|
---|
| 1272 | * m[i][0] = M_{i,i-1} for i=1,2,...,n-1 and m[0][0] = M_{0,n-1} ,
|
---|
| 1273 | * m[i][1] = M_{i, i } for i=0,1,...,n-1
|
---|
| 1274 | * m[i][2] = M_{i,i+1} for i=0,1,...,n-2 and m[n-1][2] = M_{n-1,0}.
|
---|
| 1275 | * M should be symmetric (m[i+1][0]=m[i][2]) and positiv definite.
|
---|
| 1276 | * The size of the system is given in n (n>=1).
|
---|
| 1277 | *
|
---|
| 1278 | * In the first procedure the Cholesky decomposition M = C^T * D * C
|
---|
| 1279 | * (C is upper triangle with unit diagonal, D is diagonal) is calculated.
|
---|
| 1280 | * Return TRUE if decomposition exist.
|
---|
| 1281 | */
|
---|
| 1282 | static int
|
---|
| 1283 | solve_cubic_1(m, n)
|
---|
| 1284 | tri_diag m[];
|
---|
| 1285 | int n;
|
---|
| 1286 | {
|
---|
| 1287 | int i;
|
---|
| 1288 | double m_ij, m_n, m_nn, d;
|
---|
| 1289 |
|
---|
| 1290 | if (n < 1)
|
---|
| 1291 | return FALSE; /* Dimension should be at least 1 */
|
---|
| 1292 |
|
---|
| 1293 | d = m[0][1]; /* D_{0,0} = M_{0,0} */
|
---|
| 1294 | if (d <= 0.)
|
---|
| 1295 | return FALSE; /* M (or D) should be positiv definite */
|
---|
| 1296 | m_n = m[0][0]; /* M_{0,n-1} */
|
---|
| 1297 | m_nn = m[n - 1][1]; /* M_{n-1,n-1} */
|
---|
| 1298 | for (i = 0; i < n - 2; i++) {
|
---|
| 1299 | m_ij = m[i][2]; /* M_{i,1} */
|
---|
| 1300 | m[i][2] = m_ij / d; /* C_{i,i+1} */
|
---|
| 1301 | m[i][0] = m_n / d; /* C_{i,n-1} */
|
---|
| 1302 | m_nn -= m[i][0] * m_n; /* to get C_{n-1,n-1} */
|
---|
| 1303 | m_n = -m[i][2] * m_n; /* to get C_{i+1,n-1} */
|
---|
| 1304 | d = m[i + 1][1] - m[i][2] * m_ij; /* D_{i+1,i+1} */
|
---|
| 1305 | if (d <= 0.)
|
---|
| 1306 | return FALSE; /* Elements of D should be positiv */
|
---|
| 1307 | m[i + 1][1] = d;
|
---|
| 1308 | }
|
---|
| 1309 | if (n >= 2) { /* Complete last column */
|
---|
| 1310 | m_n += m[n - 2][2]; /* add M_{n-2,n-1} */
|
---|
| 1311 | m[n - 2][0] = m_n / d; /* C_{n-2,n-1} */
|
---|
| 1312 | m[n - 1][1] = d = m_nn - m[n - 2][0] * m_n; /* D_{n-1,n-1} */
|
---|
| 1313 | if (d <= 0.)
|
---|
| 1314 | return FALSE;
|
---|
| 1315 | }
|
---|
| 1316 | return TRUE;
|
---|
| 1317 | }
|
---|
| 1318 |
|
---|
| 1319 | /*
|
---|
| 1320 | * The second procedure solves the linear system, with the Choleky
|
---|
| 1321 | * decomposition calculated above (in m[][]) and the right side b given
|
---|
| 1322 | * in x[]. The solution x overwrites the right side in x[].
|
---|
| 1323 | */
|
---|
| 1324 | static void
|
---|
| 1325 | solve_cubic_2(m, x, n)
|
---|
| 1326 | tri_diag m[];
|
---|
| 1327 | double x[];
|
---|
| 1328 | int n;
|
---|
| 1329 | {
|
---|
| 1330 | int i;
|
---|
| 1331 | double x_n;
|
---|
| 1332 |
|
---|
| 1333 | /* Division by transpose of C : b = C^{-T} * b */
|
---|
| 1334 | x_n = x[n - 1];
|
---|
| 1335 | for (i = 0; i < n - 2; i++) {
|
---|
| 1336 | x[i + 1] -= m[i][2] * x[i]; /* C_{i,i+1} * x_{i} */
|
---|
| 1337 | x_n -= m[i][0] * x[i]; /* C_{i,n-1} * x_{i} */
|
---|
| 1338 | }
|
---|
| 1339 | if (n >= 2)
|
---|
| 1340 | x[n - 1] = x_n - m[n - 2][0] * x[n - 2]; /* C_{n-2,n-1} * x_{n-1} */
|
---|
| 1341 |
|
---|
| 1342 | /* Division by D: b = D^{-1} * b */
|
---|
| 1343 | for (i = 0; i < n; i++)
|
---|
| 1344 | x[i] /= m[i][1];
|
---|
| 1345 |
|
---|
| 1346 | /* Division by C: b = C^{-1} * b */
|
---|
| 1347 | x_n = x[n - 1];
|
---|
| 1348 | if (n >= 2)
|
---|
| 1349 | x[n - 2] -= m[n - 2][0] * x_n; /* C_{n-2,n-1} * x_{n-1} */
|
---|
| 1350 | for (i = n - 3; i >= 0; i--) {
|
---|
| 1351 | /* C_{i,i+1} * x_{i+1} + C_{i,n-1} * x_{n-1} */
|
---|
| 1352 | x[i] -= m[i][2] * x[i + 1] + m[i][0] * x_n;
|
---|
| 1353 | }
|
---|
| 1354 | return;
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
| 1357 | /*
|
---|
| 1358 | * Solve tri diagonal linear system equation. The tri diagonal matrix is
|
---|
| 1359 | * defined via matrix M, right side is r, and solution X i.e. M * X = R.
|
---|
| 1360 | * Size of system given in n. Return TRUE if solution exist.
|
---|
| 1361 | */
|
---|
| 1362 | /* not used any more in "contour.c", but in "spline.c" (21. Dec. 1995) ! */
|
---|
| 1363 |
|
---|
| 1364 | int
|
---|
| 1365 | solve_tri_diag(m, r, x, n)
|
---|
| 1366 | tri_diag m[];
|
---|
| 1367 | double r[], x[];
|
---|
| 1368 | int n;
|
---|
| 1369 | {
|
---|
| 1370 | int i;
|
---|
| 1371 | double t;
|
---|
| 1372 |
|
---|
| 1373 | for (i = 1; i < n; i++) { /* Eliminate element m[i][i-1] (lower diagonal). */
|
---|
| 1374 | if (m[i - 1][1] == 0)
|
---|
| 1375 | return FALSE;
|
---|
| 1376 | t = m[i][0] / m[i - 1][1]; /* Find ratio between the two lines. */
|
---|
| 1377 | /* m[i][0] = m[i][0] - m[i-1][1] * t; */
|
---|
| 1378 | /* m[i][0] is not used any more (and set to 0 in the above line) */
|
---|
| 1379 | m[i][1] = m[i][1] - m[i - 1][2] * t;
|
---|
| 1380 | r[i] = r[i] - r[i - 1] * t;
|
---|
| 1381 | }
|
---|
| 1382 | /* Now do back subtitution - update the solution vector X: */
|
---|
| 1383 | if (m[n - 1][1] == 0)
|
---|
| 1384 | return FALSE;
|
---|
| 1385 | x[n - 1] = r[n - 1] / m[n - 1][1]; /* Find last element. */
|
---|
| 1386 | for (i = n - 2; i >= 0; i--) {
|
---|
| 1387 | if (m[i][1] == 0)
|
---|
| 1388 | return FALSE;
|
---|
| 1389 | x[i] = (r[i] - x[i + 1] * m[i][2]) / m[i][1];
|
---|
| 1390 | }
|
---|
| 1391 | return TRUE;
|
---|
| 1392 | }
|
---|
| 1393 |
|
---|
| 1394 | /*
|
---|
| 1395 | * Generate a Bspline curve defined by all the points given in linked list p:
|
---|
| 1396 | * Algorithm: using deBoor algorithm
|
---|
| 1397 | * Note: if Curvekind is open contour than Open end knot vector is assumed,
|
---|
| 1398 | * else (closed contour) Float end knot vector is assumed.
|
---|
| 1399 | * It is assumed that num_of_points is at least 2, and order of Bspline is less
|
---|
| 1400 | * than num_of_points!
|
---|
| 1401 | */
|
---|
| 1402 | static void
|
---|
| 1403 | gen_bspline_approx(p_cntr, num_of_points, order, contr_isclosed)
|
---|
| 1404 | struct cntr_struct *p_cntr;
|
---|
| 1405 | int num_of_points, order;
|
---|
| 1406 | TBOOLEAN contr_isclosed;
|
---|
| 1407 | {
|
---|
| 1408 | int knot_index = 0, pts_count = 1;
|
---|
| 1409 | double dt, t, next_t, t_min, t_max, x, y;
|
---|
| 1410 | struct cntr_struct *pc_temp = p_cntr, *pc_tail = NULL;
|
---|
| 1411 |
|
---|
| 1412 | /* If the contour is Closed one we must update few things:
|
---|
| 1413 | * 1. Make the list temporary circular, so we can close the contour.
|
---|
| 1414 | * 2. Update num_of_points - increase it by "order-1" so contour will be
|
---|
| 1415 | * closed. This will evaluate order more sections to close it!
|
---|
| 1416 | */
|
---|
| 1417 | if (contr_isclosed) {
|
---|
| 1418 | pc_tail = p_cntr;
|
---|
| 1419 | while (pc_tail->next)
|
---|
| 1420 | pc_tail = pc_tail->next; /* Find last point. */
|
---|
| 1421 |
|
---|
| 1422 | /* test if first and last point are equal */
|
---|
| 1423 | if (fuzzy_equal(pc_tail, p_cntr)) {
|
---|
| 1424 | /* Close contour list - make it circular. */
|
---|
| 1425 | pc_tail->next = p_cntr->next;
|
---|
| 1426 | num_of_points += order - 1;
|
---|
| 1427 | } else {
|
---|
| 1428 | pc_tail->next = p_cntr;
|
---|
| 1429 | num_of_points += order;
|
---|
| 1430 | }
|
---|
| 1431 | }
|
---|
| 1432 | /* Find first (t_min) and last (t_max) t value to eval: */
|
---|
| 1433 | t = t_min = fetch_knot(contr_isclosed, num_of_points, order, order);
|
---|
| 1434 | t_max = fetch_knot(contr_isclosed, num_of_points, order, num_of_points);
|
---|
| 1435 | next_t = t_min + 1.0;
|
---|
| 1436 | knot_index = order;
|
---|
| 1437 | dt = 1.0 / contour_pts; /* Number of points per one section. */
|
---|
| 1438 |
|
---|
| 1439 |
|
---|
| 1440 | while (t < t_max) {
|
---|
| 1441 | if (t > next_t) {
|
---|
| 1442 | pc_temp = pc_temp->next; /* Next order ctrl. pt. to blend. */
|
---|
| 1443 | knot_index++;
|
---|
| 1444 | next_t += 1.0;
|
---|
| 1445 | }
|
---|
| 1446 | eval_bspline(t, pc_temp, num_of_points, order, knot_index,
|
---|
| 1447 | contr_isclosed, &x, &y); /* Next pt. */
|
---|
| 1448 | add_cntr_point(x, y);
|
---|
| 1449 | pts_count++;
|
---|
| 1450 | /* As we might have some real number round off problems we do */
|
---|
| 1451 | /* the last point outside the loop */
|
---|
| 1452 | if (pts_count == contour_pts * (num_of_points - order) + 1)
|
---|
| 1453 | break;
|
---|
| 1454 | t += dt;
|
---|
| 1455 | }
|
---|
| 1456 |
|
---|
| 1457 | /* Now do the last point */
|
---|
| 1458 | eval_bspline(t_max - EPSILON, pc_temp, num_of_points, order, knot_index,
|
---|
| 1459 | contr_isclosed, &x, &y);
|
---|
| 1460 | add_cntr_point(x, y); /* Complete the contour. */
|
---|
| 1461 |
|
---|
| 1462 | if (contr_isclosed) /* Update list - un-circular it. */
|
---|
| 1463 | pc_tail->next = NULL;
|
---|
| 1464 | }
|
---|
| 1465 |
|
---|
| 1466 | /*
|
---|
| 1467 | * The routine to evaluate the B-spline value at point t using knot vector
|
---|
| 1468 | * from function fetch_knot(), and the control points p_cntr.
|
---|
| 1469 | * Returns (x, y) of approximated B-spline. Note that p_cntr points on the
|
---|
| 1470 | * first control point to blend with. The B-spline is of order order.
|
---|
| 1471 | */
|
---|
| 1472 | static void
|
---|
| 1473 | eval_bspline(t, p_cntr, num_of_points, order, j, contr_isclosed, x, y)
|
---|
| 1474 | double t;
|
---|
| 1475 | struct cntr_struct *p_cntr;
|
---|
| 1476 | int num_of_points, order, j;
|
---|
| 1477 | TBOOLEAN contr_isclosed;
|
---|
| 1478 | double *x, *y;
|
---|
| 1479 | {
|
---|
| 1480 | int i, p;
|
---|
| 1481 | double ti, tikp, *dx, *dy; /* Copy p_cntr into it to make it faster. */
|
---|
| 1482 |
|
---|
| 1483 | dx = (double *) gp_alloc((order + j) * sizeof(double), "contour b_spline");
|
---|
| 1484 | dy = (double *) gp_alloc((order + j) * sizeof(double), "contour b_spline");
|
---|
| 1485 |
|
---|
| 1486 | /* Set the dx/dy - [0] iteration step, control points (p==0 iterat.): */
|
---|
| 1487 | for (i = j - order; i <= j; i++) {
|
---|
| 1488 | dx[i] = p_cntr->X;
|
---|
| 1489 | dy[i] = p_cntr->Y;
|
---|
| 1490 | p_cntr = p_cntr->next;
|
---|
| 1491 | }
|
---|
| 1492 |
|
---|
| 1493 | for (p = 1; p <= order; p++) { /* Iteration (b-spline level) counter. */
|
---|
| 1494 | for (i = j; i >= j - order + p; i--) { /* Control points indexing. */
|
---|
| 1495 | ti = fetch_knot(contr_isclosed, num_of_points, order, i);
|
---|
| 1496 | tikp = fetch_knot(contr_isclosed, num_of_points, order, i + order + 1 - p);
|
---|
| 1497 | if (ti == tikp) { /* Should not be a problems but how knows... */
|
---|
| 1498 | } else {
|
---|
| 1499 | dx[i] = dx[i] * (t - ti) / (tikp - ti) + /* Calculate x. */
|
---|
| 1500 | dx[i - 1] * (tikp - t) / (tikp - ti);
|
---|
| 1501 | dy[i] = dy[i] * (t - ti) / (tikp - ti) + /* Calculate y. */
|
---|
| 1502 | dy[i - 1] * (tikp - t) / (tikp - ti);
|
---|
| 1503 | }
|
---|
| 1504 | }
|
---|
| 1505 | }
|
---|
| 1506 | *x = dx[j];
|
---|
| 1507 | *y = dy[j];
|
---|
| 1508 | free((char *) dx);
|
---|
| 1509 | free((char *) dy);
|
---|
| 1510 | }
|
---|
| 1511 |
|
---|
| 1512 | /*
|
---|
| 1513 | * Routine to get the i knot from uniform knot vector. The knot vector
|
---|
| 1514 | * might be float (Knot(i) = i) or open (where the first and last "order"
|
---|
| 1515 | * knots are equal). contr_isclosed determines knot kind - open contour means
|
---|
| 1516 | * open knot vector, and closed contour selects float knot vector.
|
---|
| 1517 | * Note the knot vector is not exist and this routine simulates it existance
|
---|
| 1518 | * Also note the indexes for the knot vector starts from 0.
|
---|
| 1519 | */
|
---|
| 1520 | static double
|
---|
| 1521 | fetch_knot(contr_isclosed, num_of_points, order, i)
|
---|
| 1522 | TBOOLEAN contr_isclosed;
|
---|
| 1523 | int num_of_points, order, i;
|
---|
| 1524 | {
|
---|
| 1525 | if(! contr_isclosed) {
|
---|
| 1526 | if (i <= order)
|
---|
| 1527 | return 0.0;
|
---|
| 1528 | else if (i <= num_of_points)
|
---|
| 1529 | return (double) (i - order);
|
---|
| 1530 | else
|
---|
| 1531 | return (double) (num_of_points - order);
|
---|
| 1532 | } else {
|
---|
| 1533 | return (double) i;
|
---|
| 1534 | }
|
---|
| 1535 | }
|
---|