Toshihiro Shimizu 890ddd
#ifndef PLASTICSKELETONDEFORMATION_H
Toshihiro Shimizu 890ddd
#define PLASTICSKELETONDEFORMATION_H
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
#include "tdoubleparam.h"
Toshihiro Shimizu 890ddd
#include "tdoublekeyframe.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzExt includes
Toshihiro Shimizu 890ddd
#include "ext/plastichandle.h"
Toshihiro Shimizu 890ddd
#include "ext/plasticskeleton.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// tcg includes
Toshihiro Shimizu 890ddd
#include "tcg/tcg_any_iterator.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TNZEXT_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//====================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//    Forward declarations
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ParamsObserver;
Toshihiro Shimizu 890ddd
class ParamChange;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//====================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
//    PlasticSkeletonVertexDeformation  declaration
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! The deformation of a plastic skeleton vertex.
Toshihiro Shimizu 890ddd
typedef struct DVAPI PlasticSkeletonVertexDeformation : public TPersist {
Toshihiro Shimizu 890ddd
	PERSIST_DECLARATION(PlasticSkeletonVertexDeformation)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	enum Params {
Toshihiro Shimizu 890ddd
		ANGLE = 0, //!< Distance from parent vertex (delta)
Toshihiro Shimizu 890ddd
		DISTANCE,  //!< Angle with parent edge (delta)
Toshihiro Shimizu 890ddd
		SO,		   //!< Vertex's stacking order
Toshihiro Shimizu 890ddd
		PARAMS_COUNT
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	struct Keyframe {
Toshihiro Shimizu 890ddd
		TDoubleKeyframe m_keyframes[PARAMS_COUNT];
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TDoubleParamP m_params[PARAMS_COUNT];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Keyframe getKeyframe(double frame) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setKeyframe(double frame);
Toshihiro Shimizu 890ddd
	bool setKeyframe(const Keyframe &values);
Toshihiro Shimizu 890ddd
	bool setKeyframe(const Keyframe &values, double frame,
Toshihiro Shimizu 890ddd
					 double easeIn = -1.0, double easeOut = -1.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isKeyframe(double frame) const;
Toshihiro Shimizu 890ddd
	bool isFullKeyframe(double frame) const;
Toshihiro Shimizu 890ddd
	void deleteKeyframe(double frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void saveData(TOStream &os);
Toshihiro Shimizu 890ddd
	void loadData(TIStream &is);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} SkVD;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
//    PlasticSkeletonDeformationKeyframe  declaration
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! The keyframe of a plastic skeleton vertex deformation.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  \note A deformation keyframe stores vertex deformation keyframes by vertex \a names.
Toshihiro Shimizu 890ddd
  This is the approach we use to deal with keyframe pasting to different skeletons.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
typedef struct PlasticSkeletonDeformationKeyframe {
Toshihiro Shimizu 890ddd
	std::map<qstring, skvd::keyframe=""> m_vertexKeyframes; //!< Keyframes by vertex \a name</qstring,>
Toshihiro Shimizu 890ddd
	TDoubleKeyframe m_skelIdKeyframe;					 //!< Skeleton id keyframe
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} SkDKey;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
//    PlasticSkeletonDeformation  declaration
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  PlasticSkeletonDeformation models the deformation of a group of PlasticSkeleton instances.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
\par Description
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  A PlasticSkeleton instance is typically used to act as a deformable object - in other
Toshihiro Shimizu 890ddd
  words, it defines the 'original' form of a hierarchy of vertices that can be manipulated
Toshihiro Shimizu 890ddd
  interactively to obtain a 'deformed' configuration.
Toshihiro Shimizu 890ddd
  \n\n
Toshihiro Shimizu 890ddd
  PlasticSkeletonDeformation represents a deformation of PlasticSkeleton objects,
Toshihiro Shimizu 890ddd
  therefore acting primarily as a collection of PlasticSkeletonVertexDeformation
Toshihiro Shimizu 890ddd
  instances - one per skeleton vertex. The collection is an associative container mapping a
Toshihiro Shimizu 890ddd
   vertex name <\I> to its deformation; using names as keys is a useful abstraction
Toshihiro Shimizu 890ddd
  that allows vertex deformations data (eg keyframes) to be copied to skeleton deformations
Toshihiro Shimizu 890ddd
  whose skeletons have a different internal configuration (ie vertex indices and such).
Toshihiro Shimizu 890ddd
  \n\n
Toshihiro Shimizu 890ddd
  Each vertex deformation also stores a unique  hook number <\I> that can be used during
Toshihiro Shimizu 890ddd
  xsheet animation to link stage objects to a skeleton vertex.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
\par The Skeletons group
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  The PlasticSkeletonDeformation implementation has been extended to work on multiple
Toshihiro Shimizu 890ddd
  skeletons for one class instance. This class stores a map associating skeleton indices
Toshihiro Shimizu 890ddd
  to the skeleton instances, that can be used to select a skeleton to be deformed with
Toshihiro Shimizu 890ddd
  the deformation's data.
Toshihiro Shimizu 890ddd
  \n\n
Toshihiro Shimizu 890ddd
  Vertices in different skeleton instances share the same animation if their name is the same.
Toshihiro Shimizu 890ddd
  \n\n
Toshihiro Shimizu 890ddd
  This class provides an animatable parameter that is intended to choose the \a active
Toshihiro Shimizu 890ddd
  skeleton along an xsheet timeline. It is retrievable through the skeletonIdsParam() method.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
\par Notable implementation details
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  In current implementation, a PlasticSkeletonDeformation keeps shared ownership of the
Toshihiro Shimizu 890ddd
  skeletons it is attached to. It is therefore intended to be a \a container of said skeletons.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
class DVAPI PlasticSkeletonDeformation : public TSmartObject, public TPersist
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	DECLARE_CLASS_CODE
Toshihiro Shimizu 890ddd
	PERSIST_DECLARATION(PlasticSkeletonDeformation)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	class Imp;
Shinya Kitaoka 262a92
	std::unique_ptr<imp> m_imp;</imp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	typedef tcg::any_it<int, *="" int,="" void="">::bidirectional skelId_iterator;</int,>
Toshihiro Shimizu 890ddd
	typedef tcg::any_it<std::pair<const *="" *,="" qstring="" skvd="">,</std::pair<const>
Toshihiro Shimizu 890ddd
						std::pair<const *="" *,="" qstring="" skvd="">, void *>::bidirectional vd_iterator;</const>
Toshihiro Shimizu 890ddd
	typedef tcg::any_it<std::pair<int, int="">,</std::pair<int,>
Toshihiro Shimizu 890ddd
						std::pair<int, int="">, void *>::bidirectional vx_iterator;</int,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	PlasticSkeletonDeformation();										 //!< Constructs an empty deformation
Toshihiro Shimizu 890ddd
	PlasticSkeletonDeformation(const PlasticSkeletonDeformation &other); //!< Constructs a deformation \a cloning other's skeletons
Toshihiro Shimizu 890ddd
	~PlasticSkeletonDeformation();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	PlasticSkeletonDeformation &operator=(const PlasticSkeletonDeformation &other);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Skeleton-related methods
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool empty() const;
Toshihiro Shimizu 890ddd
	int skeletonsCount() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Acquires  shared ownership <\I> of the specified skeleton, under given skeletonId
Toshihiro Shimizu 890ddd
	void attach(int skeletonId, PlasticSkeleton *skeleton);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Releases the skeleton associated to specified skeletonId
Toshihiro Shimizu 890ddd
	void detach(int skeletonId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	PlasticSkeletonP skeleton(int skeletonId) const;
Toshihiro Shimizu 890ddd
	int skeletonId(PlasticSkeleton *skeleton) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Returns the ordered range containing the skeleton ids
Toshihiro Shimizu 890ddd
	void skeletonIds(skelId_iterator &begin, skelId_iterator &end) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDoubleParamP skeletonIdsParam() const; //!< Returns the skeleton id by frame animatable parameter
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	PlasticSkeletonP skeleton(double frame) const; //!< Returns the \a active skeleton by xsheet frame
Toshihiro Shimizu 890ddd
	int skeletonId(double frame) const;			   //!< Returns the \a active skeleton id by xsheet frame
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Vertex deformations-related methods
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int vertexDeformationsCount() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	SkVD *vertexDeformation(const QString &vertexName) const; //!< Returns the vertex deformation associated to given
Toshihiro Shimizu 890ddd
															  //!< vertex name. The returned pointer is  owned by
Toshihiro Shimizu 890ddd
															  //!< the deformation - it must \b not be deleted <\I>
Toshihiro Shimizu 890ddd
	SkVD *vertexDeformation(int skelId, int v) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void vertexDeformations(vd_iterator &begin, vd_iterator &end) const; //!< Returns the ordered range of vertex deformations
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void vdSkeletonVertices(const QString &vertexName,
Toshihiro Shimizu 890ddd
							vx_iterator &begin, vx_iterator &end) const; //!< Returns the ordered range of skeleton vertices
Toshihiro Shimizu 890ddd
																		 //!< (at max one per skeleton id) associated to a
Toshihiro Shimizu 890ddd
																		 //!< vertex name
Toshihiro Shimizu 890ddd
	// Hook number-related methods
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int hookNumber(const QString &name) const;
Toshihiro Shimizu 890ddd
	int hookNumber(int skelId, int v) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString vertexName(int hookNumber) const;
Toshihiro Shimizu 890ddd
	int vertexIndex(int hookNumber, int skelId) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Parameters-related methods
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addObserver(TParamObserver *observer);
Toshihiro Shimizu 890ddd
	void removeObserver(TParamObserver *observer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setGrammar(TSyntax::Grammar *grammar);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Keyframes-related methods
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void getKeyframeAt(double frame, SkDKey &keysMap) const; //!< \note keysMap returned by argument to avoid map
Toshihiro Shimizu 890ddd
															 //!< copies in case move semantics is not available
Toshihiro Shimizu 890ddd
	void setKeyframe(double frame);
Toshihiro Shimizu 890ddd
	bool setKeyframe(const SkDKey &keyframe);
Toshihiro Shimizu 890ddd
	bool setKeyframe(const SkDKey &keyframe, double frame,
Toshihiro Shimizu 890ddd
					 double easeIn = -1.0, double easeOut = -1.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isKeyframe(double frame) const;
Toshihiro Shimizu 890ddd
	bool isFullKeyframe(double frame) const;
Toshihiro Shimizu 890ddd
	void deleteKeyframe(double frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Interface methods using a deformed copy of the original skeleton (which is owned by this class)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void storeDeformedSkeleton(int skeletonId, double frame, PlasticSkeleton &skeleton) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void updatePosition(const PlasticSkeleton &originalSkeleton, PlasticSkeleton &deformedSkeleton,
Toshihiro Shimizu 890ddd
						double frame, int v, const TPointD &pos);
Toshihiro Shimizu 890ddd
	void updateAngle(const PlasticSkeleton &originalSkeleton, PlasticSkeleton &deformedSkeleton,
Toshihiro Shimizu 890ddd
					 double frame, int v, const TPointD &pos);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	void saveData(TOStream &os);
Toshihiro Shimizu 890ddd
	void loadData(TIStream &is);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	friend class PlasticSkeleton;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addVertex(PlasticSkeleton *sk, int v);	//!< Deals with vertex deformations when v has been added
Toshihiro Shimizu 890ddd
	void insertVertex(PlasticSkeleton *sk, int v); //!< Deals with vertex deformations when v has been inserted in an edge
Toshihiro Shimizu 890ddd
	void deleteVertex(PlasticSkeleton *sk, int v); //!< Removes vertex deformation for v, \a before it is deleted
Toshihiro Shimizu 890ddd
	void vertexNameChange(PlasticSkeleton *sk, int v,
Toshihiro Shimizu 890ddd
						  const QString &newName); //!< Rebinds a vertex deformation name
Toshihiro Shimizu 890ddd
	void clear(PlasticSkeleton *sk);			   //!< Clears all vertex deformations
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void loadData_prerelease(TIStream &is); // Toonz 7.0 pre-release loading function. Will be deleted
Toshihiro Shimizu 890ddd
											// in the next minor release.
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef PlasticSkeletonDeformation SkD;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
template class DVAPI TSmartPointerT<plasticskeletondeformation>;</plasticskeletondeformation>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef TSmartPointerT<plasticskeletondeformation> PlasticSkeletonDeformationP;</plasticskeletondeformation>
Toshihiro Shimizu 890ddd
typedef PlasticSkeletonDeformationP SkDP;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // PLASTICSKELETONDEFORMATION_H