2007-07-05 04:01:12 +02:00
|
|
|
// Copyright (c) 2007, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file spline.cpp
|
|
|
|
/// @brief Manage vector drawings for visual typesetting tools
|
|
|
|
/// @ingroup visual_ts
|
|
|
|
///
|
2007-07-05 04:01:12 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-07-05 04:01:12 +02:00
|
|
|
#include "spline.h"
|
2011-11-06 18:18:20 +01:00
|
|
|
|
2007-07-07 07:51:18 +02:00
|
|
|
#include "utils.h"
|
2011-11-06 18:18:20 +01:00
|
|
|
#include "visual_tool.h"
|
2007-07-05 16:30:28 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/util.h>
|
|
|
|
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
#include <limits>
|
|
|
|
|
2012-01-26 01:29:08 +01:00
|
|
|
Spline::Spline(const VisualToolBase &tl)
|
|
|
|
: coord_translator(tl)
|
|
|
|
, scale(1)
|
|
|
|
, raw_scale(1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D Spline::ToScript(Vector2D vec) const {
|
|
|
|
return coord_translator.ToScriptCoords(vec) * scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D Spline::FromScript(Vector2D vec) const {
|
|
|
|
return coord_translator.FromScriptCoords(vec / scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Spline::SetScale(int new_scale) {
|
|
|
|
raw_scale = new_scale;
|
|
|
|
scale = 1 << (raw_scale - 1);
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string Spline::EncodeToAss() const {
|
|
|
|
std::string result;
|
2011-11-06 18:18:20 +01:00
|
|
|
result.reserve(size() * 10);
|
|
|
|
char last = 0;
|
2007-07-05 04:01:12 +02:00
|
|
|
|
2012-03-20 01:39:10 +01:00
|
|
|
for (const_iterator cur = begin(); cur != end(); ++cur) {
|
2007-07-05 04:01:12 +02:00
|
|
|
switch (cur->type) {
|
2011-11-06 18:18:20 +01:00
|
|
|
case SplineCurve::POINT:
|
|
|
|
if (last != 'm') {
|
2011-09-28 21:43:11 +02:00
|
|
|
result += "m ";
|
2011-11-06 18:18:20 +01:00
|
|
|
last = 'm';
|
2010-06-08 08:09:19 +02:00
|
|
|
}
|
2012-01-26 01:29:08 +01:00
|
|
|
result += ToScript(cur->p1).DStr(' ');
|
2010-06-08 08:09:19 +02:00
|
|
|
break;
|
2011-11-06 18:18:20 +01:00
|
|
|
|
|
|
|
case SplineCurve::LINE:
|
|
|
|
if (last != 'l') {
|
2011-09-28 21:43:11 +02:00
|
|
|
result += "l ";
|
2011-11-06 18:18:20 +01:00
|
|
|
last = 'l';
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
2012-01-26 01:29:08 +01:00
|
|
|
result += ToScript(cur->p2).DStr(' ');
|
2007-07-05 04:01:12 +02:00
|
|
|
break;
|
2011-11-06 18:18:20 +01:00
|
|
|
|
|
|
|
case SplineCurve::BICUBIC:
|
|
|
|
if (last != 'b') {
|
2011-09-28 21:43:11 +02:00
|
|
|
result += "b ";
|
2011-11-06 18:18:20 +01:00
|
|
|
last = 'b';
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
2012-01-26 01:29:08 +01:00
|
|
|
result += ToScript(cur->p2).DStr(' ') + " ";
|
|
|
|
result += ToScript(cur->p3).DStr(' ') + " ";
|
|
|
|
result += ToScript(cur->p4).DStr(' ');
|
2007-07-05 04:01:12 +02:00
|
|
|
break;
|
2011-11-06 18:18:20 +01:00
|
|
|
|
2007-07-05 04:01:12 +02:00
|
|
|
default: break;
|
|
|
|
}
|
2011-11-06 18:18:20 +01:00
|
|
|
result += " ";
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void Spline::DecodeFromAss(std::string const& str) {
|
2007-07-05 04:01:12 +02:00
|
|
|
// Clear current
|
2010-06-08 08:09:19 +02:00
|
|
|
clear();
|
2011-11-06 18:18:20 +01:00
|
|
|
std::vector<float> stack;
|
2007-07-05 06:32:46 +02:00
|
|
|
|
|
|
|
// Prepare
|
2011-11-06 18:18:20 +01:00
|
|
|
char command = 'm';
|
2011-12-07 00:13:06 +01:00
|
|
|
Vector2D pt(0, 0);
|
2007-07-05 06:32:46 +02:00
|
|
|
|
|
|
|
// Tokenize the string
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::char_separator<char> sep("|");
|
|
|
|
for (auto const& token : boost::tokenizer<boost::char_separator<char>>(str, sep)) {
|
2011-11-06 18:18:20 +01:00
|
|
|
double n;
|
2013-01-04 16:01:50 +01:00
|
|
|
if (agi::util::try_parse(token, &n)) {
|
2007-07-05 06:32:46 +02:00
|
|
|
stack.push_back(n);
|
|
|
|
|
|
|
|
// Move
|
2011-11-06 18:18:20 +01:00
|
|
|
if (stack.size() == 2 && command == 'm') {
|
2012-01-26 01:29:08 +01:00
|
|
|
pt = FromScript(Vector2D(stack[0], stack[1]));
|
2007-07-05 06:32:46 +02:00
|
|
|
stack.clear();
|
2011-11-06 18:18:20 +01:00
|
|
|
|
|
|
|
push_back(pt);
|
2007-07-05 06:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Line
|
2011-11-06 18:18:20 +01:00
|
|
|
if (stack.size() == 2 && command == 'l') {
|
2012-01-26 01:29:08 +01:00
|
|
|
SplineCurve curve(pt, FromScript(Vector2D(stack[0], stack[1])));
|
2010-06-08 08:09:19 +02:00
|
|
|
push_back(curve);
|
2011-11-06 18:18:20 +01:00
|
|
|
|
|
|
|
pt = curve.p2;
|
|
|
|
stack.clear();
|
2007-07-05 06:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bicubic
|
2011-11-06 18:18:20 +01:00
|
|
|
else if (stack.size() == 6 && command == 'b') {
|
|
|
|
SplineCurve curve(pt,
|
2012-01-26 01:29:08 +01:00
|
|
|
FromScript(Vector2D(stack[0], stack[1])),
|
|
|
|
FromScript(Vector2D(stack[2], stack[3])),
|
|
|
|
FromScript(Vector2D(stack[4], stack[5])));
|
2010-06-08 08:09:19 +02:00
|
|
|
push_back(curve);
|
2007-07-05 06:32:46 +02:00
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
pt = curve.p4;
|
2007-07-05 06:32:46 +02:00
|
|
|
stack.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Got something else
|
2011-11-06 18:18:20 +01:00
|
|
|
else if (token.size() == 1) {
|
|
|
|
command = token[0];
|
|
|
|
stack.clear();
|
2007-07-05 06:32:46 +02:00
|
|
|
}
|
|
|
|
}
|
2012-01-27 20:22:46 +01:00
|
|
|
|
|
|
|
if (!empty() && front().type != SplineCurve::POINT)
|
|
|
|
push_front(pt);
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
void Spline::MovePoint(iterator curve,int point,Vector2D pos) {
|
2012-11-29 05:36:03 +01:00
|
|
|
iterator prev = std::prev(curve, curve != begin());
|
|
|
|
iterator next = std::next(curve);
|
2011-11-06 18:18:20 +01:00
|
|
|
if (next != end() && next->type == SplineCurve::POINT)
|
|
|
|
next = end();
|
2007-07-05 06:32:46 +02:00
|
|
|
|
|
|
|
// Modify
|
|
|
|
if (point == 0) {
|
2010-06-08 08:09:19 +02:00
|
|
|
curve->p1 = pos;
|
2011-11-06 18:18:20 +01:00
|
|
|
if (curve != begin() && curve->type != SplineCurve::POINT)
|
|
|
|
prev->EndPoint() = pos;
|
|
|
|
if (next != end() && curve->type == SplineCurve::POINT)
|
|
|
|
next->p1 = pos;
|
2007-07-05 06:32:46 +02:00
|
|
|
}
|
|
|
|
else if (point == 1) {
|
2010-06-08 08:09:19 +02:00
|
|
|
curve->p2 = pos;
|
2011-11-06 18:18:20 +01:00
|
|
|
if (next != end() && curve->type == SplineCurve::LINE)
|
|
|
|
next->p1 = pos;
|
2007-07-05 06:32:46 +02:00
|
|
|
}
|
|
|
|
else if (point == 2) {
|
2010-06-08 08:09:19 +02:00
|
|
|
curve->p3 = pos;
|
2007-07-05 06:32:46 +02:00
|
|
|
}
|
|
|
|
else if (point == 3) {
|
2010-06-08 08:09:19 +02:00
|
|
|
curve->p4 = pos;
|
2011-11-06 18:18:20 +01:00
|
|
|
if (next != end())
|
|
|
|
next->p1 = pos;
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
void Spline::GetPointList(std::vector<float>& points, std::vector<int>& first, std::vector<int>& count) {
|
2007-07-05 04:01:12 +02:00
|
|
|
points.clear();
|
2010-06-08 08:09:19 +02:00
|
|
|
first.clear();
|
|
|
|
count.clear();
|
|
|
|
|
|
|
|
points.reserve((size() + 1) * 2);
|
|
|
|
int curCount = 0;
|
2007-07-05 04:01:12 +02:00
|
|
|
|
|
|
|
// Generate points for each curve
|
2011-11-06 18:18:20 +01:00
|
|
|
for (iterator cur = begin(); cur != end(); ++cur) {
|
2012-03-20 01:39:33 +01:00
|
|
|
if (cur->type == SplineCurve::POINT) {
|
|
|
|
if (curCount > 0)
|
|
|
|
count.push_back(curCount);
|
2011-11-06 18:18:20 +01:00
|
|
|
|
2012-03-20 01:39:33 +01:00
|
|
|
// start new path
|
|
|
|
first.push_back(points.size() / 2);
|
|
|
|
curCount = 0;
|
2007-07-08 09:22:09 +02:00
|
|
|
}
|
2012-03-20 01:39:33 +01:00
|
|
|
curCount += cur->GetPoints(points);
|
2010-06-08 08:09:19 +02:00
|
|
|
}
|
2007-07-05 04:01:12 +02:00
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
count.push_back(curCount);
|
|
|
|
}
|
2011-11-06 18:18:20 +01:00
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
void Spline::GetPointList(std::vector<float> &points, iterator curve) {
|
|
|
|
points.clear();
|
|
|
|
if (curve == end()) return;
|
|
|
|
switch (curve->type) {
|
2011-11-06 18:18:20 +01:00
|
|
|
case SplineCurve::LINE:
|
|
|
|
points.push_back(curve->p1.X());
|
|
|
|
points.push_back(curve->p1.Y());
|
|
|
|
points.push_back(curve->p2.X());
|
|
|
|
points.push_back(curve->p2.Y());
|
2010-06-08 08:09:19 +02:00
|
|
|
break;
|
2011-11-06 18:18:20 +01:00
|
|
|
|
|
|
|
case SplineCurve::BICUBIC:
|
2012-03-20 01:39:33 +01:00
|
|
|
curve->GetPoints(points);
|
2010-06-08 08:09:19 +02:00
|
|
|
break;
|
2011-11-06 18:18:20 +01:00
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
default: break;
|
2007-07-05 06:32:46 +02:00
|
|
|
}
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
void Spline::GetClosestParametricPoint(Vector2D reference,iterator &curve,float &t,Vector2D &pt) {
|
2010-06-08 08:09:19 +02:00
|
|
|
curve = end();
|
|
|
|
t = 0.f;
|
|
|
|
if (empty()) return;
|
2007-07-07 10:53:11 +02:00
|
|
|
|
|
|
|
// Close the shape
|
2012-11-28 16:35:26 +01:00
|
|
|
emplace_back(back().EndPoint(), front().p1);
|
2007-07-07 10:53:11 +02:00
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
float closest = std::numeric_limits<float>::infinity();
|
2011-11-06 18:18:20 +01:00
|
|
|
for (iterator cur = begin(); cur != end(); ++cur) {
|
2007-07-07 10:53:11 +02:00
|
|
|
float param = cur->GetClosestParam(reference);
|
|
|
|
Vector2D p1 = cur->GetPoint(param);
|
2010-06-08 08:09:19 +02:00
|
|
|
float dist = (p1-reference).SquareLen();
|
2007-07-07 10:53:11 +02:00
|
|
|
if (dist < closest) {
|
|
|
|
closest = dist;
|
|
|
|
t = param;
|
2010-06-08 08:09:19 +02:00
|
|
|
curve = cur;
|
2007-07-07 10:53:11 +02:00
|
|
|
pt = p1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
if (&*curve == &back()) {
|
|
|
|
curve = end();
|
|
|
|
}
|
|
|
|
|
2007-07-07 10:53:11 +02:00
|
|
|
// Remove closing and return
|
2010-06-08 08:09:19 +02:00
|
|
|
pop_back();
|
2007-07-05 16:30:28 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D Spline::GetClosestPoint(Vector2D reference) {
|
2010-06-08 08:09:19 +02:00
|
|
|
iterator curve;
|
2007-07-07 10:53:11 +02:00
|
|
|
float t;
|
|
|
|
Vector2D point;
|
2011-11-06 18:18:20 +01:00
|
|
|
GetClosestParametricPoint(reference, curve, t, point);
|
2007-07-07 10:53:11 +02:00
|
|
|
return point;
|
2007-07-05 04:01:12 +02:00
|
|
|
}
|
|
|
|
|
2007-07-07 07:51:18 +02:00
|
|
|
void Spline::Smooth(float smooth) {
|
|
|
|
// See if there are enough curves
|
2010-06-08 08:09:19 +02:00
|
|
|
if (size() < 3) return;
|
2007-07-07 07:51:18 +02:00
|
|
|
|
|
|
|
// Smooth curve
|
2012-11-29 05:36:03 +01:00
|
|
|
for (iterator cur = begin(); cur != end(); ++cur) {
|
|
|
|
iterator prev_curve = prev(cur == begin() ? cur : end());
|
|
|
|
iterator next_curve = next(cur);
|
|
|
|
if (next_curve == end())
|
|
|
|
next_curve = begin();
|
|
|
|
|
|
|
|
cur->Smooth(prev_curve->p1, next_curve->EndPoint(), smooth);
|
2007-07-07 07:51:18 +02:00
|
|
|
}
|
|
|
|
}
|