00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 00022 // Don't include this directly. Instead include Trig.hpp 00023 00024 00025 #ifndef base_math_Trig_FixedPoint_hpp 00026 #define base_math_Trig_FixedPoint_hpp 00027 00028 #include "util_math.hpp" 00029 #include "util/type/util_type.hpp" 00030 #include "BrayT.hpp" 00031 00032 #ifdef SE_FIXED_POINT 00033 00034 00035 namespace se_core { 00036 class _SeCoreExport Trig { 00037 public: 00038 static const short TRIG_SHIFT = 13; 00039 00040 inline static float trigToFloat(trig_t value) { 00041 return value / static_cast<float>(1L << TRIG_SHIFT); 00042 } 00043 00044 inline static quat_t trigToQuat(trig_t value) { 00045 return value << (QUAT_SHIFT - TRIG_SHIFT); 00046 //return value / static_cast<float>(1L << TRIG_SHIFT); 00047 } 00048 00049 static quat_t cosQuat(bray_t bray) { 00050 return trigToQuat(cosAbs(bray)) * cosSign(bray); 00051 } 00052 00053 static quat_t sinQuat(bray_t bray) { 00054 return trigToQuat(sinAbs(bray)) * sinSign(bray); 00055 } 00056 00057 inline static coor_t cosScale(coor_t scale, bray_t bray) { 00058 return static_cast<coor_t>(((scale * Trig::cosAbs(bray)) >> Trig::TRIG_SHIFT) * Trig::cosSign(bray)); 00059 } 00060 00061 inline static coor_t sinScale(coor_t scale, bray_t bray) { 00062 return static_cast<coor_t>(((scale * Trig::sinAbs(bray)) >> Trig::TRIG_SHIFT) * Trig::sinSign(bray)); 00063 } 00064 00065 inline static trig_t tan(bray_t bray) { 00066 return tangents[ bray >> BRAY_SHIFT ]; 00067 } 00068 00069 inline static bray_t asin(trig_t t) { 00070 //TODO: 00071 return 0; 00072 } 00073 inline static bray_t acos(trig_t t) { 00074 //TODO: 00075 return 0; 00076 } 00077 00078 00082 inline static bray_t atan(trig_t t) { 00083 bray_t max, min; 00084 if(t >= 0) { 00085 max = BrayT::DEG90; 00086 min = 0; 00087 } 00088 else { 00089 min = BrayT::DEG90 + 1; 00090 max = BRAY_RANGE; 00091 } 00092 00093 while(min + 1 < max) { 00094 int middle = (min + max) / 2; 00095 if(t > tangents[middle]) 00096 min = middle; 00097 else 00098 max = middle; 00099 } 00100 return min; 00101 } 00102 00103 00104 inline static bray_t atan2(int xp, int yp) { 00105 if(yp != 0) { 00106 //Calculate angle of vector 00107 short angle=0; 00108 if (xp!=0) { 00109 if((xp >= 0) && (yp >= 0)) 00110 angle = atan((yp << 9) / xp); 00111 else if((xp < 0) && (yp >= 0)) 00112 angle = BrayT::DEG180 - atan((yp << 9) / -xp); 00113 else if((xp < 0) && (yp < 0)) 00114 angle = BrayT::DEG180 + atan(-(yp << 9) / -xp); 00115 else if((xp >= 0) && (yp < 0)) 00116 angle = BrayT::DEG360 - atan(-(yp << 9) / xp); 00117 } 00118 else { 00119 if(yp >= 0) 00120 angle = BrayT::DEG180; 00121 else 00122 angle = BrayT::DEG270; 00123 } 00124 return angle; 00125 } 00126 else { 00127 if(xp == 0 && yp == 0) return BrayT::DEG0; 00128 if(xp == 0 && yp > 0) return BrayT::DEG90; 00129 if(xp == 0 && yp < 0) return BrayT::DEG270; 00130 if(yp == 0 && xp > 0) return BrayT::DEG0; 00131 else return BrayT::DEG180; 00132 } 00133 } 00134 00135 00136 00137 static bray_t clockwise8Direction(coor_t xp, coor_t yp); 00138 00139 private: 00140 inline static trig_t sinAbs(bray_t bray) { 00141 return sinesAbs[ bray >> BRAY_SHIFT ]; 00142 } 00143 00144 00145 inline static trig_sign_t sinSign(bray_t bray) { 00146 return sinesSign[ bray >> BRAY_SHIFT ]; 00147 } 00148 00149 00150 inline static trig_sign_t cosAbs(bray_t bray) { 00151 return cosinesAbs[ bray >> BRAY_SHIFT ]; 00152 } 00153 00154 00155 inline static trig_sign_t cosSign(bray_t bray) { 00156 return cosinesSign[ bray >> BRAY_SHIFT ]; 00157 } 00158 00159 static const trig_t cosinesAbs[]; 00160 static const trig_t cosinesSign[]; 00161 static const trig_t sinesAbs[]; 00162 static const trig_t sinesSign[]; 00163 static const trig_t tangents[]; 00164 }; 00165 00166 } 00167 00168 #endif // SE_FIXED_POINT 00169 00170 #endif 00171 00172 00173
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.