modify reduce path

This commit is contained in:
kekee000 2015-01-04 17:57:47 +08:00
parent 94c03efcae
commit b4e7f7b663
5 changed files with 87 additions and 76 deletions

View File

@ -13,11 +13,11 @@ define(
var isPathCross = require('../isPathCross');
var isInsidePath = require('../isInsidePath');
var isPathOverlap = require('../isPathOverlap');
var reducePath = require('../reducePath');
var getBezierQ2Point = require('math/getBezierQ2Point');
var Relation = require('./relation');
var util = require('../pathUtil');
var interpolate = util.interpolate;
var removeLinePoint = util.removeLinePoint;
var pathSplit = require('./pathSplit');
var pathCombine = require('./pathCombine');
var getVirtualJoint = require('./getVirtualJoint');
@ -72,8 +72,8 @@ define(
// 这里对路径进行插值,以便于求交运算
var newPath0 = interpolate(removeLinePoint(path0));
var newPath1 = interpolate(removeLinePoint(path1));
var newPath0 = interpolate(reducePath(path0));
var newPath1 = interpolate(reducePath(path1));
var joint = isPathCross(newPath0, newPath1);

View File

@ -62,7 +62,7 @@ define(
* @param {string} indexProp 索引号
* @return {Array} 分割后的路径集合
*/
function pathSplit (path, joint) {
function pathSplit(path, joint) {
joint = sortJoint(path, joint);
var jointOffset = 0; // 用来标记插入点产生的偏移

View File

@ -64,38 +64,6 @@ define(
}
/**
* 移除路径中三点共线中的多余点
*
* @param {Array} path 路径
* @return {Array} 路径
*/
function removeLinePoint(path) {
var newPath = [];
var count = 0;
for (var i = 0, l = path.length; i < l; i++) {
var next = i === l - 1 ? 0 : i + 1;
var prev = (i - count) === 0 ? l - 1 : i - count - 1;
// 三点共线的情况
if (
path[prev].onCurve && path[i].onCurve && path[next].onCurve
&& Math.abs(
(path[i].y - path[prev].y) * (path[i].x - path[next].x)
- (path[i].y - path[next].y) * (path[i].x - path[prev].x)
) <= 0.001
) {
count++;
continue;
}
newPath.push(path[i]);
}
return newPath;
}
/**
* 判断路径的方向是否顺时针
* see:
@ -180,7 +148,6 @@ define(
return {
interpolate: interpolate,
deInterpolate: deInterpolate,
removeLinePoint: removeLinePoint,
isClockWise: isClockWise,
removeOverlapPoints: removeOverlapPoints,
getPointHash: getPointHash,

View File

@ -0,0 +1,77 @@
/**
* @file reducePath.js
* @author mengke01
* @date
* @description
* 缩减glyf大小去除冗余节点
*/
define(
function (require) {
/**
* 判断点是否多余的点
*
* @param {Object} prev 上一个
* @param {Object} p 当前
* @param {Object} next 下一个
* @return {boolean}
*/
function redundant(prev, p, next) {
// 是否重合的点, 只有两个点同在曲线上或者同不在曲线上移出
if (
(p.onCurve && next.onCurve || !p.onCurve && !next.onCurve)
&& Math.pow(p.x - next.x, 2) + Math.pow(p.y - next.y, 2) <= 1
) {
return true;
}
// 三点同线 仅处理三个在直线上的点
if (
(p.onCurve && prev.onCurve && next.onCurve)
&& Math.abs((next.y - p.y) * (prev.x - p.x) - (prev.y - p.y) * (next.x - p.x)) <= 0.001
) {
return true;
}
return false;
}
/**
* 缩减glyf去除冗余节点
*
* @param {Array} contour 路径对象
* @return {Array} 路径对象
*/
function reducePath(contour) {
if (!contour.length) {
return contour;
}
var prev;
var next;
var p;
for (var i = contour.length - 1, last = i; i >= 0; i--) {
// 这里注意逆序
p = contour[i];
next = i === last ? contour[0] : contour[i + 1];
prev = i === 0 ? contour[last] : contour[i - 1];
if (redundant(prev, p, next)) {
// console.log('reduce path ...');
contour.splice(i, 1);
last--;
continue;
}
}
return contour;
}
return reducePath;
}
);

View File

@ -10,28 +10,7 @@
define(
function (require) {
/**
* 判断点是否多余的点
*
* @param {Object} p 当前
* @param {Object} prev 上一个
* @param {Object} next 下一个
* @return {boolean}
*/
function redundant(p, prev, next) {
// 是否重合的点, 只有两个点同在曲线上或者同不在曲线上移出
if (Math.pow(p.x - next.x, 2) + Math.pow(p.y - next.y, 2) <= 1) {
return !p.onCurve ^ !!next.onCurve;
}
// 三点同线
// else if ((next.y - p.y) * (prev.x - p.x) == (prev.y - p.y) * (next.x - p.x)) {
// return p.onCurve && prev.onCurve && next.onCurve;
// }
return false;
}
var reducePath = require('graphics/reducePath');
/**
* 缩减glyf去除冗余节点
@ -43,29 +22,17 @@ define(
var contours = glyf.contours;
var contour;
var prev;
var next;
var length;
for (var j = contours.length - 1; j >= 0; j--) {
contour = contours[j];
reducePath(contour);
length = contour.length;
// 空轮廓
if (0 === contour.length) {
if (0 === length || 2 === length) {
contours.splice(j, 1);
continue;
}
for (var i = contour.length - 1, last = i; i >= 0; i--) {
// 这里注意逆序
next = i === last ? contour[0] : contour[i + 1];
prev = i === 0 ? contour[last] : contour[i - 1];
var p = contour[i];
if (redundant(p, prev, next)) {
contour.splice(i, 1);
last--;
continue;
}
}
}
if (0 === glyf.contours.length) {