-
Notifications
You must be signed in to change notification settings - Fork 5
/
ply.cpp
152 lines (139 loc) · 4.16 KB
/
ply.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/******************************************************/
/* */
/* ply.cpp - polygon files */
/* */
/******************************************************/
/* Copyright 2019-2021 Pierre Abbat.
* This file is part of PerfectTIN.
*
* PerfectTIN is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* PerfectTIN 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 and Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and Lesser General Public License along with PerfectTIN. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#undef VERSION
#undef COPY_YEAR
#ifdef Plytapus_FOUND
#include <plytapus.h>
#endif
#include "ply.h"
#include "cloud.h"
#include "adjelev.h"
#include "octagon.h"
#include "color.h"
using namespace std;
#ifdef Plytapus_FOUND
using namespace plytapus;
vector<triangle *> trianglesToWrite;
double plyUnit;
xyz plyOffset;
bool centerPlyOut=false;
string plytapusVersion()
{
return version();
}
int plytapusYear()
{
return copyrightYear();
}
void receivePoint(ElementBuffer &buf)
{
if (buf.size()>=3)
{
xyz pnt(buf[0],buf[1],buf[2]);
cloud.push_back(pnt);
}
}
void transmitPoint(ElementBuffer &buf,size_t i)
{
xyz pnt=net.points[i+1]-plyOffset;
Color color=colorize(&net.points[i+1]);
buf[0]=pnt.getx()/plyUnit;
buf[1]=pnt.gety()/plyUnit;
buf[2]=pnt.getz()/plyUnit;
buf[3]=color.br();
buf[4]=color.bg();
buf[5]=color.bb();
}
void transmitTriangle(ElementBuffer &buf,size_t i)
{
triangle *tri=trianglesToWrite[i];
buf.reset(3);
buf[0]=net.revpoints[tri->a]-1;
buf[1]=net.revpoints[tri->b]-1;
buf[2]=net.revpoints[tri->c]-1;
}
void readPly(string fileName)
{
try
{
File plyfile(fileName);
ElementReadCallback pointCallback=receivePoint;
plyfile.setElementReadCallback("vertex",pointCallback);
plyfile.read();
}
catch (...)
{
}
}
void writePly(string filename,bool asc,double outUnit,int flags)
{
vector<Property> vertexProperties,faceProperties;
vertexProperties.push_back(Property("x",Type::DOUBLE,false));
vertexProperties.push_back(Property("y",Type::DOUBLE,false));
vertexProperties.push_back(Property("z",Type::DOUBLE,false));
vertexProperties.push_back(Property("red",Type::UCHAR,false));
vertexProperties.push_back(Property("green",Type::UCHAR,false));
vertexProperties.push_back(Property("blue",Type::UCHAR,false));
faceProperties.push_back(Property("vertex_index",Type::INT,true));
int i;
plyOffset=xyz(0,0,0);
if (centerPlyOut) // This is to get around a bug in MeshLab
for (i=1;i<=8;i++) // where files are read in single precision.
plyOffset+=net.points[i];
plyOffset/=8;
trianglesToWrite.clear();
plyUnit=outUnit;
for (i=0;i<net.triangles.size();i++)
if (net.triangles[i].ptValid())
if (net.shouldWrite(i,flags,false))
trianglesToWrite.push_back(&net.triangles[i]);
else;
else
cerr<<"Invalid triangle "<<i<<endl;
Element vertexElement("vertex",net.points.size(),vertexProperties);
Element faceElement("face",trianglesToWrite.size(),faceProperties);
FileOut plyfile(filename,asc?File::Format::ASCII:File::Format::BINARY_LITTLE_ENDIAN);
ElementWriteCallback pointCallback=transmitPoint;
ElementWriteCallback triangleCallback=transmitTriangle;
ElementsDefinition elements;
elements.push_back(vertexElement);
elements.push_back(faceElement);
plyfile.setElementsDefinition(elements);
plyfile.setElementWriteCallback("vertex",pointCallback);
plyfile.setElementWriteCallback("face",triangleCallback);
plyfile.write();
}
#else
string plytapusVersion()
{
return "";
}
void readPly(string fileName)
{
}
void writePly(string fileName,bool asc,double outUnit,int flags)
{
}
#endif