forked from syoyo/tinyxpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxpd_reader_example.cc
More file actions
167 lines (135 loc) · 4.7 KB
/
Copy pathxpd_reader_example.cc
File metadata and controls
167 lines (135 loc) · 4.7 KB
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
#define TINY_XPD_IMPLEMENTATION
#include "tiny_xpd.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace tiny_xpd;
static std::string PrintPrimType(Xpd::PrimType prim) {
if (prim == Xpd::PrimType::Point) {
return "Point";
} else if (prim == Xpd::PrimType::Spline) {
return "Spline";
} else if (prim == Xpd::PrimType::Card) {
return "Card";
} else if (prim == Xpd::PrimType::Sphere) {
return "Sphere";
} else if (prim == Xpd::PrimType::Archive) {
return "Archive";
} else if (prim == Xpd::PrimType::CustomPT) {
return "CustomPT";
}
return "UNKNOWN PrimType. value = " + std::to_string(int(prim));
}
static std::string PrintCoordSpace(Xpd::CoordSpace space) {
if (space == Xpd::CoordSpace::World) {
return "World";
} else if (space == Xpd::CoordSpace::Object) {
return "Object";
} else if (space == Xpd::CoordSpace::Local) {
return "Local";
} else if (space == Xpd::CoordSpace::Micro) {
return "Micro";
} else if (space == Xpd::CoordSpace::CustomCS) {
return "CustomCS";
}
return "UNKNOWN CoordSpace. value = " + std::to_string(int(space));
}
static void GetPrimData(const tiny_xpd::XPDHeader &xpd, const std::vector<uint8_t> &xpd_data, size_t face_idx, size_t block_idx, std::vector<float> *prims)
{
prims->clear();
uint32_t num_prims = xpd.numPrims[face_idx];
if (num_prims == 0) {
return;
}
// prim_count = num_prims * sum(xpd.primSize[])
size_t prim_count = 0;
for (size_t p = 0; p < xpd.primSize.size(); p++) {
prim_count += xpd.primSize[p];
}
prim_count *= num_prims;
// Pritive value is always float.
const size_t num_bytes = sizeof(float) * prim_count;
const size_t src_offset = xpd.blockPosition[face_idx * xpd.numBlocks + block_idx];
std::vector<float> buffer;
buffer.resize(prim_count);
memcpy(buffer.data(), xpd_data.data() + src_offset, num_bytes);
prims->insert(prims->end(), buffer.begin(), buffer.end());
}
static void PrintXPD(const tiny_xpd::XPDHeader &xpd,
const std::vector<uint8_t> &xpd_data) {
(void)xpd_data;
std::cout << "fileVersion : " << int(xpd.fileVersion) << "\n";
std::cout << "primType : " << PrintPrimType(xpd.primType) << "\n";
std::cout << "primVersion : " << int(xpd.primVersion) << "\n";
std::cout << "time : " << xpd.time << "\n";
std::cout << "numCVs : " << xpd.numCVs << "\n";
std::cout << "coordSpace : " << PrintCoordSpace(xpd.coordSpace) << "\n";
std::cout << "numBlocks : " << xpd.numBlocks << "\n";
std::cout << "numFaces : " << xpd.numFaces << "\n";
std::cout << "# of keys : " << xpd.key.size() << "\n";
std::cout << "# of keyToIds : " << xpd.keyToId.size() << "\n";
std::cout << "# of faceids : " << xpd.faceid.size() << "\n";
for (size_t i = 0; i < xpd.block.size(); i++) {
std::cout << "block[" << i << "] = " << xpd.block[i]
<< "\n";
}
for (size_t i = 0; i < xpd.key.size(); i++) {
std::cout << "key[" << i << "] = " << xpd.key[i]
<< "\n";
}
for (const auto &it : xpd.keyToId) {
std::cout << "keyToId[" << it.first << "] = " << it.second
<< "\n";
}
for (size_t i = 0; i < xpd.faceid.size(); i++) {
std::cout << "faceid[" << i << "] = " << xpd.faceid[i]
<< "\n";
}
// print blockPositon
for (size_t i = 0; i < xpd.blockPosition.size(); i++) {
std::cout << "blockPosition[" << i << "] = " << xpd.blockPosition[i]
<< "\n";
}
// foreach face.
for (size_t f = 0; f < xpd.numFaces; f++) {
// foreach block
for (size_t b = 0; b < xpd.numBlocks; b++) {
std::cout << "face[" << f << "] block[" << b
<< "].numPrims = " << xpd.numPrims[f] << "\n";
std::vector<float> prims;
GetPrimData(xpd, xpd_data, f, b, &prims);
std::cout << "{";
for (size_t p = 0; p < prims.size(); p++) {
std::cout << prims[p];
if (p != (prims.size() - 1)) {
std::cout << ", ";
}
}
std::cout << "}\n";
}
}
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Requires input.xpd\n";
return EXIT_FAILURE;
}
std::string xpd_filename = argv[1];
std::string err;
tiny_xpd::XPDHeader xpd_header;
std::vector<uint8_t> xpd_data;
if (!tiny_xpd::ParseXPDFromFile(xpd_filename, &xpd_header, &xpd_data, &err)) {
if (!err.empty()) {
std::cerr << "Parse error message: " << err << "\n";
}
std::cerr << "Failed to parse XPD file : " << xpd_filename << "\n";
return EXIT_FAILURE;
}
if (xpd_header.primType != tiny_xpd::Xpd::PrimType::Spline) {
std::cerr << "Currently we only support Spline primitive.\n";
return EXIT_FAILURE;
}
PrintXPD(xpd_header, xpd_data);
return EXIT_SUCCESS;
}