MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Essentials/MOOSUtilityLib/MOOSTerrain.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS Core Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00030 // MOOSTerrain.cpp: implementation of the CMOOSTerrain class.
00031 //
00033 
00034 
00035 #include "MOOSTerrain.h"
00036 
00037 #include <fstream>
00038 #include <iostream>
00039 using namespace std;
00040 
00041 
00043 // Construction/Destruction
00045 
00046 CMOOSTerrain::CMOOSTerrain()
00047 {
00048     m_dfMinZ=1e10;
00049     m_dfMaxZ=-1e10;
00050 
00051 }
00052 
00053 CMOOSTerrain::~CMOOSTerrain()
00054 {
00055 
00056 }
00057 
00058 bool CMOOSTerrain::Load(const char *sFileName)
00059 {
00060     ifstream In;
00061 
00062     In.open(sFileName);
00063 
00064     if(In.is_open())
00065     {
00066         double dfRows;
00067         double dfCols;
00068 
00069 
00070         In>>dfRows;
00071         In>>dfCols;
00072 //        m_Data.ReSize(dfRows,dfCols);
00073 
00074 
00075     
00076         m_Data.resize((int)dfRows+1);
00077 
00078         STLMATRIX::iterator p;
00079 
00080         for(p=m_Data.begin();p!=m_Data.end();p++)
00081         {
00082             p->resize((int)(dfCols+1));
00083         }
00084 
00085     
00086         
00087         m_X.resize((int)dfCols+1);
00088         m_Y.resize((int)dfRows+1);
00089 
00090         double dfTmp;
00091         int i,j;
00092         for(i =1;i<=dfCols;i++)
00093         {
00094             In>>dfTmp;
00095             m_X[i]=dfTmp;
00096         }
00097         for(i =1;i<=dfRows;i++)
00098         {
00099             In>>dfTmp;
00100             m_Y[i]=dfTmp;
00101         }
00102         for(i =1;i<=dfRows;i++)
00103         {
00104             for(j =1;j<=dfCols;j++)
00105             {
00106                 In>>dfTmp;
00107 
00108                 m_dfMinZ = dfTmp<m_dfMinZ ? dfTmp : m_dfMinZ;
00109                 m_dfMaxZ = dfTmp>m_dfMaxZ ? dfTmp : m_dfMaxZ;
00110 
00111                 m_Data[i][j]=dfTmp;
00112             }
00113         }
00114     
00115 
00116         return true;
00117     }
00118     else
00119     {
00120         return false;
00121     }
00122 }
00123 
00124 double CMOOSTerrain::GetAltitude(double dfX, double dfY, double dfZ)
00125 {
00126     double dfAltitude = -1;
00127 
00128     //find x and y 
00129     int n = m_X.size()-1;
00130     int I=-1;
00131     double dfAlphaI = 0;
00132     for(int i=2;i<=n;i++)
00133     {
00134         if(m_X[i-1]<=dfX && m_X[i]>=dfX)
00135         {
00136             I = i;
00137             if(m_X[i-1]!=m_X[i])
00138             {
00139                 dfAlphaI = (dfX-m_X[i-1])/(m_X[i]-m_X[i-1]);
00140 
00141             }
00142             break;
00143         }
00144     }
00145 
00146 
00147     n = m_Y.size()-1;
00148     double dfAlphaJ = 0;
00149     int J=-1;
00150     for(int j=2;j<=n;j++)
00151     {
00152         if(m_Y[j-1]<=dfY && m_Y[j]>=dfY)
00153         {
00154             J = j;
00155             if(m_Y[j-1]!=m_Y[j])
00156             {
00157                 dfAlphaJ = (dfY-m_Y[j-1])/(m_Y[j]-m_Y[j-1]);
00158 
00159             }
00160             break;
00161         }
00162     }
00163 
00164     if(J!=-1 && I!=-1)
00165     {
00166         double Z0 = m_Data[I-1][J-1];
00167         double Z1 = m_Data[I][J-1];
00168         double Z2 = m_Data[I][J];
00169         double Z3 = m_Data[I-1][J];
00170 
00171 
00172        dfAltitude =  dfZ-   (   (1-dfAlphaI)*(1-dfAlphaJ)*Z0 +
00173                             (1-dfAlphaI)*dfAlphaJ*Z3 +
00174                             dfAlphaI*(1-dfAlphaJ)*Z1 +
00175                             dfAlphaI*dfAlphaJ*Z2);
00176 
00177 
00178 
00179     }
00180     
00181     return dfAltitude;
00182 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines