CConverter.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, LABUST, UNIZG-FER
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the LABUST nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 #include <labust/blueview/CConverter.hpp>
00035 #include <labust/blueview/TrackerROI.hpp>
00036 #include <labust/math/uBlasOperations.hpp>
00037 
00038 using namespace labust::blueview;
00039 
00040 CConverter::CConverter(){};
00041 
00042 CConverter::~CConverter(){};
00043 
00044 void CConverter::llz2xy(const SonarHead& head, const TrackedFeaturePtr tracklet)
00045 {
00046   //Remember to update the rotation matrix
00047   this->update(head);
00048 
00049   //Calculate the X-Y offset
00050   std::pair<double,double>
00051    value = deg2meter(tracklet->latlon.x - head.latlon.x,tracklet->latlon.y - head.latlon.y,head.latlon.x);
00052 
00053   //Testing code
00054   //std::cout<<"Move:"<<value.first<<","<<value.second<<std::endl;
00055 
00056   enum {x = 0,y,z};
00057   vector pos(3);
00058   pos(x) = value.first;
00059   pos(y) = value.second;
00060   //We assume its in the middle of the beam (A BIG ASSUMPTION)
00061   pos(z) = tracklet->latlon.z;
00062 
00063   //Calculate to head X-Y-Z coordinates
00064   matrix inverse;
00065   labust::math::gjinverse(R,inverse);
00066   pos = prod(inverse,pos);
00067 
00068   //Update tracklet X-Y position
00069   tracklet->position.x = pos(x);
00070   tracklet->position.y = pos(y);
00071 }
00072 
00073 void CConverter::xy2llz(const SonarHead& head, const TrackedFeaturePtr tracklet)
00074 {
00075   //Remember to update the rotation matrix
00076   this->update(head);
00077 
00078   enum {x = 0,y,z};
00079   vector pos(3);
00080   pos(x) = tracklet->position.x;
00081   pos(y) = tracklet->position.y;
00082   //We assume its in the middle of the beam (A BIG ASSUMPTION)
00083   pos(z) = 0;
00084 
00085   pos = prod(R,pos);
00086   std::pair<double,double> value = meter2deg(pos(x), pos(y), head.latlon.x);
00087 
00088   //Convert to latlon
00089   tracklet->latlon.x = head.latlon.x + value.first;
00090   tracklet->latlon.y = head.latlon.y + value.second;
00091   tracklet->latlon.z = head.latlon.z + pos(z);
00092 }
00093 
00094 void CConverter::meter2pixel(const TrackerROI& roi, const TrackedFeaturePtr tracklet)
00095 {
00096   //std::cout<<"ROI origin:"<<roi->origin.x<<","<<roi->origin.y<<std::endl;
00097   //std::cout<<"Meter position:"<<(tracklet.position.x)<<","<<(tracklet.position.y)<<std::endl;
00098 
00099   double pxrange = std::sqrt(std::pow(tracklet->position.y,2) + std::pow(tracklet->position.x,2))/roi.headData.resolution;
00100   double bearing = std::atan2(tracklet->position.y,tracklet->position.x);
00101 
00102   tracklet->pposition.y = roi.origin.y - int(pxrange*std::cos(bearing));
00103   tracklet->pposition.x = roi.origin.x + int(pxrange*std::sin(bearing));
00104 
00105   //std::cout<<"Pixel position: ("<<tracklet.pposition.x<<","<<tracklet.pposition.y<<")"<<std::endl;
00106 }
00107 
00108 void CConverter::pixel2meter(const TrackerROI& roi, const TrackedFeaturePtr tracklet)
00109 {
00110   //std::cout<<"ROI origin:"<<roi->origin.x<<","<<roi->origin.y<<std::endl;
00111   //std::cout<<"Pixel position 2:"<<(tracklet.pposition.x)<<","<<(tracklet.pposition.y)<<std::endl;
00112 
00113   tracklet->position.y = (-roi.origin.x + tracklet->pposition.x)*roi.headData.resolution;
00114   tracklet->position.x = (roi.origin.y - tracklet->pposition.y)*roi.headData.resolution;
00115 
00116   //std::cout<<"Meter position: ("<<tracklet.position.x<<","<<tracklet.position.y<<")"<<std::endl;
00117 
00118   //std::cout<<"Before:"<<tracklet.position.x<<","<<tracklet.position.y<<std::endl;
00119 }
00120 
00121 void CConverter::update(const SonarHead& head)
00122 {
00123         labust::math::rotation_matrix R_hb(0,head.tiltAngle*M_PI/180,head.panAngle*M_PI/180);
00124         labust::math::rotation_matrix R_bn(0,0,head.heading*M_PI/180);
00125 
00126         this->R = prod(R_bn(),R_hb());
00127 }
00128 
00129 std::pair<double,double> CConverter::meter2deg(double x, double y, double lat)
00130 {
00131   static const double radius = 6378137;
00132   static const double ratio = 0.99664719;
00137   double mpdlat = 111132.954 - 559.822*cos(2*lat*M_PI/180) + 1.175*cos(4*lat*M_PI/180);
00138   double mpdlon = M_PI*radius*cos(atan(ratio*tan(lat*M_PI/180)))/180;
00139 
00140   return std::pair<double,double>(x/mpdlat,y/mpdlon);
00141 }
00142 
00143 std::pair<double,double> CConverter::deg2meter(double difflat, double difflon, double lat)
00144 {
00145   static const double radius = 6378137;
00146   static const double ratio = 0.99664719;
00151   double mpdlat = 111132.954 - 559.822*cos(2*lat*M_PI/180) + 1.175*cos(4*lat*M_PI/180);
00152   double mpdlon = M_PI*radius*cos(atan(ratio*tan(lat*M_PI/180)))/180;
00153 
00154   return std::pair<double,double>(difflat*mpdlat,difflon*mpdlon);
00155 }


target_detector
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:36:05