diff --git a/Analysis.cpp b/Analysis.cpp index ff25423d7fd385d22e5e6e713323f1934d61363d..2c7588b6b4a09a2e652b78654b93074d394fee55 100644 --- a/Analysis.cpp +++ b/Analysis.cpp @@ -166,7 +166,8 @@ void Analysis::InitArgs(ArgumentParser* args) _StopFramesMethodD = args->GetStopFramesMethodD(); _IndividualFDFlags = args->GetIndividualFDFlags(); _plotTimeseriesD=args->GetIsPlotTimeSeriesD(); - _geoPoly = ReadGeometry(args->GetGeometryFilename(), _areaForMethod_D); + _geoPolyMethodD = ReadGeometry(args->GetGeometryFilename(), _areaForMethod_D); + } if(args->GetIsMethodI()) { _DoesUseMethodI = true; @@ -179,14 +180,19 @@ void Analysis::InitArgs(ArgumentParser* args) _StopFramesMethodI = args->GetStopFramesMethodI(); _IndividualFDFlags = args->GetIndividualFDFlags(); _plotTimeseriesI=args->GetIsPlotTimeSeriesI(); - _geoPoly = ReadGeometry(args->GetGeometryFilename(), _areaForMethod_I); + _geoPolyMethodI = ReadGeometry(args->GetGeometryFilename(), _areaForMethod_I); } - if( _DoesUseMethodD && _DoesUseMethodI) - { - Log->Write("Warning:\t Using both method D and I is not safe!"); - // because ReadGeomtry() may be called twice - } +// ToDo: obsolete ? + +// if( _DoesUseMethodD && _DoesUseMethodI) +// { +// Log->Write("Warning:\t Using both method D and I is not safe!"); +// // because ReadGeometry() may be called twice (line 169 and 182) +// // overwrite _geoPoly -> new value for each Method +// Log->Write("Info:\t Using both method D and I is safe! :-)"); +// } + _deltaF = args->GetDelatT_Vins(); _cutByCircle = args->GetIsCutByCircle(); _getProfile = args->GetIsGetProfile(); @@ -336,6 +342,7 @@ int Analysis::RunAnalysis(const fs::path& filename, const fs::path& path) if(result_A) { Log->Write("INFO:\tSuccess with Method A using measurement area id %d!\n",_areaForMethod_A[i]->_id); + std::cout << "INFO:\tSuccess with Method A using measurement area id "<< _areaForMethod_A[i]->_id << "\n"; } else { @@ -361,6 +368,7 @@ int Analysis::RunAnalysis(const fs::path& filename, const fs::path& path) if(result_B) { Log->Write("INFO:\tSuccess with Method B using measurement area id %d!\n",_areaForMethod_B[i]->_id); + std::cout << "INFO:\tSuccess with Method B using measurement area id "<< _areaForMethod_B[i]->_id << "\n"; } else { @@ -385,6 +393,7 @@ int Analysis::RunAnalysis(const fs::path& filename, const fs::path& path) if(result_C) { Log->Write("INFO:\tSuccess with Method C using measurement area id %d!\n",_areaForMethod_C[i]->_id); + std::cout << "INFO:\tSuccess with Method C using measurement area id "<< _areaForMethod_C[i]->_id << "\n"; if(_plotTimeseriesC[i]) { string parameters_Timeseries=" " + _scriptsLocation.string()+ @@ -417,7 +426,7 @@ int Analysis::RunAnalysis(const fs::path& filename, const fs::path& path) method_D.SetStartFrame(_StartFramesMethodD[i]); method_D.SetStopFrame(_StopFramesMethodD[i]); method_D.SetCalculateIndividualFD(_IndividualFDFlags[i]); - method_D.SetGeometryPolygon(_geoPoly[_areaForMethod_D[i]->_id]); + method_D.SetGeometryPolygon(_geoPolyMethodD[_areaForMethod_D[i]->_id]); method_D.SetGeometryFileName(_geometryFileName); method_D.SetGeometryBoundaries(_lowVertexX, _lowVertexY, _highVertexX, _highVertexY); method_D.SetGridSize(_grid_size_X, _grid_size_Y); @@ -470,15 +479,15 @@ int Analysis::RunAnalysis(const fs::path& filename, const fs::path& path) method_I.SetStartFrame(_StartFramesMethodI[i]); method_I.SetStopFrame(_StopFramesMethodI[i]); method_I.SetCalculateIndividualFD(_IndividualFDFlags[i]); - method_I.SetGeometryPolygon(_geoPoly[_areaForMethod_I[i]->_id]); + method_I.SetGeometryPolygon(_geoPolyMethodI[_areaForMethod_I[i]->_id]); method_I.SetGeometryFileName(_geometryFileName); method_I.SetGeometryBoundaries(_lowVertexX, _lowVertexY, _highVertexX, _highVertexY); method_I.SetGridSize(_grid_size_X, _grid_size_Y); method_I.SetOutputVoronoiCellData(_outputGraph); - method_I.SetPlotVoronoiGraph(_plotGraph); + // method_I.SetPlotVoronoiGraph(_plotGraph); method_I.SetPlotVoronoiIndex(_plotIndex); method_I.SetDimensional(_isOneDimensional); - method_I.SetCalculateProfiles(_getProfile); + // method_I.SetCalculateProfiles(_getProfile); method_I.SetTrajectoriesLocation(path); if(_cutByCircle) { diff --git a/Analysis.h b/Analysis.h index 6f89efd5e8ebe68d4da8f7ca3e0bbbb44101b924..627b3cb288d78ed5a68e0e15fa26ca611b2266d6 100644 --- a/Analysis.h +++ b/Analysis.h @@ -111,7 +111,8 @@ private: Building* _building; //polygon_2d _geoPoly; - std::map<int, polygon_2d> _geoPoly; + std::map<int, polygon_2d> _geoPolyMethodD; + std::map<int, polygon_2d> _geoPolyMethodI; double _grid_size_X; // the size of the grid double _grid_size_Y; diff --git a/methods/Method_D.cpp b/methods/Method_D.cpp index 6fd827bd8c486d697ed4bde5cc9779b0f79fe364..1032a80646ef23705c424fff90f74ac6844589d3 100644 --- a/methods/Method_D.cpp +++ b/methods/Method_D.cpp @@ -140,6 +140,7 @@ bool Method_D::Process (const PedData& peddata,const fs::path& scriptsLocation, vector<int> IdInFrame = peddata.GetIdInFrame(frameNr, ids, zPos_measureArea); vector<double> XInFrame = peddata.GetXInFrame(frameNr, ids, zPos_measureArea); vector<double> YInFrame = peddata.GetYInFrame(frameNr, ids, zPos_measureArea); + vector<double> ZInFrame = peddata.GetZInFrame(frameNr, ids, zPos_measureArea); vector<double> VInFrame = peddata.GetVInFrame(frameNr, ids, zPos_measureArea); //vector int to_remove //------------------------------Remove peds outside geometry------------------------------------------ @@ -151,6 +152,7 @@ bool Method_D::Process (const PedData& peddata,const fs::path& scriptsLocation, IdInFrame.erase(IdInFrame.begin() + i); XInFrame.erase(XInFrame.begin() + i); YInFrame.erase(YInFrame.begin() + i); + ZInFrame.erase(ZInFrame.begin() + i); VInFrame.erase(VInFrame.begin() + i); i--; } @@ -189,7 +191,8 @@ bool Method_D::Process (const PedData& peddata,const fs::path& scriptsLocation, { if(!_isOneDimensional) { - GetIndividualFD(polygons,VInFrame, IdInFrame, _areaForMethod_D->_poly, str_frid); // TODO polygons_id + // GetIndividualFD(polygons,VInFrame, IdInFrame, _areaForMethod_D->_poly, str_frid); // TODO polygons_id + GetIndividualFD(polygons,VInFrame, IdInFrame, _areaForMethod_D->_poly, str_frid, XInFrame, YInFrame, ZInFrame); } } if(_getProfile) @@ -257,7 +260,7 @@ bool Method_D::OpenFileIndividualFD() { fs::path trajFileName("_id_"+_measureAreaId+".dat"); fs::path indFDPath("Fundamental_Diagram"); - indFDPath = _outputLocation / indFDPath / "IndividualFD" / (_trajName.string() + trajFileName.string()); + indFDPath = _outputLocation / indFDPath / "IndividualFD" / ("IFD_D_" +_trajName.string() + trajFileName.string()); string Individualfundment=indFDPath.string(); if((_fIndividualFD=Analysis::CreateFile(Individualfundment))==nullptr) { @@ -268,11 +271,11 @@ bool Method_D::OpenFileIndividualFD() { if(_isOneDimensional) { - fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame \t PedId \t Individual density(m^(-1)) \t Individual velocity(m/s) \t Headway(m)\n",_fps); + fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame\tPersID\tIndividual density(m^(-1))\tIndividual velocity(m/s)\tHeadway(m)\n",_fps); } else { - fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame \t PedId \t Individual density(m^(-2)) \t Individual velocity(m/s) \t Voronoi Polygon \t Intersection Polygon\n",_fps); + fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame\tPersID\tx/m\ty/m\tz/m\tIndividual density(m^(-2))\tIndividual velocity(m/s)\tVoronoi Polygon\tIntersection Polygon\n",_fps); } return true; } @@ -561,10 +564,11 @@ void Method_D::OutputVoroGraph(const string & frameId, std::vector<std::pair<po return polygon_str; }*/ -void Method_D::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<double>& Velocity, const vector<int>& Id, const polygon_2d& measureArea, const string& frid) +void Method_D::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<double>& Velocity, const vector<int>& Id, const polygon_2d& measureArea, const string& frid, vector<double>& XInFrame, vector<double>& YInFrame, vector<double>& ZInFrame) { double uniquedensity=0; double uniquevelocity=0; + double x, y, z; int uniqueId=0; int temp=0; for (const auto & polygon_iterator:polygon) @@ -581,9 +585,15 @@ void Method_D::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<d uniquedensity=1.0/(area(polygon_iterator)*CMtoM*CMtoM); uniquevelocity=Velocity[temp]; uniqueId=Id[temp]; - fprintf(_fIndividualFD,"%s\t%d\t%.3f\t%.3f\t%s\t%s\n", + x = XInFrame[temp]*CMtoM; + y = YInFrame[temp]*CMtoM; + z = ZInFrame[temp]*CMtoM; + fprintf(_fIndividualFD,"%s\t %d\t %.4f\t %.4f\t %.4f\t %.4f\t %.4f\t%s\t%s\n", frid.c_str(), uniqueId, + x, + y, + z, uniquedensity, uniquevelocity, polygon_str.c_str(), diff --git a/methods/Method_D.h b/methods/Method_D.h index 96aa7946e16f8ce3e14e1bc47382204c5f30aa55..2480672adc3f81e6ff77757d63f1f8c7eb5fbebb 100644 --- a/methods/Method_D.h +++ b/methods/Method_D.h @@ -103,7 +103,7 @@ private: std::tuple<double,double> GetVoronoiDensityVelocity(const std::vector<polygon_2d>& polygon, const std::vector<double>& Velocity, const polygon_2d & measureArea); void GetProfiles(const std::string& frameId, const std::vector<polygon_2d>& polygons, const std::vector<double>& velocity); void OutputVoroGraph(const std::string & frameId, std::vector<std::pair<polygon_2d, int> >& polygons, int numPedsInFrame,const std::vector<double>& VInFrame); - void GetIndividualFD(const std::vector<polygon_2d>& polygon, const std::vector<double>& Velocity, const std::vector<int>& Id, const polygon_2d& measureArea, const std::string& frid); + void GetIndividualFD(const std::vector<polygon_2d>& polygon, const std::vector<double>& Velocity, const std::vector<int>& Id, const polygon_2d& measureArea, const std::string& frid, std::vector<double>& XInFrame, std::vector<double>& YInFrame, std::vector<double>& ZInFrame); /** * Reduce the precision of the points to two digits * @param polygon diff --git a/methods/Method_I.cpp b/methods/Method_I.cpp index b2a1f4a7d05a90e731ff4975eced8452c3db93d6..70a351f82711714cf9bfdc296878b412183d87b8 100644 --- a/methods/Method_I.cpp +++ b/methods/Method_I.cpp @@ -54,7 +54,7 @@ Method_I::Method_I() _cutRadius = -1; _circleEdges = -1; _fIndividualFD = nullptr; - _calcIndividualFD = false; + _calcIndividualFD = true; _areaForMethod_I = nullptr; _plotVoronoiCellData=false; _isOneDimensional=false; @@ -160,7 +160,7 @@ bool Method_I::Process(const PedData& peddata,const fs::path& scriptsLocation, c } } - int NumPeds = IdInFrame.size(); + // int NumPeds = IdInFrame.size(); // std::cout << "numpeds = " << NumPeds << "\n"; //--------------------------------------------------------------------------------------------------------------- @@ -199,14 +199,16 @@ bool Method_I::Process(const PedData& peddata,const fs::path& scriptsLocation, c GetIndividualFD(polygons,VInFrame, IdInFrame, str_frid, XInFrame, YInFrame, ZInFrame); // } } - if(_getProfile) - { // field analysis - GetProfiles(str_frid, polygons, VInFrame); // TODO polygons_id - } - if(_outputVoronoiCellData) - { // output the Voronoi polygons of a frame - OutputVoroGraph(str_frid, polygons_id, NumPeds,VInFrame); - } + // ToDo: is obsolete +// if(_getProfile) +// { // field analysis +// GetProfiles(str_frid, polygons, VInFrame); // TODO polygons_id +// } + // ToDo: is obsolete +// if(_outputVoronoiCellData) +// { // output the Voronoi polygons of a frame +// OutputVoroGraph(str_frid, polygons_id, NumPeds,VInFrame); +// } } else { @@ -225,40 +227,44 @@ bool Method_I::Process(const PedData& peddata,const fs::path& scriptsLocation, c return return_value; } - bool Method_I::OpenFileMethodI() - { - - std::string voroLocation(VORO_LOCATION); - fs::path tmp("_id_"+_measureAreaId+".dat"); - tmp = _outputLocation / voroLocation / ("rho_v_Voronoi_" + _trajName.string() + tmp.string()); -// _outputLocation.string() + voroLocation+"rho_v_Voronoi_"+_trajName+"_id_"+_measureAreaId+".dat"; - string results_V= tmp.string(); +// ToDo: this function OpenFileMethodI is obsolete - can be deleted + +// bool Method_I::OpenFileMethodI() +// { +// +// std::string voroLocation(VORO_LOCATION); +// fs::path tmp("_id_"+_measureAreaId+".dat"); +// tmp = _outputLocation / voroLocation / ("rho_v_Voronoi_" + _trajName.string() + tmp.string()); +// // _outputLocation.string() + voroLocation+"rho_v_Voronoi_"+_trajName+"_id_"+_measureAreaId+".dat"; +// string results_V= tmp.string(); +// +// +// if((_fVoronoiRhoV=Analysis::CreateFile(results_V))==nullptr) +// { +// Log->Write("ERROR: \tcannot open the file to write Voronoi density and velocity\n"); +// return false; +// } +// else +// { +// if(_isOneDimensional) +// { +// fprintf(_fVoronoiRhoV,"#framerate:\t%.2f\n\n#Frame \t Voronoi density(m^(-1))\t Voronoi velocity(m/s)\n",_fps); +// } +// else +// { +// fprintf(_fVoronoiRhoV,"#framerate:\t%.2f\n\n#Frame \t Voronoi density(m^(-2))\t Voronoi velocity(m/s)\n",_fps); +// } +// return true; +// } +// } - if((_fVoronoiRhoV=Analysis::CreateFile(results_V))==nullptr) - { - Log->Write("ERROR: \tcannot open the file to write Voronoi density and velocity\n"); - return false; - } - else - { - if(_isOneDimensional) - { - fprintf(_fVoronoiRhoV,"#framerate:\t%.2f\n\n#Frame \t Voronoi density(m^(-1))\t Voronoi velocity(m/s)\n",_fps); - } - else - { - fprintf(_fVoronoiRhoV,"#framerate:\t%.2f\n\n#Frame \t Voronoi density(m^(-2))\t Voronoi velocity(m/s)\n",_fps); - } - return true; - } - } bool Method_I::OpenFileIndividualFD() { fs::path trajFileName("_id_"+_measureAreaId+".dat"); fs::path indFDPath("Fundamental_Diagram"); - indFDPath = _outputLocation / indFDPath / "IndividualFD" / (_trajName.string() + trajFileName.string()); + indFDPath = _outputLocation / indFDPath / "IndividualFD" / ("IFD_I_" +_trajName.string() + trajFileName.string()); string Individualfundment=indFDPath.string(); if((_fIndividualFD=Analysis::CreateFile(Individualfundment))==nullptr) { @@ -269,11 +275,11 @@ bool Method_I::Process(const PedData& peddata,const fs::path& scriptsLocation, c { if(_isOneDimensional) { - fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame \t PersID \t Individual density(m^(-1)) \t Individual velocity(m/s) \t Headway(m)\n",_fps); + fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame\tPersID\tIndividual density(m^(-1))\tIndividual velocity(m/s)\tHeadway(m)\n",_fps); } else { - fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame \t PersID \t x/m \t y/m \t z/m \t Individual density(m^(-2)) \t Individual velocity(m/s) \t Voronoi Polygon\n",_fps); + fprintf(_fIndividualFD,"#framerate (fps):\t%.2f\n\n#Frame\tPersID\tx/m\ty/m\tz/m\tIndividual density(m^(-2))\tIndividual velocity(m/s)\tVoronoi Polygon\n",_fps); } return true; } @@ -294,265 +300,276 @@ bool Method_I::Process(const PedData& peddata,const fs::path& scriptsLocation, c //todo HH polygons_id = vd.cutPolygonsWithGeometry(polygons_id, _geoPoly, XInFrame, YInFrame); // todo HH - // std:: cout << dsv(_geoPoly) << "\n"; + // std:: cout << dsv(_geoPoly) << "\n"; // ToDo: obsolete ? for(auto && p:polygons_id) { poly = p.first; ReducePrecision(poly); // TODO update polygon_id? } - // std:: cout << " GetPolygons leave " << polygons_id.size() << "\n"; + // std:: cout << " GetPolygons leave " << polygons_id.size() << "\n"; // ToDo: obsolete ? return polygons_id; } /** * Output the Voronoi density and velocity in the corresponding file */ - void Method_I::OutputVoronoiResults(const vector<polygon_2d>& polygons, const string& frid, const vector<double>& VInFrame) - { - double VoronoiVelocity=1; - double VoronoiDensity=-1; - std::tie(VoronoiDensity, VoronoiVelocity) = GetVoronoiDensityVelocity(polygons,VInFrame,_areaForMethod_I->_poly); - fprintf(_fVoronoiRhoV,"%s\t%.3f\t%.3f\n",frid.c_str(),VoronoiDensity, VoronoiVelocity); - } + +// ToDo: this function OutputVoronoiResults is obsolete - can be deleted + +// void Method_I::OutputVoronoiResults(const vector<polygon_2d>& polygons, const string& frid, const vector<double>& VInFrame) +// { +// double VoronoiVelocity=1; +// double VoronoiDensity=-1; +// std::tie(VoronoiDensity, VoronoiVelocity) = GetVoronoiDensityVelocity(polygons,VInFrame,_areaForMethod_I->_poly); +// fprintf(_fVoronoiRhoV,"%s\t%.3f\t%.3f\n",frid.c_str(),VoronoiDensity, VoronoiVelocity); +// } /* * calculate the voronoi density and velocity according to voronoi cell of each pedestrian and their instantaneous velocity "Velocity". * input: voronoi cell and velocity of each pedestrian and the measurement area * output: the voronoi density and velocity in the measurement area (tuple) */ - std::tuple<double,double> Method_I::GetVoronoiDensityVelocity(const vector<polygon_2d>& polygon, const vector<double>& Velocity, const polygon_2d & measureArea) - { - double meanV=0; - double density=0; - int temp=0; - for (auto && polygon_iterator:polygon) - { - polygon_list v; - intersection(measureArea, polygon_iterator, v); - if(!v.empty()) - { - meanV+=Velocity[temp]*area(v[0]); - density+=area(v[0])/area(polygon_iterator); - if((area(v[0]) - area(polygon_iterator))>J_EPS) - { - std::cout<<"----------------------Now calculating density-velocity!!!-----------------\n "; - std::cout<<"measure area: \t"<<std::setprecision(16)<<dsv(measureArea)<<"\n"; - std::cout<<"Original polygon:\t"<<std::setprecision(16)<<dsv(polygon_iterator)<<"\n"; - std::cout<<"intersected polygon: \t"<<std::setprecision(16)<<dsv(v[0])<<"\n"; - std::cout<<"this is a wrong result in density calculation\t "<<area(v[0])<<'\t'<<area(polygon_iterator)<< " (diff=" << (area(v[0]) - area(polygon_iterator)) << ")" << "\n"; - } - } - temp++; - } - meanV = meanV/area(measureArea); - density = density/(area(measureArea)*CMtoM*CMtoM); - return std::make_tuple(density, meanV); - } -// and velocity is calculated for every frame - void Method_I::GetProfiles(const string& frameId, const vector<polygon_2d>& polygons, const vector<double>& velocity) - { - std::string voroLocation(VORO_LOCATION); - fs::path tmp("field"); - fs::path vtmp ("velocity"); - fs::path dtmp("density"); - tmp = _outputLocation / voroLocation / tmp; - vtmp = tmp / vtmp / ("Prf_v_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"); - dtmp = tmp / dtmp / ("Prf_d_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"); - //string voronoiLocation=_outputLocation.string() + voroLocation+"field/"; - - // string Prfvelocity=voronoiLocation+"/velocity/Prf_v_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"; - // string Prfdensity=voronoiLocation+"/density/Prf_d_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"; - string Prfvelocity = vtmp.string(); - string Prfdensity = dtmp.string(); - - FILE *Prf_velocity; - if((Prf_velocity=Analysis::CreateFile(Prfvelocity))==nullptr) { - Log->Write("cannot open the file <%s> to write the field data\n",Prfvelocity.c_str()); - exit(EXIT_FAILURE); - } - FILE *Prf_density; - if((Prf_density=Analysis::CreateFile(Prfdensity))==nullptr) { - Log->Write("cannot open the file to write the field density\n"); - exit(EXIT_FAILURE); - } - - int NRow = (int)ceil((_geoMaxY-_geoMinY)/_grid_size_Y); // the number of rows that the geometry will be discretized for field analysis - int NColumn = (int)ceil((_geoMaxX-_geoMinX)/_grid_size_X); //the number of columns that the geometry will be discretized for field analysis - for(int row_i=0; row_i<NRow; row_i++) { // - for(int colum_j=0; colum_j<NColumn; colum_j++) { - polygon_2d measurezoneXY; - { - const double coor[][2] = { - {_geoMinX+colum_j*_grid_size_X,_geoMaxY-row_i*_grid_size_Y}, {_geoMinX+colum_j*_grid_size_X+_grid_size_X,_geoMaxY-row_i*_grid_size_Y}, {_geoMinX+colum_j*_grid_size_X+_grid_size_X, _geoMaxY-row_i*_grid_size_Y-_grid_size_Y}, - {_geoMinX+colum_j*_grid_size_X, _geoMaxY-row_i*_grid_size_Y-_grid_size_Y}, - {_geoMinX+colum_j*_grid_size_X,_geoMaxY-row_i*_grid_size_Y} // closing point is opening point - }; - assign_points(measurezoneXY, coor); - } - correct(measurezoneXY); // Polygons should be closed, and directed clockwise. If you're not sure if that is the case, call this function - double densityXY; - double velocityXY; - std::tie(densityXY, velocityXY) = GetVoronoiDensityVelocity(polygons,velocity,measurezoneXY); - fprintf(Prf_density,"%.3f\t",densityXY); - fprintf(Prf_velocity,"%.3f\t",velocityXY); - } - fprintf(Prf_density,"\n"); - fprintf(Prf_velocity,"\n"); - } - fclose(Prf_velocity); - fclose(Prf_density); - } - - void Method_I::OutputVoroGraph(const string & frameId, std::vector<std::pair<polygon_2d, int> >& polygons_id, int numPedsInFrame,const vector<double>& VInFrame) - { - //string voronoiLocation=_projectRootDir+"./Output/Fundamental_Diagram/Classical_Voronoi/VoronoiCell/id_"+_measureAreaId; - - fs::path voroLocPath(_outputLocation); - fs::path voro_location_path (VORO_LOCATION); // TODO: convert - // this MACRO to - // path. Maybe - // remove the MACRO? - voroLocPath = voroLocPath / voro_location_path / "VoronoiCell"; - polygon_2d poly; - if(!fs::exists(voroLocPath)) - { - if(!fs::create_directories(voroLocPath)) - { - Log->Write("ERROR:\tcan not create directory <%s>", voroLocPath.string().c_str()); - std::cout << "can not create directory "<< voroLocPath.string().c_str() << "\n"; - exit(EXIT_FAILURE); - } - else - std::cout << "create directory "<< voroLocPath.string().c_str() << "\n"; - } - - fs::path polygonPath=voroLocPath / "polygon"; - if(!fs::exists(polygonPath)) - { - if(!fs::create_directory(polygonPath)) - { - Log->Write("ERROR:\tcan not create directory <%s>", polygonPath.string().c_str()); - exit(EXIT_FAILURE); - } - } - fs::path trajFileName(_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"); - fs::path p = polygonPath / trajFileName; - string polygon = p.string(); - ofstream polys (polygon.c_str()); - - if(polys.is_open()) - { - //for(vector<polygon_2d> polygon_iterator=polygons.begin(); polygon_iterator!=polygons.end(); polygon_iterator++) - for(auto && polygon_id:polygons_id) - { - poly = polygon_id.first; - for(auto&& point:poly.outer()) - { - point.x(point.x()*CMtoM); - point.y(point.y()*CMtoM); - } - for(auto&& innerpoly:poly.inners()) - { - for(auto&& point:innerpoly) - { - point.x(point.x()*CMtoM); - point.y(point.y()*CMtoM); - } - } - polys << polygon_id.second << " | " << dsv(poly) << endl; - //polys <<dsv(poly)<< endl; - } - } - else - { - Log->Write("ERROR:\tcannot create the file <%s>",polygon.c_str()); - exit(EXIT_FAILURE); - } - fs::path speedPath=voroLocPath / "speed"; - if(!fs::exists(speedPath)) - if(!fs::create_directory(speedPath)) - { - Log->Write("ERROR:\tcan not create directory <%s>", speedPath.string().c_str()); - exit(EXIT_FAILURE); - } - fs::path pv = speedPath /trajFileName; - string v_individual= pv.string(); - ofstream velo (v_individual.c_str()); - if(velo.is_open()) - { - for(int pts=0; pts<numPedsInFrame; pts++) - { - velo << fabs(VInFrame[pts]) << endl; - } - } - else - { - Log->Write("ERROR:\tcannot create the file <%s>",pv.string().c_str()); - exit(EXIT_FAILURE); - } - - /*string point=voronoiLocation+"/points"+_trajName+"_id_"+_measureAreaId+"_"+frameId+".dat"; - ofstream points (point.c_str()); - if( points.is_open()) - { - for(int pts=0; pts<numPedsInFrame; pts++) - { - points << XInFrame[pts]*CMtoM << "\t" << YInFrame[pts]*CMtoM << endl; - } - } - else - { - Log->Write("ERROR:\tcannot create the file <%s>",point.c_str()); - exit(EXIT_FAILURE); - }*/ - - if(_plotVoronoiCellData) - { - //@todo: use fs::path - string parameters_rho=" " + _scriptsLocation.string()+"/_Plot_cell_rho.py -f \""+ voroLocPath.string() + "\" -n "+ _trajName.string()+"_id_"+_measureAreaId+"_"+frameId+ - " -g "+_geometryFileName.string()+" -p "+_trajectoryPath.string(); - string parameters_v=" " + _scriptsLocation.string()+"/_Plot_cell_v.py -f \""+ voroLocPath.string() + "\" -n "+ _trajName.string() + "_id_"+_measureAreaId+"_"+frameId+ - " -g "+_geometryFileName.string()+" -p "+_trajectoryPath.string(); - - if(_plotVoronoiIndex) - parameters_rho += " -i"; - - Log->Write("INFO:\t%s",parameters_rho.c_str()); - Log->Write("INFO:\tPlotting Voronoi Cell at the frame <%s>",frameId.c_str()); - parameters_rho = PYTHON + parameters_rho; - parameters_v = PYTHON + parameters_v; - system(parameters_rho.c_str()); - system(parameters_v.c_str()); - } - //points.close(); - polys.close(); - velo.close(); - } - - - void Method_I::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<double>& Velocity, const vector<int>& Id, const string& frid) - { - double uniquedensity=0; - double uniquevelocity=0; - int uniqueId=0; - int temp=0; - for (const auto & polygon_iterator:polygon) - { - string polygon_str = polygon_to_string(polygon_iterator); - uniquedensity=1.0/(area(polygon_iterator)*CMtoM*CMtoM); - uniquevelocity=Velocity[temp]; - uniqueId=Id[temp]; - fprintf(_fIndividualFD,"%s\t%d\t%.3f\t%.3f\t%s\n", - frid.c_str(), - uniqueId, - uniquedensity, - uniquevelocity, - polygon_str.c_str() - ); - temp++; - } - } +// ToDo: this function GetVoronoiDensityVelocity obsolete - can be deleted + +// std::tuple<double,double> Method_I::GetVoronoiDensityVelocity(const vector<polygon_2d>& polygon, const vector<double>& Velocity, const polygon_2d & measureArea) +// { +// double meanV=0; +// double density=0; +// int temp=0; +// for (auto && polygon_iterator:polygon) +// { +// polygon_list v; +// intersection(measureArea, polygon_iterator, v); +// if(!v.empty()) +// { +// meanV+=Velocity[temp]*area(v[0]); +// density+=area(v[0])/area(polygon_iterator); +// if((area(v[0]) - area(polygon_iterator))>J_EPS) +// { +// std::cout<<"----------------------Now calculating density-velocity!!!-----------------\n "; +// std::cout<<"measure area: \t"<<std::setprecision(16)<<dsv(measureArea)<<"\n"; +// std::cout<<"Original polygon:\t"<<std::setprecision(16)<<dsv(polygon_iterator)<<"\n"; +// std::cout<<"intersected polygon: \t"<<std::setprecision(16)<<dsv(v[0])<<"\n"; +// std::cout<<"this is a wrong result in density calculation\t "<<area(v[0])<<'\t'<<area(polygon_iterator)<< " (diff=" << (area(v[0]) - area(polygon_iterator)) << ")" << "\n"; +// } +// } +// temp++; +// } +// meanV = meanV/area(measureArea); +// density = density/(area(measureArea)*CMtoM*CMtoM); +// return std::make_tuple(density, meanV); +// } + +// ToDo: this function GetProfiles is obsolete - can be deleted +// and velocity is calculated for every frame +// void Method_I::GetProfiles(const string& frameId, const vector<polygon_2d>& polygons, const vector<double>& velocity) +// { +// std::string voroLocation(VORO_LOCATION); +// fs::path tmp("field"); +// fs::path vtmp ("velocity"); +// fs::path dtmp("density"); +// tmp = _outputLocation / voroLocation / tmp; +// vtmp = tmp / vtmp / ("Prf_v_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"); +// dtmp = tmp / dtmp / ("Prf_d_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"); +// //string voronoiLocation=_outputLocation.string() + voroLocation+"field/"; +// +// // string Prfvelocity=voronoiLocation+"/velocity/Prf_v_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"; +// // string Prfdensity=voronoiLocation+"/density/Prf_d_"+_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"; +// string Prfvelocity = vtmp.string(); +// string Prfdensity = dtmp.string(); +// +// FILE *Prf_velocity; +// if((Prf_velocity=Analysis::CreateFile(Prfvelocity))==nullptr) { +// Log->Write("cannot open the file <%s> to write the field data\n",Prfvelocity.c_str()); +// exit(EXIT_FAILURE); +// } +// FILE *Prf_density; +// if((Prf_density=Analysis::CreateFile(Prfdensity))==nullptr) { +// Log->Write("cannot open the file to write the field density\n"); +// exit(EXIT_FAILURE); +// } +// +// int NRow = (int)ceil((_geoMaxY-_geoMinY)/_grid_size_Y); // the number of rows that the geometry will be discretized for field analysis +// int NColumn = (int)ceil((_geoMaxX-_geoMinX)/_grid_size_X); //the number of columns that the geometry will be discretized for field analysis +// for(int row_i=0; row_i<NRow; row_i++) { // +// for(int colum_j=0; colum_j<NColumn; colum_j++) { +// polygon_2d measurezoneXY; +// { +// const double coor[][2] = { +// {_geoMinX+colum_j*_grid_size_X,_geoMaxY-row_i*_grid_size_Y}, {_geoMinX+colum_j*_grid_size_X+_grid_size_X,_geoMaxY-row_i*_grid_size_Y}, {_geoMinX+colum_j*_grid_size_X+_grid_size_X, _geoMaxY-row_i*_grid_size_Y-_grid_size_Y}, +// {_geoMinX+colum_j*_grid_size_X, _geoMaxY-row_i*_grid_size_Y-_grid_size_Y}, +// {_geoMinX+colum_j*_grid_size_X,_geoMaxY-row_i*_grid_size_Y} // closing point is opening point +// }; +// assign_points(measurezoneXY, coor); +// } +// correct(measurezoneXY); // Polygons should be closed, and directed clockwise. If you're not sure if that is the case, call this function +// +// double densityXY; +// double velocityXY; +// std::tie(densityXY, velocityXY) = GetVoronoiDensityVelocity(polygons,velocity,measurezoneXY); +// fprintf(Prf_density,"%.3f\t",densityXY); +// fprintf(Prf_velocity,"%.3f\t",velocityXY); +// } +// fprintf(Prf_density,"\n"); +// fprintf(Prf_velocity,"\n"); +// } +// fclose(Prf_velocity); +// fclose(Prf_density); +// } + +// ToDo: this function OutputVoroGraph is obsolete - can be deleted + +// void Method_I::OutputVoroGraph(const string & frameId, std::vector<std::pair<polygon_2d, int> >& polygons_id, int numPedsInFrame,const vector<double>& VInFrame) +// { +// //string voronoiLocation=_projectRootDir+"./Output/Fundamental_Diagram/Classical_Voronoi/VoronoiCell/id_"+_measureAreaId; +// +// fs::path voroLocPath(_outputLocation); +// fs::path voro_location_path (VORO_LOCATION); // +// // this MACRO to +// // path. Maybe +// // remove the MACRO? +// voroLocPath = voroLocPath / voro_location_path / "VoronoiCell"; +// polygon_2d poly; +// if(!fs::exists(voroLocPath)) +// { +// if(!fs::create_directories(voroLocPath)) +// { +// Log->Write("ERROR:\tcan not create directory <%s>", voroLocPath.string().c_str()); +// std::cout << "can not create directory "<< voroLocPath.string().c_str() << "\n"; +// exit(EXIT_FAILURE); +// } +// else +// std::cout << "create directory "<< voroLocPath.string().c_str() << "\n"; +// } +// +// fs::path polygonPath=voroLocPath / "polygon"; +// if(!fs::exists(polygonPath)) +// { +// if(!fs::create_directory(polygonPath)) +// { +// Log->Write("ERROR:\tcan not create directory <%s>", polygonPath.string().c_str()); +// exit(EXIT_FAILURE); +// } +// } +// fs::path trajFileName(_trajName.string()+"_id_"+_measureAreaId+"_"+frameId+".dat"); +// fs::path p = polygonPath / trajFileName; +// string polygon = p.string(); +// ofstream polys (polygon.c_str()); +// +// if(polys.is_open()) +// { +// //for(vector<polygon_2d> polygon_iterator=polygons.begin(); polygon_iterator!=polygons.end(); polygon_iterator++) +// for(auto && polygon_id:polygons_id) +// { +// poly = polygon_id.first; +// for(auto&& point:poly.outer()) +// { +// point.x(point.x()*CMtoM); +// point.y(point.y()*CMtoM); +// } +// for(auto&& innerpoly:poly.inners()) +// { +// for(auto&& point:innerpoly) +// { +// point.x(point.x()*CMtoM); +// point.y(point.y()*CMtoM); +// } +// } +// polys << polygon_id.second << " | " << dsv(poly) << endl; +// //polys <<dsv(poly)<< endl; +// } +// } +// else +// { +// Log->Write("ERROR:\tcannot create the file <%s>",polygon.c_str()); +// exit(EXIT_FAILURE); +// } +// fs::path speedPath=voroLocPath / "speed"; +// if(!fs::exists(speedPath)) +// if(!fs::create_directory(speedPath)) +// { +// Log->Write("ERROR:\tcan not create directory <%s>", speedPath.string().c_str()); +// exit(EXIT_FAILURE); +// } +// fs::path pv = speedPath /trajFileName; +// string v_individual= pv.string(); +// ofstream velo (v_individual.c_str()); +// if(velo.is_open()) +// { +// for(int pts=0; pts<numPedsInFrame; pts++) +// { +// velo << fabs(VInFrame[pts]) << endl; +// } +// } +// else +// { +// Log->Write("ERROR:\tcannot create the file <%s>",pv.string().c_str()); +// exit(EXIT_FAILURE); +// } +// +// *//*string point=voronoiLocation+"/points"+_trajName+"_id_"+_measureAreaId+"_"+frameId+".dat"; +// ofstream points (point.c_str()); +// if( points.is_open()) +// { +// for(int pts=0; pts<numPedsInFrame; pts++) +// { +// points << XInFrame[pts]*CMtoM << "\t" << YInFrame[pts]*CMtoM << endl; +// } +// } +// else +// { +// Log->Write("ERROR:\tcannot create the file <%s>",point.c_str()); +// exit(EXIT_FAILURE); +// } +// +// if(_plotVoronoiCellData) +// { +// string parameters_rho=" " + _scriptsLocation.string()+"/_Plot_cell_rho.py -f \""+ voroLocPath.string() + "\" -n "+ _trajName.string()+"_id_"+_measureAreaId+"_"+frameId+ +// " -g "+_geometryFileName.string()+" -p "+_trajectoryPath.string(); +// string parameters_v=" " + _scriptsLocation.string()+"/_Plot_cell_v.py -f \""+ voroLocPath.string() + "\" -n "+ _trajName.string() + "_id_"+_measureAreaId+"_"+frameId+ +// " -g "+_geometryFileName.string()+" -p "+_trajectoryPath.string(); +// +// if(_plotVoronoiIndex) +// parameters_rho += " -i"; +// +// Log->Write("INFO:\t%s",parameters_rho.c_str()); +// Log->Write("INFO:\tPlotting Voronoi Cell at the frame <%s>",frameId.c_str()); +// parameters_rho = PYTHON + parameters_rho; +// parameters_v = PYTHON + parameters_v; +// system(parameters_rho.c_str()); +// system(parameters_v.c_str()); +// } +// //points.close(); +// polys.close(); +// velo.close(); +// } + + +// ToDo: this function GetIndividualFD is obsolete - can be deleted + +// void Method_I::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<double>& Velocity, const vector<int>& Id, const string& frid) +// { +// double uniquedensity=0; +// double uniquevelocity=0; +// int uniqueId=0; +// int temp=0; +// for (const auto & polygon_iterator:polygon) +// { +// string polygon_str = polygon_to_string(polygon_iterator); +// uniquedensity=1.0/(area(polygon_iterator)*CMtoM*CMtoM); +// uniquevelocity=Velocity[temp]; +// uniqueId=Id[temp]; +// fprintf(_fIndividualFD,"%s\t%d\t%.3f\t%.3f\t%s\n", +// frid.c_str(), +// uniqueId, +// uniquedensity, +// uniquevelocity, +// polygon_str.c_str() +// ); +// temp++; +// } +// } void Method_I::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<double>& Velocity, const vector<int>& Id, const string& frid, vector<double>& XInFrame, vector<double>& YInFrame, vector<double>& ZInFrame) { @@ -586,7 +603,7 @@ void Method_I::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<d void Method_I::SetCalculateIndividualFD(bool individualFD) { - _calcIndividualFD = individualFD; + _calcIndividualFD = true ; } void Method_I::SetStartFrame(int startFrame) @@ -635,20 +652,25 @@ void Method_I::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<d _grid_size_Y = y; } - void Method_I::SetCalculateProfiles(bool calcProfile) - { - _getProfile =calcProfile; - } + +// ToDo: obsolete ? + +// void Method_I::SetCalculateProfiles(bool calcProfile) +// { +// _getProfile = false ; +// } void Method_I::SetOutputVoronoiCellData(bool outputCellData) { _outputVoronoiCellData = outputCellData; } - void Method_I::SetPlotVoronoiGraph(bool plotVoronoiGraph) - { - _plotVoronoiCellData = plotVoronoiGraph; - } +// ToDo: obsolete ? +// void Method_I::SetPlotVoronoiGraph(bool plotVoronoiGraph) +// { +// _plotVoronoiCellData = plotVoronoiGraph; +// } + void Method_I::SetPlotVoronoiIndex(bool plotVoronoiIndex) { _plotVoronoiIndex = plotVoronoiIndex; @@ -673,19 +695,21 @@ void Method_I::GetIndividualFD(const vector<polygon_2d>& polygon, const vector<d } } - bool Method_I::IsPedInGeometry(int frames, int peds, double **Xcor, double **Ycor, int *firstFrame, int *lastFrame) - { - for(int i=0; i<peds; i++) - for(int j =0; j<frames; j++) - { - if (j>firstFrame[i] && j< lastFrame[i] && (false==within(point_2d(round(Xcor[i][j]), round(Ycor[i][j])), _geoPoly))) - { - Log->Write("Error:\tPedestrian at the position <x=%.4f, y=%.4f> is outside geometry. Please check the geometry or trajectory file!", Xcor[i][j]*CMtoM, Ycor[i][j]*CMtoM ); - return false; - } - } - return true; - } +// ToDo: obsolete ? + +// bool Method_I::IsPedInGeometry(int frames, int peds, double **Xcor, double **Ycor, int *firstFrame, int *lastFrame) +// { +// for(int i=0; i<peds; i++) +// for(int j =0; j<frames; j++) +// { +// if (j>firstFrame[i] && j< lastFrame[i] && (false==within(point_2d(round(Xcor[i][j]), round(Ycor[i][j])), _geoPoly))) +// { +// Log->Write("Error:\tPedestrian at the position <x=%.4f, y=%.4f> is outside geometry. Please check the geometry or trajectory file!", Xcor[i][j]*CMtoM, Ycor[i][j]*CMtoM ); +// return false; +// } +// } +// return true; +// } void Method_I::CalcVoronoiResults1D(vector<double>& XInFrame, vector<double>& VInFrame, vector<int>& IdInFrame, const polygon_2d & measureArea,const string& frid) { diff --git a/methods/Method_I.h b/methods/Method_I.h index 01ec100843c7d3e951eede75814b31a83eaf87a0..71e57f2d51bd9a8c005a666640d57c3eb560f0cc 100644 --- a/methods/Method_I.h +++ b/methods/Method_I.h @@ -51,9 +51,9 @@ public: void SetGeometryFileName(const fs::path& geometryFile); void SetGeometryBoundaries(double minX, double minY, double maxX, double maxY); void SetGridSize(double x, double y); - void SetCalculateProfiles(bool calcProfile); + void SetCalculateProfiles(bool calcProfile); // ToDo: obsolete ? void SetOutputVoronoiCellData(bool outputCellData); - void SetPlotVoronoiGraph(bool plotVoronoiGraph); + void SetPlotVoronoiGraph(bool plotVoronoiGraph); // ToDo: obsolete ? void SetPlotVoronoiIndex(bool plotVoronoiIndex); void SetMeasurementArea (MeasurementArea_B* area); void SetDimensional (bool dimension); @@ -70,11 +70,11 @@ private: fs::path _outputLocation; fs::path _scriptsLocation; bool _calcIndividualFD; - polygon_2d _areaIndividualFD; - bool _getProfile; - bool _outputVoronoiCellData; - bool _plotVoronoiCellData; - bool _plotVoronoiIndex; + polygon_2d _areaIndividualFD; // ToDo: obsolete ? + bool _getProfile; // ToDo: obsolete ? + bool _outputVoronoiCellData; // ToDo: obsolete ? + bool _plotVoronoiCellData; // ToDo: obsolete ? + bool _plotVoronoiIndex; // ToDo: obsolete ? bool _isOneDimensional; bool _cutByCircle; //Adjust whether cut each original voronoi cell by a circle double _cutRadius; @@ -86,10 +86,10 @@ private: double _geoMaxY; FILE* _fVoronoiRhoV; FILE* _fIndividualFD; - double _grid_size_X; // the size of the grid - double _grid_size_Y; + double _grid_size_X; // the size of the grid // ToDo: obsolete ? + double _grid_size_Y; // ToDo: obsolete ? float _fps; - bool OpenFileMethodI(); + bool OpenFileMethodI(); // ToDo: obsolete ? bool OpenFileIndividualFD(); fs::path _geometryFileName; fs::path _trajectoryPath; @@ -99,11 +99,13 @@ private: std::vector<std::pair<polygon_2d, int> > GetPolygons(std::vector<double>& XInFrame, std::vector<double>& YInFrame, std::vector<double>& VInFrame, std::vector<int>& IdInFrame); + // ToDo: This functions are obsolete. void OutputVoronoiResults(const std::vector<polygon_2d>& polygons, const std::string& frid, const std::vector<double>& VInFrame); std::tuple<double,double> GetVoronoiDensityVelocity(const std::vector<polygon_2d>& polygon, const std::vector<double>& Velocity, const polygon_2d & measureArea); void GetProfiles(const std::string& frameId, const std::vector<polygon_2d>& polygons, const std::vector<double>& velocity); void OutputVoroGraph(const std::string & frameId, std::vector<std::pair<polygon_2d, int> >& polygons, int numPedsInFrame,const std::vector<double>& VInFrame); void GetIndividualFD(const std::vector<polygon_2d>& polygon, const std::vector<double>& Velocity, const std::vector<int>& Id, const std::string& frid); + void GetIndividualFD(const std::vector<polygon_2d>& polygon, const std::vector<double>& Velocity, const std::vector<int>& Id, const std::string& frid, std::vector<double>& XInFrame, std::vector<double>& YInFrame, std::vector<double>& ZInFrame); /** * Reduce the precision of the points to two digits @@ -111,7 +113,10 @@ private: */ void CalcVoronoiResults1D(std::vector<double>& XInFrame, std::vector<double>& VInFrame, std::vector<int>& IdInFrame, const polygon_2d & measureArea, const std::string& frid); void ReducePrecision(polygon_2d& polygon); + + // ToDo: This function is obsolete. bool IsPedInGeometry(int frames, int peds, double **Xcor, double **Ycor, int *firstFrame, int *lastFrame); //check whether all the pedestrians are in the geometry + double getOverlapRatio(const double& left, const double& right, const double& measurearea_left, const double& measurearea_right); bool IsPointsOnOneLine(std::vector<double>& XInFrame, std::vector<double>& YInFrame); };