UFO: Alien Invasion
Loading...
Searching...
No Matches
mathlib_extra.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23*/
24
25#include "mathlib_extra.h"
26#include <math.h>
27
36double FpCurveUp (double fpVal, double mEffect)
37{
38 /* If there isn't any "curve" to skew fpVal with (flat, straight line), we can stop right here (no change). */
39 if (mEffect == 0.0)
40 return fpVal;
41
42 /* Safe values only please, to avoid breaking things */
43 fpVal = std::min(DENORM_INV, std::max(DENORM, fpVal));
44 mEffect = std::min(DENORM_INV, std::max(DENORM, mEffect));
45
46 fpVal = ((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fpVal);
47
48 return fpVal;
49}
50
59double FpCurveDn (double fpVal, double mEffect)
60{
61 /* If there isn't any "curve" to skew fpVal with (flat, straight line), we can stop right here (no change). */
62 if (mEffect == 0.0)
63 return fpVal;
64
65 /* Safe values only please, to avoid breaking things */
66 fpVal = std::min(DENORM_INV, std::max(DENORM, fpVal));
67 mEffect = std::min(DENORM_INV, std::max(DENORM, mEffect));
68
69 fpVal = 1.0 - fpVal;
70 fpVal = ((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fpVal);
71 fpVal = 1.0 - fpVal;
72
73 return fpVal;
74}
75
85double FpCurveUpRs (double fpVal, double mEffect)
86{
87 /* If there isn't any "curve" to skew fpVal with (flat, straight line), we can stop right here (no change). */
88 if (mEffect == 0.0)
89 return fpVal;
90
91 /* Safe values only please, to avoid breaking things */
92 fpVal = std::min(DENORM_INV, std::max(DENORM, fpVal));
93 mEffect = std::min(DENORM_INV, std::max(DENORM, mEffect));
94
95 fpVal = (((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fpVal)) + fpVal;
96 fpVal *= 0.50;
97
98 return fpVal;
99}
100
110double FpCurveDnRs (double fpVal, double mEffect)
111{
112 /* If there isn't any "curve" to skew fpVal with (flat, straight line), we can stop right here (no change). */
113 if (mEffect == 0.0)
114 return fpVal;
115
116 /* Safe values only please, to avoid breaking things */
117 fpVal = std::min(DENORM_INV, std::max(DENORM, fpVal));
118 mEffect = std::min(DENORM_INV, std::max(DENORM, mEffect));
119
120 fpVal = 1.0 - fpVal;
121 fpVal = (((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fpVal)) + fpVal;
122 fpVal *= 0.50;
123 fpVal = 1.0 - fpVal;
124
125 return fpVal;
126}
127
137double FpCurve1D_u_in (double fpVal, double mEffect, double cntPnt)
138{
139 /* If there isn't any "curve" to skew fpVal with (flat, straight line), we can stop right here (no change). */
140 if (mEffect == 0.0)
141 return fpVal;
142
143 /* Safe values only please, to avoid breaking things */
144 fpVal = std::min(DENORM_INV, std::max(DENORM, fpVal));
145 mEffect = std::min(DENORM_INV, std::max(DENORM, mEffect));
146 cntPnt = std::min(DENORM_INV, std::max(DENORM, cntPnt));
147
148 if (fpVal > cntPnt) {
149 double fpVal_mod = (fpVal - cntPnt) / (1.0 - cntPnt);
150
151 fpVal_mod = FpCurveDn(fpVal_mod, mEffect);
152 fpVal_mod *= 1.0 - cntPnt;
153 fpVal_mod += cntPnt;
154 return fpVal_mod;
155 }
156 if (fpVal < cntPnt) {
157 double fpVal_mod = (cntPnt - fpVal) / cntPnt;
158
159 fpVal_mod = FpCurveDn(fpVal_mod, mEffect);
160 fpVal_mod *= cntPnt;
161 fpVal_mod = cntPnt - fpVal_mod;
162 return fpVal_mod;
163 }
164 /* fpVal = cntPnt, so no change */
165 return fpVal;
166}
167
178double FpCurve1D_u_out (double fpVal, double mEffect, double cntPnt)
179{
180 /* If there isn't any "curve" to skew fpVal with (flat, straight line), we can stop right here (no change). */
181 if (mEffect == 0.0)
182 return fpVal;
183
184 /* Safe values only please, to avoid breaking things */
185 fpVal = std::min(DENORM_INV, std::max(DENORM, fpVal));
186 mEffect = std::min(DENORM_INV, std::max(DENORM, mEffect));
187 cntPnt = std::min(DENORM_INV, std::max(DENORM, cntPnt));
188
189 if (fpVal > cntPnt) {
190 double fpVal_mod = (fpVal - cntPnt) / (1.0 - cntPnt);
191
192 fpVal_mod = FpCurveUp(fpVal_mod, mEffect);
193 fpVal_mod *= 1.0 - cntPnt;
194 fpVal_mod += cntPnt;
195 return fpVal_mod;
196 }
197 if (fpVal < cntPnt) {
198 double fpVal_mod = (cntPnt - fpVal) / cntPnt;
199
200 fpVal_mod = FpCurveUp(fpVal_mod, mEffect);
201 fpVal_mod *= cntPnt;
202 fpVal_mod = cntPnt - fpVal_mod;
203 return fpVal_mod;
204 }
205 /* fpVal = cntPnt, so no change */
206 return fpVal;
207}
208
218double FpCurve1D_s_out (double fpVal, double mEffect)
219{
220 /* If there isn't any "curve" to skew fpVal with (flat, straight line), we can stop right here (no change). */
221 if (mEffect == 0.0)
222 return fpVal;
223
224 /* Safe values only please, to avoid breaking things */
225 fpVal = std::min(DENORM_INV, std::max(DENORM, fpVal));
226 mEffect = std::min(DENORM_INV, std::max(DENORM, mEffect));
227
228 fpVal = ((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fabs(fpVal));
229
230 return fpVal;
231}
232
241float FpUcurve_f(const float inpVal, const float hard)
242{
243 return (float) ( inpVal >= 0.f ? (inpVal * (hard+inpVal) / (1.f + (hard*inpVal) + (inpVal*inpVal))) :
244 (inpVal * (hard-inpVal) / (1.f - (hard*inpVal) + (inpVal*inpVal))) );
245}
246
255double FpUcurve_d(const double inpVal, const double hard)
256{
257 return (double) ( inpVal >= 0.0 ? (inpVal * (hard+inpVal) / (1.0 + (hard*inpVal) + (inpVal*inpVal))) :
258 (inpVal * (hard-inpVal) / (1.0 - (hard*inpVal) + (inpVal*inpVal))) );
259}
260
270float FpUcurveSc_f(const float inpVal, const float hard, const float scale)
271{
272 return (float) ( inpVal >= 0.f ? (inpVal * (hard+inpVal) / (scale + (hard*inpVal) + (inpVal*inpVal))) :
273 (inpVal * (hard-inpVal) / (scale - (hard*inpVal) + (inpVal*inpVal))) );
274}
275
285double FpUcurveSc_d(const double inpVal, const double hard, const double scale)
286{
287 return (double) ( inpVal >= 0.0 ? (inpVal * (hard+inpVal) / (scale + (hard*inpVal) + (inpVal*inpVal))) :
288 (inpVal * (hard-inpVal) / (scale - (hard*inpVal) + (inpVal*inpVal))) );
289}
double FpCurveUpRs(double fpVal, double mEffect)
Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same ran...
double FpCurve1D_s_out(double fpVal, double mEffect)
Takes a floating-point value (double) between -1.0 and +1.0 and returns a new value within the same r...
double FpCurveDn(double fpVal, double mEffect)
Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same ran...
float FpUcurveSc_f(const float inpVal, const float hard, const float scale)
Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve,...
double FpCurve1D_u_in(double fpVal, double mEffect, double cntPnt)
Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same ran...
float FpUcurve_f(const float inpVal, const float hard)
Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve,...
double FpCurveUp(double fpVal, double mEffect)
Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same ran...
double FpUcurveSc_d(const double inpVal, const double hard, const double scale)
Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve,...
double FpUcurve_d(const double inpVal, const double hard)
Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve,...
double FpCurve1D_u_out(double fpVal, double mEffect, double cntPnt)
Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same ran...
double FpCurveDnRs(double fpVal, double mEffect)
Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same ran...
Special, additional math algorithms for floating-point values.
#define DENORM
This "DENORM" is for avoiding denormal-related issues when values equal to a perfect 0....
#define DENORM_INV
static const vec3_t scale