~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Mod/Points/App/PointsAlgos.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) Juergen Riegel         <juergen.riegel@web.de>          *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This library is free software; you can redistribute it and/or         *
 
7
 *   modify it under the terms of the GNU Library General Public           *
 
8
 *   License as published by the Free Software Foundation; either          *
 
9
 *   version 2 of the License, or (at your option) any later version.      *
 
10
 *                                                                         *
 
11
 *   This library  is distributed in the hope that it will be useful,      *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU Library General Public License for more details.                  *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU Library General Public     *
 
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
 
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
#ifndef _PreComp_
 
26
#ifdef FC_OS_LINUX
 
27
# include <unistd.h>
 
28
#endif
 
29
# include <sstream>
 
30
#endif
 
31
 
 
32
 
 
33
#include "PointsAlgos.h"
 
34
#include "Points.h"
 
35
 
 
36
#include <Base/Exception.h>
 
37
#include <Base/FileInfo.h>
 
38
#include <Base/Console.h>
 
39
#include <Base/Sequencer.h>
 
40
#include <Base/Stream.h>
 
41
 
 
42
#include <boost/regex.hpp>
 
43
 
 
44
using namespace Points;
 
45
 
 
46
void PointsAlgos::Load(PointKernel &points, const char *FileName)
 
47
{
 
48
    Base::FileInfo File(FileName);
 
49
 
 
50
    // checking on the file
 
51
    if (!File.isReadable())
 
52
        throw Base::Exception("File to load not existing or not readable");
 
53
 
 
54
    if (File.extension() == "asc" ||File.extension() == "ASC")
 
55
        LoadAscii(points,FileName);
 
56
    else
 
57
        throw Base::Exception("Unknown ending");
 
58
}
 
59
 
 
60
void PointsAlgos::LoadAscii(PointKernel &points, const char *FileName)
 
61
{
 
62
    boost::regex rx("^\\s*([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
 
63
                     "\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
 
64
                     "\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)\\s*$");
 
65
    //boost::regex rx("(\\b[0-9]+\\.([0-9]+\\b)?|\\.[0-9]+\\b)");
 
66
    //boost::regex rx("^\\s*(-?[0-9]*)\\.([0-9]+)\\s+(-?[0-9]*)\\.([0-9]+)\\s+(-?[0-9]*)\\.([0-9]+)\\s*$");
 
67
    boost::cmatch what;
 
68
 
 
69
    Base::Vector3d pt;
 
70
    int LineCnt=0;
 
71
    std::string line;
 
72
    Base::FileInfo fi(FileName);
 
73
 
 
74
    Base::ifstream tmp_str(fi, std::ios::in);
 
75
 
 
76
    // estimating size
 
77
    while (std::getline(tmp_str,line))
 
78
        LineCnt++;
 
79
 
 
80
    // resize the PointKernel
 
81
    points.resize(LineCnt);
 
82
 
 
83
    Base::SequencerLauncher seq( "Loading points...", LineCnt );
 
84
 
 
85
    // again to the beginning
 
86
    Base::ifstream file(fi, std::ios::in);
 
87
    LineCnt = 0;
 
88
 
 
89
    try {
 
90
        // read file
 
91
        while (std::getline(file, line)) {
 
92
            if (boost::regex_match(line.c_str(), what, rx)) {
 
93
                pt.x = std::atof(what[1].first);
 
94
                pt.y = std::atof(what[4].first);
 
95
                pt.z = std::atof(what[7].first);
 
96
 
 
97
                points.setPoint(LineCnt,pt);
 
98
                seq.next();
 
99
                LineCnt++;
 
100
            }
 
101
        }
 
102
    }
 
103
    catch (...) {
 
104
        points.clear();
 
105
        throw Base::Exception("Reading in points failed.");
 
106
    }
 
107
 
 
108
    // now remove the last points from the kernel
 
109
    // Note: first we allocate memory corresponding to the number of lines (points and comments)
 
110
    //       and read in the file twice. But then the size of the kernel is too high
 
111
    if (LineCnt < (int)points.size())
 
112
        points.erase(LineCnt, points.size());
 
113
}