UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mathlib_extra.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 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 
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 = 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 
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 = 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 
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 = 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 
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 = 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 
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 = 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 
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 = 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 
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 = 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 
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 
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 
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 
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 }
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, so that as the input value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
static const vec3_t scale
Special, additional math algorithms for floating-point values.
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...
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...
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, so that as the input value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
#define DENORM
This "DENORM" is for avoiding denormal-related issues when values equal to a perfect 0...
Definition: mathlib_extra.h:53
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...
#define DENORM_INV
Definition: mathlib_extra.h:56
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...
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
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, so that as the input value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
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, so that as the input value gets farther from 0.0f it slows down and never quite gets to +1.f or -1.f.
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_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 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...