]> git.mxchange.org Git - simgear.git/blob - simgear/timing/geocoord.cxx
939ca42abe149b0a2732677411a186abb9abf4a6
[simgear.git] / simgear / timing / geocoord.cxx
1 /* -*- Mode: C++ -*- *****************************************************
2  * geocoord.h
3  * Written by Durk Talsma. Started March 1998.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  **************************************************************************/
20
21 /*************************************************************************
22  *
23  * This file defines a small and simple class to store geocentric 
24  * coordinates. Basically, class GeoCoord is intended as a base class for
25  * any kind of of object, that can be categorized according to its 
26  * location on earth, be it navaids, or aircraft. This class for originally
27  * written for FlightGear, in order to store Timezone control points. 
28  *
29  ************************************************************************/
30 #include "geocoord.h"
31 #include <plib/sg.h>
32
33 GeoCoord::GeoCoord(const GeoCoord& other)
34 {
35   lat = other.lat;
36   lon = other.lon;
37 }
38
39 // double GeoCoord::getAngle(const GeoCoord& other) const
40 // {
41 //   Vector first(      getX(),       getY(),       getZ());
42 //   Vector secnd(other.getX(), other.getY(), other.getZ());
43 //     double
44 //       dot = VecDot(first, secnd),
45 //       len1 = first.VecLen(),
46 //       len2 = secnd.VecLen(),
47 //       len = len1 * len2,
48 //       angle = 0;
49 //     //printf ("Dot: %f, len1: %f len2: %f\n", dot, len1, len2);
50 //     /*Vector pPos = prevPos - Reference->prevPos;
51 //       Vector pVel = prevVel - Reference->prevVel;*/
52
53
54 //     if ( ( (dot / len) < 1) && (dot / len > -1) && len )
55 //      angle = acos(dot / len);
56 //     return angle;
57 // }
58
59 // GeoCoord* GeoCoordContainer::getNearest(const GeoCoord& ref) const
60 // {
61 //   float angle, maxAngle = 180;
62
63 //   GeoCoordVectorConstIterator i, nearest;
64 //   for (i = data.begin(); i != data.end(); i++)
65 //     {
66 //       angle = RAD_TO_DEG * (*i)->getAngle(ref);
67 //       if (angle < maxAngle)
68 //      {
69 //        maxAngle = angle;
70 //        nearest = i;
71 //      }
72 //     }
73 //   return *nearest;
74 // }
75
76
77 GeoCoord* GeoCoordContainer::getNearest(const GeoCoord& ref) const
78 {
79   sgVec3 first, secnd;
80   float dist, maxDist=SG_MAX;
81   sgSetVec3( first, ref.getX(), ref.getY(), ref.getZ());
82   GeoCoordVectorConstIterator i, nearest;
83   for (i = data.begin(); i != data.end(); i++)
84     {
85       sgSetVec3(secnd, (*i)->getX(), (*i)->getY(), (*i)->getZ());
86       dist = sgDistanceSquaredVec3(first, secnd);
87       if (dist < maxDist)
88         {
89           maxDist = dist;
90           nearest = i;
91         }
92     }
93   return *nearest;
94 }
95
96
97 GeoCoordContainer::~GeoCoordContainer()
98 {
99     GeoCoordVectorIterator i = data.begin();
100   while (i != data.end())
101     delete *i++;
102 }