/
helpers.segment.js
320 lines (276 loc) · 8.67 KB
/
helpers.segment.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import {_angleBetween, _angleDiff, _normalizeAngle} from './helpers.math';
/**
* @typedef { import("../elements/element.line").default } LineElement
* @typedef { import("../elements/element.point").default } PointElement
* @typedef {{start: number, end: number, loop: boolean, style?: any}} Segment
*/
function propertyFn(property) {
if (property === 'angle') {
return {
between: _angleBetween,
compare: _angleDiff,
normalize: _normalizeAngle,
};
}
return {
between: (n, s, e) => n >= Math.min(s, e) && n <= Math.max(e, s),
compare: (a, b) => a - b,
normalize: x => x
};
}
function normalizeSegment({start, end, count, loop, style}) {
return {
start: start % count,
end: end % count,
loop: loop && (end - start + 1) % count === 0,
style
};
}
function getSegment(segment, points, bounds) {
const {property, start: startBound, end: endBound} = bounds;
const {between, normalize} = propertyFn(property);
const count = points.length;
// eslint-disable-next-line prefer-const
let {start, end, loop} = segment;
let i, ilen;
if (loop) {
start += count;
end += count;
for (i = 0, ilen = count; i < ilen; ++i) {
if (!between(normalize(points[start % count][property]), startBound, endBound)) {
break;
}
start--;
end--;
}
start %= count;
end %= count;
}
if (end < start) {
end += count;
}
return {start, end, loop, style: segment.style};
}
/**
* Returns the sub-segment(s) of a line segment that fall in the given bounds
* @param {object} segment
* @param {number} segment.start - start index of the segment, referring the points array
* @param {number} segment.end - end index of the segment, referring the points array
* @param {boolean} segment.loop - indicates that the segment is a loop
* @param {object} [segment.style] - segment style
* @param {PointElement[]} points - the points that this segment refers to
* @param {object} [bounds]
* @param {string} bounds.property - the property of a `PointElement` we are bounding. `x`, `y` or `angle`.
* @param {number} bounds.start - start value of the property
* @param {number} bounds.end - end value of the property
* @private
**/
export function _boundSegment(segment, points, bounds) {
if (!bounds) {
return [segment];
}
const {property, start: startBound, end: endBound} = bounds;
const count = points.length;
const {compare, between, normalize} = propertyFn(property);
const {start, end, loop, style} = getSegment(segment, points, bounds);
const result = [];
let inside = false;
let subStart = null;
let value, point, prevValue;
const startIsBefore = () => between(startBound, prevValue, value) && compare(startBound, prevValue) !== 0;
const endIsBefore = () => compare(endBound, value) === 0 || between(endBound, prevValue, value);
const shouldStart = () => inside || startIsBefore();
const shouldStop = () => !inside || endIsBefore();
for (let i = start, prev = start; i <= end; ++i) {
point = points[i % count];
if (point.skip) {
continue;
}
value = normalize(point[property]);
if (value === prevValue) {
continue;
}
inside = between(value, startBound, endBound);
if (subStart === null && shouldStart()) {
subStart = compare(value, startBound) === 0 ? i : prev;
}
if (subStart !== null && shouldStop()) {
result.push(normalizeSegment({start: subStart, end: i, loop, count, style}));
subStart = null;
}
prev = i;
prevValue = value;
}
if (subStart !== null) {
result.push(normalizeSegment({start: subStart, end, loop, count, style}));
}
return result;
}
/**
* Returns the segments of the line that are inside given bounds
* @param {LineElement} line
* @param {object} [bounds]
* @param {string} bounds.property - the property we are bounding with. `x`, `y` or `angle`.
* @param {number} bounds.start - start value of the `property`
* @param {number} bounds.end - end value of the `property`
* @private
*/
export function _boundSegments(line, bounds) {
const result = [];
const segments = line.segments;
for (let i = 0; i < segments.length; i++) {
const sub = _boundSegment(segments[i], line.points, bounds);
if (sub.length) {
result.push(...sub);
}
}
return result;
}
/**
* Find start and end index of a line.
*/
function findStartAndEnd(points, count, loop, spanGaps) {
let start = 0;
let end = count - 1;
if (loop && !spanGaps) {
// loop and not spaning gaps, first find a gap to start from
while (start < count && !points[start].skip) {
start++;
}
}
// find first non skipped point (after the first gap possibly)
while (start < count && points[start].skip) {
start++;
}
// if we looped to count, start needs to be 0
start %= count;
if (loop) {
// loop will go past count, if start > 0
end += start;
}
while (end > start && points[end % count].skip) {
end--;
}
// end could be more than count, normalize
end %= count;
return {start, end};
}
/**
* Compute solid segments from Points, when spanGaps === false
* @param {PointElement[]} points - the points
* @param {number} start - start index
* @param {number} max - max index (can go past count on a loop)
* @param {boolean} loop - boolean indicating that this would be a loop if no gaps are found
*/
function solidSegments(points, start, max, loop) {
const count = points.length;
const result = [];
let last = start;
let prev = points[start];
let end;
for (end = start + 1; end <= max; ++end) {
const cur = points[end % count];
if (cur.skip || cur.stop) {
if (!prev.skip) {
loop = false;
result.push({start: start % count, end: (end - 1) % count, loop});
// @ts-ignore
start = last = cur.stop ? end : null;
}
} else {
last = end;
if (prev.skip) {
start = end;
}
}
prev = cur;
}
if (last !== null) {
result.push({start: start % count, end: last % count, loop});
}
return result;
}
/**
* Compute the continuous segments that define the whole line
* There can be skipped points within a segment, if spanGaps is true.
* @param {LineElement} line
* @param {object} [segmentOptions]
* @return {Segment[]}
* @private
*/
export function _computeSegments(line, segmentOptions) {
const points = line.points;
const spanGaps = line.options.spanGaps;
const count = points.length;
if (!count) {
return [];
}
const loop = !!line._loop;
const {start, end} = findStartAndEnd(points, count, loop, spanGaps);
const baseStyle = readStyle(line.options);
if (spanGaps === true) {
return splitByStyles([{start, end, loop}], points, baseStyle, segmentOptions);
}
const max = end < start ? end + count : end;
const completeLoop = !!line._fullLoop && start === 0 && end === count - 1;
return splitByStyles(solidSegments(points, start, max, completeLoop), points, baseStyle, segmentOptions);
}
/**
* @param {Segment[]} segments
* @param {PointElement[]} points
* @param {object} [segmentOptions]
* @return {Segment[]}
*/
function splitByStyles(segments, points, baseStyle, segmentOptions) {
if (!segmentOptions || !segmentOptions.setContext || !points) {
return segments;
}
return doSplitByStyles(segments, points, baseStyle, segmentOptions);
}
/**
* @param {Segment[]} segments
* @param {PointElement[]} points
* @param {object} [segmentOptions]
* @return {Segment[]}
*/
function doSplitByStyles(segments, points, baseStyle, segmentOptions) {
const count = points.length;
const result = [];
let start = segments[0].start;
let i = start;
for (const segment of segments) {
let prevStyle = baseStyle;
let prev = points[start % count];
let style;
for (i = start + 1; i <= segment.end; i++) {
const pt = points[i % count];
style = readStyle(segmentOptions.setContext({type: 'segment', p0: prev, p1: pt}));
if (styleChanged(style, prevStyle)) {
result.push({start: start, end: i - 1, loop: segment.loop, style: prevStyle});
prevStyle = style;
start = i - 1;
}
prev = pt;
prevStyle = style;
}
if (start < i - 1) {
result.push({start, end: i - 1, loop: segment.loop, style});
start = i - 1;
}
}
return result;
}
function readStyle(options) {
return {
backgroundColor: options.backgroundColor,
borderCapStyle: options.borderCapStyle,
borderDash: options.borderDash,
borderDashOffset: options.borderDashOffset,
borderJoinStyle: options.borderJoinStyle,
borderWidth: options.borderWidth,
borderColor: options.borderColor
};
}
function styleChanged(style, prevStyle) {
return prevStyle && JSON.stringify(style) !== JSON.stringify(prevStyle);
}