Blender V2.61 - r43446

math_base_inline.c

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