LibreOffice Module basegfx (master) 1
ftools.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21
22namespace basegfx
23{
24 double snapToNearestMultiple(double v, const double fStep)
25 {
26 if(fTools::equalZero(fStep))
27 {
28 // with a zero step, all snaps to 0.0
29 return 0.0;
30 }
31 else
32 {
33 const double fHalfStep(fStep * 0.5);
34 const double fChange(fHalfStep - fmod(v + fHalfStep, fStep));
35
36 if(basegfx::fTools::equal(fabs(v), fabs(fChange)))
37 {
38 return 0.0;
39 }
40 else
41 {
42 return v + fChange;
43 }
44 }
45 }
46
47 double snapToZeroRange(double v, double fWidth)
48 {
49 if(fTools::equalZero(fWidth))
50 {
51 // with no range all snaps to range bound
52 return 0.0;
53 }
54 else
55 {
56 if(v < 0.0 || v > fWidth)
57 {
58 double fRetval(fmod(v, fWidth));
59
60 if(fRetval < 0.0)
61 {
62 fRetval += fWidth;
63 }
64
65 return fRetval;
66 }
67 else
68 {
69 return v;
70 }
71 }
72 }
73
74 double snapToRange(double v, double fLow, double fHigh)
75 {
76 if(fTools::equal(fLow, fHigh))
77 {
78 // with no range all snaps to range bound
79 return 0.0;
80 }
81 else
82 {
83 if(fLow > fHigh)
84 {
85 // correct range order. Evtl. assert this (?)
86 std::swap(fLow, fHigh);
87 }
88
89 if(v < fLow || v > fHigh)
90 {
91 return snapToZeroRange(v - fLow, fHigh - fLow) + fLow;
92 }
93 else
94 {
95 return v;
96 }
97 }
98 }
99
100 double normalizeToRange(double v, const double fRange)
101 {
102 if(fTools::lessOrEqual(fRange, 0.0))
103 {
104 // with a zero (or less) range, all normalizes to 0.0
105 return 0.0;
106 }
107
108 const bool bNegative(fTools::less(v, 0.0));
109
110 if(bNegative)
111 {
112 if(fTools::moreOrEqual(v, -fRange))
113 {
114 // in range [-fRange, 0.0[, shift one step
115 return v + fRange;
116 }
117
118 // re-calculate
119 return v - (floor(v/fRange)*fRange);
120 }
121 else
122 {
123 if(fTools::less(v, fRange))
124 {
125 // already in range [0.0, fRange[, nothing to do
126 return v;
127 }
128
129 // re-calculate
130 return v - (floor(v/fRange)*fRange);
131 }
132 }
133} // end of namespace basegfx
134
135/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
float v
bool lessOrEqual(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:188
bool equalZero(const T &rfVal)
Compare against small value.
Definition: ftools.hxx:156
bool equal(T const &rfValA, T const &rfValB)
Definition: ftools.hxx:169
bool less(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:182
bool moreOrEqual(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:200
double snapToNearestMultiple(double v, const double fStep)
Snap v to nearest multiple of fStep, from negative and positive side.
Definition: ftools.cxx:24
double normalizeToRange(double v, const double fRange)
RotateFlyFrame3: Normalize to range defined by [0.0 ... fRange[, independent if v is positive or nega...
Definition: ftools.cxx:100
double snapToRange(double v, double fLow, double fHigh)
Snap v to the range [fLow .
Definition: ftools.cxx:74
double snapToZeroRange(double v, double fWidth)
Snap v to the range [0.0 .
Definition: ftools.cxx:47