Lagrangian Liquid Simulation
Master Thesis project on simulation of liquids using Lagrangian approach and SPH
src/Cache.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 #include "Cache.h"
00005 #include <fstream>
00006 #include <QString>
00007 
00008 Cache::Cache()
00009 {
00010 
00011 }
00012 
00013 void Cache::addItem(const CacheItem& _cacheItem)
00014 {
00015     m_fluidCache.push_back(_cacheItem);
00016 }
00017 
00018 void Cache::flushCache(const std::string _path)
00019 {
00020     //loop through cache items
00021     for (int i = 0; i < m_fluidCache.size(); ++i)
00022     {
00023         //get cache item
00024         CacheItem item = m_fluidCache[i];
00025 
00026         //loop through name list of the item
00027         for (int j = 0; j < item.getNameList().size(); ++j)
00028         {
00029             //determine type of item
00030             std::string itemType;
00031             if (item.getCacheItemType() == FLUID_CACHE)
00032                 itemType = "Fluid";
00033             else if (item.getCacheItemType() == BOUNDARY_CACHE)
00034                 itemType = "Boundary";
00035             else if (item.getCacheItemType() == SPHERE_CACHE)
00036                 itemType = "Sphere";
00037             else if (item.getCacheItemType() == CAPSULE_CACHE)
00038                 itemType = "Capsule";
00039             else
00040                 itemType = "NA";
00041 
00042             //construct filename of the item for the current frame number
00043             QString text = QString("%1%2_%3_Frame_%4.geo")
00044                             .arg(_path.c_str())
00045                             .arg(itemType.c_str())
00046                             .arg(std::string(item.getNameList()[j]).c_str())
00047                             .arg(item.getFrameNumber());
00048 
00049             //write data to file
00050             if (item.getCacheItemType() == FLUID_CACHE || item.getCacheItemType() == SPHERE_CACHE || item.getCacheItemType() == CAPSULE_CACHE)
00051                 writePointData(item.getPositionList()[j], text.toStdString());
00052             else if (item.getCacheItemType() == BOUNDARY_CACHE)
00053                 writeBoundaryData(item.getPositionList()[j], text.toStdString());
00054 
00055         }
00056     }
00057 
00058     //clear cache
00059     m_fluidCache.clear();
00060 }
00061 
00062 void Cache::writePointData
00063                 (
00064                     std::vector<ngl::Vector>& _positionList,
00065                     const std::string& _filename
00066                 )
00067 {
00068     //open file for writing
00069     std::ofstream outFile(_filename.c_str(), std::ios::out);
00070 
00071     //write header
00072     outFile << "PGEOMETRY V5\n";
00073     outFile << "NPoints " << _positionList.size() << " NPrims 0\n";
00074     outFile << "NPointGroups 0 NPrimGroups 0\n";
00075     outFile << "NPointAttrib 0 NVertexAttrib 0 NPrimAttrib 0 NAttrib 0\n";
00076 
00077     //loop through position list
00078     for (int i = 0; i < _positionList.size(); ++i)
00079     {
00080         outFile
00081                 << _positionList[i].m_x << " "
00082                 << _positionList[i].m_y << " "
00083                 << _positionList[i].m_z << " "
00084                 << _positionList[i].m_w << "\n";
00085     }
00086 
00087     //write the outro
00088     outFile << "beginExtra\n";
00089     outFile << "endExtra";
00090 
00091     //close file
00092     outFile.close();
00093 }
00094 
00095 void Cache::writeBoundaryData
00096                 (
00097                     std::vector<ngl::Vector>& _positionList,
00098                     const std::string& _filename
00099                 )
00100 {
00101     //open file for writing
00102     std::ofstream outFile(_filename.c_str(), std::ios::out);
00103 
00104     //write header
00105     outFile << "PGEOMETRY V5\n";
00106     outFile << "NPoints 8 NPrims 6\n";
00107     outFile << "NPointGroups 0 NPrimGroups 0\n";
00108     outFile << "NPointAttrib 0 NVertexAttrib 0 NPrimAttrib 0 NAttrib 0\n";
00109 
00110     //loop through position list
00111     for (int i = 0; i < _positionList.size(); ++i)
00112     {
00113         outFile
00114                 << _positionList[i].m_x << " "
00115                 << _positionList[i].m_y << " "
00116                 << _positionList[i].m_z << " "
00117                 << _positionList[i].m_w << "\n";
00118     }
00119 
00120     //write box vertex connections
00121     outFile << "Run 6 Poly\n";
00122     outFile << " 4 < 1 5 4 0\n";
00123     outFile << " 4 < 2 6 5 1\n";
00124     outFile << " 4 < 3 7 6 2\n";
00125     outFile << " 4 < 0 4 7 3\n";
00126     outFile << " 4 < 2 1 0 3\n";
00127     outFile << " 4 < 5 6 7 4\n";
00128 
00129     //write the outro
00130     outFile << "beginExtra\n";
00131     outFile << "endExtra";
00132 
00133     //close file
00134     outFile.close();
00135 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator