Changeset 51
- Timestamp:
- 07/21/07 00:46:34 (1 year ago)
- Files:
-
- trunk/bin/yage3d.exe (modified) (previous)
- trunk/src/yage/core/all.d (modified) (2 diffs)
- trunk/src/yage/core/math.d (added)
- trunk/src/yage/core/matrix.d (modified) (2 diffs)
- trunk/src/yage/core/misc.d (modified) (4 diffs)
- trunk/src/yage/core/quatrn.d (modified) (1 diff)
- trunk/src/yage/core/types.d (added)
- trunk/src/yage/core/vector.d (modified) (2 diffs)
- trunk/src/yage/node/light.d (modified) (1 diff)
- trunk/src/yage/node/sound.d (modified) (1 diff)
- trunk/src/yage/resource/texture.d (modified) (1 diff)
- trunk/src/yage/util/flyer.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/yage/core/all.d
r32 r51 16 16 { import yage.core.array; 17 17 import yage.core.freelist; 18 import yage.core.math; 18 19 import yage.core.matrix; 19 20 import yage.core.misc; … … 23 24 import yage.core.repeater; 24 25 import yage.core.timer; 26 import yage.core.types; 25 27 import yage.core.vector; 26 28 import yage.core.xml; trunk/src/yage/core/matrix.d
r28 r51 12 12 import yage.core.vector; 13 13 import yage.core.quatrn; 14 import yage.core.math; 14 15 import yage.core.misc; 15 16 import yage.core.parse; … … 120 121 bool almostEqual(Matrix s, float fudge=0.0001) 121 122 { for (int i=0; i<v.length; i++) 122 if (!yage.core.m isc.almostEqual(v[i], s.v[i], fudge))123 if (!yage.core.math.almostEqual(v[i], s.v[i], fudge)) 123 124 return false; 124 125 return true; trunk/src/yage/core/misc.d
r38 r51 19 19 // extern (C) void *memcpy(void *, void *, uint); 20 20 21 const float PI_180 = 0.01745329251994; // PI / 18022 const float _180_PI = 57.2957795130823; // 180 / pi23 21 24 22 25 /**26 * Allow for easy bit-by-bit conversion from one two-byte type to another.27 * Example:28 * --------------------------------29 * short a;30 * word w;31 * w.s = a;32 * char c = w.c[1]; // c is the second byte of a.33 * --------------------------------*/34 union word35 { short s; /// Union of various types.36 ushort us; /// ditto37 byte[2] b; /// ditto38 ubyte[2] ub; /// ditto39 char[2] c; /// ditto40 41 /// Convert ushort to word42 static word opApply(ushort us)43 { word res;44 res.us = us;45 return res;46 }47 }48 49 /// Allow for easy bit-by-bit conversion from one four-byte type to another50 struct dword51 { union52 { int i; /// Union of various types.53 uint ui; /// ditto54 float f; /// ditto55 short[2] s; /// ditto56 ushort[2] us; /// ditto57 byte[4] b; /// ditto58 ubyte[4] ub; /// ditto59 char[4] c; /// ditto60 }61 62 /// Convert uint to dword63 static dword opApply(uint ui)64 { dword res;65 res.ui = ui;66 return res;67 }68 }69 70 /// Allow for easy bit-by-bit conversion from one eight-byte type to another.71 union qword72 { long l; /// Union of various types.73 ulong ul; /// ditto74 double d; /// ditto75 float[2] f; /// ditto76 int[2] i; /// ditto77 uint[2] ui; /// ditto78 short[4] s; /// ditto79 ushort[4] us; /// ditto80 byte[8] b; /// ditto81 ubyte[8] ub; /// ditto82 char[8] c; /// ditto83 }84 85 /**86 * Check if two floats are almost equal, that is, they differ no more than87 * fudge from one another, relatively speaking. If fudge is 0.0001 (the default)88 * Then 10000 and 10001 will compare equally and so will 1.000 and 1.0001; but if89 * either of those differ more, they are not considered equal. Also,90 * numbers with an absolute difference less than or equal to fudge will always91 * compare equal. This allows 0.00001 and 0 to be almost equal. */92 bool almostEqual(float a, float b, float fudge=0.0001)93 { if (fabs(a-b) <= fudge)94 return true;95 96 if (fabs(b) > fabs(a))97 { if (fabs((a-b)/b) <= fudge)98 return true;99 }else100 if (fabs((a-b)/b) <= fudge)101 return true;102 return false;103 }104 unittest105 { assert(almostEqual(0, 0.0001));106 assert(almostEqual(0, -0.0001));107 assert(almostEqual(1, 1.000099));108 assert(almostEqual(1000, 1000.1));109 assert(!almostEqual(10000, 10001.01));110 //assert(almostEqual(float.infinity, float.infinity));111 }112 23 113 24 /// Given relative path rel_path, returns an absolute path. … … 129 40 chdir(cur_path); 130 41 return result~filename; 131 }132 133 /// Clamp v between l and u134 T clamp(T)(T v, T lower, T upper)135 { if (v<lower) return lower;136 if (v>upper) return upper;137 return v;138 42 } 139 43 … … 169 73 } 170 74 171 /**172 * Returns the first integer n such that 2^n >= input.173 * Example:174 * nextPow2(9); // returns 16 */175 uint nextPow2(uint input)176 { if (0 == input)177 return 1;178 int msb = std.intrinsic.bsr(input); // get first bit set, starting with most significant.179 if ((1 << msb) == input) // If already equal to a power of two180 return input;181 return 2 << msb;182 }183 unittest184 { assert(nextPow2(9) == 16);185 assert(nextPow2(1) == 1); // 2^0 == 1186 assert(nextPow2(16) == 16);187 }188 189 /// Map a value from one range to another190 float map(float v, float oldmin, float oldmax, float newmin, float newmax)191 { return ((newmax-newmin)*v/(oldmax-oldmin))+newmin;192 }193 194 195 /// Return the maximum of all arguments196 T[0] max(T...)(T a)197 { typeof(a[0]) max = a[0];198 foreach(T x; a)199 if (x>max)200 max = x;201 return max;202 }203 unittest204 { assert(max(3, 4, -1) == 4);205 }206 207 /// Return the minimum of all arguments.208 T[0] min(T...)(T a)209 { typeof(a[0]) min = a[0];210 foreach (T x; a)211 if (x<min)212 min = x;213 return min;214 }215 unittest216 { assert(min(3, 4, -1) == -1);217 }218 219 220 75 /// 221 76 long getCPUCount() … … 238 93 printf("\n"); 239 94 } 240 241 /// Generate a random number between min and max.242 float random(float min, float max)243 { return (rand()/4294967296.0f)*(max-min)+min;244 }245 trunk/src/yage/core/quatrn.d
r28 r51 150 150 bool almostEqual(Quatrn s, float fudge=0.0001) 151 151 { for (int i=0; i<v.length; i++) 152 if (!yage.core.m isc.almostEqual(v[i], s.v[i], fudge))152 if (!yage.core.math.almostEqual(v[i], s.v[i], fudge)) 153 153 return false; 154 154 return true; trunk/src/yage/core/vector.d
r32 r51 9 9 import std.math; 10 10 import std.stdio; 11 import yage.core. parse;11 import yage.core.all; 12 12 import yage.core.matrix; 13 import yage.core.math; 13 14 import yage.core.quatrn; 14 import yage.core.misc;15 16 17 15 /** 18 16 * This is a template to create a vector of any type with any number of elements. … … 291 289 bool almostEqual(Vec3f s, float fudge=0.0001) 292 290 { for (int i=0; i<v.length; i++) 293 if (!yage.core.m isc.almostEqual(v[i], s.v[i], fudge))291 if (!yage.core.math.almostEqual(v[i], s.v[i], fudge)) 294 292 return false; 295 293 return true; trunk/src/yage/node/light.d
r31 r51 11 11 import derelict.opengl.gl; 12 12 import derelict.opengl.glext; 13 import yage.core.vector; 14 import yage.core.misc; 13 import yage.core.all; 15 14 import yage.resource.material; 16 15 import yage.node.base; trunk/src/yage/node/sound.d
r32 r51 11 11 import std.stdio; 12 12 import derelict.openal.al; 13 import yage.core.m isc;13 import yage.core.math; 14 14 import yage.resource.resource; 15 15 import yage.resource.sound; trunk/src/yage/resource/texture.d
r45 r51 16 16 import derelict.opengl.glu; 17 17 import derelict.opengl.glext; 18 import yage.core.m isc;18 import yage.core.math; 19 19 import yage.core.vector; 20 20 import yage.resource.resource; trunk/src/yage/util/flyer.d
r49 r51 7 7 module yage.util.flyer; 8 8 9 import yage.core.matrix; 10 import yage.core.misc; 11 import yage.core.vector; 9 import yage.core.all; 12 10 import yage.node.base; 13 11 import yage.node.camera;
