artkpFixedBase_generic.h
Go to the documentation of this file.
1 /* ========================================================================
2  * PROJECT: ARToolKitPlus
3  * ========================================================================
4  * This work is based on the original ARToolKit developed by
5  * Hirokazu Kato
6  * Mark Billinghurst
7  * HITLab, University of Washington, Seattle
8  * http://www.hitl.washington.edu/artoolkit/
9  *
10  * Copyright of the derived and new portions of this work
11  * (C) 2006 Graz University of Technology
12  *
13  * This framework is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This framework is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this framework; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * For further information please contact
28  * Dieter Schmalstieg
29  * <schmalstieg@icg.tu-graz.ac.at>
30  * Graz University of Technology,
31  * Institut for Computer Graphics and Vision,
32  * Inffeldgasse 16a, 8010 Graz, Austria.
33  * ========================================================================
34  ** @author Daniel Wagner
35  *
36  * $Id$
37  * @file
38  * ======================================================================== */
39 
40 
41 #ifndef __ARTKPFIXEDBASE_GENERIC_HEADERFILE__
42 #define __ARTKPFIXEDBASE_GENERIC_HEADERFILE__
43 
44 #include <assert.h>
45 
46 
47 // Generic MATHBASE implementation for all
48 // platforms. This implementation is totally
49 // unoptimized.
50 // (e.g. using the sin() function from math.h)
51 //
52 template <int PBITS_, int CHECK_>
54 {
55 public:
56  enum {
57  PBITS = PBITS_,
58  CHECK = CHECK_
59  };
60 
61 
62  static void checkInt(int nInteger)
63  {
64  __int64 v64 = ((__int64)nInteger) << PBITS;
65  if(v64 != int(v64))
66  {
67  assert(false && "Integer to Fixed-Point conversion failed: the target's range was overflowed");
68  };
69  }
70 
71  static void checkFloat(float nFloat)
72  {
73  __int64 v64 = (__int64)(nFloat * ((float)(1 << PBITS) + 0.5f));
74  if(v64 != int(v64))
75  {
76  assert(false && "Float to Fixed-Point conversion failed: the target's range was overflowed");
77  };
78  }
79 
80  static void checkDouble(double nDouble)
81  {
82  __int64 v64 = (__int64)(nDouble * ((double)(1 << PBITS) + 0.5f));
83  if(v64 != int(v64))
84  {
85  assert(false && "Double to Fixed-Point conversion failed: the target's range was overflowed");
86  };
87  }
88 
89  static float floatFromFixed(int nFixed)
90  {
91  return nFixed/(float)(1 << PBITS);
92  }
93 
94  static double doubleFromFixed(int nFixed)
95  {
96  return nFixed/(double)(1 << PBITS);
97  }
98 
99  static int fixedFromInt(int nV)
100  {
101  if(CHECK)
102  checkInt(nV);
103  return nV<<PBITS;
104  }
105 
106  static int fixedFromFloat(float nV)
107  {
108  if(CHECK)
109  checkFloat(nV);
110  return (int)(nV * (float)(1 << PBITS) + 0.5f);
111  }
112 
113  static int fixedFromDouble(double nV)
114  {
115  if(CHECK)
116  checkDouble(nV);
117  return (int)(nV * (double)(1 << PBITS) + 0.5f);
118  }
119 
120  static int inverse(int nFixed)
121  {
122  return (__int32)(((__int64)1<<(2*PBITS))/nFixed);
123  }
124 
125  static int multiply(int nLeftFixed, int nRightFixed)
126  {
127  return (__int32)(((__int64)nLeftFixed * (__int64)nRightFixed) >> PBITS);
128  }
129 
130  static int divide(int nLeftFixed, int nRightFixed)
131  {
132  return (__int32)(((__int64)nLeftFixed << PBITS) / nRightFixed);
133  }
134 
135  static int cos(int nFixed)
136  {
137  return fixedFromDouble(::cos(floatFromFixed(nFixed)));
138  }
139 
140  static int sin(int nFixed)
141  {
142  return fixedFromDouble(::sin(floatFromFixed(nFixed)));
143  }
144 
145  static int fabs(int nFixed)
146  {
147  return nFixed<0 ? -nFixed : nFixed;
148  }
149 
150  static int sqrt(int nFixed)
151  {
152  return fixedFromDouble(::sqrt(floatFromFixed(nFixed)));
153  }
154 
155  static int inverseSqrt(int nFixed)
156  {
157  return inverse(sqrt(nFixed));
158  }
159 
160  static int ceil(int nFixed)
161  {
162  int ret = (nFixed>>PBITS)<<PBITS;
163 
164  if(nFixed>=0 && ret<nFixed)
165  ret += fixedFromInt(1);
166 
167  return ret;
168  }
169 
170 };
171 
172 
173 #endif //__ARTKPFIXEDBASE_GENERIC_HEADERFILE__
static void checkInt(int nInteger)
static int multiply(int nLeftFixed, int nRightFixed)
static int sin(int nFixed)
f
static int inverseSqrt(int nFixed)
static int divide(int nLeftFixed, int nRightFixed)
static int fixedFromFloat(float nV)
static int fixedFromDouble(double nV)
static int cos(int nFixed)
static int fabs(int nFixed)
static float floatFromFixed(int nFixed)
static int inverse(int nFixed)
static int ceil(int nFixed)
static double doubleFromFixed(int nFixed)
static int sqrt(int nFixed)
static void checkDouble(double nDouble)
static void checkFloat(float nFloat)
static int fixedFromInt(int nV)


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33