Panda3D
|
00001 // Filename: distanceUnit.cxx 00002 // Created by: drose (17Apr01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "distanceUnit.h" 00016 #include "config_pandatoolbase.h" 00017 #include "string_utils.h" 00018 #include "pnotify.h" 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: format_abbrev_unit 00022 // Description: Returns the string representing the common 00023 // abbreviation for the given unit. 00024 //////////////////////////////////////////////////////////////////// 00025 string 00026 format_abbrev_unit(DistanceUnit unit) { 00027 switch (unit) { 00028 case DU_millimeters: 00029 return "mm"; 00030 00031 case DU_centimeters: 00032 return "cm"; 00033 00034 case DU_meters: 00035 return "m"; 00036 00037 case DU_kilometers: 00038 return "km"; 00039 00040 case DU_yards: 00041 return "yd"; 00042 00043 case DU_feet: 00044 return "ft"; 00045 00046 case DU_inches: 00047 return "in"; 00048 00049 case DU_nautical_miles: 00050 return "nmi"; 00051 00052 case DU_statute_miles: 00053 return "mi"; 00054 00055 case DU_invalid: 00056 return "invalid"; 00057 } 00058 nout << "**unexpected DistanceUnit value: (" << (int)unit << ")**"; 00059 return "**"; 00060 } 00061 00062 //////////////////////////////////////////////////////////////////// 00063 // Function: format_long_unit 00064 // Description: Returns the string representing the full name (plural) 00065 // for the given unit. 00066 //////////////////////////////////////////////////////////////////// 00067 string 00068 format_long_unit(DistanceUnit unit) { 00069 switch (unit) { 00070 case DU_millimeters: 00071 return "millimeters"; 00072 00073 case DU_centimeters: 00074 return "centimeters"; 00075 00076 case DU_meters: 00077 return "meters"; 00078 00079 case DU_kilometers: 00080 return "kilometers"; 00081 00082 case DU_yards: 00083 return "yards"; 00084 00085 case DU_feet: 00086 return "feet"; 00087 00088 case DU_inches: 00089 return "inches"; 00090 00091 case DU_nautical_miles: 00092 return "nautical miles"; 00093 00094 case DU_statute_miles: 00095 return "miles"; 00096 00097 case DU_invalid: 00098 return "invalid"; 00099 } 00100 nout << "**unexpected DistanceUnit value: (" << (int)unit << ")**"; 00101 return "**"; 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: DistanceUnit output operator 00106 // Description: 00107 //////////////////////////////////////////////////////////////////// 00108 ostream & 00109 operator << (ostream &out, DistanceUnit unit) { 00110 return out << format_abbrev_unit(unit); 00111 } 00112 00113 //////////////////////////////////////////////////////////////////// 00114 // Function: DistanceUnit input operator 00115 // Description: 00116 //////////////////////////////////////////////////////////////////// 00117 istream & 00118 operator >> (istream &in, DistanceUnit &unit) { 00119 string word; 00120 in >> word; 00121 unit = string_distance_unit(word); 00122 if (unit == DU_invalid) { 00123 pandatoolbase_cat->error() 00124 << "Invalid distance unit: " << word << "\n"; 00125 } 00126 return in; 00127 } 00128 00129 //////////////////////////////////////////////////////////////////// 00130 // Function: string_distance_unit 00131 // Description: Converts from a string, as might be input by the 00132 // user, to one of the known DistanceUnit types. 00133 // Returns DU_invalid if the string is unknown. 00134 //////////////////////////////////////////////////////////////////// 00135 DistanceUnit 00136 string_distance_unit(const string &str) { 00137 if (cmp_nocase(str, "mm") == 0 || cmp_nocase(str, "millimeters") == 0) { 00138 return DU_millimeters; 00139 00140 } else if (cmp_nocase(str, "cm") == 0 || cmp_nocase(str, "centimeters") == 0) { 00141 return DU_centimeters; 00142 00143 } else if (cmp_nocase(str, "m") == 0 || cmp_nocase(str, "meters") == 0) { 00144 return DU_meters; 00145 00146 } else if (cmp_nocase(str, "km") == 0 || cmp_nocase(str, "kilometers") == 0) { 00147 return DU_kilometers; 00148 00149 } else if (cmp_nocase(str, "yd") == 0 || cmp_nocase(str, "yards") == 0) { 00150 return DU_yards; 00151 00152 } else if (cmp_nocase(str, "ft") == 0 || cmp_nocase(str, "feet") == 0) { 00153 return DU_feet; 00154 00155 } else if (cmp_nocase(str, "in") == 0 || cmp_nocase(str, "inches") == 0) { 00156 return DU_inches; 00157 00158 } else if (cmp_nocase(str, "nmi") == 0 || 00159 cmp_nocase(str, "nm") == 0 || 00160 cmp_nocase_uh(str, "nautical_miles") == 0) { 00161 return DU_nautical_miles; 00162 00163 } else if (cmp_nocase(str, "mi") == 0 || 00164 cmp_nocase(str, "miles") == 0 || 00165 cmp_nocase_uh(str, "statute_miles") == 0) { 00166 return DU_statute_miles; 00167 00168 } else { 00169 return DU_invalid; 00170 } 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: unit_scale 00175 // Description: Returns the number of the indicated unit per each 00176 // centimeter. This internal function is used to 00177 // implement convert_units(), below. 00178 //////////////////////////////////////////////////////////////////// 00179 static double unit_scale(DistanceUnit unit) { 00180 switch (unit) { 00181 case DU_millimeters: 00182 return 0.1; 00183 00184 case DU_centimeters: 00185 return 1.0; 00186 00187 case DU_meters: 00188 return 100.0; 00189 00190 case DU_kilometers: 00191 return 100000.0; 00192 00193 case DU_yards: 00194 return 3.0 * 12.0 * 2.54; 00195 00196 case DU_feet: 00197 return 12.0 * 2.54; 00198 00199 case DU_inches: 00200 return 2.54; 00201 00202 case DU_nautical_miles: 00203 // This is the U.S. definition. 00204 return 185200.0; 00205 00206 case DU_statute_miles: 00207 return 5280.0 * 12.0 * 2.54; 00208 00209 case DU_invalid: 00210 return 1.0; 00211 } 00212 00213 return 1.0; 00214 } 00215 00216 //////////////////////////////////////////////////////////////////// 00217 // Function: convert_units 00218 // Description: Returns the scaling factor that must be applied to 00219 // convert from units of "from" to "to". 00220 //////////////////////////////////////////////////////////////////// 00221 double convert_units(DistanceUnit from, DistanceUnit to) { 00222 return unit_scale(from) / unit_scale(to); 00223 } 00224