tahoma2d/toonz/sources/tnzext/NotSymmetricBezierPotential.cpp

191 lines
5.5 KiB
C++
Raw Normal View History

2016-09-06 01:20:21 +12:00
#include "ext/NotSymmetricBezierPotential.h"
2016-03-19 06:57:51 +13:00
#include "tstroke.h"
#include <tmathutil.h>
#include <tcurves.h>
#include <algorithm>
using namespace std;
using namespace std;
using namespace ToonzExt;
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
typedef unary_function<double, double> unary_functionDD;
//---------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class myBlendFunc : unary_functionDD {
// TCubic c;
TQuadratic curve;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
myBlendFunc(double t = 0.0) {
curve.setP0(TPointD(0.0, 1.0));
curve.setP1(TPointD(0.5 * (1.0 - t), 1.0));
curve.setP2(TPointD(1.0, 0.0));
}
result_type operator()(argument_type x) {
result_type out = 0.0;
x = fabs(x);
if (x >= 1.0) return 0.0;
out = curve.getPoint(x).y;
return out;
}
2016-03-19 06:57:51 +13:00
};
}
//-----------------------------------------------------------------------------
2016-09-06 01:20:21 +12:00
ToonzExt::NotSymmetricBezierPotential::~NotSymmetricBezierPotential() {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-09-06 01:20:21 +12:00
void ToonzExt::NotSymmetricBezierPotential::setParameters_(const TStroke *ref,
2016-06-15 18:43:10 +12:00
double w,
double al) {
assert(ref);
ref_ = ref;
par_ = w;
actionLength_ = al;
strokeLength_ = ref->getLength();
2016-09-06 01:20:21 +12:00
lengthAtParam_ = ref->getLength(par_);
2016-06-15 18:43:10 +12:00
// lunghezza dal pto di click all'inizio della curva
2016-09-06 01:20:21 +12:00
leftFactor_ = min(lengthAtParam_,
actionLength_ * 0.5); // lengthAtParam_ / strokeLength_;
2016-06-15 18:43:10 +12:00
// lunghezza dal pto di click alla fine
2016-09-06 01:20:21 +12:00
rightFactor_ = min(strokeLength_ - lengthAtParam_, actionLength_ * 0.5);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-09-06 01:20:21 +12:00
double ToonzExt::NotSymmetricBezierPotential::value_(double value2test) const {
2016-06-15 18:43:10 +12:00
assert(0.0 <= value2test && value2test <= 1.0);
return this->compute_value(value2test);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
// normalization of parameter in range interval
2016-09-06 01:20:21 +12:00
double ToonzExt::NotSymmetricBezierPotential::compute_shape(
2016-06-15 18:43:10 +12:00
double value2test) const {
double x = ref_->getLength(value2test);
double shape = this->actionLength_ * 0.5;
if (isAlmostZero(shape)) shape = 1.0;
2016-09-06 01:20:21 +12:00
x = (x - lengthAtParam_) / shape;
2016-06-15 18:43:10 +12:00
return x;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-09-06 01:20:21 +12:00
double ToonzExt::NotSymmetricBezierPotential::compute_value(
2016-06-15 18:43:10 +12:00
double value2test) const {
myBlendFunc me;
// on extremes use
// 2
// 1-x
//
// when is near to extreme uses a mix notation
double x = 0.0;
double res = 0.0;
2016-09-06 01:20:21 +12:00
// length at parameter
2016-06-15 18:43:10 +12:00
x = ref_->getLength(value2test);
const double tolerance = 0.0; // need to be pixel based
// if is an extreme
2016-09-06 01:20:21 +12:00
if (max(lengthAtParam_, 0.0) < tolerance ||
max(strokeLength_ - lengthAtParam_, 0.0) < tolerance) {
2016-06-15 18:43:10 +12:00
double tmp_al = actionLength_ * 0.5;
// compute correct parameter considering offset
// try to have a square curve like shape
//
// 2
// m = x
//
if (leftFactor_ <= tolerance)
x = 1.0 - x / tmp_al;
else
x = (x - (strokeLength_ - tmp_al)) / tmp_al;
if (x < 0.0) return 0.0;
assert(0.0 <= x && x <= 1.0 + TConsts::epsilon);
x = std::min(x, 1.0); // just to avoid problem in production code
res = sq(x);
} else // when is not an extreme
{
2016-09-06 01:20:21 +12:00
double length_at_value2test = ref_->getLength(value2test);
2016-06-15 18:43:10 +12:00
const double min_level = 0.01;
// if check a parameter over click point
2016-09-06 01:20:21 +12:00
if (length_at_value2test >= lengthAtParam_) {
2016-06-15 18:43:10 +12:00
// check if extreme can be moved from this parameter configuration
double tmp_x = this->compute_shape(1.0);
double tmp_res = me(tmp_x);
if (tmp_res > min_level) {
// please note that in this case
2016-09-06 01:20:21 +12:00
// lengthAtParam_ + rightFactor_ == strokeLength_
2016-06-15 18:43:10 +12:00
// (by ctor).
if (rightFactor_ != 0.0)
2016-09-06 01:20:21 +12:00
x = (length_at_value2test - lengthAtParam_) / rightFactor_;
2016-06-15 18:43:10 +12:00
else
x = 0.0;
assert(0.0 - TConsts::epsilon <= x && x <= 1.0 + TConsts::epsilon);
if (isAlmostZero(x)) x = 0.0;
if (areAlmostEqual(x, 1.0)) x = 1.0;
double how_many_of_shape =
2016-09-06 01:20:21 +12:00
(strokeLength_ - lengthAtParam_) / (actionLength_ * 0.5);
2016-06-15 18:43:10 +12:00
assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0);
myBlendFunc bf(how_many_of_shape);
return bf(x);
}
} else {
// leftFactor_
double tmp_x = this->compute_shape(0.0);
double tmp_res = me(tmp_x);
if (tmp_res > min_level) {
2016-09-06 01:20:21 +12:00
double x = length_at_value2test / leftFactor_;
2016-06-15 18:43:10 +12:00
assert(0.0 <= x && x <= 1.0);
// then movement use another shape
double diff = x - 1.0;
2016-09-06 01:20:21 +12:00
double how_many_of_shape = lengthAtParam_ / (actionLength_ * 0.5);
2016-06-15 18:43:10 +12:00
assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0);
myBlendFunc bf(how_many_of_shape);
return bf(diff);
}
}
// default behaviour is an exp
x = this->compute_shape(value2test);
res = me(x);
}
return res;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-09-06 01:20:21 +12:00
ToonzExt::Potential *ToonzExt::NotSymmetricBezierPotential::clone() {
return new NotSymmetricBezierPotential;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------