PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/v5_9_8_debian/lib/csa/csa.c

#
C | 2241 lines | 1736 code | 297 blank | 208 comment | 400 complexity | db0a5d075c4e7b95c225cbfd29e0ee87 MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, Apache-2.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. //--------------------------------------------------------------------------
  2. //
  3. // File: csa.c
  4. //
  5. // Created: 16/10/2002
  6. //
  7. // Author: Pavel Sakov
  8. // CSIRO Marine Research
  9. //
  10. // Purpose: 2D data approximation with bivariate C1 cubic spline.
  11. // A set of library functions + standalone utility.
  12. //
  13. // Description: See J. Haber, F. Zeilfelder, O.Davydov and H.-P. Seidel,
  14. // Smooth approximation and rendering of large scattered data
  15. // sets, in ``Proceedings of IEEE Visualization 2001''
  16. // (Th.Ertl, K.Joy and A.Varshney, Eds.), pp.341-347, 571,
  17. // IEEE Computer Society, 2001.
  18. // http://www.uni-giessen.de/www-Numerische-Mathematik/
  19. // davydov/VIS2001.ps.gz
  20. // http://www.math.uni-mannheim.de/~lsmath4/paper/
  21. // VIS2001.pdf.gz
  22. //
  23. // Revisions: 09/04/2003 PS: Modified points_read() to read from a
  24. // file specified by name, not by handle.
  25. //
  26. //--------------------------------------------------------------------------
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <stdarg.h>
  30. #include <limits.h>
  31. #include <float.h>
  32. #include <math.h>
  33. #include <assert.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include "version.h"
  37. #include "nan.h"
  38. #include "csa.h"
  39. int csa_verbose = 0;
  40. #define NPASTART 5 // Number of Points Allocated at Start
  41. #define SVD_NMAX 30 // Maximal number of iterations in svd()
  42. // default algorithm parameters
  43. #define NPMIN_DEF 3
  44. #define NPMAX_DEF 40
  45. #define K_DEF 140
  46. #define NPPC_DEF 5
  47. struct square;
  48. typedef struct square square;
  49. typedef struct
  50. {
  51. square * parent;
  52. int index; // index within parent square; 0 <= index <=
  53. // 3
  54. point vertices[3];
  55. point middle; // barycenter
  56. double h; // parent square edge length
  57. double r; // data visibility radius
  58. //
  59. // points used -- in primary triangles only
  60. //
  61. int nallocated;
  62. int npoints;
  63. point** points;
  64. int primary; // flag -- whether calculate spline
  65. // coefficients directly (by least squares
  66. // method) (primary = 1) or indirectly (from
  67. // C1 smoothness conditions) (primary = 0)
  68. int hascoeffs; // flag -- whether there are no NaNs among
  69. // the spline coefficients
  70. int order; // spline order -- for primary triangles only
  71. //
  72. } triangle;
  73. struct square
  74. {
  75. csa * parent;
  76. int i, j; // indices
  77. int nallocated;
  78. int npoints;
  79. point ** points;
  80. int primary; // flag -- whether this square contains a
  81. // primary triangle
  82. triangle* triangles[4];
  83. double coeffs[25];
  84. };
  85. struct csa
  86. {
  87. double xmin;
  88. double xmax;
  89. double ymin;
  90. double ymax;
  91. int nallocated;
  92. int npoints;
  93. point ** points;
  94. //
  95. // squarization
  96. //
  97. int ni;
  98. int nj;
  99. double h;
  100. square *** squares; // square* [j][i]
  101. int npt; // Number of Primary Triangles
  102. triangle** pt; // Primary Triangles -- triangle* [npt]
  103. //
  104. // algorithm parameters
  105. //
  106. int npmin; // minimal number of points locally involved
  107. // in * spline calculation (normally = 3)
  108. int npmax; // maximal number of points locally involved
  109. // in * spline calculation (required > 10, *
  110. // recommended 20 < npmax < 60)
  111. double k; // relative tolerance multiple in fitting
  112. // spline coefficients: the higher this
  113. // value, the higher degree of the locally
  114. // fitted spline (recommended 80 < k < 200)
  115. int nppc; // average number of points per square
  116. };
  117. static void csa_quit( char* format, ... )
  118. {
  119. va_list args;
  120. fflush( stdout ); // just in case -- to have the exit message
  121. // last
  122. fprintf( stderr, "error: csa: " );
  123. va_start( args, format );
  124. vfprintf( stderr, format, args );
  125. va_end( args );
  126. exit( 1 );
  127. }
  128. // Allocates n1xn2 matrix of something. Note that it will be accessed as
  129. // [n2][n1].
  130. // @param n1 Number of columns
  131. // @param n2 Number of rows
  132. // @return Matrix
  133. //
  134. static void* alloc2d( int n1, int n2, size_t unitsize )
  135. {
  136. unsigned int size;
  137. char * p;
  138. char ** pp;
  139. int i;
  140. assert( n1 > 0 );
  141. assert( n2 > 0 );
  142. assert( (double) n1 * (double) n2 <= (double) UINT_MAX );
  143. size = n1 * n2;
  144. if ( ( p = calloc( size, unitsize ) ) == NULL )
  145. csa_quit( "alloc2d(): %s\n", strerror( errno ) );
  146. assert( (double) n2 * (double) sizeof ( void* ) <= (double) UINT_MAX );
  147. size = n2 * sizeof ( void* );
  148. if ( ( pp = malloc( size ) ) == NULL )
  149. csa_quit( "alloc2d(): %s\n", strerror( errno ) );
  150. for ( i = 0; i < n2; i++ )
  151. pp[i] = &p[i * n1 * unitsize];
  152. return pp;
  153. }
  154. // Destroys n1xn2 matrix.
  155. // @param pp Matrix
  156. //
  157. static void free2d( void* pp )
  158. {
  159. void* p;
  160. assert( pp != NULL );
  161. p = ( (void **) pp )[0];
  162. free( pp );
  163. assert( p != NULL );
  164. free( p );
  165. }
  166. static triangle* triangle_create( square* s, point vertices[], int index )
  167. {
  168. triangle* t = malloc( sizeof ( triangle ) );
  169. t->parent = s;
  170. memcpy( t->vertices, vertices, sizeof ( point ) * 3 );
  171. t->middle.x = ( vertices[0].x + vertices[1].x + vertices[2].x ) / 3.0;
  172. t->middle.y = ( vertices[0].y + vertices[1].y + vertices[2].y ) / 3.0;
  173. t->h = s->parent->h;
  174. t->index = index;
  175. t->r = 0.0;
  176. t->points = 0;
  177. t->nallocated = 0;
  178. t->npoints = 0;
  179. t->hascoeffs = 0;
  180. t->primary = 0;
  181. t->order = -1;
  182. return t;
  183. }
  184. static void triangle_addpoint( triangle* t, point* p )
  185. {
  186. if ( t->nallocated == t->npoints )
  187. {
  188. if ( t->nallocated == 0 )
  189. {
  190. t->points = malloc( NPASTART * sizeof ( point* ) );
  191. t->nallocated = NPASTART;
  192. }
  193. else
  194. {
  195. t->points = realloc( t->points, t->nallocated * 2 * sizeof ( point* ) );
  196. t->nallocated *= 2;
  197. }
  198. }
  199. t->points[t->npoints] = p;
  200. t->npoints++;
  201. }
  202. static void triangle_destroy( triangle* t )
  203. {
  204. if ( t->points != NULL )
  205. free( t->points );
  206. free( t );
  207. }
  208. // Calculates barycentric coordinates of a point.
  209. // Takes into account that possible triangles are rectangular, with the right
  210. // angle at t->vertices[0], the vertices[1] vertex being in
  211. // (-3*PI/4) + (PI/2) * t->index direction from vertices[0], and
  212. // vertices[2] being at (-5*PI/4) + (PI/2) * t->index.
  213. //
  214. static void triangle_calculatebc( triangle* t, point* p, double bc[] )
  215. {
  216. double dx = p->x - t->vertices[0].x;
  217. double dy = p->y - t->vertices[0].y;
  218. if ( t->index == 0 )
  219. {
  220. bc[1] = ( dy - dx ) / t->h;
  221. bc[2] = -( dx + dy ) / t->h;
  222. }
  223. else if ( t->index == 1 )
  224. {
  225. bc[1] = ( dx + dy ) / t->h;
  226. bc[2] = ( dy - dx ) / t->h;
  227. }
  228. else if ( t->index == 2 )
  229. {
  230. bc[1] = ( dx - dy ) / t->h;
  231. bc[2] = ( dx + dy ) / t->h;
  232. }
  233. else
  234. {
  235. bc[1] = -( dx + dy ) / t->h;
  236. bc[2] = ( dx - dy ) / t->h;
  237. }
  238. bc[0] = 1.0 - bc[1] - bc[2];
  239. }
  240. static square* square_create( csa* parent, double xmin, double ymin, int i, int j )
  241. {
  242. int ii;
  243. square * s = malloc( sizeof ( square ) );
  244. double h = parent->h;
  245. s->parent = parent;
  246. s->i = i;
  247. s->j = j;
  248. s->points = NULL;
  249. s->nallocated = 0;
  250. s->npoints = 0;
  251. s->primary = 0;
  252. //
  253. // create 4 triangles formed by diagonals
  254. //
  255. for ( ii = 0; ii < 4; ++ii )
  256. {
  257. point vertices[3];
  258. vertices[0].x = xmin + h / 2.0;
  259. vertices[0].y = ymin + h / 2.0;
  260. vertices[1].x = xmin + h * ( ( ( ii + 1 ) % 4 ) / 2 ); // 0 1 1 0
  261. vertices[1].y = ymin + h * ( ( ( ii + 2 ) % 4 ) / 2 ); // 1 1 0 0
  262. vertices[2].x = xmin + h * ( ii / 2 ); // 0 0 1 1
  263. vertices[2].y = ymin + h * ( ( ( ii + 1 ) % 4 ) / 2 ); // 0 1 1 0
  264. s->triangles[ii] = triangle_create( s, vertices, ii );
  265. }
  266. for ( ii = 0; ii < 25; ++ii )
  267. s->coeffs[ii] = NaN;
  268. return s;
  269. }
  270. static void square_destroy( square* s )
  271. {
  272. int i;
  273. for ( i = 0; i < 4; ++i )
  274. triangle_destroy( s->triangles[i] );
  275. if ( s->points != NULL )
  276. free( s->points );
  277. free( s );
  278. }
  279. static void square_addpoint( square* s, point* p )
  280. {
  281. if ( s->nallocated == s->npoints )
  282. {
  283. if ( s->nallocated == 0 )
  284. {
  285. s->points = malloc( NPASTART * sizeof ( point* ) );
  286. s->nallocated = NPASTART;
  287. }
  288. else
  289. {
  290. s->points = realloc( s->points, s->nallocated * 2 * sizeof ( point* ) );
  291. s->nallocated *= 2;
  292. }
  293. }
  294. s->points[s->npoints] = p;
  295. s->npoints++;
  296. }
  297. csa* csa_create()
  298. {
  299. csa* a = malloc( sizeof ( csa ) );
  300. a->xmin = DBL_MAX;
  301. a->xmax = -DBL_MAX;
  302. a->ymin = DBL_MAX;
  303. a->ymax = -DBL_MAX;
  304. a->points = malloc( NPASTART * sizeof ( point* ) );
  305. a->nallocated = NPASTART;
  306. a->npoints = 0;
  307. a->ni = 0;
  308. a->nj = 0;
  309. a->h = NaN;
  310. a->squares = NULL;
  311. a->npt = 0;
  312. a->pt = NULL;
  313. a->npmin = NPMIN_DEF;
  314. a->npmax = NPMAX_DEF;
  315. a->k = K_DEF;
  316. a->nppc = NPPC_DEF;
  317. return a;
  318. }
  319. void csa_destroy( csa* a )
  320. {
  321. int i, j;
  322. if ( a->squares != NULL )
  323. {
  324. for ( j = 0; j < a->nj; ++j )
  325. for ( i = 0; i < a->ni; ++i )
  326. square_destroy( a->squares[j][i] );
  327. free2d( a->squares );
  328. }
  329. if ( a->pt != NULL )
  330. free( a->pt );
  331. if ( a->points != NULL )
  332. free( a->points );
  333. free( a );
  334. }
  335. void csa_addpoints( csa* a, int n, point points[] )
  336. {
  337. int na = a->nallocated;
  338. int i;
  339. assert( a->squares == NULL );
  340. //
  341. // (can be called prior to squarization only)
  342. //
  343. while ( na < a->npoints + n )
  344. na *= 2;
  345. if ( na != a->nallocated )
  346. {
  347. a->points = realloc( a->points, na * sizeof ( point* ) );
  348. a->nallocated = na;
  349. }
  350. for ( i = 0; i < n; ++i )
  351. {
  352. point* p = &points[i];
  353. a->points[a->npoints] = p;
  354. a->npoints++;
  355. if ( p->x < a->xmin )
  356. a->xmin = p->x;
  357. if ( p->x > a->xmax )
  358. a->xmax = p->x;
  359. if ( p->y < a->ymin )
  360. a->ymin = p->y;
  361. if ( p->y > a->ymax )
  362. a->ymax = p->y;
  363. }
  364. }
  365. // Marks the squares containing "primary" triangles by setting "primary" flag
  366. // to 1.
  367. //
  368. static void csa_setprimaryflag( csa* a )
  369. {
  370. square*** squares = a->squares;
  371. int nj1 = a->nj - 1;
  372. int ni1 = a->ni - 1;
  373. int i, j;
  374. for ( j = 1; j < nj1; ++j )
  375. {
  376. for ( i = 1; i < ni1; ++i )
  377. {
  378. if ( squares[j][i]->npoints > 0 )
  379. {
  380. if ( ( i + j ) % 2 == 0 )
  381. {
  382. squares[j][i]->primary = 1;
  383. squares[j - 1][i - 1]->primary = 1;
  384. squares[j + 1][i - 1]->primary = 1;
  385. squares[j - 1][i + 1]->primary = 1;
  386. squares[j + 1][i + 1]->primary = 1;
  387. }
  388. else
  389. {
  390. squares[j - 1][i]->primary = 1;
  391. squares[j + 1][i]->primary = 1;
  392. squares[j][i - 1]->primary = 1;
  393. squares[j][i + 1]->primary = 1;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. // Splits the data domain in a number of squares.
  400. //
  401. static void csa_squarize( csa* a )
  402. {
  403. int nps[7] = { 0, 0, 0, 0, 0, 0 }; // stats on number of points per
  404. // square
  405. double dx = a->xmax - a->xmin;
  406. double dy = a->ymax - a->ymin;
  407. int npoints = a->npoints;
  408. double h;
  409. int i, j, ii, nadj;
  410. if ( csa_verbose )
  411. {
  412. fprintf( stderr, "squarizing csa:\n" );
  413. fflush( stderr );
  414. }
  415. assert( a->squares == NULL );
  416. //
  417. // (can be done only once)
  418. //
  419. h = sqrt( dx * dy * a->nppc / npoints ); // square edge size
  420. if ( dx < h )
  421. h = dy * a->nppc / npoints;
  422. if ( dy < h )
  423. h = dx * a->nppc / npoints;
  424. a->h = h;
  425. a->ni = (int) ( ceil( dx / h ) + 2 );
  426. a->nj = (int) ( ceil( dy / h ) + 2 );
  427. if ( csa_verbose )
  428. {
  429. fprintf( stderr, " %d x %d squares\n", a->ni, a->nj );
  430. fflush( stderr );
  431. }
  432. //
  433. // create squares
  434. //
  435. a->squares = alloc2d( a->ni, a->nj, sizeof ( void* ) );
  436. for ( j = 0; j < a->nj; ++j )
  437. for ( i = 0; i < a->ni; ++i )
  438. a->squares[j][i] = square_create( a, a->xmin + h * ( i - 1 ), a->ymin + h * ( j - 1 ), i, j );
  439. //
  440. // map points to squares
  441. //
  442. for ( ii = 0; ii < npoints; ++ii )
  443. {
  444. point* p = a->points[ii];
  445. i = (int) ( floor( ( p->x - a->xmin ) / h ) + 1 );
  446. j = (int) ( floor( ( p->y - a->ymin ) / h ) + 1 );
  447. square_addpoint( a->squares[j][i], p );
  448. }
  449. //
  450. // mark relevant squares with no points
  451. //
  452. csa_setprimaryflag( a );
  453. //
  454. // Create a list of "primary" triangles, for which spline coefficients
  455. // will be calculated directy (by least squares method), without using
  456. // C1 smoothness conditions.
  457. //
  458. a->pt = malloc( ( a->ni / 2 + 1 ) * a->nj * sizeof ( triangle* ) );
  459. for ( j = 0, ii = 0, nadj = 0; j < a->nj; ++j )
  460. {
  461. for ( i = 0; i < a->ni; ++i )
  462. {
  463. square* s = a->squares[j][i];
  464. if ( s->npoints > 0 )
  465. {
  466. int nn = s->npoints / 5;
  467. if ( nn > 6 )
  468. nn = 6;
  469. nps[nn]++;
  470. ii++;
  471. }
  472. if ( s->primary && s->npoints == 0 )
  473. nadj++;
  474. if ( s->primary )
  475. {
  476. a->pt[a->npt] = s->triangles[0];
  477. s->triangles[0]->primary = 1;
  478. a->npt++;
  479. }
  480. }
  481. }
  482. if ( csa_verbose )
  483. {
  484. fprintf( stderr, " %d non-empty squares\n", ii );
  485. fprintf( stderr, " %d primary squares\n", a->npt );
  486. fprintf( stderr, " %d primary squares with no data\n", nadj );
  487. fprintf( stderr, " %.2f points per square \n", (double) a->npoints / ii );
  488. }
  489. if ( csa_verbose == 2 )
  490. {
  491. for ( i = 0; i < 6; ++i )
  492. fprintf( stderr, " %d-%d points -- %d squares\n", i * 5, i * 5 + 4, nps[i] );
  493. fprintf( stderr, " %d or more points -- %d squares\n", i * 5, nps[i] );
  494. }
  495. if ( csa_verbose == 2 )
  496. {
  497. fprintf( stderr, " j\\i" );
  498. for ( i = 0; i < a->ni; ++i )
  499. fprintf( stderr, "%3d ", i );
  500. fprintf( stderr, "\n" );
  501. for ( j = a->nj - 1; j >= 0; --j )
  502. {
  503. fprintf( stderr, "%3d ", j );
  504. for ( i = 0; i < a->ni; ++i )
  505. {
  506. square* s = a->squares[j][i];
  507. if ( s->npoints > 0 )
  508. fprintf( stderr, "%3d ", s->npoints );
  509. else
  510. fprintf( stderr, " . " );
  511. }
  512. fprintf( stderr, "\n" );
  513. }
  514. }
  515. if ( csa_verbose )
  516. fflush( stderr );
  517. }
  518. // Returns all squares intersecting with a square with center in t->middle
  519. // and edges of length 2*t->r parallel to X and Y axes.
  520. //
  521. static void getsquares( csa* a, triangle* t, int* n, square*** squares )
  522. {
  523. int imin = (int) floor( ( t->middle.x - t->r - a->xmin ) / t->h );
  524. int imax = (int) ceil( ( t->middle.x + t->r - a->xmin ) / t->h );
  525. int jmin = (int) floor( ( t->middle.y - t->r - a->ymin ) / t->h );
  526. int jmax = (int) ceil( ( t->middle.y + t->r - a->ymin ) / t->h );
  527. int i, j;
  528. if ( imin < 0 )
  529. imin = 0;
  530. if ( imax >= a->ni )
  531. imax = a->ni - 1;
  532. if ( jmin < 0 )
  533. jmin = 0;
  534. if ( jmax >= a->nj )
  535. jmax = a->nj - 1;
  536. *n = 0;
  537. ( *squares ) = malloc( ( imax - imin + 1 ) * ( jmax - jmin + 1 ) * sizeof ( square* ) );
  538. for ( j = jmin; j <= jmax; ++j )
  539. {
  540. for ( i = imin; i <= imax; ++i )
  541. {
  542. square* s = a->squares[j][i];
  543. if ( s->npoints > 0 )
  544. {
  545. ( *squares )[*n] = a->squares[j][i];
  546. ( *n )++;
  547. }
  548. }
  549. }
  550. }
  551. static double distance( point* p1, point* p2 )
  552. {
  553. return hypot( p1->x - p2->x, p1->y - p2->y );
  554. }
  555. // Thins data by creating an auxiliary regular grid and for leaving only
  556. // the most central point within each grid cell.
  557. // (I follow the paper here. It is possible that taking average -- in terms of
  558. // both value and position -- of all points within a cell would be a bit more
  559. // robust. However, because of keeping only shallow copies of input points,
  560. // this would require quite a bit of structural changes. So, leaving it as is
  561. // for now.)
  562. //
  563. static void thindata( triangle* t, int npmax )
  564. {
  565. csa * a = t->parent->parent;
  566. int imax = (int) ceil( sqrt( (double) ( npmax * 3 / 2 ) ) );
  567. square *** squares = alloc2d( imax, imax, sizeof ( void* ) );
  568. double h = t->r * 2.0 / imax;
  569. double h2 = h / 2.0;
  570. double xmin = t->middle.x - t->r;
  571. double ymin = t->middle.y - t->r;
  572. int i, j, ii;
  573. for ( j = 0; j < imax; ++j )
  574. for ( i = 0; i < imax; ++i )
  575. squares[j][i] = square_create( a, xmin + h * i, ymin + h * j, i, j );
  576. for ( ii = 0; ii < t->npoints; ++ii )
  577. {
  578. point * p = t->points[ii];
  579. int i = (int) floor( ( p->x - xmin ) / h );
  580. int j = (int) floor( ( p->y - ymin ) / h );
  581. square* s = squares[j][i];
  582. if ( s->npoints == 0 )
  583. square_addpoint( s, p );
  584. else // npoints == 1
  585. {
  586. point pmiddle;
  587. pmiddle.x = xmin + h * i + h2;
  588. pmiddle.y = ymin + h * j + h2;
  589. if ( distance( s->points[0], &pmiddle ) > distance( p, &pmiddle ) )
  590. s->points[0] = p;
  591. }
  592. }
  593. t->npoints = 0;
  594. for ( j = 0; j < imax; ++j )
  595. {
  596. for ( i = 0; i < imax; ++i )
  597. {
  598. square* s = squares[j][i];
  599. if ( squares[j][i]->npoints != 0 )
  600. triangle_addpoint( t, s->points[0] );
  601. square_destroy( s );
  602. }
  603. }
  604. free2d( squares );
  605. imax++;
  606. }
  607. // Finds data points to be used in calculating spline coefficients for each
  608. // primary triangle.
  609. //
  610. static void csa_attachpoints( csa* a )
  611. {
  612. int npmin = a->npmin;
  613. int npmax = a->npmax;
  614. int nincreased = 0;
  615. int nthinned = 0;
  616. int i;
  617. assert( a->npt > 0 );
  618. if ( csa_verbose )
  619. {
  620. fprintf( stderr, "pre-processing data points:\n " );
  621. fflush( stderr );
  622. }
  623. for ( i = 0; i < a->npt; ++i )
  624. {
  625. triangle* t = a->pt[i];
  626. int increased = 0;
  627. if ( csa_verbose )
  628. {
  629. fprintf( stderr, "." );
  630. fflush( stderr );
  631. }
  632. t->r = t->h * 1.25;
  633. while ( 1 )
  634. {
  635. int nsquares = 0;
  636. square** squares = NULL;
  637. int ii;
  638. getsquares( a, t, &nsquares, &squares );
  639. for ( ii = 0; ii < nsquares; ++ii )
  640. {
  641. square* s = squares[ii];
  642. int iii;
  643. for ( iii = 0; iii < s->npoints; ++iii )
  644. {
  645. point* p = s->points[iii];
  646. if ( distance( p, &t->middle ) <= t->r )
  647. triangle_addpoint( t, p );
  648. }
  649. }
  650. free( squares );
  651. if ( t->npoints < npmin )
  652. {
  653. if ( !increased )
  654. {
  655. increased = 1;
  656. nincreased++;
  657. }
  658. t->r *= 1.25;
  659. t->npoints = 0;
  660. }
  661. else if ( t->npoints > npmax )
  662. {
  663. nthinned++;
  664. thindata( t, npmax );
  665. if ( t->npoints > npmin )
  666. break;
  667. else
  668. {
  669. //
  670. // Sometimes you have too much data, you thin it and --
  671. // oops -- you have too little. This is not a frequent
  672. // event, so let us not bother to put a new subdivision.
  673. //
  674. t->r *= 1.25;
  675. t->npoints = 0;
  676. }
  677. }
  678. else
  679. break;
  680. }
  681. }
  682. if ( csa_verbose )
  683. {
  684. fprintf( stderr, "\n %d sets enhanced, %d sets thinned\n", nincreased, nthinned );
  685. fflush( stderr );
  686. }
  687. }
  688. static int n2q( int n )
  689. {
  690. assert( n >= 3 );
  691. if ( n >= 10 )
  692. return 3;
  693. else if ( n >= 6 )
  694. return 2;
  695. else // n == 3
  696. return 1;
  697. }
  698. //* Singular value decomposition.
  699. // Borrowed from EISPACK (1972-1973).
  700. // Presents input matrix A as A = U.W.V'.
  701. //
  702. // @param a Input matrix A = U.W[0..m-1][0..n-1]; output matrix U
  703. // @param n Number of columns
  704. // @param m Number of rows
  705. // @param w Ouput vector that presents diagonal matrix W
  706. // @param V output matrix V
  707. //
  708. static void svd( double** a, int n, int m, double* w, double** v )
  709. {
  710. double * rv1;
  711. int i, j, k, l = -1;
  712. double tst1, c, f, g, h, s, scale;
  713. assert( m > 0 && n > 0 );
  714. rv1 = malloc( n * sizeof ( double ) );
  715. //
  716. // householder reduction to bidiagonal form
  717. //
  718. g = 0.0;
  719. scale = 0.0;
  720. tst1 = 0.0;
  721. for ( i = 0; i < n; i++ )
  722. {
  723. l = i + 1;
  724. rv1[i] = scale * g;
  725. g = 0.0;
  726. s = 0.0;
  727. scale = 0.0;
  728. if ( i < m )
  729. {
  730. for ( k = i; k < m; k++ )
  731. scale += fabs( a[k][i] );
  732. if ( scale != 0.0 )
  733. {
  734. for ( k = i; k < m; k++ )
  735. {
  736. a[k][i] /= scale;
  737. s += a[k][i] * a[k][i];
  738. }
  739. f = a[i][i];
  740. g = -copysign( sqrt( s ), f );
  741. h = f * g - s;
  742. a[i][i] = f - g;
  743. if ( i < n - 1 )
  744. {
  745. for ( j = l; j < n; j++ )
  746. {
  747. s = 0.0;
  748. for ( k = i; k < m; k++ )
  749. s += a[k][i] * a[k][j];
  750. f = s / h;
  751. for ( k = i; k < m; k++ )
  752. a[k][j] += f * a[k][i];
  753. }
  754. }
  755. for ( k = i; k < m; k++ )
  756. a[k][i] *= scale;
  757. }
  758. }
  759. w[i] = scale * g;
  760. g = 0.0;
  761. s = 0.0;
  762. scale = 0.0;
  763. if ( i < m && i < n - 1 )
  764. {
  765. for ( k = l; k < n; k++ )
  766. scale += fabs( a[i][k] );
  767. if ( scale != 0.0 )
  768. {
  769. for ( k = l; k < n; k++ )
  770. {
  771. a[i][k] /= scale;
  772. s += a[i][k] * a[i][k];
  773. }
  774. f = a[i][l];
  775. g = -copysign( sqrt( s ), f );
  776. h = f * g - s;
  777. a[i][l] = f - g;
  778. for ( k = l; k < n; k++ )
  779. rv1[k] = a[i][k] / h;
  780. for ( j = l; j < m; j++ )
  781. {
  782. s = 0.0;
  783. for ( k = l; k < n; k++ )
  784. s += a[j][k] * a[i][k];
  785. for ( k = l; k < n; k++ )
  786. a[j][k] += s * rv1[k];
  787. }
  788. for ( k = l; k < n; k++ )
  789. a[i][k] *= scale;
  790. }
  791. }
  792. {
  793. double tmp = fabs( w[i] ) + fabs( rv1[i] );
  794. tst1 = ( tst1 > tmp ) ? tst1 : tmp;
  795. }
  796. }
  797. //
  798. // accumulation of right-hand transformations
  799. //
  800. for ( i = n - 1; i >= 0; i-- )
  801. {
  802. if ( i < n - 1 )
  803. {
  804. if ( g != 0.0 )
  805. {
  806. for ( j = l; j < n; j++ )
  807. //
  808. // double division avoids possible underflow
  809. //
  810. v[j][i] = ( a[i][j] / a[i][l] ) / g;
  811. for ( j = l; j < n; j++ )
  812. {
  813. s = 0.0;
  814. for ( k = l; k < n; k++ )
  815. s += a[i][k] * v[k][j];
  816. for ( k = l; k < n; k++ )
  817. v[k][j] += s * v[k][i];
  818. }
  819. }
  820. for ( j = l; j < n; j++ )
  821. {
  822. v[i][j] = 0.0;
  823. v[j][i] = 0.0;
  824. }
  825. }
  826. v[i][i] = 1.0;
  827. g = rv1[i];
  828. l = i;
  829. }
  830. //
  831. // accumulation of left-hand transformations
  832. //
  833. for ( i = ( m < n ) ? m - 1 : n - 1; i >= 0; i-- )
  834. {
  835. l = i + 1;
  836. g = w[i];
  837. if ( i != n - 1 )
  838. for ( j = l; j < n; j++ )
  839. a[i][j] = 0.0;
  840. if ( g != 0.0 )
  841. {
  842. for ( j = l; j < n; j++ )
  843. {
  844. s = 0.0;
  845. for ( k = l; k < m; k++ )
  846. s += a[k][i] * a[k][j];
  847. //
  848. // double division avoids possible underflow
  849. //
  850. f = ( s / a[i][i] ) / g;
  851. for ( k = i; k < m; k++ )
  852. a[k][j] += f * a[k][i];
  853. }
  854. for ( j = i; j < m; j++ )
  855. a[j][i] /= g;
  856. }
  857. else
  858. for ( j = i; j < m; j++ )
  859. a[j][i] = 0.0;
  860. a[i][i] += 1.0;
  861. }
  862. //
  863. // diagonalization of the bidiagonal form
  864. //
  865. for ( k = n - 1; k >= 0; k-- )
  866. {
  867. int k1 = k - 1;
  868. int its = 0;
  869. while ( 1 )
  870. {
  871. int docancellation = 1;
  872. double x, y, z;
  873. int l1 = -1;
  874. its++;
  875. if ( its > SVD_NMAX )
  876. csa_quit( "svd(): no convergence in %d iterations", SVD_NMAX );
  877. for ( l = k; l >= 0; l-- ) // test for splitting
  878. {
  879. double tst2 = fabs( rv1[l] ) + tst1;
  880. if ( tst2 == tst1 )
  881. {
  882. docancellation = 0;
  883. break;
  884. }
  885. l1 = l - 1;
  886. //
  887. // rv1(1) is always zero, so there is no exit through the
  888. // bottom of the loop
  889. //
  890. tst2 = fabs( w[l - 1] ) + tst1;
  891. if ( tst2 == tst1 )
  892. break;
  893. }
  894. //
  895. // cancellation of rv1[l] if l > 1
  896. //
  897. if ( docancellation )
  898. {
  899. c = 0.0;
  900. s = 1.0;
  901. for ( i = l; i <= k; i++ )
  902. {
  903. f = s * rv1[i];
  904. rv1[i] = c * rv1[i];
  905. if ( ( fabs( f ) + tst1 ) == tst1 )
  906. break;
  907. g = w[i];
  908. h = hypot( f, g );
  909. w[i] = h;
  910. h = 1.0 / h;
  911. c = g * h;
  912. s = -f * h;
  913. for ( j = 0; j < m; j++ )
  914. {
  915. double y = a[j][l1];
  916. double z = a[j][i];
  917. a[j][l1] = y * c + z * s;
  918. a[j][i] = z * c - y * s;
  919. }
  920. }
  921. }
  922. //
  923. // test for convergence
  924. //
  925. z = w[k];
  926. if ( l != k )
  927. {
  928. int i1;
  929. //
  930. // shift from bottom 2 by 2 minor
  931. //
  932. x = w[l];
  933. y = w[k1];
  934. g = rv1[k1];
  935. h = rv1[k];
  936. f = 0.5 * ( ( ( g + z ) / h ) * ( ( g - z ) / y ) + y / h - h / y );
  937. g = hypot( f, 1.0 );
  938. f = x - ( z / x ) * z + ( h / x ) * ( y / ( f + copysign( g, f ) ) - h );
  939. //
  940. // next qr transformation
  941. //
  942. c = 1.0;
  943. s = 1.0;
  944. for ( i1 = l; i1 < k; i1++ )
  945. {
  946. i = i1 + 1;
  947. g = rv1[i];
  948. y = w[i];
  949. h = s * g;
  950. g = c * g;
  951. z = hypot( f, h );
  952. rv1[i1] = z;
  953. c = f / z;
  954. s = h / z;
  955. f = x * c + g * s;
  956. g = g * c - x * s;
  957. h = y * s;
  958. y *= c;
  959. for ( j = 0; j < n; j++ )
  960. {
  961. x = v[j][i1];
  962. z = v[j][i];
  963. v[j][i1] = x * c + z * s;
  964. v[j][i] = z * c - x * s;
  965. }
  966. z = hypot( f, h );
  967. w[i1] = z;
  968. //
  969. // rotation can be arbitrary if z = 0
  970. //
  971. if ( z != 0.0 )
  972. {
  973. c = f / z;
  974. s = h / z;
  975. }
  976. f = c * g + s * y;
  977. x = c * y - s * g;
  978. for ( j = 0; j < m; j++ )
  979. {
  980. y = a[j][i1];
  981. z = a[j][i];
  982. a[j][i1] = y * c + z * s;
  983. a[j][i] = z * c - y * s;
  984. }
  985. }
  986. rv1[l] = 0.0;
  987. rv1[k] = f;
  988. w[k] = x;
  989. }
  990. else
  991. {
  992. //
  993. // w[k] is made non-negative
  994. //
  995. if ( z < 0.0 )
  996. {
  997. w[k] = -z;
  998. for ( j = 0; j < n; j++ )
  999. v[j][k] = -v[j][k];
  1000. }
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. free( rv1 );
  1006. }
  1007. // Least squares fitting via singular value decomposition.
  1008. //
  1009. static void lsq( double** A, int ni, int nj, double* z, double* w, double* sol )
  1010. {
  1011. double** V = alloc2d( ni, ni, sizeof ( double ) );
  1012. double** B = alloc2d( nj, ni, sizeof ( double ) );
  1013. int i, j, ii;
  1014. svd( A, ni, nj, w, V );
  1015. for ( j = 0; j < ni; ++j )
  1016. for ( i = 0; i < ni; ++i )
  1017. V[j][i] /= w[i];
  1018. for ( i = 0; i < ni; ++i )
  1019. {
  1020. double* v = V[i];
  1021. for ( j = 0; j < nj; ++j )
  1022. {
  1023. double* a = A[j];
  1024. double* b = &B[i][j];
  1025. for ( ii = 0; ii < ni; ++ii )
  1026. *b += v[ii] * a[ii];
  1027. }
  1028. }
  1029. for ( i = 0; i < ni; ++i )
  1030. sol[i] = 0.0;
  1031. for ( i = 0; i < ni; ++i )
  1032. for ( j = 0; j < nj; ++j )
  1033. sol[i] += B[i][j] * z[j];
  1034. free2d( B );
  1035. free2d( V );
  1036. }
  1037. //
  1038. // square->coeffs[]:
  1039. //
  1040. // ---------------------
  1041. // | 3 10 17 24 |
  1042. // | 6 13 20 |
  1043. // | 2 9 16 23 |
  1044. // | 5 12 19 |
  1045. // | 1 8 15 22 |
  1046. // | 4 11 18 |
  1047. // | 0 7 14 21 |
  1048. // ---------------------
  1049. //
  1050. // Calculates spline coefficients in each primary triangle by least squares
  1051. // fitting to data attached by csa_attachpoints().
  1052. //
  1053. static void csa_findprimarycoeffs( csa* a )
  1054. {
  1055. int n[4] = { 0, 0, 0, 0 };
  1056. int i;
  1057. if ( csa_verbose )
  1058. fprintf( stderr, "calculating spline coefficients for primary triangles:\n " );
  1059. for ( i = 0; i < a->npt; ++i )
  1060. {
  1061. triangle* t = a->pt[i];
  1062. int npoints = t->npoints;
  1063. point ** points = t->points;
  1064. double * z = malloc( npoints * sizeof ( double ) );
  1065. int q = n2q( t->npoints );
  1066. int ok = 1;
  1067. double b[10];
  1068. double b1[6];
  1069. int ii;
  1070. if ( csa_verbose )
  1071. {
  1072. fprintf( stderr, "." );
  1073. fflush( stderr );
  1074. }
  1075. for ( ii = 0; ii < npoints; ++ii )
  1076. z[ii] = points[ii]->z;
  1077. do
  1078. {
  1079. double bc[3];
  1080. double wmin, wmax;
  1081. if ( !ok )
  1082. q--;
  1083. assert( q >= 0 );
  1084. if ( q == 3 )
  1085. {
  1086. double ** A = alloc2d( 10, npoints, sizeof ( double ) );
  1087. double w[10];
  1088. for ( ii = 0; ii < npoints; ++ii )
  1089. {
  1090. double * aii = A[ii];
  1091. double tmp;
  1092. triangle_calculatebc( t, points[ii], bc );
  1093. //
  1094. // 0 1 2 3 4 5 6 7 8 9
  1095. // 300 210 201 120 111 102 030 021 012 003
  1096. //
  1097. tmp = bc[0] * bc[0];
  1098. aii[0] = tmp * bc[0];
  1099. tmp *= 3.0;
  1100. aii[1] = tmp * bc[1];
  1101. aii[2] = tmp * bc[2];
  1102. tmp = bc[1] * bc[1];
  1103. aii[6] = tmp * bc[1];
  1104. tmp *= 3.0;
  1105. aii[3] = tmp * bc[0];
  1106. aii[7] = tmp * bc[2];
  1107. tmp = bc[2] * bc[2];
  1108. aii[9] = tmp * bc[2];
  1109. tmp *= 3.0;
  1110. aii[5] = tmp * bc[0];
  1111. aii[8] = tmp * bc[1];
  1112. aii[4] = bc[0] * bc[1] * bc[2] * 6.0;
  1113. }
  1114. lsq( A, 10, npoints, z, w, b );
  1115. wmin = w[0];
  1116. wmax = w[0];
  1117. for ( ii = 1; ii < 10; ++ii )
  1118. {
  1119. if ( w[ii] < wmin )
  1120. wmin = w[ii];
  1121. else if ( w[ii] > wmax )
  1122. wmax = w[ii];
  1123. }
  1124. if ( wmin < wmax / a->k )
  1125. ok = 0;
  1126. free2d( A );
  1127. }
  1128. else if ( q == 2 )
  1129. {
  1130. double ** A = alloc2d( 6, npoints, sizeof ( double ) );
  1131. double w[6];
  1132. for ( ii = 0; ii < npoints; ++ii )
  1133. {
  1134. double* aii = A[ii];
  1135. triangle_calculatebc( t, points[ii], bc );
  1136. //
  1137. // 0 1 2 3 4 5
  1138. // 200 110 101 020 011 002
  1139. //
  1140. aii[0] = bc[0] * bc[0];
  1141. aii[1] = bc[0] * bc[1] * 2.0;
  1142. aii[2] = bc[0] * bc[2] * 2.0;
  1143. aii[3] = bc[1] * bc[1];
  1144. aii[4] = bc[1] * bc[2] * 2.0;
  1145. aii[5] = bc[2] * bc[2];
  1146. }
  1147. lsq( A, 6, npoints, z, w, b1 );
  1148. wmin = w[0];
  1149. wmax = w[0];
  1150. for ( ii = 1; ii < 6; ++ii )
  1151. {
  1152. if ( w[ii] < wmin )
  1153. wmin = w[ii];
  1154. else if ( w[ii] > wmax )
  1155. wmax = w[ii];
  1156. }
  1157. if ( wmin < wmax / a->k )
  1158. ok = 0;
  1159. else // degree raising
  1160. {
  1161. ok = 1;
  1162. b[0] = b1[0];
  1163. b[1] = ( b1[0] + 2.0 * b1[1] ) / 3.0;
  1164. b[2] = ( b1[0] + 2.0 * b1[2] ) / 3.0;
  1165. b[3] = ( b1[3] + 2.0 * b1[1] ) / 3.0;
  1166. b[4] = ( b1[1] + b1[2] + b1[4] ) / 3.0;
  1167. b[5] = ( b1[5] + 2.0 * b1[2] ) / 3.0;
  1168. b[6] = b1[3];
  1169. b[7] = ( b1[3] + 2.0 * b1[4] ) / 3.0;
  1170. b[8] = ( b1[5] + 2.0 * b1[4] ) / 3.0;
  1171. b[9] = b1[5];
  1172. }
  1173. free2d( A );
  1174. }
  1175. else if ( q == 1 )
  1176. {
  1177. double ** A = alloc2d( 3, npoints, sizeof ( double ) );
  1178. double w[3];
  1179. for ( ii = 0; ii < npoints; ++ii )
  1180. {
  1181. double* aii = A[ii];
  1182. triangle_calculatebc( t, points[ii], bc );
  1183. aii[0] = bc[0];
  1184. aii[1] = bc[1];
  1185. aii[2] = bc[2];
  1186. }
  1187. lsq( A, 3, npoints, z, w, b1 );
  1188. wmin = w[0];
  1189. wmax = w[0];
  1190. for ( ii = 1; ii < 3; ++ii )
  1191. {
  1192. if ( w[ii] < wmin )
  1193. wmin = w[ii];
  1194. else if ( w[ii] > wmax )
  1195. wmax = w[ii];
  1196. }
  1197. if ( wmin < wmax / a->k )
  1198. ok = 0;
  1199. else // degree raising
  1200. {
  1201. ok = 1;
  1202. b[0] = b1[0];
  1203. b[1] = ( 2.0 * b1[0] + b1[1] ) / 3.0;
  1204. b[2] = ( 2.0 * b1[0] + b1[2] ) / 3.0;
  1205. b[3] = ( 2.0 * b1[1] + b1[0] ) / 3.0;
  1206. b[4] = ( b1[0] + b1[1] + b1[2] ) / 3.0;
  1207. b[5] = ( 2.0 * b1[2] + b1[0] ) / 3.0;
  1208. b[6] = b1[1];
  1209. b[7] = ( 2.0 * b1[1] + b1[2] ) / 3.0;
  1210. b[8] = ( 2.0 * b1[2] + b1[1] ) / 3.0;
  1211. b[9] = b1[2];
  1212. }
  1213. free2d( A );
  1214. }
  1215. else if ( q == 0 )
  1216. {
  1217. double ** A = alloc2d( 1, npoints, sizeof ( double ) );
  1218. double w[1];
  1219. for ( ii = 0; ii < npoints; ++ii )
  1220. A[ii][0] = 1.0;
  1221. lsq( A, 1, npoints, z, w, b1 );
  1222. ok = 1;
  1223. b[0] = b1[0];
  1224. b[1] = b1[0];
  1225. b[2] = b1[0];
  1226. b[3] = b1[0];
  1227. b[4] = b1[0];
  1228. b[5] = b1[0];
  1229. b[6] = b1[0];
  1230. b[7] = b1[0];
  1231. b[8] = b1[0];
  1232. b[9] = b1[0];
  1233. free2d( A );
  1234. }
  1235. } while ( !ok );
  1236. n[q]++;
  1237. t->order = q;
  1238. {
  1239. square* s = t->parent;
  1240. double* coeffs = s->coeffs;
  1241. coeffs[12] = b[0];
  1242. coeffs[9] = b[1];
  1243. coeffs[6] = b[3];
  1244. coeffs[3] = b[6];
  1245. coeffs[2] = b[7];
  1246. coeffs[1] = b[8];
  1247. coeffs[0] = b[9];
  1248. coeffs[4] = b[5];
  1249. coeffs[8] = b[2];
  1250. coeffs[5] = b[4];
  1251. }
  1252. free( z );
  1253. }
  1254. if ( csa_verbose )
  1255. {
  1256. fprintf( stderr, "\n 3rd order -- %d sets\n", n[3] );
  1257. fprintf( stderr, " 2nd order -- %d sets\n", n[2] );
  1258. fprintf( stderr, " 1st order -- %d sets\n", n[1] );
  1259. fprintf( stderr, " 0th order -- %d sets\n", n[0] );
  1260. fflush( stderr );
  1261. }
  1262. if ( csa_verbose == 2 )
  1263. {
  1264. int j;
  1265. fprintf( stderr, " j\\i" );
  1266. for ( i = 0; i < a->ni; ++i )
  1267. fprintf( stderr, "%2d ", i );
  1268. fprintf( stderr, "\n" );
  1269. for ( j = a->nj - 1; j >= 0; --j )
  1270. {
  1271. fprintf( stderr, "%2d ", j );
  1272. for ( i = 0; i < a->ni; ++i )
  1273. {
  1274. square* s = a->squares[j][i];
  1275. if ( s->triangles[0]->primary )
  1276. fprintf( stderr, "%2d ", s->triangles[0]->order );
  1277. else
  1278. fprintf( stderr, " . " );
  1279. }
  1280. fprintf( stderr, "\n" );
  1281. }
  1282. }
  1283. }
  1284. // Finds spline coefficients in (adjacent to primary triangles) secondary
  1285. // triangles from C1 smoothness conditions.
  1286. //
  1287. static void csa_findsecondarycoeffs( csa* a )
  1288. {
  1289. square*** squares = a->squares;
  1290. int ni = a->ni;
  1291. int nj = a->nj;
  1292. int ii;
  1293. if ( csa_verbose )
  1294. {
  1295. fprintf( stderr, "propagating spline coefficients to the remaining triangles:\n" );
  1296. fflush( stderr );
  1297. }
  1298. //
  1299. // red
  1300. //
  1301. for ( ii = 0; ii < a->npt; ++ii )
  1302. {
  1303. triangle* t = a->pt[ii];
  1304. square * s = t->parent;
  1305. int i = s->i;
  1306. int j = s->j;
  1307. double * c = s->coeffs;
  1308. double * cl = ( i > 0 ) ? squares[j][i - 1]->coeffs : NULL;
  1309. double * cb = ( j > 0 ) ? squares[j - 1][i]->coeffs : NULL;
  1310. double * cbl = ( i > 0 && j > 0 ) ? squares[j - 1][i - 1]->coeffs : NULL;
  1311. double * ca = ( j < nj - 1 ) ? squares[j + 1][i]->coeffs : NULL;
  1312. double * cal = ( j < nj - 1 && i > 0 ) ? squares[j + 1][i - 1]->coeffs : NULL;
  1313. c[7] = 2.0 * c[4] - c[1];
  1314. c[11] = 2.0 * c[8] - c[5];
  1315. c[15] = 2.0 * c[12] - c[9];
  1316. c[10] = 2.0 * c[6] - c[2];
  1317. c[13] = 2.0 * c[9] - c[5];
  1318. c[16] = 2.0 * c[12] - c[8];
  1319. c[19] = 2.0 * c[15] - c[11];
  1320. if ( cl != NULL )
  1321. {
  1322. cl[21] = c[0];
  1323. cl[22] = c[1];
  1324. cl[23] = c[2];
  1325. cl[24] = c[3];
  1326. cl[18] = c[0] + c[1] - c[4];
  1327. cl[19] = c[1] + c[2] - c[5];
  1328. cl[20] = c[2] + c[3] - c[6];
  1329. cl[17] = 2.0 * cl[20] - cl[23];
  1330. cl[14] = 2.0 * cl[18] - cl[22];
  1331. }
  1332. if ( cb != NULL )
  1333. {
  1334. cb[3] = c[0];
  1335. cb[10] = c[7];
  1336. cb[6] = c[0] + c[7] - c[4];
  1337. cb[2] = 2.0 * cb[6] - cb[10];
  1338. }
  1339. if ( cbl != NULL )
  1340. {
  1341. cbl[23] = cb[2];
  1342. cbl[24] = cb[3];
  1343. cbl[20] = cb[2] + cb[3] - cb[6];
  1344. cbl[17] = cl[14];
  1345. }
  1346. if ( ca != NULL )
  1347. {
  1348. ca[0] = c[3];
  1349. ca[7] = c[10];
  1350. ca[4] = c[3] + c[10] - c[6];
  1351. ca[1] = 2.0 * ca[4] - ca[7];
  1352. }
  1353. if ( cal != NULL )
  1354. {
  1355. cal[21] = c[3];
  1356. cal[22] = ca[1];
  1357. cal[18] = ca[0] + ca[1] - ca[4];
  1358. cal[14] = cl[17];
  1359. }
  1360. }
  1361. //
  1362. // blue
  1363. //
  1364. for ( ii = 0; ii < a->npt; ++ii )
  1365. {
  1366. triangle* t = a->pt[ii];
  1367. square * s = t->parent;
  1368. int i = s->i;
  1369. int j = s->j;
  1370. double * c = s->coeffs;
  1371. double * cr = ( i < ni - 1 ) ? squares[j][i + 1]->coeffs : NULL;
  1372. double * car = ( i < ni - 1 && j < nj - 1 ) ? squares[j + 1][i + 1]->coeffs : NULL;
  1373. double * cbr = ( i < ni - 1 && j > 0 ) ? squares[j - 1][i + 1]->coeffs : NULL;
  1374. if ( car != NULL )
  1375. cr[13] = car[7] + car[14] - car[11];
  1376. if ( cbr != NULL )
  1377. cr[11] = cbr[10] + cbr[17] - cbr[13];
  1378. if ( cr != NULL )
  1379. cr[5] = c[22] + c[23] - c[19];
  1380. }
  1381. //
  1382. // green & yellow
  1383. //
  1384. for ( ii = 0; ii < a->npt; ++ii )
  1385. {
  1386. triangle* t = a->pt[ii];
  1387. square * s = t->parent;
  1388. int i = s->i;
  1389. int j = s->j;
  1390. double * cr = ( i < ni - 1 ) ? squares[j][i + 1]->coeffs : NULL;
  1391. if ( cr != NULL )
  1392. {
  1393. cr[9] = ( cr[5] + cr[13] ) / 2.0;
  1394. cr[8] = ( cr[5] + cr[11] ) / 2.0;
  1395. cr[15] = ( cr[11] + cr[19] ) / 2.0;
  1396. cr[16] = ( cr[13] + cr[19] ) / 2.0;
  1397. cr[12] = ( cr[8] + cr[16] ) / 2.0;
  1398. }
  1399. }
  1400. if ( csa_verbose )
  1401. {
  1402. fprintf( stderr, "checking that all coefficients have been set:\n" );
  1403. fflush( stderr );
  1404. }
  1405. for ( ii = 0; ii < ni * nj; ++ii )
  1406. {
  1407. square* s = squares[0][ii];
  1408. double* c = s->coeffs;
  1409. int i;
  1410. if ( s->npoints == 0 )
  1411. continue;
  1412. for ( i = 0; i < 25; ++i )
  1413. if ( isnan( c[i] ) )
  1414. fprintf( stderr, " squares[%d][%d]->coeffs[%d] = NaN\n", s->j, s->i, i );
  1415. }
  1416. }
  1417. static int i300[] = { 12, 12, 12, 12 };
  1418. static int i030[] = { 3, 24, 21, 0 };
  1419. static int i003[] = { 0, 3, 24, 21 };
  1420. static int i210[] = { 9, 16, 15, 8 };
  1421. static int i021[] = { 2, 17, 22, 7 };
  1422. static int i102[] = { 4, 6, 20, 18 };
  1423. static int i120[] = { 6, 20, 18, 4 };
  1424. static int i012[] = { 1, 10, 23, 14 };
  1425. static int i201[] = { 8, 9, 16, 15 };
  1426. static int i111[] = { 5, 13, 19, 11 };
  1427. static int * iall[] = { i300, i030, i003, i210, i021, i102, i120, i012, i201, i111 };
  1428. static void csa_sethascoeffsflag( csa* a )
  1429. {
  1430. int i, j;
  1431. for ( j = 0; j < a->nj; ++j )
  1432. {
  1433. for ( i = 0; i < a->ni; ++i )
  1434. {
  1435. square* s = a->squares[j][i];
  1436. double* coeffs = s->coeffs;
  1437. int ii;
  1438. for ( ii = 0; ii < 4; ++ii )
  1439. {
  1440. triangle* t = s->triangles[ii];
  1441. int cc;
  1442. for ( cc = 0; cc < 10; ++cc )
  1443. if ( isnan( coeffs[iall[cc][ii]] ) )
  1444. break;
  1445. if ( cc == 10 )
  1446. t->hascoeffs = 1;
  1447. }
  1448. }
  1449. }
  1450. }
  1451. void csa_calculatespline( csa* a )
  1452. {
  1453. csa_squarize( a );
  1454. csa_attachpoints( a );
  1455. csa_findprimarycoeffs( a );
  1456. csa_findsecondarycoeffs( a );
  1457. csa_sethascoeffsflag( a );
  1458. }
  1459. void csa_approximate_point( csa* a, point* p )
  1460. {
  1461. double h = a->h;
  1462. double ii = ( p->x - a->xmin ) / h + 1.0;
  1463. double jj = ( p->y - a->ymin ) / h + 1.0;
  1464. int i, j;
  1465. square * s;
  1466. double fi, fj;
  1467. int ti;
  1468. triangle* t;
  1469. double bc[3];
  1470. if ( ii < 0.0 || jj < 0.0 || ii > (double) a->ni - 1.0 || jj > (double) a->nj - 1.0 )
  1471. {
  1472. p->z = NaN;
  1473. return;
  1474. }
  1475. i = (int) floor( ii );
  1476. j = (int) floor( jj );
  1477. s = a->squares[j][i];
  1478. fi = ii - i;
  1479. fj = jj - j;
  1480. if ( fj < fi )
  1481. {
  1482. if ( fi + fj < 1.0 )
  1483. ti = 3;
  1484. else
  1485. ti = 2;
  1486. }
  1487. else
  1488. {
  1489. if ( fi + fj < 1.0 )
  1490. ti = 0;
  1491. else
  1492. ti = 1;
  1493. }
  1494. t = s->triangles[ti];
  1495. if ( !t->hascoeffs )
  1496. {
  1497. p->z = NaN;
  1498. return;
  1499. }
  1500. triangle_calculatebc( t, p, bc );
  1501. {
  1502. double * c = s->coeffs;
  1503. double bc1 = bc[0];
  1504. double bc2 = bc[1];
  1505. double bc3 = bc[2];
  1506. double tmp1 = bc1 * bc1;
  1507. double tmp2 = bc2 * bc2;
  1508. double tmp3 = bc3 * bc3;
  1509. switch ( ti )
  1510. {
  1511. case 0:
  1512. p->z = c[12] * bc1 * tmp1 + c[3] * bc2 * tmp2 + c[0] * bc3 * tmp3 + 3.0 * ( c[9] * tmp1 * bc2 + c[2] * tmp2 * bc3 + c[4] * tmp3 * bc1 + c[6] * bc1 * tmp2 + c[1] * bc2 * tmp3 + c[8] * tmp1 * bc3 ) + 6.0 * c[5] * bc1 * bc2 * bc3;
  1513. break;
  1514. case 1:
  1515. p->z = c[12] * bc1 * tmp1 + c[24] * bc2 * tmp2 + c[3] * bc3 * tmp3 + 3.0 * ( c[16] * tmp1 * bc2 + c[17] * tmp2 * bc3 + c[6] * tmp3 * bc1 + c[20] * bc1 * tmp2 + c[10] * bc2 * tmp3 + c[9] * tmp1 * bc3 ) + 6.0 * c[13] * bc1 * bc2 * bc3;
  1516. break;
  1517. case 2:
  1518. p->z = c[12] * bc1 * tmp1 + c[21] * bc2 * tmp2 + c[24] * bc3 * tmp3 + 3.0 * ( c[15] * tmp1 * bc2 + c[22] * tmp2 * bc3 + c[20] * tmp3 * bc1 + c[18] * bc1 * tmp2 + c[23] * bc2 * tmp3 + c[16] * tmp1 * bc3 ) + 6.0 * c[19] * bc1 * bc2 * bc3;
  1519. break;
  1520. default: // 3
  1521. p->z = c[12] * bc1 * tmp1 + c[0] * bc2 * tmp2 + c[21] * bc3 * tmp3 + 3.0 * ( c[8] * tmp1 * bc2 + c[7] * tmp2 * bc3 + c[18] * tmp3 * bc1 + c[4] * bc1 * tmp2 + c[14] * bc2 * tmp3 + c[15] * tmp1 * bc3 ) + 6.0 * c[11] * bc1 * bc2 * bc3;
  1522. }
  1523. }
  1524. }
  1525. void csa_approximate_points( csa* a, int n, point* points )
  1526. {
  1527. int ii;
  1528. for ( ii = 0; ii < n; ++ii )
  1529. csa_approximate_point( a, &points[ii] );
  1530. }
  1531. void csa_setnpmin( csa* a, int npmin )
  1532. {
  1533. a->npmin = npmin;
  1534. }
  1535. void csa_setnpmax( csa* a, int npmax )
  1536. {
  1537. a->npmax = npmax;
  1538. }
  1539. void csa_setk( csa* a, int k )
  1540. {
  1541. a->k = k;
  1542. }
  1543. void csa_setnppc( csa* a, double nppc )
  1544. {
  1545. a->nppc = (int) nppc;
  1546. }
  1547. #if defined ( STANDALONE )
  1548. #include "minell.h"
  1549. #define NIMAX 2048
  1550. #define BUFSIZE 10240
  1551. #define STRBUFSIZE 64
  1552. static void points_generate( double xmin, double xmax, double ymin, double ymax, int nx, int ny, int* nout, point** pout )
  1553. {
  1554. double stepx, stepy;
  1555. double x0, xx, yy;
  1556. int i, j, ii;
  1557. if ( nx < 1 || ny < 1 )
  1558. {
  1559. *pout = NULL;
  1560. *nout = 0;
  1561. return;
  1562. }
  1563. *nout = nx * ny;
  1564. *pout = malloc( *nout * sizeof ( point ) );
  1565. stepx = ( nx > 1 ) ? ( xmax - xmin ) / ( nx - 1 ) : 0.0;
  1566. stepy = ( ny > 1 ) ? ( ymax - ymin ) / ( ny - 1 ) : 0.0;
  1567. x0 = ( nx > 1 ) ? xmin : ( xmin + xmax ) / 2.0;
  1568. yy = ( ny > 1 ) ? ymin : ( ymin + ymax ) / 2.0;
  1569. ii = 0;
  1570. for ( j = 0; j < ny; ++j )
  1571. {
  1572. xx = x0;
  1573. for ( i = 0; i < nx; ++i )
  1574. {
  1575. point* p = &( *pout )[ii];
  1576. p->x = xx;
  1577. p->y = yy;
  1578. xx += stepx;
  1579. ii++;
  1580. }
  1581. yy += stepy;
  1582. }
  1583. }
  1584. static int str2double( char* token, double* value )
  1585. {
  1586. char* end = NULL;
  1587. if ( token == NULL )
  1588. {
  1589. *value = NaN;
  1590. return 0;
  1591. }
  1592. *value = strtod( token, &end );
  1593. if ( end == token )
  1594. {
  1595. *value = NaN;
  1596. return 0;
  1597. }
  1598. return 1;
  1599. }
  1600. #define NALLOCATED_START 102

Large files files are truncated, but you can click here to view the full file