Skip to content

Commit

Permalink
Update VoxFormat module
Browse files Browse the repository at this point in the history
  • Loading branch information
mik14a committed Nov 8, 2020
1 parent 0a84f29 commit 08f07ec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 45 deletions.
9 changes: 8 additions & 1 deletion Source/VOX4UEditor/Private/FVoxel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ FVoxel::FVoxel() : Min(ForceInit), Max(ForceInit)
FVoxel::FVoxel(const FString& Filename, const void* Data, int64 Size, const UVoxImportOption* ImportOption)
: Min(ForceInit), Max(ForceInit)
{
UE_LOG(LogVox, Log, TEXT("Start import vox [%s]."), *Filename);

this->Filename = Filename;
this->ImportOption = ImportOption;
FVox vox = ReadVox(Data, Size);

auto ExtensionFormat = !vox.Node.empty() || !vox.Layer.empty();
auto ExtensionFormat = !!vox.Node.Num();
UE_LOG(LogVox, Log, TEXT("Vox %s extension format."), ExtensionFormat ? TEXT("is") : TEXT("is not"));
auto Importer = TUniquePtr<IVoxImporter>(
ExtensionFormat ? static_cast<IVoxImporter*>(new VoxExtensionImporter(this)) : static_cast<IVoxImporter*>(new VoxImporter(this))
);
Importer->Import(vox);

// Centering
if (ImportOption->bImportXYCenter) {
UE_LOG(LogVox, Log, TEXT("Centering objects."));
const auto Volume = Max - Min + FIntVector(1, 1, 1);
const auto Offset = FIntVector(Min.X + Volume.X / 2, Min.Y + Volume.Y / 2, 0);
auto Temp = TMap<FIntVector, uint8>();
Expand All @@ -64,6 +68,7 @@ FVoxel::FVoxel(const FString& Filename, const void* Data, int64 Size, const UVox

// X forwarding
if (ImportOption->bImportXForward) {
UE_LOG(LogVox, Log, TEXT("X forwarding objects."));
auto Temp = TMap<FIntVector, uint8>();
Temp.Reserve(Voxel.Num());
for (const auto& Cell : Voxel) {
Expand All @@ -81,6 +86,8 @@ FVoxel::FVoxel(const FString& Filename, const void* Data, int64 Size, const UVox
Max.Z = FMath::Max(Max.Z, Cell.Key.Z);
}
}

UE_LOG(LogVox, Log, TEXT("Import done."));
}

FVoxel::~FVoxel()
Expand Down
66 changes: 33 additions & 33 deletions Source/VOX4UEditor/Private/Importer/VoxExtensionImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ VoxExtensionImporter::VoxExtensionImporter(FVoxel* voxel)

void VoxExtensionImporter::Import(const FVox& vox)
{
auto root = vox.Node.find(0);
auto translation = ::translation();
auto rotation = ::rotation();
if (root != vox.Node.end()) {
auto node = root->second;
auto root = vox.Node.Find(0);
auto translation = FVoxTranslation();
auto rotation = FVoxRotation();
if (root != nullptr) {
auto node = *root;
Visit(vox, node, translation, rotation);
}
for (const auto& Cell : Voxel->Voxel) {
Expand All @@ -37,13 +37,13 @@ void VoxExtensionImporter::Import(const FVox& vox)
}
const auto& palette = vox.Palette.Palettes;
for (const auto& color : palette) {
Voxel->Palette.Add(FColor(color.r, color.g, color.b, color.a));
Voxel->Palette.Add(FColor(color.R, color.G, color.B, color.A));
}
}

#define TAG(tag) (tag) & 0xff, ((tag) >> 8) & 0xff, ((tag) >> 16) & 0xff, ((tag) >> 24) & 0xff

void VoxExtensionImporter::Visit(const FVox& vox, std::shared_ptr<FVoxNode> node, const translation& translation, const rotation& rotation)
void VoxExtensionImporter::Visit(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation)
{
auto function = SceneGraphOperator.Find(node->Tag);
if (function) {
Expand All @@ -55,35 +55,35 @@ void VoxExtensionImporter::Visit(const FVox& vox, std::shared_ptr<FVoxNode> node

#define UE_LOG_TRANSFORM(CategoryName, Verbosity, Depth, Transform) \
UE_LOG(CategoryName, Verbosity, TEXT("%s* Transfrom: Name[%s], Translation[%d,%d,%d], Rotation[[%d,%d,%d][%d,%d,%d][%d,%d,%d]]"), \
* FString::ChrN(Depth, ' '), \
* FString(Transform->Name.c_str()), \
Transform->Frame[0].translation.x, Transform->Frame[0].translation.y, Transform->Frame[0].translation.z, \
Transform->Frame[0].rotation.m[0][0], Transform->Frame[0].rotation.m[0][1], Transform->Frame[0].rotation.m[0][2], \
Transform->Frame[0].rotation.m[1][0], Transform->Frame[0].rotation.m[1][1], Transform->Frame[0].rotation.m[1][2], \
Transform->Frame[0].rotation.m[2][0], Transform->Frame[0].rotation.m[2][1], Transform->Frame[0].rotation.m[2][2] \
*FString::ChrN(Depth, ' '), \
*Transform->Name, \
Transform->Frame[0].Translation.X, Transform->Frame[0].Translation.Y, Transform->Frame[0].Translation.Z, \
Transform->Frame[0].Rotation.M[0][0], Transform->Frame[0].Rotation.M[0][1], Transform->Frame[0].Rotation.M[0][2], \
Transform->Frame[0].Rotation.M[1][0], Transform->Frame[0].Rotation.M[1][1], Transform->Frame[0].Rotation.M[1][2], \
Transform->Frame[0].Rotation.M[2][0], Transform->Frame[0].Rotation.M[2][1], Transform->Frame[0].Rotation.M[2][2] \
)

void VoxExtensionImporter::Transform(const FVox& vox, std::shared_ptr<FVoxNode> node, const ::translation& translation, const ::rotation& rotation)
void VoxExtensionImporter::Transform(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation)
{
const auto transform = std::static_pointer_cast<FVoxNodeTransform>(node);
const auto transform = StaticCastSharedPtr<FVoxNodeTransform>(node);
UE_LOG_TRANSFORM(LogVoxImporter, Log, Depth, transform);

const auto& tname = transform->Name;
const auto& ftranslation = transform->Frame[0].translation;
const auto& frotation = transform->Frame[0].rotation;
Visit(vox, vox.Node.at(transform->Child), ftranslation, frotation);
const auto& ftranslation = transform->Frame[0].Translation;
const auto& frotation = transform->Frame[0].Rotation;
Visit(vox, *vox.Node.Find(transform->Child), ftranslation, frotation);
}

#define UE_LOG_GROUP(CategoryName, Verbosity, Depth, Group) \
UE_LOG(CategoryName, Verbosity, TEXT("%s* Group"), *FString::ChrN(Depth, ' '))

void VoxExtensionImporter::Group(const FVox& vox, std::shared_ptr<FVoxNode> node, const ::translation& translation, const ::rotation& rotation)
void VoxExtensionImporter::Group(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation)
{
const auto group = std::static_pointer_cast<FVoxNodeGroup>(node);
const auto group = StaticCastSharedPtr<FVoxNodeGroup>(node);
UE_LOG_GROUP(LogVoxImporter, Log, Depth, group);

for (const auto& child : group->Child) {
Visit(vox, vox.Node.at(child), translation, rotation);
Visit(vox, *vox.Node.Find(child), translation, rotation);
}
}

Expand All @@ -94,15 +94,15 @@ UE_LOG(CategoryName, Verbosity, TEXT("%s* Shape: Id[%d], Size[%d,%d,%d]"), \
Voxel.Size[Shape->Model[0].Id].Z, Voxel.Size[Shape->Model[0].Id].Y, Voxel.Size[Shape->Model[0].Id].Z \
)

void VoxExtensionImporter::Shape(const FVox& vox, std::shared_ptr<FVoxNode> node, const ::translation& translation, const ::rotation& rotation)
void VoxExtensionImporter::Shape(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation)
{
const auto shape = std::static_pointer_cast<FVoxNodeShape>(node);
const auto shape = StaticCastSharedPtr<FVoxNodeShape>(node);
UE_LOG_SHAPE(LogVoxImporter, Log, Depth, vox, shape);

const auto sid = shape->Model[0].Id;
const auto& ssize = vox.Size[sid];
const auto center = Transform(rotation, ::translation{ ssize.X / 2, ssize.Y / 2, ssize.Z / 2 });
const auto Translation = FIntVector{ translation.x - center.x, translation.y - center.y, translation.z - center.z };
const auto center = Transform(rotation, FVoxTranslation{ ssize.X / 2, ssize.Y / 2, ssize.Z / 2 });
const auto Translation = FIntVector{ translation.X - center.X, translation.Y - center.Y, translation.Z - center.Z };
const auto Delta = Transform(rotation, FIntVector{ 1, 1, 1 });
const auto Correct = FIntVector(Delta.X < 0 ? -1 : 0, Delta.Y < 0 ? -1 : 0, Delta.Z < 0 ? -1 : 0);
const auto& svoxel = vox.Voxel[sid];
Expand All @@ -113,20 +113,20 @@ void VoxExtensionImporter::Shape(const FVox& vox, std::shared_ptr<FVoxNode> node
}
}

::translation VoxExtensionImporter::Transform(const ::rotation& M, const ::translation& T)
FVoxTranslation VoxExtensionImporter::Transform(const FVoxRotation& M, const FVoxTranslation& T)
{
return {
T.x * M.m[0][0] + T.y * M.m[1][0] + T.z * M.m[2][0],
T.x * M.m[0][1] + T.y * M.m[1][1] + T.z * M.m[2][1],
T.x * M.m[0][2] + T.y * M.m[1][2] + T.z * M.m[2][2]
T.X * M.M[0][0] + T.Y * M.M[1][0] + T.Z * M.M[2][0],
T.X * M.M[0][1] + T.Y * M.M[1][1] + T.Z * M.M[2][1],
T.X * M.M[0][2] + T.Y * M.M[1][2] + T.Z * M.M[2][2]
};
}

FIntVector VoxExtensionImporter::Transform(const ::rotation& M, const FIntVector& V)
FIntVector VoxExtensionImporter::Transform(const FVoxRotation& M, const FIntVector& V)
{
return {
V.X * M.m[0][0] + V.Y * M.m[1][0] + V.Z * M.m[2][0],
V.X * M.m[0][1] + V.Y * M.m[1][1] + V.Z * M.m[2][1],
V.X * M.m[0][2] + V.Y * M.m[1][2] + V.Z * M.m[2][2]
V.X * M.M[0][0] + V.Y * M.M[1][0] + V.Z * M.M[2][0],
V.X * M.M[0][1] + V.Y * M.M[1][1] + V.Z * M.M[2][1],
V.X * M.M[0][2] + V.Y * M.M[1][2] + V.Z * M.M[2][2]
};
}
16 changes: 8 additions & 8 deletions Source/VOX4UEditor/Private/Importer/VoxExtensionImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
struct VoxExtensionImporter : public IVoxImporter
{
using SceneGraphOperatorT = void(VoxExtensionImporter::*)(const FVox&, std::shared_ptr<FVoxNode>, const ::translation&, const ::rotation&);
template <typename T> using TypeIndexMapT = TMap<uint32_t, T>;
using SceneGraphOperatorT = void(VoxExtensionImporter::*)(const FVox&, TSharedPtr<FVoxNode>, const FVoxTranslation&, const FVoxRotation&);
template <typename T> using TypeIndexMapT = TMap<uint32, T>;
static const TypeIndexMapT<SceneGraphOperatorT> SceneGraphOperator;

public:
Expand All @@ -23,19 +23,19 @@ struct VoxExtensionImporter : public IVoxImporter

protected:
/** Visit scene graph */
void Visit(const FVox& vox, std::shared_ptr<FVoxNode> node, const ::translation& translation, const ::rotation& rotation);
void Visit(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation);
/** Visit transform node (nTRN) */
void Transform(const FVox& vox, std::shared_ptr<FVoxNode> node, const ::translation& translation, const ::rotation& rotation);
void Transform(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation);
/** Visit group node (nGRP) */
void Group(const FVox& vox, std::shared_ptr<FVoxNode> node, const ::translation& translation, const ::rotation& rotation);
void Group(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation);
/** Visit shape node (nSHP) */
void Shape(const FVox& vox, std::shared_ptr<FVoxNode> node, const ::translation& translation, const ::rotation& rotation);
void Shape(const FVox& vox, TSharedPtr<FVoxNode> node, const FVoxTranslation& translation, const FVoxRotation& rotation);

private:
/** Transform translation */
static ::translation Transform(const ::rotation& M, const ::translation& T);
static FVoxTranslation Transform(const FVoxRotation& M, const FVoxTranslation& T);
/** Transform vector */
static FIntVector Transform(const ::rotation& M, const FIntVector& V);
static FIntVector Transform(const FVoxRotation& M, const FIntVector& V);

private: // Immutable members
/** Scene graph depth */
Expand Down
4 changes: 2 additions & 2 deletions Source/VOX4UEditor/Private/Importer/VoxImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ void VoxImporter::Import(const FVox& vox)
Voxel->Voxel.Add(MoveTemp(vector), cell.i);
}
const auto& palette = vox.Palette.Palettes;
if (!palette.empty()) {
if (palette.Num()) {
for (const auto& color : palette) {
Voxel->Palette.Add(FColor(color.r, color.g, color.b, color.a));
Voxel->Palette.Add(FColor(color.R, color.G, color.B, color.A));
}
} else {
for (const auto& color : FVox::DefaultPalette) {
Expand Down

0 comments on commit 08f07ec

Please sign in to comment.