libifs.c 31.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
/*
2 3 4
   flame - cosmic recursive fractal flames
   Copyright (C) 1992  Scott Draves <spot@cs.cmu.edu>

5
   This program is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3 of the License, or
8 9 10 11 12 13 14 15
   (at your option) 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 <http://www.gnu.org/licenses/>.
17
 */
18

19
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
20 21

#include <stdlib.h>
Hans Breuer's avatar
Hans Breuer committed
22
#include <string.h> /* strcmp */
Elliot Lee's avatar
Elliot Lee committed
23

24
#include "libgimp/gimp.h"
25

26
#include "libifs.h"
Manish Singh's avatar
Manish Singh committed
27

Elliot Lee's avatar
Elliot Lee committed
28 29
#define CHOOSE_XFORM_GRAIN 100

30 31
static int    flam3_random_bit (void);
static double flam3_random01   (void);
Elliot Lee's avatar
Elliot Lee committed
32 33 34 35 36 37 38

/*
 * run the function system described by CP forward N generations.
 * store the n resulting 3 vectors in POINTS.  the initial point is passed
 * in POINTS[0].  ignore the first FUSE iterations.
 */

39 40 41 42 43
void
iterate (control_point *cp,
         int            n,
         int            fuse,
         point         *points)
Elliot Lee's avatar
Elliot Lee committed
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
  int    i, j, count_large = 0, count_nan = 0;
  int    xform_distrib[CHOOSE_XFORM_GRAIN];
  double p[3], t, r, dr;
  p[0] = points[0][0];
  p[1] = points[0][1];
  p[2] = points[0][2];

  /*
   * first, set up xform, which is an array that converts a uniform random
   * variable into one with the distribution dictated by the density
   * fields
   */
  dr = 0.0;
  for (i = 0; i < NXFORMS; i++)
    dr += cp->xform[i].density;
  dr = dr / CHOOSE_XFORM_GRAIN;

  j = 0;
  t = cp->xform[0].density;
  r = 0.0;
  for (i = 0; i < CHOOSE_XFORM_GRAIN; i++)
    {
      while (r >= t)
        {
          j++;
          t += cp->xform[j].density;
        }
Elliot Lee's avatar
Elliot Lee committed
72 73
      xform_distrib[i] = j;
      r += dr;
74
    }
Elliot Lee's avatar
Elliot Lee committed
75

