Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// StrokeDeformation.cpp: implementation of the StrokeDeformation class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
#define _STLP_DEBUG 1
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/StrokeDeformation.h"
Toshihiro Shimizu 890ddd
#include "ext/SquarePotential.h"
Toshihiro Shimizu 890ddd
#include "ext/StrokeParametricDeformer.h"
MCCCS a0ce32
#include "ext/NotSymmetricBezierPotential.h"
Toshihiro Shimizu 890ddd
#include "ext/SmoothDeformation.h"
Toshihiro Shimizu 890ddd
#include "ext/ContextStatus.h"
Toshihiro Shimizu 890ddd
#include "ext/Designer.h"
Toshihiro Shimizu 890ddd
//#include "ext/TriParam.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/StraightCornerDeformation.h"
Toshihiro Shimizu 890ddd
#include "ext/CornerDeformation.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ext/StrokeDeformationImpl.h"
Toshihiro Shimizu 890ddd
#include "DeformationSelector.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <tcurves.h></tcurves.h>
Toshihiro Shimizu 890ddd
#include <tstrokeutil.h></tstrokeutil.h>
Toshihiro Shimizu 890ddd
#include <tmathutil.h></tmathutil.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
#include <iterator></iterator>
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace ToonzExt;
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/**
Toshihiro Shimizu 890ddd
 * @brief Manage a mutable context.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static TThread::Mutex s_mutex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StrokeDeformation::StrokeDeformation() {
Shinya Kitaoka 120a6e
  state_           = CREATED;
Shinya Kitaoka 120a6e
  deformationImpl_ = SmoothDeformation::instance();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StrokeDeformation::~StrokeDeformation() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StrokeDeformation::activate(const ContextStatus *status) {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(status && "Status is null!!!");
Shinya Kitaoka 120a6e
  if (!status) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (state_ == RESETTED) delete this->deactivate();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (state_ == CREATED || state_ == DEACTIVE) {
Shinya Kitaoka 120a6e
    deformationImpl_ = this->retrieveDeformator(status);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    // if it is a not valid entry state_ recover
Shinya Kitaoka 120a6e
    assert(deformationImpl_ && "Can not find a valid deformator!!!");
Shinya Kitaoka 120a6e
    if (!deformationImpl_) return;
Shinya Kitaoka 120a6e
    deformationImpl_->reset();
Shinya Kitaoka 120a6e
    deformationImpl_ = DeformationSelector::instance()->getDeformation(status);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(deformationImpl_ && "Deformation is null!!!");
Shinya Kitaoka 120a6e
  if (!deformationImpl_) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!deformationImpl_->activate_impl(status)) {
Shinya Kitaoka 120a6e
    deformationImpl_->reset();
Shinya Kitaoka 120a6e
    state_ = DEACTIVE;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  state_ = ACTIVE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StrokeDeformation::update(const TPointD &delta) {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(deformationImpl_ && "Deformation is null!!!");
Shinya Kitaoka 120a6e
  if (!deformationImpl_) {
Shinya Kitaoka 120a6e
    state_ = DEACTIVE;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (state_ != ACTIVE && state_ != UPDATING) {
Shinya Kitaoka 120a6e
    deformationImpl_->reset();
Shinya Kitaoka 120a6e
    state_ = ACTIVE;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  deformationImpl_->update_impl(delta);
Shinya Kitaoka 120a6e
  state_ = UPDATING;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TStroke *StrokeDeformation::deactivate() {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!deformationImpl_) {
Shinya Kitaoka 120a6e
    state_ = DEACTIVE;
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (state_ != ACTIVE && state_ != UPDATING) {
Shinya Kitaoka 120a6e
    state_ = DEACTIVE;
Shinya Kitaoka 120a6e
    deformationImpl_->reset();
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  state_           = DEACTIVE;
Shinya Kitaoka 120a6e
  TStroke *out     = deformationImpl_->deactivate_impl();
Shinya Kitaoka 120a6e
  deformationImpl_ = 0;
Shinya Kitaoka 120a6e
  return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StrokeDeformation::reset() {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  state_ = RESETTED;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StrokeDeformation::draw(Designer *designer) {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(designer && "Designer is null!!!");
Shinya Kitaoka 120a6e
  if (!deformationImpl_ || !designer) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // this is to draw the icon
Shinya Kitaoka 120a6e
  deformationImpl_->draw(designer);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // this is to draw stroke when change something
Shinya Kitaoka 120a6e
  designer->draw(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StrokeDeformation::check(const ContextStatus *status) {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  if (state_ != UPDATING) deformationImpl_ = this->retrieveDeformator(status);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TStroke *StrokeDeformation::getCopiedStroke() const {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  if (deformationImpl_) return deformationImpl_->getCopiedStroke();
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TStroke *StrokeDeformation::getStroke() const {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  if (deformationImpl_) return deformationImpl_->getStrokeSelected();
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TStroke *StrokeDeformation::getTransformedStroke() const {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  if (deformationImpl_) return deformationImpl_->getTransformedStroke();
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StrokeDeformation::recover() {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  assert(deformationImpl_ && "Deformation is null!!!");
Shinya Kitaoka 120a6e
  if (!deformationImpl_) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  deformationImpl_->reset();
Shinya Kitaoka 120a6e
  //  this->clearStatus();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // deformationImpl_ = DeformationSelector::instance()->getDeformation(status);
Shinya Kitaoka 120a6e
  // assert(deformationImpl_ &&  "Deformation is null!!!" );
Shinya Kitaoka 120a6e
  // if( !deformationImpl_ )
Shinya Kitaoka 120a6e
  //  return;
Shinya Kitaoka 120a6e
  // deformationImpl_->activate_impl(status);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const ContextStatus *StrokeDeformation::getStatus() const {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  if (deformationImpl_) return deformationImpl_->getImplStatus();
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToonzExt::Interval StrokeDeformation::getExtremes() const {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  if (!deformationImpl_) return Interval(-1, -1);
Shinya Kitaoka 120a6e
  return deformationImpl_->getExtremes();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StrokeDeformationImpl *StrokeDeformation::retrieveDeformator(
Shinya Kitaoka 120a6e
    const ContextStatus *status) {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  return DeformationSelector::instance()->getDeformation(status);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int StrokeDeformation::getCursorId() const {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&s_mutex);
Shinya Kitaoka 120a6e
  if (!deformationImpl_) return -1;
Shinya Kitaoka 120a6e
  return deformationImpl_->getCursorId();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const Potential *StrokeDeformation::getPotential() const {
Shinya Kitaoka 120a6e
  assert(deformationImpl_ && "Deformation is null!!!");
Shinya Kitaoka 120a6e
  if (!deformationImpl_) return 0;
Shinya Kitaoka 120a6e
  return deformationImpl_->getPotential();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const StrokeDeformationImpl *StrokeDeformation::getDeformationImpl() const {
Shinya Kitaoka 120a6e
  return deformationImpl_;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------