tahoma2d/toonz/sources/tnztools/plastictool_build.cpp

939 lines
28 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "tundo.h"
// TnzExt includes
#include "ext/plasticdeformerstorage.h"
// TnzLib includes
#include "toonz/txshcell.h"
#include "toonz/txshsimplelevel.h"
#include "toonz/txsheethandle.h"
#include "toonz/stage.h"
// tcg includes
#include "tcg/tcg_point_ops.h"
#include "plastictool.h"
using namespace PlasticToolLocals;
//****************************************************************************************
// Local namespace stuff
//****************************************************************************************
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TPointD closestMeshVertexPos(const TPointD &pos, double *distance = 0) {
const TXshCell &imageCell = TTool::getImageCell();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *sl = imageCell.getSimpleLevel();
assert(sl);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TMeshImageP mi(TTool::getImage(false));
assert(mi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Retrieve *level* dpi
// NOTE: This is different than current IMAGE's dpi. An image is actually
// displayed with
// its level owner's dpi, RATHER than its own.
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TPointD &dpi = sl->getDpi(imageCell.getFrameId());
assert(dpi.x > 0.0 && dpi.y > 0.0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Cast pos to image coordinates
const TPointD pos_mesh(pos.x * (dpi.x / Stage::inch),
pos.y * (dpi.y / Stage::inch));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Retrieve the closest vertex to pos_mesh
const std::pair<double, PlasticTool::MeshIndex> &closest =
PlasticToolLocals::closestVertex(*mi, pos_mesh);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TPointD &vxPos_mesh =
mi->meshes()[closest.second.m_meshIdx]->vertex(closest.second.m_idx).P();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (distance)
*distance =
std::min(Stage::inch / dpi.x, Stage::inch / dpi.y) * closest.first;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Cast it back to world coordinates
return TPointD(vxPos_mesh.x * (Stage::inch / dpi.x),
vxPos_mesh.y * (Stage::inch / dpi.y));
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TPointD closestSkeletonVertexPos(const TPointD &pos) {
const PlasticSkeletonP &skeleton = l_plasticTool.skeleton();
if (!skeleton || skeleton->empty()) return TConsts::napd;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const PlasticSkeleton::vertices_container &vertices = skeleton->vertices();
2016-03-19 06:57:51 +13:00
2019-02-20 20:43:51 +13:00
return std::min_element(vertices.begin(), vertices.end(),
[&pos](PlasticSkeleton::vertex_type const &x,
PlasticSkeleton::vertex_type const &y) {
return tcg::point_ops::dist2(pos, x.P()) <
tcg::point_ops::dist2(pos, y.P());
})
2016-06-15 18:43:10 +12:00
->P();
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
} // namespace
2016-03-19 06:57:51 +13:00
//****************************************************************************************
// Undo definitions
//****************************************************************************************
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
class VertexUndo : public TUndo {
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
int m_row, m_col; //!< Xsheet coordinates
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int m_v, m_vParent; //!< Indices of the added vertex and its parent
PlasticSkeletonVertex m_vx; //!< Added vertex
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<int> m_children; //!< Children of the vertex to insert
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
VertexUndo() : m_row(::row()), m_col(::column()), m_v(-1), m_vParent(-1) {}
2016-03-19 06:57:51 +13:00
2016-06-20 14:23:05 +12:00
int getSize() const override {
return sizeof(*this);
} // sizeof this is roughly ok
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void storeChildren(const PlasticSkeleton &skeleton,
const PlasticSkeletonVertex &vx) {
m_children.clear();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Traverse vx's edges and build its children table
PlasticSkeletonVertex::edges_const_iterator et, eEnd(vx.edgesEnd());
for (et = vx.edgesBegin(); et != eEnd; ++et) {
int vChild = skeleton.edge(*et).vertex(1);
if (vChild == vx.getIndex()) continue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_children.push_back(vChild);
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void addVertex() {
assert(m_vx.edges().empty());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Load the right skeleton by moving to the stored xsheet pos
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const PlasticSkeletonP &skeleton = l_plasticTool.skeleton();
TCG_ASSERT(skeleton || m_vParent < 0, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Perform addition
l_plasticTool.setSkeletonSelection(m_vParent);
l_plasticTool.addVertex(m_vx);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Store data to invert the operation
assert(l_plasticTool.skeletonVertexSelection().hasSingleObject());
m_v = l_plasticTool.skeletonVertexSelection();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void insertVertex() {
if (m_children.empty()) {
addVertex();
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(m_vx.edges().empty());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TCG_ASSERT(m_vParent >= 0, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const PlasticSkeletonP &skeleton = l_plasticTool.skeleton();
TCG_ASSERT(skeleton, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Perform insertion
l_plasticTool.insertVertex(m_vx, m_vParent, m_children);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Store data to invert the operation
assert(l_plasticTool.skeletonVertexSelection().hasSingleObject());
m_v = l_plasticTool.skeletonVertexSelection();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void removeVertex() {
TCG_ASSERT(m_v >= 0, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const PlasticSkeletonP &skeleton = l_plasticTool.skeleton();
TCG_ASSERT(skeleton, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Store data to invert the operation
{
const PlasticSkeletonVertex &vx = skeleton->vertex(m_v);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_vParent = vx.parent();
m_vx = PlasticSkeletonVertex(vx.P());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
storeChildren(*skeleton, vx);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Perform removal
if (m_v > 0) {
l_plasticTool.setSkeletonSelection(m_v);
l_plasticTool.removeVertex();
} else
l_plasticTool.removeSkeleton(::skeletonId());
}
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------
class AddVertexUndo final : public VertexUndo {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
AddVertexUndo(int vParent, const PlasticSkeletonVertex &vx) {
m_vParent = vParent, m_vx = vx;
assert(m_vx.edges().empty());
}
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
const_cast<AddVertexUndo &>(*this).VertexUndo::addVertex();
}
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
const_cast<AddVertexUndo &>(*this).VertexUndo::removeVertex();
}
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------
class RemoveVertexUndo final : public VertexUndo {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
RemoveVertexUndo(int v) {
assert(v >= 0);
m_v = v;
}
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
const_cast<RemoveVertexUndo &>(*this).VertexUndo::removeVertex();
}
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
const_cast<RemoveVertexUndo &>(*this).VertexUndo::insertVertex();
}
2016-03-19 06:57:51 +13:00
};
//========================================================================
class InsertVertexUndo final : public VertexUndo {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
InsertVertexUndo(int e, const PlasticSkeletonVertex &vx) {
const PlasticSkeleton &skeleton = *l_plasticTool.skeleton();
const PlasticSkeleton::edge_type &ed = skeleton.edge(e);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_vParent = ed.vertex(0), m_vx = vx;
std::vector<int>(1, ed.vertex(1)).swap(m_children);
}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
const_cast<InsertVertexUndo &>(*this).VertexUndo::insertVertex();
}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
TCG_ASSERT(!m_children.empty(), return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const_cast<InsertVertexUndo &>(*this).VertexUndo::removeVertex();
}
};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//========================================================================
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
class AddSkeletonUndo : public TUndo {
protected:
int m_row, m_col; //!< Xsheet coordinates
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int m_skelId; //!< The added skeleton's id
PlasticSkeletonP m_skeleton; //!< A COPY of the added skeleton
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
public:
AddSkeletonUndo(int skelId, const PlasticSkeletonP &skeletonCopy)
: m_row(::row())
, m_col(::column())
, m_skelId(skelId)
, m_skeleton(skeletonCopy) {}
// This is not correct... it's tough to build the right one! We're like
// storing the whole
// cleared deformation! So, I guess 1 MB (100 of these in the standard undos
// pool)
// is a reasonable estimate...
2016-06-19 20:06:29 +12:00
int getSize() const override { return 1 << 20; }
2016-06-15 18:43:10 +12:00
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
l_plasticTool.addSkeleton(m_skelId, new PlasticSkeleton(*m_skeleton));
::invalidateXsheet();
}
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
l_plasticTool.removeSkeleton(m_skelId);
}
2016-03-19 06:57:51 +13:00
};
//========================================================================
2016-06-15 18:43:10 +12:00
class RemoveSkeletonUndo : public AddSkeletonUndo {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
RemoveSkeletonUndo(int skelId)
: AddSkeletonUndo(skelId, l_plasticTool.skeleton()) {}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void redo() const override { AddSkeletonUndo::undo(); }
void undo() const override { AddSkeletonUndo::redo(); }
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------
class RemoveSkeletonUndo_WithKeyframes final : public RemoveSkeletonUndo {
2016-06-15 18:43:10 +12:00
mutable std::vector<TDoubleKeyframe>
m_skelIdsKeyframes; //!< Skeleton Ids param curve keyframes
//!< for m_skelId
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
RemoveSkeletonUndo_WithKeyframes(int skelId) : RemoveSkeletonUndo(skelId) {}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
// Erase all keyframes corresponding to m_skelId from sd's skeleton ids
// curve
const SkDP &sd = l_plasticTool.deformation();
TCG_ASSERT(sd, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TDoubleParamP &skelIdsParam = sd->skeletonIdsParam();
if (skelIdsParam->getKeyframeCount() > 0) {
double frame;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int k = 0; k >= 0; k = skelIdsParam->getNextKeyframe(frame)) {
const TDoubleKeyframe &kf = skelIdsParam->getKeyframe(k);
frame = kf.m_frame;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_skelId == (int)kf.m_value) {
m_skelIdsKeyframes.push_back(kf);
skelIdsParam->deleteKeyframe(frame);
}
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
RemoveSkeletonUndo::redo(); // Invalidates the xsheet
}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
l_plasticTool
.touchDeformation(); // Skeleton removal could have destroyed the sd
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Restore saved keyframes to sd's skelIdsParam curve
const SkDP &sd = l_plasticTool.deformation();
assert(sd);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TDoubleParamP &skelIdsParam = sd->skeletonIdsParam();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<TDoubleKeyframe>::iterator kt, kEnd(m_skelIdsKeyframes.end());
for (kt = m_skelIdsKeyframes.begin(); kt != kEnd; ++kt)
skelIdsParam->setKeyframe(*kt);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_skelIdsKeyframes.clear();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
RemoveSkeletonUndo::undo(); // Invalidates the xsheet
}
2016-03-19 06:57:51 +13:00
};
//========================================================================
class SetSkeletonIdUndo final : public TUndo {
2016-06-15 18:43:10 +12:00
int m_row, m_col; //!< Xsheet coordinates
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int m_skelId; //!< The new skeleton id value
mutable TDoubleKeyframe
m_oldKf; //!< Old keyframe values for skelIds parameter
mutable bool m_added1stKeyframe; //!< Whether the redo() added the first
2016-06-20 14:23:05 +12:00
//! skelIds keyframe
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
SetSkeletonIdUndo(int skelId)
: m_row(::row()), m_col(::column()), m_skelId(skelId) {}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
int getSize() const override { return sizeof(*this); }
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const SkDP &sd = l_plasticTool.deformation();
TCG_ASSERT(sd, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TDoubleParamP &skelIdsParam = sd->skeletonIdsParam();
double frame = ::frame();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_oldKf = skelIdsParam->getKeyframeAt(frame);
m_added1stKeyframe = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (frame > 0.0 && (skelIdsParam->getKeyframeCount() == 0 ||
skelIdsParam->getKeyframe(0).m_frame >= frame)) {
// Put a keyframe at the previous cell to preserve values before current
// frame
TDoubleKeyframe kf(frame - 1.0, skelIdsParam->getDefaultValue());
kf.m_type = TDoubleKeyframe::Constant;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
skelIdsParam->setKeyframe(kf);
m_added1stKeyframe = true;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TDoubleKeyframe kf(frame, m_skelId);
kf.m_type = TDoubleKeyframe::Constant;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
skelIdsParam->setKeyframe(kf);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// No need to invoke PlasticTool::storeSkeletonId() - automatic through
// onChange()
}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const SkDP &sd = l_plasticTool.deformation();
TCG_ASSERT(sd, return );
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TDoubleParamP &skelIdsParam = sd->skeletonIdsParam();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_oldKf.m_isKeyframe)
skelIdsParam->setKeyframe(m_oldKf);
else
skelIdsParam->deleteKeyframe(m_oldKf.m_frame);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_added1stKeyframe) {
const TDoubleKeyframe &kf = skelIdsParam->getKeyframe(0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(kf.m_value == skelIdsParam->getDefaultValue());
if (kf.m_value == skelIdsParam->getDefaultValue())
skelIdsParam->deleteKeyframe(kf.m_frame);
}
}
2016-03-19 06:57:51 +13:00
};
//========================================================================
class MoveVertexUndo_Build final : public TUndo {
2016-06-15 18:43:10 +12:00
int m_row, m_col; //!< Xsheet coordinates
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<int> m_vIdxs; //!< Moved vertices
std::vector<TPointD> m_origVxsPos; //!< Original vertex positions
TPointD m_posShift; //!< Vertex positions shift
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
MoveVertexUndo_Build(const std::vector<int> &vIdxs,
const std::vector<TPointD> &origVxsPos,
const TPointD &posShift)
: m_row(::row())
, m_col(::column())
, m_vIdxs(vIdxs)
, m_origVxsPos(origVxsPos)
, m_posShift(posShift) {
assert(m_vIdxs.size() == m_origVxsPos.size());
}
2016-06-19 20:06:29 +12:00
int getSize() const override {
2016-06-15 18:43:10 +12:00
return int(sizeof(*this) +
m_vIdxs.size() * (sizeof(int) + 2 * sizeof(TPointD)));
}
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
l_plasticTool.setSkeletonSelection(m_vIdxs);
l_plasticTool.moveVertex_build(m_origVxsPos, m_posShift);
::stageObject()
->invalidate(); // Should be a TStageObject's implementation detail ...
l_plasticTool.invalidate();
}
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
l_plasticTool.setSkeletonSelection(m_vIdxs);
l_plasticTool.moveVertex_build(m_origVxsPos, TPointD());
::stageObject()
->invalidate(); // Should be a TStageObject's implementation detail ...
l_plasticTool.invalidate();
}
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace
2016-03-19 06:57:51 +13:00
//****************************************************************************************
// PlasticTool functions
//****************************************************************************************
2016-06-15 18:43:10 +12:00
void PlasticTool::mouseMove_build(const TPointD &pos, const TMouseEvent &me) {
// Track mouse position
m_pos = pos; // Needs to be done now - ensures m_pos is valid
m_svHigh = m_seHigh = -1; // Reset highlighted primitives
double d, highlightRadius = getPixelSize() * HIGHLIGHT_DISTANCE;
const PlasticSkeletonP &skeleton = this->skeleton();
if (skeleton) {
// Search nearest skeleton entities
// Look for nearest vertex
int v = skeleton->closestVertex(pos, &d);
if (v >= 0 && d < highlightRadius)
m_svHigh = v;
else {
// Look for nearest edge
int e = skeleton->closestEdge(pos, &d);
if (e >= 0 && d < highlightRadius) m_seHigh = e;
}
}
if (m_svHigh < 0 && m_seHigh < 0) {
// No highlighted skeleton primitive. Mouse position may be
// a candidate for vertex addition - snap to mesh if required
if (m_snapToMesh.getValue()) {
const TPointD &mvPos = ::closestMeshVertexPos(
pos, &d); // No need to check against closest skeleton vertex,
// since vertex highlighting kicks in at the same time
if (d < highlightRadius) // the snapping would take place.
m_pos = mvPos;
}
}
invalidate();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::leftButtonDown_build(const TPointD &pos,
const TMouseEvent &me) {
// Track mouse position
m_pressedPos = m_pos = pos;
// Update selections
{
const PlasticSkeletonP &skel = skeleton();
if (m_svHigh >= 0) {
PlasticVertexSelection vSel(
me.isShiftPressed()
? PlasticVertexSelection(branchSelection(m_svHigh))
: PlasticVertexSelection(m_svHigh));
if (me.isCtrlPressed())
toggleSkeletonSelection(vSel);
else if (!m_svSel.contains(vSel))
setSkeletonSelection(vSel);
} else if (m_seHigh >= 0) {
// Insert a vertex in the edge
TUndo *op = new InsertVertexUndo(
m_seHigh, PlasticSkeletonVertex(projection(*skel, m_seHigh, m_pos)));
TUndoManager::manager()->add(op);
op->redo();
} else if (!skel || skel->empty() || m_svSel.hasSingleObject()) {
// Snap to mesh if required
if (m_snapToMesh.getValue()) {
double d, highlightRadius = getPixelSize() * HIGHLIGHT_DISTANCE;
const TPointD &mvPos = ::closestMeshVertexPos(
pos, &d); // Again, no need to check against closest
// skeleton vertex.
if (d < highlightRadius) m_pos = mvPos;
}
// Add a new vertex
TUndo *op = new AddVertexUndo(m_svSel, PlasticSkeletonVertex(m_pos));
TUndoManager::manager()->add(op);
op->redo();
assert(skeleton());
} else
setSkeletonSelection(-1);
}
// Start move vertex operation
if (!m_svSel.isEmpty()) {
const PlasticSkeletonP &skel = skeleton();
assert(skel);
// Adjust mouse press to the selected skeleton vertex, if necessary
if (m_svSel.hasSingleObject()) m_pressedPos = skel->vertex(m_svSel).P();
// Store original vertex positions
2019-04-16 17:56:35 +12:00
std::vector<TPointD> v;
for (auto const &e : m_svSel.objects()) {
v.push_back(skel->vertex(e).P());
}
m_pressedVxsPos = std::move(v);
2016-06-15 18:43:10 +12:00
}
invalidate();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::leftButtonDrag_build(const TPointD &pos,
const TMouseEvent &me) {
// Track mouse position
if (m_snapToMesh.getValue()) {
const TPointD &mvPos = ::closestMeshVertexPos(pos),
&svPos = ::closestSkeletonVertexPos(mvPos);
if (tcg::point_ops::dist(mvPos, svPos) >
getPixelSize()) // 1) If said distance is sub-pixel, the user
m_pos = mvPos; // just cannot see the assignment - so it's not safe.
} // 2) The moveVertex_build() below manipulates m_pos
else // (not how m_pressedPos is subtracted), so !=
m_pos = pos; // is not a choice.
moveVertex_build(m_pressedVxsPos, m_pos - m_pressedPos);
invalidate();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::leftButtonUp_build(const TPointD &pos,
const TMouseEvent &me) {
// Track mouse position
if (m_snapToMesh.getValue()) {
const TPointD &mvPos = ::closestMeshVertexPos(pos),
&svPos = ::closestSkeletonVertexPos(mvPos);
if (tcg::point_ops::dist(mvPos, svPos) > getPixelSize()) // Same as above
m_pos = mvPos;
} else
m_pos = pos;
if (!m_svSel.isEmpty() && m_dragged) {
TUndoManager::manager()->add(new MoveVertexUndo_Build(
m_svSel.objects(), m_pressedVxsPos, m_pos - m_pressedPos));
::stageObject()
->invalidate(); // Should be a TStageObject's implementation detail ...
invalidate(); // .. it's that it caches placement data and we must
} // invalidate it. Gross. Can't we do anything about it?
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::addContextMenuActions_build(QMenu *menu) {
bool ret = true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_svSel.isEmpty()) {
QAction *deleteVertex = menu->addAction(tr("Delete Vertex"));
ret = ret && connect(deleteVertex, SIGNAL(triggered()), &l_plasticTool,
SLOT(deleteSelectedVertex_undo()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
menu->addSeparator();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(ret);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::moveVertex_build(const std::vector<TPointD> &origVxsPos,
const TPointD &posShift) {
if (!m_svSel.isEmpty()) {
const PlasticSkeletonP &skeleton = this->skeleton();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Move selected vertices
int v, vCount = int(m_svSel.objects().size());
for (v = 0; v != vCount; ++v)
skeleton->moveVertex(m_svSel.objects()[v], origVxsPos[v] + posShift);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Deformation must be recompiled
PlasticDeformerStorage::instance()->invalidateSkeleton(
m_sd.getPointer(), ::skeletonId(), PlasticDeformerStorage::ALL);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_mode.getIndex() == ANIMATE_IDX)
storeDeformation(); // Rebuild deformed skeleton - default values have
// changed
}
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::addVertex(const PlasticSkeletonVertex &vx) {
touchSkeleton();
const PlasticSkeletonP &skeleton = this->skeleton();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation =
true; // Some vertex parameters change during insert
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(
m_svSel.isEmpty() ||
m_svSel
.hasSingleObject()); // Could there be no parent (inserting the root)
setSkeletonSelection(skeleton->addVertex(vx, m_svSel));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation = false;
onChange(); // Update once after add
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// NOTE: Root addition does NOT currently add channels, so the above
// onChange() is
// quite necessary to cover that case too!
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Xsheet change notification is necessary to inform the Function Editor that
// new
// channels (the vertex deformation ones) have been introduced
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
PlasticDeformerStorage::instance()->invalidateSkeleton(
m_sd.getPointer(), ::skeletonId(), PlasticDeformerStorage::ALL);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::insertVertex(const PlasticSkeletonVertex &vx, int parent,
const std::vector<int> &children) {
const PlasticSkeletonP &skeleton = this->skeleton();
assert(skeleton);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation =
true; // Some vertex parameters change during insert.
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
setSkeletonSelection(skeleton->insertVertex(vx, parent, children));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation = false;
onChange(); // Update once after insertion
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
PlasticDeformerStorage::instance()->invalidateSkeleton(
m_sd.getPointer(), ::skeletonId(), PlasticDeformerStorage::ALL);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::insertVertex(const PlasticSkeletonVertex &vx, int e) {
const PlasticSkeletonP &skeleton = this->skeleton();
assert(skeleton);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const PlasticSkeleton::edge_type &ed = skeleton->edge(e);
insertVertex(vx, ed.vertex(0),
std::vector<int>(1, skeleton->edge(e).vertex(1)));
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::removeVertex() {
const PlasticSkeletonP &skeleton = this->skeleton();
assert(skeleton && m_svSel.hasSingleObject() && m_svSel > 0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation =
true; // Some vertex parameters change during removal.
// We need to avoid updating WHILE removing.
skeleton->removeVertex(m_svSel);
PlasticDeformerStorage::instance()->invalidateSkeleton(
m_sd.getPointer(), ::skeletonId(), PlasticDeformerStorage::ALL);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation = false;
onChange();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
clearSkeletonSelections(); // Remove mesh references - could be
// invalidated...
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Xsheet change notification is necessary to inform the Function Editor that
// some
// channels (the vertex deformation ones) have been removed
TTool::getApplication()
->getCurrentXsheet()
->notifyXsheetChanged(); // NOTE: This COULD invoke invalidate()...
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Rebuild the stage object's keyframes table
stageObject()->updateKeyframes();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int PlasticTool::addSkeleton(const PlasticSkeletonP &skeleton) {
assert(TTool::isEnabled());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
touchDeformation();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int skelId;
{
if (m_sd->empty())
skelId = 1;
else {
// Get the first unused skeleton id in m_sd
SkD::skelId_iterator st, sEnd;
m_sd->skeletonIds(st, sEnd);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (skelId = 1; st != sEnd && skelId == *st; ++skelId, ++st)
;
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
addSkeleton(skelId, skeleton);
return skelId;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::addSkeleton(int skelId, const PlasticSkeletonP &skeleton) {
assert(TTool::isEnabled());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
touchDeformation();
m_sd->attach(skelId, skeleton.getPointer());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
emit skelIdsListChanged();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::removeSkeleton(int skelId) {
// Remove the entire deformation
clearSkeletonSelections();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_sd) {
2018-06-08 17:31:04 +12:00
// in order to solve the crash issue #1967, try releasing deformer data here
PlasticDeformerStorage::instance()->releaseSkeletonData(
stageObject()->getPlasticSkeletonDeformation().getPointer(), skelId);
2016-06-15 18:43:10 +12:00
m_sd->detach(skelId);
if (m_sd->empty())
stageObject()->setPlasticSkeletonDeformation(
PlasticSkeletonDeformationP());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
::invalidateXsheet(); // Updates m_sd
emit skelIdsListChanged();
}
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::deleteSelectedVertex_undo() {
if (m_svSel.isEmpty()) return;
TUndoManager *manager = TUndoManager::manager();
if (m_svSel.contains(0)) {
TUndo *op = new RemoveSkeletonUndo(::skeletonId());
manager->add(op);
op->redo();
} else {
typedef PlasticVertexSelection::objects_container objects_container;
objects_container vertexIdxs =
m_svSel.objects(); // Each undo will reset the vertex selection,
// so we need a copy
manager->beginBlock();
{
objects_container::const_iterator vit, viEnd = vertexIdxs.end();
for (vit = vertexIdxs.begin(); vit != viEnd; ++vit) {
TUndo *op = new RemoveVertexUndo(*vit);
manager->add(op);
op->redo();
}
}
manager->endBlock();
}
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int PlasticTool::addSkeleton_undo(const PlasticSkeletonP &skeleton) {
int skelId;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndoManager *manager = TUndoManager::manager();
manager->beginBlock();
{
skelId = l_plasticTool.addSkeleton(skeleton);
assert(l_plasticTool.deformation());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndo *addUndo =
new AddSkeletonUndo(skelId, new PlasticSkeleton(*skeleton));
manager->add(addUndo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndo *setIdUndo = new SetSkeletonIdUndo(skelId);
manager->add(setIdUndo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
setIdUndo->redo();
}
manager->endBlock();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
::invalidateXsheet();
return skelId;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::addSkeleton_undo(int skelId,
const PlasticSkeletonP &skeleton) {
TUndoManager *manager = TUndoManager::manager();
manager->beginBlock();
{
l_plasticTool.addSkeleton(skelId, skeleton);
assert(l_plasticTool.deformation());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndo *addUndo =
new AddSkeletonUndo(skelId, new PlasticSkeleton(*skeleton));
manager->add(addUndo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndo *setIdUndo = new SetSkeletonIdUndo(skelId);
manager->add(setIdUndo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
setIdUndo->redo();
}
manager->endBlock();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
::invalidateXsheet();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::removeSkeleton_undo(int skelId) {
TUndo *op = new RemoveSkeletonUndo(skelId);
TUndoManager::manager()->add(op);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
op->redo();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::removeSkeleton_withKeyframes_undo(int skelId) {
TUndo *op = new RemoveSkeletonUndo_WithKeyframes(skelId);
TUndoManager::manager()->add(op);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
op->redo();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::editSkelId_undo(int skelId) {
TUndo *op = new SetSkeletonIdUndo(skelId);
TUndoManager::manager()->add(op);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
op->redo();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::draw_build() {
double pixelSize = getPixelSize();
// Draw original skeleton
const PlasticSkeletonP &skeleton = this->skeleton();
if (skeleton) {
drawOnionSkinSkeletons_build(pixelSize);
drawSkeleton(*skeleton, pixelSize);
drawSelections(m_sd, *skeleton, pixelSize);
}
drawHighlights(m_sd, skeleton.getPointer(), pixelSize);
if (!skeleton || skeleton->vertices().empty() ||
(m_svSel.hasSingleObject() && m_svHigh < 0 && m_seHigh < 0)) {
// Draw a handle at current mouse pos (will add a vertex)
drawSquare(m_pos, HANDLE_SIZE * pixelSize);
}
2016-03-19 06:57:51 +13:00
}