|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tgl.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "toonz/strokegenerator.h"
|
|
Toshihiro Shimizu |
890ddd |
//#include "tofflinegl.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
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 StrokeGenerator::clear() {
|
|
Shinya Kitaoka |
120a6e |
m_points.clear();
|
|
Shinya Kitaoka |
120a6e |
m_modifiedRegion = TRectD();
|
|
Shinya Kitaoka |
120a6e |
m_paintedPointCount = 0;
|
|
Shinya Kitaoka |
120a6e |
m_p0 = m_p1 = TPointD();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
bool StrokeGenerator::isEmpty() const { return m_points.empty(); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void StrokeGenerator::add(const TThickPoint &point, double pixelSize2) {
|
|
Shinya Kitaoka |
120a6e |
if (m_points.empty()) {
|
|
Shinya Kitaoka |
120a6e |
double x = point.x, y = point.y, d = point.thick + 3;
|
|
Shinya Kitaoka |
120a6e |
m_points.push_back(point);
|
|
Shinya Kitaoka |
120a6e |
TRectD rect(x - d, y - d, x + d, y + d);
|
|
Shinya Kitaoka |
120a6e |
m_modifiedRegion = rect;
|
|
Shinya Kitaoka |
120a6e |
m_lastModifiedRegion = rect;
|
|
Shinya Kitaoka |
120a6e |
m_p0 = m_p1 = point;
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
TThickPoint lastPoint = m_points.back();
|
|
Shinya Kitaoka |
120a6e |
if (tdistance2(lastPoint, point) >= 4 * pixelSize2) {
|
|
Shinya Kitaoka |
120a6e |
m_points.push_back(point);
|
|
Shinya Kitaoka |
120a6e |
double d = std::max(point.thick, lastPoint.thick) + 3;
|
|
Shinya Kitaoka |
120a6e |
TRectD rect(TRectD(lastPoint, point).enlarge(d));
|
|
Shinya Kitaoka |
120a6e |
m_modifiedRegion += rect;
|
|
Shinya Kitaoka |
120a6e |
m_lastModifiedRegion += rect;
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
m_points.back().thick = std::max(m_points.back().thick, point.thick);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void StrokeGenerator::filterPoints() {
|
|
Shinya Kitaoka |
120a6e |
if (m_points.size() < 10) return;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// filtra m_points iniziali: generalmente elevate variazioni di thickness
|
|
Shinya Kitaoka |
120a6e |
// si hanno tra m_points[0] (al massimo m_points[1]) e i successivi)
|
|
Shinya Kitaoka |
120a6e |
int size1 = m_points.size();
|
|
Shinya Kitaoka |
120a6e |
int kMin = 0;
|
|
Shinya Kitaoka |
120a6e |
int kMax = std::min(
|
|
Shinya Kitaoka |
120a6e |
4,
|
|
Shinya Kitaoka |
120a6e |
size1 -
|
|
Shinya Kitaoka |
120a6e |
2); // confronta 5 m_points iniziali con i successivi corrispondenti
|
|
Shinya Kitaoka |
120a6e |
int k = kMax;
|
|
Shinya Kitaoka |
120a6e |
for (k = kMax; k >= kMin; --k) {
|
|
Shinya Kitaoka |
120a6e |
TThickPoint currPoint = m_points[k];
|
|
Shinya Kitaoka |
120a6e |
TThickPoint nextPoint = m_points[k + 1];
|
|
Shinya Kitaoka |
120a6e |
double dist = tdistance(currPoint, nextPoint);
|
|
Shinya Kitaoka |
120a6e |
double deltaThick = fabs(currPoint.thick - nextPoint.thick);
|
|
Shinya Kitaoka |
120a6e |
if (deltaThick > 0.6 * dist) // deltaThick <= dist (condizione
|
|
Shinya Kitaoka |
120a6e |
// approssimata di non-autocontenimento per
|
|
Shinya Kitaoka |
120a6e |
// TTQ)
|
|
Shinya Kitaoka |
120a6e |
{
|
|
Shinya Kitaoka |
120a6e |
vector<tthickpoint>::iterator it1 = m_points.begin();</tthickpoint>
|
|
Shinya Kitaoka |
120a6e |
vector<tthickpoint>::iterator it2 = it1 + k + 1;</tthickpoint>
|
|
Shinya Kitaoka |
120a6e |
m_points.erase(it1, it2); // cancella da m_points[0] a m_points[k]
|
|
Shinya Kitaoka |
120a6e |
assert((int)m_points.size() == size1 - k - 1);
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
// filtra m_points finali: generalmente elevate variazioni di thickness
|
|
Shinya Kitaoka |
120a6e |
// si hanno tra m_points[size - 1] (al massimo m_points[size - 2]) e i
|
|
Shinya Kitaoka |
120a6e |
// predecessori)
|
|
Shinya Kitaoka |
120a6e |
int size2 = m_points.size();
|
|
Shinya Kitaoka |
120a6e |
kMax = size2 - 1;
|
|
Shinya Kitaoka |
120a6e |
kMin = std::max(
|
|
Shinya Kitaoka |
120a6e |
kMax - 4,
|
|
Shinya Kitaoka |
120a6e |
1); // confronta 5 m_points finali con i predecessori corrispondenti
|
|
Shinya Kitaoka |
120a6e |
k = kMin;
|
|
Shinya Kitaoka |
120a6e |
for (k = kMin; k <= kMax; ++k) {
|
|
Shinya Kitaoka |
120a6e |
TThickPoint currPoint = m_points[k];
|
|
Shinya Kitaoka |
120a6e |
TThickPoint prevPoint = m_points[k - 1];
|
|
Shinya Kitaoka |
120a6e |
double dist = tdistance(currPoint, prevPoint);
|
|
Shinya Kitaoka |
120a6e |
double deltaThick = fabs(currPoint.thick - prevPoint.thick);
|
|
Shinya Kitaoka |
120a6e |
if (deltaThick > 0.6 * dist) // deltaThick <= dist (condizione
|
|
Shinya Kitaoka |
120a6e |
// approssimata di non-autocontenimento per
|
|
Shinya Kitaoka |
120a6e |
// TTQ)
|
|
Shinya Kitaoka |
120a6e |
{
|
|
Shinya Kitaoka |
120a6e |
int kTmp = k;
|
|
Shinya Kitaoka |
120a6e |
while (k <= kMax) // cancella da m_points[k] a m_points[size2 - 1]
|
|
Shinya Kitaoka |
120a6e |
{
|
|
Shinya Kitaoka |
120a6e |
m_points.pop_back();
|
|
Shinya Kitaoka |
120a6e |
++k;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
assert((int)m_points.size() == size2 - (kMax - kTmp + 1));
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void StrokeGenerator::drawFragments(int first, int last) {
|
|
Shinya Kitaoka |
120a6e |
if (m_points.empty()) return;
|
|
Shinya Kitaoka |
120a6e |
int i = first;
|
|
Shinya Kitaoka |
120a6e |
if (last >= (int)m_points.size()) last = m_points.size() - 1;
|
|
Shinya Kitaoka |
120a6e |
const double h = 0.01;
|
|
Shinya Kitaoka |
120a6e |
TThickPoint a;
|
|
Shinya Kitaoka |
120a6e |
TThickPoint b;
|
|
Shinya Kitaoka |
120a6e |
TThickPoint c;
|
|
Shinya Kitaoka |
120a6e |
TPointD v;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
while (i < last) {
|
|
Shinya Kitaoka |
120a6e |
a = m_points[i - 1];
|
|
Shinya Kitaoka |
120a6e |
b = m_points[i];
|
|
Shinya Kitaoka |
120a6e |
c = m_points[i + 1];
|
|
Shinya Kitaoka |
120a6e |
if (a.thick >= h && b.thick >= h && tdistance2(b, a) >= h &&
|
|
Shinya Kitaoka |
120a6e |
tdistance2(a, c) >= h) {
|
|
Shinya Kitaoka |
120a6e |
if (i - 1 == 0) {
|
|
Shinya Kitaoka |
120a6e |
assert(tdistance(b, a) > h);
|
|
Shinya Kitaoka |
120a6e |
v = a.thick * normalize(rotate90(b - a));
|
|
Shinya Kitaoka |
120a6e |
m_p0 = a + v;
|
|
Shinya Kitaoka |
120a6e |
m_p1 = a - v;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
assert(tdistance(c, a) > h);
|
|
Shinya Kitaoka |
120a6e |
v = b.thick * normalize(rotate90(c - a));
|
|
Shinya Kitaoka |
120a6e |
TPointD p0 = b + v;
|
|
Shinya Kitaoka |
120a6e |
TPointD p1 = b - v;
|
|
Shinya Kitaoka |
120a6e |
glBegin(GL_POLYGON);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(m_p0);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(m_p1);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(p1);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(p0);
|
|
Shinya Kitaoka |
120a6e |
glEnd();
|
|
Shinya Kitaoka |
120a6e |
m_p0 = p0;
|
|
Shinya Kitaoka |
120a6e |
m_p1 = p1;
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
m_p0 = m_p1 = b;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
glBegin(GL_LINE_STRIP);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(a);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(b);
|
|
Shinya Kitaoka |
120a6e |
glEnd();
|
|
Shinya Kitaoka |
120a6e |
i++;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void StrokeGenerator::drawLastFragments() {
|
|
Shinya Kitaoka |
120a6e |
if (m_points.empty()) return;
|
|
Shinya Kitaoka |
120a6e |
int n = m_points.size();
|
|
Shinya Kitaoka |
120a6e |
int i = m_paintedPointCount;
|
|
Shinya Kitaoka |
120a6e |
const double h = 0.01;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (i == 0) {
|
|
Shinya Kitaoka |
120a6e |
TThickPoint a = m_points[0];
|
|
Shinya Kitaoka |
120a6e |
if (a.thick >= h) tglDrawDisk(a, a.thick);
|
|
Shinya Kitaoka |
120a6e |
i++;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
drawFragments(i, n - 1);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
m_paintedPointCount = std::max(0, n - 2);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void StrokeGenerator::drawAllFragments() {
|
|
Shinya Kitaoka |
120a6e |
if (m_points.empty()) return;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
int n = m_points.size();
|
|
Shinya Kitaoka |
120a6e |
int i = 0;
|
|
Shinya Kitaoka |
120a6e |
const double h = 0.01;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
TThickPoint a = m_points[0];
|
|
Shinya Kitaoka |
120a6e |
if (a.thick >= h) tglDrawDisk(a, a.thick);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
drawFragments(1, n - 1);
|
|
Shinya Kitaoka |
120a6e |
/*
|
|
Shinya Kitaoka |
120a6e |
//last fragment
|
|
Shinya Kitaoka |
120a6e |
TPointD p0 = c+v;
|
|
Shinya Kitaoka |
120a6e |
TPointD p1 = c-v;
|
|
Shinya Kitaoka |
120a6e |
glBegin(GL_POLYGON);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(m_p0);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(m_p1);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(p1);
|
|
Shinya Kitaoka |
120a6e |
tglVertex(p0);
|
|
Shinya Kitaoka |
120a6e |
glEnd();
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
a = m_points.back();
|
|
Shinya Kitaoka |
120a6e |
if (a.thick >= h) tglDrawDisk(a, a.thick);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TRectD StrokeGenerator::getModifiedRegion() const { return m_modifiedRegion; }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TRectD StrokeGenerator::getLastModifiedRegion() {
|
|
Shinya Kitaoka |
120a6e |
TRectD lastModifiedRegion = m_lastModifiedRegion;
|
|
Shinya Kitaoka |
120a6e |
m_lastModifiedRegion.empty();
|
|
Shinya Kitaoka |
120a6e |
return lastModifiedRegion;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TStroke *StrokeGenerator::makeStroke(double error, UINT onlyLastPoints) const {
|
|
Shinya Kitaoka |
120a6e |
if (onlyLastPoints == 0 || onlyLastPoints > m_points.size())
|
|
Shinya Kitaoka |
120a6e |
return TStroke::interpolate(m_points, error);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
vector<tthickpoint> lastPoints(onlyLastPoints);</tthickpoint>
|
|
Shinya Kitaoka |
120a6e |
vector<tthickpoint>::const_iterator first =</tthickpoint>
|
|
Shinya Kitaoka |
120a6e |
m_points.begin() + (m_points.size() - onlyLastPoints);
|
|
Shinya Kitaoka |
120a6e |
copy(first, m_points.end(), lastPoints.begin());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
return TStroke::interpolate(lastPoints, error);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------
|