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