-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxyzfile.cpp
93 lines (89 loc) · 2.42 KB
/
xyzfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/******************************************************/
/* */
/* xyzfile.cpp - XYZ point cloud files */
/* */
/******************************************************/
/* Copyright 2021 Pierre Abbat.
* This file is part of Wolkenbase.
*
* Wolkenbase is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wolkenbase is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wolkenbase. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cmath>
#include "xyzfile.h"
#include "cloud.h"
#include "ldecimal.h"
using namespace std;
/* There is no specification for XYZ point cloud files. They are plain text,
* with the first three fields being x, y, and z, but the fields after that
* could be anything, and the separator could be spaces, comma, or tab.
* There is also a chemical XYZ file format, in which the first field is
* a chemical element symbol (alphabetic, e.g. F or Ne).
*/
xyz parseXyz(string line)
{
double x=NAN,y=NAN,z=NAN;
size_t pos;
int i,ncomma;
vector<string> words;
while (line.length())
{
i=ncomma=0;
while (i<line.length())
{
if (line[i]==',' && ncomma)
break;
if (line[i]==',')
ncomma++;
if (line[i]!=',' && !isblank(line[i]))
break;
i++;
}
line.erase(0,i);
pos=line.find_first_of(" \t,");
words.push_back(line.substr(0,pos));
line.erase(0,pos);
}
if (words.size()>=3)
{
try
{
x=stod(words[0]);
y=stod(words[1]);
z=stod(words[2]);
}
catch (...)
{
x=y=z=NAN;
}
}
return xyz(x,y,z);
}
void readXyzText(string fname)
{
ifstream xyzfile(fname);
string line;
xyz pnt;
while (xyzfile)
{
getline(xyzfile,line);
pnt=parseXyz(line);
if (pnt.isnan())
break;
cloud.push_back(pnt);
}
}
void writeXyzTextDot(ofstream &file,xyz dot)
{
file<<ldecimal(dot.getx())<<' '<<ldecimal(dot.gety())<<' '<<ldecimal(dot.getz())<<'\n';
}