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

7 8 9 10 11 12 13
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
14
// Copyright (C) 2009-2011 Albert Astals Cid <aacid@kde.org>
15 16 17 18 19 20
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

Kristian Høgsberg's avatar
Kristian Høgsberg committed
21 22 23
#ifndef SPLASHMATH_H
#define SPLASHMATH_H

24 25
#include "poppler/poppler-config.h"

26
#if USE_FIXEDPOINT
27
#include "goo/FixedPoint.h"
28
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
29
#include <math.h>
30
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
31 32 33
#include "SplashTypes.h"

static inline SplashCoord splashAbs(SplashCoord x) {
34 35
#if USE_FIXEDPOINT
  return FixedPoint::abs(x);
36 37
#elif USE_FLOAT
  return fabsf(x);
38
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
39
  return fabs(x);
40
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
41 42 43
}

static inline int splashFloor(SplashCoord x) {
44 45
  #if USE_FIXEDPOINT
    return FixedPoint::floor(x);
46 47
  #elif USE_FLOAT
    return (int)floorf(x);
48
  #else
49 50
    if (x > 0) return (int)x;
    else return (int)floor(x);
51
  #endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
52 53 54
}

static inline int splashCeil(SplashCoord x) {
55 56
#if USE_FIXEDPOINT
  return FixedPoint::ceil(x);
57 58
#elif USE_FLOAT
  return (int)ceilf(x);
59
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
60
  return (int)ceil(x);
61
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
62 63 64
}

static inline int splashRound(SplashCoord x) {
65 66 67
#if USE_FIXEDPOINT
  return FixedPoint::round(x);
#else
68
  return (int)splashFloor(x + 0.5);
69
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
70 71 72
}

static inline SplashCoord splashSqrt(SplashCoord x) {
73 74
#if USE_FIXEDPOINT
  return FixedPoint::sqrt(x);
75 76
#elif USE_FLOAT
  return sqrtf(x);
77
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
78
  return sqrt(x);
79
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
80 81 82
}

static inline SplashCoord splashPow(SplashCoord x, SplashCoord y) {
83 84
#if USE_FIXEDPOINT
  return FixedPoint::pow(x, y);
85 86
#elif USE_FLOAT
  return powf(x, y);
87
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
88
  return pow(x, y);
89
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
90 91 92 93 94 95 96
}

static inline SplashCoord splashDist(SplashCoord x0, SplashCoord y0,
				     SplashCoord x1, SplashCoord y1) {
  SplashCoord dx, dy;
  dx = x1 - x0;
  dy = y1 - y0;
97
#if USE_FIXEDPOINT
98 99
  // this handles the situation where dx*dx or dy*dy is too large to
  // fit in the 16.16 fixed point format
100
  SplashCoord dxa, dya, d;
101 102 103 104 105
  dxa = splashAbs(dx);
  dya = splashAbs(dy);
  if (dxa == 0 && dya == 0) {
    return 0;
  } else if (dxa > dya) {
106 107
    d = dya / dxa;
    return dxa * FixedPoint::sqrt(d*d + 1);
108
  } else {
109 110
    d = dxa / dya;
    return dya * FixedPoint::sqrt(d*d + 1);
111
  }
112
#else
113
  return splashSqrt(dx * dx + dy * dy);
114
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
115 116
}

117 118 119 120 121 122 123 124 125 126
static inline GBool splashCheckDet(SplashCoord m11, SplashCoord m12,
				   SplashCoord m21, SplashCoord m22,
				   SplashCoord epsilon) {
#if USE_FIXEDPOINT
  return FixedPoint::checkDet(m11, m12, m21, m22, epsilon);
#else
  return fabs(m11 * m22 - m12 * m21) >= epsilon;
#endif
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
127
#endif