PointsAlgos.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 #ifdef FC_OS_LINUX
00027 # include <unistd.h>
00028 #endif
00029 # include <sstream>
00030 #endif
00031
00032
00033 #include "PointsAlgos.h"
00034 #include "Points.h"
00035
00036 #include <Base/Exception.h>
00037 #include <Base/FileInfo.h>
00038 #include <Base/Console.h>
00039 #include <Base/Sequencer.h>
00040 #include <Base/Stream.h>
00041
00042 #include <boost/regex.hpp>
00043
00044 using namespace Points;
00045
00046 void PointsAlgos::Load(PointKernel &points, const char *FileName)
00047 {
00048 Base::FileInfo File(FileName);
00049
00050
00051 if (!File.isReadable())
00052 throw Base::FileException("File to load not existing or not readable", FileName);
00053
00054 if (File.extension() == "asc" ||File.extension() == "ASC")
00055 LoadAscii(points,FileName);
00056 else
00057 throw Base::Exception("Unknown ending");
00058 }
00059
00060 void PointsAlgos::LoadAscii(PointKernel &points, const char *FileName)
00061 {
00062 boost::regex rx("^\\s*([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
00063 "\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
00064 "\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)\\s*$");
00065
00066
00067 boost::cmatch what;
00068
00069 Base::Vector3d pt;
00070 int LineCnt=0;
00071 std::string line;
00072 Base::FileInfo fi(FileName);
00073
00074 Base::ifstream tmp_str(fi, std::ios::in);
00075
00076
00077 while (std::getline(tmp_str,line))
00078 LineCnt++;
00079
00080
00081 points.resize(LineCnt);
00082
00083 Base::SequencerLauncher seq( "Loading points...", LineCnt );
00084
00085
00086 Base::ifstream file(fi, std::ios::in);
00087 LineCnt = 0;
00088
00089 try {
00090
00091 while (std::getline(file, line)) {
00092 if (boost::regex_match(line.c_str(), what, rx)) {
00093 pt.x = std::atof(what[1].first);
00094 pt.y = std::atof(what[4].first);
00095 pt.z = std::atof(what[7].first);
00096
00097 points.setPoint(LineCnt,pt);
00098 seq.next();
00099 LineCnt++;
00100 }
00101 }
00102 }
00103 catch (...) {
00104 points.clear();
00105 throw Base::Exception("Reading in points failed.");
00106 }
00107
00108
00109
00110
00111 if (LineCnt < (int)points.size())
00112 points.erase(LineCnt, points.size());
00113 }