Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
math-utils.h
Go to the documentation of this file.
1/*
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 <array>
41#include <cmath>
42#include <utility> // for std::pair
43#include <boost/math/special_functions/fpclassify.hpp>
44
45namespace Geom {
46
51template <class T> inline int sgn(const T& x) {
52 return (x < 0 ? -1 : (x > 0 ? 1 : 0) );
53// can we 'optimize' this with:
54// return ( (T(0) < x) - (x < T(0)) );
55}
56
57template <class T> inline T sqr(const T& x) {return x * x;}
58template <class T> inline T cube(const T& x) {return x * x * x;}
59
63template <class T> inline const T& between (const T& min, const T& max, const T& x)
64 { return (min < x) && (max > x); }
65
77inline double decimal_round(double x, int p) {
78 //TODO: possibly implement with modulus instead?
79 double const multiplier = ::pow(10.0, p);
80 return ::round( x * multiplier ) / multiplier;
81}
82
89inline void sincos(double angle, double &sin_, double &cos_) {
90#ifdef HAVE_SINCOS
91 ::sincos(angle, &sin_, &cos_);
92#else
93 sin_ = ::sin(angle);
94 cos_ = ::cos(angle);
95#endif
96}
97
107template <size_t N>
108inline int rescale_homogenous(std::array<double, N> &values)
109{
110 if constexpr (N == 0) {
111 return 0;
112 }
113 std::array<int, N> exponents;
114 std::array<double, N> mantissas;
115 int average = 0;
116 for (size_t i = 0; i < N; i++) {
117 mantissas[i] = std::frexp(values[i], &exponents[i]);
118 average += exponents[i];
119 }
120 average /= (int)N;
121 for (size_t i = 0; i < N; i++) {
122 values[i] = std::ldexp(mantissas[i], exponents[i] - average);
123 }
124 return -average;
125}
126
127} // end namespace Geom
128
129#endif // LIB2GEOM_SEEN_MATH_UTILS_H
130
131/*
132 Local Variables:
133 mode:c++
134 c-file-style:"stroustrup"
135 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
136 indent-tabs-mode:nil
137 fill-column:99
138 End:
139*/
140// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Various utility functions.
Definition affine.h:22
SBasisN< n > cos(LinearN< n > bo, int k)
void sincos(double angle, double &sin_, double &cos_)
Simultaneously compute a sine and a cosine of the same angle.
Definition math-utils.h:89
int sgn(const T &x)
Sign function - indicates the sign of a numeric type.
Definition math-utils.h:51
MultiDegree< n > max(MultiDegree< n > const &p, MultiDegree< n > const &q)
Returns the maximal degree appearing in the two arguments for each variables.
Definition sbasisN.h:158
const T & between(const T &min, const T &max, const T &x)
Between function - returns true if a number x is within a range: (min < x) && (max > x).
Definition math-utils.h:63
T cube(const T &x)
Definition math-utils.h:58
double decimal_round(double x, int p)
Returns x rounded to the nearest multiple of .
Definition math-utils.h:77
int rescale_homogenous(std::array< double, N > &values)
Scale the doubles in the passed array to make them "reasonably large".
Definition math-utils.h:108
T sqr(const T &x)
Definition math-utils.h:57
Piecewise< SBasis > min(SBasis const &f, SBasis const &g)
Return the more negative of the two functions pointwise.
SBasisN< n > sin(LinearN< n > bo, int k)
size_t N