From 13b569983a8fdc699fb0ae98f2496599dfada773 Mon Sep 17 00:00:00 2001 From: zhouhang95 <765229842@qq.com> Date: Fri, 22 Dec 2023 16:57:32 +0800 Subject: [PATCH 1/2] path-PrimitiveTransform --- zeno/src/nodes/prim/TransformPrimitive.cpp | 137 ++++++++++++++------- 1 file changed, 93 insertions(+), 44 deletions(-) diff --git a/zeno/src/nodes/prim/TransformPrimitive.cpp b/zeno/src/nodes/prim/TransformPrimitive.cpp index 9f0d3c3038..6474f4eb29 100644 --- a/zeno/src/nodes/prim/TransformPrimitive.cpp +++ b/zeno/src/nodes/prim/TransformPrimitive.cpp @@ -1,5 +1,7 @@ #include "zeno/types/UserData.h" #include "zeno/funcs/ObjectGeometryInfo.h" +#include "zeno/types/ListObject.h" +#include "zeno/utils/log.h" #include #include #include @@ -11,6 +13,7 @@ #include #include #include +#include namespace zeno { namespace { @@ -208,6 +211,89 @@ struct PrimitiveTransform : zeno::INode { return glm::normalize(vector3); } + static std::optional> get_from_list(std::string path, std::shared_ptr iObject) { + if (path.empty() || path == "/") { + return iObject; + } + auto cur_root = iObject; + auto idxs = split_str(path, '/'); + std::vector idxs_int; + for (const auto &idx: idxs) { + if (idx.empty()) { + continue; + } + auto i = std::stoi(idx); + if (auto list = std::dynamic_pointer_cast(cur_root)) { + if (i >= list->arr.size()) { + zeno::log_info("out of range"); + return std::nullopt; + } + cur_root = list->arr[i]; + } + else { + return cur_root; + } + } + zeno::log_info("missing"); + return cur_root; + } + + static void transformObj( + std::shared_ptr iObject + , glm::mat4 matrix + , std::string pivotType + , vec3f translate + , vec4f rotation + , vec3f scaling + ) { + if (auto prim = std::dynamic_pointer_cast(iObject)) { + + zeno::vec3f _pivot = {}; + if (pivotType == "bboxCenter") { + zeno::vec3f _min; + zeno::vec3f _max; + objectGetBoundingBox(prim.get(), _min, _max); + auto p = (_min + _max) / 2; + auto pivot_to_local = glm::translate(glm::vec3(-p[0], -p[1], -p[2])); + auto pivot_to_world = glm::translate(glm::vec3(p[0], p[1], p[2])); + matrix = pivot_to_world * matrix * pivot_to_local; + _pivot = p; + } + + if (prim->has_attr("pos")) { + auto &pos = prim->attr("pos"); + prim->verts.add_attr("_origin_pos") = pos; + #pragma omp parallel for + for (int i = 0; i < pos.size(); i++) { + auto p = zeno::vec_to_other(pos[i]); + p = mapplypos(matrix, p); + pos[i] = zeno::other_to_vec<3>(p); + } + } + + if (prim->has_attr("nrm")) { + auto &nrm = prim->attr("nrm"); + prim->verts.add_attr("_origin_nrm") = nrm; + #pragma omp parallel for + for (int i = 0; i < nrm.size(); i++) { + auto n = zeno::vec_to_other(nrm[i]); + n = mapplynrm(matrix, n); + nrm[i] = zeno::other_to_vec<3>(n); + } + } + + auto& user_data = prim->userData(); + user_data.setLiterial("_translate", translate); + user_data.setLiterial("_rotate", rotation); + user_data.setLiterial("_scale", scaling); + user_data.set2("_pivot", _pivot); + } + else if (auto list = std::dynamic_pointer_cast(iObject)) { + for (auto &item : list->arr) { + transformObj(item, matrix, pivotType, translate, rotation, scaling); + } + } + } virtual void apply() override { zeno::vec3f translate = {0,0,0}; zeno::vec4f rotation = {0,0,0,1}; @@ -269,59 +355,22 @@ struct PrimitiveTransform : zeno::INode { auto matrix = pre_mat*local*matTrans*matRotate*matQuat*matScal*matShearZ*matShearY*matShearX*glm::translate(glm::vec3(offset[0], offset[1], offset[2]))*glm::inverse(local)*pre_apply; - auto prim = get_input("prim"); + auto iObject = get_input2("prim"); + auto path = get_input2("path"); std::string pivotType = get_input2("pivot"); - zeno::vec3f _pivot = {}; - if (pivotType == "bboxCenter") { - zeno::vec3f _min; - zeno::vec3f _max; - objectGetBoundingBox(prim.get(), _min, _max); - auto p = (_min + _max) / 2; - auto pivot_to_local = glm::translate(glm::vec3(-p[0], -p[1], -p[2])); - auto pivot_to_world = glm::translate(glm::vec3(p[0], p[1], p[2])); - matrix = pivot_to_world * matrix * pivot_to_local; - _pivot = p; - } - - auto outprim = std::make_unique(*prim); - - if (prim->has_attr("pos")) { - auto &pos = outprim->attr("pos"); - outprim->verts.add_attr("_origin_pos") = pos; -#pragma omp parallel for - for (int i = 0; i < pos.size(); i++) { - auto p = zeno::vec_to_other(pos[i]); - p = mapplypos(matrix, p); - pos[i] = zeno::other_to_vec<3>(p); - } + auto select = get_from_list(path, iObject); + if (select.has_value()) { + transformObj(select.value(), matrix, pivotType, translate, rotation, scaling); } - if (prim->has_attr("nrm")) { - auto &nrm = outprim->attr("nrm"); - outprim->verts.add_attr("_origin_nrm") = nrm; -#pragma omp parallel for - for (int i = 0; i < nrm.size(); i++) { - auto n = zeno::vec_to_other(nrm[i]); - n = mapplynrm(matrix, n); - nrm[i] = zeno::other_to_vec<3>(n); - } - } - - auto& user_data = outprim->userData(); - user_data.setLiterial("_translate", translate); - vec4f rotate = {myQuat.x, myQuat.y, myQuat.z, myQuat.w}; - user_data.setLiterial("_rotate", rotate); - user_data.setLiterial("_scale", scaling); - user_data.set2("_pivot", _pivot); - //auto oMat = std::make_shared(); - //oMat->m = matrix; - set_output("outPrim", std::move(outprim)); + set_output("outPrim", std::move(iObject)); } }; ZENDEFNODE(PrimitiveTransform, { { + {"string", "path"}, {"PrimitiveObject", "prim"}, {"enum world bboxCenter", "pivot", "bboxCenter"}, {"vec3f", "translation", "0,0,0"}, From 36fd6da318be4f0a23cc94a6f0089c5bab53cf81 Mon Sep 17 00:00:00 2001 From: zhouhang95 <765229842@qq.com> Date: Fri, 22 Dec 2023 17:01:46 +0800 Subject: [PATCH 2/2] remove log --- zeno/src/nodes/prim/TransformPrimitive.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zeno/src/nodes/prim/TransformPrimitive.cpp b/zeno/src/nodes/prim/TransformPrimitive.cpp index 6474f4eb29..52a5c992f6 100644 --- a/zeno/src/nodes/prim/TransformPrimitive.cpp +++ b/zeno/src/nodes/prim/TransformPrimitive.cpp @@ -225,7 +225,7 @@ struct PrimitiveTransform : zeno::INode { auto i = std::stoi(idx); if (auto list = std::dynamic_pointer_cast(cur_root)) { if (i >= list->arr.size()) { - zeno::log_info("out of range"); + zeno::log_warn("out of range"); return std::nullopt; } cur_root = list->arr[i]; @@ -234,7 +234,6 @@ struct PrimitiveTransform : zeno::INode { return cur_root; } } - zeno::log_info("missing"); return cur_root; }