Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/**
Toshihiro Shimizu 890ddd
 * @author  Fabrizio Morciano <fabrizio.morciano@gmail.com></fabrizio.morciano@gmail.com>
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/StrokeParametricDeformer.h"
Toshihiro Shimizu 890ddd
#include "ext/NotSimmetricExpPotential.h"
Toshihiro Shimizu 890ddd
//#include "ext/NotSimmetricBezierPotential.h"
Toshihiro Shimizu 890ddd
//#include "ext/SquarePotential.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <sstream></sstream>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <tstroke.h></tstroke.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzExt::StrokeParametricDeformer::StrokeParametricDeformer(double actionLenght,
Toshihiro Shimizu 890ddd
															 double startParameter,
Toshihiro Shimizu 890ddd
															 TStroke *s,
Toshihiro Shimizu 890ddd
															 Potential *pot)
Toshihiro Shimizu 890ddd
	: startParameter_(startParameter), actionLenght_(actionLenght), vx_(1.0), vy_(1.0), pot_(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	diff_ = 0.001;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		ref_copy_ = new TStroke(*s);
Toshihiro Shimizu 890ddd
	} catch (std::bad_alloc &) {
Toshihiro Shimizu 890ddd
		throw std::invalid_argument("Not possible to have a copy of stroke!!!");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(0.0 <= startParameter_ &&
Toshihiro Shimizu 890ddd
		   startParameter_ <= 1.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//double
Toshihiro Shimizu 890ddd
	//  max_length = s->getLength();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//  if( actionLenght == -1 )
Toshihiro Shimizu 890ddd
	//  {
Toshihiro Shimizu 890ddd
	//    assert(false && "Rispristina la vecchia gestione");
Toshihiro Shimizu 890ddd
	//    actionLenght_ = 2.0 * max_length;
Toshihiro Shimizu 890ddd
	//  }
Toshihiro Shimizu 890ddd
	//  else
Toshihiro Shimizu 890ddd
	//  {
Toshihiro Shimizu 890ddd
	assert(0.0 <= actionLenght_);
Toshihiro Shimizu 890ddd
	// && actionLenght_ <= max_length );
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (0.0 > actionLenght_)
Toshihiro Shimizu 890ddd
		actionLenght_ = 0.0;
Toshihiro Shimizu 890ddd
	//else if ( actionLenght_ > max_length )
Toshihiro Shimizu 890ddd
	//  actionLenght_ = max_length;
Toshihiro Shimizu 890ddd
	//}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pot_ = pot;
Toshihiro Shimizu 890ddd
	if (!pot_)
Toshihiro Shimizu 890ddd
		throw std::invalid_argument("Not Possible to have a ref of Potential!!!");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pot_->setParameters(ref_copy_,
Toshihiro Shimizu 890ddd
						startParameter_,
Toshihiro Shimizu 890ddd
						actionLenght_);
Toshihiro Shimizu 890ddd
	assert(pot_);
Toshihiro Shimizu 890ddd
	startLenght_ = startParameter_;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ToonzExt::StrokeParametricDeformer::setMouseMove(double vx,
Toshihiro Shimizu 890ddd
													  double vy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	vx_ = vx;
Toshihiro Shimizu 890ddd
	vy_ = vy;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzExt::StrokeParametricDeformer::~StrokeParametricDeformer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete pot_;
Toshihiro Shimizu 890ddd
	delete ref_copy_;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickPoint
Toshihiro Shimizu 890ddd
ToonzExt::StrokeParametricDeformer::getDisplacement(const TStroke &stroke,
Toshihiro Shimizu 890ddd
													double w) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// conversion in absolute system for shape deformation
Toshihiro Shimizu 890ddd
	double val = pot_->value(w);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(val >= 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// amplify movement
Toshihiro Shimizu 890ddd
	return TThickPoint(vx_ * val, vy_ * val, 0.0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickPoint
Toshihiro Shimizu 890ddd
ToonzExt::StrokeParametricDeformer::getDisplacementForControlPoint(
Toshihiro Shimizu 890ddd
	const TStroke &stroke,
Toshihiro Shimizu 890ddd
	UINT n) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double w = stroke.getParameterAtControlPoint(n);
Toshihiro Shimizu 890ddd
	return this->getDisplacement(stroke, w);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickPoint
Toshihiro Shimizu 890ddd
ToonzExt::StrokeParametricDeformer::getDisplacementForControlPointLen(
Toshihiro Shimizu 890ddd
	const TStroke &stroke,
Toshihiro Shimizu 890ddd
	double cpLen) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return this->getDisplacement(stroke, cpLen);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double
Toshihiro Shimizu 890ddd
ToonzExt::StrokeParametricDeformer::getDelta(const TStroke &stroke,
Toshihiro Shimizu 890ddd
											 double w) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#if 0
Toshihiro Shimizu 890ddd
  double  w0 = w;
Toshihiro Shimizu 890ddd
  if(w0 == 1.0)
Toshihiro Shimizu 890ddd
    return norm( stroke.getSpeed(1.0));
Toshihiro Shimizu 890ddd
  double tmp = stroke.getLength(w0);
Toshihiro Shimizu 890ddd
  tmp+=1.0;
Toshihiro Shimizu 890ddd
  double w1 = stroke.getParameterAtLength(tmp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  /*
Toshihiro Shimizu 890ddd
  double  w1 = w + 0.01;  
Toshihiro Shimizu 890ddd
  if(w1>1.0)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    w0 = 1.0 - 0.01;
Toshihiro Shimizu 890ddd
    w1 = 1.0;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  const double  dx = 1.0; //stroke.getLength(w0,w1);
Toshihiro Shimizu 890ddd
  TThickPoint pnt = this->getDisplacement(stroke,w1) - 
Toshihiro Shimizu 890ddd
                    this->getDisplacement(stroke,w0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  double  dy =  sqrt( sq(pnt.x) + sq(pnt.y) + sq(pnt.thick) );
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  assert(dx!=0.0);
Toshihiro Shimizu 890ddd
  return dy/dx;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  /*
Toshihiro Shimizu 890ddd
  // the increaser is wrong than this value need to be the deformation
Toshihiro Shimizu 890ddd
  //  value 
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  // is norm 
Toshihiro Shimizu 890ddd
  TThickPoint pnt = this->getDisplacement(stroke,
Toshihiro Shimizu 890ddd
                                          w);
Toshihiro Shimizu 890ddd
  return sqrt( sq(pnt.x) + sq(pnt.y) + sq(pnt.thick) );
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  //return pot_->gradient(w);
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return this->getDisplacement(stroke, w).y;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double
Toshihiro Shimizu 890ddd
ToonzExt::StrokeParametricDeformer::getMaxDiff() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return diff_;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ToonzExt::StrokeParametricDeformer::getRange(double &from,
Toshihiro Shimizu 890ddd
												  double &to)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double x = ref_copy_->getLength(startParameter_);
Toshihiro Shimizu 890ddd
	double delta = x - actionLenght_ * 0.5;
Toshihiro Shimizu 890ddd
	if (delta > 0)
Toshihiro Shimizu 890ddd
		from = ref_copy_->getParameterAtLength(delta);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		from = 0.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	delta = x + actionLenght_ * 0.5;
Toshihiro Shimizu 890ddd
	if (delta < ref_copy_->getLength())
Toshihiro Shimizu 890ddd
		to = ref_copy_->getParameterAtLength(delta);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		to = 1.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------