PSFluent.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 03/03/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::PSFluent::PSFluent()
00046 {
00047         this->maxPointsPerCell = 10;
00048         this->fileName = "fluentlog";
00049         this->numOfFrames = 1;
00050         this->cellSize = 0.1;
00051         this->maxOdorValue = 1.0;
00052 }
00053 
00054 // *****************************************************************************
00055 // Destructor.
00056 plumesim::PSFluent::~PSFluent()
00057 {
00058         // Clean up...
00059 }
00060 
00061 // *****************************************************************************
00062 // Setup routine.
00063 int plumesim::PSFluent::setup()
00064 {
00065         if(!plumePoints.empty()) plumePoints.clear();
00066         if(!frame.empty()) frame.clear();
00067         
00068         if(this->numOfFrames > 1) this->changesOverTime = true;
00069         else this->changesOverTime = false;
00070         
00071         this->isPlaying = true;
00072         
00073         this->currentFrame = 1;
00074         
00075         return 0;
00076 }
00077 
00078 // *****************************************************************************
00079 // Cleanup routine.
00080 int plumesim::PSFluent::cleanup()
00081 {
00082         plumePoints.clear();
00083         frame.clear();
00084         return 0;
00085 }
00086 
00087 // *****************************************************************************
00088 // Read points from the log file.
00089 int plumesim::PSFluent::generatePoints()
00090 {
00091         if(currentFrame > numOfFrames)
00092         {
00093                 isPlaying = false;
00094                 return(0);
00095         }
00096         
00097         int i, j;
00098         int pointsCount;
00099         
00100         if(!plumePoints.empty()) plumePoints.clear();
00101         if(!frame.empty()) frame.clear();
00102         
00103         char logFileName[128];
00104         char logFileNum[8];
00105         strcpy(logFileName, fileName.c_str());
00106         sprintf(logFileNum, "%03d", currentFrame);
00107         strncat(logFileName, logFileNum, strlen(logFileNum));
00108         // Open the file
00109         logFile = fopen(logFileName, "r");
00110         if(logFile == NULL) return(-1);
00111         // Ignore the first line
00112         char dummy[64];
00113         fgets(dummy, 64, logFile);
00114         
00115         while( !feof(logFile) )
00116         {
00117                 PSFramePoint pointFromFrame;
00118                 pointFromFrame.point.pz = 0.0;
00119                 // index x y chemical
00120                 fscanf(logFile, "%d,%lf,%lf,%lf\n", &i, &pointFromFrame.point.px, &pointFromFrame.point.py, &pointFromFrame.odor.chemical);
00121                 pointFromFrame.odor.chemical = 100.0*pointFromFrame.odor.chemical/maxOdorValue;
00122                 
00123                 frame.push_back(pointFromFrame);
00124                         
00125                 pointsCount = round(maxPointsPerCell * pointFromFrame.odor.chemical/100.0);
00126                 for(j=0 ; j<pointsCount ; j++)
00127                 {
00128                         PSPoint3d point;
00129                         point.px = pointFromFrame.point.px + cellSize * randomNormal();
00130                         point.py = pointFromFrame.point.py + cellSize * randomNormal();
00131                         // TODO: Complete for full 3D support!
00132                         point.pz = 0.0;
00133                         plumePoints.push_back(point);
00134                 }
00135         }
00136         currentFrame++;
00137         
00138         // Close the file
00139         fclose(logFile);
00140         
00141         return(0);
00142 }
00143 
00144 // *****************************************************************************
00145 // Sends the chemical reading for a given position.
00146 int plumesim::PSFluent::getChemicalReading(PSPoint3d * point, PSOdorData * odor_data)
00147 {       
00148         double distance;
00149         double minDistance = cellSize;
00150         
00151         odor_data->chemical = 0.0;
00152         odor_data->windSpeed = 0.0;
00153         odor_data->windDirection = 0.0;
00154         
00155         PSFramePoint * framePoint;
00156         for(int i=0 ; i<this->frame.size() ; i++)
00157         {
00158                 framePoint = &this->frame[i];
00159                 
00160                 distance = sqrt((point->px - framePoint->point.px)*(point->px - framePoint->point.px) + (point->py - framePoint->point.py)*(point->py - framePoint->point.py) + (point->pz - framePoint->point.pz)*(point->pz - framePoint->point.pz));
00161                 if(distance < minDistance)
00162                 {
00163                         odor_data->chemical = framePoint->odor.chemical;
00164                         odor_data->windSpeed = framePoint->odor.windSpeed;
00165                         odor_data->windDirection = framePoint->odor.windDirection;
00166                         
00167                         minDistance = distance;
00168                 }
00169         }
00170         return(0);
00171 }
00172 
00173 // EOF
00174 


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