Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#define NOMINMAX
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/NotSimmetricBezierPotential.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <tmathutil.h></tmathutil.h>
Toshihiro Shimizu 890ddd
#include <tcurves.h></tcurves.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
using namespace ToonzExt;
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
typedef unary_function<double, double=""> unary_functionDD;</double,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class myBlendFunc : unary_functionDD
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//TCubic  c;
Toshihiro Shimizu 890ddd
	TQuadratic curve;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	myBlendFunc(double t = 0.0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		curve.setP0(TPointD(0.0, 1.0));
Toshihiro Shimizu 890ddd
		curve.setP1(TPointD(0.5 * (1.0 - t), 1.0));
Toshihiro Shimizu 890ddd
		curve.setP2(TPointD(1.0, 0.0));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	result_type operator()(argument_type x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		result_type out = 0.0;
Toshihiro Shimizu 890ddd
		x = fabs(x);
Toshihiro Shimizu 890ddd
		if (x >= 1.0)
Toshihiro Shimizu 890ddd
			return 0.0;
Toshihiro Shimizu 890ddd
		out = curve.getPoint(x).y;
Toshihiro Shimizu 890ddd
		return out;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzExt::NotSimmetricBezierPotential::~NotSimmetricBezierPotential()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ToonzExt::NotSimmetricBezierPotential::setParameters_(const TStroke *ref,
Toshihiro Shimizu 890ddd
														   double w,
Toshihiro Shimizu 890ddd
														   double al)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(ref);
Toshihiro Shimizu 890ddd
	ref_ = ref;
Toshihiro Shimizu 890ddd
	par_ = w;
Toshihiro Shimizu 890ddd
	actionLength_ = al;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	strokeLength_ = ref->getLength();
Toshihiro Shimizu 890ddd
	lenghtAtParam_ = ref->getLength(par_);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// lunghezza dal pto di click all'inizio della curva
Toshihiro Shimizu 890ddd
	leftFactor_ = min(lenghtAtParam_,
Toshihiro Shimizu 890ddd
					  actionLength_ * 0.5); //lenghtAtParam_ / strokeLength_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// lunghezza dal pto di click alla fine
Toshihiro Shimizu 890ddd
	rightFactor_ = min(strokeLength_ - lenghtAtParam_,
Toshihiro Shimizu 890ddd
					   actionLength_ * 0.5);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double
Toshihiro Shimizu 890ddd
ToonzExt::NotSimmetricBezierPotential::value_(double value2test) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(0.0 <= value2test &&
Toshihiro Shimizu 890ddd
		   value2test <= 1.0);
Toshihiro Shimizu 890ddd
	return this->compute_value(value2test);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// normalization of parameter in range interval
Toshihiro Shimizu 890ddd
double ToonzExt::NotSimmetricBezierPotential::compute_shape(double value2test) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double x = ref_->getLength(value2test);
Toshihiro Shimizu 890ddd
	double shape = this->actionLength_ * 0.5;
Toshihiro Shimizu 890ddd
	if (isAlmostZero(shape))
Toshihiro Shimizu 890ddd
		shape = 1.0;
Toshihiro Shimizu 890ddd
	x = (x - lenghtAtParam_) / shape;
Toshihiro Shimizu 890ddd
	return x;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double
Toshihiro Shimizu 890ddd
ToonzExt::NotSimmetricBezierPotential::compute_value(double value2test) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	myBlendFunc me;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// on extremes use
Toshihiro Shimizu 890ddd
	//     2
Toshihiro Shimizu 890ddd
	//  1-x
Toshihiro Shimizu 890ddd
	//
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// when is near to extreme uses a mix notation
Toshihiro Shimizu 890ddd
	double x = 0.0;
Toshihiro Shimizu 890ddd
	double res = 0.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// lenght  at parameter
Toshihiro Shimizu 890ddd
	x = ref_->getLength(value2test);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const double tolerance = 0.0; // need to be pixel based
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// if is an extreme
Toshihiro Shimizu 890ddd
	if (max(lenghtAtParam_, 0.0) < tolerance ||
Toshihiro Shimizu 890ddd
		max(strokeLength_ - lenghtAtParam_, 0.0) < tolerance) {
Toshihiro Shimizu 890ddd
		double tmp_al = actionLength_ * 0.5;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// compute correct parameter considering offset
Toshihiro Shimizu 890ddd
		// try to have a square curve like shape
Toshihiro Shimizu 890ddd
		//
Toshihiro Shimizu 890ddd
		//       2
Toshihiro Shimizu 890ddd
		//  m = x
Toshihiro Shimizu 890ddd
		//
Toshihiro Shimizu 890ddd
		if (leftFactor_ <= tolerance)
Toshihiro Shimizu 890ddd
			x = 1.0 - x / tmp_al;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			x = (x - (strokeLength_ - tmp_al)) / tmp_al;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (x < 0.0)
Toshihiro Shimizu 890ddd
			return 0.0;
Toshihiro Shimizu 890ddd
		assert(0.0 <= x &&
Toshihiro Shimizu 890ddd
			   x <= 1.0 + TConsts::epsilon);
Toshihiro Shimizu 890ddd
		x = std::min(x, 1.0); // just to avoid problem in production code
Toshihiro Shimizu 890ddd
		res = sq(x);
Toshihiro Shimizu 890ddd
	} else // when is not an extreme
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		double lenght_at_value2test = ref_->getLength(value2test);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		const double min_level = 0.01;
Toshihiro Shimizu 890ddd
		// if check a parameter over click point
Toshihiro Shimizu 890ddd
		if (lenght_at_value2test >= lenghtAtParam_) {
Toshihiro Shimizu 890ddd
			// check if extreme can be moved from this parameter configuration
Toshihiro Shimizu 890ddd
			double tmp_x = this->compute_shape(1.0);
Toshihiro Shimizu 890ddd
			double tmp_res = me(tmp_x);
Toshihiro Shimizu 890ddd
			if (tmp_res > min_level) {
Toshihiro Shimizu 890ddd
				// please note that in this case
Toshihiro Shimizu 890ddd
				//  lenghtAtParam_ + rightFactor_ == strokeLength_
Toshihiro Shimizu 890ddd
				// (by ctor).
Toshihiro Shimizu 890ddd
				if (rightFactor_ != 0.0)
Toshihiro Shimizu 890ddd
					x = (lenght_at_value2test - lenghtAtParam_) / rightFactor_;
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					x = 0.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				assert(0.0 - TConsts::epsilon <= x &&
Toshihiro Shimizu 890ddd
					   x <= 1.0 + TConsts::epsilon);
Toshihiro Shimizu 890ddd
				if (isAlmostZero(x))
Toshihiro Shimizu 890ddd
					x = 0.0;
Toshihiro Shimizu 890ddd
				if (areAlmostEqual(x, 1.0))
Toshihiro Shimizu 890ddd
					x = 1.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				double how_many_of_shape = (strokeLength_ - lenghtAtParam_) / (actionLength_ * 0.5);
Toshihiro Shimizu 890ddd
				assert(0.0 <= how_many_of_shape &&
Toshihiro Shimizu 890ddd
					   how_many_of_shape <= 1.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				myBlendFunc bf(how_many_of_shape);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				return bf(x);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			// leftFactor_
Toshihiro Shimizu 890ddd
			double tmp_x = this->compute_shape(0.0);
Toshihiro Shimizu 890ddd
			double tmp_res = me(tmp_x);
Toshihiro Shimizu 890ddd
			if (tmp_res > min_level) {
Toshihiro Shimizu 890ddd
				double x = lenght_at_value2test / leftFactor_;
Toshihiro Shimizu 890ddd
				assert(0.0 <= x &&
Toshihiro Shimizu 890ddd
					   x <= 1.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// then movement use another shape
Toshihiro Shimizu 890ddd
				double diff = x - 1.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				double how_many_of_shape = lenghtAtParam_ / (actionLength_ * 0.5);
Toshihiro Shimizu 890ddd
				assert(0.0 <= how_many_of_shape &&
Toshihiro Shimizu 890ddd
					   how_many_of_shape <= 1.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				myBlendFunc bf(how_many_of_shape);
Toshihiro Shimizu 890ddd
				return bf(diff);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// default behaviour is an exp
Toshihiro Shimizu 890ddd
		x = this->compute_shape(value2test);
Toshihiro Shimizu 890ddd
		res = me(x);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return res;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzExt::Potential *
Toshihiro Shimizu 890ddd
ToonzExt::NotSimmetricBezierPotential::clone()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return new NotSimmetricBezierPotential;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------