-
Notifications
You must be signed in to change notification settings - Fork 2
/
Helpers.h
108 lines (101 loc) · 3.55 KB
/
Helpers.h
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include "Common.h"
//-----------------------------------------------------------------------------
template <typename RealPoint, typename RealVector>
std::pair< std::vector<RealPoint>, std::vector<RealVector> >
readOBJAsPointCloud( std::istream & input )
{
std::vector<RealPoint> vertices;
std::vector<RealVector> normals;
std::string linestr;
std::string keyword;
std::string indices;
RealPoint p;
RealVector n;
std::getline( input, linestr );
int l = 0;
for ( ; input.good() && ! input.eof(); std::getline( input, linestr ), l++ )
{
if ( linestr.empty() ) continue; // skip empty line
if ( linestr[0] == '#' ) continue; // skip comment line
std::istringstream lineinput( linestr );
std::operator>>( lineinput, keyword ); // lineinput >> keyword;
if ( keyword == "v" ) {
lineinput >> p[ 0 ] >> p[ 1 ] >> p[ 2 ];
vertices.push_back( p );
} else if ( keyword == "vn" ) {
lineinput >> n[ 0 ] >> n[ 1 ] >> n[ 2 ];
normals.push_back( n );
}
// Weird: necessary to clear them.
keyword = ""; linestr = "";
}
// Creating SurfaceMesh
std::cout << "[readOBJAsPointCloud] Read"
<< " #lines=" << l
<< " #V=" << vertices.size()
<< " #VN=" << normals.size()
<< std::endl;
if ( input.bad() )
std::cerr << "[readOBJAsPointCloud] Some I/O error occured."
<< " Proceeding but the point cloud may be damaged." << std::endl;
return std::make_pair( vertices, normals );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Read a file
template <typename RealPoint, typename RealVector>
std::pair< std::vector<RealPoint>, std::vector<RealVector> >
readPointCloud( std::istream & input )
{
std::vector<RealPoint> vertices;
std::vector<RealVector> normals;
std::string linestr;
std::string indices;
RealPoint p;
RealVector n;
std::getline( input, linestr );
int l = 0;
for ( ; input.good() && ! input.eof(); std::getline( input, linestr ), l++ )
{
if ( linestr.empty() ) continue; // skip empty line
if ( linestr[0] == '#' ) continue; // skip comment line
std::istringstream lineinput( linestr );
lineinput >> p[ 0 ] >> p[ 1 ] >> p[ 2 ];
vertices.push_back( p );
lineinput >> n[ 0 ] >> n[ 1 ] >> n[ 2 ];
normals.push_back( n );
linestr = "";
}
// Creating SurfaceMesh
std::cout << "[readPointCloud] Read"
<< " #lines=" << l
<< " #V=" << vertices.size()
<< " #VN=" << normals.size()
<< std::endl;
if ( input.bad() )
std::cerr << "[readPointCloud] Some I/O error occured."
<< " Proceeding but the point cloud may be damaged." << std::endl;
return std::make_pair( vertices, normals );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template <typename RealPoint>
std::vector<RealPoint> addUniformNoise( const std::vector<RealPoint> &input,
double eps)
{
//hardcoded noise
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(0.0, 1.0);
std::vector<RealPoint> points(input.size());
auto cpt=0u;
for(auto &p: input)
{
RealPoint dec = {eps*dis(gen),eps*dis(gen),eps*dis(gen)};
points[cpt] = p + dec;
cpt++;
}
return points;
}
//-----------------------------------------------------------------------------