Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
mathfns.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
13#ifndef INKSCAPE_HELPER_MATHFNS_H
14#define INKSCAPE_HELPER_MATHFNS_H
15
16#include <cmath>
17#include <2geom/point.h>
18
19namespace Inkscape {
20namespace Util {
21
29inline double round_to_nearest_multiple_plus(double x, double c1, double c0)
30{
31 return std::floor((x - c0) / c1 + 0.5) * c1 + c0;
32}
33
41inline double round_to_lower_multiple_plus(double x, double c1, double c0 = 0.0)
42{
43 return std::floor((x - c0) / c1) * c1 + c0;
44}
45
53inline double round_to_upper_multiple_plus(double x, double const c1, double const c0 = 0)
54{
55 return std::ceil((x - c0) / c1) * c1 + c0;
56}
57
59// Note: This is a naive implementation.
60// Todo: (C++20) Replace with std::bit_floor.
61template <typename T>
62int constexpr floorlog2(T x)
63{
64 int n = -1;
65 while (x > 0) {
66 x /= 2;
67 n++;
68 }
69 return n;
70}
71
73template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
74T constexpr safemod(T a, T b)
75{
76 a %= b;
77 return a < 0 ? a + b : a;
78}
79
81template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
82T constexpr round_down(T a, T b)
83{
84 return a - safemod(a, b);
85}
86
88template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
89T constexpr round_up(T a, T b)
90{
91 return round_down(a - 1, b) + b;
92}
93
98template <typename T>
99T safeclamp(T val, T lo, T hi)
100{
101 if (val < lo) return lo;
102 if (val > hi) return hi;
103 return val;
104}
105
106} // namespace Util
107} // namespace Inkscape
108
109#endif // INKSCAPE_HELPER_MATHFNS_H
110
111/*
112 Local Variables:
113 mode:c++
114 c-file-style:"stroustrup"
115 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
116 indent-tabs-mode:nil
117 fill-column:99
118 End:
119*/
120// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
Miscellaneous supporting code.
Definition document.h:93
T safeclamp(T val, T lo, T hi)
Just like std::clamp, except it doesn't deliberately crash if lo > hi due to rounding errors,...
Definition mathfns.h:99
int constexpr floorlog2(T x)
Returns floor(log_2(x)), assuming x >= 1.
Definition mathfns.h:62
T constexpr safemod(T a, T b)
Returns a mod b, always in the range 0..b-1, assuming b >= 1.
Definition mathfns.h:74
T constexpr round_down(T a, T b)
Returns a rounded down to the nearest multiple of b, assuming b >= 1.
Definition mathfns.h:82
T constexpr round_up(T a, T b)
Returns a rounded up to the nearest multiple of b, assuming b >= 1.
Definition mathfns.h:89
double round_to_upper_multiple_plus(double x, double const c1, double const c0=0)
Definition mathfns.h:53
double round_to_nearest_multiple_plus(double x, double c1, double c0)
Definition mathfns.h:29
double round_to_lower_multiple_plus(double x, double c1, double c0=0.0)
Definition mathfns.h:41
Helper class to stream background task notifications as a series of messages.