Path: blob/master/thirdparty/msdfgen/core/edge-segments.cpp
9903 views
1#include "edge-segments.h"23#include "arithmetics.hpp"4#include "equation-solver.h"56namespace msdfgen {78EdgeSegment *EdgeSegment::create(Point2 p0, Point2 p1, EdgeColor edgeColor) {9return new LinearSegment(p0, p1, edgeColor);10}1112EdgeSegment *EdgeSegment::create(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) {13if (!crossProduct(p1-p0, p2-p1))14return new LinearSegment(p0, p2, edgeColor);15return new QuadraticSegment(p0, p1, p2, edgeColor);16}1718EdgeSegment *EdgeSegment::create(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) {19Vector2 p12 = p2-p1;20if (!crossProduct(p1-p0, p12) && !crossProduct(p12, p3-p2))21return new LinearSegment(p0, p3, edgeColor);22if ((p12 = 1.5*p1-.5*p0) == 1.5*p2-.5*p3)23return new QuadraticSegment(p0, p12, p3, edgeColor);24return new CubicSegment(p0, p1, p2, p3, edgeColor);25}2627void EdgeSegment::distanceToPerpendicularDistance(SignedDistance &distance, Point2 origin, double param) const {28if (param < 0) {29Vector2 dir = direction(0).normalize();30Vector2 aq = origin-point(0);31double ts = dotProduct(aq, dir);32if (ts < 0) {33double perpendicularDistance = crossProduct(aq, dir);34if (fabs(perpendicularDistance) <= fabs(distance.distance)) {35distance.distance = perpendicularDistance;36distance.dot = 0;37}38}39} else if (param > 1) {40Vector2 dir = direction(1).normalize();41Vector2 bq = origin-point(1);42double ts = dotProduct(bq, dir);43if (ts > 0) {44double perpendicularDistance = crossProduct(bq, dir);45if (fabs(perpendicularDistance) <= fabs(distance.distance)) {46distance.distance = perpendicularDistance;47distance.dot = 0;48}49}50}51}5253LinearSegment::LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor) : EdgeSegment(edgeColor) {54p[0] = p0;55p[1] = p1;56}5758QuadraticSegment::QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) : EdgeSegment(edgeColor) {59p[0] = p0;60p[1] = p1;61p[2] = p2;62}6364CubicSegment::CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : EdgeSegment(edgeColor) {65p[0] = p0;66p[1] = p1;67p[2] = p2;68p[3] = p3;69}7071LinearSegment *LinearSegment::clone() const {72return new LinearSegment(p[0], p[1], color);73}7475QuadraticSegment *QuadraticSegment::clone() const {76return new QuadraticSegment(p[0], p[1], p[2], color);77}7879CubicSegment *CubicSegment::clone() const {80return new CubicSegment(p[0], p[1], p[2], p[3], color);81}8283int LinearSegment::type() const {84return (int) EDGE_TYPE;85}8687int QuadraticSegment::type() const {88return (int) EDGE_TYPE;89}9091int CubicSegment::type() const {92return (int) EDGE_TYPE;93}9495const Point2 *LinearSegment::controlPoints() const {96return p;97}9899const Point2 *QuadraticSegment::controlPoints() const {100return p;101}102103const Point2 *CubicSegment::controlPoints() const {104return p;105}106107Point2 LinearSegment::point(double param) const {108return mix(p[0], p[1], param);109}110111Point2 QuadraticSegment::point(double param) const {112return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);113}114115Point2 CubicSegment::point(double param) const {116Vector2 p12 = mix(p[1], p[2], param);117return mix(mix(mix(p[0], p[1], param), p12, param), mix(p12, mix(p[2], p[3], param), param), param);118}119120Vector2 LinearSegment::direction(double param) const {121return p[1]-p[0];122}123124Vector2 QuadraticSegment::direction(double param) const {125Vector2 tangent = mix(p[1]-p[0], p[2]-p[1], param);126if (!tangent)127return p[2]-p[0];128return tangent;129}130131Vector2 CubicSegment::direction(double param) const {132Vector2 tangent = mix(mix(p[1]-p[0], p[2]-p[1], param), mix(p[2]-p[1], p[3]-p[2], param), param);133if (!tangent) {134if (param == 0) return p[2]-p[0];135if (param == 1) return p[3]-p[1];136}137return tangent;138}139140Vector2 LinearSegment::directionChange(double param) const {141return Vector2();142}143144Vector2 QuadraticSegment::directionChange(double param) const {145return (p[2]-p[1])-(p[1]-p[0]);146}147148Vector2 CubicSegment::directionChange(double param) const {149return mix((p[2]-p[1])-(p[1]-p[0]), (p[3]-p[2])-(p[2]-p[1]), param);150}151152double LinearSegment::length() const {153return (p[1]-p[0]).length();154}155156double QuadraticSegment::length() const {157Vector2 ab = p[1]-p[0];158Vector2 br = p[2]-p[1]-ab;159double abab = dotProduct(ab, ab);160double abbr = dotProduct(ab, br);161double brbr = dotProduct(br, br);162double abLen = sqrt(abab);163double brLen = sqrt(brbr);164double crs = crossProduct(ab, br);165double h = sqrt(abab+abbr+abbr+brbr);166return (167brLen*((abbr+brbr)*h-abbr*abLen)+168crs*crs*log((brLen*h+abbr+brbr)/(brLen*abLen+abbr))169)/(brbr*brLen);170}171172SignedDistance LinearSegment::signedDistance(Point2 origin, double ¶m) const {173Vector2 aq = origin-p[0];174Vector2 ab = p[1]-p[0];175param = dotProduct(aq, ab)/dotProduct(ab, ab);176Vector2 eq = p[param > .5]-origin;177double endpointDistance = eq.length();178if (param > 0 && param < 1) {179double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);180if (fabs(orthoDistance) < endpointDistance)181return SignedDistance(orthoDistance, 0);182}183return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));184}185186SignedDistance QuadraticSegment::signedDistance(Point2 origin, double ¶m) const {187Vector2 qa = p[0]-origin;188Vector2 ab = p[1]-p[0];189Vector2 br = p[2]-p[1]-ab;190double a = dotProduct(br, br);191double b = 3*dotProduct(ab, br);192double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);193double d = dotProduct(qa, ab);194double t[3];195int solutions = solveCubic(t, a, b, c, d);196197Vector2 epDir = direction(0);198double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A199param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);200{201epDir = direction(1);202double distance = (p[2]-origin).length(); // distance from B203if (distance < fabs(minDistance)) {204minDistance = nonZeroSign(crossProduct(epDir, p[2]-origin))*distance;205param = dotProduct(origin-p[1], epDir)/dotProduct(epDir, epDir);206}207}208for (int i = 0; i < solutions; ++i) {209if (t[i] > 0 && t[i] < 1) {210Point2 qe = qa+2*t[i]*ab+t[i]*t[i]*br;211double distance = qe.length();212if (distance <= fabs(minDistance)) {213minDistance = nonZeroSign(crossProduct(ab+t[i]*br, qe))*distance;214param = t[i];215}216}217}218219if (param >= 0 && param <= 1)220return SignedDistance(minDistance, 0);221if (param < .5)222return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));223else224return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[2]-origin).normalize())));225}226227SignedDistance CubicSegment::signedDistance(Point2 origin, double ¶m) const {228Vector2 qa = p[0]-origin;229Vector2 ab = p[1]-p[0];230Vector2 br = p[2]-p[1]-ab;231Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;232233Vector2 epDir = direction(0);234double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A235param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);236{237epDir = direction(1);238double distance = (p[3]-origin).length(); // distance from B239if (distance < fabs(minDistance)) {240minDistance = nonZeroSign(crossProduct(epDir, p[3]-origin))*distance;241param = dotProduct(epDir-(p[3]-origin), epDir)/dotProduct(epDir, epDir);242}243}244// Iterative minimum distance search245for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {246double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;247Vector2 qe = qa+3*t*ab+3*t*t*br+t*t*t*as;248for (int step = 0; step < MSDFGEN_CUBIC_SEARCH_STEPS; ++step) {249// Improve t250Vector2 d1 = 3*ab+6*t*br+3*t*t*as;251Vector2 d2 = 6*br+6*t*as;252t -= dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));253if (t <= 0 || t >= 1)254break;255qe = qa+3*t*ab+3*t*t*br+t*t*t*as;256double distance = qe.length();257if (distance < fabs(minDistance)) {258minDistance = nonZeroSign(crossProduct(d1, qe))*distance;259param = t;260}261}262}263264if (param >= 0 && param <= 1)265return SignedDistance(minDistance, 0);266if (param < .5)267return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));268else269return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[3]-origin).normalize())));270}271272int LinearSegment::scanlineIntersections(double x[3], int dy[3], double y) const {273if ((y >= p[0].y && y < p[1].y) || (y >= p[1].y && y < p[0].y)) {274double param = (y-p[0].y)/(p[1].y-p[0].y);275x[0] = mix(p[0].x, p[1].x, param);276dy[0] = sign(p[1].y-p[0].y);277return 1;278}279return 0;280}281282int QuadraticSegment::scanlineIntersections(double x[3], int dy[3], double y) const {283int total = 0;284int nextDY = y > p[0].y ? 1 : -1;285x[total] = p[0].x;286if (p[0].y == y) {287if (p[0].y < p[1].y || (p[0].y == p[1].y && p[0].y < p[2].y))288dy[total++] = 1;289else290nextDY = 1;291}292{293Vector2 ab = p[1]-p[0];294Vector2 br = p[2]-p[1]-ab;295double t[2];296int solutions = solveQuadratic(t, br.y, 2*ab.y, p[0].y-y);297// Sort solutions298double tmp;299if (solutions >= 2 && t[0] > t[1])300tmp = t[0], t[0] = t[1], t[1] = tmp;301for (int i = 0; i < solutions && total < 2; ++i) {302if (t[i] >= 0 && t[i] <= 1) {303x[total] = p[0].x+2*t[i]*ab.x+t[i]*t[i]*br.x;304if (nextDY*(ab.y+t[i]*br.y) >= 0) {305dy[total++] = nextDY;306nextDY = -nextDY;307}308}309}310}311if (p[2].y == y) {312if (nextDY > 0 && total > 0) {313--total;314nextDY = -1;315}316if ((p[2].y < p[1].y || (p[2].y == p[1].y && p[2].y < p[0].y)) && total < 2) {317x[total] = p[2].x;318if (nextDY < 0) {319dy[total++] = -1;320nextDY = 1;321}322}323}324if (nextDY != (y >= p[2].y ? 1 : -1)) {325if (total > 0)326--total;327else {328if (fabs(p[2].y-y) < fabs(p[0].y-y))329x[total] = p[2].x;330dy[total++] = nextDY;331}332}333return total;334}335336int CubicSegment::scanlineIntersections(double x[3], int dy[3], double y) const {337int total = 0;338int nextDY = y > p[0].y ? 1 : -1;339x[total] = p[0].x;340if (p[0].y == y) {341if (p[0].y < p[1].y || (p[0].y == p[1].y && (p[0].y < p[2].y || (p[0].y == p[2].y && p[0].y < p[3].y))))342dy[total++] = 1;343else344nextDY = 1;345}346{347Vector2 ab = p[1]-p[0];348Vector2 br = p[2]-p[1]-ab;349Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;350double t[3];351int solutions = solveCubic(t, as.y, 3*br.y, 3*ab.y, p[0].y-y);352// Sort solutions353double tmp;354if (solutions >= 2) {355if (t[0] > t[1])356tmp = t[0], t[0] = t[1], t[1] = tmp;357if (solutions >= 3 && t[1] > t[2]) {358tmp = t[1], t[1] = t[2], t[2] = tmp;359if (t[0] > t[1])360tmp = t[0], t[0] = t[1], t[1] = tmp;361}362}363for (int i = 0; i < solutions && total < 3; ++i) {364if (t[i] >= 0 && t[i] <= 1) {365x[total] = p[0].x+3*t[i]*ab.x+3*t[i]*t[i]*br.x+t[i]*t[i]*t[i]*as.x;366if (nextDY*(ab.y+2*t[i]*br.y+t[i]*t[i]*as.y) >= 0) {367dy[total++] = nextDY;368nextDY = -nextDY;369}370}371}372}373if (p[3].y == y) {374if (nextDY > 0 && total > 0) {375--total;376nextDY = -1;377}378if ((p[3].y < p[2].y || (p[3].y == p[2].y && (p[3].y < p[1].y || (p[3].y == p[1].y && p[3].y < p[0].y)))) && total < 3) {379x[total] = p[3].x;380if (nextDY < 0) {381dy[total++] = -1;382nextDY = 1;383}384}385}386if (nextDY != (y >= p[3].y ? 1 : -1)) {387if (total > 0)388--total;389else {390if (fabs(p[3].y-y) < fabs(p[0].y-y))391x[total] = p[3].x;392dy[total++] = nextDY;393}394}395return total;396}397398static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {399if (p.x < l) l = p.x;400if (p.y < b) b = p.y;401if (p.x > r) r = p.x;402if (p.y > t) t = p.y;403}404405void LinearSegment::bound(double &l, double &b, double &r, double &t) const {406pointBounds(p[0], l, b, r, t);407pointBounds(p[1], l, b, r, t);408}409410void QuadraticSegment::bound(double &l, double &b, double &r, double &t) const {411pointBounds(p[0], l, b, r, t);412pointBounds(p[2], l, b, r, t);413Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);414if (bot.x) {415double param = (p[1].x-p[0].x)/bot.x;416if (param > 0 && param < 1)417pointBounds(point(param), l, b, r, t);418}419if (bot.y) {420double param = (p[1].y-p[0].y)/bot.y;421if (param > 0 && param < 1)422pointBounds(point(param), l, b, r, t);423}424}425426void CubicSegment::bound(double &l, double &b, double &r, double &t) const {427pointBounds(p[0], l, b, r, t);428pointBounds(p[3], l, b, r, t);429Vector2 a0 = p[1]-p[0];430Vector2 a1 = 2*(p[2]-p[1]-a0);431Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];432double params[2];433int solutions;434solutions = solveQuadratic(params, a2.x, a1.x, a0.x);435for (int i = 0; i < solutions; ++i)436if (params[i] > 0 && params[i] < 1)437pointBounds(point(params[i]), l, b, r, t);438solutions = solveQuadratic(params, a2.y, a1.y, a0.y);439for (int i = 0; i < solutions; ++i)440if (params[i] > 0 && params[i] < 1)441pointBounds(point(params[i]), l, b, r, t);442}443444void LinearSegment::reverse() {445Point2 tmp = p[0];446p[0] = p[1];447p[1] = tmp;448}449450void QuadraticSegment::reverse() {451Point2 tmp = p[0];452p[0] = p[2];453p[2] = tmp;454}455456void CubicSegment::reverse() {457Point2 tmp = p[0];458p[0] = p[3];459p[3] = tmp;460tmp = p[1];461p[1] = p[2];462p[2] = tmp;463}464465void LinearSegment::moveStartPoint(Point2 to) {466p[0] = to;467}468469void QuadraticSegment::moveStartPoint(Point2 to) {470Vector2 origSDir = p[0]-p[1];471Point2 origP1 = p[1];472p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);473p[0] = to;474if (dotProduct(origSDir, p[0]-p[1]) < 0)475p[1] = origP1;476}477478void CubicSegment::moveStartPoint(Point2 to) {479p[1] += to-p[0];480p[0] = to;481}482483void LinearSegment::moveEndPoint(Point2 to) {484p[1] = to;485}486487void QuadraticSegment::moveEndPoint(Point2 to) {488Vector2 origEDir = p[2]-p[1];489Point2 origP1 = p[1];490p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);491p[2] = to;492if (dotProduct(origEDir, p[2]-p[1]) < 0)493p[1] = origP1;494}495496void CubicSegment::moveEndPoint(Point2 to) {497p[2] += to-p[3];498p[3] = to;499}500501void LinearSegment::splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const {502part0 = new LinearSegment(p[0], point(1/3.), color);503part1 = new LinearSegment(point(1/3.), point(2/3.), color);504part2 = new LinearSegment(point(2/3.), p[1], color);505}506507void QuadraticSegment::splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const {508part0 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);509part1 = new QuadraticSegment(point(1/3.), mix(mix(p[0], p[1], 5/9.), mix(p[1], p[2], 4/9.), .5), point(2/3.), color);510part2 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);511}512513void CubicSegment::splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const {514part0 = new CubicSegment(p[0], p[0] == p[1] ? p[0] : mix(p[0], p[1], 1/3.), mix(mix(p[0], p[1], 1/3.), mix(p[1], p[2], 1/3.), 1/3.), point(1/3.), color);515part1 = new CubicSegment(point(1/3.),516mix(mix(mix(p[0], p[1], 1/3.), mix(p[1], p[2], 1/3.), 1/3.), mix(mix(p[1], p[2], 1/3.), mix(p[2], p[3], 1/3.), 1/3.), 2/3.),517mix(mix(mix(p[0], p[1], 2/3.), mix(p[1], p[2], 2/3.), 2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), 1/3.),518point(2/3.), color);519part2 = new CubicSegment(point(2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), p[2] == p[3] ? p[3] : mix(p[2], p[3], 2/3.), p[3], color);520}521522EdgeSegment *QuadraticSegment::convertToCubic() const {523return new CubicSegment(p[0], mix(p[0], p[1], 2/3.), mix(p[1], p[2], 1/3.), p[2], color);524}525526}527528529