MultibeamSensor.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2013 University of Jaume-I.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Mario Prats
00010  *     Javier Perez
00011  */
00012 
00013 #include <uwsim/MultibeamSensor.h>
00014 
00015 MultibeamSensor::MultibeamSensor(osg::Group *uwsim_root, std::string name, osg::Node *trackNode, double initAngle,
00016                                  double finalAngle, double alpha, double range) :
00017     VirtualCamera(uwsim_root, name, trackNode, fabs(finalAngle - initAngle) / alpha + 1, fabs(finalAngle - initAngle),
00018                   range)
00019 {
00020 
00021   this->numpixels = fabs(finalAngle - initAngle) / alpha + 1;
00022   this->range = range;
00023   this->initAngle = initAngle;
00024   this->finalAngle = finalAngle;
00025   this->angleIncr = alpha;
00026   preCalcTable();
00027 }
00028 
00029 void MultibeamSensor::preCalcTable()
00030 {
00031 
00032   //Create matrix to unproject camera points to real world
00033   osg::Matrix *MVPW = new osg::Matrix(
00034       textureCamera->getViewMatrix() * textureCamera->getProjectionMatrix()
00035           * textureCamera->getViewport()->computeWindowMatrix());
00036   MVPW->invert(*MVPW);
00037 
00038   //Get real fov from camera
00039   osg::Vec3d first = osg::Vec3d(0, 0, 1) * (*MVPW), last = osg::Vec3d(0, numpixels - 1, 1) * (*MVPW), center =
00040                  osg::Vec3d(0, numpixels / 2, 1) * (*MVPW);
00041   double realfov = acos((first * last) / (last.length() * first.length()));
00042   double thetacenter = acos((first * center) / (center.length() * first.length()));
00043   double alpha = realfov / (numpixels);
00044   //std::cout<<realfov<<" "<<alpha<<std::endl;
00045 
00046   //Interpolate points
00047   remapVector.resize(numpixels);
00048   int current = 0;
00049   double lastTheta = 0;
00050   for (int i = 0; i < numpixels; i++)
00051   {
00052     osg::Vec3d point = osg::Vec3d(0, i, 1) * (*MVPW);
00053 
00054     double theta = acos((first * point) / (first.length() * point.length()));
00055     while (theta >= alpha * current && current < numpixels)
00056     {
00057       if (theta == alpha * current)
00058       { //usually only first iteration as point has to be exactly the same
00059         remapVector[current].pixel1 = i;
00060         remapVector[current].weight1 = 0.50;
00061         remapVector[current].pixel2 = i;
00062         remapVector[current].weight2 = 0.50;
00063       }
00064       else
00065       { //Interpolate between this and last point
00066         double dist = fabs(theta - alpha * current), prevdist = fabs(lastTheta - alpha * current);
00067         remapVector[current].pixel1 = i;
00068         remapVector[current].weight1 = prevdist / (dist + prevdist);
00069         remapVector[current].pixel2 = i - 1;
00070         remapVector[current].weight2 = dist / (dist + prevdist);
00071         //std::cout<<remapVector[current].weight1<<" "<<remapVector[current].weight2<<" "<<remapVector[current].weight1+remapVector[current].weight2<<std::endl;
00072       }
00073       remapVector[current].distort = 1 / cos(fabs(theta - thetacenter));
00074       //std::cout<<"remap: "<<remapVector[current].distort<<std::endl;
00075       current++;
00076       //std::cout<<theta<<":"<<tan(theta)<<" asd:"<<fx<<std::endl;
00077       //std::cout<<current<<" "<<i<<std::endl;
00078     }
00079     lastTheta = theta;
00080     //std::cout<<" THETA: "<<theta<<"Current point: "<<current*alpha<<"Error: "<<theta-i*alpha<<"asd: "<<current<<std::endl;
00081   }
00082 
00083 }


uwsim
Author(s): Mario Prats
autogenerated on Mon Oct 6 2014 08:24:07