Changeset 51

Show
Ignore:
Timestamp:
07/21/07 00:46:34 (1 year ago)
Author:
JoeCoder
Message:

Added yage.core.math (split from yage.core.misc) and yage.core.types (incomplete).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/yage/core/all.d

    r32 r51  
    1616{   import yage.core.array; 
    1717    import yage.core.freelist; 
     18    import yage.core.math; 
    1819    import yage.core.matrix; 
    1920    import yage.core.misc; 
     
    2324    import yage.core.repeater; 
    2425    import yage.core.timer; 
     26    import yage.core.types; 
    2527    import yage.core.vector; 
    2628    import yage.core.xml; 
  • trunk/src/yage/core/matrix.d

    r28 r51  
    1212import yage.core.vector; 
    1313import yage.core.quatrn; 
     14import yage.core.math; 
    1415import yage.core.misc; 
    1516import yage.core.parse; 
     
    120121    bool almostEqual(Matrix s, float fudge=0.0001) 
    121122    {   for (int i=0; i<v.length; i++) 
    122             if (!yage.core.misc.almostEqual(v[i], s.v[i], fudge)) 
     123            if (!yage.core.math.almostEqual(v[i], s.v[i], fudge)) 
    123124                return false; 
    124125        return true; 
  • trunk/src/yage/core/misc.d

    r38 r51  
    1919// extern (C) void *memcpy(void *, void *, uint); 
    2020 
    21 const float PI_180 = 0.01745329251994;  // PI / 180 
    22 const float _180_PI = 57.2957795130823; // 180 / pi 
    2321 
    2422 
    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 word 
    35 {   short s;        /// Union of various types. 
    36     ushort us;      /// ditto 
    37     byte[2] b;      /// ditto 
    38     ubyte[2] ub;    /// ditto 
    39     char[2] c;      /// ditto 
    40      
    41     /// Convert ushort to word 
    42     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 another 
    50 struct dword 
    51 {   union 
    52     {   int i;          /// Union of various types. 
    53         uint ui;        /// ditto 
    54         float f;        /// ditto 
    55         short[2] s;     /// ditto 
    56         ushort[2] us;   /// ditto 
    57         byte[4] b;      /// ditto 
    58         ubyte[4] ub;    /// ditto 
    59         char[4] c;      /// ditto 
    60     } 
    61  
    62     /// Convert uint to dword 
    63     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 qword 
    72 {   long l;         /// Union of various types. 
    73     ulong ul;       /// ditto 
    74     double d;       /// ditto 
    75     float[2] f;     /// ditto 
    76     int[2] i;       /// ditto 
    77     uint[2] ui;     /// ditto 
    78     short[4] s;     /// ditto 
    79     ushort[4] us;   /// ditto 
    80     byte[8] b;      /// ditto 
    81     ubyte[8] ub;    /// ditto 
    82     char[8] c;      /// ditto 
    83 } 
    84  
    85 /** 
    86  * Check if two floats are almost equal, that is, they differ no more than 
    87  * 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 if 
    89  * either of those differ more, they are not considered equal.  Also, 
    90  * numbers with an absolute difference less than or equal to fudge will always 
    91  * 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     }else 
    100         if (fabs((a-b)/b) <= fudge) 
    101             return true; 
    102     return false; 
    103 } 
    104 unittest 
    105 {   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 } 
    11223 
    11324/// Given relative path rel_path, returns an absolute path. 
     
    12940    chdir(cur_path); 
    13041    return result~filename; 
    131 } 
    132  
    133 /// Clamp v between l and u 
    134 T clamp(T)(T v, T lower, T upper) 
    135 {   if (v<lower) return lower; 
    136     if (v>upper) return upper; 
    137     return v; 
    13842} 
    13943 
     
    16973} 
    17074 
    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 two 
    180         return input; 
    181     return 2 << msb; 
    182 } 
    183 unittest 
    184 {   assert(nextPow2(9) == 16); 
    185     assert(nextPow2(1) == 1);   // 2^0 == 1 
    186     assert(nextPow2(16) == 16); 
    187 } 
    188  
    189 /// Map a value from one range to another 
    190 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 arguments  
    196 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 unittest 
    204 {   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 unittest 
    216 {   assert(min(3, 4, -1) == -1); 
    217 } 
    218  
    219  
    22075/// 
    22176long getCPUCount() 
     
    23893    printf("\n"); 
    23994} 
    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  
    150150    bool almostEqual(Quatrn s, float fudge=0.0001) 
    151151    {   for (int i=0; i<v.length; i++) 
    152             if (!yage.core.misc.almostEqual(v[i], s.v[i], fudge)) 
     152            if (!yage.core.math.almostEqual(v[i], s.v[i], fudge)) 
    153153                return false; 
    154154        return true; 
  • trunk/src/yage/core/vector.d

    r32 r51  
    99import std.math; 
    1010import std.stdio; 
    11 import yage.core.parse
     11import yage.core.all
    1212import yage.core.matrix; 
     13import yage.core.math; 
    1314import yage.core.quatrn; 
    14 import yage.core.misc; 
    15  
    16  
    1715/** 
    1816 * This is a template to create a vector of any type with any number of elements. 
     
    291289    bool almostEqual(Vec3f s, float fudge=0.0001) 
    292290    {   for (int i=0; i<v.length; i++) 
    293             if (!yage.core.misc.almostEqual(v[i], s.v[i], fudge)) 
     291            if (!yage.core.math.almostEqual(v[i], s.v[i], fudge)) 
    294292                return false; 
    295293        return true; 
  • trunk/src/yage/node/light.d

    r31 r51  
    1111import derelict.opengl.gl; 
    1212import derelict.opengl.glext; 
    13 import yage.core.vector; 
    14 import yage.core.misc; 
     13import yage.core.all; 
    1514import yage.resource.material; 
    1615import yage.node.base; 
  • trunk/src/yage/node/sound.d

    r32 r51  
    1111import std.stdio; 
    1212import derelict.openal.al; 
    13 import yage.core.misc
     13import yage.core.math
    1414import yage.resource.resource; 
    1515import yage.resource.sound; 
  • trunk/src/yage/resource/texture.d

    r45 r51  
    1616import derelict.opengl.glu; 
    1717import derelict.opengl.glext; 
    18 import yage.core.misc
     18import yage.core.math
    1919import yage.core.vector; 
    2020import yage.resource.resource; 
  • trunk/src/yage/util/flyer.d

    r49 r51  
    77module yage.util.flyer; 
    88 
    9 import yage.core.matrix; 
    10 import yage.core.misc; 
    11 import yage.core.vector; 
     9import yage.core.all; 
    1210import yage.node.base; 
    1311import yage.node.camera;