]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AICarrier.cxx
Remove unused variables
[flightgear.git] / src / AIModel / AICarrier.cxx
index 3dd427ce01852e53b0fe293bf0487b2675a3fff8..1a9a1d2b72dc51fddfadfd4ed221ca26b474a3c7 100644 (file)
 #  include <config.h>
 #endif
 
+#include <algorithm>
 #include <string>
 #include <vector>
 
+#include <osg/NodeVisitor>
+
 #include <simgear/math/SGMath.hxx>
-#include <simgear/math/point3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
+#include <simgear/scene/util/SGNodeMasks.hxx>
+
 #include <math.h>
 #include <Main/util.hxx>
 #include <Main/viewer.hxx>
 
 #include "AICarrier.hxx"
 
-/** Value of earth radius (meters) */
-#define RADIUS_M   SG_EQUATORIAL_RADIUS_M
+class FGCarrierVisitor : public osg::NodeVisitor {
+public:
+  FGCarrierVisitor(FGAICarrier* carrier,
+                   const std::list<std::string>& wireObjects,
+                   const std::list<std::string>& catapultObjects,
+                   const std::list<std::string>& solidObjects) :
+    osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
+                     osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
+    mWireObjects(wireObjects),
+    mCatapultObjects(catapultObjects),
+    mSolidObjects(solidObjects),
+    mFoundHot(false),
+    mCarrier(carrier)
+  { }
+  virtual void apply(osg::Node& node)
+  {
+    osg::ref_ptr<osg::Referenced> oldUserData = mUserData;
+    bool oldFoundHot = mFoundHot;
+    mFoundHot = false;
+
+    if (std::find(mWireObjects.begin(), mWireObjects.end(), node.getName())
+        != mWireObjects.end()) {
+      mFoundHot = true;
+      mUserData = FGAICarrierHardware::newWire(mCarrier);
+    }
+    if (std::find(mCatapultObjects.begin(), mCatapultObjects.end(), node.getName())
+        != mCatapultObjects.end()) {
+      mFoundHot = true;
+      mUserData = FGAICarrierHardware::newCatapult(mCarrier);
+    }
+    if (std::find(mSolidObjects.begin(), mSolidObjects.end(), node.getName())
+        != mSolidObjects.end()) {
+      mFoundHot = true;
+      mUserData = FGAICarrierHardware::newSolid(mCarrier);
+      //SG_LOG(SG_GENERAL, SG_ALERT, "AICarrierVisitor::apply() solidObject" );
+    }
+    node.setUserData(mUserData.get());
+
+    traverse(node);
 
+    mFoundHot = oldFoundHot || mFoundHot;
 
+    if (mFoundHot) {
+      node.setNodeMask(node.getNodeMask() | SG_NODEMASK_TERRAIN_BIT);
+    } else
+      node.setNodeMask(node.getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
+
+    mUserData = oldUserData;
+  }
+  
+private:
+  std::list<std::string> mWireObjects;
+  std::list<std::string> mCatapultObjects;
+  std::list<std::string> mSolidObjects;
+  bool mFoundHot;
+  FGAICarrier* mCarrier;
+  osg::ref_ptr<osg::Referenced> mUserData;
+};
 
 FGAICarrier::FGAICarrier() : FGAIShip(otCarrier) {
 }
@@ -140,10 +198,10 @@ void FGAICarrier::setTACANChannelID(const string& id) {
     TACAN_channel_id = id;
 }
 
-void FGAICarrier::getVelocityWrtEarth(sgdVec3& v, sgdVec3& omega, sgdVec3& pivot) {
-    sgdCopyVec3(v, vel_wrt_earth.sg() );
-    sgdCopyVec3(omega, rot_wrt_earth.sg() );
-    sgdCopyVec3(pivot, rot_pivot_wrt_earth.sg() );
+void FGAICarrier::getVelocityWrtEarth(SGVec3d& v, SGVec3d& omega, SGVec3d& pivot) {
+    v = vel_wrt_earth;
+    omega = rot_wrt_earth;
+    pivot = rot_pivot_wrt_earth;
 }
 
 void FGAICarrier::update(double dt) {
@@ -152,14 +210,14 @@ void FGAICarrier::update(double dt) {
     // but by just apply discrete changes at its velocity variables.
     // Update the velocity information stored in those nodes.
     // Transform that one to the horizontal local coordinate system.
-    SGQuatd ec2hl = SGQuatd::fromLonLatDeg(pos.lon(), pos.lat());
+    SGQuatd ec2hl = SGQuatd::fromLonLat(pos);
     // The orientation of the carrier wrt the horizontal local frame
     SGQuatd hl2body = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
     // and postrotate the orientation of the AIModel wrt the horizontal
     // local frame
     SGQuatd ec2body = ec2hl*hl2body;
     // The cartesian position of the carrier in the wgs84 world
-    SGVec3d cartPos = SGGeod::fromDegFt(pos.lon(), pos.lat(), pos.elev());
+    SGVec3d cartPos = SGVec3d::fromGeod(pos);
     // Store for later use by the groundcache
     rot_pivot_wrt_earth = cartPos;
 
@@ -189,7 +247,7 @@ void FGAICarrier::update(double dt) {
       // Now here is the finite difference ...
 
       // Transform that one to the horizontal local coordinate system.
-      SGQuatd ec2hlNew = SGQuatd::fromLonLatDeg(pos.lon(), pos.lat());
+      SGQuatd ec2hlNew = SGQuatd::fromLonLat(pos);
       // compute the new orientation
       SGQuatd hl2bodyNew = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
       // The rotation difference
@@ -214,7 +272,7 @@ void FGAICarrier::update(double dt) {
     // For the flols reuse some computations done above ...
 
     // The position of the eyepoint - at least near that ...
-    SGVec3d eyePos(globals->get_current_view()->get_absolute_view_pos());
+    SGVec3d eyePos(globals->get_current_view()->get_view_pos());
     // Add the position offset of the AIModel to gain the earth
     // centered position
     SGVec3d eyeWrtCarrier = eyePos - cartPos;
@@ -252,26 +310,10 @@ void FGAICarrier::update(double dt) {
       source = 0;
 } //end update
 
-bool FGAICarrier::init() {
-    if (!FGAIShip::init())
+bool FGAICarrier::init(bool search_in_AI_path) {
+    if (!FGAIShip::init(search_in_AI_path))
         return false;
 
-    // process the 3d model here
-    // mark some objects solid, mark the wires ...
-
-    // The model should be used for altitude computations.
-    // To avoid that every detail in a carrier 3D model will end into
-    // the aircraft local cache, only set the HOT traversal bit on
-    // selected objects.
-    ssgEntity *sel = aip.getSceneGraph();
-    // Clear the HOT traversal flag
-    mark_nohot(sel);
-    // Selectively set that flag again for wires/cats/solid objects.
-    // Attach a pointer to this carrier class to those objects.
-    mark_wires(sel, wire_objects);
-    mark_cat(sel, catapult_objects);
-    mark_solid(sel, solid_objects);
-
     _longitude_node = fgGetNode("/position/longitude-deg", true);
     _latitude_node = fgGetNode("/position/latitude-deg", true);
     _altitude_node = fgGetNode("/position/altitude-ft", true);
@@ -287,7 +329,7 @@ bool FGAICarrier::init() {
     turn_to_launch_hdg = false;
     returning = false;
 
-    initialpos = pos;
+    mOpBoxPos = pos;
     base_course = hdg;
     base_speed = speed;
 
@@ -302,6 +344,27 @@ bool FGAICarrier::init() {
     return true;
 }
 
+void FGAICarrier::initModel(osg::Node *node)
+{
+    // SG_LOG(SG_GENERAL, SG_BULK, "AICarrier::initModel()" );
+    FGAIShip::initModel(node);
+    // process the 3d model here
+    // mark some objects solid, mark the wires ...
+
+    // The model should be used for altitude computations.
+    // To avoid that every detail in a carrier 3D model will end into
+    // the aircraft local cache, only set the HOT traversal bit on
+    // selected objects.
+
+    // Clear the HOT traversal flag
+    // Selectively set that flag again for wires/cats/solid objects.
+    // Attach a pointer to this carrier class to those objects.
+    // SG_LOG(SG_GENERAL, SG_BULK, "AICarrier::initModel() visit" );
+    FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects, solid_objects);
+    model->accept(carrierVisitor);
+//    model->setNodeMask(node->getNodeMask() & SG_NODEMASK_TERRAIN_BIT | model->getNodeMask());
+}
+
 void FGAICarrier::bind() {
     FGAIShip::bind();
 
@@ -322,9 +385,9 @@ void FGAICarrier::bind() {
     props->tie("controls/base-speed-kts",
                 SGRawValuePointer<double>(&base_speed));
     props->tie("controls/start-pos-lat-deg",
-                SGRawValuePointer<double>(&initialpos[1]));
+               SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLatitudeDeg));
     props->tie("controls/start-pos-long-deg",
-                SGRawValuePointer<double>(&initialpos[0]));
+               SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLongitudeDeg));
     props->tie("velocities/speed-kts",
                 SGRawValuePointer<double>(&speed));
     props->tie("environment/surface-wind-speed-true-kts",
@@ -402,7 +465,7 @@ bool FGAICarrier::getParkPosition(const string& id, SGGeod& geodPos,
         if ((*it).name == id || id.empty()) {
             ParkPosition ppos = *it;
             SGVec3d cartPos = getCartPosAt(ppos.offset);
-            geodPos = cartPos;
+            geodPos = SGGeod::fromCart(cartPos);
             hdng = hdg + ppos.heading_deg;
             double shdng = sin(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
             double chdng = cos(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
@@ -416,187 +479,6 @@ bool FGAICarrier::getParkPosition(const string& id, SGGeod& geodPos,
     return false;
 }
 
-
-void FGAICarrier::mark_nohot(ssgEntity* e) {
-    if (e->isAKindOf(ssgTypeBranch())) {
-        ssgBranch* br = (ssgBranch*)e;
-        ssgEntity* kid;
-        for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
-            mark_nohot(kid);
-
-        br->clrTraversalMaskBits(SSGTRAV_HOT);
-
-    } else if (e->isAKindOf(ssgTypeLeaf())) {
-
-        e->clrTraversalMaskBits(SSGTRAV_HOT);
-    }
-}
-
-
-bool FGAICarrier::mark_wires(ssgEntity* e, const list<string>& wire_objects, bool mark) {
-    bool found = false;
-    if (e->isAKindOf(ssgTypeBranch())) {
-        ssgBranch* br = (ssgBranch*)e;
-        ssgEntity* kid;
-
-        list<string>::const_iterator it;
-        for (it = wire_objects.begin(); it != wire_objects.end(); ++it)
-            mark = mark || (e->getName() && (*it) == e->getName());
-
-        for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
-            found = mark_wires(kid, wire_objects, mark) || found;
-
-        if (found)
-            br->setTraversalMaskBits(SSGTRAV_HOT);
-
-    } else if (e->isAKindOf(ssgTypeLeaf())) {
-        list<string>::const_iterator it;
-        for (it = wire_objects.begin(); it != wire_objects.end(); ++it) {
-            if (mark || (e->getName() && (*it) == e->getName())) {
-                e->setTraversalMaskBits(SSGTRAV_HOT);
-                ssgBase* ud = e->getUserData();
-                if (ud) {
-                    FGAICarrierHardware* ch = dynamic_cast<FGAICarrierHardware*>(ud);
-                    if (ch) {
-                        SG_LOG(SG_GENERAL, SG_WARN,
-                                "AICarrier: Carrier hardware gets marked twice!\n"
-                                "           You have probably a whole branch marked as"
-                                " a wire which also includes other carrier hardware.");
-                    } else {
-                        SG_LOG(SG_GENERAL, SG_ALERT,
-                                "AICarrier: Found user data attached to a leaf node which "
-                                "should be marked as a wire!\n    ****Skipping!****");
-                    }
-                } else {
-                    e->setUserData( FGAICarrierHardware::newWire( this ) );
-                    ssgLeaf *l = (ssgLeaf*)e;
-                    if ( l->getNumLines() != 1 ) {
-                        SG_LOG(SG_GENERAL, SG_ALERT,
-                                "AICarrier: Found wires not modeled with exactly one line!");
-                    }
-                    found = true;
-                }
-            }
-        }
-    }
-    return found;
-}
-
-
-bool FGAICarrier::mark_solid(ssgEntity* e, const list<string>& solid_objects, bool mark) {
-    bool found = false;
-    if (e->isAKindOf(ssgTypeBranch())) {
-        ssgBranch* br = (ssgBranch*)e;
-        ssgEntity* kid;
-
-        list<string>::const_iterator it;
-        for (it = solid_objects.begin(); it != solid_objects.end(); ++it)
-            mark = mark || (e->getName() && (*it) == e->getName());
-
-        for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
-            found = mark_solid(kid, solid_objects, mark) || found;
-
-        if (found)
-            br->setTraversalMaskBits(SSGTRAV_HOT);
-
-    } else if (e->isAKindOf(ssgTypeLeaf())) {
-        list<string>::const_iterator it;
-        for (it = solid_objects.begin(); it != solid_objects.end(); ++it) {
-            if (mark || (e->getName() && (*it) == e->getName())) {
-                e->setTraversalMaskBits(SSGTRAV_HOT);
-                ssgBase* ud = e->getUserData();
-
-                if (ud) {
-                    FGAICarrierHardware* ch = dynamic_cast<FGAICarrierHardware*>(ud);
-                    if (ch) {
-                        SG_LOG(SG_GENERAL, SG_WARN,
-                                "AICarrier: Carrier hardware gets marked twice!\n"
-                                "           You have probably a whole branch marked solid"
-                                " which also includes other carrier hardware.");
-                    } else {
-                        SG_LOG(SG_GENERAL, SG_ALERT,
-                                "AICarrier: Found user data attached to a leaf node which "
-                                "should be marked solid!\n    ****Skipping!****");
-                    }
-                } else {
-                    e->setUserData( FGAICarrierHardware::newSolid( this ) );
-                    found = true;
-                }
-            }
-        }
-    }
-    return found;
-}
-
-
-bool FGAICarrier::mark_cat(ssgEntity* e, const list<string>& cat_objects, bool mark) {
-    bool found = false;
-    if (e->isAKindOf(ssgTypeBranch())) {
-        ssgBranch* br = (ssgBranch*)e;
-        ssgEntity* kid;
-
-        list<string>::const_iterator it;
-        for (it = cat_objects.begin(); it != cat_objects.end(); ++it)
-            mark = mark || (e->getName() && (*it) == e->getName());
-
-        for ( kid = br->getKid(0); kid != NULL ; kid = br->getNextKid() )
-            found = mark_cat(kid, cat_objects, mark) || found;
-
-        if (found)
-            br->setTraversalMaskBits(SSGTRAV_HOT);
-
-    } else if (e->isAKindOf(ssgTypeLeaf())) {
-        list<string>::const_iterator it;
-        for (it = cat_objects.begin(); it != cat_objects.end(); ++it) {
-            if (mark || (e->getName() && (*it) == e->getName())) {
-                e->setTraversalMaskBits(SSGTRAV_HOT);
-                ssgBase* ud = e->getUserData();
-                if (ud) {
-                    FGAICarrierHardware* ch = dynamic_cast<FGAICarrierHardware*>(ud);
-                    if (ch) {
-                        SG_LOG(SG_GENERAL, SG_WARN,
-                                "AICarrier: Carrier hardware gets marked twice!\n"
-                                "You have probably a whole branch marked as"
-                                "a catapult which also includes other carrier hardware.");
-                    } else {
-                        SG_LOG(SG_GENERAL, SG_ALERT,
-                                "AICarrier: Found user data attached to a leaf node which "
-                                "should be marked as a catapult!\n    ****Skipping!****");
-                    }
-                } else {
-                    e->setUserData( FGAICarrierHardware::newCatapult( this ) );
-                    ssgLeaf *l = (ssgLeaf*)e;
-                    if ( l->getNumLines() != 1 ) {
-                        SG_LOG(SG_GENERAL, SG_ALERT,
-                                "AICarrier: Found a cat not modeled with exactly "
-                                "one line!");
-                    } else {
-                        // Now some special code to make sure the cat points in the right
-                        // direction. The 0 index must be the backward end, the 1 index
-                        // the forward end.
-                        // Forward is positive x-direction in our 3D model, also the model
-                        // as such is flattened when it is loaded, so we do not need to
-                        // care for transforms ...
-                        short v[2];
-                        l->getLine(0, v, v+1 );
-                        sgVec3 ends[2];
-                        for (int k=0; k<2; ++k)
-                            sgCopyVec3( ends[k], l->getVertex( v[k] ) );
-
-                        // When the 1 end is behind the 0 end, swap the coordinates.
-                        if (ends[0][0] < ends[1][0]) {
-                            sgCopyVec3( l->getVertex( v[0] ), ends[1] );
-                            sgCopyVec3( l->getVertex( v[1] ), ends[0] );
-                        }
-                        found = true;
-                    }
-                }
-            }
-        }
-    }
-    return found;
-}
-
 // find relative wind
 void FGAICarrier::UpdateWind( double dt) {
 
@@ -685,18 +567,8 @@ void FGAICarrier::TurnToBase(){
 void FGAICarrier::ReturnToBox(){
     double course, distance, az2;
 
-    //get the carrier position
-    carrierpos = pos;
-
-    //cout << "lat: " << carrierpos[1] << " lon: " << carrierpos[0] << endl;
-
     //calculate the bearing and range of the initial position from the carrier
-    geo_inverse_wgs_84(carrierpos[2],
-                       carrierpos[1],
-                       carrierpos[0],
-                       initialpos[1],
-                       initialpos[0],
-                       &course, &az2, &distance);
+    geo_inverse_wgs_84(pos, mOpBoxPos, &course, &az2, &distance);
 
     distance *= SG_METER_TO_NM;
 
@@ -720,33 +592,33 @@ bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operat
         return false;
     }
 
-    if (initialpos[1] >= 0) { //northern hemisphere
-        if (pos[1] >= initialpos[1] + max_lat)
+    if (mOpBoxPos.getLatitudeDeg() >= 0) { //northern hemisphere
+        if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + max_lat)
             return true;
 
-        if (pos[1] <= initialpos[1] - min_lat)
+        if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - min_lat)
             return true;
 
     } else {                  //southern hemisphere
-        if (pos[1] <= initialpos[1] - max_lat)
+        if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - max_lat)
             return true;
 
-        if (pos[1] >= initialpos[1] + min_lat)
+        if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + min_lat)
             return true;
     }
 
-    if (initialpos[0] >=0) { //eastern hemisphere
-        if (pos[0] >= initialpos[0] + max_long)
+    if (mOpBoxPos.getLongitudeDeg() >=0) { //eastern hemisphere
+        if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + max_long)
             return true;
 
-        if (pos[0] <= initialpos[0] - min_long)
+        if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - min_long)
             return true;
 
     } else {                 //western hemisphere
-        if (pos[0] <= initialpos[0] - max_long)
+        if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - max_long)
             return true;
 
-        if (pos[0] >= initialpos[0] + min_long)
+        if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + min_long)
             return true;
     }
 
@@ -756,12 +628,6 @@ bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operat
 } // end OutsideBox
 
 
-// return the distance to the horizon, given the altitude and the radius of the earth
-float FGAICarrier::Horizon(float h) {
-    return RADIUS_M * acos(RADIUS_M / (RADIUS_M + h));
-}
-
-
 bool FGAICarrier::InToWind() {
     if ( fabs(rel_wind) < 5 )
         return true;