Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imported from https://github.com/benmclean/BenVoxel #548

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/modules/voxelformat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ set(SRCS
private/benvoxel/BenBinary.h private/benvoxel/BenBinary.cpp
private/benvoxel/BenJson.h private/benvoxel/BenJson.cpp
private/benvoxel/BenShared.h private/benvoxel/BenShared.cpp
private/benvoxel/SparseVoxelOctree.h private/benvoxel/SparseVoxelOctree.cpp
private/benvoxel/Node.h private/benvoxel/Node.cpp
private/benvoxel/Branch.h private/benvoxel/Branch.cpp
private/benvoxel/Leaf.h private/benvoxel/Leaf.cpp
private/benvoxel/SeekableReadStreamAdapter.hpp

private/binvox/BinVoxFormat.h private/binvox/BinVoxFormat.cpp
private/chronovox/CSMFormat.h private/chronovox/CSMFormat.cpp
Expand Down
71 changes: 6 additions & 65 deletions src/modules/voxelformat/private/benvoxel/BenShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,64 +48,6 @@ bool addPointNode(scenegraph::SceneGraph &sceneGraph, const core::String &name,
return sceneGraph.emplace(core::move(pointNode), parent) != InvalidNodeId;
}

// static bool loadLeaf(io::SeekableReadStream &stream, scenegraph::SceneGraphNode &node, const voxel::Voxel &voxel) {
// return false;
// }

static bool loadBranch(io::SeekableReadStream &stream, scenegraph::SceneGraphNode &node, int depth) {
uint8_t header;
if (stream.readUInt8(header) != 0) {
Log::error("Failed to read header");
return InvalidNodeId;
}
// const uint8_t octant = header & 7;
const uint8_t type = (header >> 6) & 3;
switch (type) {
case 0: { // regular branch
const uint8_t children = (header >> 3 & 7) + 1;
for (uint8_t i = 0; i < children; ++i) {
uint8_t childHeader;
if (stream.peekUInt8(childHeader) != 0) {
Log::error("Failed to read child header");
return InvalidNodeId;
}
// const uint8_t childOctant = childHeader & 7;
if ((childHeader & 0b10000000) > 0) {
// TODO: VOXELFORMAT: load the Sparse Voxel Octree
// if (!loadLeaf(stream, node)) {
// return false;
// }
} else {
if (!loadBranch(stream, node, depth++)) {
return false;
}
}
}
break;
}
case 1: { // collapsed branch
uint8_t color;
if (stream.readUInt8(color) != 0) {
Log::error("Failed to read color for collapsed branch");
return InvalidNodeId;
}
// TODO: VOXELFORMAT: load the Sparse Voxel Octree
// voxel::Voxel voxel = color == 0 ? voxel::Voxel() : voxel::createVoxel(node.palette(), color);
if (depth == 15) {
for (uint8_t c_octant = 0u; c_octant < 8u; c_octant++) {
// loadLeafVoxels(stream, node, c_octant, voxel);
}
} else {
for (uint8_t c_octant = 0u; c_octant < 8u; c_octant++) {
loadBranch(stream, node, /*voxel, */ depth);
}
}
break;
}
}
return true;
}

int createModelNode(scenegraph::SceneGraph &sceneGraph, const palette::Palette &palette, const core::String &name,
int width, int height, int depth, io::SeekableReadStream &stream, const Metadata &globalMetadata,
const Metadata &metadata) {
Expand All @@ -116,13 +58,12 @@ int createModelNode(scenegraph::SceneGraph &sceneGraph, const palette::Palette &
node.setPalette(palette);
node.setVolume(v, true);

int svoDepth = 0;
while (!stream.eos()) {
if (!loadBranch(stream, node, svoDepth)) {
Log::error("Failed to load branch");
return InvalidNodeId;
}
}
io::SeekableReadStreamAdapter adapter(stream);
BenVoxel::SparseVoxelOctree svo(adapter, static_cast<uint16_t>(width), static_cast<uint16_t>(depth),
static_cast<uint16_t>(height));
for (BenVoxel::Voxel voxel : svo.voxels())
v->setVoxel(static_cast<int32_t>(voxel.x), static_cast<int32_t>(voxel.z), static_cast<int32_t>(voxel.y),
voxel::createVoxel(voxel::VoxelType::Generic, voxel.index, 255, 0));

int nodeId = sceneGraph.emplace(core::move(node));
if (nodeId == InvalidNodeId) {
Expand Down
2 changes: 2 additions & 0 deletions src/modules/voxelformat/private/benvoxel/BenShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#pragma once

#include "SeekableReadStreamAdapter.hpp"
#include "SparseVoxelOctree.h"
#include "core/collection/StringMap.h"
#include "io/Stream.h"
#include "palette/Palette.h"
Expand Down
152 changes: 152 additions & 0 deletions src/modules/voxelformat/private/benvoxel/Branch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// MIT License
//
// Copyright (c) 2024 Ben McLean
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "Branch.h"
namespace BenVoxel {
Branch::Branch() : Node(nullptr, 0), children{} {
}
Branch::Branch(Branch *parent, std::uint8_t octant) : Node(parent, octant & 0b111), children{} {
}
Branch::Branch(Branch *parent, std::uint8_t octant, std::uint8_t color) : Node(parent, octant & 0b111), children{} {
expandCollapsed(color);
}
Branch::Branch(Branch *parent, std::istream &in) : Node(parent, in), children{} {
std::uint8_t header = readByte(in, "Failed to read branch header byte from input stream.");
switch (header & TYPE_MASK) {
case BRANCH_REGULAR: {
std::uint8_t count = ((header >> 3) & 0b111) + 1;
for (std::uint8_t child = 0; child < count; child++)
if (peekByte(in) >> 7) // Check if it's a leaf (both 2-byte and 8-byte start with 1)
set(std::make_unique<Leaf>(this, in));
else
set(std::make_unique<Branch>(this, in));
break;
}
case BRANCH_COLLAPSED: {
expandCollapsed(readByte(in, "Failed to read collapsed branch value from input stream."));
break;
}
default:
throw std::runtime_error("Invalid branch type in header");
}
}
void Branch::expandCollapsed(std::uint8_t color) {
if (depth() == 15)
for (std::uint8_t i = 0; i < 8; i++)
set(std::make_unique<Leaf>(this, i, color));
else
for (std::uint8_t i = 0; i < 8; i++)
set(std::make_unique<Branch>(this, i, color));
}
void Branch::write(std::ostream &out) const {
if (!parent && !first()) { // Empty model case
char branchHeaders[15] = {};
out.write(branchHeaders, sizeof branchHeaders);
out.put(LEAF_2BYTE); // 2-byte payload leaf header
out.put(0); // Both foreground and background zero
out.put(0);
return;
}
std::uint8_t collapsedValue = tryCollapse();
if (collapsedValue != 0) {
out.put(BRANCH_COLLAPSED | (octant & 0b111)); // Header
out.put(collapsedValue);
return;
}
out.put(BRANCH_REGULAR | ((count() - 1) << 3) | (octant & 0b111)); // Header
for (std::uint8_t i = 0; i < 8; i++)
if (children[i])
children[i]->write(out);
}
std::uint8_t Branch::count() const {
std::uint8_t count = 0;
for (std::uint8_t i = 0; i < 8; i++)
if (children[i])
count++;
return count;
}
Node *Branch::first() const {
for (std::uint8_t i = 0; i < 8; i++)
if (children[i])
return children[i].get();
return nullptr;
}
Node *Branch::nextValidChild(std::uint8_t previous) const {
for (std::uint8_t i = previous + 1; i < 8; i++)
if (children[i])
return children[i].get();
return nullptr;
}
Node *Branch::operator[](std::uint8_t child) const {
return children[child].get();
}
Branch &Branch::operator=(Branch &&other) noexcept {
if (this != &other) {
octant = other.octant;
parent = other.parent;
children = std::move(other.children);
}
return *this;
}
void Branch::set(std::unique_ptr<Node> child) {
if (!child)
throw std::invalid_argument("child should not be nullptr.");
children[child->getOctant()] = std::move(child);
}
void Branch::remove(std::uint8_t child) {
children[child] = nullptr;
if (parent && !first())
parent->remove(this->octant);
}
std::uint8_t Branch::tryCollapse() const {
return tryCollapsing(tryCollapseGetColor());
}
std::uint8_t Branch::tryCollapsing(std::uint8_t color) const {
if (color == 0)
return 0;
for (const std::unique_ptr<Node> &child : children) {
if (!child)
return 0;
Leaf *leaf = dynamic_cast<Leaf *>(child.get());
if (leaf) {
for (std::uint8_t i = 0; i < 8; i++)
if (leaf->operator[](i) != color)
return 0;
} else {
Branch *branch = dynamic_cast<Branch *>(child.get());
if (branch && branch->tryCollapsing(color) == 0)
return 0;
}
}
return color;
}
std::uint8_t Branch::tryCollapseGetColor() const {
if (!children[0])
return 0;
Leaf *leaf = dynamic_cast<Leaf *>(children[0].get());
if (leaf)
return leaf->operator[](0);
Branch *branch = dynamic_cast<Branch *>(children[0].get());
if (branch)
return branch->tryCollapseGetColor();
return 0;
}
} // namespace BenVoxel
51 changes: 51 additions & 0 deletions src/modules/voxelformat/private/benvoxel/Branch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once
#include "Leaf.h"
#include "Node.h"
#include <array>
#include <iostream>
// MIT License
//
// Copyright (c) 2024 Ben McLean
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace BenVoxel {
class Branch : public Node {
protected:
std::array<std::unique_ptr<Node>, 8> children;

public:
Branch();
Branch(Branch *parent, std::uint8_t octant);
Branch(Branch *parent, std::uint8_t octant, std::uint8_t color);
Branch(Branch *parent, std::istream &in);
virtual ~Branch() override = default;
void write(std::ostream &out) const override;
std::uint8_t count() const;
Node *first() const;
Node *nextValidChild(std::uint8_t previous) const;
Node *operator[](std::uint8_t child) const;
Branch &operator=(Branch &&other) noexcept;
void set(std::unique_ptr<Node> child);
void remove(std::uint8_t child);
void expandCollapsed(std::uint8_t color);
std::uint8_t tryCollapse() const;
std::uint8_t tryCollapsing(std::uint8_t color) const;
std::uint8_t tryCollapseGetColor() const;
};
} // namespace BenVoxel
Loading
Loading