-
Notifications
You must be signed in to change notification settings - Fork 3
/
nxs.cpp
316 lines (261 loc) · 8.84 KB
/
nxs.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Nexus
Copyright(C) 2012 - Federico Ponchio
ISTI - Italian National Research Council - Visual Computing Lab
This program 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 2 of the License, or
(at your option) any later version.
This program 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 (http://www.gnu.org/licenses/gpl.txt)
for more details.
*/
#include "nxs.h"
#include <iostream>
#include <iomanip>
#include <QStringList>
#include <QtPlugin>
#include <wrap/system/qgetopt.h>
#include "meshstream.h"
#include "kdtree.h"
#include "../nxsbuild/nexusbuilder.h"
#include "../common/qtnexusfile.h"
#include "../common/traversal.h"
#include "../nxsedit/extractor.h"
#include "plyloader.h"
#include "objloader.h"
#include "tsploader.h"
using namespace std;
using namespace nx;
NXS_DLL NXSErr nexusBuild(const char *input, const char *output){
#ifdef INITIALIZE_STATIC_LIBJPEG
Q_IMPORT_PLUGIN(QJpegPlugin);
#endif
std::cout << "Nexus build: " << input << std::endl;
// we create a QCoreApplication just so that QT loads image IO plugins (for jpg and tiff loading)
int node_size = 1<<15;
float texel_weight =0.1; //relative weight of texels.
int top_node_size = 4096;
float vertex_quantization = 0.0f; //optionally quantize vertices position.
int tex_quality(95); //default jpg texture quality
int ram_buffer(2000); //Mb of ram to use
int n_threads = 4;
float scaling(0.5); //simplification ratio
int skiplevels = 0;
QString mtl;
QString translate;
// bool center = false;
bool point_cloud = false;
bool normals = false;
bool no_normals = false;
bool colors = false;
bool no_colors = false;
bool no_texcoords = false;
bool useOrigTex = false;
bool create_pow_two_tex = false;
bool deepzoom = false;
//BTREE options
float adaptive = 0.333f;
//Check parameters are correct
QStringList inputs;
inputs.append(input);
vcg::Point3d origin(0, 0, 0);
Stream *stream = 0;
KDTree *tree = 0;
NXSErr returncode = NXSERR_NONE;
try {
quint64 max_memory = (1<<20)*(uint64_t)ram_buffer/4; //hack 4 is actually an estimate...
string input = "mesh";
stream = new StreamSoup("cache_stream");
stream->setVertexQuantization(vertex_quantization);
stream->setMaxMemory(max_memory);
stream->origin = origin;
vcg::Point3d &o = stream->origin;
//TODO: actually the stream will store textures or normals or colors even if not needed
stream->load(inputs, mtl);
bool has_colors = stream->hasColors();
bool has_normals = stream->hasNormals();
bool has_textures = stream->hasTextures();
//cout << "Components: " << input;
//if(has_normals) cout << " normals";
//if(has_colors) cout << " colors";
//if(has_textures) cout << " textures";
//cout << "\n";
quint32 components = 0;
if(!point_cloud) components |= NexusBuilder::FACES;
if((!no_normals && (!point_cloud || has_normals)) || normals) {
components |= NexusBuilder::NORMALS;
//cout << "Normals enabled\n";
}
if((has_colors && !no_colors ) || colors ) {
components |= NexusBuilder::COLORS;
//cout << "Colors enabled\n";
}
if(has_textures && !no_texcoords) {
components |= NexusBuilder::TEXTURES;
//cout << "Textures enabled\n";
}
//WORKAROUND to save loading textures not needed
if(!(components & NexusBuilder::TEXTURES)) {
stream->textures.clear();
}
NexusBuilder builder(components);
builder.skipSimplifyLevels = skiplevels;
builder.setMaxMemory(max_memory);
builder.n_threads = n_threads;
builder.setScaling(scaling);
builder.useNodeTex = !useOrigTex;
builder.createPowTwoTex = create_pow_two_tex;
if(deepzoom)
builder.header.signature.flags |= nx::Signature::Flags::DEEPZOOM;
builder.tex_quality = tex_quality;
bool success = builder.initAtlas(stream->textures);
if(!success) {
//cerr << "Exiting" << endl;
return NXSERR_EXCEPTION;
}
tree = new KDTreeSoup("cache_tree", adaptive);
tree->setMaxMemory((1<<20)*(uint64_t)ram_buffer/2);
KDTreeSoup *treesoup = dynamic_cast<KDTreeSoup *>(tree);
if(treesoup) {
treesoup->setMaxWeight(node_size);
treesoup->texelWeight = texel_weight;
treesoup->setTrianglesPerBlock(node_size);
}
builder.create(tree, stream, top_node_size);
QString qOutput(output);
bool compress = qOutput.endsWith(".nxz");
if (compress){
// Generate temporary uncompressed file first
qOutput = qOutput + ".tmp.nxs";
}
builder.save(qOutput);
if (compress){
float coord_step = 0.0f; //approxismate step for quantization
int position_bits = 0;
float error_q = 0.1;
int luma_bits = 6;
int chroma_bits = 6;
int alpha_bits = 5;
int norm_bits = 10;
float tex_step = 0.25;
double error(-1.0);
double max_size(0.0);
int max_triangles(0.0);
int max_level(-1);
QString projection("");
QString matrix("");
QString imatrix("");
QString compresslib("corto");
bool info = false;
bool check = false;
bool drop_level = false;
QString recompute_error;
inputs.clear();
inputs.append(qOutput);
NexusData nexus;
nexus.file = new QTNexusFile();
bool read_only = true;
if(!recompute_error.isEmpty())
read_only = false;
if(!nexus.open(inputs[0].toLatin1())) {
//cerr << "Fatal error: could not open file " << qPrintable(inputs[0]) << endl;
return NXSERR_EXCEPTION;
}
QString qCompressedOutput(output);
Extractor extractor(&nexus);
// if(max_size != 0.0)
// extractor.selectBySize(max_size*(1<<20));
// if(error != -1)
// extractor.selectByError(error);
// if(max_triangles != 0)
// extractor.selectByTriangles(max_triangles);
// if(max_level >= 0)
// extractor.selectByLevel(max_level);
// if(drop_level)
// extractor.dropLevel();
// bool invert = false;
// if(!imatrix.isEmpty()) {
// matrix = imatrix;
// invert = true;
// }
// if(!matrix.isEmpty()) {
// QStringList sl = matrix.split(":");
// if(sl.size() != 16) {
// cerr << "Wrong matrix: found only " << sl.size() << " elements" << endl;
// exit(-1);
// }
// vcg::Matrix44f m;
// for(int i = 0; i < sl.size(); i++)
// m.V()[i] = sl.at(i).toFloat();
// //if(invert)
// // m = vcg::Invert(m);
// extractor.setMatrix(m);
// }
Signature signature = nexus.header.signature;
signature.flags &= ~(Signature::MECO | Signature::CORTO);
if(compresslib == "meco")
signature.flags |= Signature::MECO;
else if(compresslib == "corto")
signature.flags |= Signature::CORTO;
else {
//cerr << "Unknown compression method: " << qPrintable(compresslib) << endl;
return NXSERR_EXCEPTION;
}
if(coord_step) { //global precision, absolute value
extractor.error_factor = 0.0; //ignore error factor.
//do nothing
} else if(position_bits) {
vcg::Sphere3f &sphere = nexus.header.sphere;
coord_step = sphere.Radius()/pow(2.0f, position_bits);
extractor.error_factor = 0.0;
} else if(error_q) {
//take node 0:
uint32_t sink = nexus.header.n_nodes -1;
coord_step = error_q*nexus.nodes[0].error/2;
for(unsigned int i = 0; i < sink; i++){
Node &n = nexus.nodes[i];
Patch &patch = nexus.patches[n.first_patch];
if(patch.node != sink)
continue;
double e = error_q*n.error/2;
if(e < coord_step && e > 0)
coord_step = e; //we are looking at level1 error, need level0 estimate.
}
extractor.error_factor = error_q;
}
//cout << "Vertex quantization step: " << coord_step << endl;
//cout << "Texture quantization step: " << tex_step << endl;
extractor.coord_q =(int)log2(coord_step);
extractor.norm_bits = norm_bits;
extractor.color_bits[0] = luma_bits;
extractor.color_bits[1] = chroma_bits;
extractor.color_bits[2] = chroma_bits;
extractor.color_bits[3] = alpha_bits;
extractor.tex_step = tex_step; //was (int)log2(tex_step * pow(2, -12));, moved to per node value
//cout << "Texture step: " << extractor.tex_step << endl;
//cout << "Saving with flag: " << signature.flags;
/*if (signature.flags & Signature::MECO) cout << " (compressed with MECO)";
else if (signature.flags & Signature::CORTO) cout << " (compressed with CORTO)";
else cout << " (not compressed)";
cout << endl;*/
extractor.save(qCompressedOutput, signature);
//cout << "Saving to file " << qPrintable(output) << endl;
}
if (compress){
// Remove old tmp file
QFile::remove(qOutput);
}
} catch(QString error) {
//cerr << "Fatal error: " << qPrintable(error) << endl;
returncode = NXSERR_EXCEPTION;
} catch(const char *error) {
//cerr << "Fatal error: " << error << endl;
returncode = NXSERR_EXCEPTION;
}
if(tree) delete tree;
if(stream) delete stream;
return returncode;
}