CYCLUS
Loading...
Searching...
No Matches
position.cc
Go to the documentation of this file.
1#include "position.h"
2
3#include <math.h>
4#include <stdio.h>
5#include <iomanip>
6#include <sstream>
7
8namespace cyclus {
9namespace toolkit {
10
11Position::Position() : latitude_(0), longitude_(0) {}
12
13Position::Position(double decimal_lat, double decimal_lon) {
14 latitude(decimal_lat);
15 longitude(decimal_lon);
16}
17
19
20double Position::latitude() const {
21 return SetPrecision(latitude_ / CYCLUS_DECIMAL_SECOND_MULTIPLIER, 6);
22}
23
24double Position::longitude() const {
25 return SetPrecision(longitude_ / CYCLUS_DECIMAL_SECOND_MULTIPLIER, 6);
26}
27
28void Position::latitude(double lat) {
29 if (!ValidLatitude(lat)) {
30 latitude_ = kInvalidLatitude;
31 std::stringstream msg;
32 msg << "The provided latitude (" << lat
33 << ") is outside the acceptable range "
34 << "[-90, 90]. Latitude has been set to " << latitude_;
36 } else {
37 latitude_ = SetPrecision(lat * CYCLUS_DECIMAL_SECOND_MULTIPLIER, 1);
38 }
39}
40
41void Position::longitude(double lon) {
42 if (!ValidLongitude(lon)) {
43 longitude_ = kInvalidLongitude;
44 std::stringstream msg;
45 msg << "The provided longitude (" << lon
46 << ") is outside the acceptable range "
47 << "[-180, 180]. Longitude has been set to " << longitude_;
49 } else {
50 longitude_ = SetPrecision(lon * CYCLUS_DECIMAL_SECOND_MULTIPLIER, 1);
51 }
52}
53
54void Position::set_position(double lat, double lon) {
55 latitude(lat);
56 longitude(lon);
57}
58
60 agent->context()
61 ->NewDatum("AgentPosition")
62 ->AddVal("Spec", agent->spec())
63 ->AddVal("Prototype", agent->prototype())
64 ->AddVal("AgentId", agent->id())
65 ->AddVal("Latitude", latitude_ / CYCLUS_DECIMAL_SECOND_MULTIPLIER)
66 ->AddVal("Longitude", longitude_ / CYCLUS_DECIMAL_SECOND_MULTIPLIER)
67 ->Record();
68}
69bool Position::ValidLatitude(double lat) {
70 if (lat > 90 || lat < -90) {
71 return false;
72 }
73 return true;
74}
75
76bool Position::ValidLongitude(double lon) {
77 if (lon > 180 || lon < -180) {
78 return false;
79 }
80 return true;
81}
82
83double Position::Distance(Position target) const {
84 double earth_radius = 6372.8; // in kilometers (KM)
85 double curr_longitude = this->longitude() * M_PI / 180;
86 double curr_latitude = this->latitude() * M_PI / 180;
87 double tarlongitude = target.longitude() * M_PI / 180;
88 double tarlatitude = target.latitude() * M_PI / 180;
89 double dlong = tarlongitude - curr_longitude;
90 double dlat = tarlatitude - curr_latitude;
91
92 double half_chord_length_sq = pow(sin(dlat / 2), 2) + pow(sin(dlong / 2), 2) *
93 cos(curr_latitude) *
94 cos(tarlatitude);
95
96 double angular_distance =
97 2 * atan2(sqrt(half_chord_length_sq), sqrt(1 - half_chord_length_sq));
98 return earth_radius * angular_distance;
99}
100
102 std::stringstream lat_string;
103 std::stringstream lon_string;
104 double lat = this->latitude();
105 double lon = this->longitude();
106 switch (format) {
108 lat_string << ToStringHelperLat(format, lat);
109 lon_string << ToStringHelperLon(format, lon);
110 break;
112 lat_string << ToStringHelperLat(format, lat);
113 lon_string << ToStringHelperLon(format, lon);
114 break;
116 lat_string << ToStringHelperLat(format, lat);
117 lon_string << ToStringHelperLon(format, lon);
118 break;
119 }
120 return lat_string.str() + lon_string.str() + "/";
121}
122
123double Position::SetPrecision(double value, double precision) const {
124 if (precision == 0) {
125 return floor(value);
126 }
127 return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision));
128}
129
130std::string Position::ToStringHelperLat(int mode, double lat) const {
131 std::stringstream lat_string;
132 double lat_int;
133 lat_string << ToStringHelper(lat);
134
135 lat = modf(lat, &lat_int);
136 if (((int)lat_int) / 10 == 0) {
137 lat_string << "0";
138 }
139 switch (mode) {
141 lat_string << std::setprecision(7) << fabs(lat_int + lat);
142 break;
144 lat_string << abs((int)lat_int) << ToStringHelperDM(lat);
145 break;
147 lat_string << abs((int)lat_int) << ToStringHelperDMS(lat);
148 break;
149 }
150 return lat_string.str();
151}
152
153std::string Position::ToStringHelperLon(int mode, double lon) const {
154 std::stringstream lon_string;
155 double lon_int;
156 lon_string << ToStringHelper(lon);
157 lon = modf(lon, &lon_int);
158 if (((int)lon_int) / 100 == 0) {
159 if (((int)lon_int) / 10 == 0) {
160 lon_string << "0";
161 }
162 lon_string << "0";
163 }
164 switch (mode) {
166 lon_string << std::setprecision(7) << fabs(lon_int + lon);
167 break;
169 lon_string << abs((int)lon_int) << ToStringHelperDM(lon);
170 break;
172 lon_string << abs((int)lon_int) << ToStringHelperDMS(lon);
173 break;
174 }
175 return lon_string.str();
176}
177
178std::string Position::ToStringHelper(double value) const {
179 std::stringstream temp;
180 if (value > 0) {
181 temp << "+";
182 } else {
183 temp << "-";
184 }
185 return temp.str();
186}
187
188std::string Position::ToStringHelperDM(double value) const {
189 std::stringstream temp;
190 value = fabs(value) * 60;
191 if (((int)fabs(value)) / 10 == 0) {
192 temp << "0";
193 }
194 temp << std::setprecision(5) << value;
195
196 return temp.str();
197}
198
199std::string Position::ToStringHelperDMS(double value) const {
200 std::stringstream temp;
201 double value_int;
202 value = fabs(value) * 60;
203 value = modf(value, &value_int);
204 if (((int)value_int) / 10 == 0) {
205 temp << "0";
206 }
207 temp << fabs(value_int);
208 value = value * 60;
209 if ((int)fabs(value) / 10 == 0) {
210 temp << "0";
211 }
212 temp << std::setprecision(1) << std::fixed << value;
213
214 return temp.str();
215}
216
217} // namespace toolkit
218} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:50
const std::string prototype() const
Returns the agent's prototype.
Definition agent.h:342
std::string spec()
The string representation of the agent spec that uniquely identifies the concrete agent class/module.
Definition agent.h:353
Context * context() const
Returns this agent's simulation context.
Definition agent.h:364
virtual const int id() const
The agent instance's unique ID within a simulation.
Definition agent.h:349
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition context.cc:351
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Definition datum.cc:21
void Record()
Record this datum to its Recorder.
Definition datum.cc:34
double Distance(Position target) const
Returns the distance (in km) between this Position object and the target Position object.
Definition position.cc:83
double longitude() const
Returns the current longitude.
Definition position.cc:24
~Position()
The default destructor for Position.
Definition position.cc:18
Position()
The default constructor for Position.
Definition position.cc:11
void RecordPosition(Agent *agent)
Definition position.cc:59
void set_position(double lat, double lon)
Sets a new latitude and longitude.
Definition position.cc:54
std::string ToString(Position::StringFormat format=StringFormat::DEGREES) const
Converts Position location into a string expression that follows ISO 6709 Annex H.
Definition position.cc:101
double latitude() const
Returns the current latitude.
Definition position.cc:20
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
void Warn(const std::string &msg)
Issue a warning with the approriate message, accoring to the current warning settings.
Definition error.h:108
#define CYCLUS_DECIMAL_SECOND_MULTIPLIER
Definition position.h:3