|  | 
|  | 1 | +import intersect from '../intersect.js'; | 
|  | 2 | +import intersect2, { indexPath } from '../indexing.js'; | 
|  | 3 | + | 
|  | 4 | +/** | 
|  | 5 | + * Needs to be implemented, probably a simple nested grid or a quad tree will do | 
|  | 6 | + * @typedef {import('../indexing.js').SpatialIndex} Interface | 
|  | 7 | + * @type {import('../indexing.js').SpatialIndex} | 
|  | 8 | + * @implements {Interface} | 
|  | 9 | + */ | 
|  | 10 | +class SpatialIndex { | 
|  | 11 | +  /** | 
|  | 12 | +   * | 
|  | 13 | +   * @param {number} pathId | 
|  | 14 | +   * @param {number} curveIndex | 
|  | 15 | +   * @param {import('../intersect.js').PathComponent} curve | 
|  | 16 | +   * @param {import('../indexing.js').BBox} bbox | 
|  | 17 | +   */ | 
|  | 18 | +  add(pathId, curveIndex, curve, bbox) { | 
|  | 19 | +    throw new Error('Method not implemented.'); | 
|  | 20 | +  } | 
|  | 21 | + | 
|  | 22 | +  /** | 
|  | 23 | +   * | 
|  | 24 | +   * @param {number} pathId | 
|  | 25 | +   */ | 
|  | 26 | +  remove(pathId) { | 
|  | 27 | +    throw new Error('Method not implemented.'); | 
|  | 28 | +  } | 
|  | 29 | + | 
|  | 30 | +  /** | 
|  | 31 | +   * | 
|  | 32 | +   * @param {number[]} pathIds | 
|  | 33 | +   */ | 
|  | 34 | +  intersect(pathIds) { | 
|  | 35 | +    throw new Error('Method not implemented.'); | 
|  | 36 | +    return []; | 
|  | 37 | +  } | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +/** | 
|  | 41 | + * | 
|  | 42 | + * @param {number} n | 
|  | 43 | + * @returns {import('../intersect.js').PathComponent[]} | 
|  | 44 | + */ | 
|  | 45 | +const createPath = (n) => { | 
|  | 46 | +  /** | 
|  | 47 | +   * | 
|  | 48 | +   * @param {'M'|'L'} cmd | 
|  | 49 | +   * @returns {import('../intersect.js').PathComponent} | 
|  | 50 | +   */ | 
|  | 51 | +  const cmd = (cmd) => [ | 
|  | 52 | +    cmd, | 
|  | 53 | +    Math.round(Math.random() * 800), | 
|  | 54 | +    Math.round(Math.random() * 800), | 
|  | 55 | +  ]; | 
|  | 56 | +  return [cmd('M')].concat(new Array(n).fill(0).map(() => cmd('L'))); | 
|  | 57 | +}; | 
|  | 58 | + | 
|  | 59 | +const a = createPath(5000); | 
|  | 60 | +const b = createPath(5000); | 
|  | 61 | + | 
|  | 62 | +const index = new SpatialIndex(); | 
|  | 63 | + | 
|  | 64 | +// when | 
|  | 65 | + | 
|  | 66 | +performance.mark('total'); | 
|  | 67 | +performance.mark('index'); | 
|  | 68 | +const id1 = indexPath(a, index); | 
|  | 69 | +const id2 = indexPath(b, index); | 
|  | 70 | +const mark0 = performance.measure( | 
|  | 71 | +  'indexing', | 
|  | 72 | +  { detail: { ids: [id1, id2] } }, | 
|  | 73 | +  'index' | 
|  | 74 | +); | 
|  | 75 | +performance.mark('intersect2'); | 
|  | 76 | +const intersections = intersect2([id1, id2], index).length; | 
|  | 77 | +const mark1 = performance.measure( | 
|  | 78 | +  'intersect', | 
|  | 79 | +  { detail: { intersections } }, | 
|  | 80 | +  'intersect2' | 
|  | 81 | +); | 
|  | 82 | +const mark = performance.measure('intersect2 total', 'total'); | 
|  | 83 | + | 
|  | 84 | +console.log(mark0.toJSON(), mark1.toJSON(), mark.toJSON()); | 
|  | 85 | + | 
|  | 86 | +performance.mark('intersect'); | 
|  | 87 | +const baseline = intersect(a, b, true); | 
|  | 88 | +const baselineMark = performance.measure( | 
|  | 89 | +  'baseline', | 
|  | 90 | +  { detail: { intersections: baseline } }, | 
|  | 91 | +  'intersect' | 
|  | 92 | +); | 
|  | 93 | + | 
|  | 94 | +console.log(baselineMark.toJSON()); | 
0 commit comments