artkpFixedBase_generic.h
Go to the documentation of this file.
00001 /* ========================================================================
00002  * PROJECT: ARToolKitPlus
00003  * ========================================================================
00004  * This work is based on the original ARToolKit developed by
00005  *   Hirokazu Kato
00006  *   Mark Billinghurst
00007  *   HITLab, University of Washington, Seattle
00008  * http://www.hitl.washington.edu/artoolkit/
00009  *
00010  * Copyright of the derived and new portions of this work
00011  *     (C) 2006 Graz University of Technology
00012  *
00013  * This framework is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This framework is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this framework; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  *
00027  * For further information please contact 
00028  *   Dieter Schmalstieg
00029  *   <schmalstieg@icg.tu-graz.ac.at>
00030  *   Graz University of Technology, 
00031  *   Institut for Computer Graphics and Vision,
00032  *   Inffeldgasse 16a, 8010 Graz, Austria.
00033  * ========================================================================
00034  ** @author   Daniel Wagner
00035  *
00036  * $Id$
00037  * @file
00038  * ======================================================================== */
00039 
00040 
00041 #ifndef __ARTKPFIXEDBASE_GENERIC_HEADERFILE__
00042 #define __ARTKPFIXEDBASE_GENERIC_HEADERFILE__
00043 
00044 #include <assert.h>
00045 
00046 
00047 // Generic MATHBASE implementation for all
00048 // platforms. This implementation is totally
00049 // unoptimized.
00050 // (e.g. using the sin() function from math.h)
00051 //
00052 template <int PBITS_, int CHECK_>
00053 class artkpFixedBase_generic
00054 {
00055 public:
00056         enum {
00057                 PBITS = PBITS_,
00058                 CHECK = CHECK_
00059         };
00060 
00061 
00062         static void checkInt(int nInteger)
00063         {
00064                 __int64 v64 = ((__int64)nInteger) << PBITS;
00065                 if(v64 != int(v64))
00066                 {
00067                         assert(false && "Integer to Fixed-Point conversion failed: the target's range was overflowed");
00068                 };
00069         }
00070 
00071         static void checkFloat(float nFloat)
00072         {
00073                 __int64 v64 = (__int64)(nFloat *  ((float)(1 << PBITS) + 0.5f));
00074                 if(v64 != int(v64))
00075                 {
00076                         assert(false && "Float to Fixed-Point conversion failed: the target's range was overflowed");
00077                 };
00078         }
00079 
00080         static void checkDouble(double nDouble)
00081         {
00082                 __int64 v64 = (__int64)(nDouble *  ((double)(1 << PBITS) + 0.5f));
00083                 if(v64 != int(v64))
00084                 {
00085                         assert(false && "Double to Fixed-Point conversion failed: the target's range was overflowed");
00086                 };
00087         }
00088 
00089         static float floatFromFixed(int nFixed)
00090         {
00091                 return nFixed/(float)(1 << PBITS);
00092         }       
00093 
00094         static double doubleFromFixed(int nFixed)
00095         {
00096                 return nFixed/(double)(1 << PBITS);
00097         }
00098 
00099         static int fixedFromInt(int nV)
00100         {
00101                 if(CHECK)
00102                         checkInt(nV);
00103                 return nV<<PBITS;
00104         }
00105 
00106         static int fixedFromFloat(float nV)
00107         {
00108                 if(CHECK)
00109                         checkFloat(nV);
00110                 return (int)(nV *  (float)(1 << PBITS) + 0.5f);
00111         }
00112 
00113         static int fixedFromDouble(double nV)
00114         {
00115                 if(CHECK)
00116                         checkDouble(nV);
00117                 return (int)(nV * (double)(1 << PBITS) + 0.5f);
00118         }
00119 
00120         static int inverse(int nFixed)
00121         {
00122                 return (__int32)(((__int64)1<<(2*PBITS))/nFixed);
00123         }
00124 
00125         static int multiply(int nLeftFixed, int nRightFixed)
00126         {
00127                 return (__int32)(((__int64)nLeftFixed * (__int64)nRightFixed) >> PBITS);
00128         }
00129 
00130         static int divide(int nLeftFixed, int nRightFixed)
00131         {
00132                 return (__int32)(((__int64)nLeftFixed << PBITS) / nRightFixed);
00133         }
00134 
00135         static int cos(int nFixed)
00136         {
00137                 return fixedFromDouble(::cos(floatFromFixed(nFixed)));
00138         }
00139 
00140         static int sin(int nFixed)
00141         {
00142                 return fixedFromDouble(::sin(floatFromFixed(nFixed)));
00143         }
00144 
00145         static int fabs(int nFixed)
00146         {
00147                 return nFixed<0 ? -nFixed : nFixed;
00148         }
00149 
00150         static int sqrt(int nFixed)
00151         {
00152                 return fixedFromDouble(::sqrt(floatFromFixed(nFixed)));
00153         }
00154 
00155         static int inverseSqrt(int nFixed)
00156         {
00157                 return inverse(sqrt(nFixed));
00158         }
00159 
00160         static int ceil(int nFixed)
00161         {
00162                 int ret = (nFixed>>PBITS)<<PBITS;
00163 
00164                 if(nFixed>=0 && ret<nFixed)
00165                         ret += fixedFromInt(1);
00166 
00167                 return ret;
00168         }
00169 
00170 };
00171 
00172 
00173 #endif //__ARTKPFIXEDBASE_GENERIC_HEADERFILE__


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun May 29 2016 02:50:12