SplashMath.h 1.78 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4 5 6 7 8 9
//========================================================================
//
// SplashMath.h
//
//========================================================================

#ifndef SPLASHMATH_H
#define SPLASHMATH_H

10
#if USE_FIXEDPOINT
11
#include "goo/FixedPoint.h"
12
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
13
#include <math.h>
14
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
15 16 17
#include "SplashTypes.h"

static inline SplashCoord splashAbs(SplashCoord x) {
18 19 20
#if USE_FIXEDPOINT
  return FixedPoint::abs(x);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
21
  return fabs(x);
22
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
23 24 25
}

static inline int splashFloor(SplashCoord x) {
26 27 28
  #if USE_FIXEDPOINT
    return FixedPoint::floor(x);
  #else
29 30
    if (x > 0) return (int)x;
    else return (int)floor(x);
31
  #endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
32 33 34
}

static inline int splashCeil(SplashCoord x) {
35 36 37
#if USE_FIXEDPOINT
  return FixedPoint::ceil(x);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
38
  return (int)ceil(x);
39
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
40 41 42
}

static inline int splashRound(SplashCoord x) {
43 44 45
#if USE_FIXEDPOINT
  return FixedPoint::round(x);
#else
46
  return (int)splashFloor(x + 0.5);
47
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
48 49 50
}

static inline SplashCoord splashSqrt(SplashCoord x) {
51 52 53
#if USE_FIXEDPOINT
  return FixedPoint::sqrt(x);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
54
  return sqrt(x);
55
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
56 57 58
}

static inline SplashCoord splashPow(SplashCoord x, SplashCoord y) {
59 60 61
#if USE_FIXEDPOINT
  return FixedPoint::pow(x, y);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
62
  return pow(x, y);
63
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
64 65 66 67 68 69 70
}

static inline SplashCoord splashDist(SplashCoord x0, SplashCoord y0,
				     SplashCoord x1, SplashCoord y1) {
  SplashCoord dx, dy;
  dx = x1 - x0;
  dy = y1 - y0;
71
#if USE_FIXEDPOINT
72 73 74 75 76 77 78 79 80 81 82 83
  // this handles the situation where dx*dx or dy*dy is too large to
  // fit in the 16.16 fixed point format
  SplashCoord dxa, dya;
  dxa = splashAbs(dx);
  dya = splashAbs(dy);
  if (dxa == 0 && dya == 0) {
    return 0;
  } else if (dxa > dya) {
    return dxa * FixedPoint::sqrt(dya / dxa + 1);
  } else {
    return dya * FixedPoint::sqrt(dxa / dya + 1);
  }
84
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
85
  return sqrt(dx * dx + dy * dy);
86
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
87 88 89
}

#endif