File size: 6,121 Bytes
ae4627d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var BADNUM = require('../../constants/numerical').BADNUM;
module.exports = function linePoints(d, opts) {
var xa = opts.xaxis,
ya = opts.yaxis,
simplify = opts.simplify,
connectGaps = opts.connectGaps,
baseTolerance = opts.baseTolerance,
linear = opts.linear,
segments = [],
minTolerance = 0.2, // fraction of tolerance "so close we don't even consider it a new point"
pts = new Array(d.length),
pti = 0,
i,
// pt variables are pixel coordinates [x,y] of one point
clusterStartPt, // these four are the outputs of clustering on a line
clusterEndPt,
clusterHighPt,
clusterLowPt,
thisPt, // "this" is the next point we're considering adding to the cluster
clusterRefDist,
clusterHighFirst, // did we encounter the high point first, then a low point, or vice versa?
clusterUnitVector, // the first two points in the cluster determine its unit vector
// so the second is always in the "High" direction
thisVector, // the pixel delta from clusterStartPt
// val variables are (signed) pixel distances along the cluster vector
clusterHighVal,
clusterLowVal,
thisVal,
// deviation variables are (signed) pixel distances normal to the cluster vector
clusterMinDeviation,
clusterMaxDeviation,
thisDeviation;
if(!simplify) {
baseTolerance = minTolerance = -1;
}
// turn one calcdata point into pixel coordinates
function getPt(index) {
var x = xa.c2p(d[index].x),
y = ya.c2p(d[index].y);
if(x === BADNUM || y === BADNUM) return false;
return [x, y];
}
// if we're off-screen, increase tolerance over baseTolerance
function getTolerance(pt) {
var xFrac = pt[0] / xa._length,
yFrac = pt[1] / ya._length;
return (1 + 10 * Math.max(0, -xFrac, xFrac - 1, -yFrac, yFrac - 1)) * baseTolerance;
}
function ptDist(pt1, pt2) {
var dx = pt1[0] - pt2[0],
dy = pt1[1] - pt2[1];
return Math.sqrt(dx * dx + dy * dy);
}
// loop over ALL points in this trace
for(i = 0; i < d.length; i++) {
clusterStartPt = getPt(i);
if(!clusterStartPt) continue;
pti = 0;
pts[pti++] = clusterStartPt;
// loop over one segment of the trace
for(i++; i < d.length; i++) {
clusterHighPt = getPt(i);
if(!clusterHighPt) {
if(connectGaps) continue;
else break;
}
// can't decimate if nonlinear line shape
// TODO: we *could* decimate [hv]{2,3} shapes if we restricted clusters to horz or vert again
// but spline would be verrry awkward to decimate
if(!linear) {
pts[pti++] = clusterHighPt;
continue;
}
clusterRefDist = ptDist(clusterHighPt, clusterStartPt);
if(clusterRefDist < getTolerance(clusterHighPt) * minTolerance) continue;
clusterUnitVector = [
(clusterHighPt[0] - clusterStartPt[0]) / clusterRefDist,
(clusterHighPt[1] - clusterStartPt[1]) / clusterRefDist
];
clusterLowPt = clusterStartPt;
clusterHighVal = clusterRefDist;
clusterLowVal = clusterMinDeviation = clusterMaxDeviation = 0;
clusterHighFirst = false;
clusterEndPt = clusterHighPt;
// loop over one cluster of points that collapse onto one line
for(i++; i < d.length; i++) {
thisPt = getPt(i);
if(!thisPt) {
if(connectGaps) continue;
else break;
}
thisVector = [
thisPt[0] - clusterStartPt[0],
thisPt[1] - clusterStartPt[1]
];
// cross product (or dot with normal to the cluster vector)
thisDeviation = thisVector[0] * clusterUnitVector[1] - thisVector[1] * clusterUnitVector[0];
clusterMinDeviation = Math.min(clusterMinDeviation, thisDeviation);
clusterMaxDeviation = Math.max(clusterMaxDeviation, thisDeviation);
if(clusterMaxDeviation - clusterMinDeviation > getTolerance(thisPt)) break;
clusterEndPt = thisPt;
thisVal = thisVector[0] * clusterUnitVector[0] + thisVector[1] * clusterUnitVector[1];
if(thisVal > clusterHighVal) {
clusterHighVal = thisVal;
clusterHighPt = thisPt;
clusterHighFirst = false;
} else if(thisVal < clusterLowVal) {
clusterLowVal = thisVal;
clusterLowPt = thisPt;
clusterHighFirst = true;
}
}
// insert this cluster into pts
// we've already inserted the start pt, now check if we have high and low pts
if(clusterHighFirst) {
pts[pti++] = clusterHighPt;
if(clusterEndPt !== clusterLowPt) pts[pti++] = clusterLowPt;
} else {
if(clusterLowPt !== clusterStartPt) pts[pti++] = clusterLowPt;
if(clusterEndPt !== clusterHighPt) pts[pti++] = clusterHighPt;
}
// and finally insert the end pt
pts[pti++] = clusterEndPt;
// have we reached the end of this segment?
if(i >= d.length || !thisPt) break;
// otherwise we have an out-of-cluster point to insert as next clusterStartPt
pts[pti++] = thisPt;
clusterStartPt = thisPt;
}
segments.push(pts.slice(0, pti));
}
return segments;
};
|