spline.c 6.05 KB
Newer Older
1
/* spline.c: spline and spline list (represented as arrays) manipulation.
Sven Neumann's avatar
Sven Neumann committed
2 3 4
 *
 * Copyright (C) 1992 Free Software Foundation, Inc.
 *
5
 * This program is free software: you can redistribute it and/or modify
Sven Neumann's avatar
Sven Neumann committed
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3, or (at your option)
Sven Neumann's avatar
Sven Neumann committed
8 9 10 11 12 13 14 15
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Sven Neumann's avatar
Sven Neumann committed
17 18 19
 */

#include "config.h"
20 21 22

#include <assert.h>

23 24
#include <glib.h>

25 26 27 28 29 30
#include "global.h"
#include "bounding-box.h"
#include "spline.h"
#include "vector.h"

/* Return a new spline structure, initialized with (recognizable)
31
   garbage.  */
32 33

spline_type
34
new_spline (void)
35
{
36
  real_coordinate_type coord = { -100.0, -100.0 };
37 38 39 40 41 42
  spline_type spline;

  START_POINT (spline)
    = CONTROL1 (spline)
    = CONTROL2 (spline)
    = END_POINT (spline)
43
    = coord;
44
  SPLINE_DEGREE (spline) = -1;
45
  SPLINE_LINEARITY (spline) = 0;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  return spline;
}


/* Print a spline in human-readable form.  */

void
print_spline (FILE *f, spline_type s)
{
  if (SPLINE_DEGREE (s) == LINEAR)
    fprintf (f, "(%.3f,%.3f)--(%.3f,%.3f).\n",
                START_POINT (s).x, START_POINT (s).y,
                END_POINT (s).x, END_POINT (s).y);

  else if (SPLINE_DEGREE (s) == CUBIC)
    fprintf (f, "(%.3f,%.3f)..ctrls(%.3f,%.3f)&(%.3f,%.3f)..(%.3f,%.3f).\n",
                START_POINT (s).x, START_POINT (s).y,
                CONTROL1 (s).x, CONTROL1 (s).y,
                CONTROL2 (s).x, CONTROL2 (s).y,
                END_POINT (s).x, END_POINT (s).y);

  else
    {
/*       FATAL1 ("print_spline: strange degree (%d)", SPLINE_DEGREE (s)); */
    }
}

/* Evaluate the spline S at a given T value.  This is an implementation
   of de Casteljau's algorithm.  See Schneider's thesis (reference in
   ../limn/README), p.37.  The variable names are taken from there.  */

real_coordinate_type
evaluate_spline (spline_type s, real t)
{
  spline_type V[4];    /* We need degree+1 splines, but assert degree <= 3.  */
  unsigned i, j;
  real one_minus_t = 1.0 - t;
  polynomial_degree degree = SPLINE_DEGREE (s);

  for (i = 0; i <= degree; i++)
    V[0].v[i] = s.v[i];

  for (j = 1; j <= degree; j++)
    for (i = 0; i <= degree - j; i++)
      {
92
#if defined (__GNUC__)
93 94 95
        real_coordinate_type t1 = Pmult_scalar (V[j - 1].v[i], one_minus_t);
        real_coordinate_type t2 = Pmult_scalar (V[j - 1].v[i + 1], t);
        V[j].v[i] = Padd (t1, t2);
96
#else
97
	/* HB: the above is really nice, but is there any other compiler
98 99 100 101 102 103 104 105 106 107 108
	 * supporting this ??
	 */
        real_coordinate_type t1;
        real_coordinate_type t2;
        t1.x = V[j - 1].v[i].x * one_minus_t;
        t1.y = V[j - 1].v[i].y * one_minus_t;
        t2.x = V[j - 1].v[i + 1].x * t;
        t2.y = V[j - 1].v[i + 1].y * t;
        V[j].v[i].x = t1.x + t2.x;
        V[j].v[i].y = t1.y + t2.y;
#endif
109 110 111 112 113 114 115 116 117 118
      }

  return V[degree].v[0];
}



/* Return a new, empty, spline list.  */

