Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// StrokeDeformation.cpp: implementation of the StrokeDeformation class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
#define _STLP_DEBUG 1
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/StrokeDeformation.h"
Toshihiro Shimizu 890ddd
#include "ext/SquarePotential.h"
Toshihiro Shimizu 890ddd
#include "ext/StrokeParametricDeformer.h"
Toshihiro Shimizu 890ddd
#include "ext/NotSimmetricBezierPotential.h"
Toshihiro Shimizu 890ddd
#include "ext/SmoothDeformation.h"
Toshihiro Shimizu 890ddd
#include "ext/ContextStatus.h"
Toshihiro Shimizu 890ddd
#include "ext/Designer.h"
Toshihiro Shimizu 890ddd
//#include "ext/TriParam.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/StraightCornerDeformation.h"
Toshihiro Shimizu 890ddd
#include "ext/CornerDeformation.h"
Toshihiro Shimizu 890ddd
#include "ext/SmoothDeformation.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/StrokeDeformationImpl.h"
Toshihiro Shimizu 890ddd
#include "DeformationSelector.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <tcurves.h></tcurves.h>
Toshihiro Shimizu 890ddd
#include <tstrokeutil.h></tstrokeutil.h>
Toshihiro Shimizu 890ddd
#include <tmathutil.h></tmathutil.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
#include <iterator></iterator>
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace ToonzExt;
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/**
Toshihiro Shimizu 890ddd
 * @brief Manage a mutable context.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static TThread::Mutex s_mutex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StrokeDeformation::StrokeDeformation()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	state_ = CREATED;
Toshihiro Shimizu 890ddd
	deformationImpl_ = SmoothDeformation::instance();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StrokeDeformation::~StrokeDeformation()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokeDeformation::activate(const ContextStatus *status)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(status && "Status is null!!!");
Toshihiro Shimizu 890ddd
	if (!status)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (state_ == RESETTED)
Toshihiro Shimizu 890ddd
		delete this->deactivate();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (state_ == CREATED ||
Toshihiro Shimizu 890ddd
		state_ == DEACTIVE) {
Toshihiro Shimizu 890ddd
		deformationImpl_ = this->retrieveDeformator(status);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		// if it is a not valid entry state_ recover
Toshihiro Shimizu 890ddd
		assert(deformationImpl_ && "Can not find a valid deformator!!!");
Toshihiro Shimizu 890ddd
		if (!deformationImpl_)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		deformationImpl_->reset();
Toshihiro Shimizu 890ddd
		deformationImpl_ = DeformationSelector::instance()->getDeformation(status);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(deformationImpl_ && "Deformation is null!!!");
Toshihiro Shimizu 890ddd
	if (!deformationImpl_)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!deformationImpl_->activate_impl(status)) {
Toshihiro Shimizu 890ddd
		deformationImpl_->reset();
Toshihiro Shimizu 890ddd
		state_ = DEACTIVE;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	state_ = ACTIVE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokeDeformation::update(const TPointD &delta)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(deformationImpl_ && "Deformation is null!!!");
Toshihiro Shimizu 890ddd
	if (!deformationImpl_) {
Toshihiro Shimizu 890ddd
		state_ = DEACTIVE;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (state_ != ACTIVE &&
Toshihiro Shimizu 890ddd
		state_ != UPDATING) {
Toshihiro Shimizu 890ddd
		deformationImpl_->reset();
Toshihiro Shimizu 890ddd
		state_ = ACTIVE;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	deformationImpl_->update_impl(delta);
Toshihiro Shimizu 890ddd
	state_ = UPDATING;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TStroke *
Toshihiro Shimizu 890ddd
StrokeDeformation::deactivate()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!deformationImpl_) {
Toshihiro Shimizu 890ddd
		state_ = DEACTIVE;
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (state_ != ACTIVE &&
Toshihiro Shimizu 890ddd
		state_ != UPDATING) {
Toshihiro Shimizu 890ddd
		state_ = DEACTIVE;
Toshihiro Shimizu 890ddd
		deformationImpl_->reset();
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	state_ = DEACTIVE;
Toshihiro Shimizu 890ddd
	TStroke *
Toshihiro Shimizu 890ddd
		out = deformationImpl_->deactivate_impl();
Toshihiro Shimizu 890ddd
	deformationImpl_ = 0;
Toshihiro Shimizu 890ddd
	return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokeDeformation::reset()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	state_ = RESETTED;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokeDeformation::draw(Designer *designer)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(designer && "Designer is null!!!");
Toshihiro Shimizu 890ddd
	if (!deformationImpl_ ||
Toshihiro Shimizu 890ddd
		!designer)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// this is to draw the icon
Toshihiro Shimizu 890ddd
	deformationImpl_->draw(designer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// this is to draw stroke when change something
Toshihiro Shimizu 890ddd
	designer->draw(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokeDeformation::check(const ContextStatus *status)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	if (state_ != UPDATING)
Toshihiro Shimizu 890ddd
		deformationImpl_ = this->retrieveDeformator(status);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TStroke *
Toshihiro Shimizu 890ddd
StrokeDeformation::getCopiedStroke() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	if (deformationImpl_)
Toshihiro Shimizu 890ddd
		return deformationImpl_->getCopiedStroke();
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TStroke *
Toshihiro Shimizu 890ddd
StrokeDeformation::getStroke() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	if (deformationImpl_)
Toshihiro Shimizu 890ddd
		return deformationImpl_->getStrokeSelected();
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TStroke *
Toshihiro Shimizu 890ddd
StrokeDeformation::getTransformedStroke() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	if (deformationImpl_)
Toshihiro Shimizu 890ddd
		return deformationImpl_->getTransformedStroke();
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokeDeformation::recover()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	assert(deformationImpl_ && "Deformation is null!!!");
Toshihiro Shimizu 890ddd
	if (!deformationImpl_)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	deformationImpl_->reset();
Toshihiro Shimizu 890ddd
	//  this->clearStatus();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//deformationImpl_ = DeformationSelector::instance()->getDeformation(status);
Toshihiro Shimizu 890ddd
	//assert(deformationImpl_ &&  "Deformation is null!!!" );
Toshihiro Shimizu 890ddd
	//if( !deformationImpl_ )
Toshihiro Shimizu 890ddd
	//  return;
Toshihiro Shimizu 890ddd
	// deformationImpl_->activate_impl(status);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const ContextStatus *
Toshihiro Shimizu 890ddd
StrokeDeformation::getStatus() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	if (deformationImpl_)
Toshihiro Shimizu 890ddd
		return deformationImpl_->getImplStatus();
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzExt::Interval
Toshihiro Shimizu 890ddd
StrokeDeformation::getExtremes() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	if (!deformationImpl_)
Toshihiro Shimizu 890ddd
		return Interval(-1, -1);
Toshihiro Shimizu 890ddd
	return deformationImpl_->getExtremes();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StrokeDeformationImpl *
Toshihiro Shimizu 890ddd
StrokeDeformation::retrieveDeformator(const ContextStatus *status)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	return DeformationSelector::instance()->getDeformation(status);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int StrokeDeformation::getCursorId() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
	if (!deformationImpl_)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return deformationImpl_->getCursorId();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const Potential *
Toshihiro Shimizu 890ddd
StrokeDeformation::getPotential() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(deformationImpl_ && "Deformation is null!!!");
Toshihiro Shimizu 890ddd
	if (!deformationImpl_)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return deformationImpl_->getPotential();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const StrokeDeformationImpl *
Toshihiro Shimizu 890ddd
StrokeDeformation::getDeformationImpl() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return deformationImpl_;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------