ws-3d3: codecs/model/Draco: Draco decoder (port of Google Draco 1.5.7, Apache-2.0)
Meshes (sequential; Edgebreaker standard / predictive / valence, topology splits, attribute seams) and sequential point clouds, bitstreams 1.0 - 2.2; generic / integer / quantization / octahedral normal attribute decoders; difference, parallelogram, multi and constrained multi-parallelogram, tex coords (deprecated and portable, exact int64 arithmetic) and geometric normal predictions; metadata. Byte-exact against libdraco on 196 files (Draco test data 0.9 - 2.2 and draco_encoder outputs at every level / quantization). kd-tree point clouds: UNSUPPORTED for now.
This commit is contained in:
1 parent
de4f14a343
commit
9a6ddd5c16
12 files changed
+4159
No files matched your search
@@ -0,0 +1,44 @@
|
||||
// Dumps a decoded Draco file: points, faces and every attribute's per-point bytes (hex), for the fixture generator.
|
||||
#include "draco/compression/decode.h"
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
static void hex(const uint8_t* p, size_t n) { for (size_t i = 0; i < n; ++i) printf("%02x", p[i]); }
|
||||
int main(int argc, char** argv) {
|
||||
std::ifstream f(argv[1], std::ios::binary);
|
||||
std::vector<char> data((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
draco::DecoderBuffer buf;
|
||||
buf.Init(data.data(), data.size());
|
||||
auto type = draco::Decoder::GetEncodedGeometryType(&buf);
|
||||
if (!type.ok()) { printf("ERROR %s\n", type.status().error_msg()); return 0; }
|
||||
draco::Decoder dec;
|
||||
std::unique_ptr<draco::PointCloud> pc;
|
||||
std::unique_ptr<draco::Mesh> mesh;
|
||||
if (type.value() == draco::TRIANGULAR_MESH) {
|
||||
auto r = dec.DecodeMeshFromBuffer(&buf);
|
||||
if (!r.ok()) { printf("ERROR %s\n", r.status().error_msg()); return 0; }
|
||||
mesh = std::move(r).value();
|
||||
} else {
|
||||
auto r = dec.DecodePointCloudFromBuffer(&buf);
|
||||
if (!r.ok()) { printf("ERROR %s\n", r.status().error_msg()); return 0; }
|
||||
pc = std::move(r).value();
|
||||
}
|
||||
const draco::PointCloud* g = mesh ? mesh.get() : pc.get();
|
||||
printf("POINTS %u\n", g->num_points());
|
||||
if (mesh) {
|
||||
printf("FACES %u ", mesh->num_faces());
|
||||
for (draco::FaceIndex fi(0); fi < mesh->num_faces(); ++fi) {
|
||||
const auto& face = mesh->face(fi);
|
||||
for (int c = 0; c < 3; ++c) { uint32_t v = face[c].value(); hex((const uint8_t*)&v, 4); }
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
for (int i = 0; i < g->num_attributes(); ++i) {
|
||||
const draco::PointAttribute* a = g->attribute(i);
|
||||
printf("ATTR %d %d %d %d %u ", (int)a->attribute_type(), (int)a->data_type(), (int)a->num_components(), a->normalized() ? 1 : 0, a->unique_id());
|
||||
for (draco::PointIndex p(0); p < g->num_points(); ++p) hex(a->GetAddress(a->mapped_index(p)), a->byte_stride());
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
# Draco fixtures (tools/fixtures.py): every .drc file of Google's Draco test data (bitstreams 0.9 - 2.2, read from the
|
||||
# nixpkgs draco source, never committed) and files made here by draco_encoder 1.5.7 (nix) from the test data meshes
|
||||
# and generated ones (a UV sphere with seams, a noisy grid, a coloured PLY), at every compression level (sequential,
|
||||
# Edgebreaker standard / valence; difference, parallelogram, constrained multi-parallelogram, portable tex coords and
|
||||
# geometric normal predictions), several quantizations (up to 30 bits) and as point clouds. The reference values come
|
||||
# from libdraco itself: tools/fixtures/_draco_dump.cc, compiled here against nixpkgs#draco, prints the decoded points,
|
||||
# faces and each attribute's bytes per point.
|
||||
# tests/gen/draco_cases.luau { { name, data, error? = true, points, faces, indices = crc, attrs = { { type, dataType,
|
||||
# components, normalized, uniqueId, crc } } } }
|
||||
import math, os, subprocess, tempfile, zlib
|
||||
|
||||
INPUTS = ["tools/fixtures/_draco_dump.cc"]
|
||||
|
||||
|
||||
def store_path(attr):
|
||||
out = subprocess.run(["nix", "build", "nixpkgs#" + attr, "--no-link", "--print-out-paths"], capture_output=True, check=True)
|
||||
return out.stdout.decode().strip().split("\n")[-1]
|
||||
|
||||
|
||||
def uv_sphere(rings=12, segs=20):
|
||||
# an OBJ sphere with a UV seam and poles: vertices shared by position, split by UV
|
||||
v, vt, vn, f = [], [], [], []
|
||||
for r in range(rings + 1):
|
||||
th = math.pi * r / rings
|
||||
for s in range(segs + 1):
|
||||
ph = 2 * math.pi * s / segs
|
||||
x, y, z = math.sin(th) * math.cos(ph), math.cos(th), math.sin(th) * math.sin(ph)
|
||||
v.append((x, y, z))
|
||||
vn.append((x, y, z))
|
||||
vt.append((s / segs, r / rings))
|
||||
w = segs + 1
|
||||
for r in range(rings):
|
||||
for s in range(segs):
|
||||
a, b, c, d = r * w + s + 1, r * w + s + 2, (r + 1) * w + s + 2, (r + 1) * w + s + 1
|
||||
if r > 0:
|
||||
f.append((a, b, c))
|
||||
if r < rings - 1:
|
||||
f.append((a, c, d))
|
||||
out = ["v %.6f %.6f %.6f" % p for p in v] + ["vt %.6f %.6f" % p for p in vt] + ["vn %.6f %.6f %.6f" % p for p in vn]
|
||||
out += ["f %d/%d/%d %d/%d/%d %d/%d/%d" % (a, a, a, b, b, b, c, c, c) for a, b, c in f]
|
||||
return "\n".join(out) + "\n"
|
||||
|
||||
|
||||
def noisy_grid(rng, n=24):
|
||||
# a height field with a hole and per-vertex UVs: boundaries, holes and parallelogram predictions
|
||||
v, vt, f = [], [], []
|
||||
for j in range(n):
|
||||
for i in range(n):
|
||||
v.append((i * 0.1, rng.uniform(-0.05, 0.05), j * 0.1))
|
||||
vt.append((i / (n - 1), j / (n - 1)))
|
||||
for j in range(n - 1):
|
||||
for i in range(n - 1):
|
||||
if 8 <= i < 12 and 8 <= j < 12:
|
||||
continue
|
||||
a, b, c, d = j * n + i + 1, j * n + i + 2, (j + 1) * n + i + 2, (j + 1) * n + i + 1
|
||||
f.append((a, b, c))
|
||||
f.append((a, c, d))
|
||||
out = ["v %.6f %.6f %.6f" % p for p in v] + ["vt %.6f %.6f" % p for p in vt]
|
||||
out += ["f %d/%d %d/%d %d/%d" % (a, a, b, b, c, c) for a, b, c in f]
|
||||
return "\n".join(out) + "\n"
|
||||
|
||||
|
||||
def colored_ply(rng, n=200):
|
||||
# an ASCII point set / mesh with colours (u8) and a generic float property
|
||||
pts = [(rng.uniform(-1, 1), rng.uniform(-1, 1), rng.uniform(-1, 1)) for _ in range(n)]
|
||||
faces = [(i, i + 1, i + 2) for i in range(0, n - 2, 3)]
|
||||
head = ["ply", "format ascii 1.0", "element vertex %d" % n, "property float x", "property float y", "property float z",
|
||||
"property uchar red", "property uchar green", "property uchar blue", "property uchar alpha",
|
||||
"element face %d" % len(faces), "property list uchar int vertex_indices", "end_header"]
|
||||
body = ["%.5f %.5f %.5f %d %d %d %d" % (p[0], p[1], p[2], rng.randrange(256), rng.randrange(256), rng.randrange(256), 255) for p in pts]
|
||||
body += ["3 %d %d %d" % t for t in faces]
|
||||
return "\n".join(head + body) + "\n"
|
||||
|
||||
|
||||
def parse_dump(txt):
|
||||
out = {"attrs": []}
|
||||
for line in txt.split("\n"):
|
||||
parts = line.split(" ")
|
||||
if parts[0] == "ERROR":
|
||||
return {"error": True}
|
||||
if parts[0] == "POINTS":
|
||||
out["points"] = int(parts[1])
|
||||
elif parts[0] == "FACES":
|
||||
out["faces"] = int(parts[1])
|
||||
out["indices"] = zlib.crc32(bytes.fromhex(parts[2] if len(parts) > 2 else ""))
|
||||
elif parts[0] == "ATTR":
|
||||
out["attrs"].append({"type": int(parts[1]), "dataType": int(parts[2]), "components": int(parts[3]),
|
||||
"normalized": parts[4] == "1", "uniqueId": int(parts[5]),
|
||||
"crc": zlib.crc32(bytes.fromhex(parts[6] if len(parts) > 6 else ""))})
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
draco = store_path("draco")
|
||||
src = store_path("draco.src")
|
||||
testdata = os.path.join(src, "testdata")
|
||||
tmp = tempfile.mkdtemp(prefix="formats-draco-")
|
||||
dump = os.path.join(tmp, "dump")
|
||||
ctx.nix("gcc", ["g++", "-O1", "-std=c++17", "-I" + os.path.join(draco, "include"), os.path.join(ctx.root, "tools", "fixtures", "_draco_dump.cc"),
|
||||
"-L" + os.path.join(draco, "lib"), "-l:libdraco.a", "-o", dump])
|
||||
encoder = os.path.join(draco, "bin", "draco_encoder")
|
||||
|
||||
files = [] # (name, path)
|
||||
for f in sorted(os.listdir(testdata)):
|
||||
if f.endswith(".drc"):
|
||||
files.append(("testdata/" + f, os.path.join(testdata, f)))
|
||||
|
||||
rng = ctx.rng(31)
|
||||
gen_inputs = {"uvsphere.obj": uv_sphere(), "grid.obj": noisy_grid(rng), "colored.ply": colored_ply(rng)}
|
||||
for name, text in gen_inputs.items():
|
||||
with open(os.path.join(tmp, name), "w") as fh:
|
||||
fh.write(text)
|
||||
inputs = [(n, os.path.join(tmp, n)) for n in gen_inputs] + [(n, os.path.join(testdata, n)) for n in
|
||||
("cube_att.obj", "test_nm.obj", "sphere.obj", "cube_quads.obj", "test_generic.ply", "Box.ply")]
|
||||
variants = [("cl%d" % cl, ["-cl", str(cl)]) for cl in range(11)]
|
||||
variants += [("q14", ["-qp", "14", "-qt", "12", "-qn", "10"]), ("q20", ["-qp", "20", "-qt", "16", "-qn", "14", "-qg", "12"]),
|
||||
("q30", ["-qp", "30", "-qt", "30", "-qn", "30", "-qg", "30"]), ("q30cl10", ["-cl", "10", "-qp", "24", "-qt", "24", "-qn", "24"]),
|
||||
("raw", ["-qp", "0", "-qt", "0", "-qn", "0", "-qg", "0"]), ("meta", ["--metadata"]), ("poly", ["-preserve_polygons"]),
|
||||
("pc", ["-point_cloud", "-cl", "0"]), ("pckd", ["-point_cloud"])]
|
||||
for iname, ipath in inputs:
|
||||
for vname, args in variants:
|
||||
out = os.path.join(tmp, "%s.%s.drc" % (iname, vname))
|
||||
r = subprocess.run([encoder, "-i", ipath, "-o", out] + args, capture_output=True)
|
||||
if r.returncode == 0 and os.path.exists(out):
|
||||
files.append(("enc/%s.%s" % (iname, vname), out))
|
||||
# the large bunny once (Edgebreaker valence + normals)
|
||||
out = os.path.join(tmp, "bunny.drc")
|
||||
if subprocess.run([encoder, "-i", os.path.join(testdata, "bunny_norm.obj"), "-o", out, "-cl", "7"], capture_output=True).returncode == 0:
|
||||
files.append(("enc/bunny_norm.obj.cl7", out))
|
||||
|
||||
cases = []
|
||||
for name, path in files:
|
||||
ref = parse_dump(subprocess.run([dump, path], capture_output=True).stdout.decode())
|
||||
case = {"name": name, "data": open(path, "rb").read()}
|
||||
case.update(ref)
|
||||
cases.append(case)
|
||||
ctx.emit("draco_cases", cases)
|
||||
Reference in new issue
Block a user