Panda3D
distanceUnit.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file distanceUnit.cxx
10  * @author drose
11  * @date 2001-04-17
12  */
13 
14 #include "distanceUnit.h"
15 #include "config_pandatoolbase.h"
16 #include "string_utils.h"
17 #include "pnotify.h"
18 
19 using std::istream;
20 using std::ostream;
21 using std::string;
22 
23 /**
24  * Returns the string representing the common abbreviation for the given unit.
25  */
26 string
28  switch (unit) {
29  case DU_millimeters:
30  return "mm";
31 
32  case DU_centimeters:
33  return "cm";
34 
35  case DU_meters:
36  return "m";
37 
38  case DU_kilometers:
39  return "km";
40 
41  case DU_yards:
42  return "yd";
43 
44  case DU_feet:
45  return "ft";
46 
47  case DU_inches:
48  return "in";
49 
50  case DU_nautical_miles:
51  return "nmi";
52 
53  case DU_statute_miles:
54  return "mi";
55 
56  case DU_invalid:
57  return "invalid";
58  }
59  nout << "**unexpected DistanceUnit value: (" << (int)unit << ")**";
60  return "**";
61 }
62 
63 /**
64  * Returns the string representing the full name (plural) for the given unit.
65  */
66 string
68  switch (unit) {
69  case DU_millimeters:
70  return "millimeters";
71 
72  case DU_centimeters:
73  return "centimeters";
74 
75  case DU_meters:
76  return "meters";
77 
78  case DU_kilometers:
79  return "kilometers";
80 
81  case DU_yards:
82  return "yards";
83 
84  case DU_feet:
85  return "feet";
86 
87  case DU_inches:
88  return "inches";
89 
90  case DU_nautical_miles:
91  return "nautical miles";
92 
93  case DU_statute_miles:
94  return "miles";
95 
96  case DU_invalid:
97  return "invalid";
98  }
99  nout << "**unexpected DistanceUnit value: (" << (int)unit << ")**";
100  return "**";
101 }
102 
103 /**
104  *
105  */
106 ostream &
107 operator << (ostream &out, DistanceUnit unit) {
108  return out << format_abbrev_unit(unit);
109 }
110 
111 /**
112  *
113  */
114 istream &
115 operator >> (istream &in, DistanceUnit &unit) {
116  string word;
117  in >> word;
118  unit = string_distance_unit(word);
119  if (unit == DU_invalid) {
120  pandatoolbase_cat->error()
121  << "Invalid distance unit: " << word << "\n";
122  }
123  return in;
124 }
125 
126 /**
127  * Converts from a string, as might be input by the user, to one of the known
128  * DistanceUnit types. Returns DU_invalid if the string is unknown.
129  */
131 string_distance_unit(const string &str) {
132  if (cmp_nocase(str, "mm") == 0 || cmp_nocase(str, "millimeters") == 0) {
133  return DU_millimeters;
134 
135  } else if (cmp_nocase(str, "cm") == 0 || cmp_nocase(str, "centimeters") == 0) {
136  return DU_centimeters;
137 
138  } else if (cmp_nocase(str, "m") == 0 || cmp_nocase(str, "meters") == 0) {
139  return DU_meters;
140 
141  } else if (cmp_nocase(str, "km") == 0 || cmp_nocase(str, "kilometers") == 0) {
142  return DU_kilometers;
143 
144  } else if (cmp_nocase(str, "yd") == 0 || cmp_nocase(str, "yards") == 0) {
145  return DU_yards;
146 
147  } else if (cmp_nocase(str, "ft") == 0 || cmp_nocase(str, "feet") == 0) {
148  return DU_feet;
149 
150  } else if (cmp_nocase(str, "in") == 0 || cmp_nocase(str, "inches") == 0) {
151  return DU_inches;
152 
153  } else if (cmp_nocase(str, "nmi") == 0 ||
154  cmp_nocase(str, "nm") == 0 ||
155  cmp_nocase_uh(str, "nautical_miles") == 0) {
156  return DU_nautical_miles;
157 
158  } else if (cmp_nocase(str, "mi") == 0 ||
159  cmp_nocase(str, "miles") == 0 ||
160  cmp_nocase_uh(str, "statute_miles") == 0) {
161  return DU_statute_miles;
162 
163  } else {
164  return DU_invalid;
165  }
166 }
167 
168 /**
169  * Returns the number of the indicated unit per each centimeter. This
170  * internal function is used to implement convert_units(), below.
171  */
172 static double unit_scale(DistanceUnit unit) {
173  switch (unit) {
174  case DU_millimeters:
175  return 0.1;
176 
177  case DU_centimeters:
178  return 1.0;
179 
180  case DU_meters:
181  return 100.0;
182 
183  case DU_kilometers:
184  return 100000.0;
185 
186  case DU_yards:
187  return 3.0 * 12.0 * 2.54;
188 
189  case DU_feet:
190  return 12.0 * 2.54;
191 
192  case DU_inches:
193  return 2.54;
194 
195  case DU_nautical_miles:
196  // This is the U.S. definition.
197  return 185200.0;
198 
199  case DU_statute_miles:
200  return 5280.0 * 12.0 * 2.54;
201 
202  case DU_invalid:
203  return 1.0;
204  }
205 
206  return 1.0;
207 }
208 
209 /**
210  * Returns the scaling factor that must be applied to convert from units of
211  * "from" to "to".
212  */
214  return unit_scale(from) / unit_scale(to);
215 }
string format_long_unit(DistanceUnit unit)
Returns the string representing the full name (plural) for the given unit.
string format_abbrev_unit(DistanceUnit unit)
Returns the string representing the common abbreviation for the given unit.
double convert_units(DistanceUnit from, DistanceUnit to)
Returns the scaling factor that must be applied to convert from units of "from" to "to".
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DistanceUnit
This enumerated type lists all the kinds of units we're likely to come across in model conversion pro...
Definition: distanceUnit.h:23
DistanceUnit string_distance_unit(const string &str)
Converts from a string, as might be input by the user, to one of the known DistanceUnit types.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.