Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/pandatoolbase/distanceUnit.cxx

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

Generated on Fri May 2 03:21:27 2003 for Panda-Tool by doxygen1.3