40 #include "FGWaypoint.h" 41 #include "input_output/FGXMLElement.h" 42 #include "input_output/FGPropertyManager.h" 43 #include "math/FGLocation.h" 49 IDENT(IdSrc,
"$Id: FGWaypoint.cpp,v 1.8 2016/05/05 15:32:42 bcoconni Exp $");
50 IDENT(IdHdr,ID_WAYPOINT);
58 FGWaypoint::FGWaypoint(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
60 if (Type ==
"WAYPOINT_HEADING") WaypointType = eHeading;
61 else if (Type ==
"WAYPOINT_DISTANCE") WaypointType = eDistance;
63 target_latitude_unit = 1.0;
64 target_longitude_unit = 1.0;
65 source_latitude_unit = 1.0;
66 source_longitude_unit = 1.0;
68 if (element->FindElement(
"target_latitude") ) {
69 target_latitude = PropertyManager->CreatePropertyObject<
double>(element->FindElementValue(
"target_latitude"));
70 if (element->FindElement(
"target_latitude")->HasAttribute(
"unit")) {
71 if (element->FindElement(
"target_latitude")->GetAttributeValue(
"unit") ==
"DEG") {
72 target_latitude_unit = 0.017453293;
76 cerr << element->ReadFrom() << endl
77 <<
"Target latitude is required for waypoint component: " << Name
79 throw(
"Malformed waypoint definition");
82 if (element->FindElement(
"target_longitude") ) {
83 target_longitude = PropertyManager->CreatePropertyObject<
double>(element->FindElementValue(
"target_longitude"));
84 if (element->FindElement(
"target_longitude")->HasAttribute(
"unit")) {
85 if (element->FindElement(
"target_longitude")->GetAttributeValue(
"unit") ==
"DEG") {
86 target_longitude_unit = 0.017453293;
90 cerr << element->ReadFrom() << endl
91 <<
"Target longitude is required for waypoint component: " << Name
93 throw(
"Malformed waypoint definition");
96 if (element->FindElement(
"source_latitude") ) {
97 source_latitude = PropertyManager->CreatePropertyObject<
double>(element->FindElementValue(
"source_latitude"));
98 if (element->FindElement(
"source_latitude")->HasAttribute(
"unit")) {
99 if (element->FindElement(
"source_latitude")->GetAttributeValue(
"unit") ==
"DEG") {
100 source_latitude_unit = 0.017453293;
104 cerr << element->ReadFrom() << endl
105 <<
"Source latitude is required for waypoint component: " << Name
107 throw(
"Malformed waypoint definition");
110 if (element->FindElement(
"source_longitude") ) {
111 source_longitude = PropertyManager->CreatePropertyObject<
double>(element->FindElementValue(
"source_longitude"));
112 if (element->FindElement(
"source_longitude")->HasAttribute(
"unit")) {
113 if (element->FindElement(
"source_longitude")->GetAttributeValue(
"unit") ==
"DEG") {
114 source_longitude_unit = 0.017453293;
118 cerr << element->ReadFrom() << endl
119 <<
"Source longitude is required for waypoint component: " << Name
121 throw(
"Malformed waypoint definition");
124 if (element->FindElement(
"radius"))
125 radius = element->FindElementValueAsNumberConvertTo(
"radius",
"FT");
127 FGLocation source(source_longitude * source_latitude_unit,
128 source_latitude * source_longitude_unit, 1.0);
129 radius = source.GetSeaLevelRadius();
132 unit = element->GetAttributeValue(
"unit");
133 if (WaypointType == eHeading) {
135 if (unit ==
"DEG") eUnit = eDeg;
136 else if (unit ==
"RAD") eUnit = eRad;
138 cerr << element->ReadFrom() << endl
139 <<
"Unknown unit " << unit <<
" in HEADING waypoint component, " 141 throw(
"Malformed waypoint definition");
148 if (unit ==
"FT") eUnit = eFeet;
149 else if (unit ==
"M") eUnit = eMeters;
151 cerr << element->ReadFrom() << endl
152 <<
"Unknown unit " << unit <<
" in DISTANCE waypoint component, " 154 throw(
"Malformed waypoint definition");
161 FGFCSComponent::bind();
167 FGWaypoint::~FGWaypoint()
174 bool FGWaypoint::Run(
void )
176 double target_latitude_rad = target_latitude * target_latitude_unit;
177 double target_longitude_rad = target_longitude * target_longitude_unit;
178 FGLocation source(source_longitude * source_latitude_unit,
179 source_latitude * source_longitude_unit, radius);
181 if (WaypointType == eHeading) {
182 double heading_to_waypoint_rad = source.GetHeadingTo(target_longitude_rad,
183 target_latitude_rad);
185 if (eUnit == eDeg) Output = heading_to_waypoint_rad * radtodeg;
186 else Output = heading_to_waypoint_rad;
189 double wp_distance = source.GetDistanceTo(target_longitude_rad,
190 target_latitude_rad);
192 if (eUnit == eMeters) Output =
FeetToMeters(wp_distance);
193 else Output = wp_distance;
197 if (IsOutput) SetOutput();
221 void FGWaypoint::Debug(
int from)
223 if (debug_lvl <= 0)
return;
229 if (debug_lvl & 2 ) {
230 if (from == 0) cout <<
"Instantiated: FGWaypoint" << endl;
231 if (from == 1) cout <<
"Destroyed: FGWaypoint" << endl;
233 if (debug_lvl & 4 ) {
235 if (debug_lvl & 8 ) {
237 if (debug_lvl & 16) {
239 if (debug_lvl & 64) {
241 cout << IdSrc << endl;
242 cout << IdHdr << endl;
static double FeetToMeters(double measure)
Converts from feet to meters.