2007-07-07 07:51:18 +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/
|
2007-07-07 07:51:18 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file spline_curve.cpp
|
|
|
|
/// @brief Handle bicubic splines (Bezier curves) in vector drawings
|
|
|
|
/// @ingroup visual_ts
|
|
|
|
///
|
2007-07-07 07:51:18 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-07-07 07:51:18 +02:00
|
|
|
#include "spline_curve.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <limits>
|
|
|
|
#include <numeric>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SplineCurve::SplineCurve(Vector2D p1) : p1(p1), type(POINT) { }
|
|
|
|
SplineCurve::SplineCurve(Vector2D p1, Vector2D p2) : p1(p1), p2(p2), type(LINE) { }
|
|
|
|
SplineCurve::SplineCurve(Vector2D p1, Vector2D p2, Vector2D p3, Vector2D p4)
|
|
|
|
: p1(p1), p2(p2), p3(p3), p4(p4), type(BICUBIC)
|
|
|
|
{
|
2007-07-07 07:51:18 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:25:08 +01:00
|
|
|
std::pair<SplineCurve, SplineCurve> SplineCurve::Split(float t) {
|
2011-11-06 18:18:20 +01:00
|
|
|
if (type == LINE) {
|
2011-12-22 22:25:08 +01:00
|
|
|
Vector2D m = p1 * (1 - t) + p2 * t;
|
|
|
|
return std::make_pair(
|
|
|
|
SplineCurve(p1, m),
|
|
|
|
SplineCurve(m, p2));
|
2007-07-07 07:51:18 +02:00
|
|
|
}
|
2011-11-06 18:18:20 +01:00
|
|
|
else if (type == BICUBIC) {
|
|
|
|
float u = 1 - t;
|
|
|
|
Vector2D p12 = p1 * u + p2 * t;
|
|
|
|
Vector2D p23 = p2 * u + p3 * t;
|
|
|
|
Vector2D p34 = p3 * u + p4 * t;
|
|
|
|
Vector2D p123 = p12 * u + p23 * t;
|
|
|
|
Vector2D p234 = p23 * u + p34 * t;
|
|
|
|
Vector2D p1234 = p123 * u + p234 * t;
|
|
|
|
|
2011-12-22 22:25:08 +01:00
|
|
|
return std::make_pair(
|
|
|
|
SplineCurve(p1, p12, p123, p1234),
|
|
|
|
SplineCurve(p1234, p234, p34, p4));
|
2007-07-07 07:51:18 +02:00
|
|
|
}
|
2011-12-22 22:25:08 +01:00
|
|
|
return std::make_pair(SplineCurve(p1), SplineCurve(p1));
|
2007-07-07 07:51:18 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
void SplineCurve::Smooth(Vector2D p0, Vector2D p3, float smooth) {
|
|
|
|
if (type != LINE || p1 == p2) return;
|
|
|
|
smooth = mid(0.f, smooth, 1.f);
|
2007-07-07 07:51:18 +02:00
|
|
|
|
|
|
|
// Calculate intermediate points
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D c1 = (p0 + p1) / 2.f;
|
|
|
|
Vector2D c2 = (p1 + p2) / 2.f;
|
|
|
|
Vector2D c3 = (p2 + p3) / 2.f;
|
|
|
|
|
|
|
|
float len1 = (p1 - p0).Len();
|
|
|
|
float len2 = (p2 - p1).Len();
|
|
|
|
float len3 = (p3 - p2).Len();
|
|
|
|
|
|
|
|
float k1 = len1 / (len1 + len2);
|
|
|
|
float k2 = len2 / (len2 + len3);
|
|
|
|
|
|
|
|
Vector2D m1 = c1 + (c2 - c1) * k1;
|
|
|
|
Vector2D m2 = c2 + (c3 - c2) * k2;
|
2007-07-07 07:51:18 +02:00
|
|
|
|
|
|
|
// Set curve points
|
|
|
|
p4 = p2;
|
2011-11-06 18:18:20 +01:00
|
|
|
p3 = m2 + (c2 - m2) * smooth + p2 - m2;
|
|
|
|
p2 = m1 + (c2 - m1) * smooth + p1 - m1;
|
|
|
|
type = BICUBIC;
|
2007-07-07 07:51:18 +02:00
|
|
|
}
|
2007-07-07 08:41:14 +02:00
|
|
|
|
2007-07-07 10:53:11 +02:00
|
|
|
Vector2D SplineCurve::GetPoint(float t) const {
|
2011-11-06 18:18:20 +01:00
|
|
|
float u = 1.f - t;
|
|
|
|
|
|
|
|
if (type == POINT)
|
|
|
|
return p1;
|
|
|
|
if (type == LINE)
|
|
|
|
return p1 * u + p2 * t;
|
2007-07-07 10:53:11 +02:00
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
return p1*u*u*u + 3*p2*t*u*u + 3*p3*t*t*u + p4*t*t*t;
|
2007-07-07 08:41:14 +02:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
Vector2D& SplineCurve::EndPoint() {
|
2007-07-07 08:41:14 +02:00
|
|
|
switch (type) {
|
2011-11-06 18:18:20 +01:00
|
|
|
case POINT: return p1;
|
|
|
|
case LINE: return p2;
|
|
|
|
case BICUBIC: return p4;
|
|
|
|
default: return p1;
|
2007-07-07 08:41:14 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-07 10:53:11 +02:00
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D SplineCurve::GetClosestPoint(Vector2D ref) const {
|
2007-07-07 10:53:11 +02:00
|
|
|
return GetPoint(GetClosestParam(ref));
|
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
float SplineCurve::GetClosestParam(Vector2D ref) const {
|
|
|
|
if (type == LINE)
|
|
|
|
return GetClosestSegmentPart(p1, p2, ref);
|
|
|
|
|
|
|
|
if (type == BICUBIC) {
|
2007-07-07 10:53:11 +02:00
|
|
|
int steps = 100;
|
2011-11-06 18:18:20 +01:00
|
|
|
float bestDist = std::numeric_limits<float>::max();
|
2010-06-08 08:09:19 +02:00
|
|
|
float bestT = 0.f;
|
2011-11-06 18:18:20 +01:00
|
|
|
for (int i = 0; i <= steps; ++i) {
|
|
|
|
float t = i / float(steps);
|
|
|
|
float dist = (GetPoint(t) - ref).SquareLen();
|
2007-07-07 10:53:11 +02:00
|
|
|
if (dist < bestDist) {
|
|
|
|
bestDist = dist;
|
|
|
|
bestT = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestT;
|
|
|
|
}
|
2011-11-06 18:18:20 +01:00
|
|
|
|
2010-06-08 08:09:19 +02:00
|
|
|
return 0.f;
|
2007-07-07 10:53:11 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
float SplineCurve::GetQuickDistance(Vector2D ref) const {
|
|
|
|
if (type == BICUBIC) {
|
|
|
|
float lens[] = {
|
|
|
|
GetClosestSegmentDistance(p1, p2, ref),
|
|
|
|
GetClosestSegmentDistance(p2, p3, ref),
|
|
|
|
GetClosestSegmentDistance(p3, p4, ref),
|
|
|
|
GetClosestSegmentDistance(p4, p1, ref),
|
|
|
|
GetClosestSegmentDistance(p1, p3, ref),
|
|
|
|
GetClosestSegmentDistance(p2, p4, ref)
|
|
|
|
};
|
|
|
|
return *std::min_element(lens, lens + 6);
|
2007-07-07 23:13:20 +02:00
|
|
|
}
|
2011-11-06 18:18:20 +01:00
|
|
|
return (GetClosestPoint(ref) - ref).Len();
|
2007-07-07 10:53:11 +02:00
|
|
|
}
|
2007-07-07 23:13:20 +02:00
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
float SplineCurve::GetClosestSegmentPart(Vector2D pt1, Vector2D pt2, Vector2D pt3) const {
|
|
|
|
return mid(0.f, (pt3 - pt1).Dot(pt2 - pt1) / (pt2 - pt1).SquareLen(), 1.f);
|
2007-07-07 23:13:20 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
float SplineCurve::GetClosestSegmentDistance(Vector2D pt1, Vector2D pt2, Vector2D pt3) const {
|
|
|
|
float t = GetClosestSegmentPart(pt1, pt2, pt3);
|
|
|
|
return (pt1 * (1.f - t) + pt2 * t - pt3).Len();
|
2007-07-07 23:13:20 +02:00
|
|
|
}
|