PointsAlgos.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Juergen Riegel         <juergen.riegel@web.de>          *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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     // checking on the file
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     //boost::regex rx("(\\b[0-9]+\\.([0-9]+\\b)?|\\.[0-9]+\\b)");
00066     //boost::regex rx("^\\s*(-?[0-9]*)\\.([0-9]+)\\s+(-?[0-9]*)\\.([0-9]+)\\s+(-?[0-9]*)\\.([0-9]+)\\s*$");
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     // estimating size
00077     while (std::getline(tmp_str,line))
00078         LineCnt++;
00079 
00080     // resize the PointKernel
00081     points.resize(LineCnt);
00082 
00083     Base::SequencerLauncher seq( "Loading points...", LineCnt );
00084 
00085     // again to the beginning
00086     Base::ifstream file(fi, std::ios::in);
00087     LineCnt = 0;
00088 
00089     try {
00090         // read file
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     // now remove the last points from the kernel
00109     // Note: first we allocate memory corresponding to the number of lines (points and comments)
00110     //       and read in the file twice. But then the size of the kernel is too high
00111     if (LineCnt < (int)points.size())
00112         points.erase(LineCnt, points.size());
00113 }

Generated on Wed Nov 23 19:00:27 2011 for FreeCAD by  doxygen 1.6.1