spline_list_type *
119
new_spline_list (void)
120
{
121
  spline_list_type *answer = g_new (spline_list_type, 1);
122 123 124 125 126 127 128 129 130 131 132 133 134

  SPLINE_LIST_DATA (*answer) = NULL;
  SPLINE_LIST_LENGTH (*answer) = 0;

  return answer;
}


/* Return a new spline list with SPLINE as the first element.  */

spline_list_type *
init_spline_list (spline_type spline)
{
135
  spline_list_type *answer = g_new (spline_list_type, 1);
136

137
  SPLINE_LIST_DATA (*answer) = g_new (spline_type, 1);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  SPLINE_LIST_ELT (*answer, 0) = spline;
  SPLINE_LIST_LENGTH (*answer) = 1;

  return answer;
}


/* Free the storage in a spline list.  We don't have to free the
   elements, since they are arrays in automatic storage.  And we don't
   want to free the list if it was empty.  */

void
free_spline_list (spline_list_type *spline_list)
{
  if (SPLINE_LIST_DATA (*spline_list) != NULL)
    safe_free ((address *) &(SPLINE_LIST_DATA (*spline_list)));
}


/* Append the spline S to the list SPLINE_LIST.  */

void
append_spline (spline_list_type *l, spline_type s)
{
  assert (l != NULL);
163

164
  SPLINE_LIST_LENGTH (*l)++;
165
  SPLINE_LIST_DATA (*l) = g_realloc (SPLINE_LIST_DATA (*l),
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                               SPLINE_LIST_LENGTH (*l) * sizeof (spline_type));
  LAST_SPLINE_LIST_ELT (*l) = s;
}


/* Tack the elements in the list S2 onto the end of S1.
   S2 is not changed.  */

void
concat_spline_lists (spline_list_type *s1, spline_list_type s2)
{
  unsigned this_spline;
  unsigned new_length;

  assert (s1 != NULL);

  new_length = SPLINE_LIST_LENGTH (*s1) + SPLINE_LIST_LENGTH (s2);

184
  SPLINE_LIST_DATA (*s1) = g_realloc(SPLINE_LIST_DATA (*s1),new_length * sizeof(spline_type));
185 186 187 188 189 190 191 192 193 194

  for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH (s2); this_spline++)
    SPLINE_LIST_ELT (*s1, SPLINE_LIST_LENGTH (*s1)++)
      = SPLINE_LIST_ELT (s2, this_spline);
}


/* Return a new, empty, spline list array.  */

spline_list_array_type
195
new_spline_list_array (void)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
{
  spline_list_array_type answer;

  SPLINE_LIST_ARRAY_DATA (answer) = NULL;
  SPLINE_LIST_ARRAY_LENGTH (answer) = 0;

  return answer;
}


/* Free the storage in a spline list array.  We don't
   want to free the list if it is empty.  */

void
free_spline_list_array (spline_list_array_type *spline_list_array)
{
  unsigned this_list;

  for (this_list = 0;
       this_list < SPLINE_LIST_ARRAY_LENGTH (*spline_list_array);
216
       this_list++)
217 218 219 220 221 222 223 224 225 226 227 228 229
    free_spline_list (&SPLINE_LIST_ARRAY_ELT (*spline_list_array, this_list));

  if (SPLINE_LIST_ARRAY_DATA (*spline_list_array) != NULL)
    safe_free ((address *) &(SPLINE_LIST_ARRAY_DATA (*spline_list_array)));
}


/* Append the spline S to the list SPLINE_LIST_ARRAY.  */

void
append_spline_list (spline_list_array_type *l, spline_list_type s)
{
  SPLINE_LIST_ARRAY_LENGTH (*l)++;
230

231
  SPLINE_LIST_ARRAY_DATA (*l) = g_realloc(SPLINE_LIST_ARRAY_DATA (*l),(SPLINE_LIST_ARRAY_LENGTH (*l))*sizeof(spline_list_type));
232 233
  LAST_SPLINE_LIST_ARRAY_ELT (*l) = s;
}