Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform pivot #1690

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions ui/zenoedit/viewportinteraction/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,10 @@ void FakeTransformer::addObject(const std::string& name) {
return;

auto object = dynamic_cast<PrimitiveObject*>(scene->objectsMan->get(name).value());
m_objects_center *= m_objects.size();
auto& user_data = object->userData();
zeno::vec3f bmin, bmax;
if (user_data.has("_bboxMin") && user_data.has("_bboxMax")) {
bmin = user_data.getLiterial<zeno::vec3f>("_bboxMin");
bmax = user_data.getLiterial<zeno::vec3f>("_bboxMax");
} else {
std::tie(bmin, bmax) = zeno::primBoundingBox(object);
user_data.setLiterial("_bboxMin", bmin);
user_data.setLiterial("_bboxMax", bmax);
}
if (!user_data.has("_pivot")) {
zeno::vec3f bmin, bmax;
std::tie(bmin, bmax) = zeno::primBoundingBox(object);
zeno::vec3f translate = {0, 0, 0};
user_data.setLiterial("_translate", translate);
zeno::vec4f rotate = {0, 0, 0, 1};
Expand All @@ -83,10 +75,9 @@ void FakeTransformer::addObject(const std::string& name) {
object->verts.add_attr<zeno::vec3f>("_origin_nrm") = nrm;
}
}
auto m = zeno::vec_to_other<glm::vec3>(bmax);
auto n = zeno::vec_to_other<glm::vec3>(bmin);
m_pivot = zeno::vec_to_other<glm::vec3>(user_data.get2<vec3f>("_pivot"));
m_objects_center += (m + n) / 2.0f;
m_objects_center *= m_objects.size();
m_objects_center += m_pivot + zeno::vec_to_other<glm::vec3>(user_data.get2<vec3f>("_translate"));
m_objects[name] = object;
m_objects_center /= m_objects.size();
}
Expand Down Expand Up @@ -315,6 +306,7 @@ bool FakeTransformer::isTransforming() const {
}

void FakeTransformer::startTransform() {
_objects_center_start = m_objects_center;
markObjectsInteractive();
}

Expand Down Expand Up @@ -677,9 +669,10 @@ void FakeTransformer::rotate(glm::vec3 start_vec, glm::vec3 end_vec, glm::vec3 a

void FakeTransformer::doTransform() {
// qDebug() << "transformer's objects count " << m_objects.size();
glm::vec3 new_objects_center = {0, 0, 0};
for (auto &[obj_name, obj] : m_objects) {
auto& user_data = obj->userData();
user_data.del("_bboxMin");
user_data.del("_bboxMax");

// get transform info
auto translate = zeno::vec_to_other<glm::vec3>(user_data.getLiterial<zeno::vec3f>("_translate"));
Expand Down Expand Up @@ -722,17 +715,8 @@ void FakeTransformer::doTransform() {
onrm[i] = zeno::other_to_vec<3>(t);
}
}
vec3f bmin, bmax;
if (user_data.has("_bboxMin") && user_data.has("_bboxMax")) {
std::tie(bmin, bmax) = primBoundingBox(obj);
user_data.setLiterial("_bboxMin", bmin);
user_data.setLiterial("_bboxMax", bmax);
}
new_objects_center += (zeno::vec_to_other<glm::vec3>(bmin) + zeno::vec_to_other<glm::vec3>(bmax)) / 2.0f;
}

new_objects_center /= m_objects.size();
m_objects_center = new_objects_center;
m_objects_center = _objects_center_start + m_trans;
m_handler->setCenter({m_objects_center[0], m_objects_center[1], m_objects_center[2]});
}

Expand Down
1 change: 1 addition & 0 deletions ui/zenoedit/viewportinteraction/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class FakeTransformer {
glm::vec3 m_trans_start;
glm::vec3 m_rotate_start;
// glm::vec3 m_scale_start;
glm::vec3 _objects_center_start;

bool m_status;
int m_operation;
Expand Down
28 changes: 18 additions & 10 deletions zeno/src/nodes/prim/TransformPrimitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "zeno/funcs/ObjectGeometryInfo.h"
#include "zeno/types/ListObject.h"
#include "zeno/utils/log.h"
#include "zeno/funcs/PrimitiveUtils.h"
#include <cstring>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
Expand Down Expand Up @@ -241,6 +242,7 @@ struct PrimitiveTransform : zeno::INode {
std::shared_ptr<IObject> iObject
, glm::mat4 matrix
, std::string pivotType
, vec3f pivotPos
, vec3f translate
, vec4f rotation
, vec3f scaling
Expand All @@ -251,13 +253,15 @@ struct PrimitiveTransform : zeno::INode {
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;
std::tie(_min, _max) = primBoundingBox(prim.get());
_pivot = (_min + _max) / 2;
}
else if (pivotType == "custom") {
_pivot = pivotPos;
}
auto pivot_to_local = glm::translate(glm::vec3(-_pivot[0], -_pivot[1], -_pivot[2]));
auto pivot_to_world = glm::translate(glm::vec3(_pivot[0], _pivot[1], _pivot[2]));
matrix = pivot_to_world * matrix * pivot_to_local;

if (prim->has_attr("pos")) {
auto &pos = prim->attr<zeno::vec3f>("pos");
Expand Down Expand Up @@ -286,10 +290,12 @@ struct PrimitiveTransform : zeno::INode {
user_data.setLiterial("_rotate", rotation);
user_data.setLiterial("_scale", scaling);
user_data.set2("_pivot", _pivot);
user_data.del("_bboxMin");
user_data.del("_bboxMax");
}
else if (auto list = std::dynamic_pointer_cast<ListObject>(iObject)) {
for (auto &item : list->arr) {
transformObj(item, matrix, pivotType, translate, rotation, scaling);
transformObj(item, matrix, pivotType, translate, pivotPos, rotation, scaling);
}
}
}
Expand Down Expand Up @@ -358,15 +364,16 @@ struct PrimitiveTransform : zeno::INode {
auto path = get_input2<std::string>("path");

std::string pivotType = get_input2<std::string>("pivot");
auto pivotPos = get_input2<vec3f>("pivotPos");

if (std::dynamic_pointer_cast<PrimitiveObject>(iObject)) {
iObject = iObject->clone();
transformObj(iObject, matrix, pivotType, translate, rotation, scaling);
transformObj(iObject, matrix, pivotType, pivotPos, translate, rotation, scaling);
}
else {
auto select = get_from_list(path, iObject);
if (select.has_value()) {
transformObj(select.value(), matrix, pivotType, translate, rotation, scaling);
transformObj(select.value(), matrix, pivotType, pivotPos, translate, rotation, scaling);
}
}

Expand All @@ -378,12 +385,13 @@ ZENDEFNODE(PrimitiveTransform, {
{
{"PrimitiveObject", "prim"},
{"string", "path"},
{"enum world bboxCenter", "pivot", "bboxCenter"},
{"vec3f", "translation", "0,0,0"},
{"vec3f", "eulerXYZ", "0,0,0"},
{"vec4f", "quatRotation", "0,0,0,1"},
{"vec3f", "scaling", "1,1,1"},
{"vec3f", "shear", "0,0,0"},
{"enum world bboxCenter custom", "pivot", "bboxCenter"},
{"vec3f", "pivotPos", "0,0,0"},
{"Matrix"},
{"preTransform"},
{"local"},
Expand Down
Loading