Skip to content

Commit 339d0af

Browse files
committed
Add remove geometric loops option for PathMeanderize node
1 parent 5ecb2d8 commit 339d0af

File tree

2 files changed

+81
-24
lines changed

2 files changed

+81
-24
lines changed

Hesiod/src/model/nodes/nodes_function/path_meanderize.cpp

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,103 @@ using namespace attr;
1414
namespace hesiod
1515
{
1616

17+
// -----------------------------------------------------------------------------
18+
// Ports & Attributes
19+
// -----------------------------------------------------------------------------
20+
21+
constexpr const char *P_INPUT = "input";
22+
constexpr const char *P_OUTPUT = "output";
23+
24+
constexpr const char *A_RATIO = "ratio";
25+
constexpr const char *A_NOISE_RATIO = "noise_ratio";
26+
constexpr const char *A_SEED = "seed";
27+
constexpr const char *A_ITERATIONS = "iterations";
28+
constexpr const char *A_EDGE_DIVISIONS = "edge_divisions";
29+
constexpr const char *A_REMOVE_LOOPS = "remove_loops";
30+
31+
// -----------------------------------------------------------------------------
32+
// Setup
33+
// -----------------------------------------------------------------------------
34+
1735
void setup_path_meanderize_node(BaseNode &node)
1836
{
1937
Logger::log()->trace("setup node {}", node.get_label());
2038

2139
// port(s)
22-
node.add_port<hmap::Path>(gnode::PortType::IN, "input");
23-
node.add_port<hmap::Path>(gnode::PortType::OUT, "output");
40+
node.add_port<hmap::Path>(gnode::PortType::IN, P_INPUT);
41+
node.add_port<hmap::Path>(gnode::PortType::OUT, P_OUTPUT);
2442

2543
// attribute(s)
26-
node.add_attr<FloatAttribute>("ratio", "ratio", 0.2f, 0.f, 1.f);
27-
node.add_attr<FloatAttribute>("noise_ratio", "noise_ratio", 0.1f, 0.f, 1.f);
28-
node.add_attr<SeedAttribute>("seed", "Seed");
29-
node.add_attr<IntAttribute>("iterations", "iterations", 2, 1, 8);
30-
node.add_attr<IntAttribute>("edge_divisions", "edge_divisions", 10, 1, 32);
44+
node.add_attr<FloatAttribute>(A_RATIO, "Meander Ratio", 0.2f, 0.f, 1.f);
45+
node.add_attr<FloatAttribute>(A_NOISE_RATIO, "Noise Ratio", 0.1f, 0.f, 1.f);
46+
node.add_attr<SeedAttribute>(A_SEED, "Seed");
47+
node.add_attr<IntAttribute>(A_ITERATIONS, "Solver Iterations", 2, 1, 8);
48+
node.add_attr<IntAttribute>(A_EDGE_DIVISIONS, "Edge Divisions", 10, 1, 32);
49+
node.add_attr<BoolAttribute>(A_REMOVE_LOOPS, "Remove Geometric Loops", false);
3150

3251
// attribute(s) order
33-
node.set_attr_ordered_key(
34-
{"ratio", "noise_ratio", "seed", "iterations", "edge_divisions"});
52+
node.set_attr_ordered_key({"_GROUPBOX_BEGIN_Meander Parameters",
53+
A_RATIO,
54+
A_NOISE_RATIO,
55+
A_SEED,
56+
A_REMOVE_LOOPS,
57+
"_GROUPBOX_END_",
58+
//
59+
"_GROUPBOX_BEGIN_Solver",
60+
A_ITERATIONS,
61+
A_EDGE_DIVISIONS,
62+
"_GROUPBOX_END_"});
3563
}
3664

65+
// -----------------------------------------------------------------------------
66+
// Compute
67+
// -----------------------------------------------------------------------------
68+
3769
void compute_path_meanderize_node(BaseNode &node)
3870
{
3971
Logger::log()->trace("computing node [{}]/[{}]", node.get_label(), node.get_id());
4072

41-
hmap::Path *p_in = node.get_value_ref<hmap::Path>("input");
73+
hmap::Path *p_in = node.get_value_ref<hmap::Path>(P_INPUT);
74+
hmap::Path *p_out = node.get_value_ref<hmap::Path>(P_OUTPUT);
75+
76+
if (!p_in || p_in->get_npoints() < 2)
77+
return;
4278

43-
if (p_in)
79+
// --- Parameters wrapper
80+
81+
const auto params = [&node]()
4482
{
45-
hmap::Path *p_out = node.get_value_ref<hmap::Path>("output");
46-
47-
// copy the input heightmap
48-
*p_out = *p_in;
49-
50-
if (p_in->get_npoints() > 1)
51-
p_out->meanderize(node.get_attr<FloatAttribute>("ratio"),
52-
node.get_attr<FloatAttribute>("noise_ratio"),
53-
node.get_attr<SeedAttribute>("seed"),
54-
node.get_attr<IntAttribute>("iterations"),
55-
node.get_attr<IntAttribute>("edge_divisions"));
56-
}
83+
struct P
84+
{
85+
float ratio;
86+
float noise_ratio;
87+
uint seed;
88+
int iterations;
89+
int edge_divisions;
90+
bool remove_loops;
91+
};
92+
return P{
93+
.ratio = node.get_attr<FloatAttribute>(A_RATIO),
94+
.noise_ratio = node.get_attr<FloatAttribute>(A_NOISE_RATIO),
95+
.seed = node.get_attr<SeedAttribute>(A_SEED),
96+
.iterations = node.get_attr<IntAttribute>(A_ITERATIONS),
97+
.edge_divisions = node.get_attr<IntAttribute>(A_EDGE_DIVISIONS),
98+
.remove_loops = node.get_attr<BoolAttribute>(A_REMOVE_LOOPS),
99+
};
100+
}();
101+
102+
// --- Apply meanderize
103+
104+
*p_out = *p_in;
105+
106+
p_out->meanderize(params.ratio,
107+
params.noise_ratio,
108+
params.seed,
109+
params.iterations,
110+
params.edge_divisions);
111+
112+
if (params.remove_loops)
113+
*p_out = hmap::remove_geometric_loops(*p_out);
57114
}
58115

59116
} // namespace hesiod

0 commit comments

Comments
 (0)