]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/groundradar.cxx
Merge branch 'jmt/units-fix' into maint
[flightgear.git] / src / Instrumentation / groundradar.cxx
1 //  groundradar.cxx - Background layer for the ATC radar.
2 //
3 //  Copyright (C) 2007 Csaba Halasz.
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include <osg/Node>
24 #include <osg/Geode>
25 #include <osg/Geometry>
26 #include <osg/Camera>
27 #include <osg/Texture2D>
28 #include <osgViewer/Viewer>
29
30 #include <osgText/Text>
31 #include <osgDB/Registry>
32 #include <osgDB/ReaderWriter>
33 #include <osgUtil/Tessellator>
34
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/renderer.hxx>
38 #include <Cockpit/panel.hxx>
39 #include <Airports/simple.hxx>
40 #include <Airports/runways.hxx>
41 #include <Airports/pavement.hxx>
42 #include <simgear/math/sg_geodesy.hxx>
43 #include <simgear/math/beziercurve.hxx>
44
45 #include "groundradar.hxx"
46
47 static const char* airport_source_node_name = "airport-id-source";
48 static const char* default_airport_node_name = "/sim/tower/airport-id";
49 static const char* texture_node_name = "texture-name";
50 static const char* default_texture_name = "Aircraft/Instruments/Textures/od_groundradar.rgb";
51 static const char* range_source_node_name = "range-source";
52 static const char* default_range_node_name = "/instrumentation/radar/range";
53
54 struct SingleFrameCallback : public osg::Camera::DrawCallback
55 {
56     virtual void operator () (const osg::Camera& camera) const
57     {
58         const_cast<osg::Camera&>(camera).setNodeMask(0);
59     }
60 };
61
62 GroundRadar::GroundRadar(SGPropertyNode *node)
63 {
64     _airport_node = fgGetNode(node->getStringValue(airport_source_node_name, default_airport_node_name), true);
65     _range_node = fgGetNode(node->getStringValue(range_source_node_name, default_range_node_name), true);
66     createTexture(node->getStringValue(texture_node_name, default_texture_name));
67     updateTexture();
68     _airport_node->addChangeListener(this);
69     _range_node->addChangeListener(this);
70 }
71
72 GroundRadar::~GroundRadar()
73 {
74     _airport_node->removeChangeListener(this);
75     _range_node->removeChangeListener(this);
76 }
77
78 void GroundRadar::valueChanged(SGPropertyNode*)
79 {
80     updateTexture();
81 }
82
83 inline static osg::Vec3 fromPolar(double fi, double r)
84 {
85     return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
86 }
87
88 void GroundRadar::createTexture(const char* texture_name)
89 {
90     setSize(TextureHalfSize + TextureHalfSize);
91     allocRT();
92
93     _geode = new osg::Geode();
94     osg::StateSet* stateset = _geode->getOrCreateStateSet();
95     stateset->setMode(GL_BLEND, osg::StateAttribute::OFF);
96
97     osg::Vec4Array* taxi_color = new osg::Vec4Array;
98     taxi_color->push_back(osg::Vec4(0.0f, 0.5f, 0.0f, 1.0f));
99     osg::Vec4Array* rwy_color = new osg::Vec4Array;
100     rwy_color->push_back(osg::Vec4(0.0f, 0.5f, 0.5f, 1.0f));
101
102     osg::Geometry *taxi_geom = new osg::Geometry();
103     taxi_geom->setColorArray(taxi_color);
104     taxi_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
105     taxi_geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0)); // Taxiways
106     _geode->addDrawable(taxi_geom);
107
108     osg::Geometry *pvt_geom = new osg::Geometry();
109     pvt_geom->setColorArray(taxi_color);
110     pvt_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
111     // no primitive set for the moment. It needs tessellation
112     _geode->addDrawable(pvt_geom);
113
114     osg::Geometry *rwy_geom = new osg::Geometry();
115     rwy_geom->setColorArray(rwy_color);
116     rwy_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
117     rwy_geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0)); // Runways
118     _geode->addDrawable(rwy_geom);
119
120     osg::Camera* camera = getCamera();
121     camera->setPostDrawCallback(new SingleFrameCallback());
122     camera->addChild(_geode.get());
123     camera->setNodeMask(0);
124     camera->setProjectionMatrixAsOrtho2D(0, getTexture()->getTextureWidth(), 0, getTexture()->getTextureHeight());
125
126     // Texture in the 2D panel system
127     FGTextureManager::addTexture(texture_name, getTexture());
128 }
129
130 void GroundRadar::addRunwayVertices(const FGRunwayBase* aRunway, double aTowerLat, double aTowerLon, double aScale, osg::Vec3Array* aVertices)
131 {
132   double az1, az2, dist_m;
133   geo_inverse_wgs_84(aTowerLat, aTowerLon, aRunway->latitude(), aRunway->longitude(), &az1, &az2, &dist_m);
134
135   osg::Vec3 center = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
136   osg::Vec3 leftcenter = fromPolar(aRunway->headingDeg(), aRunway->lengthM() * aScale / 2) + center;
137   osg::Vec3 lefttop = fromPolar(aRunway->headingDeg() - 90, aRunway->widthM() * aScale / 2) + leftcenter;
138   osg::Vec3 leftbottom = leftcenter * 2 - lefttop;
139   osg::Vec3 rightbottom = center * 2 - lefttop;
140   osg::Vec3 righttop = center * 2 - leftbottom;
141
142   aVertices->push_back(lefttop);
143   aVertices->push_back(leftbottom);
144   aVertices->push_back(rightbottom);
145   aVertices->push_back(righttop);
146 }
147
148 osg::Geometry *GroundRadar::addPavementGeometry(const FGPavement* aPavement, double aTowerLat, double aTowerLon, double aScale)
149 {
150
151   osg::ref_ptr<osgUtil::Tessellator> tess = new osgUtil::Tessellator;
152   osg::ref_ptr<osg::Geometry> polygon = new osg::Geometry;
153   osg::ref_ptr<osg::Vec3Array> pts = new osg::Vec3Array;
154
155   double az1, az2, dist_m;
156   const FGPavement::NodeList &nodeLst = aPavement->getNodeList();
157   FGPavement::NodeList::const_iterator it = nodeLst.begin(),
158                                         loopBegin = it;
159   while ( it != nodeLst.end() )
160   {
161     bool close = (*it)->mClose;
162     geo_inverse_wgs_84(aTowerLat, aTowerLon, (*it)->mPos.getLatitudeDeg(), (*it)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
163     osg::Vec3 p1 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
164     const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>( it->ptr() );
165     if ( bn != 0 )
166     {
167       geo_inverse_wgs_84(aTowerLat, aTowerLon, bn->mControl.getLatitudeDeg(), bn->mControl.getLongitudeDeg(), &az1, &az2, &dist_m);
168       osg::Vec3 p2 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0),
169                 p3;
170       ++it;
171       if ( it == nodeLst.end() || close )
172       {
173         geo_inverse_wgs_84(aTowerLat, aTowerLon, (*loopBegin)->mPos.getLatitudeDeg(), (*loopBegin)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
174       }
175       else
176       {
177         geo_inverse_wgs_84(aTowerLat, aTowerLon, (*it)->mPos.getLatitudeDeg(), (*it)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
178       }
179       p3 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
180       simgear::BezierCurve<osg::Vec3> bCurv( p1, p2, p3 );
181       simgear::BezierCurve<osg::Vec3>::PointList &ptList = bCurv.pointList();
182       for ( simgear::BezierCurve<osg::Vec3>::PointList::iterator ii = ptList.begin(); ii != ptList.end(); ++ii )
183       {
184         pts->push_back( *ii );
185       }
186       pts->pop_back(); // Last point belongs to next segment
187     }
188     else
189     {
190       pts->push_back( p1 );
191       ++it;
192     }
193
194     if ( close ) // One loop for the moment
195       break;
196   }
197   geo_inverse_wgs_84(aTowerLat, aTowerLon, (*loopBegin)->mPos.getLatitudeDeg(), (*loopBegin)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
198   osg::Vec3 p1 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
199   pts->push_back( p1 );
200   polygon->setVertexArray( pts.get() );
201
202   polygon->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::POLYGON, 0, pts->size() ) );
203
204   tess->setTessellationType( osgUtil::Tessellator::TESS_TYPE_GEOMETRY );
205   tess->setBoundaryOnly( false );
206   tess->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD );
207   tess->retessellatePolygons( *polygon );
208   return polygon.release();
209 }
210
211 void GroundRadar::updateTexture()
212 {
213     osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
214     osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
215     osg::ref_ptr<osg::Vec3Array> pvt_vertices = new osg::Vec3Array;
216
217     const string airport_name = _airport_node->getStringValue();
218
219     const FGAirport* airport = fgFindAirportID(airport_name);
220     if (airport == 0)
221         return;
222
223     const SGGeod& tower_location = airport->getTowerLocation();
224     const double tower_lat = tower_location.getLatitudeDeg();
225     const double tower_lon = tower_location.getLongitudeDeg();
226     double scale = SG_METER_TO_NM * 200 / _range_node->getDoubleValue();
227   
228     const FGAirport* apt = fgFindAirportID(airport_name);
229     assert(apt);
230
231     for (unsigned int i=0; i<apt->numTaxiways(); ++i)
232     {
233       FGTaxiway* txwy(apt->getTaxiwayByIndex(i));
234       addRunwayVertices(txwy, tower_lat, tower_lon, scale, taxi_vertices.get());
235     }
236     osg::Geometry *taxi_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(0));
237     taxi_geom->setVertexArray(taxi_vertices.get());
238     osg::DrawArrays* taxi = dynamic_cast<osg::DrawArrays*>(taxi_geom->getPrimitiveSet(0));
239     taxi->setCount(taxi_vertices->size());
240
241     osg::Geometry *pvt_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(1));
242     osg::Geometry::PrimitiveSetList &pvt_prim_list = pvt_geom->getPrimitiveSetList();
243     pvt_prim_list.clear();
244     for (unsigned int i=0; i<apt->numPavements(); ++i)
245     {
246       FGPavement* pvt(apt->getPavementByIndex(i));
247       osg::ref_ptr<osg::Geometry> geom = addPavementGeometry(pvt, tower_lat, tower_lon, scale);
248       osg::Geometry::PrimitiveSetList &prim_list = geom->getPrimitiveSetList();
249       osg::Vec3Array *vertices = dynamic_cast<osg::Vec3Array *>(geom->getVertexArray());
250       size_t before = pvt_vertices->size(),
251             count = vertices->size();
252       for (size_t i = 0; i < count; ++i )
253       {
254         pvt_vertices->push_back( (*vertices)[i] );
255       }
256       for (osg::Geometry::PrimitiveSetList::iterator ii = prim_list.begin(); ii != prim_list.end(); ++ii )
257       {
258         osg::DrawArrays *da;
259         osg::DrawElementsUByte *de1;
260         osg::DrawElementsUShort *de2;
261         osg::DrawElementsUInt *de3;
262         if ((da = dynamic_cast<osg::DrawArrays *>(ii->get())) != 0)
263         {
264           osg::DrawArrays *ps = new osg::DrawArrays(*da);
265           ps->setFirst(da->getFirst() + before);
266           pvt_prim_list.push_back(ps);
267         }
268         else if ((de1 = dynamic_cast<osg::DrawElementsUByte *>(ii->get())) != 0)
269         {
270           if (before + count <= 255)
271           {
272             osg::DrawElementsUByte *ps = new osg::DrawElementsUByte(*de1);
273             for (size_t j = 0; j < ps->size(); ++j)
274             {
275               (*ps)[j] += before;
276             }
277             pvt_prim_list.push_back(ps);
278           }
279           else if (before + count <= 65535)
280           {
281             osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(de1->getMode(), de1->begin(), de1->end());
282             for (size_t j = 0; j < ps->size(); ++j)
283             {
284               (*ps)[j] += before;
285             }
286             pvt_prim_list.push_back(ps);
287           }
288           else
289           {
290             osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de1->getMode(), de1->begin(), de1->end());
291             for (size_t j = 0; j < ps->size(); ++j)
292             {
293               (*ps)[j] += before;
294             }
295             pvt_prim_list.push_back(ps);
296           }
297         }
298         else if ((de2 = dynamic_cast<osg::DrawElementsUShort *>(ii->get())) != 0)
299         {
300           if (before + count <= 65535)
301           {
302             osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(*de2);
303             for (size_t j = 0; j < ps->size(); ++j)
304             {
305               (*ps)[j] += before;
306             }
307             pvt_prim_list.push_back(ps);
308           }
309           else
310           {
311             osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de2->getMode(), de2->begin(), de2->end());
312             for (size_t j = 0; j < ps->size(); ++j)
313             {
314               (*ps)[j] += before;
315             }
316             pvt_prim_list.push_back(ps);
317           }
318         }
319         else if ((de3 = dynamic_cast<osg::DrawElementsUInt *>(ii->get())) != 0)
320         {
321           osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(*de3);
322           for (size_t j = 0; j < ps->size(); ++j)
323           {
324             (*ps)[j] += before;
325           }
326           pvt_prim_list.push_back(ps);
327         }
328       }
329     }
330     pvt_geom->setVertexArray(pvt_vertices.get());
331
332     for (unsigned int i=0; i<apt->numRunways(); ++i)
333     {
334       FGRunway* runway(apt->getRunwayByIndex(i));
335       if (runway->isReciprocal()) continue;
336       
337       addRunwayVertices(runway, tower_lat, tower_lon, scale, rwy_vertices.get());
338     }
339     osg::Geometry *rwy_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(2));
340     rwy_geom->setVertexArray(rwy_vertices.get());
341     osg::DrawArrays* rwy = dynamic_cast<osg::DrawArrays*>(rwy_geom->getPrimitiveSet(0));
342     rwy->setCount(rwy_vertices->size());
343
344     getCamera()->setNodeMask(0xffffffff);
345 }
346
347 // end of GroundRadar.cxx