Skip to content

Commit 5d161b3

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

File tree

1 file changed

+76
-23
lines changed

1 file changed

+76
-23
lines changed

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

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,99 @@ 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_ITERATIONS = "iterations";
25+
constexpr const char *A_SEED = "seed";
26+
constexpr const char *A_SIGMA = "sigma";
27+
constexpr const char *A_ORIENTATION = "orientation";
28+
constexpr const char *A_PERSISTENCE = "persistence";
29+
constexpr const char *A_REMOVE_LOOPS = "remove_loops";
30+
31+
// -----------------------------------------------------------------------------
32+
// Setup
33+
// -----------------------------------------------------------------------------
34+
1735
void setup_path_fractalize_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<IntAttribute>("iterations", "iterations", 4, 1, 10);
27-
node.add_attr<SeedAttribute>("seed", "Seed");
28-
node.add_attr<FloatAttribute>("sigma", "sigma", 0.3f, 0.f, 1.f);
29-
node.add_attr<IntAttribute>("orientation", "orientation", 0, 0, 1);
30-
node.add_attr<FloatAttribute>("persistence", "Persistence", 1.f, 0.01f, 4.f);
44+
node.add_attr<IntAttribute>(A_ITERATIONS, "Iterations", 4, 1, 10);
45+
node.add_attr<SeedAttribute>(A_SEED, "Random Seed");
46+
node.add_attr<FloatAttribute>(A_SIGMA, "Sigma", 0.3f, 0.f, 1.f);
47+
node.add_attr<IntAttribute>(A_ORIENTATION, "Orientation", 0, 0, 1);
48+
node.add_attr<FloatAttribute>(A_PERSISTENCE, "Persistence", 1.f, 0.01f, 4.f);
49+
node.add_attr<BoolAttribute>(A_REMOVE_LOOPS, "Remove Geometric Loops", false);
3150

3251
// attribute(s) order
33-
node.set_attr_ordered_key(
34-
{"iterations", "seed", "sigma", "orientation", "persistence"});
52+
node.set_attr_ordered_key({"_GROUPBOX_BEGIN_Fractal Parameters",
53+
A_ITERATIONS,
54+
A_SEED,
55+
A_SIGMA,
56+
A_ORIENTATION,
57+
A_PERSISTENCE,
58+
A_REMOVE_LOOPS,
59+
"_GROUPBOX_END_"});
3560
}
3661

62+
// -----------------------------------------------------------------------------
63+
// Compute
64+
// -----------------------------------------------------------------------------
65+
3766
void compute_path_fractalize_node(BaseNode &node)
3867
{
3968
Logger::log()->trace("computing node [{}]/[{}]", node.get_label(), node.get_id());
4069

41-
hmap::Path *p_in = node.get_value_ref<hmap::Path>("input");
70+
hmap::Path *p_in = node.get_value_ref<hmap::Path>(P_INPUT);
71+
hmap::Path *p_out = node.get_value_ref<hmap::Path>(P_OUTPUT);
72+
73+
if (!p_in || p_in->get_npoints() < 2)
74+
return;
4275

43-
if (p_in)
76+
// --- Parameters wrapper
77+
78+
const auto params = [&node]()
4479
{
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->fractalize(node.get_attr<IntAttribute>("iterations"),
52-
node.get_attr<SeedAttribute>("seed"),
53-
node.get_attr<FloatAttribute>("sigma"),
54-
node.get_attr<IntAttribute>("orientation"),
55-
node.get_attr<FloatAttribute>("persistence"));
56-
}
80+
struct P
81+
{
82+
int iterations;
83+
uint seed;
84+
float sigma;
85+
int orientation;
86+
float persistence;
87+
bool remove_loops;
88+
};
89+
90+
return P{.iterations = node.get_attr<IntAttribute>(A_ITERATIONS),
91+
.seed = node.get_attr<SeedAttribute>(A_SEED),
92+
.sigma = node.get_attr<FloatAttribute>(A_SIGMA),
93+
.orientation = node.get_attr<IntAttribute>(A_ORIENTATION),
94+
.persistence = node.get_attr<FloatAttribute>(A_PERSISTENCE),
95+
.remove_loops = node.get_attr<BoolAttribute>(A_REMOVE_LOOPS)};
96+
}();
97+
98+
// --- Apply fractalize
99+
100+
*p_out = *p_in;
101+
102+
p_out->fractalize(params.iterations,
103+
params.seed,
104+
params.sigma,
105+
params.orientation,
106+
params.persistence);
107+
108+
if (params.remove_loops)
109+
*p_out = hmap::remove_geometric_loops(*p_out);
57110
}
58111

59112
} // namespace hesiod

0 commit comments

Comments
 (0)