Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/LinearPotential.h"
Toshihiro Shimizu 890ddd
#include <tmathutil.h></tmathutil.h>
Toshihiro Shimizu 890ddd
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ToonzExt::LinearPotential::setParameters_(const TStroke *ref,
Toshihiro Shimizu 890ddd
											   double par,
Toshihiro Shimizu 890ddd
											   double al)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ref_ = ref;
Toshihiro Shimizu 890ddd
	par_ = par;
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_ = lenghtAtParam_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// lunghezza dal pto di click alla fine
Toshihiro Shimizu 890ddd
	rightFactor_ = strokeLength_ - lenghtAtParam_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// considero come intervallo di mapping [-range,range].
Toshihiro Shimizu 890ddd
	//  4 ha come valore c.a. 10exp-6
Toshihiro Shimizu 890ddd
	//  cioƩ sposterei un pixel su un movimento di un milione di pixel
Toshihiro Shimizu 890ddd
	range_ = 2.8;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzExt::LinearPotential::~LinearPotential()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double
Toshihiro Shimizu 890ddd
ToonzExt::LinearPotential::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::LinearPotential::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_) * range_) / 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::LinearPotential::compute_value(double value2test) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// use
Toshihiro Shimizu 890ddd
	//
Toshihiro Shimizu 890ddd
	//  1-x
Toshihiro Shimizu 890ddd
	//
Toshihiro Shimizu 890ddd
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
	double tmp_al = actionLength_ * 0.5;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (isAlmostZero(par_) ||
Toshihiro Shimizu 890ddd
		areAlmostEqual(par_, 1.0))
Toshihiro Shimizu 890ddd
		tmp_al = actionLength_;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// this is ckecked from wrapper class
Toshihiro Shimizu 890ddd
	assert(tmp_al != 0.0);
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
	//  m = x
Toshihiro Shimizu 890ddd
	//
Toshihiro Shimizu 890ddd
	if (leftFactor_ == 0.0)
Toshihiro Shimizu 890ddd
		x = 1.0 - x / tmp_al;
Toshihiro Shimizu 890ddd
	else if (rightFactor_ == 0.0)
Toshihiro Shimizu 890ddd
		x = (x - (strokeLength_ - tmp_al)) / tmp_al;
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		if (x <= lenghtAtParam_ &&
Toshihiro Shimizu 890ddd
			((lenghtAtParam_ - x) <= leftFactor_))
Toshihiro Shimizu 890ddd
			x = (x - (lenghtAtParam_ - leftFactor_)) / leftFactor_;
Toshihiro Shimizu 890ddd
		else if (x > lenghtAtParam_ &&
Toshihiro Shimizu 890ddd
				 ((x - lenghtAtParam_) < rightFactor_))
Toshihiro Shimizu 890ddd
			x = (rightFactor_ - (x - lenghtAtParam_)) / rightFactor_;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			x = -1;
Toshihiro Shimizu 890ddd
	}
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
	res = 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::LinearPotential::clone()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return new LinearPotential;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------