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 #ifndef BrayT_hpp 00023 #define BrayT_hpp 00024 00025 #include "util/error/Log.hpp" 00026 #include "util_math.hpp" 00027 #include "util/type/util_type.hpp" 00028 00029 namespace se_core { 00030 class _SeCoreExport BrayT { 00031 public: 00032 const static bray_t DEG0 = 0; 00033 const static bray_t DEG45 = BRAY_RANGE * 1 / 8; 00034 const static bray_t DEG90 = BRAY_RANGE * 2 / 8; 00035 const static bray_t DEG135 = BRAY_RANGE * 3 / 8; 00036 const static bray_t DEG180 = BRAY_RANGE * 4 / 8; 00037 const static bray_t DEG225 = BRAY_RANGE * 5 / 8; 00038 const static bray_t DEG270 = BRAY_RANGE * 6 / 8; 00039 const static bray_t DEG315 = BRAY_RANGE * 7 / 8; 00040 const static bray_t DEG360 = BRAY_RANGE * 8 / 8; 00041 00042 00043 inline static bool isLeftwise(bray_t bray) { 00044 AssertFatal(BrayT::mask(bray) == bray, "parameter must be normalized"); 00045 return (bray > DEG180); 00046 } 00047 00048 inline static bool isRightwise(bray_t bray) { 00049 AssertFatal(BrayT::mask(bray) == bray, "parameter must be normalized"); 00050 return (bray < DEG180 && bray > 0); 00051 } 00052 00053 00054 inline static bray_t mask(bray_t bray) { 00055 return bray & BRAY_MASK; 00056 } 00057 00058 inline static bray_t add(bray_t b1, bray_t b2) { 00059 return mask(b1 + b2); 00060 } 00061 00062 inline static bray_t sub(bray_t b1, bray_t b2) { 00063 return mask(b1 - b2); 00064 } 00065 00066 inline static bray_t diff(bray_t b1, bray_t b2) { 00067 return abs(b1 - b2); 00068 } 00069 00070 00071 inline static bray_t towards(bray_t from, bray_t to, bray_t d) { 00072 bray_t diff = mask(from - to); 00073 if(abs(diff) <= d) 00074 return to; 00075 00076 if(diff < DEG180) { 00077 return sub(from, d); 00078 } 00079 else { 00080 return add(from, d); 00081 } 00082 } 00083 00084 00085 inline static float toRad(bray_t bray) { 00086 return bray * PI * 2 / static_cast<float>(BRAY_RANGE); 00087 } 00088 00089 inline static bray_t fromRad(float rad) { 00090 return static_cast<bray_t>(rad * BRAY_RANGE / (PI * 2)); 00091 } 00092 00093 inline static float toDeg(bray_t bray) { 00094 return bray * 360.0f / static_cast<float>(BRAY_RANGE); 00095 } 00096 00097 inline static float toBray(bray_t bray) { 00098 return bray * 256.0f / static_cast<float>(BRAY_RANGE); 00099 } 00100 00101 inline static bray_t fromDeg(float deg) { 00102 return mask(static_cast<bray_t>(deg * BRAY_RANGE / (360.0f))); 00103 } 00104 00105 inline static short to4(bray_t bray) { 00106 static const bray_t mask = (0x0c << (BRAY_RANGE_SHIFT - 4)); 00107 return (bray + DEG45) & mask; 00108 } 00109 00110 inline static short to8(bray_t bray) { 00111 static const bray_t mask = (0x0e << (BRAY_RANGE_SHIFT - 4)); 00112 return (bray + (BRAY_RANGE / 16)) & mask; 00113 } 00114 00115 inline static short to16(bray_t bray) { 00116 static const bray_t mask = (0x0f << (BRAY_RANGE_SHIFT - 4)); 00117 return (bray + (BRAY_RANGE / 32)) & mask; 00118 } 00119 00120 inline static short toPlatform(bray_t bray) { 00121 #ifdef IS_GBA 00122 return to8(bray); 00123 #else 00124 return bray; 00125 #endif 00126 } 00127 00128 inline static short toClockwise4(bray_t bray) { 00129 return mask((bray + 32) / (BRAY_RANGE >> 2)); 00130 } 00131 00132 inline static short toClockwise8(bray_t bray) { 00133 return mask((bray + 16) / (BRAY_RANGE >> 3)); 00134 } 00135 00136 inline static short toClockwise16(bray_t bray) { 00137 return mask((bray + 8) / (BRAY_RANGE >> 4)); 00138 } 00139 00140 inline static short toClockwisePlatform(bray_t bray) { 00141 #ifdef IS_GBA 00142 return toClockwise8(bray); 00143 #else 00144 return bray; 00145 #endif 00146 } 00147 00148 inline static bray_t fromClockwise4(short dir) { 00149 return mask(dir << 6); 00150 } 00151 00152 inline static bray_t fromClockwise8(short dir) { 00153 return mask(dir << 5); 00154 } 00155 00156 inline static bray_t fromClockwise16(short dir) { 00157 return mask(dir << 4); 00158 } 00159 00160 inline static bray_t fromClockwisePlatform(short dir) { 00161 #ifdef IS_GBA 00162 return fromClockwise8(dir); 00163 #else 00164 return dir; 00165 #endif 00166 } 00167 00172 inline static bray_t abs(bray_t v) { 00173 bray_t m = mask(v); 00174 return (m >= DEG180) ? (DEG360 - m) : m; 00175 } 00176 00177 inline static scale_t abss(scale_t t) { return t > 0 ? t : -t; } 00178 inline static bray_t fromScale(scale_t v) { 00179 //return static_cast<bray_t>(abss(v)) & BRAY_MASK; 00180 return static_cast<bray_t>(v) & BRAY_MASK; 00181 } 00182 00183 //inline static bray_t scaleAbs(scale_t s, bray_t b) { 00184 // return fromScale(s * b); 00185 //} 00186 00187 inline static bray_t scale(scale_t s, bray_t b) { 00188 if(mask(b) < DEG180) { 00189 return fromScale(s * b); 00190 } 00191 else { 00192 return negate(fromScale(s * negate(mask(b)))); 00193 } 00194 } 00195 00196 inline static bray_t invert(bray_t b) { 00197 return mask(b + DEG180); 00198 } 00199 00200 inline static bray_t negate(bray_t b) { 00201 return mask(DEG360 - b); 00202 } 00203 00204 }; 00205 00206 } 00207 00208 #endif
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.