diff --git a/.clang-format b/.clang-format index 46dba9a..7cd8a15 100644 --- a/.clang-format +++ b/.clang-format @@ -4,10 +4,10 @@ Language: Cpp Standard: Cpp03 AccessModifierOffset: -4 AlignAfterOpenBracket: Align -AlignConsecutiveAssignments: true -AlignConsecutiveDeclarations: true -AlignEscapedNewlines: Left -AlignOperands: true +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: false +AlignOperands: false AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: false AllowShortBlocksOnASingleLine: false diff --git a/VdbWriter.hpp b/VdbWriter.hpp index a0f8a4b..df13a75 100644 --- a/VdbWriter.hpp +++ b/VdbWriter.hpp @@ -106,36 +106,36 @@ inline void write_meta_vec3i(FILE* fp, const std::string& name, const std::array class ATree { protected: - dAABBCC m_Volume; + dAABBCC m_Volume; std::string m_Name; protected: virtual bool addVoxel(const uint32_t& vX, const uint32_t& vY, const uint32_t& vZ, void* vDatas, const size_t& vByteSize, const size_t& vCount) = 0; static uint32_t getBitIndex4(const uint32_t& vX, const uint32_t& vY, const uint32_t& vZ) { - const auto& x = vX & (uint32_t)(4096 - 1); - const auto& y = vY & (uint32_t)(4096 - 1); - const auto& z = vZ & (uint32_t)(4096 - 1); - uint32_t idx_3d[3] = {x >> 7, y >> 7, z >> 7}; - uint64_t idx = idx_3d[2] | (idx_3d[1] << 5) | (idx_3d[0] << 10); + const auto& x = vX & (uint32_t)(4096 - 1); + const auto& y = vY & (uint32_t)(4096 - 1); + const auto& z = vZ & (uint32_t)(4096 - 1); + uint32_t idx_3d[3] = {x >> 7, y >> 7, z >> 7}; + uint64_t idx = idx_3d[2] | (idx_3d[1] << 5) | (idx_3d[0] << 10); return static_cast(idx); } static uint32_t getBitIndex3(const uint32_t& vX, const uint32_t& vY, const uint32_t& vZ) { - const auto& x = vX & (uint32_t)(128 - 1); - const auto& y = vY & (uint32_t)(128 - 1); - const auto& z = vZ & (uint32_t)(128 - 1); - uint32_t idx_3d[3] = {x >> 3, y >> 3, z >> 3}; - uint64_t idx = idx_3d[2] | (idx_3d[1] << 4) | (idx_3d[0] << 8); + const auto& x = vX & (uint32_t)(128 - 1); + const auto& y = vY & (uint32_t)(128 - 1); + const auto& z = vZ & (uint32_t)(128 - 1); + uint32_t idx_3d[3] = {x >> 3, y >> 3, z >> 3}; + uint64_t idx = idx_3d[2] | (idx_3d[1] << 4) | (idx_3d[0] << 8); return static_cast(idx); } static uint32_t getBitIndex0(const uint32_t& vX, const uint32_t& vY, const uint32_t& vZ) { - const auto& x = vX & (uint32_t)(8 - 1); - const auto& y = vY & (uint32_t)(8 - 1); - const auto& z = vZ & (uint32_t)(8 - 1); - uint32_t idx_3d[3] = {x >> 0, y >> 0, z >> 0}; - uint64_t idx = idx_3d[2] | (idx_3d[1] << 3) | (idx_3d[0] << 6); + const auto& x = vX & (uint32_t)(8 - 1); + const auto& y = vY & (uint32_t)(8 - 1); + const auto& z = vZ & (uint32_t)(8 - 1); + uint32_t idx_3d[3] = {x >> 0, y >> 0, z >> 0}; + uint64_t idx = idx_3d[2] | (idx_3d[1] << 3) | (idx_3d[0] << 6); return static_cast(idx); } @@ -159,17 +159,17 @@ template class VdbTree : public ATree { private: struct Node3 { - uint64_t mask[8] = {}; + uint64_t mask[8] = {}; std::array data[512] = {}; // data }; struct Node4 { - uint64_t mask[64] = {}; + uint64_t mask[64] = {}; std::map nodes; // loc, node }; struct Node5 { - uint64_t mask[512] = {}; + uint64_t mask[512] = {}; std::map nodes; // loc, node }; @@ -204,14 +204,14 @@ class VdbTree : public ATree { const auto& base_bit_4_idx = ((uint32_t)word5_idx++) * 64; for (; word5 != 0; word5 &= word5 - 1) { const auto& bit_4_index = base_bit_4_idx + (uint32_t)count_trailing_zeros(word5); - auto& nodes4Ref = nodes5Ref.nodes.at(bit_4_index); + auto& nodes4Ref = nodes5Ref.nodes.at(bit_4_index); writeNode4EmptyHeader(fp, &nodes4Ref); size_t word4_idx = 0; for (auto word4 : nodes4Ref.mask) { const auto& base_bit_3_idx = ((uint32_t)word4_idx++) * 64; for (; word4 != 0; word4 &= word4 - 1) { const auto& bit_3_index = base_bit_3_idx + (uint32_t)count_trailing_zeros(word4); - const auto& nodes3Ref = nodes4Ref.nodes.at(bit_3_index); + const auto& nodes3Ref = nodes4Ref.nodes.at(bit_3_index); write_data_arr(fp, nodes3Ref.mask, 8); } } @@ -222,13 +222,13 @@ class VdbTree : public ATree { const auto& base_bit_4_idx = ((uint32_t)word5_idx++) * 64; for (; word5 != 0; word5 &= word5 - 1) { const auto& bit_4_index = base_bit_4_idx + (uint32_t)count_trailing_zeros(word5); - const auto& nodes4Ref = nodes5Ref.nodes.at(bit_4_index); - size_t word4_idx = 0; + const auto& nodes4Ref = nodes5Ref.nodes.at(bit_4_index); + size_t word4_idx = 0; for (auto word4 : nodes4Ref.mask) { const auto& base_bit_3_idx = ((uint32_t)word4_idx++) * 64; for (; word4 != 0; word4 &= word4 - 1) { const auto& bit_3_index = base_bit_3_idx + (uint32_t)count_trailing_zeros(word4); - const auto& nodes3Ref = nodes4Ref.nodes.at(bit_3_index); + const auto& nodes3Ref = nodes4Ref.nodes.at(bit_3_index); write_data_arr(fp, nodes3Ref.mask, 8); write_data(fp, 6); write_data_arr>(fp, nodes3Ref.data, 512); @@ -297,8 +297,8 @@ class VdbTree : public ATree { const auto& bit_index_4 = getBitIndex4(vX, vY, vZ); const auto& bit_index_3 = getBitIndex3(vX, vY, vZ); const auto& bit_index_0 = getBitIndex0(vX, vY, vZ); - auto& nodes4Ref = m_Nodes.nodes[bit_index_4]; - auto& nodes3Ref = nodes4Ref.nodes[bit_index_3]; + auto& nodes4Ref = m_Nodes.nodes[bit_index_4]; + auto& nodes3Ref = nodes4Ref.nodes[bit_index_3]; m_Nodes.mask[bit_index_4 >> 6] |= static_cast(1) << (bit_index_4 & (64 - 1)); // active the voxel 4 nodes4Ref.mask[bit_index_3 >> 6] |= static_cast(1) << (bit_index_3 & (64 - 1)); // active the voxel 3 nodes3Ref.mask[bit_index_0 >> 6] |= static_cast(1) << (bit_index_0 & (64 - 1)); // active the voxel 0 @@ -402,9 +402,9 @@ typedef uint32_t LayerId; class VdbWriter { private: std::unordered_map>> m_Trees; - KeyFrame m_CurrentKeyFrame = 0U; - FILE* m_File = nullptr; - int32_t m_LastError = 0; + KeyFrame m_CurrentKeyFrame = 0U; + FILE* m_File = nullptr; + int32_t m_LastError = 0; public: template @@ -422,8 +422,8 @@ class VdbWriter { if (!vFilePathName.empty()) { auto dot_p = vFilePathName.find_last_of('.'); if (dot_p != std::string::npos) { - auto base_file_path_name = vFilePathName.substr(0, dot_p); - size_t idx = 1; + auto base_file_path_name = vFilePathName.substr(0, dot_p); + size_t idx = 1; for (auto& vdb : m_Trees) { std::stringstream str; if (m_Trees.size() > 1) { @@ -443,11 +443,11 @@ class VdbWriter { } // common grid types - VdbFloatGrid* getFloatLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } + VdbFloatGrid* getFloatLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } VdbDoubleGrid* getDoubleLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } - VdbVec3sGrid* getVec3sLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } - VdbVec3dGrid* getVec3dLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } - VdbVec3iGrid* getVec3iLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } + VdbVec3sGrid* getVec3sLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } + VdbVec3dGrid* getVec3dLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } + VdbVec3iGrid* getVec3iLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } VdbVec3uiGrid* getVec3uiLayer(const uint32_t& vLayerId, const std::string& vLayerName) { return getLayer(vLayerId, vLayerName); } private: @@ -470,7 +470,7 @@ class VdbWriter { #if _MSC_VER m_LastError = fopen_s(&m_File, vFilePathName.c_str(), "wb"); #else - m_File = fopen(vFilePathName.c_str(), "wb"); + m_File = fopen(vFilePathName.c_str(), "wb"); m_LastError = m_File ? 0 : errno; #endif return (m_LastError == 0); diff --git a/main.cpp b/main.cpp index 08c2f11..2bb8cc9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,8 @@ #include "VdbWriter.hpp" +#include // std::cout #include // int32_t #include // std::chrono -#include // std::cout +#include // sin #define USE_ANIMATED_WAVE // #define JULIA_REVOLUTE @@ -9,26 +10,26 @@ #ifdef USE_ANIMATED_WAVE int main() { - const int32_t SIZE = 150; - const double D_SIZE = (double)SIZE; - const int32_t OFFSET = SIZE; - const float Z_SCALE = 0.5f; - const int32_t FRAMES = 10; - const float len_ratio = 1.0f / (SIZE * SIZE); + const int32_t SIZE = 150; + const double D_SIZE = (double)SIZE; + const int32_t OFFSET = SIZE; + const float Z_SCALE = 0.5f; + const int32_t FRAMES = 10; + const float len_ratio = 1.0f / (SIZE * SIZE); vdb::VdbWriter vdb; - float r, g, b; - float time = 0.0f; + float r, g, b; + float time = 0.0f; for (int32_t f = 0; f < FRAMES; ++f) { vdb.setKeyFrame(f); auto* floatLayerPtr = vdb.getFloatLayer(0, "density"); auto* vec3sLayerPtr = vdb.getVec3sLayer(1, "color"); for (int32_t i = -SIZE; i < SIZE; ++i) { for (int32_t j = -SIZE; j < SIZE; ++j) { - float len = (i * i + j * j) * len_ratio; - int32_t pz = (int32_t)((std::sin(len * 10.0 + time) * 0.5 + 0.5) * (std::abs(50.0f - 25.0f * len)) * Z_SCALE); + float len = (i * i + j * j) * len_ratio; + int32_t pz = (int32_t)((std::sin(len * 10.0 + time) * 0.5 + 0.5) * (std::abs(50.0f - 25.0f * len)) * Z_SCALE); int32_t cube_color = (int32_t)(len * 100.0) % 255 + 1; - auto px = i + SIZE; - auto py = j + SIZE; + auto px = i + SIZE; + auto py = j + SIZE; floatLayerPtr->addVoxel(px, py, pz, 1.0f); r = sin(len * 10.0f) * 0.5f + 0.5f; g = sin(len * 7.0f) * 0.5f + 0.5f; @@ -44,35 +45,35 @@ int main() { #ifdef JULIA_REVOLUTE static double mix(const double& x, const double& y, const double& a) { return x * (1.0 - a) + y * a; } -int main() { - const int32_t SIZE = 375; - const double ZOOM_XZ = 7.5; - const double ZOOM_Y = 7.5; +int main() { + const int32_t SIZE = 375; + const double ZOOM_XZ = 7.5; + const double ZOOM_Y = 7.5; const int32_t ITERATIONS = 5; - const int32_t FRAMES = 10; + const int32_t FRAMES = 10; double time = 0.0f; double kk, hh, px, pz, an, cx, cy, path, rev_x, rev_y, tmp_x, tmp_y, rev_x_squared, rev_y_squared, df; double rot2D[4] = {1, 0, 0, 1}; // c,-s,s,c for t=0 - std::array offset = {}; - bool first_offset = true; - vdb::VdbWriter vdb; - int32_t cube_color; - double time_step = 6.28318 / (double)FRAMES; + std::array offset = {}; + bool first_offset = true; + vdb::VdbWriter vdb; + int32_t cube_color; + double time_step = 6.28318 / (double)FRAMES; for (int32_t f = 0; f < FRAMES; ++f) { vdb.setKeyFrame(f); auto* densityLayerPtr = vdb.getFloatLayer(0, "density"); - auto* sdfLayerPtr = vdb.getDoubleLayer(1, "sdf"); + auto* sdfLayerPtr = vdb.getDoubleLayer(1, "sdf"); time += time_step; for (int32_t i = -SIZE; i < SIZE; ++i) { px = ((double)i * 2.0 / (double)SIZE - 1.0) * ZOOM_XZ; for (int32_t k = -SIZE; k < SIZE; ++k) { - pz = ((double)k * 2.0 / (double)SIZE - 1.0) * ZOOM_XZ; - an = std::atan2(px, pz); - cx = mix(0.2, -0.5, std::sin(an * 2.0)); - cy = mix(0.5, 0.0, std::sin(an * 3.0)); - path = sqrt(px * px + pz * pz) - 3.0; + pz = ((double)k * 2.0 / (double)SIZE - 1.0) * ZOOM_XZ; + an = std::atan2(px, pz); + cx = mix(0.2, -0.5, std::sin(an * 2.0)); + cy = mix(0.5, 0.0, std::sin(an * 3.0)); + path = sqrt(px * px + pz * pz) - 3.0; rot2D[0] = std::cos(an + time); rot2D[1] = -std::sin(an + time); rot2D[2] = std::sin(an + time); @@ -82,8 +83,8 @@ int main() { tmp_x = path; rev_x = rot2D[0] * tmp_x + rot2D[1] * tmp_y; // rx2d rev_y = rot2D[2] * tmp_x + rot2D[3] * tmp_y; // ry2d - kk = 1.0; - hh = 1.0; + kk = 1.0; + hh = 1.0; for (int32_t idx = 0; idx < ITERATIONS; ++idx) { rev_x_squared = rev_x * rev_x; rev_y_squared = rev_y * rev_y; @@ -99,9 +100,9 @@ int main() { if (df < 0.0) { if (first_offset) { first_offset = false; - offset[0] = i; - offset[1] = k; - offset[2] = j; + offset[0] = i; + offset[1] = k; + offset[2] = j; } cube_color = (int32_t)((std::sin(rev_x + rev_y) * 0.5 + 0.5) * 6.0) + 249; densityLayerPtr->addVoxel(i + SIZE - offset[0], k + SIZE - offset[1], j + SIZE - offset[2], 1.0f); @@ -118,21 +119,21 @@ int main() { #ifdef USE_Vdb_WRITER int main() { vdb::VdbWriter vdb; - auto* floatLayerPtr = vdb.getFloatLayer(0, "density"); - const uint32_t R = 128; - const uint32_t D = R * 2; + auto* floatLayerPtr = vdb.getFloatLayer(0, "density"); + const uint32_t R = 128; + const uint32_t D = R * 2; for (uint32_t z = 0; z < D; ++z) { for (uint32_t y = 0; y < D; ++y) { for (uint32_t x = 0; x < D; ++x) { - const auto& px = x - R; - const auto& py = y - R; - const auto& pz = z - R; + const auto& px = x - R; + const auto& py = y - R; + const auto& pz = z - R; const auto& length_squared = px * px + py * py + pz * pz; if (length_squared < R * R) { floatLayerPtr->addVoxel(x, y, z, 1.0f); - float fx = (float)px; - float fy = (float)py; - float fz = (float)pz; + float fx = (float)px; + float fy = (float)py; + float fz = (float)pz; const auto& len = sqrtf(fx * fx + fy * fy + fz * fz); fx /= len; fy /= len;