From cf813a58c8b0e6e505eccc6316ce6256c5c85ef6 Mon Sep 17 00:00:00 2001 From: Pitchaya Boonsarngsuk <2285135b@student.gla.ac.uk> Date: Mon, 5 Feb 2018 16:33:46 +0000 Subject: [PATCH] Neighbour change api --- examples/js/src/example-papaparsing/hybrid.js | 1 + .../src/example-papaparsing/neighbourSampling.js | 2 +- src/hybridSimulation.js | 8 +++----- src/neighbourSampling.js | 14 ++++++++------ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/js/src/example-papaparsing/hybrid.js b/examples/js/src/example-papaparsing/hybrid.js index 551ae1f..57d2db5 100644 --- a/examples/js/src/example-papaparsing/hybrid.js +++ b/examples/js/src/example-papaparsing/hybrid.js @@ -16,6 +16,7 @@ function startHybridSimulation() { let forceSample = d3.forceNeighbourSampling() .neighbourSize(NEIGHBOUR_SIZE) .sampleSize(SAMPLE_SIZE) + .stableVelocity(0) .distance(distance) let forceFull = d3.forceNeighbourSampling() diff --git a/examples/js/src/example-papaparsing/neighbourSampling.js b/examples/js/src/example-papaparsing/neighbourSampling.js index 40f8603..ee11d4e 100644 --- a/examples/js/src/example-papaparsing/neighbourSampling.js +++ b/examples/js/src/example-papaparsing/neighbourSampling.js @@ -16,7 +16,7 @@ function startNeighbourSamplingSimulation() { return distanceFunction(s, t, props, norm); }) .stableVelocity(0.000001) //TODO - .stableVeloHandler(ended); + .onStableVelo(ended); simulation .alphaDecay(0) diff --git a/src/hybridSimulation.js b/src/hybridSimulation.js index 524cfad..260efd3 100644 --- a/src/hybridSimulation.js +++ b/src/hybridSimulation.js @@ -15,7 +15,7 @@ import {takeSampleFrom} from "./interpolation/helpers"; * clean up the model. * @param {object} sim - D3 Simulation object * @param {object} forceS - Pre-configured D3 force object for the sample set. - The ending condition will be re-configured. + The ending handler will be re-configured. Neighbour sampling force is expected, but other D3 forces may also work. * @param {object} forceF - Pre-configured D3 force object for the simultion ran @@ -60,10 +60,8 @@ export default function (sim, forceS, forceF) { } function initForces(){ - if (forceSample.stableVelocity && forceSample.stableVeloHandler) { - forceSample - .stableVelocity(0) //TODO - .stableVeloHandler(sampleEnded); + if (forceSample.onStableVelo) { + forceSample.onStableVelo(sampleEnded); } // Set default value for interpDistanceFn if not been specified yet diff --git a/src/neighbourSampling.js b/src/neighbourSampling.js index 7445a10..0cc134a 100644 --- a/src/neighbourSampling.js +++ b/src/neighbourSampling.js @@ -20,7 +20,8 @@ export default function () { sampleSize = 10, stableVelocity = 0, stableVeloHandler = null, - dataSizeFactor; + dataSizeFactor, + latestVelocityDiff = 0; /** * Apply spring forces at each simulation iteration. @@ -29,7 +30,7 @@ export default function () { function force(alpha) { let n = nodes.length; // Cache old velocity for comparison later - if (stableVeloHandler!==null && stableVelocity!=0) { + if (stableVeloHandler!==null && stableVelocity>=0) { for (let i = n-1, node; i>=0; i--) { node = nodes[i]; node.oldvx = node.vx; @@ -53,13 +54,14 @@ export default function () { } // Calculate velocity changes, aka force applied. - if (stableVeloHandler!==null && stableVelocity!=0) { + if (stableVeloHandler!==null && stableVelocity>=0) { let velocityDiff = 0; for (let i = n-1, node; i>=0; i--) { node = nodes[i]; velocityDiff += Math.abs(Math.hypot(node.vx-node.oldvx, node.vy-node.oldvy)); } velocityDiff /= n*(neighbourSize+sampleSize); + latestVelocityDiff = velocityDiff; if(stableVeloHandler!==null && velocityDiff