载入中...
搜索中...
未找到
PointSetAttributeOps.cpp
浏览该文件的文档.
1#include "procgen/PointSet.h"
2
3#include <utility>
4
5#include <algorithm>
6#include <cmath>
7#include <cstdint>
8#include <limits>
9#include <string_view>
10#include <unordered_map>
11
12namespace eve::procgen {
13namespace {
14
15Result<float> invalidChannelFloat(std::string message, std::string path) {
18}
19
20bool isMetadataName(std::string_view name) noexcept {
21 return !name.empty() && name.front() != '$' && name.front() != '@';
22}
23
24std::string_view stripDomainPrefix(std::string_view name, std::string_view* domain) {
25 if (name.size() >= 6 && name.substr(0, 6) == "@Data.") {
26 *domain = "data";
27 return name.substr(6);
28 }
29 if (name.size() >= 10 && name.substr(0, 10) == "@Elements.") {
30 *domain = "elements";
31 return name.substr(10);
32 }
33 if (name.size() >= 6 && name.substr(0, 6) == "@Last.") {
34 *domain = "elements"; // closed-set alias of incoming Elements domain
35 return name.substr(6);
36 }
37 *domain = "elements";
38 return name;
39}
40
41void rotationBasis(float pitchDeg, float yawDeg, float rollDeg, float* forward, float* right, float* up) {
42 const float deg = 0.017453292519943295f;
43 const float cp = std::cos(pitchDeg * deg), sp = std::sin(pitchDeg * deg);
44 const float cy = std::cos(yawDeg * deg), sy = std::sin(yawDeg * deg);
45 const float cr = std::cos(rollDeg * deg), sr = std::sin(rollDeg * deg);
46 // Yaw-Pitch-Roll (Y-up) basis matching engine point transforms.
47 forward[0] = sy * cp;
48 forward[1] = -sp;
49 forward[2] = cy * cp;
50 right[0] = cy * cr + sy * sp * sr;
51 right[1] = cp * sr;
52 right[2] = -sy * cr + cy * sp * sr;
53 up[0] = -cy * sr + sy * sp * cr;
54 up[1] = cp * cr;
55 up[2] = sy * sr + cy * sp * cr;
56}
57
58void appendRow(PointSet& output, const PointSet& input, std::size_t index) {
59 output.appendPointFrom(input, index).expect("PointSet attribute op requires compatible schemas");
60}
61
62} // namespace
63
64bool isPointFloatSelector(std::string_view name) noexcept {
65 return name == "$Density" || name == "$Seed" || name == "$Steepness" || name == "$Position.X" ||
66 name == "$Position.Y" || name == "$Position.Z" || name == "$Normal.X" || name == "$Normal.Y" ||
67 name == "$Normal.Z" || name == "$Rotation.Pitch" || name == "$Rotation.Yaw" ||
68 name == "$Rotation.Roll" || name == "$Rotation.Forward.X" || name == "$Rotation.Forward.Y" ||
69 name == "$Rotation.Forward.Z" || name == "$Rotation.Right.X" || name == "$Rotation.Right.Y" ||
70 name == "$Rotation.Right.Z" || name == "$Rotation.Up.X" || name == "$Rotation.Up.Y" ||
71 name == "$Rotation.Up.Z" || name == "$Scale.X" || name == "$Scale.Y" || name == "$Scale.Z" ||
72 name == "$Color.R" || name == "$Color.G" || name == "$Color.B" || name == "$Color.A" ||
73 name == "$BoundsMin.X" || name == "$BoundsMin.Y" || name == "$BoundsMin.Z" || name == "$BoundsMax.X" ||
74 name == "$BoundsMax.Y" || name == "$BoundsMax.Z" || name == "$Position.ZYX.X" ||
75 name == "$Position.ZYX.Y" || name == "$Position.ZYX.Z" || name == "$Position.XZY.X" ||
76 name == "$Position.XZY.Y" || name == "$Position.XZY.Z" || name == "$Normal.ZYX.X" ||
77 name == "$Normal.ZYX.Y" || name == "$Normal.ZYX.Z";
78}
79
80bool isPointFloatChannel(std::string_view name) noexcept {
81 return isPointFloatSelector(name) || isMetadataName(name);
82}
83
85 if (index < 0 || index >= points.getCount())
86 return invalidChannelFloat("point index is out of range", "index");
87 if (name.empty()) return invalidChannelFloat("attribute channel must not be empty", "name");
88 if (name.front() != '$') {
89 std::string_view domain = "elements";
90 const std::string_view leaf = stripDomainPrefix(name, &domain);
91 if (leaf.empty()) return invalidChannelFloat("attribute channel must not be empty", "name");
92 if (domain == "data") {
93 if (points.dataAttributes().rowCount() == 0) return Result<float>::success(defaultValue);
94 return Result<float>::success(points.dataAttributes().getFloat(0, leaf).value_or(defaultValue));
95 }
96 return Result<float>::success(points.getFloatAttribute(index, std::string(leaf), defaultValue));
97 }
99 return invalidChannelFloat("unknown float selector '" + std::string(name) + "'", "name");
100
101 const ProcgenPoint& point = points.points()[std::size_t(index)];
102 if (name == "$Density") return Result<float>::success(point.density);
103 if (name == "$Seed") return Result<float>::success(float(point.seed));
104 if (name == "$Steepness") return Result<float>::success(point.steepness);
105 if (name == "$Position.X") return Result<float>::success(point.x);
106 if (name == "$Position.Y") return Result<float>::success(point.y);
107 if (name == "$Position.Z") return Result<float>::success(point.z);
108 if (name == "$Normal.X") return Result<float>::success(point.normalX);
109 if (name == "$Normal.Y") return Result<float>::success(point.normalY);
110 if (name == "$Normal.Z") return Result<float>::success(point.normalZ);
111 if (name == "$Rotation.Pitch") return Result<float>::success(point.pitch);
112 if (name == "$Rotation.Yaw") return Result<float>::success(point.yaw);
113 if (name == "$Rotation.Roll") return Result<float>::success(point.roll);
114 if (name == "$Scale.X") return Result<float>::success(point.scaleX);
115 if (name == "$Scale.Y") return Result<float>::success(point.scaleY);
116 if (name == "$Scale.Z") return Result<float>::success(point.scaleZ);
117 if (name == "$Color.R") return Result<float>::success(point.colorR);
118 if (name == "$Color.G") return Result<float>::success(point.colorG);
119 if (name == "$Color.B") return Result<float>::success(point.colorB);
120 if (name == "$Color.A") return Result<float>::success(point.colorA);
121 if (name == "$BoundsMin.X") return Result<float>::success(point.boundsMinX);
122 if (name == "$BoundsMin.Y") return Result<float>::success(point.boundsMinY);
123 if (name == "$BoundsMin.Z") return Result<float>::success(point.boundsMinZ);
124 if (name == "$BoundsMax.X") return Result<float>::success(point.boundsMaxX);
125 if (name == "$BoundsMax.Y") return Result<float>::success(point.boundsMaxY);
126 if (name == "$BoundsMax.Z") return Result<float>::success(point.boundsMaxZ);
127 float forward[3], right[3], up[3];
128 if (name.rfind("$Rotation.Forward.", 0) == 0 || name.rfind("$Rotation.Right.", 0) == 0 ||
129 name.rfind("$Rotation.Up.", 0) == 0) {
130 rotationBasis(point.pitch, point.yaw, point.roll, forward, right, up);
131 const float* axis = name.rfind("$Rotation.Forward.", 0) == 0 ? forward
132 : name.rfind("$Rotation.Right.", 0) == 0 ? right
133 : up;
134 if (!name.empty() && name.back() == 'X') return Result<float>::success(axis[0]);
135 if (!name.empty() && name.back() == 'Y') return Result<float>::success(axis[1]);
136 if (!name.empty() && name.back() == 'Z') return Result<float>::success(axis[2]);
137 }
138 if (name == "$Position.ZYX.X") return Result<float>::success(point.z);
139 if (name == "$Position.ZYX.Y") return Result<float>::success(point.y);
140 if (name == "$Position.ZYX.Z") return Result<float>::success(point.x);
141 if (name == "$Position.XZY.X") return Result<float>::success(point.x);
142 if (name == "$Position.XZY.Y") return Result<float>::success(point.z);
143 if (name == "$Position.XZY.Z") return Result<float>::success(point.y);
144 if (name == "$Normal.ZYX.X") return Result<float>::success(point.normalZ);
145 if (name == "$Normal.ZYX.Y") return Result<float>::success(point.normalY);
146 if (name == "$Normal.ZYX.Z") return Result<float>::success(point.normalX);
147 return invalidChannelFloat("unknown float selector '" + std::string(name) + "'", "name");
148}
149
151 if (index < 0 || index >= points.getCount()) return Result<void>::failure(
152 Diagnostic::error(DiagnosticCode::InvalidArgument, "point index is out of range", "index"));
153 if (name.empty()) return Result<void>::failure(
154 Diagnostic::error(DiagnosticCode::InvalidArgument, "attribute channel must not be empty", "name"));
155 if (name.front() != '$') {
156 std::string_view domain = "elements";
157 const std::string_view leaf = stripDomainPrefix(name, &domain);
158 if (leaf.empty()) return Result<void>::failure(
159 Diagnostic::error(DiagnosticCode::InvalidArgument, "attribute channel must not be empty", "name"));
160 if (domain == "data") {
162 return points.mutableDataAttributes().setFloat(0, leaf, value);
163 }
164 return points.trySetFloatAttribute(index, std::string(leaf), value);
165 }
168 Diagnostic::error(DiagnosticCode::InvalidArgument, "unknown float selector '" + std::string(name) + "'", "name"));
169
170 ProcgenPoint& point = points.mutablePoint(std::size_t(index));
171 if (name == "$Density") {
173 return Result<void>::success();
174 }
175 if (name == "$Seed") {
176 point.seed = value <= 0.f ? 0u : uint32_t(value);
177 return Result<void>::success();
178 }
179 if (name == "$Steepness") {
180 point.steepness = std::clamp(value, 0.f, 1.f);
181 return Result<void>::success();
182 }
183 if (name == "$Position.X") {
184 point.x = value;
185 return Result<void>::success();
186 }
187 if (name == "$Position.Y") {
188 point.y = value;
189 return Result<void>::success();
190 }
191 if (name == "$Position.Z") {
192 point.z = value;
193 return Result<void>::success();
194 }
195 if (name == "$Normal.X") {
196 point.normalX = value;
197 return Result<void>::success();
198 }
199 if (name == "$Normal.Y") {
200 point.normalY = value;
201 return Result<void>::success();
202 }
203 if (name == "$Normal.Z") {
204 point.normalZ = value;
205 return Result<void>::success();
206 }
207 if (name == "$Rotation.Pitch") {
208 point.pitch = value;
209 return Result<void>::success();
210 }
211 if (name == "$Rotation.Yaw") {
212 point.yaw = value;
213 return Result<void>::success();
214 }
215 if (name == "$Rotation.Roll") {
216 point.roll = value;
217 return Result<void>::success();
218 }
219 if (name == "$Scale.X") {
220 point.scaleX = value;
221 return Result<void>::success();
222 }
223 if (name == "$Scale.Y") {
224 point.scaleY = value;
225 return Result<void>::success();
226 }
227 if (name == "$Scale.Z") {
228 point.scaleZ = value;
229 return Result<void>::success();
230 }
231 if (name == "$Color.R") {
232 point.colorR = value;
233 return Result<void>::success();
234 }
235 if (name == "$Color.G") {
236 point.colorG = value;
237 return Result<void>::success();
238 }
239 if (name == "$Color.B") {
240 point.colorB = value;
241 return Result<void>::success();
242 }
243 if (name == "$Color.A") {
244 point.colorA = value;
245 return Result<void>::success();
246 }
247 if (name == "$BoundsMin.X") {
248 point.boundsMinX = value;
249 return Result<void>::success();
250 }
251 if (name == "$BoundsMin.Y") {
252 point.boundsMinY = value;
253 return Result<void>::success();
254 }
255 if (name == "$BoundsMin.Z") {
256 point.boundsMinZ = value;
257 return Result<void>::success();
258 }
259 if (name == "$BoundsMax.X") {
260 point.boundsMaxX = value;
261 return Result<void>::success();
262 }
263 if (name == "$BoundsMax.Y") {
264 point.boundsMaxY = value;
265 return Result<void>::success();
266 }
267 if (name == "$BoundsMax.Z") {
268 point.boundsMaxZ = value;
269 return Result<void>::success();
270 }
272 Diagnostic::error(DiagnosticCode::InvalidArgument, "unknown float selector '" + std::string(name) + "'", "name"));
273}
274
276 const std::string& outputAttribute, const std::string& operation, float operand,
277 float defaultValue) {
279 for (int index = 0; index < result.getCount(); ++index) {
280 const float value =
281 readPointFloatChannel(result, index, attribute, defaultValue).expect("mathPointFloatAttribute input");
282 float output = value;
283 if (operation == "add")
284 output += operand;
285 else if (operation == "subtract")
286 output -= operand;
287 else if (operation == "multiply")
288 output *= operand;
289 else if (operation == "divide")
290 output /= operand;
291 else if (operation == "min")
292 output = std::min(output, operand);
293 else if (operation == "max")
294 output = std::max(output, operand);
295 writePointFloatChannel(result, index, outputAttribute, output).expect("mathPointFloatAttribute output");
296 }
297 return result;
298}
299
300PointSet filterPointFloatAttribute(const PointSet& input, const std::string& name, float minValue, float maxValue,
301 bool invert) {
302 if (minValue > maxValue) std::swap(minValue, maxValue);
304 for (int index = 0; index < input.getCount(); ++index) {
305 bool keep = false;
306 if (isMetadataName(name)) {
307 const auto stored = input.attributes().getFloat(std::size_t(index), name);
308 keep = stored && *stored >= minValue && *stored <= maxValue;
309 } else {
310 const float value =
311 readPointFloatChannel(input, index, name, 0.f).expect("filterPointFloatAttribute channel");
312 keep = value >= minValue && value <= maxValue;
313 }
314 if (keep != invert) appendRow(output, input, std::size_t(index));
315 }
316 return output;
317}
318
319PointSet filterPointIntAttribute(const PointSet& input, const std::string& name, std::int64_t minValue,
320 std::int64_t maxValue, bool invert) {
321 if (minValue > maxValue) std::swap(minValue, maxValue);
323 for (std::size_t index = 0; index < input.points().size(); ++index) {
324 const auto value = input.attributes().getInt(index, name);
325 const bool matches = value && *value >= minValue && *value <= maxValue;
326 if (matches != invert) appendRow(output, input, index);
327 }
328 return output;
329}
330
331PointSet filterPointBoolAttribute(const PointSet& input, const std::string& name, bool value, bool invert) {
333 for (std::size_t index = 0; index < input.points().size(); ++index) {
334 const auto found = input.attributes().getBool(index, name);
335 const bool matches = found && *found == value;
336 if (matches != invert) appendRow(output, input, index);
337 }
338 return output;
339}
340
341Result<PointSet> copyPointAttribute(const PointSet& input, const std::string& source, const std::string& target) {
342 if (source.empty() || target.empty())
344 Diagnostic::error(DiagnosticCode::InvalidArgument, "attribute copy requires source and target", "name"));
346
347 const bool sourceSelector = isPointFloatSelector(source);
348 const bool targetSelector = isPointFloatSelector(target);
349 if (!source.empty() && source.front() == '$' && !sourceSelector)
351 Diagnostic::error(DiagnosticCode::InvalidArgument, "unknown float selector '" + source + "'", "source"));
352 if (!target.empty() && target.front() == '$' && !targetSelector)
354 Diagnostic::error(DiagnosticCode::InvalidArgument, "unknown float selector '" + target + "'", "target"));
355
356 if (!sourceSelector && !targetSelector) {
358 auto copied = result.tryCopyAttribute(source, target);
359 if (!copied.ok()) return Result<PointSet>::failure(copied.status());
360 return Result<PointSet>::success(std::move(result));
361 }
362
365 DiagnosticCode::InvalidArgument, "attribute copy with selectors requires float channels", "name"));
366
368 for (int index = 0; index < result.getCount(); ++index) {
369 if (isMetadataName(source) && !result.attributes().has(std::size_t(index), source)) continue;
370 const float value =
371 readPointFloatChannel(result, index, source, 0.f).expect("copyPointAttribute source channel");
373 if (!written.ok()) return Result<PointSet>::failure(written.status());
374 }
375 return Result<PointSet>::success(std::move(result));
376}
377
378Result<PointSet> renamePointAttribute(const PointSet& input, const std::string& from, const std::string& to) {
380 auto renamed = result.tryRenameAttribute(from, to);
381 if (!renamed.ok()) return Result<PointSet>::failure(renamed.status());
382 return Result<PointSet>::success(std::move(result));
383}
384
387 auto deleted = result.tryDeleteAttribute(name);
388 if (!deleted.ok()) return Result<PointSet>::failure(deleted.status());
389 return Result<PointSet>::success(std::move(result));
390}
391
393 const std::string& outputAttribute, const std::string& mode) {
394 const std::string output = outputAttribute.empty() ? attribute : outputAttribute;
395 if (attribute.empty() || output.empty())
397 DiagnosticCode::InvalidArgument, "attribute transfer requires attribute names", "attribute"));
398 if (mode != "index" && mode != "id" && mode != "nearest")
400 DiagnosticCode::InvalidArgument, "attribute transfer mode must be index, id, or nearest", "mode"));
401
402 const bool sourceSelector = isPointFloatSelector(attribute);
403 const bool targetSelector = isPointFloatSelector(output);
404 if (!attribute.empty() && attribute.front() == '$' && !sourceSelector)
406 DiagnosticCode::InvalidArgument, "unknown float selector '" + attribute + "'", "attribute"));
407 if (!output.empty() && output.front() == '$' && !targetSelector)
409 DiagnosticCode::InvalidArgument, "unknown float selector '" + output + "'", "outputAttribute"));
410
411 std::optional<ProcgenAttributeType> metadataType;
412 if (!sourceSelector) {
413 metadataType = source.attributes().typeOf(attribute);
414 if (!metadataType)
416 DiagnosticCode::NotFound, "source attribute '" + attribute + "' does not exist", "attribute"));
417 if (targetSelector && *metadataType != ProcgenAttributeType::Float)
419 DiagnosticCode::TypeMismatch, "only float metadata can transfer into selectors", "attribute"));
420 }
421
422 std::unordered_map<std::uint64_t, int> ids;
423 if (mode == "id") {
424 for (int index = 0; index < source.getCount(); ++index) {
425 const std::uint64_t id = source.getPointId(index);
426 if (id != 0) ids.emplace(id, index);
427 }
428 }
429
430 auto nearestSource = [&](int targetIndex) -> int {
431 const ProcgenPoint& point = target.points()[std::size_t(targetIndex)];
432 int best = -1;
433 float bestD = std::numeric_limits<float>::infinity();
434 for (int index = 0; index < source.getCount(); ++index) {
435 const ProcgenPoint& other = source.points()[std::size_t(index)];
436 const float dx = point.x - other.x;
437 const float dy = point.y - other.y;
438 const float dz = point.z - other.z;
439 const float d = dx * dx + dy * dy + dz * dz;
440 if (d < bestD) {
441 bestD = d;
442 best = index;
443 }
444 }
445 return best;
446 };
447
449 for (int targetIndex = 0; targetIndex < result.getCount(); ++targetIndex) {
450 int sourceIndex = -1;
451 if (mode == "index") {
452 if (targetIndex < source.getCount()) sourceIndex = targetIndex;
453 } else if (mode == "id") {
454 const std::uint64_t id = result.getPointId(targetIndex);
455 if (id != 0) {
456 const auto found = ids.find(id);
457 if (found != ids.end()) sourceIndex = found->second;
458 }
459 } else {
460 sourceIndex = nearestSource(targetIndex);
461 }
462 if (sourceIndex < 0) continue;
463
464 if (sourceSelector || targetSelector ||
465 (metadataType && *metadataType == ProcgenAttributeType::Float && isPointFloatChannel(output))) {
466 if (isMetadataName(attribute) && !source.attributes().has(std::size_t(sourceIndex), attribute)) continue;
467 const float value =
468 readPointFloatChannel(source, sourceIndex, attribute, 0.f).expect("transfer float source");
469 auto written = writePointFloatChannel(result, targetIndex, output, value);
470 if (!written.ok()) return Result<PointSet>::failure(written.status());
471 continue;
472 }
473
474 switch (*metadataType) {
476 const auto value = source.attributes().getFloat(std::size_t(sourceIndex), attribute);
477 if (!value) break;
478 auto written = result.trySetFloatAttribute(targetIndex, output, *value);
479 if (!written.ok()) return Result<PointSet>::failure(written.status());
480 break;
481 }
483 const auto value = source.attributes().getInt(std::size_t(sourceIndex), attribute);
484 if (!value) break;
485 auto written = result.trySetIntAttribute(targetIndex, output, *value);
486 if (!written.ok()) return Result<PointSet>::failure(written.status());
487 break;
488 }
490 const auto value = source.attributes().getBool(std::size_t(sourceIndex), attribute);
491 if (!value) break;
492 auto written = result.trySetBoolAttribute(targetIndex, output, *value);
493 if (!written.ok()) return Result<PointSet>::failure(written.status());
494 break;
495 }
497 const auto value = source.attributes().getVector(std::size_t(sourceIndex), attribute);
498 if (!value) break;
499 auto written = result.trySetVectorAttribute(targetIndex, output, value->x, value->y, value->z);
500 if (!written.ok()) return Result<PointSet>::failure(written.status());
501 break;
502 }
504 const auto value = source.attributes().getString(std::size_t(sourceIndex), attribute);
505 if (!value) break;
506 auto written = result.trySetStringAttribute(targetIndex, output, std::string(*value));
507 if (!written.ok()) return Result<PointSet>::failure(written.status());
508 break;
509 }
510 }
511 }
512 return Result<PointSet>::success(std::move(result));
513}
514
515PointSet setPointIntAttribute(const PointSet& input, const std::string& attribute, std::int64_t value) {
517 for (int index = 0; index < result.getCount(); ++index)
518 result.trySetIntAttribute(index, attribute, value).expect("setPointIntAttribute schema");
519 return result;
520}
521
522PointSet setPointBoolAttribute(const PointSet& input, const std::string& attribute, bool value) {
524 for (int index = 0; index < result.getCount(); ++index)
525 result.trySetBoolAttribute(index, attribute, value).expect("setPointBoolAttribute schema");
526 return result;
527}
528
529PointSet setPointVectorAttribute(const PointSet& input, const std::string& attribute, float x, float y, float z) {
531 for (int index = 0; index < result.getCount(); ++index)
532 result.trySetVectorAttribute(index, attribute, x, y, z).expect("setPointVectorAttribute schema");
533 return result;
534}
535
537 const std::string& comparison, float operand,
538 const std::string& outputAttribute, float defaultValue) {
541 Diagnostic::error(DiagnosticCode::InvalidArgument, "compare requires a float channel", "attribute"));
542 if (outputAttribute.empty() || !isMetadataName(outputAttribute))
544 DiagnosticCode::InvalidArgument, "compare output must be a metadata bool attribute", "outputAttribute"));
545 if (comparison != "eq" && comparison != "ne" && comparison != "lt" && comparison != "le" && comparison != "gt" &&
546 comparison != "ge")
548 DiagnosticCode::InvalidArgument, "compare comparison must be eq/ne/lt/le/gt/ge", "comparison"));
549
551 for (int index = 0; index < result.getCount(); ++index) {
552 const float value =
553 readPointFloatChannel(result, index, attribute, defaultValue).expect("comparePointFloatAttribute");
554 bool matches = false;
555 if (comparison == "eq")
556 matches = value == operand;
557 else if (comparison == "ne")
558 matches = value != operand;
559 else if (comparison == "lt")
560 matches = value < operand;
561 else if (comparison == "le")
562 matches = value <= operand;
563 else if (comparison == "gt")
564 matches = value > operand;
565 else
566 matches = value >= operand;
567 auto written = result.trySetBoolAttribute(index, outputAttribute, matches);
568 if (!written.ok()) return Result<PointSet>::failure(written.status());
569 }
570 return Result<PointSet>::success(std::move(result));
571}
572
573Result<PointSet> selectPointFloatAttribute(const PointSet& input, const std::string& conditionAttribute,
574 const std::string& trueAttribute, const std::string& falseAttribute,
575 const std::string& outputAttribute, float trueDefault,
576 float falseDefault) {
577 if (conditionAttribute.empty() || !isMetadataName(conditionAttribute))
579 DiagnosticCode::InvalidArgument, "select condition must be a metadata bool attribute",
580 "conditionAttribute"));
581 if (!isPointFloatChannel(trueAttribute) || !isPointFloatChannel(falseAttribute) ||
582 !isPointFloatChannel(outputAttribute))
584 Diagnostic::error(DiagnosticCode::InvalidArgument, "select float channels are invalid", "attribute"));
585
587 for (int index = 0; index < result.getCount(); ++index) {
588 const bool takeTrue = result.getBoolAttribute(index, conditionAttribute, false);
589 const float value = readPointFloatChannel(result, index, takeTrue ? trueAttribute : falseAttribute,
590 takeTrue ? trueDefault : falseDefault)
591 .expect("selectPointFloatAttribute channel");
592 auto written = writePointFloatChannel(result, index, outputAttribute, value);
593 if (!written.ok()) return Result<PointSet>::failure(written.status());
594 }
595 return Result<PointSet>::success(std::move(result));
596}
597
599 AttributeTable& data = points.mutableDataAttributes();
600 if (data.rowCount() == 0)
601 (void)data.appendRow();
602 else if (data.rowCount() > 1)
603 data.resize(1);
604}
605
607 if (attribute.empty() || attribute.front() == '$' || attribute.front() == '@')
609 "data attribute name is invalid", "attribute"));
611 return points.mutableDataAttributes().setFloat(0, attribute, value);
612}
613
615 if (attribute.empty() || attribute.front() == '$' || attribute.front() == '@')
617 "data attribute name is invalid", "attribute"));
619 return points.mutableDataAttributes().setInt(0, attribute, value);
620}
621
622Result<void> setPointDataStringAttribute(PointSet& points, const std::string& attribute, const std::string& value) {
623 if (attribute.empty() || attribute.front() == '$' || attribute.front() == '@')
625 "data attribute name is invalid", "attribute"));
627 return points.mutableDataAttributes().setString(0, attribute, value);
628}
629
631 const std::string& outputAttribute, const std::string& mode) {
633 const std::string out = outputAttribute.empty() ? "partition" : outputAttribute;
634 const bool useHash = mode == "hash";
635 for (int i = 0; i < result.getCount(); ++i) {
636 std::int64_t partition = 0;
637 if (result.hasIntAttribute(i, attribute))
638 partition = result.getIntAttribute(i, attribute, 0);
639 else if (result.hasStringAttribute(i, attribute)) {
640 const std::string value = result.getStringAttribute(i, attribute, {});
641 if (useHash) {
642 std::uint64_t hash = 14695981039346656037ull;
643 for (unsigned char c : value) {
644 hash ^= c;
645 hash *= 1099511628211ull;
646 }
647 partition = std::int64_t(hash & 0x7fffffffull);
648 } else {
649 // Stable dense-ish id from string bytes without allocating a map.
650 std::uint64_t hash = 14695981039346656037ull;
651 for (unsigned char c : value) {
652 hash ^= c;
653 hash *= 1099511628211ull;
654 }
655 partition = std::int64_t(hash & 0xffffull);
656 }
657 } else if (result.hasFloatAttribute(i, attribute)) {
658 partition = std::int64_t(std::llround(result.getFloatAttribute(i, attribute, 0.f)));
659 }
660 result.trySetIntAttribute(i, out, partition).expect("partition write");
661 }
662 return result;
663}
664
665static float hashNoise(uint32_t seed, int index, float x, float z, float frequency) {
666 std::uint32_t h = seed * 747796405u + 2891336453u;
667 h ^= std::uint32_t(index) * 2246822519u;
668 h ^= std::uint32_t(std::int32_t(x * frequency * 1000.f)) * 3266489917u;
669 h ^= std::uint32_t(std::int32_t(z * frequency * 1000.f)) * 668265263u;
670 h ^= h >> 15;
671 h *= 2246822519u;
672 h ^= h >> 13;
673 return float(h & 0x00ffffffu) / float(0x00ffffffu);
674}
675
676PointSet noisePointFloatAttribute(const PointSet& input, const std::string& attribute, uint32_t seed,
677 float frequency, float amplitude, float offset) {
679 const std::string out = attribute.empty() ? "noise" : attribute;
680 for (int i = 0; i < result.getCount(); ++i) {
681 const auto& p = result.points()[std::size_t(i)];
682 const float n = hashNoise(seed, i, p.x, p.z, frequency) * amplitude + offset;
683 result.trySetFloatAttribute(i, out, n).expect("noise write");
684 }
685 return result;
686}
687
689 const std::string& outputAttribute, const std::string& operation, std::int64_t operand,
690 std::int64_t defaultValue) {
692 const std::string out = outputAttribute.empty() ? attribute : outputAttribute;
693 for (int i = 0; i < result.getCount(); ++i) {
694 const std::int64_t value = result.getIntAttribute(i, attribute, defaultValue);
695 std::int64_t output = value;
696 if (operation == "add") output = value + operand;
697 else if (operation == "subtract") output = value - operand;
698 else if (operation == "multiply") output = value * operand;
699 else if (operation == "divide") output = operand == 0 ? value : value / operand;
700 else if (operation == "min") output = std::min(value, operand);
701 else if (operation == "max") output = std::max(value, operand);
702 else if (operation == "modulo") output = operand == 0 ? 0 : value % operand;
703 result.trySetIntAttribute(i, out, output).expect("int math write");
704 }
705 return result;
706}
707
709 const std::string& outputAttribute, const std::string& operation, float operandX,
710 float operandY, float operandZ, float defaultX, float defaultY, float defaultZ) {
712 const std::string out = outputAttribute.empty() ? attribute : outputAttribute;
713 for (int i = 0; i < result.getCount(); ++i) {
714 float x = result.getVectorAttributeX(i, attribute, defaultX);
715 float y = result.getVectorAttributeY(i, attribute, defaultY);
716 float z = result.getVectorAttributeZ(i, attribute, defaultZ);
717 if (operation == "add") {
718 x += operandX; y += operandY; z += operandZ;
719 } else if (operation == "subtract") {
720 x -= operandX; y -= operandY; z -= operandZ;
721 } else if (operation == "multiply" || operation == "scale") {
722 x *= operandX; y *= operandY; z *= operandZ;
723 } else if (operation == "divide") {
724 if (operandX != 0.f) x /= operandX;
725 if (operandY != 0.f) y /= operandY;
726 if (operandZ != 0.f) z /= operandZ;
727 } else if (operation == "set") {
728 x = operandX; y = operandY; z = operandZ;
729 }
730 result.trySetVectorAttribute(i, out, x, y, z).expect("vector math write");
731 }
732 return result;
733}
734
735} // namespace eve::procgen
double z
double y
double x
LogicalId target
ActionParameterOperation operation
std::string output
float cy
Definition CardTypes.cpp:37
glm::vec4 p[6]
std::string name
Definition ECS.cpp:154
std::vector< std::uint8_t > stored
Definition Evpack.cpp:171
EvpackChunkInput input
Definition Evpack.cpp:170
std::string message
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
HexVec3 up
HexVec3 right
std::int32_t c
int h
JointPoint2D axis
size_t offset
std::string to
std::string from
Poco::JSON::Object::Ptr result
double defaultValue
glm::dvec3 dx
glm::dvec3 dz
std::string path
Definition PlayHost.cpp:111
std::shared_ptr< const std::vector< glm::vec2 > > points
Object::Ptr value
std::string hash
float d
std::string domain
std::string attribute
bool found
ScriptStructure out
std::string_view source
float dy
Definition Shadow.cpp:151
std::map< Cell, int > best
uint32_t seed
Definition TreeMesh.cpp:276
float size
Definition TreeMesh.cpp:158
uint32_t index
V forward
glm::vec3 point
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
void expect(std::string_view message) const
Require success for a void operation.
Definition Result.h:582
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Schema-bearing column store aligned with a PointSet's point rows.
std::optional< std::int64_t > getInt(std::size_t row, std::string_view name) const
Read an integer value, or no value for an absent row, name, or type.
std::optional< bool > getBool(std::size_t row, std::string_view name) const
Read a Boolean value, or no value for an absent row, name, or type.
std::optional< float > getFloat(std::size_t row, std::string_view name) const
Read a float value, or no value for an absent row, name, or type.
Script-friendly collection of attributed 3D samples.
Definition PointSet.h:59
bool empty() const
Empty.
Definition PointSet.cpp:50
Result< void > tryCopyAttribute(const std::string &from, const std::string &to)
Copy one metadata column onto another name on this set.
Definition PointSet.cpp:450
Result< void > tryRenameAttribute(const std::string &from, const std::string &to)
Rename one metadata column on this set.
Definition PointSet.cpp:442
Result< void > trySetBoolAttribute(int index, const std::string &name, bool value)
Canonical checked Boolean metadata write.
Definition PointSet.cpp:372
Result< void > trySetVectorAttribute(int index, const std::string &name, float x, float y, float z)
Canonical checked vector metadata write.
Definition PointSet.cpp:392
std::uint64_t getPointId(int index) const
Return the stable point identity, or zero when it has not been assigned.
Definition PointSet.cpp:290
bool getBoolAttribute(int index, const std::string &name, bool fallback) const
Read boolean metadata or return the caller-provided default when absent.
Definition PointSet.cpp:383
Result< void > trySetIntAttribute(int index, const std::string &name, std::int64_t value)
Canonical checked signed integer metadata write.
Definition PointSet.cpp:352
const AttributeTable & attributes() const noexcept
Borrow the authoritative schema-bearing attribute table.
Definition PointSet.h:216
Result< void > tryDeleteAttribute(const std::string &name)
Delete one metadata column on this set.
Definition PointSet.cpp:446
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
Result< PointSet > deletePointAttribute(const PointSet &input, const std::string &name)
Delete one metadata column.
PointSet mathPointIntAttribute(const PointSet &input, const std::string &attribute, const std::string &outputAttribute, const std::string &operation, std::int64_t operand, std::int64_t defaultValue)
Apply integer math to an int metadata column.
bool isPointFloatChannel(std::string_view name) noexcept
Return whether name is a valid float channel (builtin selector or metadata name).
EVENGINE_API_DOMAINS Result< float > readPointFloatChannel(const PointSet &points, int index, std::string_view name, float defaultValue)
Read a float metadata column or closed $ point-field selector.
PointSet filterPointIntAttribute(const PointSet &input, const std::string &name, std::int64_t minValue, std::int64_t maxValue, bool invert)
Select points whose named int attribute lies in an inclusive range.
Result< PointSet > copyPointAttribute(const PointSet &input, const std::string &source, const std::string &target)
Copy one float channel or typed metadata column onto another name.
EVENGINE_API_DOMAINS Result< void > setPointDataFloatAttribute(PointSet &points, const std::string &attribute, float value)
Write a float into the PointSet @Data domain.
Result< PointSet > selectPointFloatAttribute(const PointSet &input, const std::string &conditionAttribute, const std::string &trueAttribute, const std::string &falseAttribute, const std::string &outputAttribute, float trueDefault, float falseDefault)
Select between two float channels using a bool metadata condition.
EVENGINE_API_DOMAINS PointSet noisePointFloatAttribute(const PointSet &input, const std::string &attribute, uint32_t seed, float frequency, float amplitude, float offset)
Write deterministic float noise into a metadata attribute.
PointSet mathPointFloatAttribute(const PointSet &input, const std::string &attribute, const std::string &outputAttribute, const std::string &operation, float operand, float defaultValue)
Apply one scalar operation to a float metadata attribute or $ selector.
EVENGINE_API_DOMAINS PointSet filterPointFloatAttribute(const PointSet &input, const std::string &name, float minValue, float maxValue, bool invert)
Select points whose named float attribute or $ selector lies in an inclusive range.
PointSet mathPointVectorAttribute(const PointSet &input, const std::string &attribute, const std::string &outputAttribute, const std::string &operation, float operandX, float operandY, float operandZ, float defaultX, float defaultY, float defaultZ)
Apply vector math to a vector metadata column.
PointSet setPointIntAttribute(const PointSet &input, const std::string &attribute, std::int64_t value)
Write a constant int metadata column on every point.
EVENGINE_API_DOMAINS PointSet partitionPointAttribute(const PointSet &input, const std::string &attribute, const std::string &outputAttribute, const std::string &mode)
Assign partition indices from a string/int attribute (mode: value|hash).
void ensurePointSetDataRow(PointSet &points)
Return whether name is a known $-prefixed float point-field selector.
Result< void > setPointDataStringAttribute(PointSet &points, const std::string &attribute, const std::string &value)
Write a string into the PointSet @Data domain.
Result< void > setPointDataIntAttribute(PointSet &points, const std::string &attribute, std::int64_t value)
Write an int into the PointSet @Data domain.
Result< void > writePointFloatChannel(PointSet &points, int index, std::string_view name, float value)
Write a float metadata column or closed $ point-field selector.
Result< PointSet > transferPointAttribute(const PointSet &target, const PointSet &source, const std::string &attribute, const std::string &outputAttribute, const std::string &mode)
Transfer one attribute from source onto target points.
PointSet setPointVectorAttribute(const PointSet &input, const std::string &attribute, float x, float y, float z)
Write a constant vector metadata column on every point.
PointSet setPointBoolAttribute(const PointSet &input, const std::string &attribute, bool value)
Write a constant bool metadata column on every point.
Result< PointSet > renamePointAttribute(const PointSet &input, const std::string &from, const std::string &to)
Rename one metadata column.
Result< PointSet > comparePointFloatAttribute(const PointSet &input, const std::string &attribute, const std::string &comparison, float operand, const std::string &outputAttribute, float defaultValue)
Compare a float channel against an operand and write a bool metadata column.
PointSet filterPointBoolAttribute(const PointSet &input, const std::string &name, bool value, bool invert)
Select points whose named bool attribute equals a value.
bool isPointFloatSelector(std::string_view name) noexcept
True when point float selector.
@ TypeMismatch
A stable reference resolved to a different canonical domain type.
One deterministic sample used by script-first procedural pipelines.
Definition PointSet.h:17