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 <math.h> // sincos is usually only available in math.h
40 #include <cmath>
41 #include <utility> // for std::pair
42 #include <boost/math/special_functions/fpclassify.hpp>
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. */
sgn(const T & x)50 template <class T> inline int sgn(const T& x) {
51 return (x < 0 ? -1 : (x > 0 ? 1 : 0) );
52 // can we 'optimize' this with:
53 // return ( (T(0) < x) - (x < T(0)) );
54 }
55
sqr(const T & x)56 template <class T> inline T sqr(const T& x) {return x * x;}
cube(const T & x)57 template <class T> inline T cube(const T& x) {return x * x * x;}
58
59 /** Between function - returns true if a number x is within a range: (min < x) && (max > x).
60 * The values delimiting the range and the number must have the same type.
61 */
between(const T & min,const T & max,const T & x)62 template <class T> inline const T& between (const T& min, const T& max, const T& x)
63 { return (min < x) && (max > x); }
64
65 /** @brief Returns @a x rounded to the nearest multiple of \f$10^{p}\f$.
66
67 Implemented in terms of round, i.e. we make no guarantees as to what happens if x is
68 half way between two rounded numbers.
69
70 Note: places is the number of decimal places without using scientific (e) notation, not the
71 number of significant figures. This function may not be suitable for values of x whose
72 magnitude is so far from 1 that one would want to use scientific (e) notation.
73
74 places may be negative: e.g. places = -2 means rounding to a multiple of .01
75 **/
decimal_round(double x,int p)76 inline double decimal_round(double x, int p) {
77 //TODO: possibly implement with modulus instead?
78 double const multiplier = ::pow(10.0, p);
79 return ::round( x * multiplier ) / multiplier;
80 }
81
82 /** @brief Simultaneously compute a sine and a cosine of the same angle.
83 * This function can be up to 2 times faster than separate computation, depending
84 * on the platform. It uses the standard library function sincos() if available.
85 * @param angle Angle
86 * @param sin_ Variable that will store the sine
87 * @param cos_ Variable that will store the cosine */
sincos(double angle,double & sin_,double & cos_)88 inline void sincos(double angle, double &sin_, double &cos_) {
89 #ifdef HAVE_SINCOS
90 ::sincos(angle, &sin_, &cos_);
91 #else
92 sin_ = ::sin(angle);
93 cos_ = ::cos(angle);
94 #endif
95 }
96
97 } // end namespace Geom
98
99 #endif // LIB2GEOM_SEEN_MATH_UTILS_H
100
101 /*
102 Local Variables:
103 mode:c++
104 c-file-style:"stroustrup"
105 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
106 indent-tabs-mode:nil
107 fill-column:99
108 End:
109 */
110 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
111