Blender  V2.59
math_base_inline.c
Go to the documentation of this file.
00001 /*
00002  * $Id: math_base_inline.c 37336 2011-06-09 14:27:51Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: some of this file.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  * */
00027 
00033 #include <float.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 
00038 #include "BLI_math.h"
00039 
00040 #ifndef BLI_MATH_BASE_INLINE_H
00041 #define BLI_MATH_BASE_INLINE_H
00042 
00043 /* A few small defines. Keep'em local! */
00044 #define SMALL_NUMBER    1.e-8
00045 
00046 MINLINE float sqrt3f(float f)
00047 {
00048         if(f==0.0f) return 0.0f;
00049         if(f<0) return (float)(-exp(log(-f)/3));
00050         else return (float)(exp(log(f)/3));
00051 }
00052 
00053 MINLINE double sqrt3d(double d)
00054 {
00055         if(d==0.0) return 0;
00056         if(d<0) return -exp(log(-d)/3);
00057         else return exp(log(d)/3);
00058 }
00059 
00060 MINLINE float saacos(float fac)
00061 {
00062         if(fac<= -1.0f) return (float)M_PI;
00063         else if(fac>=1.0f) return 0.0;
00064         else return (float)acos(fac);
00065 }
00066 
00067 MINLINE float saasin(float fac)
00068 {
00069         if(fac<= -1.0f) return (float)-M_PI/2.0f;
00070         else if(fac>=1.0f) return (float)M_PI/2.0f;
00071         else return (float)asin(fac);
00072 }
00073 
00074 MINLINE float sasqrt(float fac)
00075 {
00076         if(fac<=0.0f) return 0.0f;
00077         return (float)sqrt(fac);
00078 }
00079 
00080 MINLINE float saacosf(float fac)
00081 {
00082         if(fac<= -1.0f) return (float)M_PI;
00083         else if(fac>=1.0f) return 0.0f;
00084         else return (float)acosf(fac);
00085 }
00086 
00087 MINLINE float saasinf(float fac)
00088 {
00089         if(fac<= -1.0f) return (float)-M_PI/2.0f;
00090         else if(fac>=1.0f) return (float)M_PI/2.0f;
00091         else return (float)asinf(fac);
00092 }
00093 
00094 MINLINE float sasqrtf(float fac)
00095 {
00096         if(fac<=0.0f) return 0.0f;
00097         return (float)sqrtf(fac);
00098 }
00099 
00100 MINLINE float interpf(float target, float origin, float fac)
00101 {
00102         return (fac*target) + (1.0f-fac)*origin;
00103 }
00104 
00105 /* useful to calculate an even width shell, by taking the angle between 2 planes.
00106  * The return value is a scale on the offset.
00107  * no angle between planes is 1.0, as the angle between the 2 planes approches 180d
00108  * the distance gets very high, 180d would be inf, but this case isn't valid */
00109 MINLINE float shell_angle_to_dist(const float angle)
00110 {
00111         return (angle < (float)SMALL_NUMBER) ? 1.0f : fabsf(1.0f / cosf(angle));
00112 }
00113 
00114 /* used for zoom values*/
00115 MINLINE float power_of_2(float val)
00116 {
00117         return (float)pow(2.0, ceil(log((double)val) / M_LN2));
00118 }
00119 
00120 MINLINE float minf(float a, float b)
00121 {
00122         return (a < b)? a: b;
00123 }
00124 
00125 MINLINE float maxf(float a, float b)
00126 {
00127         return (a > b)? a: b;
00128 }
00129 
00130 MINLINE float signf(float f)
00131 {
00132         return (f < 0.f)? -1.f: 1.f;
00133 }
00134 
00135 #endif /* BLI_MATH_BASE_INLINE_H */
00136