1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/******************************************************************************
*
* Project: DXF Translator
* Purpose: Low level spline interpolation.
* Author: David F. Rogers
*
******************************************************************************
This code is derived from the code associated with the book "An Introduction
to NURBS" by David F. Rogers. More information on the book and the code is
available at:
http://www.nar-associates.com/nurbs/
David F. Rogers agreed explicitly that the subset of his code in this file to
be used under the following license (cf
https://github.com/OSGeo/gdal/issues/6524)
Copyright (c) 2009, David F. Rogers
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of David F. Rogers nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <vector>
#include "cpl_port.h"
/* used by ogrdxflayer.cpp */
void rbspline2(int npts, int k, int p1, double b[], double h[],
bool bCalculateKnots, double x[], double p[]);
/* used by ogrdxf_leader.cpp */
void basis(int c, double t, int npts, double x[], double N[]);
/* used by DWG driver */
void rbspline(int npts, int k, int p1, double b[], double h[], double p[]);
void rbsplinu(int npts, int k, int p1, double b[], double h[], double p[]);
/************************************************************************/
/* knotu() */
/************************************************************************/
/* Subroutine to generate a B-spline uniform (periodic) knot vector.
c = order of the basis function
n = the number of defining polygon vertices
nplus2 = index of x() for the first occurrence of the maximum knot
vector value nplusc = maximum value of the knot vector -- $n + c$ x[]
= array containing the knot vector
*/
static void knotu(int n, int c, double x[])
{
int nplusc, /* nplus2, */ i;
nplusc = n + c;
/* nplus2 = n + 2; */
x[1] = 0;
for (i = 2; i <= nplusc; i++)
{
x[i] = i - 1;
}
}
/************************************************************************/
/* knot() */
/************************************************************************/
/*
Subroutine to generate a B-spline open knot vector with multiplicity
equal to the order at the ends.
c = order of the basis function
n = the number of defining polygon vertices
nplus2 = index of x() for the first occurrence of the maximum knot
vector value nplusc = maximum value of the knot vector -- $n + c$ x()
= array containing the knot vector
*/
static void knot(int n, int c, double x[])
{
const int nplusc = n + c;
const int nplus2 = n + 2;
x[1] = 0.0;
for (int i = 2; i <= nplusc; i++)
{
if ((i > c) && (i < nplus2))
x[i] = x[i - 1] + 1.0;
else
x[i] = x[i - 1];
}
}
/************************************************************************/
/* basis() */
/************************************************************************/
/* Subroutine to generate B-spline basis functions--open knot vector
C code for An Introduction to NURBS
by David F. Rogers. Copyright (C) 2000 David F. Rogers,
All rights reserved.
Name: rbasis (split into rbasis and basis by AT)
Language: C
Subroutines called: none
Book reference: Chapter 4, Sec. 4. , p 296
c = order of the B-spline basis function
d = first term of the basis function recursion relation
e = second term of the basis function recursion relation
h[] = array containing the homogeneous weights
npts = number of defining polygon vertices
nplusc = constant -- npts + c -- maximum number of knot values
r[] = array containing the rationalbasis functions
r[1] contains the basis function associated with B1 etc.
t = parameter value
N[] = array output from bbasis containing the values of the
basis functions; should have capacity npts + c + 1
x[] = knot vector
*/
void basis(int c, double t, int npts, double x[], double N[])
{
const int nplusc = npts + c;
/* calculate the first order nonrational basis functions n[i] */
for (int i = 1; i <= nplusc - 1; i++)
{
if ((t >= x[i]) && (t < x[i + 1]))
N[i] = 1.0;
else
N[i] = 0.0;
}
/* calculate the higher order nonrational basis functions */
for (int k = 2; k <= c; k++)
{
for (int i = 1; i <= nplusc - k; i++)
{
double d = 0.0;
double e = 0.0;
if (N[i] != 0) /* if the lower order basis function is zero skip the
calculation */
{
double denom = x[i + k - 1] - x[i];
if (denom != 0)
d = ((t - x[i]) * N[i]) / denom;
}
// else
// d = 0.0 ;
if (N[i + 1] != 0) /* if the lower order basis function is zero skip
the calculation */
{
double denom = x[i + k] - x[i + 1];
if (denom != 0)
e = ((x[i + k] - t) * N[i + 1]) / denom;
}
// else
// e = 0.0;
N[i] = d + e;
}
}
if (t == (double)x[nplusc])
{ /* pick up last point */
N[npts] = 1;
}
}
/************************************************************************/
/* rbasis() */
/* */
/* Subroutine to generate rational B-spline basis functions. */
/* See the comment for basis() above. */
/************************************************************************/
static void rbasis(int c, double t, int npts, double x[], double h[],
double r[])
{
const int nplusc = npts + c;
std::vector<double> temp;
temp.resize(nplusc + 1);
basis(c, t, npts, x, &temp[0]);
/* calculate sum for denominator of rational basis functions */
double sum = 0.0;
for (int i = 1; i <= npts; i++)
{
sum = sum + temp[i] * h[i];
}
/* form rational basis functions and put in r vector */
for (int i = 1; i <= npts; i++)
{
if (sum != 0)
{
r[i] = (temp[i] * h[i]) / (sum);
}
else
{
r[i] = 0;
}
}
}
/************************************************************************/
/* rbspline2() */
/************************************************************************/
/* Subroutine to generate a rational B-spline curve.
C code for An Introduction to NURBS
by David F. Rogers. Copyright (C) 2000 David F. Rogers,
All rights reserved.
Name: rbspline.c
Language: C
Subroutines called: knot.c, rbasis.c, fmtmul.c
Book reference: Chapter 4, Alg. p. 297
b[] = array containing the defining polygon vertices
b[1] contains the x-component of the vertex
b[2] contains the y-component of the vertex
b[3] contains the z-component of the vertex
h[] = array containing the homogeneous weighting factors
k = order of the B-spline basis function
nbasis = array containing the basis functions for a single value of t
nplusc = number of knot values
npts = number of defining polygon vertices
p[,] = array containing the curve points
p[1] contains the x-component of the point
p[2] contains the y-component of the point
p[3] contains the z-component of the point
p1 = number of points to be calculated on the curve
t = parameter value 0 <= t <= npts - k + 1
bCalculateKnots = when set to true, x will be filled with the knot()
routine, otherwise its content will be used. knots[] = array containing
the knot vector (must be npts + k + 1 large)
*/
void rbspline2(int npts, int k, int p1, double b[], double h[],
bool bCalculateKnots, double knots[], double p[])
{
const int nplusc = npts + k;
std::vector<double> nbasis;
nbasis.resize(npts + 1);
/* generate the uniform open knot vector */
if (bCalculateKnots == true)
knot(npts, k, knots);
int icount = 0;
/* calculate the points on the rational B-spline curve */
double t = knots[1];
const double step = (knots[nplusc] - knots[1]) / ((double)(p1 - 1));
const double eps = 5e-6 * (knots[nplusc] - knots[1]);
for (int i1 = 1; i1 <= p1; i1++)
{
/* avoid undershooting the final knot */
if ((double)knots[nplusc] - t < eps)
{
t = (double)knots[nplusc];
}
/* generate the basis function for this value of t */
rbasis(k, t, npts, knots, h, &(nbasis[0]));
for (int j = 1; j <= 3; j++)
{ /* generate a point on the curve */
int jcount = j;
p[icount + j] = 0.;
for (int i = 1; i <= npts; i++)
{ /* Do local matrix multiplication */
const double temp = nbasis[i] * b[jcount];
p[icount + j] = p[icount + j] + temp;
jcount = jcount + 3;
}
}
icount = icount + 3;
t = t + step;
}
}
/************************************************************************/
/* rbspline() */
/************************************************************************/
/* Subroutine to generate a rational B-spline curve using an uniform open knot
vector
C code for An Introduction to NURBS
by David F. Rogers. Copyright (C) 2000 David F. Rogers,
All rights reserved.
Name: rbspline.c
Language: C
Subroutines called: knot.c, rbasis.c, fmtmul.c
Book reference: Chapter 4, Alg. p. 297
b[] = array containing the defining polygon vertices
b[1] contains the x-component of the vertex
b[2] contains the y-component of the vertex
b[3] contains the z-component of the vertex
h[] = array containing the homogeneous weighting factors
k = order of the B-spline basis function
nbasis = array containing the basis functions for a single value of t
nplusc = number of knot values
npts = number of defining polygon vertices
p[,] = array containing the curve points
p[1] contains the x-component of the point
p[2] contains the y-component of the point
p[3] contains the z-component of the point
p1 = number of points to be calculated on the curve
t = parameter value 0 <= t <= npts - k + 1
x[] = array containing the knot vector
*/
void rbspline(int npts, int k, int p1, double b[], double h[], double p[])
{
std::vector<double> x(npts + k + 1, 0.0);
rbspline2(npts, k, p1, b, h, true, &x[0], p);
}
/************************************************************************/
/* rbsplinu() */
/************************************************************************/
/* Subroutine to generate a rational B-spline curve using an uniform periodic
knot vector
C code for An Introduction to NURBS
by David F. Rogers. Copyright (C) 2000 David F. Rogers,
All rights reserved.
Name: rbsplinu.c
Language: C
Subroutines called: knotu.c, rbasis.c, fmtmul.c
Book reference: Chapter 4, Alg. p. 298
b[] = array containing the defining polygon vertices
b[1] contains the x-component of the vertex
b[2] contains the y-component of the vertex
b[3] contains the z-component of the vertex
h[] = array containing the homogeneous weighting factors
k = order of the B-spline basis function
nbasis = array containing the basis functions for a single value of t
nplusc = number of knot values
npts = number of defining polygon vertices
p[,] = array containing the curve points
p[1] contains the x-component of the point
p[2] contains the y-component of the point
p[3] contains the z-component of the point
p1 = number of points to be calculated on the curve
t = parameter value 0 <= t <= npts - k + 1
x[] = array containing the knot vector
*/
void rbsplinu(int npts, int k, int p1, double b[], double h[], double p[])
{
int i, j, icount, jcount;
int i1;
int nplusc;
double step;
double t;
double temp;
std::vector<double> nbasis;
std::vector<double> x;
nplusc = npts + k;
x.resize(nplusc + 1);
nbasis.resize(npts + 1);
/* zero and redimension the knot vector and the basis array */
for (i = 0; i <= npts; i++)
{
nbasis[i] = 0.;
}
for (i = 0; i <= nplusc; i++)
{
x[i] = 0;
}
/* generate the uniform periodic knot vector */
knotu(npts, k, &(x[0]));
icount = 0;
/* calculate the points on the rational B-spline curve */
t = k - 1;
step = ((double)((npts) - (k - 1))) / ((double)(p1 - 1));
for (i1 = 1; i1 <= p1; i1++)
{
if (x[nplusc] - t < 5e-6)
{
t = x[nplusc];
}
rbasis(
k, t, npts, &(x[0]), h,
&(nbasis[0])); /* generate the basis function for this value of t */
for (j = 1; j <= 3; j++)
{ /* generate a point on the curve */
jcount = j;
p[icount + j] = 0.;
for (i = 1; i <= npts; i++)
{ /* Do local matrix multiplication */
temp = nbasis[i] * b[jcount];
p[icount + j] = p[icount + j] + temp;
jcount = jcount + 3;
}
}
icount = icount + 3;
t = t + step;
}
}
|