All files / domain ShakeTrigger.res.mjs

98.11% Statements 52/53
89.18% Branches 33/37
90% Functions 9/10
98.11% Lines 52/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x                   1x   1x                                   41x                 41x 95x 49x   46x           8x 1x   7x         7x 4x   3x         62x 62x 6x                               56x               62x 62x 62x 62x 62x 62x 62x 8x                           54x 62x       62x 62x 62x 62x 62x 62x 13x                     41x           95x 41x 41x 41x               62x                         2x 2x 2x 2x 2x 2x 2x             2x 2x 1x               1x                      
// Generated by ReScript, PLEASE EDIT WITH CARE
 
import * as Belt_Array from "@rescript/runtime/lib/es6/Belt_Array.js";
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
import * as Quaternion$WsteinRegripCore from "./Quaternion.res.mjs";
 
let defaults = {
  minStepAngleDeg: 10,
  minSteps: 4,
  minReversals: 2,
  maxSampleGapMs: 250,
  burstWindowMs: 300,
  faceGuardMs: 400,
  cooldownMs: 500
};
 
let initial_rapidSteps = [];
 
let initial = {
  previous: undefined,
  rapidSteps: initial_rapidSteps,
  pending: undefined,
  lastMoveAt: undefined,
  lastDetectionAt: undefined
};
 
function negate(q) {
  return {
    x: - q.x,
    y: - q.y,
    z: - q.z,
    w: - q.w
  };
}
 
function difference(a, b) {
  return {
    x: a.x - b.x,
    y: a.y - b.y,
    z: a.z - b.z,
    w: a.w - b.w
  };
}
 
function countReversals(steps) {
  return Stdlib_Array.reduceWithIndex(steps, 0, (total, step, index) => {
    if (index === 0 || Quaternion$WsteinRegripCore.dot(steps[index - 1 | 0].direction, step.direction) >= 0) {
      return total;
    } else {
      return total + 1 | 0;
    }
  });
}
 
function elapsedAtMost(reference, now, window) {
  if (reference !== undefined) {
    return now - reference <= window;
  } else {
    return false;
  }
}
 
function elapsedLessThan(reference, now, window) {
  if (reference !== undefined) {
    return now - reference < window;
  } else {
    return false;
  }
}
 
function releaseMature(state, now, config) {
  let candidate = state.pending;
  if (candidate !== undefined && now - candidate.at >= config.faceGuardMs) {
    return [
      {
        previous: state.previous,
        rapidSteps: state.rapidSteps,
        pending: undefined,
        lastMoveAt: state.lastMoveAt,
        lastDetectionAt: candidate.at
      },
      {
        at: candidate.at,
        steps: candidate.steps,
        reversals: candidate.reversals,
        spanMs: candidate.spanMs
      }
    ];
  } else {
    return [
      state,
      undefined
    ];
  }
}
 
function observe(state, at, orientation, configOpt) {
  let config = configOpt !== undefined ? configOpt : defaults;
  let match = releaseMature(state, at, config);
  let released = match[1];
  let state$1 = match[0];
  let normalized = Quaternion$WsteinRegripCore.normalize(orientation);
  let before = state$1.previous;
  if (before === undefined) {
    return [
      {
        previous: {
          at: at,
          pose: normalized
        },
        rapidSteps: state$1.rapidSteps,
        pending: state$1.pending,
        lastMoveAt: state$1.lastMoveAt,
        lastDetectionAt: state$1.lastDetectionAt
      },
      released
    ];
  }
  let current = Quaternion$WsteinRegripCore.dot(before.pose, normalized) < 0 ? negate(normalized) : normalized;
  let state_previous = {
    at: at,
    pose: current
  };
  let state_rapidSteps = state$1.rapidSteps;
  let state_pending = state$1.pending;
  let state_lastMoveAt = state$1.lastMoveAt;
  let state_lastDetectionAt = state$1.lastDetectionAt;
  let interval = at - before.at;
  if (interval <= 0 || interval > config.maxSampleGapMs || Quaternion$WsteinRegripCore.angle(before.pose, current) < Quaternion$WsteinRegripCore.degreesToRadians(config.minStepAngleDeg)) {
    return [
      {
        previous: state_previous,
        rapidSteps: [],
        pending: state_pending,
        lastMoveAt: state_lastMoveAt,
        lastDetectionAt: state_lastDetectionAt
      },
      released
    ];
  }
  let rapidSteps = Belt_Array.concatMany([
    state_rapidSteps,
    [{
        at: at,
        direction: difference(current, before.pose)
      }]
  ]).filter(step => at - step.at <= config.burstWindowMs);
  let reversals = countReversals(rapidSteps);
  let match$1 = state_pending;
  let pending = match$1 !== undefined ? state_pending : (
      rapidSteps.length >= config.minSteps && reversals >= config.minReversals && !elapsedAtMost(state_lastMoveAt, at, config.faceGuardMs) && !elapsedLessThan(state_lastDetectionAt, at, config.cooldownMs) ? ({
          at: at,
          steps: rapidSteps.length,
          reversals: reversals,
          spanMs: at - rapidSteps[0].at
        }) : undefined
    );
  return [
    {
      previous: state_previous,
      rapidSteps: rapidSteps,
      pending: pending,
      lastMoveAt: state_lastMoveAt,
      lastDetectionAt: state_lastDetectionAt
    },
    released
  ];
}
 
function observeMove(state, at, configOpt) {
  let config = configOpt !== undefined ? configOpt : defaults;
  let state_previous = state.previous;
  let state_rapidSteps = state.rapidSteps;
  let state_pending = state.pending;
  let state_lastMoveAt = at;
  let state_lastDetectionAt = state.lastDetectionAt;
  let state$1 = {
    previous: state_previous,
    rapidSteps: state_rapidSteps,
    pending: state_pending,
    lastMoveAt: state_lastMoveAt,
    lastDetectionAt: state_lastDetectionAt
  };
  let candidate = state_pending;
  if (candidate !== undefined && Math.abs(at - candidate.at) <= config.faceGuardMs) {
    return {
      previous: state_previous,
      rapidSteps: state_rapidSteps,
      pending: undefined,
      lastMoveAt: state_lastMoveAt,
      lastDetectionAt: state_lastDetectionAt
    };
  } else {
    return state$1;
  }
}
 
export {
  defaults,
  initial,
  observe,
  observeMove,
}
/* No side effect */