Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
edisonlee0212 committed Sep 17, 2024
1 parent feeeed5 commit aad2830
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 74 deletions.
70 changes: 37 additions & 33 deletions EvoEngine_Plugins/EcoSysLab/include/Structures/StrandGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/**
Expand Down Expand Up @@ -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.
Expand All @@ -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);
};

/**
Expand All @@ -88,9 +81,7 @@ class Strand {
friend class StrandGroup;
template <typename Sgd, typename Sd, typename Ssd>
friend class StrandGroupSerializer;
StrandHandle handle_ = -1;

std::vector<StrandSegmentHandle> strand_segment_handles_;
std::vector<StrandSegmentHandle> strand_segment_handles_{};

public:
/**
Expand All @@ -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<StrandSegmentHandle>& PeekStrandSegmentHandles() const;
Strand() = default;
explicit Strand(StrandHandle handle);
};

/**
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -336,8 +334,8 @@ void StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::BuildStrand(co
template <typename StrandGroupData, typename StrandData, typename StrandSegmentData>
StrandSegmentHandle StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::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) {
Expand All @@ -354,8 +352,8 @@ StrandSegmentHandle StrandGroup<StrandGroupData, StrandData, StrandSegmentData>:

template <typename StrandGroupData, typename StrandData, typename StrandSegmentData>
StrandHandle StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::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;
Expand Down Expand Up @@ -391,12 +389,13 @@ void StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::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);
}
Expand Down Expand Up @@ -453,7 +452,6 @@ void StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::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();
}

Expand Down Expand Up @@ -494,7 +492,6 @@ void StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::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();
Expand Down Expand Up @@ -605,6 +602,13 @@ glm::vec3 StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::GetStrand
return strands_[segment.strand_handle_].start_position;
}

template <typename StrandGroupData, typename StrandData, typename StrandSegmentData>
glm::vec3 StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::GetStrandSegmentCenter(
const StrandSegmentHandle handle) const {
const auto start_position = GetStrandSegmentStart(handle);
return (start_position + strand_segments_[handle].end_position) * 0.5f;
}

template <typename StrandGroupData, typename StrandData, typename StrandSegmentData>
float StrandGroup<StrandGroupData, StrandData, StrandSegmentData>::GetStrandSegmentLength(
const StrandSegmentHandle handle) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ void StrandGroupSerializer<StrandGroupData, StrandData, StrandSegmentData>::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<YAML::Binary>();
strand.strand_segment_handles_.resize(segment_handles.size() / sizeof(StrandSegmentHandle));
Expand Down Expand Up @@ -249,7 +248,6 @@ void StrandGroupSerializer<StrandGroupData, StrandData, StrandSegmentData>::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++;
}
Expand Down
19 changes: 2 additions & 17 deletions EvoEngine_Plugins/EcoSysLab/src/StrandGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand All @@ -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<StrandSegmentHandle>& Strand::PeekStrandSegmentHandles() const {
return strand_segment_handles_;
}

Strand::Strand(const StrandHandle handle) {
handle_ = handle;
strand_segment_handles_.clear();
}
44 changes: 22 additions & 22 deletions EvoEngine_Plugins/EcoSysLab/src/StrandModelMeshGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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<int>(distance / subdivision_length));

Expand All @@ -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<float>(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&) {
});
Expand Down Expand Up @@ -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);
Expand All @@ -1879,18 +1879,18 @@ void StrandModelMeshGenerator::CalculateUv(const StrandModel& strand_model, std:
VoxelGrid<std::vector<StrandSegmentHandle>> 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) {
Expand All @@ -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;
}
}
});
Expand Down

0 comments on commit aad2830

Please sign in to comment.