~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/2geom/math-utils.h

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file
 
3
 * \brief Low level math functions and compatibility wrappers
 
4
 *//*
 
5
 * Authors:
 
6
 *   Johan Engelen <goejendaagh@zonnet.nl>
 
7
 *   Michael G. Sloan <mgsloan@gmail.com>
 
8
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 
9
 * Copyright 2006-2009 Authors
 
10
 *
 
11
 * This library is free software; you can redistribute it and/or
 
12
 * modify it either under the terms of the GNU Lesser General Public
 
13
 * License version 2.1 as published by the Free Software Foundation
 
14
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 
15
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 
16
 * notice, a recipient may use your version of this file under either
 
17
 * the MPL or the LGPL.
 
18
 *
 
19
 * You should have received a copy of the LGPL along with this library
 
20
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
22
 * You should have received a copy of the MPL along with this library
 
23
 * in the file COPYING-MPL-1.1
 
24
 *
 
25
 * The contents of this file are subject to the Mozilla Public License
 
26
 * Version 1.1 (the "License"); you may not use this file except in
 
27
 * compliance with the License. You may obtain a copy of the License at
 
28
 * http://www.mozilla.org/MPL/
 
29
 *
 
30
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 
31
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 
32
 * the specific language governing rights and limitations.
 
33
 *
 
34
 */
 
35
 
 
36
#ifndef LIB2GEOM_SEEN_MATH_UTILS_H
 
37
#define LIB2GEOM_SEEN_MATH_UTILS_H
 
38
 
 
39
#include "config.h"
 
40
#include <math.h> // sincos is usually only available in math.h
 
41
#include <cmath>
 
42
#include <utility> // for std::pair
 
43
 
 
44
namespace Geom {
 
45
 
 
46
/** @brief Sign function - indicates the sign of a numeric type.
 
47
 * Mathsy people will know this is basically the derivative of abs, except for the fact
 
48
 * that it is defined on 0.
 
49
 * @return -1 when x is negative, 1 when positive, and 0 if equal to 0. */
 
50
template <class T> inline int sgn(const T& x) {
 
51
    return (x < 0 ? -1 : (x > 0 ? 1 : 0) );
 
52
}
 
53
 
 
54
template <class T> inline T sqr(const T& x) {return x * x;}
 
55
template <class T> inline T cube(const T& x) {return x * x * x;}
 
56
 
 
57
/** Between function - returns true if a number x is within a range: (min < x) && (max > x).
 
58
 * The values delimiting the range and the number must have the same type.
 
59
 */
 
60
template <class T> inline const T& between (const T& min, const T& max, const T& x)
 
61
    { return (min < x) && (max > x); }
 
62
 
 
63
/** @brief Returns @a x rounded to the nearest multiple of \f$10^{p}\f$.
 
64
 
 
65
    Implemented in terms of round, i.e. we make no guarantees as to what happens if x is
 
66
    half way between two rounded numbers.
 
67
    
 
68
    Note: places is the number of decimal places without using scientific (e) notation, not the
 
69
    number of significant figures.  This function may not be suitable for values of x whose
 
70
    magnitude is so far from 1 that one would want to use scientific (e) notation.
 
71
 
 
72
    places may be negative: e.g. places = -2 means rounding to a multiple of .01
 
73
**/
 
74
inline double decimal_round(double x, int p) {
 
75
    //TODO: possibly implement with modulus instead?
 
76
    double const multiplier = ::pow(10.0, p);
 
77
    return ::round( x * multiplier ) / multiplier;
 
78
}
 
79
 
 
80
/** @brief Simultaneously compute a sine and a cosine of the same angle.
 
81
 * This function can be up to 2 times faster than separate computation, depending
 
82
 * on the platform. It uses the standard library function sincos() if available.
 
83
 * @param angle Angle
 
84
 * @param sin_ Variable that will store the sine
 
85
 * @param cos_ Variable that will store the cosine */
 
86
inline void sincos(double angle, double &sin_, double &cos_) {
 
87
#ifdef HAVE_SINCOS
 
88
    ::sincos(angle, &sin_, &cos_);
 
89
#else
 
90
    sin_ = ::sin(angle);
 
91
    cos_ = ::cos(angle);
 
92
#endif
 
93
}
 
94
 
 
95
/* Temporary fix for various misdefinitions of isnan().
 
96
 * isnan() is becoming undef'd in some .h files. 
 
97
 * #include this last in your .cpp file to get it right.
 
98
 *
 
99
 * The problem is that isnan and isfinite are part of C99 but aren't part of
 
100
 * the C++ standard (which predates C99).
 
101
 */
 
102
 
 
103
#if defined(__isnan)
 
104
# define IS_NAN(_a) (__isnan(_a))
 
105
#elif defined(__APPLE__) && __GNUC__ == 3
 
106
# define IS_NAN(_a) (__isnan(_a))       /* MacOSX/Darwin definition < 10.4 */
 
107
#elif defined(WIN32) || defined(_isnan)
 
108
# define IS_NAN(_a) (_isnan(_a))        /* Win32 definition */
 
109
#elif defined(isnan) || defined(__FreeBSD__) || defined(__osf__)
 
110
# define IS_NAN(_a) (isnan(_a))         /* GNU definition */
 
111
#elif defined (SOLARIS_2_8) && __GNUC__ == 3 && __GNUC_MINOR__ == 2
 
112
# define IS_NAN(_a) (isnan(_a))         /* GNU definition */
 
113
#else
 
114
# define IS_NAN(_a) (std::isnan(_a))
 
115
#endif
 
116
/* If the above doesn't work, then try (a != a). */
 
117
 
 
118
 
 
119
#if defined(__isfinite)
 
120
# define IS_FINITE(_a) (__isfinite(_a))
 
121
#elif defined(__APPLE__) && __GNUC__ == 3
 
122
# define IS_FINITE(_a) (__isfinite(_a)) /* MacOSX/Darwin definition < 10.4 */
 
123
#elif defined(__sgi)
 
124
# define IS_FINITE(_a) (_isfinite(_a))
 
125
#elif defined(isfinite)
 
126
# define IS_FINITE(_a) (isfinite(_a))
 
127
#elif defined(__osf__)
 
128
# define IS_FINITE(_a) (finite(_a) && !IS_NAN(_a))
 
129
#elif defined (SOLARIS_2_8) && __GNUC__ == 3 && __GNUC_MINOR__ == 2
 
130
#include  <ieeefp.h>
 
131
#define IS_FINITE(_a) (finite(_a) && !IS_NAN(_a))
 
132
#else
 
133
# define IS_FINITE(_a) (std::isfinite(_a))
 
134
#endif
 
135
 
 
136
} // end namespace Geom
 
137
 
 
138
#endif // LIB2GEOM_SEEN_MATH_UTILS_H
 
139
 
 
140
/*
 
141
  Local Variables:
 
142
  mode:c++
 
143
  c-file-style:"stroustrup"
 
144
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
145
  indent-tabs-mode:nil
 
146
  fill-column:99
 
147
  End:
 
148
*/
 
149
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :