Changeset 290 for trunk/thune/math.c

Show
Ignore:
Timestamp:
10/02/06 01:01:55 (2 years ago)
Author:
krobillard
Message:

Thune - Added 'minimum & 'maximum. Math operators work on vec3!.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/math.c

    r282 r290  
    3636 
    3737 
    38 #define MJ_INT_INT  0 
    39 #define MJ_INT_DEC  1 
    40 #define MJ_DEC_INT  2 
    41 #define MJ_DEC_DEC  3 
    42 #define MJ_NAN      4 
     38#define MJ_INT_INT      0 
     39#define MJ_INT_DEC      1 
     40#define MJ_DEC_INT      2 
     41#define MJ_DEC_DEC      3 
     42#define MJ_VEC3_DEC     4 
     43#define MJ_DEC_VEC3     5 
     44#define MJ_VEC3_VEC3    6 
     45#define MJ_NAN          7 
    4346 
    4447static int _mathJumpIndex( const UCell* a, const UCell* b ) 
     
    5760        if( ur_is(b, UT_DECIMAL) || ur_is(b, UT_TIME) ) 
    5861            return MJ_DEC_DEC; 
     62        if( ur_is(b, UT_VEC3) ) 
     63            return MJ_DEC_VEC3; 
     64    } 
     65    else if( ur_is(a, UT_VEC3) ) 
     66    { 
     67        if( ur_is(b, UT_VEC3) ) 
     68            return MJ_VEC3_VEC3; 
     69        if( ur_is(b, UT_DECIMAL) ) 
     70            return MJ_VEC3_DEC; 
    5971    } 
    6072    return MJ_NAN; 
     
    149161            ur_decimal(res) = ur_decimal(res) OP ur_decimal(tos); \ 
    150162            break; \ 
     163        case MJ_DEC_VEC3: {\ 
     164            double d = ur_decimal(res); \ 
     165            ur_initType(res, UT_VEC3); \ 
     166            res->vec3.xyz[0] = d OP tos->vec3.xyz[0]; \ 
     167            res->vec3.xyz[1] = d OP tos->vec3.xyz[1]; \ 
     168            res->vec3.xyz[2] = d OP tos->vec3.xyz[2]; \ 
     169            } break; \ 
     170        case MJ_VEC3_DEC: \ 
     171            res->vec3.xyz[0] = res->vec3.xyz[0] OP ur_decimal(tos); \ 
     172            res->vec3.xyz[1] = res->vec3.xyz[1] OP ur_decimal(tos); \ 
     173            res->vec3.xyz[2] = res->vec3.xyz[2] OP ur_decimal(tos); \ 
     174            break; \ 
     175        case MJ_VEC3_VEC3: \ 
     176            res->vec3.xyz[0] = res->vec3.xyz[0] OP tos->vec3.xyz[0]; \ 
     177            res->vec3.xyz[1] = res->vec3.xyz[1] OP tos->vec3.xyz[1]; \ 
     178            res->vec3.xyz[2] = res->vec3.xyz[2] OP tos->vec3.xyz[2]; \ 
     179            break; \ 
    151180        default: \ 
    152181            ur_throwErr( ur_thread, UR_EX_DATATYPE, \ 
    153                          "Math operator requires int!/decimal!" ); \ 
     182                         "Math operator requires int!/decimal!/vec3!" ); \ 
    154183            break; \ 
    155184    } 
     
    260289        ur_initType(res, UT_INT); 
    261290        ur_int(res) ^= ur_int(tos); 
     291    } 
     292} 
     293 
     294 
     295// (number number -- min) 
     296UR_CALL( uc_minimum ) 
     297{ 
     298    UCell* res; 
     299    UR_S_DROP; 
     300    res = UR_TOS; 
     301 
     302    switch( _mathJumpIndex(res, tos) ) 
     303    { 
     304        case MJ_INT_INT: 
     305            if( ur_int(res) > ur_int(tos) ) 
     306                ur_int(res) = ur_int(tos); 
     307            break; 
     308        case MJ_INT_DEC: 
     309        { 
     310            double d = ur_decimal(res); 
     311            if( d > ur_decimal(tos) ) 
     312            { 
     313                ur_initType(res, UT_DECIMAL); 
     314                ur_decimal(res) = ur_decimal(tos); 
     315            } 
     316        } 
     317            break; 
     318        case MJ_DEC_INT: 
     319        { 
     320            double d = ur_decimal(tos); 
     321            if( ur_decimal(res) > d ) 
     322                ur_decimal(res) = d; 
     323        } 
     324            break; 
     325        case MJ_DEC_DEC: 
     326            if( ur_decimal(res) > ur_decimal(tos) ) 
     327                ur_decimal(res) = ur_decimal(tos); 
     328            break; 
     329        case MJ_VEC3_VEC3: 
     330            if( res->vec3.xyz[0] > tos->vec3.xyz[0] ) 
     331                res->vec3.xyz[0] = tos->vec3.xyz[0]; 
     332            if( res->vec3.xyz[1] > tos->vec3.xyz[1] ) 
     333                res->vec3.xyz[1] = tos->vec3.xyz[1]; 
     334            if( res->vec3.xyz[2] > tos->vec3.xyz[2] ) 
     335                res->vec3.xyz[2] = tos->vec3.xyz[2]; 
     336            break; 
     337        default: 
     338            ur_throwErr( ur_thread, UR_EX_DATATYPE, 
     339                         "Invalid type for 'minimum" ); 
     340            break; 
     341    } 
     342} 
     343 
     344 
     345// (number number -- max) 
     346UR_CALL( uc_maximum ) 
     347{ 
     348    UCell* res; 
     349    UR_S_DROP; 
     350    res = UR_TOS; 
     351 
     352    switch( _mathJumpIndex(res, tos) ) 
     353    { 
     354        case MJ_INT_INT: 
     355            if( ur_int(res) < ur_int(tos) ) 
     356                ur_int(res) = ur_int(tos); 
     357            break; 
     358        case MJ_INT_DEC: 
     359        { 
     360            double d = ur_decimal(res); 
     361            if( d < ur_decimal(tos) ) 
     362            { 
     363                ur_initType(res, UT_DECIMAL); 
     364                ur_decimal(res) = ur_decimal(tos); 
     365            } 
     366        } 
     367            break; 
     368        case MJ_DEC_INT: 
     369        { 
     370            double d = ur_decimal(tos); 
     371            if( ur_decimal(res) < d ) 
     372                ur_decimal(res) = d; 
     373        } 
     374            break; 
     375        case MJ_DEC_DEC: 
     376            if( ur_decimal(res) < ur_decimal(tos) ) 
     377                ur_decimal(res) = ur_decimal(tos); 
     378            break; 
     379        case MJ_VEC3_VEC3: 
     380            if( res->vec3.xyz[0] < tos->vec3.xyz[0] ) 
     381                res->vec3.xyz[0] = tos->vec3.xyz[0]; 
     382            if( res->vec3.xyz[1] < tos->vec3.xyz[1] ) 
     383                res->vec3.xyz[1] = tos->vec3.xyz[1]; 
     384            if( res->vec3.xyz[2] < tos->vec3.xyz[2] ) 
     385                res->vec3.xyz[2] = tos->vec3.xyz[2]; 
     386            break; 
     387        default: 
     388            ur_throwErr( ur_thread, UR_EX_DATATYPE, 
     389                         "Invalid type for 'maximum" ); 
     390            break; 
    262391    } 
    263392} 
     
    651780    { uc_or,           "or" }, 
    652781    { uc_xor,          "xor" }, 
     782    { uc_minimum,      "minimum" }, 
     783    { uc_maximum,      "maximum" }, 
    653784    { uc_to_deg,       "to-deg" }, 
    654785    { uc_to_rad,       "to-rad" },