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
Shinya Kitaoka 120a6e
void ToonzExt::LinearPotential::setParameters_(const TStroke *ref, double par,
Shinya Kitaoka 120a6e
                                               double al) {
Shinya Kitaoka 120a6e
  ref_          = ref;
Shinya Kitaoka 120a6e
  par_          = par;
Shinya Kitaoka 120a6e
  actionLength_ = al;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  strokeLength_  = ref->getLength();
Shinya Kitaoka 120a6e
  lenghtAtParam_ = ref->getLength(par);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // lunghezza dal pto di click all'inizio della curva
Shinya Kitaoka 120a6e
  leftFactor_ = lenghtAtParam_;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // lunghezza dal pto di click alla fine
Shinya Kitaoka 120a6e
  rightFactor_ = strokeLength_ - lenghtAtParam_;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // considero come intervallo di mapping [-range,range].
Shinya Kitaoka 120a6e
  //  4 ha come valore c.a. 10exp-6
Shinya Kitaoka 120a6e
  //  cioƩ sposterei un pixel su un movimento di un milione di pixel
Shinya Kitaoka 120a6e
  range_ = 2.8;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToonzExt::LinearPotential::~LinearPotential() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double ToonzExt::LinearPotential::value_(double value2test) const {
Shinya Kitaoka 120a6e
  assert(0.0 <= value2test && value2test <= 1.0);
Shinya Kitaoka 120a6e
  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
Shinya Kitaoka 120a6e
double ToonzExt::LinearPotential::compute_shape(double value2test) const {
Shinya Kitaoka 120a6e
  double x                       = ref_->getLength(value2test);
Shinya Kitaoka 120a6e
  double shape                   = this->actionLength_ * 0.5;
Shinya Kitaoka 120a6e
  if (isAlmostZero(shape)) shape = 1.0;
Shinya Kitaoka 120a6e
  x                              = ((x - lenghtAtParam_) * range_) / shape;
Shinya Kitaoka 120a6e
  return x;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double ToonzExt::LinearPotential::compute_value(double value2test) const {
Shinya Kitaoka 120a6e
  // use
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  //  1-x
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double x   = 0.0;
Shinya Kitaoka 120a6e
  double res = 0.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // lenght  at parameter
Shinya Kitaoka 120a6e
  x = ref_->getLength(value2test);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double tmp_al = actionLength_ * 0.5;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (isAlmostZero(par_) || areAlmostEqual(par_, 1.0)) tmp_al = actionLength_;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // this is ckecked from wrapper class
Shinya Kitaoka 120a6e
  assert(tmp_al != 0.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // compute correct parameter considering offset
Shinya Kitaoka 120a6e
  // try to have a square curve like shape
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  //  m = x
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  if (leftFactor_ == 0.0)
Shinya Kitaoka 120a6e
    x = 1.0 - x / tmp_al;
Shinya Kitaoka 120a6e
  else if (rightFactor_ == 0.0)
Shinya Kitaoka 120a6e
    x = (x - (strokeLength_ - tmp_al)) / tmp_al;
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    if (x <= lenghtAtParam_ && ((lenghtAtParam_ - x) <= leftFactor_))
Shinya Kitaoka 120a6e
      x = (x - (lenghtAtParam_ - leftFactor_)) / leftFactor_;
Shinya Kitaoka 120a6e
    else if (x > lenghtAtParam_ && ((x - lenghtAtParam_) < rightFactor_))
Shinya Kitaoka 120a6e
      x = (rightFactor_ - (x - lenghtAtParam_)) / rightFactor_;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      x = -1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (x < 0.0) return 0.0;
Shinya Kitaoka 120a6e
  // assert( 0.0 <= x &&
Shinya Kitaoka 120a6e
  //  x <= 1.0 + TConsts::epsilon );
Shinya Kitaoka 120a6e
  res = x;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return res;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToonzExt::Potential *ToonzExt::LinearPotential::clone() {
Shinya Kitaoka 120a6e
  return new LinearPotential;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------