1 /**
2  * @file
3  * @brief Special, additional math algorithms for floating-point values
4  */
5 
6 /*
7 Copyright (C) 2002-2013 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 */
24 
25 #include "mathlib_extra.h"
26 #include <math.h>
27 
28 /**
29  * @brief Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same range,
30  * with output values skewed or "leaning" upward (toward 1.0) in a curve with a controlled shape.
31  * @param[in] fpVal Initial floating-Point value (double) to be altered.
32  * @param[in] mEffect How extreme the effect is (the shape of the curve).  A value of 0.0 is no curve or effect at all,
33  * while a value of 1.0 is for maximum effect (very extreme).
34  * @return The new, altered value.
35  */
FpCurveUp(double fpVal,double mEffect)36 double 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 = fmin(DENORM_INV, fmax(DENORM, fpVal));
44 	mEffect = fmin(DENORM_INV, fmax(DENORM, mEffect));
45 
46 	fpVal = ((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fpVal);
47 
48 	return fpVal;
49 }
50 
51 /**
52  * @brief Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same range,
53  * with output values skewed or "leaning" downward (toward 0.0) in a curve with a controlled shape.
54  * @param[in] fpVal Initial floating-Point value (double) to be altered.
55  * @param[in] mEffect How extreme the effect is (the shape of the curve).  A value of 0.0 is no curve or effect at all,
56  * while a value of 1.0 is for maximum effect (very extreme).
57  * @return The new, altered value.
58  */
FpCurveDn(double fpVal,double mEffect)59 double 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 = fmin(DENORM_INV, fmax(DENORM, fpVal));
67 	mEffect = fmin(DENORM_INV, fmax(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 
76 /**
77  * @brief Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same range,
78  * with output values skewed or "leaning" upward (toward 1.0) in a curve with a controlled shape that is more extreme
79  * closer to 0.0 and tapers off closer to 1.0, resulting in a curve that bends into a special shape.
80  * @param[in] fpVal Initial floating-Point value (double) to be altered.
81  * @param[in] mEffect How extreme the effect is (the shape of the curve).  A value of 0.0 is no curve or effect at all,
82  * while a value of 1.0 is for maximum effect (very extreme).
83  * @return The new, altered value.
84  */
FpCurveUpRs(double fpVal,double mEffect)85 double 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 = fmin(DENORM_INV, fmax(DENORM, fpVal));
93 	mEffect = fmin(DENORM_INV, fmax(DENORM, mEffect));
94 
95 	fpVal = (((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fpVal)) + fpVal;
96 	fpVal *= 0.50;
97 
98 	return fpVal;
99 }
100 
101 /**
102  * @brief Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same range,
103  * with output values skewed or "leaning" downward (toward 0.0) in a curve with a controlled shape that is more extreme
104  * closer to 0.0 and tapers off closer to 1.0, resulting in a curve that bends into a special shape.
105  * @param[in] fpVal Initial floating-Point value (double) to be altered.
106  * @param[in] mEffect How extreme the effect is (the shape of the curve).  A value of 0.0 is no curve or effect at all,
107  * while a value of 1.0 is for maximum effect (very extreme).
108  * @return The new, altered value.
109  */
FpCurveDnRs(double fpVal,double mEffect)110 double 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 = fmin(DENORM_INV, fmax(DENORM, fpVal));
118 	mEffect = fmin(DENORM_INV, fmax(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 
128 /**
129  * @brief Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same range,
130  * with output values skewed or "leaning" inward toward a given center point value in a curve with a controlled shape.
131  * @param[in] fpVal Initial floating-Point value (double) to be altered.
132  * @param[in] mEffect How extreme the effect is (the shape of the curve).  A value of 0.0 is no curve or effect at all,
133  * while a value of 1.0 is for maximum effect (very extreme).
134  * @param[in] cntPnt The center point (a value between 0.0 and 1.0) that fpVal should "lean" or skew to.
135  * @return The new, altered value.
136  */
FpCurve1D_u_in(double fpVal,double mEffect,double cntPnt)137 double 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 = fmin(DENORM_INV, fmax(DENORM, fpVal));
145 	mEffect = fmin(DENORM_INV, fmax(DENORM, mEffect));
146 	cntPnt = fmin(DENORM_INV, fmax(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 
168 /**
169  * @brief Takes a floating-point value (double) between 0.0 and 1.0 and returns a new value within the same range,
170  * with output values skewed or "leaning" outward away from a given center point value in a curve with a controlled shape.
171  * The value will curve toward either 1.0 or 0.0, depending on which one would NOT mean crossing the center point.
172  * @param[in] fpVal Initial floating-Point value (double) to be altered.
173  * @param[in] mEffect How extreme the effect is (the shape of the curve).  A value of 0.0 is no curve or effect at all,
174  * while a value of 1.0 is for maximum effect (very extreme).
175  * @param[in] cntPnt The center point (a value between 0.0 and 1.0) that fpVal should "lean" or skew away from.
176  * @return The new, altered value.
177  */
FpCurve1D_u_out(double fpVal,double mEffect,double cntPnt)178 double 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 = fmin(DENORM_INV, fmax(DENORM, fpVal));
186 	mEffect = fmin(DENORM_INV, fmax(DENORM, mEffect));
187 	cntPnt = fmin(DENORM_INV, fmax(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 
209 /**
210  * @brief Takes a floating-point value (double) between -1.0 and +1.0 and returns a new value within the same range,
211  * with output values skewed or "leaning" outward away from 0.0.
212  * The value will curve toward either -1.0 or +1.0, depending on which one would NOT mean crossing 0.0.
213  * @param[in] fpVal Initial floating-Point value (double) to be altered.  May range from -1.0 to +1.0.
214  * @param[in] mEffect How extreme the effect is (the shape of the curve).  A value of 0.0 is no curve or effect at all,
215  * while a value of 1.0 is for maximum effect (very extreme).
216  * @return The new, altered value.
217  */
FpCurve1D_s_out(double fpVal,double mEffect)218 double 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 = fmin(DENORM_INV, fmax(DENORM, fpVal));
226 	mEffect = fmin(DENORM_INV, fmax(DENORM, mEffect));
227 
228 	fpVal = ((2.0 - mEffect) * fpVal) / ((1.0 - mEffect) + fabs(fpVal));
229 
230 	return fpVal;
231 }
232 
233 /**
234  * @brief Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve, so that as the input
235  * value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
236  * @param[in] inpVal The original input value.
237  * @param[in] hard The steepness or slope of the curve, valid values are 0.f to +inf.  Higher values mean a more rapid
238  * approach of inpVal toward +1.f or -1,f.
239  * @return The altered value, between -1.f and 1.f.
240  */
FpUcurve_f(const float inpVal,const float hard)241 float 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 
247 /**
248  * @brief Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve, so that as the input
249  * value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
250  * @param[in] inpVal The original input value.
251  * @param[in] hard The steepness or slope of the curve, valid values are 0.f to +inf.  Higher values mean a more rapid
252  * approach of inpVal toward +1.f or -1,f.
253  * @return The altered value, between -1.f and 1.f.
254  */
FpUcurve_d(const double inpVal,const double hard)255 double 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 
261 /**
262  * @brief Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve, so that as the input
263  * value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
264  * @param[in] inpVal The original input value.
265  * @param[in] hard The steepness or slope of the curve, valid values are 0.f to +inf.  Higher values mean a more rapid
266  * @param[in] scale An additional scaling factor that can affect the shape of the sloped curve of potential output values.
267  * approach of inpVal toward +1.f or -1,f.
268  * @return The altered value, between -1.f and 1.f.
269  */
FpUcurveSc_f(const float inpVal,const float hard,const float scale)270 float 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 
276 /**
277  * @brief Takes a (float) of any value and outputs a value from -1.f to +1.f, along a curve, so that as the input
278  * value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
279  * @param[in] inpVal The original input value.
280  * @param[in] hard The steepness or slope of the curve, valid values are 0.f to +inf.  Higher values mean a more rapid
281  * @param[in] scale An additional scaling factor that can affect the shape of the sloped curve of potential output values.
282  * approach of inpVal toward +1.f or -1,f.
283  * @return The altered value, between -1.f and 1.f.
284  */
FpUcurveSc_d(const double inpVal,const double hard,const double scale)285 double 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 }
290