PSMeadering.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2010, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita and Pedro Sousa on 02/15/2010
00036 *********************************************************************/
00037 #include <string.h>
00038 #include <stdio.h>
00039 #include <math.h>
00040 
00041 #include "PSSources.h"
00042 
00043 // *****************************************************************************
00044 // Constructor.
00045 plumesim::PSMeadering::PSMeadering()
00046 {
00047         this->changesOverTime = false;
00048         
00049         this->startPoint.px = 0.0;
00050         this->startPoint.py = 0.0;
00051         this->startPoint.pz = 0.0;
00052         
00053         this->arenaRect.startPoint.px = 0.0;
00054         this->arenaRect.startPoint.py = 0.0;
00055         this->arenaRect.endPoint.px = 0.0;
00056         this->arenaRect.endPoint.py = 0.0;
00057         
00058         this->numOfPlumePoints = 200;
00059         this->releaseRate = 10.0;
00060         this->dispersionCoeficient = 0.1;
00061         this->radius = 0.1;
00062 }
00063 
00064 // *****************************************************************************
00065 // Destructor.
00066 plumesim::PSMeadering::~PSMeadering()
00067 {
00068         // Clean up...
00069 }
00070 
00071 // *****************************************************************************
00072 // Setup routine.
00073 int plumesim::PSMeadering::setup()
00074 {
00075         if(!plumePoints.empty()) plumePoints.clear();
00076         this->isPlaying = true;
00077         return 0;
00078 }
00079 
00080 // *****************************************************************************
00081 // Cleanup routine.
00082 int plumesim::PSMeadering::cleanup()
00083 {
00084         plumePoints.clear();
00085         return 0;
00086 }
00087 
00088 // *****************************************************************************
00089 // Generate points.
00090 int plumesim::PSMeadering::generatePoints()
00091 {
00092         int i;
00093         
00094         if(!plumePoints.empty()) plumePoints.clear();
00095 
00096         PSPoint3d startupPoints[MIN_NUM_OF_POINTS];
00097         
00098         double step = (arenaRect.endPoint.px - startPoint.px)/MIN_NUM_OF_POINTS;
00099         // Fill the startup array
00100         for(i=0 ; i<MIN_NUM_OF_POINTS ; i++) startupPoints[i].px = startPoint.px + i * step;
00101         
00102         double sigma = 0.2*step;
00103         // Posicao do centro da pluma na origem
00104         startupPoints[0].py = startPoint.py;
00105         
00106         double ay[MIN_NUM_OF_POINTS];
00107         // Fill the array
00108         for(i=0 ; i<MIN_NUM_OF_POINTS ; i++) ay[i] = sigma*randomNormal();
00109         
00110         double vy[MIN_NUM_OF_POINTS];
00111         // Velocidade lateral da pluma na origem
00112         vy[0] = sigma*randomNormal();
00113         
00114         for(i=1 ; i<MIN_NUM_OF_POINTS ; i++)
00115         {
00116                 // Perturbacao da velocidade lateral
00117                 vy[i] = vy[i-1] + ay[i-1];
00118                 // Actualizacao do centro da pluma
00119                 startupPoints[i].py = startupPoints[i-1].py + vy[i-1];
00120         }
00121         
00122         linearInterpolation(startupPoints, MIN_NUM_OF_POINTS);
00123         
00124         PSPoint3d * plumePoint;
00125         for(i=0 ; i<plumePoints.size() ; i++)
00126         {
00127                 plumePoint = &plumePoints[i];
00128                 // TODO: test for other release points
00129                 plumePoint->py = plumePoint->py + ( dispersionCoeficient * (plumePoint->px - startPoint.px) * (releaseRate * randomNormal()));
00130         }
00131         
00132         PSPoint3d center;
00133         center.px = startPoint.px + radius;
00134         center.py = startPoint.py;
00135         center.pz = 0.0;
00136         
00137         maxPoints = this->countPoints(&center, radius);
00138 
00139         this->isPlaying = false;
00140         
00141         return(0);
00142 }
00143 
00144 // *****************************************************************************
00145 // Sends the chemical reading for a given position.
00146 int plumesim::PSMeadering::getChemicalReading(PSPoint3d * point, PSOdorData * odor_data)
00147 {       
00148         int pointCount = this->countPoints(point, radius);
00149         
00150         odor_data->chemical = fmin(100.0, 100.0*pointCount/maxPoints);
00151         // Constants
00152         // TODO: Maybe get this from the .cfg file
00153         odor_data->windDirection = 0.0;         // rad
00154         odor_data->windSpeed = 0.5;                     // m/s
00155         
00156         return(0);
00157 }
00158 
00159 // *****************************************************************************
00160 // Linear interpolation
00161 void plumesim::PSMeadering::linearInterpolation(PSPoint3d * points, int numOfPoints)
00162 {
00163         double b, m;
00164         int i, j;
00165         PSPoint3d point;
00166 
00167         int pointsOnThisSection;
00168         
00169         double x_step = (points[numOfPoints-1].px - points[0].px) / numOfPlumePoints;
00170         
00171         for(i=0 ; i<numOfPoints-1 ; i++)
00172         {
00173                 point.px = points[i].px;
00174                 point.py = points[i].py;
00175                 point.pz = 0.0;
00176                 plumePoints.push_back(point);
00177                 
00178                 m = (points[i+1].py - points[i].py)/(points[i+1].px - points[i].px);
00179                 b = points[i].py - m * points[i].px;
00180                 
00181                 pointsOnThisSection = (points[i+1].px - points[i].px) / x_step;
00182                 
00183                 for(j=0 ; j<pointsOnThisSection ; j++)
00184                 {
00185                         point.px = points[i].px + (j+1)*x_step;
00186                         point.py = m * point.px + b;
00187                         point.pz = 0.0;
00188                         plumePoints.push_back(point);
00189                 }
00190         }
00191         point.px = points[numOfPoints-1].px;
00192         point.py = points[numOfPoints-1].py;
00193         point.pz = 0.0;
00194         plumePoints.push_back(point);
00195 }
00196 
00197 // EOF
00198 


plumesim
Author(s): Gonçalo Cabrita and Pedro Sousa
autogenerated on Mon Jan 6 2014 11:27:16