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