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"
MCCCS a0ce32
#include "ext/NotSymmetricExpPotential.h"
MCCCS a0ce32
//#include "ext/NotSymmetricBezierPotential.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
Shinya Kitaoka 120a6e
ToonzExt::StrokeParametricDeformer::StrokeParametricDeformer(
MCCCS a0ce32
    double actionLength, double startParameter, TStroke *s, Potential *pot)
Shinya Kitaoka 120a6e
    : startParameter_(startParameter)
MCCCS a0ce32
    , actionLength_(actionLength)
Shinya Kitaoka 120a6e
    , vx_(1.0)
Shinya Kitaoka 120a6e
    , vy_(1.0)
Shinya Kitaoka 120a6e
    , pot_(0) {
Shinya Kitaoka 120a6e
  diff_ = 0.001;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    ref_copy_ = new TStroke(*s);
Shinya Kitaoka 120a6e
  } catch (std::bad_alloc &) {
Shinya Kitaoka 120a6e
    throw std::invalid_argument("Not possible to have a copy of stroke!!!");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(0.0 <= startParameter_ && startParameter_ <= 1.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // double
Shinya Kitaoka 120a6e
  //  max_length = s->getLength();
Shinya Kitaoka 120a6e
MCCCS a0ce32
  //  if( actionLength == -1 )
Shinya Kitaoka 120a6e
  //  {
Shinya Kitaoka 120a6e
  //    assert(false && "Rispristina la vecchia gestione");
MCCCS a0ce32
  //    actionLength_ = 2.0 * max_length;
Shinya Kitaoka 120a6e
  //  }
Shinya Kitaoka 120a6e
  //  else
Shinya Kitaoka 120a6e
  //  {
MCCCS a0ce32
  assert(0.0 <= actionLength_);
MCCCS a0ce32
  // && actionLength_ <= max_length );
Shinya Kitaoka 120a6e
MCCCS a0ce32
  if (0.0 > actionLength_) actionLength_ = 0.0;
MCCCS a0ce32
  // else if ( actionLength_ > max_length )
MCCCS a0ce32
  //  actionLength_ = max_length;
Shinya Kitaoka 120a6e
  //}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pot_ = pot;
Shinya Kitaoka 120a6e
  if (!pot_)
Shinya Kitaoka 120a6e
    throw std::invalid_argument("Not Possible to have a ref of Potential!!!");
Shinya Kitaoka 120a6e
MCCCS a0ce32
  pot_->setParameters(ref_copy_, startParameter_, actionLength_);
Shinya Kitaoka 120a6e
  assert(pot_);
MCCCS a0ce32
  startLength_ = startParameter_;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToonzExt::StrokeParametricDeformer::setMouseMove(double vx, double vy) {
Shinya Kitaoka 120a6e
  vx_ = vx;
Shinya Kitaoka 120a6e
  vy_ = vy;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToonzExt::StrokeParametricDeformer::~StrokeParametricDeformer() {
Shinya Kitaoka 120a6e
  delete pot_;
Shinya Kitaoka 120a6e
  delete ref_copy_;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThickPoint ToonzExt::StrokeParametricDeformer::getDisplacement(
Shinya Kitaoka 120a6e
    const TStroke &stroke, double w) const {
Shinya Kitaoka 120a6e
  // conversion in absolute system for shape deformation
Shinya Kitaoka 120a6e
  double val = pot_->value(w);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(val >= 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // amplify movement
Shinya Kitaoka 120a6e
  return TThickPoint(vx_ * val, vy_ * val, 0.0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThickPoint ToonzExt::StrokeParametricDeformer::getDisplacementForControlPoint(
Shinya Kitaoka 120a6e
    const TStroke &stroke, UINT n) const {
Shinya Kitaoka 120a6e
  double w = stroke.getParameterAtControlPoint(n);
Shinya Kitaoka 120a6e
  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(
Shinya Kitaoka 120a6e
    const TStroke &stroke, double cpLen) const {
Shinya Kitaoka 120a6e
  return this->getDisplacement(stroke, cpLen);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double ToonzExt::StrokeParametricDeformer::getDelta(const TStroke &stroke,
Shinya Kitaoka 120a6e
                                                    double w) const {
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
Shinya Kitaoka 120a6e
  return this->getDisplacement(stroke, w).y;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double ToonzExt::StrokeParametricDeformer::getMaxDiff() const { return diff_; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToonzExt::StrokeParametricDeformer::getRange(double &from, double &to) {
Shinya Kitaoka 120a6e
  double x     = ref_copy_->getLength(startParameter_);
MCCCS a0ce32
  double delta = x - actionLength_ * 0.5;
Shinya Kitaoka 120a6e
  if (delta > 0)
Shinya Kitaoka 120a6e
    from = ref_copy_->getParameterAtLength(delta);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    from = 0.0;
Shinya Kitaoka 120a6e
MCCCS a0ce32
  delta = x + actionLength_ * 0.5;
Shinya Kitaoka 120a6e
  if (delta < ref_copy_->getLength())
Shinya Kitaoka 120a6e
    to = ref_copy_->getParameterAtLength(delta);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    to = 1.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------