1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "UI_interface.h"
#include "UI_resources.h"
#include "node_geometry_util.hh"
namespace blender::nodes::node_geo_legacy_attribute_clamp_cc {
static void geo_node_attribute_clamp_declare(NodeDeclarationBuilder &b)
{
b.add_input<decl::Geometry>(N_("Geometry"));
b.add_input<decl::String>(N_("Attribute"));
b.add_input<decl::String>(N_("Result"));
b.add_input<decl::Vector>(N_("Min"));
b.add_input<decl::Vector>(N_("Max")).default_value({1.0f, 1.0f, 1.0f});
b.add_input<decl::Float>(N_("Min"), "Min_001");
b.add_input<decl::Float>(N_("Max"), "Max_001").default_value(1.0f);
b.add_input<decl::Int>(N_("Min"), "Min_002").min(-100000).max(100000);
b.add_input<decl::Int>(N_("Max"), "Max_002").default_value(100).min(-100000).max(100000);
b.add_input<decl::Color>(N_("Min"), "Min_003").default_value({0.5f, 0.5f, 0.5f, 1.0f});
b.add_input<decl::Color>(N_("Max"), "Max_003").default_value({0.5f, 0.5f, 0.5f, 1.0f});
b.add_output<decl::Geometry>(N_("Geometry"));
}
static void geo_node_attribute_clamp_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiItemR(layout, ptr, "data_type", 0, "", ICON_NONE);
uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
}
static void geo_node_attribute_clamp_init(bNodeTree *UNUSED(tree), bNode *node)
{
NodeAttributeClamp *data = (NodeAttributeClamp *)MEM_callocN(sizeof(NodeAttributeClamp),
__func__);
data->data_type = CD_PROP_FLOAT;
data->operation = NODE_CLAMP_MINMAX;
node->storage = data;
}
static void geo_node_attribute_clamp_update(bNodeTree *ntree, bNode *node)
{
bNodeSocket *sock_min_vector = (bNodeSocket *)BLI_findlink(&node->inputs, 3);
bNodeSocket *sock_max_vector = sock_min_vector->next;
bNodeSocket *sock_min_float = sock_max_vector->next;
bNodeSocket *sock_max_float = sock_min_float->next;
bNodeSocket *sock_min_int = sock_max_float->next;
bNodeSocket *sock_max_int = sock_min_int->next;
bNodeSocket *sock_min_color = sock_max_int->next;
bNodeSocket *sock_max_color = sock_min_color->next;
const NodeAttributeClamp &storage = *(const NodeAttributeClamp *)node->storage;
const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
nodeSetSocketAvailability(ntree, sock_min_vector, data_type == CD_PROP_FLOAT3);
nodeSetSocketAvailability(ntree, sock_max_vector, data_type == CD_PROP_FLOAT3);
nodeSetSocketAvailability(ntree, sock_min_float, data_type == CD_PROP_FLOAT);
nodeSetSocketAvailability(ntree, sock_max_float, data_type == CD_PROP_FLOAT);
nodeSetSocketAvailability(ntree, sock_min_int, data_type == CD_PROP_INT32);
nodeSetSocketAvailability(ntree, sock_max_int, data_type == CD_PROP_INT32);
nodeSetSocketAvailability(ntree, sock_min_color, data_type == CD_PROP_COLOR);
nodeSetSocketAvailability(ntree, sock_max_color, data_type == CD_PROP_COLOR);
}
template<typename T> T clamp_value(const T val, const T min, const T max);
template<> inline float clamp_value(const float val, const float min, const float max)
{
return std::min(std::max(val, min), max);
}
template<> inline int clamp_value(const int val, const int min, const int max)
{
return std::min(std::max(val, min), max);
}
template<> inline float3 clamp_value(const float3 val, const float3 min, const float3 max)
{
float3 tmp;
tmp.x = std::min(std::max(val.x, min.x), max.x);
tmp.y = std::min(std::max(val.y, min.y), max.y);
tmp.z = std::min(std::max(val.z, min.z), max.z);
return tmp;
}
template<>
inline ColorGeometry4f clamp_value(const ColorGeometry4f val,
const ColorGeometry4f min,
const ColorGeometry4f max)
{
ColorGeometry4f tmp;
tmp.r = std::min(std::max(val.r, min.r), max.r);
tmp.g = std::min(std::max(val.g, min.g), max.g);
tmp.b = std::min(std::max(val.b, min.b), max.b);
tmp.a = std::min(std::max(val.a, min.a), max.a);
return tmp;
}
template<typename T>
static void clamp_attribute(const VArray<T> &inputs,
const MutableSpan<T> outputs,
const T min,
const T max)
{
for (const int i : IndexRange(outputs.size())) {
outputs[i] = clamp_value<T>(inputs[i], min, max);
}
}
static AttributeDomain get_result_domain(const GeometryComponent &component,
StringRef source_name,
StringRef result_name)
{
std::optional<AttributeMetaData> result_info = component.attribute_get_meta_data(result_name);
if (result_info) {
return result_info->domain;
}
std::optional<AttributeMetaData> source_info = component.attribute_get_meta_data(source_name);
if (source_info) {
return source_info->domain;
}
return ATTR_DOMAIN_POINT;
}
static void clamp_attribute(GeometryComponent &component, const GeoNodeExecParams ¶ms)
{
const std::string attribute_name = params.get_input<std::string>("Attribute");
const std::string result_name = params.get_input<std::string>("Result");
if (attribute_name.empty() || result_name.empty()) {
return;
}
if (!component.attribute_exists(attribute_name)) {
params.error_message_add(NodeWarningType::Error,
TIP_("No attribute with name \"") + attribute_name + "\"");
return;
}
const NodeAttributeClamp &storage = *(const NodeAttributeClamp *)params.node().storage;
const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
const AttributeDomain domain = get_result_domain(component, attribute_name, result_name);
const int operation = static_cast<int>(storage.operation);
GVArray attribute_input = component.attribute_try_get_for_read(
attribute_name, domain, data_type);
OutputAttribute attribute_result = component.attribute_try_get_for_output_only(
result_name, domain, data_type);
if (!attribute_result) {
params.error_message_add(NodeWarningType::Error,
TIP_("Could not find or create attribute with name \"") +
result_name + "\"");
return;
}
switch (data_type) {
case CD_PROP_FLOAT3: {
float3 min = params.get_input<float3>("Min");
float3 max = params.get_input<float3>("Max");
if (operation == NODE_CLAMP_RANGE) {
if (min.x > max.x) {
std::swap(min.x, max.x);
}
if (min.y > max.y) {
std::swap(min.y, max.y);
}
if (min.z > max.z) {
std::swap(min.z, max.z);
}
}
MutableSpan<float3> results = attribute_result.as_span<float3>();
clamp_attribute<float3>(attribute_input.typed<float3>(), results, min, max);
break;
}
case CD_PROP_FLOAT: {
const float min = params.get_input<float>("Min_001");
const float max = params.get_input<float>("Max_001");
MutableSpan<float> results = attribute_result.as_span<float>();
if (operation == NODE_CLAMP_RANGE && min > max) {
clamp_attribute<float>(attribute_input.typed<float>(), results, max, min);
}
else {
clamp_attribute<float>(attribute_input.typed<float>(), results, min, max);
}
break;
}
case CD_PROP_INT32: {
const int min = params.get_input<int>("Min_002");
const int max = params.get_input<int>("Max_002");
MutableSpan<int> results = attribute_result.as_span<int>();
if (operation == NODE_CLAMP_RANGE && min > max) {
clamp_attribute<int>(attribute_input.typed<int>(), results, max, min);
}
else {
clamp_attribute<int>(attribute_input.typed<int>(), results, min, max);
}
break;
}
case CD_PROP_COLOR: {
ColorGeometry4f min = params.get_input<ColorGeometry4f>("Min_003");
ColorGeometry4f max = params.get_input<ColorGeometry4f>("Max_003");
if (operation == NODE_CLAMP_RANGE) {
if (min.r > max.r) {
std::swap(min.r, max.r);
}
if (min.g > max.g) {
std::swap(min.g, max.g);
}
if (min.b > max.b) {
std::swap(min.b, max.b);
}
if (min.a > max.a) {
std::swap(min.a, max.a);
}
}
MutableSpan<ColorGeometry4f> results = attribute_result.as_span<ColorGeometry4f>();
clamp_attribute<ColorGeometry4f>(
attribute_input.typed<ColorGeometry4f>(), results, min, max);
break;
}
default: {
BLI_assert(false);
break;
}
}
attribute_result.save();
}
static void geo_node_attribute_clamp_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
geometry_set = geometry_set_realize_instances(geometry_set);
if (geometry_set.has<MeshComponent>()) {
clamp_attribute(geometry_set.get_component_for_write<MeshComponent>(), params);
}
if (geometry_set.has<PointCloudComponent>()) {
clamp_attribute(geometry_set.get_component_for_write<PointCloudComponent>(), params);
}
if (geometry_set.has<CurveComponent>()) {
clamp_attribute(geometry_set.get_component_for_write<CurveComponent>(), params);
}
params.set_output("Geometry", geometry_set);
}
} // namespace blender::nodes::node_geo_legacy_attribute_clamp_cc
void register_node_type_geo_attribute_clamp()
{
namespace file_ns = blender::nodes::node_geo_legacy_attribute_clamp_cc;
static bNodeType ntype;
geo_node_type_base(
&ntype, GEO_NODE_LEGACY_ATTRIBUTE_CLAMP, "Attribute Clamp", NODE_CLASS_ATTRIBUTE, 0);
node_type_init(&ntype, file_ns::geo_node_attribute_clamp_init);
node_type_update(&ntype, file_ns::geo_node_attribute_clamp_update);
ntype.declare = file_ns::geo_node_attribute_clamp_declare;
ntype.geometry_node_execute = file_ns::geo_node_attribute_clamp_exec;
ntype.draw_buttons = file_ns::geo_node_attribute_clamp_layout;
node_type_storage(
&ntype, "NodeAttributeClamp", node_free_standard_storage, node_copy_standard_storage);
nodeRegisterType(&ntype);
}
|