artkpFloat_fixed.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 __ARTKPFLOAT_FIXED_HEADERFILE__
00042 #define __ARTKPFLOAT_FIXED_HEADERFILE__
00043 
00044 #include <math.h>
00045 #include <assert.h>
00046 
00047 #pragma warning ( push )
00048 #pragma warning ( disable: 4293 )               // fix warning about shifting in conversion constructor
00049 
00050 // fixed point implementation of artkpFloat
00051 //
00052 // MATHBASE provides concrete methods for
00053 // internal usage. while artkpFloat_fixed is platform
00054 // independent, MATHBASE is the correct place
00055 // to implement hardware specific optimizations.
00056 // (see artkpFixedBase_generic & artkpFixedBase_gpp)
00057 //
00058 
00059 template<typename MATHBASE>
00060 class artkpFloat_fixed
00061 {
00062 public:
00063         enum {
00064                 PBITS = MATHBASE::PBITS,
00065                 CHECK = MATHBASE::CHECK
00066         };
00067 
00068         artkpFloat_fixed()                                                                      {}
00069         artkpFloat_fixed(float nV)                                                      {  setFloat(nV);  }
00070         artkpFloat_fixed(double nV)                                                     {  setDouble(nV);  }
00071         artkpFloat_fixed(const artkpFloat_fixed& nOther)        {  v = nOther.v;  }
00072 
00073         // conversion constructor. allows assigning values of different precision types
00074         template <typename OTHER_MATHBASE>
00075         artkpFloat_fixed(const artkpFloat_fixed<OTHER_MATHBASE>& nOther)
00076         {
00077                 if(OTHER_MATHBASE::PBITS<PBITS)
00078                 {
00079                         if(CHECK)
00080                         {
00081                                 // check if the conversion resulted in an overflow
00082                                 __int64 v64 = ((__int64)nOther.getFixed()) << (PBITS-OTHER_MATHBASE::PBITS);
00083                                 if(v64 != int(v))
00084                                 {
00085                                         assert(false && "Fixed-Point to Fixed-Point conversion failed: the target's range was overflowed");
00086                                 }
00087                         }
00088                         v = nOther.getFixed() << (PBITS-OTHER_MATHBASE::PBITS);
00089                 }
00090                 else
00091                 {
00092                         v = nOther.getFixed() >> (OTHER_MATHBASE::PBITS-PBITS);
00093                 }
00094         }
00095 
00096         // custom interface
00097         //
00098         bool isFixed()                                                                          {  return true;  }
00099 
00100         void setFixed(int nV)                                                           {  v = nV;  }
00101         void setInt(int nV)                                                                     {  v = MATHBASE::fixedFromInt(nV);  }
00102         void setFloat(float nV)                                                         {  v = MATHBASE::fixedFromFloat(nV);  }
00103         void setDouble(double nV)                                                       {  v = MATHBASE::fixedFromDouble(nV);  }
00104         void setArtkpFloat(const artkpFloat_fixed& nOther)      {  v = nOther.v;  }
00105 
00106         int getByte() const                                                                     {  return v>255<<(PBITS-8) ? 255 : v>>(PBITS-8);  }
00107         int getFixed() const                                                    {  return v;  }
00108         int getInt() const                                                                      {  return v>>PBITS;  }
00109         float getFloat() const                                                          {  return MATHBASE::floatFromFixed(v);  }
00110         double getDouble() const                                                        {  return MATHBASE::doubleFromFixed(v);  }
00111 
00112         void inverse(const artkpFloat_fixed& nOther)            {  v = MATHBASE::inverse(nOther.v);  }
00113         void inverse()                                                                          {  inverse(*this);  }
00114 
00115         void inverseSqrt(const artkpFloat_fixed& nOther)        {  v = MATHBASE::inverseSqrt(nOther.v);  }
00116         void inverseSqrt()                                                                      {  inverseSqrt(*this);  }
00117 
00118         void multiplyBy255()                                                            {  int vOld = v;   v<<=8;  v-=vOld;  }
00119 
00120 
00121         // some standard math.h routines applied to this class
00122         //
00123         friend inline artkpFloat_fixed cos(const artkpFloat_fixed& nV)
00124                 {  return MATHBASE::cos(nV.v);  }
00125 
00126 
00127         // overloaded operators
00128         //
00129         artkpFloat_fixed& operator=(int nV)                                                     {  setInt(nV);  return *this; }
00130         artkpFloat_fixed& operator=(float nV)                                           {  setFloat(nV);  return *this; }
00131         artkpFloat_fixed& operator=(double nV)                                          {  setDouble(nV);  return *this; }
00132         artkpFloat_fixed& operator=(const artkpFloat_fixed& nOther)     {  v = nOther.v;  return *this;  }
00133 
00134         artkpFloat_fixed& operator+=(int nV)    {  v += MATHBASE::fixedFromInt(nV);  return *this;  }
00135         artkpFloat_fixed& operator-=(int nV)    {  v -= MATHBASE::fixedFromInt(nV);  return *this;  }
00136         artkpFloat_fixed& operator*=(int nV)    {  v = MATHBASE::multiply(v, MATHBASE::fixedFromInt(nV));  return *this;  }
00137         artkpFloat_fixed& operator/=(int nV)    {  v = MATHBASE::divide(v, MATHBASE::fixedFromInt(nV));  return *this;  }
00138 
00139         artkpFloat_fixed& operator+=(float nV)  {  v += MATHBASE::fixedFromFloat(nV);  return *this;  }
00140         artkpFloat_fixed& operator-=(float nV)  {  v -= MATHBASE::fixedFromFloat(nV);  return *this;  }
00141         artkpFloat_fixed& operator*=(float nV)  {  v = MATHBASE::multiply(v, MATHBASE::fixedFromFloat(nV));  return *this;  }
00142         artkpFloat_fixed& operator/=(float nV)  {  v = MATHBASE::divide(v, MATHBASE::fixedFromFloat(nV));  return *this;  }
00143 
00144         artkpFloat_fixed& operator+=(double nV) {  v += MATHBASE::fixedFromDouble(nV);  return *this;  }
00145         artkpFloat_fixed& operator-=(double nV) {  v -= MATHBASE::fixedFromDouble(nV);  return *this;  }
00146         artkpFloat_fixed& operator*=(double nV) {  v = MATHBASE::multiply(v, fixedFromDouble(nV));  return *this;  }
00147         artkpFloat_fixed& operator/=(double nV) {  v = MATHBASE::divide(v, fixedFromDouble(nV));  return *this;  }
00148 
00149         artkpFloat_fixed& operator+=(const artkpFloat_fixed& nOther)    {  v+=nOther.v;  return *this;  }
00150         artkpFloat_fixed& operator-=(const artkpFloat_fixed& nOther)    {  v-=nOther.v;  return *this;  }
00151         artkpFloat_fixed& operator*=(const artkpFloat_fixed& nOther)    {  v = MATHBASE::multiply(v, nOther.v);  return *this;  }
00152         artkpFloat_fixed& operator/=(const artkpFloat_fixed& nOther)    {  v = MATHBASE::divide(v, nOther.v);  return *this;  }
00153 
00154         artkpFloat_fixed& operator>>=(int nBits)        {  v>>=nBits;  return *this;  }
00155         artkpFloat_fixed& operator<<=(int nBits)        {  v<<=nBits;  return *this;  }
00156 
00157         bool operator==(const artkpFloat_fixed& nOther) const           {  return v==nOther.v;  }
00158         bool operator!=(const artkpFloat_fixed& nOther) const           {  return v!=nOther.v;  }
00159         bool operator<=(const artkpFloat_fixed& nOther) const           {  return v<=nOther.v;  }
00160         bool operator>=(const artkpFloat_fixed& nOther) const           {  return v>=nOther.v;  }
00161         bool operator<(const artkpFloat_fixed& nOther) const            {  return v<nOther.v;  }
00162         bool operator>(const artkpFloat_fixed& nOther) const            {  return v>nOther.v;  }
00163 
00164         bool operator==(int nOther) const               {  return v == MATHBASE::fixedFromInt(nOther);  }
00165         bool operator!=(int nOther) const               {  return v != MATHBASE::fixedFromInt(nOther);  }
00166         bool operator<=(int nOther) const               {  return v <= MATHBASE::fixedFromInt(nOther);  }
00167         bool operator>=(int nOther) const               {  return v >= MATHBASE::fixedFromInt(nOther);  }
00168         bool operator<(int nOther) const                {  return v <  MATHBASE::fixedFromInt(nOther);  }
00169         bool operator>(int nOther) const                {  return v >  MATHBASE::fixedFromInt(nOther);  }
00170 
00171         bool operator==(float nOther) const             {  return v == MATHBASE::fixedFromFloat(nOther);  }
00172         bool operator!=(float nOther) const             {  return v != MATHBASE::fixedFromFloat(nOther);  }
00173         bool operator<=(float nOther) const             {  return v <= MATHBASE::fixedFromFloat(nOther);  }
00174         bool operator>=(float nOther) const             {  return v >= MATHBASE::fixedFromFloat(nOther);  }
00175         bool operator<(float nOther) const              {  return v <  MATHBASE::fixedFromFloat(nOther);  }
00176         bool operator>(float nOther) const              {  return v >  MATHBASE::fixedFromFloat(nOther);  }
00177 
00178         friend artkpFloat_fixed operator+ (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00179                 {  return artkpFloat_fixed(left.v+right.v);  }
00180         friend artkpFloat_fixed operator+ (const artkpFloat_fixed& left, float right)
00181                 {  return artkpFloat_fixed(left.v+MATHBASE::fixedFromFloat(right));  }
00182         friend artkpFloat_fixed operator+ (float left, const artkpFloat_fixed& right)
00183                 {  return artkpFloat_fixed(MATHBASE::fixedFromFloat(left)+right.v);  }
00184 
00185         friend artkpFloat_fixed operator- (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00186                 {  return artkpFloat_fixed(left.v-right.v);  }
00187         friend artkpFloat_fixed operator- (const artkpFloat_fixed& left, float right)
00188                 {  return artkpFloat_fixed(left.v-MATHBASE::fixedFromFloat(right));  }
00189         friend artkpFloat_fixed operator- (float left, const artkpFloat_fixed& right)
00190                 {  return artkpFloat_fixed(MATHBASE::fixedFromFloat(left)-right.v);  }
00191 
00192         friend artkpFloat_fixed operator* (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00193                 {  return artkpFloat_fixed(MATHBASE::multiply(left.v, right.v));  }
00194         friend artkpFloat_fixed operator* (const artkpFloat_fixed& left, float right)
00195                 {  return artkpFloat_fixed(MATHBASE::multiply(left.v, MATHBASE::fixedFromFloat(right)));  }
00196         friend artkpFloat_fixed operator* (float left, const artkpFloat_fixed& right)
00197                 {  return artkpFloat_fixed(MATHBASE::multiply(MATHBASE::fixedFromFloat(left), right.v));  }
00198 
00199         friend artkpFloat_fixed operator/ (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00200                 {  return artkpFloat_fixed(MATHBASE::divide(left.v, right.v));  }
00201         friend artkpFloat_fixed operator/ (const artkpFloat_fixed& left, float right)
00202                 {  return artkpFloat_fixed(MATHBASE::divide(left.v, MATHBASE::fixedFromFloat(right)));  }
00203         friend artkpFloat_fixed operator/ (float left, const artkpFloat_fixed& right)
00204                 {  return artkpFloat_fixed(MATHBASE::divide(MATHBASE::fixedFromFloat(left), right.v));  }
00205 
00206         artkpFloat_fixed operator-() const
00207                 {  artkpFloat_fixed w;  w.v = -v;  return w;  }
00208 
00209 protected:
00210         artkpFloat_fixed(int nV)                {  v = nV;  }           // constructor with fixed point parameter
00211 
00212         int v;
00213 };
00214 
00215 
00216 #pragma warning ( pop )
00217 
00218 
00219 #endif //__ARTKPFLOAT_FIXED_HEADERFILE__


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