Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/SquarePotential.h"
Toshihiro Shimizu 890ddd
#include <tmathutil.h></tmathutil.h>
Toshihiro Shimizu 890ddd
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToonzExt::SquarePotential::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
  assert(ref_);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  strokeLength_  = ref->getLength();
MCCCS a0ce32
  lengthAtParam_ = ref->getLength(par);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // lunghezza dal pto di click all'inizio della curva
MCCCS a0ce32
  leftFactor_ = min(lengthAtParam_,
MCCCS a0ce32
                    actionLength_ * 0.5);  // lengthAtParam_ / strokeLength_;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // lunghezza dal pto di click alla fine
MCCCS a0ce32
  rightFactor_ = min(strokeLength_ - lengthAtParam_, actionLength_ * 0.5);
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::SquarePotential::~SquarePotential() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double ToonzExt::SquarePotential::value_(double value2test) const {
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::SquarePotential::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;
MCCCS a0ce32
  x                              = ((x - lengthAtParam_) * range_) / shape;
Shinya Kitaoka 120a6e
  return x;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double ToonzExt::SquarePotential::compute_value(double value2test) const {
Shinya Kitaoka 120a6e
  // use
Shinya Kitaoka 120a6e
  //     2
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
MCCCS a0ce32
  // length  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
  // compute correct parameter considering offset
Shinya Kitaoka 120a6e
  // try to have a square curve like shape
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  //       2
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 {
MCCCS a0ce32
    if (x <= lengthAtParam_ && ((lengthAtParam_ - x) <= leftFactor_))
MCCCS a0ce32
      x = (x - (lengthAtParam_ - leftFactor_)) / leftFactor_;
MCCCS a0ce32
    else if (x > lengthAtParam_ && ((x - lengthAtParam_) < rightFactor_))
MCCCS a0ce32
      x = (rightFactor_ - (x - lengthAtParam_)) / 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 = sq(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::SquarePotential::clone() {
Shinya Kitaoka 120a6e
  return new SquarePotential;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------