Skip to content

Commit

Permalink
attr-scope
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhang95 committed Dec 28, 2023
1 parent e8a84fe commit 61fb01f
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 50 deletions.
34 changes: 26 additions & 8 deletions zeno/src/nodes/neo/PrimAttrOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@ struct PrimFillAttr : INode {
auto value = get_input<NumericObject>("value");
auto attr = get_input2<std::string>("attr");
auto type = get_input2<std::string>("type");
auto scope = get_input2<std::string>("scope");
std::visit([&] (auto ty) {
using T = decltype(ty);
auto &arr = prim->verts.add_attr<T>(attr);
auto val = value->get<T>();
for (size_t i = 0; i < arr.size(); i++) {
arr[i] = val;
if (scope == "vert") {
auto &arr = prim->verts.add_attr<T>(attr);
std::fill(arr.begin(), arr.end(), val);
}
else if (scope == "tri") {
auto &arr = prim->tris.add_attr<T>(attr);
std::fill(arr.begin(), arr.end(), val);
}
else if (scope == "loop") {
auto &arr = prim->loops.add_attr<T>(attr);
std::fill(arr.begin(), arr.end(), val);
}
else if (scope == "poly") {
auto &arr = prim->polys.add_attr<T>(attr);
std::fill(arr.begin(), arr.end(), val);
}
else if (scope == "line") {
auto &arr = prim->lines.add_attr<T>(attr);
std::fill(arr.begin(), arr.end(), val);
}
}, enum_variant<std::variant<
float, vec3f, int
Expand All @@ -34,13 +51,14 @@ struct PrimFillAttr : INode {

ZENDEFNODE(PrimFillAttr, {
{
{"PrimitiveObject", "prim"},
{"string", "attr", "rad"},
{"enum float vec3f int", "type", "float"},
{"float", "value", "0"},
{"PrimitiveObject", "prim"},
{"enum vert tri loop poly line", "scope", "vert"},
{"string", "attr", "rad"},
{"enum float vec3f int", "type", "float"},
{"float", "value", "0"},
},
{
{"PrimitiveObject", "prim"},
{"PrimitiveObject", "prim"},
},
{
},
Expand Down
7 changes: 4 additions & 3 deletions zeno/src/nodes/prim/PrimitiveAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ struct PrimitiveDelAttr : zeno::INode {

prim->verts.attrs.erase(name);
prim->tris.attrs.erase(name);
prim->quads.attrs.erase(name);
prim->lines.attrs.erase(name);
prim->polys.attrs.erase(name);
prim->loops.attrs.erase(name);

set_output("prim", get_input("prim"));
Expand Down Expand Up @@ -114,7 +115,7 @@ ZENDEFNODE(PrimitiveGetAttrValue,
{"string", "name", "pos"},
{"enum float float3", "type", "float3"},
}, /* category: */ {
"primitive",
"deprecated",
} });

struct PrimitiveSetAttrValue : zeno::INode {
Expand Down Expand Up @@ -154,6 +155,6 @@ ZENDEFNODE(PrimitiveSetAttrValue,
{"string", "name", "pos"},
{"enum float float3", "type", "float3"},
}, /* category: */ {
"primitive",
"deprecated",
} });
}
146 changes: 107 additions & 39 deletions zeno/src/nodes/prim/WBPrimBend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,30 +616,61 @@ ZENDEFNODE(TracePositionOneStep,
struct PrimCopyAttr : INode {
void apply() override {
auto prim = get_input<PrimitiveObject>("prim");
auto sourceName = get_input<StringObject>("sourceName")->get();
auto targetName = get_input<StringObject>("targetName")->get();
auto type = get_input2<std::string>("type");

if (!prim->verts.has_attr(sourceName))
zeno::log_error("no such attr named '{}'.", sourceName);
// if (prim->verts.has_attr(targetName))
// zeno::log_warn("already has such attr named '{}'.", targetName);
auto sourceName = get_input2<std::string>("sourceName");
auto targetName = get_input2<std::string>("targetName");
auto scope = get_input2<std::string>("scope");

std::visit(
[&](auto ty) {
using T = decltype(ty);

auto &sourceAttr = prim->verts.attr<T>(sourceName); // 源属性
auto &targetAttr = prim->verts.add_attr<T>(targetName); // 目标属性
#pragma omp parallel for
for (intptr_t i = 0; i < prim->verts.size(); i++) {
targetAttr[i] = sourceAttr[i];
}
},
enum_variant<std::variant<float, vec2f, vec3f, vec4f, int, vec2i, vec3i, vec4i>>(
array_index({"float", "vec2f", "vec3f", "vec4f", "int", "vec2i", "vec3i", "vec4i"}, type)));
if (scope == "vert") {
if (!prim->verts.has_attr(sourceName)) {
zeno::log_error("verts no such attr named '{}'.", sourceName);
}
prim->verts.attr_visit<AttrAcceptAll>(sourceName, [&] (auto const &attarr) {
using T = std::decay_t<decltype(attarr[0])>;
auto &targetAttr = prim->verts.template add_attr<T>(targetName);
std::copy(attarr.begin(), attarr.end(), targetAttr.begin());
});
}
else if (scope == "tri") {
if (!prim->tris.has_attr(sourceName)) {
zeno::log_error("tris no such attr named '{}'.", sourceName);
}
prim->tris.attr_visit<AttrAcceptAll>(sourceName, [&] (auto const &attarr) {
using T = std::decay_t<decltype(attarr[0])>;
auto &targetAttr = prim->tris.template add_attr<T>(targetName);
std::copy(attarr.begin(), attarr.end(), targetAttr.begin());
});
}
else if (scope == "loop") {
if (!prim->loops.has_attr(sourceName)) {
zeno::log_error("loops no such attr named '{}'.", sourceName);
}
prim->loops.attr_visit<AttrAcceptAll>(sourceName, [&] (auto const &attarr) {
using T = std::decay_t<decltype(attarr[0])>;
auto &targetAttr = prim->loops.template add_attr<T>(targetName);
std::copy(attarr.begin(), attarr.end(), targetAttr.begin());
});
}
else if (scope == "poly") {
if (!prim->polys.has_attr(sourceName)) {
zeno::log_error("polys no such attr named '{}'.", sourceName);
}
prim->polys.attr_visit<AttrAcceptAll>(sourceName, [&] (auto const &attarr) {
using T = std::decay_t<decltype(attarr[0])>;
auto &targetAttr = prim->polys.template add_attr<T>(targetName);
std::copy(attarr.begin(), attarr.end(), targetAttr.begin());
});
}
else if (scope == "line") {
if (!prim->lines.has_attr(sourceName)) {
zeno::log_error("lines no such attr named '{}'.", sourceName);
}
prim->lines.attr_visit<AttrAcceptAll>(sourceName, [&] (auto const &attarr) {
using T = std::decay_t<decltype(attarr[0])>;
auto &targetAttr = prim->lines.template add_attr<T>(targetName);
std::copy(attarr.begin(), attarr.end(), targetAttr.begin());
});
}

// prim->verts.erase_attr(sourceName);
set_output("prim", std::move(prim));
}
};
Expand All @@ -648,7 +679,7 @@ ZENDEFNODE(PrimCopyAttr,
"prim",
{"string", "sourceName", "s"},
{"string", "targetName", "t"},
{"enum float vec2f vec3f vec4f int vec2i vec3i vec4i", "type", "float"},
{"enum vert tri loop poly line", "scope", "vert"},
}, /* outputs: */ {
"prim",
}, /* params: */ {
Expand Down Expand Up @@ -859,22 +890,29 @@ struct PrimSetAttr : INode {
[&](auto ty) {
using T = decltype(ty);

auto val = value->get<T>();
if (method == "vert") {
if (!prim->has_attr(name)) {
prim->add_attr<T>(name);
}
auto &attr_arr = prim->attr<T>(name);

auto val = value->get<T>();
auto &attr_arr = prim->add_attr<T>(name);
if (index < attr_arr.size()) {
attr_arr[index] = val;
}
} else if (method == "tri") {
if (!prim->tris.has_attr(name)) {
prim->tris.add_attr<T>(name);
auto &attr_arr = prim->tris.add_attr<T>(name);
if (index < attr_arr.size()) {
attr_arr[index] = val;
}
auto &attr_arr = prim->tris.attr<T>(name);
auto val = value->get<T>();
} else if (method == "line") {
auto &attr_arr = prim->lines.add_attr<T>(name);
if (index < attr_arr.size()) {
attr_arr[index] = val;
}
} else if (method == "loop") {
auto &attr_arr = prim->loops.add_attr<T>(name);
if (index < attr_arr.size()) {
attr_arr[index] = val;
}
} else if (method == "poly") {
auto &attr_arr = prim->polys.add_attr<T>(name);
if (index < attr_arr.size()) {
attr_arr[index] = val;
}
Expand All @@ -894,7 +932,7 @@ ZENDEFNODE(PrimSetAttr,
{"int", "value", "0"},
{"string", "name", "index"},
{"enum float vec2f vec3f vec4f int vec2i vec3i vec4i", "type", "int"},
{"enum vert tri", "method", "tri"},
{"enum vert tri line loop poly", "method", "tri"},
{"int", "index", "0"},
}, /* outputs: */ {
"prim",
Expand Down Expand Up @@ -928,6 +966,21 @@ struct PrimGetAttr : INode {
if (index < attr_arr.size()) {
value->set<T>(attr_arr[index]);
}
} else if (method == "line") {
auto &attr_arr = prim->lines.attr<T>(name);
if (index < attr_arr.size()) {
value->set<T>(attr_arr[index]);
}
} else if (method == "loop") {
auto &attr_arr = prim->loops.attr<T>(name);
if (index < attr_arr.size()) {
value->set<T>(attr_arr[index]);
}
} else if (method == "poly") {
auto &attr_arr = prim->polys.attr<T>(name);
if (index < attr_arr.size()) {
value->set<T>(attr_arr[index]);
}
} else {
throw Exception("bad type: " + method);
}
Expand All @@ -943,7 +996,7 @@ ZENDEFNODE(PrimGetAttr,
"prim",
{"string", "name", "index"},
{"enum float vec2f vec3f vec4f int vec2i vec3i vec4i", "type", "int"},
{"enum vert tri", "method", "tri"},
{"enum vert tri line loop poly", "method", "tri"},
{"int", "index", "0"},
}, /* outputs: */ {
"value",
Expand All @@ -970,7 +1023,7 @@ struct PrimitiveDelAttrs : zeno::INode {
for(std::string attr : names) {
prim->verts.attrs.erase(attr);
prim->tris.attrs.erase(attr);
prim->quads.attrs.erase(attr);
prim->lines.attrs.erase(attr);
prim->loops.attrs.erase(attr);
prim->polys.attrs.erase(attr);
}
Expand All @@ -985,7 +1038,7 @@ struct PrimitiveDelAttrs : zeno::INode {
for(std::string attr : myKeys){
prim->verts.attrs.erase(attr);
prim->tris.attrs.erase(attr);
prim->quads.attrs.erase(attr);
prim->lines.attrs.erase(attr);
prim->loops.attrs.erase(attr);
prim->polys.attrs.erase(attr);
}
Expand Down Expand Up @@ -1737,10 +1790,24 @@ struct PrimHasAttr : INode {
void apply() override {
auto prim = get_input<PrimitiveObject>("prim");
auto attrName = get_input2<std::string>("attrName");
auto scope = get_input2<std::string>("scope");

bool x = false;
if (prim->verts.has_attr(attrName))
x = true;
if (scope == "vert") {
x = prim->verts.has_attr(attrName);
}
else if (scope == "tri") {
x = prim->tris.has_attr(attrName);
}
else if (scope == "loop") {
x = prim->loops.has_attr(attrName);
}
else if (scope == "poly") {
x = prim->polys.has_attr(attrName);
}
else if (scope == "line") {
x = prim->lines.has_attr(attrName);
}

auto hasAttr = std::make_shared<NumericObject>();
hasAttr->set<bool>(x);
Expand All @@ -1750,6 +1817,7 @@ struct PrimHasAttr : INode {
ZENDEFNODE(PrimHasAttr,
{ /* inputs: */ {
"prim",
{"enum vert tri loop poly line", "scope", "vert"},
{"string", "attrName", "attr_x"},
}, /* outputs: */ {
"hasAttr",
Expand Down

0 comments on commit 61fb01f

Please sign in to comment.