Changeset 103


Ignore:
Timestamp:
Oct 13, 2005 3:25:45 PM (19 years ago)
Author:
dkearney
Message:

added higher order conversions so now you can do stuff like
convert("1m3","ft3") and get the result 35.3147ft3
or convert("1m3","cm3") and get the result 1e+06cm3
also added units gal (for us gallon) and ft3 (for cubic feet) and
related conversions to cubic meters
In Makefile, added -lm to compile librappture.so against math library

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile

    r101 r103  
    8787librappture: $(RP_IO_DEPS) $(RP_UNITS_DEPS) $(RP_OTHER_DEPS)
    8888        $(CXX) $(DEGUG) -shared -Wl,-rpath,$(LIB_DIR)/ \
    89                 -Wl,-soname,$@.so -o $(LIB_DIR)/$@.so.0.0 $^ $(LIB_SCEW_FLAG)
     89                -Wl,-soname,$@.so -o $(LIB_DIR)/$@.so.0.0 $^ $(LIB_SCEW_FLAG) -lm
    9090
    9191        /sbin/ldconfig -n $(LIB_DIR)
  • trunk/src/core/RpUnits.cc

    r102 r103  
    444444RpUnits * RpUnits::define(  RpUnits* from,
    445445                            RpUnits* to,
     446                            double (*convForwFxnPtr)(double,double),
     447                            double (*convBackFxnPtr)(double,double))
     448{
     449    RpUnits* conv = new RpUnits(    from,
     450                                    to,
     451                                    convForwFxnPtr,
     452                                    convBackFxnPtr,
     453                                    NULL,
     454                                    NULL);
     455
     456    return conv;
     457}
     458
     459RpUnits * RpUnits::define(  RpUnits* from,
     460                            RpUnits* to,
    446461                            void* (*convForwFxnPtr)(void*, void*),
    447462                            void* convForwData,
     
    601616 *
    602617 * **********************************************************************/
     618/*
    603619int RpUnits::makeMetric(RpUnits * basis) {
    604620
     
    661677    return (1);
    662678}
     679*/
     680
     681int RpUnits::makeMetric(RpUnits * basis) {
     682
     683    if (!basis) {
     684        return 0;
     685    }
     686
     687    std::string basisName = basis->getUnitsName();
     688    std::string name;
     689    std::string forw, back;
     690
     691    name = "c" + basisName;
     692    RpUnits * centi = RpUnits::define(name, basis);
     693    RpUnits::define(centi, basis, centi2base, base2centi);
     694
     695    name = "m" + basisName;
     696    RpUnits * milli = RpUnits::define(name, basis);
     697    RpUnits::define(milli, basis, milli2base, base2milli);
     698
     699    name = "u" + basisName;
     700    RpUnits * micro = RpUnits::define(name, basis);
     701    RpUnits::define(micro, basis, micro2base, base2micro);
     702
     703    name = "n" + basisName;
     704    RpUnits * nano  = RpUnits::define(name, basis);
     705    RpUnits::define(nano, basis, nano2base, base2nano);
     706
     707    name = "p" + basisName;
     708    RpUnits * pico  = RpUnits::define(name, basis);
     709    RpUnits::define(pico, basis, pico2base, base2pico);
     710
     711    name = "f" + basisName;
     712    RpUnits * femto = RpUnits::define(name, basis);
     713    RpUnits::define(femto, basis, femto2base, base2femto);
     714
     715    name = "a" + basisName;
     716    RpUnits * atto  = RpUnits::define(name, basis);
     717    RpUnits::define(atto, basis, atto2base, base2atto);
     718
     719    name = "k" + basisName;
     720    RpUnits * kilo  = RpUnits::define(name, basis);
     721    RpUnits::define(kilo, basis, kilo2base, base2kilo);
     722
     723    name = "M" + basisName;
     724    RpUnits * mega  = RpUnits::define(name, basis);
     725    RpUnits::define(mega, basis, mega2base, base2mega);
     726
     727    name = "G" + basisName;
     728    RpUnits * giga  = RpUnits::define(name, basis);
     729    RpUnits::define(giga, basis, giga2base, base2giga);
     730
     731    name = "T" + basisName;
     732    RpUnits * tera  = RpUnits::define(name, basis);
     733    RpUnits::define(tera, basis, tera2base, base2tera);
     734
     735    name = "P" + basisName;
     736    RpUnits * peta  = RpUnits::define(name, basis);
     737    RpUnits::define(peta, basis, peta2base, base2peta);
     738
     739    return (1);
     740}
     741
     742
     743RpUnits*
     744RpUnits::find(std::string key)
     745{
     746    // dict.find seems to return a (RpUnits* const) so i had to
     747    // cast it as a (RpUnits*)
     748
     749    // dict pointer
     750    RpUnits* unitEntry = (RpUnits*) *(dict->find(key).getValue());
     751
     752    // dict pointer
     753    if (unitEntry == (RpUnits*)dict->getNullEntry().getValue()) {
     754        unitEntry = NULL;
     755    }
     756
     757    return unitEntry;
     758}
     759
    663760
    664761// convert function so people can just send in two strings and
     
    768865
    769866    if (showUnits) {
    770         unitText << retVal << toUnits->getUnits();
     867        unitText << retVal << toUnits->getUnitsName();
    771868    }
    772869    else {
     
    804901    // guard against converting to the units you are converting from...
    805902    // ie. meters->meters
    806     if (this->getUnits() == toUnit->getUnits()) {
     903    if (this->getUnitsName() == toUnit->getUnitsName()) {
    807904        if (result) {
    808905            *result = 0;
     
    816913    // need to rethink this.
    817914    //
    818     if ( (basis) && (basis->getUnits() != toUnit->getUnits()) ) {
     915    if ( (basis) && (basis->getUnitsName() != toUnit->getUnitsName()) ) {
    819916        value = convert(basis,value,&my_result);
    820917        if (my_result == 0) {
     
    827924    // and convert between basis' and then convert again back to the
    828925    // original unit.
    829     if ( (toBasis) && (toBasis->getUnits() != fromUnit->getUnits()) ) {
    830         dictToUnit = find(toBasis->getUnits());
     926    if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) {
     927        dictToUnit = find(toBasis->getUnitsName());
    831928    }
    832929    else {
    833         dictToUnit = find(toUnit->getUnits());
     930        dictToUnit = find(toUnit->getUnitsName());
    834931    }
    835932
     
    862959            // call the function pointer with value
    863960
    864             value = p->conv->convForwFxnPtr(value);
     961            // this should probably be re thought out
     962            // the problem is that convForwFxnPtr has the conversion for a
     963            // one arg conv function pointer and convForwFxnPtrDD has the
     964            // conversion for a two arg conv function pointer
     965            // need to make this simpler, more logical maybe only allow 2 arg
     966            if (       (p->conv->convForwFxnPtr)
     967                    && (! p->conv->convForwFxnPtrDD) ) {
     968
     969                value = p->conv->convForwFxnPtr(value);
     970            }
     971            else if (  (p->conv->convForwFxnPtrDD)
     972                    && (! p->conv->convForwFxnPtr) ) {
     973
     974                value =
     975                    p->conv->convForwFxnPtrDD(value, fromUnit->getExponent());
     976            }
    865977
    866978            // check to see if we converted to the actual requested unit
     
    869981            // do one last conversion from the requested unit's basis back
    870982            // to the requested unit.
    871             if ( (toBasis) && (toBasis->getUnits() != fromUnit->getUnits()) ) {
     983            if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) {
    872984                my_result = 0;
    873985                value = toBasis->convert(toUnit,value,&my_result);
     
    8901002            // call the function pointer with value
    8911003
    892             value = p->conv->convBackFxnPtr(value);
     1004            // this should probably be re thought out
     1005            // the problem is that convForwFxnPtr has the conversion for a
     1006            // one arg conv function pointer and convForwFxnPtrDD has the
     1007            // conversion for a two arg conv function pointer
     1008            // need to make this simpler, more logical maybe only allow 2 arg
     1009            if (       (p->conv->convBackFxnPtr)
     1010                    && (! p->conv->convBackFxnPtrDD) ) {
     1011
     1012                value = p->conv->convBackFxnPtr(value);
     1013            }
     1014            else if (  (p->conv->convBackFxnPtrDD)
     1015                    && (! p->conv->convBackFxnPtr) ) {
     1016
     1017                value =
     1018                    p->conv->convBackFxnPtrDD(value, fromUnit->getExponent());
     1019            }
    8931020
    8941021            // check to see if we converted to the actual requested unit
     
    8971024            // do one last conversion from the requested unit's basis back
    8981025            // to the requested unit.
    899             if ( (toBasis) && (toBasis->getUnits() != fromUnit->getUnits()) ) {
     1026            if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) {
    9001027                my_result = 0;
    9011028                value = toBasis->convert(toUnit,value,&my_result);
     
    9551082    // guard against converting to the units you are converting from...
    9561083    // ie. meters->meters
    957     if (this->getUnits() == toUnit->getUnits()) {
     1084    if (this->getUnitsName() == toUnit->getUnitsName()) {
    9581085        if (result) {
    9591086            *result = 0;
     
    9671094    // need to rethink this.
    9681095    //
    969     if ( (basis) && (basis->getUnits() != toUnit->getUnits()) ) {
     1096    if ( (basis) && (basis->getUnitsName() != toUnit->getUnitsName()) ) {
    9701097        value = convert(basis,value,&my_result);
    9711098        if (my_result == 0) {
     
    9781105    // and convert between basis' and then convert again back to the
    9791106    // original unit.
    980     if ( (toBasis) && (toBasis->getUnits() != fromUnit->getUnits()) ) {
    981         dictToUnit = find(toBasis->getUnits());
     1107    if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) {
     1108        dictToUnit = find(toBasis->getUnitsName());
    9821109    }
    9831110    else {
    984         dictToUnit = find(toUnit->getUnits());
     1111        dictToUnit = find(toUnit->getUnitsName());
    9851112    }
    9861113
     
    10201147            // do one last conversion from the requested unit's basis back
    10211148            // to the requested unit.
    1022             if ( (toBasis) && (toBasis->getUnits() != fromUnit->getUnits()) ) {
     1149            if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) {
    10231150                my_result = 0;
    10241151                value = toBasis->convert(toUnit,value,&my_result);
     
    10481175            // do one last conversion from the requested unit's basis back
    10491176            // to the requested unit.
    1050             if ( (toBasis) && (toBasis->getUnits() != fromUnit->getUnits()) ) {
     1177            if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) {
    10511178                my_result = 0;
    10521179                value = toBasis->convert(toUnit,value,&my_result);
     
    12791406    result += addPresetLength();
    12801407    result += addPresetEnergy();
     1408    result += addPresetVolume();
    12811409
    12821410    return 0;
     
    13531481}
    13541482
     1483// return codes: 0 success, anything else is error
     1484int
     1485RpUnits::addPresetVolume () {
     1486
     1487    RpUnits* cubic_meter  = RpUnits::define("m3", NULL);
     1488    RpUnits* cubic_feet   = RpUnits::define("ft3", NULL);
     1489    RpUnits* us_gallon    = RpUnits::define("gal", NULL);
     1490
     1491    RpUnits::makeMetric(cubic_meter);
     1492
     1493    // add energy definitions
     1494    RpUnits::define(cubic_meter,cubic_feet,meter2feet,feet2meter);
     1495    RpUnits::define(cubic_meter,us_gallon,cubicMeter2usGallon,usGallon2cubicMeter);
     1496    RpUnits::define(cubic_feet,us_gallon,cubicFeet2usGallon,usGallon2cubicFeet);
     1497
     1498    return 0;
     1499}
     1500
    13551501// -------------------------------------------------------------------- //
    13561502
  • trunk/src/core/RpUnitsStd.cc

    r83 r103  
     1#include <RpUnitsStd.h>
     2#include <math.h>
     3
    14#ifdef __cplusplus
    25extern "C" {
     
    710 ****************************************/
    811
    9 double centi2base (double centi)
    10 {
    11    return centi*1e-2;
    12 }
    13 
    14 double milli2base (double milli)
    15 {
    16     return milli*1e-3;
    17 }
    18 
    19 double micro2base (double micro)
    20 {
    21     return micro*1e-6;
    22 }
    23 
    24 double nano2base (double nano)
    25 {
    26     return nano*1e-9;
    27 }
    28 
    29 double pico2base (double pico)
    30 {
    31     return pico*1e-12;
    32 }
    33 
    34 double femto2base (double femto)
    35 {
    36     return femto*1e-15;
    37 }
    38 
    39 double atto2base (double atto)
    40 {
    41     return atto*1e-18;
    42 }
    43 
    44 double kilo2base (double kilo)
    45 {
    46     return kilo*1e3;
    47 }
    48 
    49 double mega2base (double mega)
    50 {
    51     return mega*1e6;
    52 }
    53 
    54 double giga2base (double giga)
    55 {
    56     return giga*1e9;
    57 }
    58 
    59 double tera2base (double tera)
    60 {
    61     return tera*1e12;
    62 }
    63 
    64 double peta2base (double peta)
    65 {
    66     return peta*1e15;
    67 }
    68 
    69 double base2centi (double base)
    70 {
    71     return base*1e2;
    72 }
    73 
    74 double base2milli (double base)
    75 {
    76     return base*1e3;
    77 }
    78 
    79 double base2micro (double base)
    80 {
    81     return base*1e6;
    82 }
    83 
    84 double base2nano (double base)
    85 {
    86     return base*1e9;
    87 }
    88 
    89 double base2pico (double base)
    90 {
    91     return base*1e12;
    92 }
    93 
    94 double base2femto (double base)
    95 {
    96     return base*1e15;
    97 }
    98 
    99 double base2atto (double base)
    100 {
    101     return base*1e18;
    102 }
    103 
    104 double base2kilo (double base)
    105 {
    106     return base*1e-3;
    107 }
    108 
    109 double base2mega (double base)
    110 {
    111     return base*1e-6;
    112 }
    113 
    114 double base2giga (double base)
    115 {
    116     return base*1e-9;
    117 }
    118 
    119 double base2tera (double base)
    120 {
    121     return base*1e-12;
    122 }
    123 
    124 double base2peta (double base)
    125 {
    126     return base*1e-15;
    127 }
    128 
    129 
     12
     13double centi2base (double centi, double power)
     14{
     15   return centi*pow(1e-2,power);
     16}
     17
     18double milli2base (double milli, double power)
     19{
     20    return milli*pow(1e-3,power);
     21}
     22
     23double micro2base (double micro, double power)
     24{
     25    return micro*pow(1e-6,power);
     26}
     27
     28double nano2base (double nano, double power)
     29{
     30    return nano*pow(1e-9,power);
     31}
     32
     33double pico2base (double pico, double power)
     34{
     35    return pico*pow(1e-12,power);
     36}
     37
     38double femto2base (double femto, double power)
     39{
     40    return femto*pow(1e-15,power);
     41}
     42
     43double atto2base (double atto, double power)
     44{
     45    return atto*pow(1e-18,power);
     46}
     47
     48double kilo2base (double kilo, double power)
     49{
     50    return kilo*pow(1e3,power);
     51}
     52
     53double mega2base (double mega, double power)
     54{
     55    return mega*pow(1e6,power);
     56}
     57
     58double giga2base (double giga, double power)
     59{
     60    return giga*pow(1e9,power);
     61}
     62
     63double tera2base (double tera, double power)
     64{
     65    return tera*pow(1e12,power);
     66}
     67
     68double peta2base (double peta, double power)
     69{
     70    return peta*pow(1e15,power);
     71}
     72
     73double base2centi (double base, double power)
     74{
     75    return base*pow(1e2,power);
     76}
     77
     78double base2milli (double base, double power)
     79{
     80    return base*pow(1e3,power);
     81}
     82
     83double base2micro (double base, double power)
     84{
     85    return base*pow(1e6,power);
     86}
     87
     88double base2nano (double base, double power)
     89{
     90    return base*pow(1e9,power);
     91}
     92
     93double base2pico (double base, double power)
     94{
     95    return base*pow(1e12,power);
     96}
     97
     98double base2femto (double base, double power)
     99{
     100    return base*pow(1e15,power);
     101}
     102
     103double base2atto (double base, double power)
     104{
     105    return base*pow(1e18,power);
     106}
     107
     108double base2kilo (double base, double power)
     109{
     110    return base*pow(1e-3,power);
     111}
     112
     113double base2mega (double base, double power)
     114{
     115    return base*pow(1e-6,power);
     116}
     117
     118double base2giga (double base, double power)
     119{
     120    return base*pow(1e-9,power);
     121}
     122
     123double base2tera (double base, double power)
     124{
     125    return base*pow(1e-12,power);
     126}
     127
     128double base2peta (double base, double power)
     129{
     130    return base*pow(1e-15,power);
     131}
    130132
    131133/****************************************
     
    135137 ****************************************/
    136138
    137 double angstrom2meter (double angstrom)
    138 {
    139         return angstrom*1.0e-10;
    140 }
    141 
    142 double meter2angstrom (double meters)
    143 {
    144         return meters*1.0e10;
    145 }
    146 
    147 double meter2inch (double m)
    148 {
    149         return (m*39.37008);
    150 }
    151 
    152 double inch2meter (double in)
    153 {
    154         return (in/39.37008);
    155 }
    156 
    157 double meter2feet (double m)
    158 {
    159         return (m*3.280840);
    160 }
    161 
    162 double feet2meter (double ft)
    163 {
    164         return (ft/3.280840);
    165 }
    166 
    167 double meter2yard (double m)
    168 {
    169         return (m*1.093613);
    170 }
    171 
    172 double yard2meter (double yd)
    173 {
    174         return (yd/1.093613);
     139double angstrom2meter (double angstrom, double power)
     140{
     141        return angstrom*(pow(1.0e-10,power));
     142}
     143
     144double meter2angstrom (double meters, double power)
     145{
     146        return meters*(pow(1.0e10,power));
     147}
     148
     149double meter2inch (double m, double power)
     150{
     151        return (m*(pow(39.37008,power)));
     152}
     153
     154double inch2meter (double in, double power)
     155{
     156        return (in/(pow(39.37008,power)));
     157}
     158
     159double meter2feet (double m, double power)
     160{
     161        return (m*(pow(3.280840,power)));
     162}
     163
     164double feet2meter (double ft, double power)
     165{
     166        return (ft/(pow(3.280840,power)));
     167}
     168
     169double meter2yard (double m, double power)
     170{
     171        return (m*(pow(1.093613,power)));
     172}
     173
     174double yard2meter (double yd, double power)
     175{
     176        return (yd/(pow(1.093613,power)));
    175177}
    176178
     
    233235}
    234236
     237/****************************************
     238 * MISC VOLUME CONVERSIONS
     239 ****************************************/
     240
     241double cubicMeter2usGallon (double m3, double none)
     242{
     243        return (m3*264.1721);
     244}
     245
     246double usGallon2cubicMeter (double g, double none)
     247{
     248        return (g/264.1721);
     249}
     250
     251double cubicFeet2usGallon (double ft3, double none)
     252{
     253        return (ft3*7.48051);
     254}
     255
     256double usGallon2cubicFeet (double g, double none)
     257{
     258        return (g/7.48051);
     259}
     260
     261
    235262#ifdef __cplusplus
    236263}
Note: See TracChangeset for help on using the changeset viewer.