From aad2830350f30a3e4db39c937cf09d8bf7ef9267 Mon Sep 17 00:00:00 2001 From: Bosheng Li Date: Tue, 17 Sep 2024 14:35:38 -0400 Subject: [PATCH] Checkpoint. --- .../include/Structures/StrandGroup.hpp | 70 ++++++++++--------- .../Structures/StrandGroupSerializer.hpp | 2 - .../EcoSysLab/src/StrandGroup.cpp | 19 +---- .../src/StrandModelMeshGenerator.cpp | 44 ++++++------ 4 files changed, 61 insertions(+), 74 deletions(-) diff --git a/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroup.hpp b/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroup.hpp index 3d0f3de..b2221d2 100644 --- a/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroup.hpp +++ b/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroup.hpp @@ -18,12 +18,11 @@ class StrandSegment { friend class StrandGroupSerializer; StrandSegmentHandle prev_handle_ = -1; - StrandSegmentHandle handle_ = -1; StrandSegmentHandle next_handle_ = -1; StrandHandle strand_handle_ = -1; - int index_ = -1; + uint32_t index_ = -1; public: /** @@ -51,12 +50,6 @@ class StrandSegment { */ [[nodiscard]] bool IsEnd() const; - /** - * Get the handle of self. - * @return strandSegmentHandle of current segment. - */ - [[nodiscard]] StrandSegmentHandle GetHandle() const; - /** * Get the handle of belonged strand. * @return strandHandle of current segment. @@ -74,9 +67,9 @@ class StrandSegment { */ [[nodiscard]] StrandSegmentHandle GetNextHandle() const; - [[nodiscard]] int GetIndex() const; + [[nodiscard]] uint32_t GetIndex() const; StrandSegment() = default; - explicit StrandSegment(StrandHandle strand_handle, StrandSegmentHandle handle, StrandSegmentHandle prev_handle); + explicit StrandSegment(StrandHandle strand_handle, StrandSegmentHandle prev_handle); }; /** @@ -88,9 +81,7 @@ class Strand { friend class StrandGroup; template friend class StrandGroupSerializer; - StrandHandle handle_ = -1; - - std::vector strand_segment_handles_; + std::vector strand_segment_handles_{}; public: /** @@ -106,19 +97,11 @@ class Strand { * \brief The color of the [[[START]]] current strand segment. */ glm::vec4 start_color = glm::vec4(1.0f); - /** - * Get the handle of self. - * @return strandSegmentHandle of current segment. - */ - [[nodiscard]] StrandHandle GetHandle() const; - /** * Access the segments that belongs to this flow. * @return The list of handles. */ [[nodiscard]] const std::vector& PeekStrandSegmentHandles() const; - Strand() = default; - explicit Strand(StrandHandle handle); }; /** @@ -285,8 +268,23 @@ class StrandGroup { * @return The version */ [[nodiscard]] int GetVersion() const; - + /** + * \brief Get the start position of a specific strand segment. + * \param handle The handle of the strand segment. + * \return The start position of a specific strand segment. + */ [[nodiscard]] glm::vec3 GetStrandSegmentStart(StrandSegmentHandle handle) const; + /** + * \brief Get the end position of a specific strand segment. + * \param handle The handle of the strand segment. + * \return The end position of a specific strand segment. + */ + [[nodiscard]] glm::vec3 GetStrandSegmentCenter(StrandSegmentHandle handle) const; + /** + * \brief Get the length of a specific strand segment. + * \param handle The handle of the strand segment. + * \return The length of a specific strand segment. + */ [[nodiscard]] float GetStrandSegmentLength(StrandSegmentHandle handle) const; }; @@ -336,8 +334,8 @@ void StrandGroup::BuildStrand(co template StrandSegmentHandle StrandGroup::AllocateStrandSegment( StrandHandle strand_handle, StrandSegmentHandle prev_handle, const int index) { - StrandSegmentHandle new_segment_handle = strand_segments_.size(); - strand_segments_.emplace_back(strand_handle, new_segment_handle, prev_handle); + const StrandSegmentHandle new_segment_handle = strand_segments_.size(); + strand_segments_.emplace_back(strand_handle, prev_handle); strand_segments_data_list.emplace_back(); auto& segment = strand_segments_[new_segment_handle]; if (prev_handle != -1) { @@ -354,8 +352,8 @@ StrandSegmentHandle StrandGroup: template StrandHandle StrandGroup::AllocateStrand() { - StrandHandle new_strand_handle = strands_.size(); - strands_.emplace_back(new_strand_handle); + const StrandHandle new_strand_handle = strands_.size(); + strands_.emplace_back(); strands_data_list.emplace_back(); version_++; return new_strand_handle; @@ -391,12 +389,13 @@ void StrandGroup::BuildParticles particle_infos.reserve(strand_segments_.size()); for (const auto& strand : strands_) { for (uint32_t i = 0; i < strand.strand_segment_handles_.size(); i++) { - const auto& segment = strand_segments_[strand.strand_segment_handles_[i]]; + const auto& strand_segment_handle = strand.strand_segment_handles_[i]; + const auto& segment = strand_segments_[strand_segment_handle]; ParticleInfo particle_info; - particle_info.instance_matrix.value = - glm::translate(segment.end_position) * glm::mat4_cast(segment.rotation) * - glm::scale(glm::vec3(segment.end_thickness, segment.end_thickness, - GetStrandSegmentLength(strand.strand_segment_handles_[i]))); + particle_info.instance_matrix.value = glm::translate(GetStrandSegmentCenter(strand_segment_handle)) * + glm::mat4_cast(segment.rotation) * + glm::scale(glm::vec3(segment.end_thickness, segment.end_thickness, + GetStrandSegmentLength(strand_segment_handle))); particle_info.instance_color = segment.end_color; particle_infos.emplace_back(particle_info); } @@ -453,7 +452,6 @@ void StrandGroup::RemoveSingleSt } strand_segments_[target_segment_handle] = last_segment; - strand_segments_[target_segment_handle].handle_ = target_segment_handle; strand_segments_data_list[target_segment_handle] = strand_segments_data_list.back(); } @@ -494,7 +492,6 @@ void StrandGroup::RemoveStrand(S const auto& last_strand = strands_.back(); strand.strand_segment_handles_ = last_strand.strand_segment_handles_; strand = last_strand; - strand.handle_ = target_strand_handle; strands_data_list[target_strand_handle] = strands_data_list.back(); } strands_.pop_back(); @@ -605,6 +602,13 @@ glm::vec3 StrandGroup::GetStrand return strands_[segment.strand_handle_].start_position; } +template +glm::vec3 StrandGroup::GetStrandSegmentCenter( + const StrandSegmentHandle handle) const { + const auto start_position = GetStrandSegmentStart(handle); + return (start_position + strand_segments_[handle].end_position) * 0.5f; +} + template float StrandGroup::GetStrandSegmentLength( const StrandSegmentHandle handle) const { diff --git a/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroupSerializer.hpp b/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroupSerializer.hpp index 5f3f0b2..35dfeb6 100644 --- a/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroupSerializer.hpp +++ b/EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroupSerializer.hpp @@ -137,7 +137,6 @@ void StrandGroupSerializer::Dese for (const auto& in_strand : in_strands) { strand_group.strands_.emplace_back(); auto& strand = strand_group.strands_.back(); - strand.handle_ = strand_handle; if (in_strand["strand_segment_handles_"]) { const auto segment_handles = in_strand["strand_segment_handles_"].as(); strand.strand_segment_handles_.resize(segment_handles.size() / sizeof(StrandSegmentHandle)); @@ -249,7 +248,6 @@ void StrandGroupSerializer::Dese StrandSegmentHandle strand_segment_handle = 0; for (const auto& in_strand_segment : in_strand_segments) { auto& strand_segment = strand_group.strand_segments_.at(strand_segment_handle); - strand_segment.handle_ = strand_segment_handle; strand_segment_func(in_strand_segment, strand_group.strand_segments_data_list[strand_segment_handle]); strand_segment_handle++; } diff --git a/EvoEngine_Plugins/EcoSysLab/src/StrandGroup.cpp b/EvoEngine_Plugins/EcoSysLab/src/StrandGroup.cpp index 8408a0e..875ea2c 100644 --- a/EvoEngine_Plugins/EcoSysLab/src/StrandGroup.cpp +++ b/EvoEngine_Plugins/EcoSysLab/src/StrandGroup.cpp @@ -6,10 +6,6 @@ bool StrandSegment::IsEnd() const { return next_handle_ == -1; } -StrandSegmentHandle StrandSegment::GetHandle() const { - return handle_; -} - StrandHandle StrandSegment::GetStrandHandle() const { return strand_handle_; } @@ -22,29 +18,18 @@ StrandSegmentHandle StrandSegment::GetNextHandle() const { return next_handle_; } -int StrandSegment::GetIndex() const { +uint32_t StrandSegment::GetIndex() const { return index_; } -StrandSegment::StrandSegment(const StrandHandle strand_handle, const StrandSegmentHandle handle, - const StrandSegmentHandle prev_handle) { +StrandSegment::StrandSegment(const StrandHandle strand_handle, const StrandSegmentHandle prev_handle) { strand_handle_ = strand_handle; - handle_ = handle; prev_handle_ = prev_handle; next_handle_ = -1; index_ = -1; } -StrandHandle Strand::GetHandle() const { - return handle_; -} - const std::vector& Strand::PeekStrandSegmentHandles() const { return strand_segment_handles_; -} - -Strand::Strand(const StrandHandle handle) { - handle_ = handle; - strand_segment_handles_.clear(); } \ No newline at end of file diff --git a/EvoEngine_Plugins/EcoSysLab/src/StrandModelMeshGenerator.cpp b/EvoEngine_Plugins/EcoSysLab/src/StrandModelMeshGenerator.cpp index afbe922..84ebac6 100644 --- a/EvoEngine_Plugins/EcoSysLab/src/StrandModelMeshGenerator.cpp +++ b/EvoEngine_Plugins/EcoSysLab/src/StrandModelMeshGenerator.cpp @@ -1392,7 +1392,7 @@ void StrandModelMeshGenerator::RecursiveSlicing(const StrandModel& strand_model, const auto& skeleton = strand_model.strand_model_skeleton; const auto& pipe_group = skeleton.data.strand_group; - if (pipe_group.PeekStrands().size() == 0) { + if (pipe_group.PeekStrands().empty()) { return; } @@ -1416,8 +1416,8 @@ void StrandModelMeshGenerator::RecursiveSlicing(const StrandModel& strand_model, // prepare initial pipe cluster: PipeCluster pipe_cluster; - for (size_t i = 0; i < pipe_group.PeekStrands().size(); i++) { - pipe_cluster.push_back(pipe_group.PeekStrands()[i].GetHandle()); + for (StrandHandle strand_handle = 0; strand_handle < pipe_group.PeekStrands().size(); strand_handle++) { + pipe_cluster.push_back(strand_handle); } float step_size = 1.0f / settings.steps_per_segment; @@ -1519,18 +1519,18 @@ void StrandModelMeshGenerator::MarchingCube(const StrandModel& strand_model, std } float subdivision_length = settings.marching_cube_radius * 0.5f; - for (uint32_t strand_segment_index = 0; strand_segment_index < strand_group.PeekStrandSegments().size(); - strand_segment_index++) { - const auto& strand_segment = strand_group.PeekStrandSegment(strand_segment_index); - const auto& strand_segment_data = strand_group.PeekStrandSegmentData(strand_segment_index); + for (StrandSegmentHandle strand_segment_handle = 0; strand_segment_handle < strand_group.PeekStrandSegments().size(); + strand_segment_handle++) { + const auto& strand_segment = strand_group.PeekStrandSegment(strand_segment_handle); + const auto& strand_segment_data = strand_group.PeekStrandSegmentData(strand_segment_handle); const auto& node = skeleton.PeekNode(strand_segment_data.node_handle); const auto& profile = node.data.profile; if (profile.PeekParticles().size() < settings.min_cell_count_for_major_branches) continue; // Get interpolated position on pipe segment. Example to get middle point here: - const auto start_position = strand_model.InterpolateStrandSegmentPosition(strand_segment.GetHandle(), 0.0f); - const auto end_position = strand_model.InterpolateStrandSegmentPosition(strand_segment.GetHandle(), 1.0f); + const auto start_position = strand_model.InterpolateStrandSegmentPosition(strand_segment_handle, 0.0f); + const auto end_position = strand_model.InterpolateStrandSegmentPosition(strand_segment_handle, 1.0f); const auto distance = glm::distance(start_position, end_position); const auto step_size = glm::max(1, static_cast(distance / subdivision_length)); @@ -1539,7 +1539,7 @@ void StrandModelMeshGenerator::MarchingCube(const StrandModel& strand_model, std glm::radians(360.0f); for (int step = 0; step < step_size; step++) { const auto a = static_cast(step) / step_size; - const auto position = strand_model.InterpolateStrandSegmentPosition(strand_segment.GetHandle(), a); + const auto position = strand_model.InterpolateStrandSegmentPosition(strand_segment_handle, a); octree.Occupy(position, [&](OctreeNode&) { }); @@ -1858,16 +1858,16 @@ void StrandModelMeshGenerator::CalculateUv(const StrandModel& strand_model, std: const auto& strand_group = strand_model.strand_model_skeleton.data.strand_group; auto min = glm::vec3(FLT_MAX); auto max = glm::vec3(FLT_MIN); - for (uint32_t strand_segment_index = 0; strand_segment_index < strand_group.PeekStrandSegments().size(); - strand_segment_index++) { - const auto& strand_segment = strand_group.PeekStrandSegment(strand_segment_index); - const auto& strand_segment_data = strand_group.PeekStrandSegmentData(strand_segment_index); + for (StrandSegmentHandle strand_segment_handle = 0; strand_segment_handle < strand_group.PeekStrandSegments().size(); + strand_segment_handle++) { + const auto& strand_segment = strand_group.PeekStrandSegment(strand_segment_handle); + const auto& strand_segment_data = strand_group.PeekStrandSegmentData(strand_segment_handle); const auto& node = strand_model.strand_model_skeleton.PeekNode(strand_segment_data.node_handle); const auto& profile = node.data.profile; if (profile.PeekParticles().size() < settings.min_cell_count_for_major_branches) continue; - const auto segment_start = strand_group.GetStrandSegmentStart(strand_segment.GetHandle()); + const auto segment_start = strand_group.GetStrandSegmentStart(strand_segment_handle); const auto segment_end = strand_segment.end_position; min = glm::min(segment_start, min); min = glm::min(segment_start, min); @@ -1879,18 +1879,18 @@ void StrandModelMeshGenerator::CalculateUv(const StrandModel& strand_model, std: VoxelGrid> boundary_segments; boundary_segments.Initialize(0.01f, min, max, {}); - for (uint32_t strand_segment_index = 0; strand_segment_index < strand_group.PeekStrandSegments().size(); - strand_segment_index++) { - const auto& strand_segment = strand_group.PeekStrandSegment(strand_segment_index); - const auto& strand_segment_data = strand_group.PeekStrandSegmentData(strand_segment_index); + for (StrandSegmentHandle strand_segment_handle = 0; strand_segment_handle < strand_group.PeekStrandSegments().size(); + strand_segment_handle++) { + const auto& strand_segment = strand_group.PeekStrandSegment(strand_segment_handle); + const auto& strand_segment_data = strand_group.PeekStrandSegmentData(strand_segment_handle); const auto& node = strand_model.strand_model_skeleton.PeekNode(strand_segment_data.node_handle); const auto& profile = node.data.profile; if (profile.PeekParticles().size() < settings.min_cell_count_for_major_branches) continue; - const auto segment_start = strand_group.GetStrandSegmentStart(strand_segment.GetHandle()); + const auto segment_start = strand_group.GetStrandSegmentStart(strand_segment_handle); const auto segment_end = strand_segment.end_position; - boundary_segments.Ref((segment_start + segment_end) * 0.5f).emplace_back(strand_segment.GetHandle()); + boundary_segments.Ref((segment_start + segment_end) * 0.5f).emplace_back(strand_segment_handle); } Jobs::RunParallelFor(vertices.size(), [&](unsigned vertex_index) { @@ -1909,7 +1909,7 @@ void StrandModelMeshGenerator::CalculateUv(const StrandModel& strand_model, std: if (const auto current_distance = glm::distance(segment_end, vertex.position); current_distance < min_distance) { min_distance = current_distance; - closest_segment_handle = segment.GetHandle(); + closest_segment_handle = segment_handle; } } });