]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/ATCProjection.cxx
try to fix yet another crash bug (don't worry, there's one for anybody!)
[flightgear.git] / src / ATC / ATCProjection.cxx
index 4e3f51f89fe64ec5b26bed9dc46a825d0bdc6838..7028004c85a3c1816f8354b51fdb61f8c8fc04b5 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
 
 #include "ATCProjection.hxx"
 #include <math.h>
 #include <simgear/constants.h>
 
-#define DCL_PI  3.1415926535f
-//#define SG_PI  ((SGfloat) M_PI)
-#define DCL_DEGREES_TO_RADIANS  (DCL_PI/180.0)
-#define DCL_RADIANS_TO_DEGREES  (180.0/DCL_PI)
-
 FGATCProjection::FGATCProjection() {
-    origin.setlat(0.0);
-    origin.setlon(0.0);
-    origin.setelev(0.0);
-    correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
+    _origin.setlat(0.0);
+    _origin.setlon(0.0);
+    _origin.setelev(0.0);
+    _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
+}
+
+FGATCProjection::FGATCProjection(const Point3D& centre) {
+    _origin = centre;
+    _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
 }
 
 FGATCProjection::~FGATCProjection() {
 }
 
-void FGATCProjection::Init(Point3D centre) {
-    origin = centre;
-    correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
+void FGATCProjection::Init(const Point3D& centre) {
+    _origin = centre;
+    _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
 }
 
-Point3D FGATCProjection::ConvertToLocal(Point3D pt) {
-    double delta_lat = pt.lat() - origin.lat();
-    double delta_lon = pt.lon() - origin.lon();
+Point3D FGATCProjection::ConvertToLocal(const Point3D& pt) {
+    double delta_lat = pt.lat() - _origin.lat();
+    double delta_lon = pt.lon() - _origin.lon();
 
-    double y = sin(delta_lat * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
-    double x = sin(delta_lon * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * correction_factor;
+    double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
+    double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
 
     return(Point3D(x,y,0.0));
 }
 
-Point3D FGATCProjection::ConvertFromLocal(Point3D pt) {
-       double delta_lat = asin(pt.y() / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES;
-       double delta_lon = (asin(pt.x() / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES) / correction_factor;
-       
-    return(Point3D(origin.lon()+delta_lon, origin.lat()+delta_lat, 0.0));
+Point3D FGATCProjection::ConvertFromLocal(const Point3D& pt) {
+    double delta_lat = asin(pt.y() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
+    double delta_lon = (asin(pt.x() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
+    
+    return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, 0.0));
 }
 
 /**********************************************************************************/
 
 FGATCAlignedProjection::FGATCAlignedProjection() {
-    origin.setlat(0.0);
-    origin.setlon(0.0);
-    origin.setelev(0.0);
-    correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
+    _origin.setlat(0.0);
+    _origin.setlon(0.0);
+    _origin.setelev(0.0);
+    _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
+}
+
+FGATCAlignedProjection::FGATCAlignedProjection(const Point3D& centre, double heading) {
+    _origin = centre;
+    _theta = heading * SG_DEGREES_TO_RADIANS;
+    _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
 }
 
 FGATCAlignedProjection::~FGATCAlignedProjection() {
 }
 
-void FGATCAlignedProjection::Init(Point3D centre, double heading) {
-    origin = centre;
-    theta = heading * DCL_DEGREES_TO_RADIANS;
-    correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
+void FGATCAlignedProjection::Init(const Point3D& centre, double heading) {
+    _origin = centre;
+    _theta = heading * SG_DEGREES_TO_RADIANS;
+    _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
 }
 
-Point3D FGATCAlignedProjection::ConvertToLocal(Point3D pt) {
+Point3D FGATCAlignedProjection::ConvertToLocal(const Point3D& pt) {
     // convert from lat/lon to orthogonal
-    double delta_lat = pt.lat() - origin.lat();
-    double delta_lon = pt.lon() - origin.lon();
-    double y = sin(delta_lat * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
-    double x = sin(delta_lon * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * correction_factor;
+    double delta_lat = pt.lat() - _origin.lat();
+    double delta_lon = pt.lon() - _origin.lon();
+    double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
+    double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
 
     // Align
-    double xbar = x;
-    x = x*cos(theta) - y*sin(theta);
-    y = (xbar*sin(theta)) + (y*cos(theta));
+    if(_theta != 0.0) {
+        double xbar = x;
+        x = x*cos(_theta) - y*sin(_theta);
+        y = (xbar*sin(_theta)) + (y*cos(_theta));
+    }
 
-    return(Point3D(x,y,0.0));
+    return(Point3D(x,y,pt.elev()));
 }
 
-Point3D FGATCAlignedProjection::ConvertFromLocal(Point3D pt) {
+Point3D FGATCAlignedProjection::ConvertFromLocal(const Point3D& pt) {
     // de-align
-    double thi = theta * -1.0;
+    double thi = _theta * -1.0;
     double x = pt.x()*cos(thi) - pt.y()*sin(thi);
     double y = (pt.x()*sin(thi)) + (pt.y()*cos(thi));
 
     // convert from orthogonal to lat/lon
-    double delta_lat = asin(y / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES;
-    double delta_lon = (asin(x / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES) / correction_factor;
+    double delta_lat = asin(y / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
+    double delta_lon = (asin(x / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
 
-    return(Point3D(origin.lon()+delta_lon, origin.lat()+delta_lat, 0.0));
+    return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, pt.elev()));
 }