76 77 78 79
  for (i = -fuse; i < n; i++)
    {
      /* FIXME: the following is supported only by gcc and c99 */
      int fn = xform_distrib[g_random_int_range (0, CHOOSE_XFORM_GRAIN)];
Elliot Lee's avatar
Elliot Lee committed
80 81 82
      double tx, ty, v;

      if (p[0] > 100.0 || p[0] < -100.0 ||
83 84
        p[1] > 100.0 || p[1] < -100.0)
      count_large++;
Elliot Lee's avatar
Elliot Lee committed
85
      if (p[0] != p[0])
86
        count_nan++;
Elliot Lee's avatar
Elliot Lee committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100

#define coef   cp->xform[fn].c
#define vari   cp->xform[fn].var

      /* first compute the color coord */
      p[2] = (p[2] + cp->xform[fn].color) / 2.0;

      /* then apply the affine part of the function */
      tx = coef[0][0] * p[0] + coef[1][0] * p[1] + coef[2][0];
      ty = coef[0][1] * p[0] + coef[1][1] * p[1] + coef[2][1];

      p[0] = p[1] = 0.0;
      /* then add in proportional amounts of each of the variations */
      v = vari[0];
101 102 103 104 105 106 107 108 109
      if (v > 0.0)
        {
          /* linear */
          double nx, ny;
          nx = tx;
          ny = ty;
          p[0] += v * nx;
          p[1] += v * ny;
        }
110

Elliot Lee's avatar
Elliot Lee committed
111
      v = vari[1];
112 113 114 115 116 117 118 119 120
      if (v > 0.0)
        {
          /* sinusoidal */
          double nx, ny;
          nx = sin (tx);
          ny = sin (ty);
          p[0] += v * nx;
          p[1] += v * ny;
        }
121

Elliot Lee's avatar
Elliot Lee committed
122
      v = vari[2];
123 124 125 126 127 128 129 130 131 132
      if (v > 0.0)
        {
          /* spherical */
          double nx, ny;
          double r2 = tx * tx + ty * ty + 1e-6;
          nx = tx / r2;
          ny = ty / r2;
          p[0] += v * nx;
          p[1] += v * ny;
        }
Elliot Lee's avatar
Elliot Lee committed
133 134

      v = vari[3];
135 136 137 138 139 140 141 142 143 144 145
      if (v > 0.0)
        {
          /* swirl */
          double r2 = tx * tx + ty * ty;  /* /k here is fun */
          double c1 = sin (r2);
          double c2 = cos (r2);
          double nx = c1 * tx - c2 * ty;
          double ny = c2 * tx + c1 * ty;
          p[0] += v * nx;
          p[1] += v * ny;
        }
146

Elliot Lee's avatar
Elliot Lee committed
147
      v = vari[4];
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      if (v > 0.0)
        {
          /* horseshoe */
          double a, c1, c2, nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            a = atan2(tx, ty);  /* times k here is fun */
          else
            a = 0.0;
          c1 = sin (a);
          c2 = cos (a);
          nx = c1 * tx - c2 * ty;
          ny = c2 * tx + c1 * ty;
          p[0] += v * nx;
          p[1] += v * ny;
        }
Elliot Lee's avatar
Elliot Lee committed
164 165

      v = vari[5];
166 167 168 169 170 171 172 173 174 175 176 177 178 179
      if (v > 0.0)
        {
          /* polar */
          double nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            nx = atan2 (tx, ty) / G_PI;
          else
            nx = 0.0;

          ny = sqrt (tx * tx + ty * ty) - 1.0;
          p[0] += v * nx;
          p[1] += v * ny;
        }
Elliot Lee's avatar
Elliot Lee committed
180 181

      v = vari[6];
182 183 184 185 186 187 188 189 190 191 192
      if (v > 0.0)
        {
          /* bent */
          double nx, ny;
          nx = tx;
          ny = ty;
          if (nx < 0.0) nx = nx * 2.0;
          if (ny < 0.0) ny = ny / 2.0;
          p[0] += v * nx;
          p[1] += v * ny;
        }
Elliot Lee's avatar
Elliot Lee committed
193

194
      v = vari[7];
195 196 197 198 199 200
      if (v > 0.0)
        {
          /* folded handkerchief */
          double theta, r2, nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
201
            theta = atan2( tx, ty );
202
          else
203
            theta = 0.0;
204 205 206 207 208 209
          r2 = sqrt (tx * tx + ty * ty);
          nx = sin (theta + r2) * r2;
          ny = cos (theta - r2) * r2;
          p[0] += v * nx;
          p[1] += v * ny;
        }
Sven Neumann's avatar
Sven Neumann committed
210

211
      v = vari[8];
212 213 214 215 216 217
      if (v > 0.0)
        {
          /* heart */
          double theta, r2, nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
218
            theta = atan2( tx, ty );
219
          else
220
            theta = 0.0;
221 222 223 224 225 226 227
          r2 = sqrt (tx * tx + ty * ty);
          theta *= r2;
          nx = sin (theta) * r2;
          ny = cos (theta) * -r2;
          p[0] += v * nx;
          p[1] += v * ny;
        }
228 229

      v = vari[9];
230 231 232 233 234 235 236 237
      if (v > 0.0)
        {
          /* disc */
          double theta, r2, nx, ny;
          if ( tx < -EPS || tx > EPS ||
               ty < - EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
238
            theta = 0.0;
239 240 241 242 243 244
          nx = tx * G_PI;
          ny = ty * G_PI;
          r2 = sqrt (nx * nx * ny * ny);
          p[0] += v * sin(r2) * theta / G_PI;
          p[1] += v * cos(r2) * theta / G_PI;
        }
245 246

      v = vari[10];
247 248 249 250 251 252
      if (v > 0.0)
        {
          /* spiral */
          double theta, r2;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
253
            theta = atan2( tx, ty );
254
          else
255
            theta = 0.0;
256 257 258 259
          r2 = sqrt (tx * tx + ty * ty) + 1e-6;
          p[0] += v * (cos (theta) + sin (r2)) / r2;
          p[1] += v * (cos (theta) + cos (r2)) / r2;
        }
260 261

      v = vari[11];
262 263 264 265 266 267 268 269
      if (v > 0.0)
        {
          /* hyperbolic */
          double theta, r2;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
270
            theta = 0.0;
271 272 273 274
          r2 = sqrt (tx * tx + ty * ty) + 1e-6;
          p[0] += v * sin (theta) / r2;
          p[1] += v * cos (theta) * r2;
        }
275 276

      v = vari[12];
277 278 279 280 281 282 283 284
      if (v > 0.0 )
        {
          double theta, r2;
          /* diamond */
          if ( tx < -EPS || tx > EPS ||
               ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
285
            theta = 0.0;
286 287 288 289
          r2 = sqrt( tx * tx + ty * ty );
          p[0] += v * sin (theta) * cos (r2);
          p[1] += v * cos (theta) * sin (r2);
        }
290 291

      v = vari[13];
292 293 294 295 296 297 298 299
      if (v > 0.0)
        {
          /* ex */
          double theta, r2, n0, n1, m0, m1;
          if ( tx < -EPS || tx > EPS ||
               ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
300
            theta = 0.0;
301 302 303 304 305 306 307 308
          r2 = sqrt( tx * tx + ty * ty );
          n0 = sin(theta + r2);
          n1 = cos(theta - r2);
          m0 = n0 * n0 * n0 * r2;
          m1 = n1 * n1 * n1 * r2;
          p[0] += v * (m0 + m1);
          p[1] += v * (m0 - m1);
        }
309 310

      v = vari[14];
311 312 313 314 315 316 317 318
      if ( v > 0.0)
        {
          double theta, r2, nx, ny;
          /* julia */
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
319
            theta = 0.0;
320 321 322 323 324 325 326 327
          if (flam3_random_bit ())
            theta += G_PI;
          r2 = pow (tx * tx + ty * ty, 0.25);
          nx = r2 * cos (theta);
          ny = r2 * sin (theta);
          p[0] += v * nx;
          p[1] += v * ny;
        }
328 329

      v = vari[15];
330 331 332 333 334 335 336 337 338 339 340
      if (v > 0.0)
        {
          /* waves */
          double dx, dy, nx, ny;
          dx = coef[2][0];
          dy = coef[2][1];
          nx = tx + coef[1][0] * sin (ty / ((dx * dx) + EPS));
          ny = ty + coef[1][1] * sin (tx / ((dy * dy) + EPS));
          p[0] += v * nx;
          p[1] += v * ny;
        }
Sven Neumann's avatar
Sven Neumann committed
341

342
      v = vari[16];
343 344 345 346 347 348 349 350
      if (v > 0.0)
        {
          /* fisheye */
          double theta, r2, nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
351
            theta = 0.0;
352 353 354 355 356 357 358
          r2 = sqrt (tx * tx + ty * ty);
          r2 = 2 * r2 / (r2 + 1);
          nx = r2 * cos (theta);
          ny = r2 * sin (theta);
          p[0] += v * nx;
          p[1] += v * ny;
        }
359 360

      v = vari[17];
361 362 363 364 365 366 367 368 369 370 371
      if (v > 0.0)
        {
          /* popcorn */
          double dx, dy, nx, ny;
          dx = tan (3 * ty);
          dy = tan (3 * tx);
          nx = tx + coef[2][0] * sin (dx);
          ny = ty + coef[2][1] * sin (dy);
          p[0] += v * nx;
          p[1] += v * ny;
        }
372 373

      v = vari[18];
374 375 376 377 378 379 380 381 382 383 384
      if (v > 0.0)
        {
          /* exponential */
          double dx, dy, nx, ny;
          dx = exp (tx - 1.0);
          dy = G_PI * ty;
          nx = cos (dy) * dx;
          ny = sin (dy) * dx;
          p[0] += v * nx;
          p[1] += v * ny;
        }
385 386

      v = vari[19];
387 388 389 390 391 392 393 394
      if (v > 0.0)
        {
          /* power */
          double theta, r2, tsin, tcos, nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
395
            theta = 0.0;
396 397 398 399
          tsin = sin (theta);
          tcos = cos (theta);
          r2 = sqrt (tx * tx + ty * ty);
          r2 = pow (r2, tsin);
Mikael Magnusson's avatar
Mikael Magnusson committed
400
          nx = r2 * tcos;
401 402 403 404
          ny = r2 * tsin;
          p[0] += v * nx;
          p[1] += v * ny;
        }
405 406

      v = vari[20];
407 408 409 410 411 412 413 414 415
      if (v > 0.0)
        {
          /* cosine */
          double nx, ny;
          nx =  cos (tx * G_PI) * cosh (ty);
          ny = -sin (tx * G_PI) * sinh (ty);
          p[0] += v * nx;
          p[1] += v * ny;
        }
416 417

      v = vari[21];
418 419 420 421 422 423 424 425
      if (v > 0.0)
        {
          /* rings */
          double theta, r2, dx, nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
426
            theta = 0;
427 428 429 430 431 432 433 434 435
          dx = coef[2][0];
          dx = dx * dx + EPS;
          r2 = sqrt (tx * tx + ty * ty);
          r2 = fmod (r2 + dx, 2 * dx) - dx + r2 * (1 - dx);
          nx = cos (theta) * r2;
          ny = sin (theta) * r2;
          p[0] += v * nx;
          p[1] += v * ny;
        }
436 437

      v = vari[22];
438 439 440 441 442 443 444 445
      if (v > 0.0)
        {
          /* fan */
          double theta, r2, dx, dy, dx2, nx, ny;
          if (tx < -EPS || tx > EPS ||
              ty < -EPS || ty > EPS)
            theta = atan2 (tx, ty);
          else
446
            theta = 0.0;
447 448 449 450 451 452 453 454 455 456 457
          dx = coef[2][0];
          dy = coef[2][1];
          dx = G_PI * (dx * dx + EPS);
          dx2 = dx / 2;
          r2 = sqrt (tx * tx + ty * ty );
          theta += (fmod (theta + dy, dx) > dx2) ? -dx2: dx2;
          nx = cos (theta) * r2;
          ny = sin (theta) * r2;
          p[0] += v * nx;
          p[1] += v * ny;
        }
458 459

      v = vari[23];
460 461 462 463 464 465 466 467
      if (v > 0.0)
        {
          /* eyefish */
          double r2;
          r2 = 2.0 * v / (sqrt(tx * tx + ty * ty) + 1.0);
          p[0] += r2 * tx;
          p[1] += r2 * ty;
        }
468 469

      v = vari[24];
470 471 472 473 474 475 476 477
      if (v > 0.0)
        {
          /* bubble */
          double r2;
          r2 = v / ((tx * tx + ty * ty) / 4 + 1);
          p[0] += r2 * tx;
          p[1] += r2 * ty;
        }
478 479

      v = vari[25];
480 481 482 483 484 485 486 487
      if (v > 0.0)
        {
          /* cylinder */
          double nx;
          nx = sin (tx);
          p[0] += v * nx;
          p[1] += v * ty;
        }
488 489

      v = vari[26];
490 491 492 493 494 495 496 497 498 499 500
      if (v > 0.0)
        {
          /* noise */
          double rx, sinr, cosr, nois;
          rx = flam3_random01 () * 2 * G_PI;
          sinr = sin (rx);
          cosr = cos (rx);
          nois = flam3_random01 ();
          p[0] += v * nois * tx * cosr;
          p[1] += v * nois * ty * sinr;
        }
501 502

      v = vari[27];
503 504 505 506 507 508 509 510 511 512 513
      if (v > 0.0)
        {
          /* blur */
          double rx, sinr, cosr, nois;
          rx = flam3_random01 () * 2 * G_PI;
          sinr = sin (rx);
          cosr = cos (rx);
          nois = flam3_random01 ();
          p[0] += v * nois * cosr;
          p[1] += v * nois * sinr;
        }
514 515

      v = vari[28];
516 517 518 519 520 521 522 523 524 525 526 527
      if (v > 0.0)
        {
          /* gaussian */
          double ang, sina, cosa, r2;
          ang = flam3_random01 () * 2 * G_PI;
          sina = sin (ang);
          cosa = cos (ang);
          r2 = v * (flam3_random01 () + flam3_random01 () + flam3_random01 () +
                    flam3_random01 () - 2.0);
          p[0] += r2 * cosa;
          p[1] += r2 * sina;
        }
Sven Neumann's avatar
Sven Neumann committed
528

Elliot Lee's avatar
Elliot Lee committed
529
      /* if fuse over, store it */
530 531 532 533 534 535 536
      if (i >= 0)
        {
          points[i][0] = p[0];
          points[i][1] = p[1];
          points[i][2] = p[2];
        }
    }
Elliot Lee's avatar
Elliot Lee committed
537 538 539
}

/* args must be non-overlapping */
540 541 542 543
void
mult_matrix (double s1[2][2],
             double s2[2][2],
             double d[2][2])
Elliot Lee's avatar
Elliot Lee committed
544
{
545 546 547 548
  d[0][0] = s1[0][0] * s2[0][0] + s1[1][0] * s2[0][1];
  d[1][0] = s1[0][0] * s2[1][0] + s1[1][0] * s2[1][1];
  d[0][1] = s1[0][1] * s2[0][0] + s1[1][1] * s2[0][1];
  d[1][1] = s1[0][1] * s2[1][0] + s1[1][1] * s2[1][1];
Elliot Lee's avatar
Elliot Lee committed
549 550
}

551 552
static
double det_matrix (double s[2][2])
Elliot Lee's avatar
Elliot Lee committed
553
{
554
  return s[0][0] * s[1][1] - s[0][1] * s[1][0];
Elliot Lee's avatar
Elliot Lee committed
555 556
}

557 558 559 560 561 562 563 564
static void
interpolate_angle (double  t,
                   double  s,
                   double *v1,
                   double *v2,
                   double *v3,
                   int     tie,
                   int     cross)
Elliot Lee's avatar
Elliot Lee committed
565
{
566 567 568 569 570 571 572 573
  double x = *v1;
  double y = *v2;
  double d;
  static double lastx, lasty;

  /* take the shorter way around the circle... */
  if (x > y)
    {
Elliot Lee's avatar
Elliot Lee committed
574
      d = x - y;
575
      if (d > G_PI + EPS ||
576 577 578 579 580
          (d > G_PI - EPS && tie))
        y += 2 * G_PI;
    }
  else
    {
Elliot Lee's avatar
Elliot Lee committed
581
      d = y - x;
582
      if (d > G_PI + EPS ||
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
          (d > G_PI - EPS && tie))
        x += 2 * G_PI;
    }
  /* unless we are supposed to avoid crossing */
  if (cross)
    {
      if (lastx > x)
        {
          if (lasty < y)
            y -= 2 * G_PI;
        }
      else
        {
          if (lasty > y)
            y += 2 * G_PI;
        }
    }
  else
    {
Elliot Lee's avatar
Elliot Lee committed
602 603
      lastx = x;
      lasty = y;
604
    }
Elliot Lee's avatar
Elliot Lee committed
605

606
  *v3 = s * x + t * y;
Elliot Lee's avatar
Elliot Lee committed
607 608
}

609 610 611 612 613 614 615 616 617
static void
interpolate_complex (double  t,
                     double  s,
                     double *r1,
                     double *r2,
                     double *r3,
                     int     flip,
                     int     tie,
                     int     cross)
Elliot Lee's avatar
Elliot Lee committed
618
{
619 620 621 622 623 624 625 626 627
  double c1[2], c2[2], c3[2];
  double a1, a2, a3, d1, d2, d3;

  c1[0] = r1[0];
  c1[1] = r1[1];
  c2[0] = r2[0];
  c2[1] = r2[1];
  if (flip)
    {
Elliot Lee's avatar
Elliot Lee committed
628 629 630 631 632 633
      double t = c1[0];
      c1[0] = c1[1];
      c1[1] = t;
      t = c2[0];
      c2[0] = c2[1];
      c2[1] = t;
634
    }
Elliot Lee's avatar
Elliot Lee committed
635

636 637 638 639 640
  /* convert to log space */
  a1 = atan2 (c1[1], c1[0]);
  a2 = atan2 (c2[1], c2[0]);
  d1 = 0.5 * log (c1[0] * c1[0] + c1[1] * c1[1]);
  d2 = 0.5 * log (c2[0] * c2[0] + c2[1] * c2[1]);
Elliot Lee's avatar
Elliot Lee committed
641

642 643 644
  /* interpolate linearly */
  interpolate_angle (t, s, &a1, &a2, &a3, tie, cross);
  d3 = s * d1 + t * d2;
Elliot Lee's avatar
Elliot Lee committed
645

646 647 648 649
  /* convert back */
  d3 = exp (d3);
  c3[0] = cos (a3) * d3;
  c3[1] = sin (a3) * d3;
Elliot Lee's avatar
Elliot Lee committed
650

651 652
  if (flip)
    {
Elliot Lee's avatar
Elliot Lee committed
653 654
      r3[1] = c3[0];
      r3[0] = c3[1];
655 656 657
    }
  else
    {
Elliot Lee's avatar
Elliot Lee committed
658 659
      r3[0] = c3[0];
      r3[1] = c3[1];
660
    }
Elliot Lee's avatar
Elliot Lee committed
661 662
}

663 664 665 666 667
static void
interpolate_matrix (double t,
                    double m1[3][2],
                    double m2[3][2],
                    double m3[3][2])
Elliot Lee's avatar
Elliot Lee committed
668
{
669 670 671 672 673 674 675 676
  double s = 1.0 - t;

  interpolate_complex (t, s, &m1[0][0], &m2[0][0], &m3[0][0], 0, 0, 0);
  interpolate_complex (t, s, &m1[1][0], &m2[1][0], &m3[1][0], 1, 1, 0);

  /* handle the translation part of the xform linearly */
  m3[2][0] = s * m1[2][0] + t * m2[2][0];
  m3[2][1] = s * m1[2][1] + t * m2[2][1];
Elliot Lee's avatar
Elliot Lee committed
677 678 679 680 681 682 683 684 685
}

#define INTERP(x)  result->x = c0 * cps[i1].x + c1 * cps[i2].x

/*
 * create a control point that interpolates between the control points
 * passed in CPS.  for now just do linear.  in the future, add control
 * point types and other things to the cps.  CPS must be sorted by time.
 */
686 687 688 689 690
void
interpolate (control_point  cps[],
             int            ncps,
             double         time,
             control_point *result)
Elliot Lee's avatar
Elliot Lee committed
691
{
692 693
  int i, j, i1, i2;
  double c0, c1, t;
Elliot Lee's avatar
Elliot Lee committed
694

695 696
  if (ncps == 1)
    {
Elliot Lee's avatar
Elliot Lee committed
697 698
      *result = cps[0];
      return;
699 700 701
    }
  if (cps[0].time >= time)
    {
Elliot Lee's avatar
Elliot Lee committed
702 703
      i1 = 0;
      i2 = 1;
704 705 706
    }
  else if (cps[ncps - 1].time <= time)
    {
Elliot Lee's avatar
Elliot Lee committed
707 708
      i1 = ncps - 2;
      i2 = ncps - 1;
709 710 711
    }
  else
    {
Elliot Lee's avatar
Elliot Lee committed
712 713
      i1 = 0;
      while (cps[i1].time < time)
714
        i1++;
Elliot Lee's avatar
Elliot Lee committed
715 716 717
      i1--;
      i2 = i1 + 1;
      if (time - cps[i1].time > -1e-7 &&
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
          time - cps[i1].time < 1e-7)
        {
          *result = cps[i1];
          return;
        }
    }

  c0 = (cps[i2].time - time) / (cps[i2].time - cps[i1].time);
  c1 = 1.0 - c0;

  result->time = time;

  if (cps[i1].cmap_inter)
    {
      for (i = 0; i < 256; i++)
        {
          double spread = 0.15;
          double d0, d1, e0, e1, c = 2 * G_PI * i / 256.0;
          c = cos(c * cps[i1].cmap_inter) + 4.0 * c1 - 2.0;
          if (c >  spread) c =  spread;
          if (c < -spread) c = -spread;
          d1 = (c + spread) * 0.5 / spread;
          d0 = 1.0 - d1;
          e0 = (d0 < 0.5) ? (d0 * 2) : (d1 * 2);
          e1 = 1.0 - e0;
          for (j = 0; j < 3; j++)
            {
              result->cmap[i][j] = (d0 * cps[i1].cmap[i][j] +
                                    d1 * cps[i2].cmap[i][j]);
Elliot Lee's avatar
Elliot Lee committed
747
#define bright_peak 2.0
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
              result->cmap[i][j] = (e1 * result->cmap[i][j] +
                                    e0 * 1.0);
            }
        }
    }
  else
    {
      for (i = 0; i < 256; i++)
        {
          double t[3], s[3];
          rgb2hsv (cps[i1].cmap[i], s);
          rgb2hsv (cps[i2].cmap[i], t);
          for (j = 0; j < 3; j++)
            t[j] = c0 * s[j] + c1 * t[j];
          hsv2rgb (t, result->cmap[i]);
        }
    }

  result->cmap_index = -1;
  INTERP(brightness);
  INTERP(contrast);
  INTERP(gamma);
  INTERP(width);
  INTERP(height);
  INTERP(spatial_oversample);
  INTERP(center[0]);
  INTERP(center[1]);
  INTERP(pixels_per_unit);
  INTERP(spatial_filter_radius);
  INTERP(sample_density);
  INTERP(zoom);
  INTERP(nbatches);
  INTERP(white_level);
  for (i = 0; i < 2; i++)
    for (j = 0; j < 2; j++)
      {
        INTERP(pulse[i][j]);
        INTERP(wiggle[i][j]);
      }

  for (i = 0; i < NXFORMS; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
790
      double r;
791 792
      double rh_time;

Elliot Lee's avatar
Elliot Lee committed
793 794
      INTERP(xform[i].density);
      if (result->xform[i].density > 0)
795
        result->xform[i].density = 1.0;
Elliot Lee's avatar
Elliot Lee committed
796 797
      INTERP(xform[i].color);
      for (j = 0; j < NVARS; j++)
798
        INTERP(xform[i].var[j]);
Elliot Lee's avatar
Elliot Lee committed
799 800
      t = 0.0;
      for (j = 0; j < NVARS; j++)
801
        t += result->xform[i].var[j];
Elliot Lee's avatar
Elliot Lee committed
802 803
      t = 1.0 / t;
      for (j = 0; j < NVARS; j++)
804
        result->xform[i].var[j] *= t;
Elliot Lee's avatar
Elliot Lee committed
805 806

      interpolate_matrix(c1, cps[i1].xform[i].c, cps[i2].xform[i].c,
807 808
                         result->xform[i].c);

809
      rh_time = time * 2 * G_PI / (60.0 * 30.0);
810

811 812 813 814 815 816 817 818 819
      /* apply pulse factor. */
      r = 1.0;
      for (j = 0; j < 2; j++)
        r += result->pulse[j][0] * sin(result->pulse[j][1] * rh_time);
      for (j = 0; j < 3; j++)
        {
          result->xform[i].c[j][0] *= r;
          result->xform[i].c[j][1] *= r;
        }
820

821 822 823 824 825 826 827 828 829
      /* apply wiggle factor */
      for (j = 0; j < 2; j++)
        {
          double tt = result->wiggle[j][1] * rh_time;
          double m = result->wiggle[j][0];
          result->xform[i].c[0][0] += m *  cos(tt);
          result->xform[i].c[1][0] += m * -sin(tt);
          result->xform[i].c[0][1] += m *  sin(tt);
          result->xform[i].c[1][1] += m *  cos(tt);
830 831
        }
    } /* for i */
Elliot Lee's avatar
Elliot Lee committed
832 833 834 835 836 837 838 839
}



/*
 * split a string passed in ss into tokens on whitespace.
 * # comments to end of line.  ; terminates the record
 */
840 841 842 843
void
tokenize (char **ss,
          char  *argv[],
          int   *argc)
Elliot Lee's avatar
Elliot Lee committed
844
{
845 846
  char *s = *ss;
  int   i = 0, state = 0;
Elliot Lee's avatar
Elliot Lee committed
847

848 849
  while (*s != ';')
    {
Elliot Lee's avatar
Elliot Lee committed
850
      char c = *s;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
      switch (state)
        {
        case 0:
          if (c == '#')
            state = 2;
          else if (!g_ascii_isspace (c))
            {
              argv[i] = s;
              i++;
              state = 1;
            }
        case 1:
          if (g_ascii_isspace (c))
            {
              *s = 0;
              state = 0;
            }
        case 2:
          if (c == '\n')
            state = 0;
        }
Elliot Lee's avatar
Elliot Lee committed
872
      s++;
873 874 875 876
    }
  *s = 0;
  *ss = s + 1;
  *argc = i;
Elliot Lee's avatar
Elliot Lee committed
877 878
}

879 880 881
static int
compare_xforms (const void *va,
                const void *vb)
Elliot Lee's avatar
Elliot Lee committed
882
{
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
  double aa[2][2];
  double bb[2][2];
  double ad, bd;
  const xform *a = va;
  const xform *b = vb;

  aa[0][0] = a->c[0][0];
  aa[0][1] = a->c[0][1];
  aa[1][0] = a->c[1][0];
  aa[1][1] = a->c[1][1];
  bb[0][0] = b->c[0][0];
  bb[0][1] = b->c[0][1];
  bb[1][0] = b->c[1][0];
  bb[1][1] = b->c[1][1];
  ad = det_matrix (aa);
  bd = det_matrix (bb);
  if (ad < bd)
    return -1;
  if (ad > bd)
    return 1;
  return 0;
Elliot Lee's avatar
Elliot Lee committed
904 905 906 907 908 909 910 911 912 913 914
}

#define MAXARGS 1000
#define streql(x,y) (!strcmp(x,y))

/*
 * given a pointer to a string SS, fill fields of a control point CP.
 * return a pointer to the first unused char in SS.  totally barfucious,
 * must integrate with tcl soon...
 */

915 916 917
void
parse_control_point (char          **ss,
                     control_point  *cp)
Elliot Lee's avatar
Elliot Lee committed
918
{
919 920 921 922 923 924 925 926 927
  char *argv[MAXARGS];
  int argc, i, j;
  int set_cm = 0, set_image_size = 0, set_nbatches = 0, set_white_level = 0, set_cmap_inter = 0;
  int set_spatial_oversample = 0;
  double *slot = NULL, xf, cm, t, nbatches, white_level, spatial_oversample, cmap_inter;
  double image_size[2];

  for (i = 0; i < NXFORMS; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
928 929 930 931
      cp->xform[i].density = 0.0;
      cp->xform[i].color = (i == 0);
      cp->xform[i].var[0] = 1.0;
      for (j = 1; j < NVARS; j++)
932
        cp->xform[i].var[j] = 0.0;
Elliot Lee's avatar
Elliot Lee committed
933 934 935 936 937 938
      cp->xform[i].c[0][0] = 1.0;
      cp->xform[i].c[0][1] = 0.0;
      cp->xform[i].c[1][0] = 0.0;
      cp->xform[i].c[1][1] = 1.0;
      cp->xform[i].c[2][0] = 0.0;
      cp->xform[i].c[2][1] = 0.0;
939 940 941
    }
  for (j = 0; j < 2; j++)
    {
Elliot Lee's avatar
Elliot Lee committed
942 943 944 945
      cp->pulse[j][0] = 0.0;
      cp->pulse[j][1] = 60.0;
      cp->wiggle[j][0] = 0.0;
      cp->wiggle[j][1] = 60.0;
946
    }
947

948 949 950
  tokenize (ss, argv, &argc);
  for (i = 0; i < argc; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
951
      if (streql("xform", argv[i]))
952
        slot = &xf;
Elliot Lee's avatar
Elliot Lee committed
953
      else if (streql("time", argv[i]))
954
        slot = &cp->time;
Elliot Lee's avatar
Elliot Lee committed
955
      else if (streql("brightness", argv[i]))
956
        slot = &cp->brightness;
Elliot Lee's avatar
Elliot Lee committed
957
      else if (streql("contrast", argv[i]))
958
        slot = &cp->contrast;
Elliot Lee's avatar
Elliot Lee committed
959
      else if (streql("gamma", argv[i]))
960
        slot = &cp->gamma;
Elliot Lee's avatar
Elliot Lee committed
961
      else if (streql("zoom", argv[i]))
962 963 964 965 966 967 968 969
        slot = &cp->zoom;
      else if (streql("image_size", argv[i]))
        {
          slot = image_size;
          set_image_size = 1;
        }
      else if (streql("center", argv[i]))
        slot = cp->center;
Elliot Lee's avatar
Elliot Lee committed
970
      else if (streql("pulse", argv[i]))
971
        slot = (double *) cp->pulse;
Elliot Lee's avatar
Elliot Lee committed
972
      else if (streql("wiggle", argv[i]))
973
        slot = (double *) cp->wiggle;
Elliot Lee's avatar
Elliot Lee committed
974
      else if (streql("pixels_per_unit", argv[i]))
975
        slot = &cp->pixels_per_unit;
Elliot Lee's avatar
Elliot Lee committed
976
      else if (streql("spatial_filter_radius", argv[i]))
977
        slot = &cp->spatial_filter_radius;
Elliot Lee's avatar
Elliot Lee committed
978
      else if (streql("sample_density", argv[i]))
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
        slot = &cp->sample_density;
      else if (streql("nbatches", argv[i]))
        {
          slot = &nbatches;
          set_nbatches = 1;
        }
      else if (streql("white_level", argv[i]))
        {
          slot = &white_level;
          set_white_level = 1;
        }
      else if (streql("spatial_oversample", argv[i]))
        {
          slot = &spatial_oversample;
          set_spatial_oversample = 1;
        }
      else if (streql("cmap", argv[i]))
        {
          slot = &cm;
          set_cm = 1;
        }
      else if (streql("density", argv[i]))
        slot = &cp->xform[(int)xf].density;
Elliot Lee's avatar
Elliot Lee committed
1002
      else if (streql("color", argv[i]))
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
        slot = &cp->xform[(int)xf].color;
      else if (streql("coefs", argv[i]))
        {
          slot = cp->xform[(int)xf].c[0];
          cp->xform[(int)xf].density = 1.0;
        }
      else if (streql("var", argv[i]))
        slot = cp->xform[(int)xf].var;
      else if (streql("cmap_inter", argv[i]))
        {
          slot = &cmap_inter;
          set_cmap_inter = 1;
        }
      else
1017
        *slot++ = g_strtod(argv[i], NULL);
1018 1019 1020
    }
  if (set_cm)
    {
Elliot Lee's avatar
Elliot Lee committed
1021 1022
      cp->cmap_index = (int) cm;
      get_cmap(cp->cmap_index, cp->cmap, 256);
1023 1024 1025
    }
  if (set_image_size)
    {
Elliot Lee's avatar
Elliot Lee committed
1026 1027
      cp->width  = (int) image_size[0];
      cp->height = (int) image_size[1];
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    }
  if (set_cmap_inter)
    cp->cmap_inter  = (int) cmap_inter;
  if (set_nbatches)
    cp->nbatches = (int) nbatches;
  if (set_spatial_oversample)
    cp->spatial_oversample = (int) spatial_oversample;
  if (set_white_level)
    cp->white_level = (int) white_level;
  for (i = 0; i < NXFORMS; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
1039 1040
      t = 0.0;
      for (j = 0; j < NVARS; j++)
1041
        t += cp->xform[i].var[j];
Elliot Lee's avatar
Elliot Lee committed
1042 1043
      t = 1.0 / t;
      for (j = 0; j < NVARS; j++)
1044 1045 1046
        cp->xform[i].var[j] *= t;
    }
  qsort ((char *) cp->xform, NXFORMS, sizeof(xform), compare_xforms);
Elliot Lee's avatar
Elliot Lee committed
1047 1048
}

1049 1050 1051 1052
void
print_control_point (FILE          *f,
                     control_point *cp,
                     int            quote)
Elliot Lee's avatar
Elliot Lee committed
1053 1054 1055
{
  int i, j;
  char *q = quote ? "# " : "";
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
  fprintf (f, "%stime %g\n", q, cp->time);
  if (cp->cmap_index != -1)
    fprintf (f, "%scmap %d\n", q, cp->cmap_index);
  fprintf (f, "%simage_size %d %d center %g %g pixels_per_unit %g\n",
           q, cp->width, cp->height, cp->center[0], cp->center[1],
           cp->pixels_per_unit);
  fprintf (f, "%sspatial_oversample %d spatial_filter_radius %g",
           q, cp->spatial_oversample, cp->spatial_filter_radius);
  fprintf (f, " sample_density %g\n", cp->sample_density);
  fprintf (f, "%snbatches %d white_level %d\n",
           q, cp->nbatches, cp->white_level);
  fprintf (f, "%sbrightness %g gamma %g cmap_inter %d\n",
           q, cp->brightness, cp->gamma, cp->cmap_inter);
Elliot Lee's avatar
Elliot Lee committed
1069 1070

  for (i = 0; i < NXFORMS; i++)
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    if (cp->xform[i].density > 0.0)
      {
        fprintf (f, "%sxform %d density %g color %g\n",
                 q, i, cp->xform[i].density, cp->xform[i].color);
        fprintf (f, "%svar", q);
        for (j = 0; j < NVARS; j++)
          fprintf (f, " %g", cp->xform[i].var[j]);
        fprintf (f, "\n%scoefs", q);
        for (j = 0; j < 3; j++)
          fprintf (f, " %g %g", cp->xform[i].c[j][0], cp->xform[i].c[j][1]);
        fprintf (f, "\n");
      }
  fprintf (f, "%s;\n", q);
Elliot Lee's avatar
Elliot Lee committed
1084 1085 1086
}

/* returns a uniform variable from 0 to 1 */
1087 1088 1089 1090
double
random_uniform01 (void)
{
  return g_random_double ();
Elliot Lee's avatar
Elliot Lee committed
1091 1092
}

1093 1094 1095
double random_uniform11 (void)
{
  return g_random_double_range (-1, 1);
Elliot Lee's avatar
Elliot Lee committed
1096 1097 1098 1099
}

/* returns a mean 0 variance 1 random variable
   see numerical recipies p 217 */
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
double random_gaussian(void)
{
  static int    iset = 0;
  static double gset;
  double fac, r, v1, v2;

  if (iset == 0)
    {
      do
        {
          v1 = random_uniform11 ();
          v2 = random_uniform11 ();
          r = v1 * v1 + v2 * v2;
        }
      while (r >= 1.0 || r == 0.0);
      fac = sqrt (-2.0 * log (r) / r);
Elliot Lee's avatar
Elliot Lee committed
1116 1117 1118
      gset = v1 * fac;
      iset = 1;
      return v2 * fac;
1119 1120 1121
    }
  iset = 0;
  return gset;
Elliot Lee's avatar
Elliot Lee committed
1122 1123 1124
}

void
1125 1126 1127
copy_variation (control_point *cp0,
                control_point *cp1)
{
Elliot Lee's avatar
Elliot Lee committed
1128
  int i, j;
1129 1130 1131 1132 1133
  for (i = 0; i < NXFORMS; i++)
    {
      for (j = 0; j < NVARS; j++)
        cp0->xform[i].var[j] = cp1->xform[i].var[j];
    }
Elliot Lee's avatar
Elliot Lee committed
1134 1135
}

1136
#define random_distrib(v) ((v)[g_random_int_range (0, vlen(v))])
Elliot Lee's avatar
Elliot Lee committed
1137

1138 1139 1140
void
random_control_point (control_point *cp,
                      int            ivar)
Elliot Lee's avatar
Elliot Lee committed
1141
{
1142 1143 1144
  int i, nxforms, var;
  static int xform_distrib[] =
    {
Elliot Lee's avatar
Elliot Lee committed
1145 1146 1147
      2, 2, 2,
      3, 3, 3,
      4, 4,
1148 1149 1150 1151
      5
    };
  static int var_distrib[] =
    {
Elliot Lee's avatar
Elliot Lee committed
1152 1153 1154 1155 1156 1157
      -1, -1, -1,
      0, 0, 0, 0,
      1, 1, 1,
      2, 2, 2,
      3, 3,
      4, 4,
1158 1159
      5
    };
Elliot Lee's avatar
Elliot Lee committed
1160

1161 1162
  static int mixed_var_distrib[] =
    {
Elliot Lee's avatar
Elliot Lee committed
1163 1164 1165 1166 1167
      0, 0, 0,
      1, 1, 1,
      2, 2, 2,
      3, 3,
      4, 4,
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
      5, 5
    };

  get_cmap (cmap_random, cp->cmap, 256);
  cp->time = 0.0;
  nxforms = random_distrib (xform_distrib);
  var = (0 > ivar) ?
      random_distrib(var_distrib) :
      ivar;
  for (i = 0; i < nxforms; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
1179 1180 1181 1182
      int j, k;
      cp->xform[i].density = 1.0 / nxforms;
      cp->xform[i].color = i == 0;
      for (j = 0; j < 3; j++)
1183 1184
        for (k = 0; k < 2; k++)
          cp->xform[i].c[j][k] = random_uniform11();
Elliot Lee's avatar
Elliot Lee committed
1185
      for (j = 0; j < NVARS; j++)
1186
        cp->xform[i].var[j] = 0.0;
Elliot Lee's avatar
Elliot Lee committed
1187
      if (var >= 0)
1188
        cp->xform[i].var[var] = 1.0;
Elliot Lee's avatar
Elliot Lee committed
1189
      else
1190
        cp->xform[i].var[random_distrib(mixed_var_distrib)] = 1.0;
1191

1192 1193 1194
    }
  for (; i < NXFORMS; i++)
    cp->xform[i].density = 0.0;
Elliot Lee's avatar
Elliot Lee committed
1195 1196 1197 1198 1199 1200 1201
}

/*
 * find a 2d bounding box that does not enclose eps of the fractal density
 * in each compass direction.  works by binary search.
 * this is stupid, it shouldjust use the find nth smallest algorithm.
 */
1202 1203 1204 1205 1206
void
estimate_bounding_box (control_point *cp,
                       double         eps,
                       double        *bmin,
                       double        *bmax)
Elliot Lee's avatar
Elliot Lee committed
1207
{
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  int    i, j, batch = (eps == 0.0) ? 10000 : 10.0/eps;
  int    low_target = batch * eps;
  int    high_target = batch - low_target;
  point  min, max, delta;
  point *points = malloc (sizeof (point) * batch);
  iterate (cp, batch, 20, points);

  min[0] = min[1] =  1e10;
  max[0] = max[1] = -1e10;

  for (i = 0; i < batch; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
1220 1221 1222 1223
      if (points[i][0] < min[0]) min[0] = points[i][0];
      if (points[i][1] < min[1]) min[1] = points[i][1];
      if (points[i][0] > max[0]) max[0] = points[i][0];
      if (points[i][1] > max[1]) max[1] = points[i][1];
1224
    }
Elliot Lee's avatar
Elliot Lee committed
1225

1226 1227
  if (low_target == 0)
    {
Elliot Lee's avatar
Elliot Lee committed
1228 1229 1230 1231 1232
      bmin[0] = min[0];
      bmin[1] = min[1];
      bmax[0] = max[0];
      bmax[1] = max[1];
      return;
1233
    }
1234

1235 1236
  delta[0] = (max[0] - min[0]) * 0.25;
  delta[1] = (max[1] - min[1]) * 0.25;
Elliot Lee's avatar
Elliot Lee committed
1237

1238 1239
  bmax[0] = bmin[0] = min[0] + 2.0 * delta[0];
  bmax[1] = bmin[1] = min[1] + 2.0 * delta[1];
Elliot Lee's avatar
Elliot Lee committed
1240

1241 1242
  for (i = 0; i < 14; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
1243 1244
      int n, s, e, w;
      n = s = e = w = 0;
1245 1246 1247 1248 1249 1250 1251
      for (j = 0; j < batch; j++)
        {
          if (points[j][0] < bmin[0]) n++;
          if (points[j][0] > bmax[0]) s++;
          if (points[j][1] < bmin[1]) w++;
          if (points[j][1] > bmax[1]) e++;
        }
Elliot Lee's avatar
Elliot Lee committed
1252 1253 1254 1255 1256 1257
      bmin[0] += (n <  low_target) ? delta[0] : -delta[0];
      bmax[0] += (s < high_target) ? delta[0] : -delta[0];
      bmin[1] += (w <  low_target) ? delta[1] : -delta[1];
      bmax[1] += (e < high_target) ? delta[1] : -delta[1];
      delta[0] = delta[0] / 2.0;
      delta[1] = delta[1] / 2.0;
1258
    }
Elliot Lee's avatar
Elliot Lee committed
1259 1260 1261 1262
}

/* this has serious flaws in it */

1263 1264 1265
double
standard_metric (control_point *cp1,
                 control_point *cp2)
Elliot Lee's avatar
Elliot Lee committed
1266
{
1267 1268 1269
  int    i, j, k;
  double t;
  double dist = 0.0;
1270

1271 1272
  for (i = 0; i < NXFORMS; i++)
    {
Elliot Lee's avatar
Elliot Lee committed
1273 1274
      double var_dist = 0.0;
      double coef_dist = 0.0;
1275 1276 1277 1278 1279
      for (j = 0; j < NVARS; j++)
        {
          t = cp1->xform[i].var[j] - cp2->xform[i].var[j];
          var_dist += t * t;
        }
Elliot Lee's avatar
Elliot Lee committed
1280
      for (j = 0; j < 3; j++)
1281 1282 1283 1284 1285
        for (k = 0; k < 2; k++)
          {
            t = cp1->xform[i].c[j][k] - cp2->xform[i].c[j][k];
            coef_dist += t *t;
          }
Elliot Lee's avatar
Elliot Lee committed
1286 1287
      /* weight them equally for now. */
      dist += var_dist + coef_dist;
1288 1289
    }
  return dist;
Elliot Lee's avatar
Elliot Lee committed
1290
}
1291

1292 1293
static int
flam3_random_bit (void)
1294
{
1295 1296 1297 1298 1299
  static int n = 0;
  static int l;
  if (n == 0)
    {
      l = g_random_int ();
1300
      n = 20;
1301 1302 1303
    }
  else
    {
1304 1305
      l = l >> 1;
      n--;
1306 1307
    }
  return l & 1;
1308 1309
}

1310 1311
static double
flam3_random01 (void)
1312
{
1313
  return (g_random_int () & 0xfffffff) / (double) 0xfffffff;
1314
}