|
| 1 | +/** |
| 2 | + * coord-systems.test.js — Unit tests for parseTranslation and parseDeviceConfigCoords. |
| 3 | + * |
| 4 | + * Canonical output conventions: |
| 5 | + * ap: positive = anterior |
| 6 | + * ml: positive = right |
| 7 | + * dv: positive = dorsal (superior) |
| 8 | + * depth: positive = deeper from brain surface (always abs) |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it, expect } from 'vitest'; |
| 12 | +import { parseTranslation, parseDeviceConfigCoords } from '../lib/coord-systems.js'; |
| 13 | + |
| 14 | +// ── Fixtures ──────────────────────────────────────────────────────────────── |
| 15 | + |
| 16 | +/** |
| 17 | + * The BREGMA_ARID coordinate system as it appears in procedures.coordinate_system: |
| 18 | + * AP axis → Posterior_to_anterior (positive = anterior) |
| 19 | + * ML axis → Left_to_right (positive = right) |
| 20 | + * SI axis → Superior_to_inferior (positive = ventral, i.e. canonical DV flipped) |
| 21 | + * Depth → depth-from-surface (index 3, always abs) |
| 22 | + */ |
| 23 | +const BREGMA_ARID_COORD_SYS = { |
| 24 | + object_type: 'Coordinate system', |
| 25 | + name: 'BREGMA_ARID', |
| 26 | + origin: 'Bregma', |
| 27 | + axes: [ |
| 28 | + { object_type: 'Axis', name: 'AP', direction: 'Posterior_to_anterior' }, |
| 29 | + { object_type: 'Axis', name: 'ML', direction: 'Left_to_right' }, |
| 30 | + { object_type: 'Axis', name: 'SI', direction: 'Superior_to_inferior' }, |
| 31 | + { object_type: 'Axis', name: 'Depth', direction: 'Superior_to_inferior' }, // index 3 — ignored (depth always uses abs v[3]) |
| 32 | + ], |
| 33 | + axis_unit: 'millimeter', |
| 34 | +}; |
| 35 | + |
| 36 | +// ── parseTranslation ──────────────────────────────────────────────────────── |
| 37 | + |
| 38 | +describe('parseTranslation', () => { |
| 39 | + describe('BREGMA_ARID coordinate system', () => { |
| 40 | + it('maps PA axis correctly: positive value → positive AP (anterior)', () => { |
| 41 | + // v[0]=1.3 along Posterior_to_anterior → ap = +1.3 |
| 42 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [1.3, 0, 0, 0]); |
| 43 | + expect(result.ap).toBeCloseTo(1.3); |
| 44 | + }); |
| 45 | + |
| 46 | + it('maps PA axis correctly: negative value → negative AP (posterior)', () => { |
| 47 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [-1.5, 0, 0, 0]); |
| 48 | + expect(result.ap).toBeCloseTo(-1.5); |
| 49 | + }); |
| 50 | + |
| 51 | + it('maps LR axis correctly: positive value → positive ML (right)', () => { |
| 52 | + // v[1]=1.8 along Left_to_right → ml = +1.8 |
| 53 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [0, 1.8, 0, 0]); |
| 54 | + expect(result.ml).toBeCloseTo(1.8); |
| 55 | + }); |
| 56 | + |
| 57 | + it('maps LR axis correctly: negative value → negative ML (left)', () => { |
| 58 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [0, -1.8, 0, 0]); |
| 59 | + expect(result.ml).toBeCloseTo(-1.8); |
| 60 | + }); |
| 61 | + |
| 62 | + it('maps SI axis correctly: positive value → negative DV (ventral)', () => { |
| 63 | + // v[2]=1 along Superior_to_inferior → dv = -1 (ventral in canonical) |
| 64 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [0, 0, 1, 0]); |
| 65 | + expect(result.dv).toBeCloseTo(-1); |
| 66 | + }); |
| 67 | + |
| 68 | + it('maps SI axis correctly: negative value → positive DV (dorsal)', () => { |
| 69 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [0, 0, -1, 0]); |
| 70 | + expect(result.dv).toBeCloseTo(1); |
| 71 | + }); |
| 72 | + |
| 73 | + it('depth is always abs of v[3] regardless of sign', () => { |
| 74 | + expect(parseTranslation(BREGMA_ARID_COORD_SYS, [0, 0, 0, 4.4]).depth).toBeCloseTo(4.4); |
| 75 | + expect(parseTranslation(BREGMA_ARID_COORD_SYS, [0, 0, 0, -4.4]).depth).toBeCloseTo(4.4); |
| 76 | + }); |
| 77 | + |
| 78 | + it('parses a complete realistic probe translation correctly', () => { |
| 79 | + // [AP=1.3, ML=-1.8, SI=0, depth=4.4] |
| 80 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [1.3, -1.8, 0, 4.4]); |
| 81 | + expect(result.ap).toBeCloseTo(1.3); |
| 82 | + expect(result.ml).toBeCloseTo(-1.8); |
| 83 | + expect(result.dv).toBeCloseTo(0); |
| 84 | + expect(result.depth).toBeCloseTo(4.4); |
| 85 | + }); |
| 86 | + |
| 87 | + it('parses a right-hemisphere probe correctly', () => { |
| 88 | + // [AP=1.1, ML=1.8, SI=0, depth=4.4] |
| 89 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [1.1, 1.8, 0, 4.4]); |
| 90 | + expect(result.ap).toBeCloseTo(1.1); |
| 91 | + expect(result.ml).toBeCloseTo(1.8); |
| 92 | + expect(result.depth).toBeCloseTo(4.4); |
| 93 | + }); |
| 94 | + |
| 95 | + it('parses a posterior probe correctly', () => { |
| 96 | + // [AP=-1.5, ML=3.0, SI=0, depth=3.9] |
| 97 | + const result = parseTranslation(BREGMA_ARID_COORD_SYS, [-1.5, 3.0, 0, 3.9]); |
| 98 | + expect(result.ap).toBeCloseTo(-1.5); |
| 99 | + expect(result.ml).toBeCloseTo(3.0); |
| 100 | + expect(result.depth).toBeCloseTo(3.9); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('axis direction sign conventions', () => { |
| 105 | + it('Anterior_to_posterior: positive value → negative AP (posterior)', () => { |
| 106 | + const cs = { axes: [{ direction: 'Anterior_to_posterior' }] }; |
| 107 | + expect(parseTranslation(cs, [2, 0, 0, 0]).ap).toBeCloseTo(-2); |
| 108 | + }); |
| 109 | + |
| 110 | + it('Posterior_to_anterior: positive value → positive AP (anterior)', () => { |
| 111 | + const cs = { axes: [{ direction: 'Posterior_to_anterior' }] }; |
| 112 | + expect(parseTranslation(cs, [2, 0, 0, 0]).ap).toBeCloseTo(2); |
| 113 | + }); |
| 114 | + |
| 115 | + it('Left_to_right: positive value → positive ML (right)', () => { |
| 116 | + const cs = { axes: [{ direction: 'Left_to_right' }] }; |
| 117 | + expect(parseTranslation(cs, [3, 0, 0, 0]).ml).toBeCloseTo(3); |
| 118 | + }); |
| 119 | + |
| 120 | + it('Right_to_left: positive value → negative ML (left)', () => { |
| 121 | + const cs = { axes: [{ direction: 'Right_to_left' }] }; |
| 122 | + expect(parseTranslation(cs, [3, 0, 0, 0]).ml).toBeCloseTo(-3); |
| 123 | + }); |
| 124 | + |
| 125 | + it('Superior_to_inferior: positive value → negative DV (ventral)', () => { |
| 126 | + const cs = { axes: [{ direction: 'Superior_to_inferior' }] }; |
| 127 | + expect(parseTranslation(cs, [1, 0, 0, 0]).dv).toBeCloseTo(-1); |
| 128 | + }); |
| 129 | + |
| 130 | + it('Inferior_to_superior: positive value → positive DV (dorsal)', () => { |
| 131 | + const cs = { axes: [{ direction: 'Inferior_to_superior' }] }; |
| 132 | + expect(parseTranslation(cs, [1, 0, 0, 0]).dv).toBeCloseTo(1); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('null/missing coordinate system fallback', () => { |
| 137 | + it('null coordinate system uses BREGMA_ARID index order: v0=AP+, v1=ML+, v2=DV+', () => { |
| 138 | + const result = parseTranslation(null, [1.3, -1.8, 0, 4.4]); |
| 139 | + expect(result.ap).toBeCloseTo(1.3); |
| 140 | + expect(result.ml).toBeCloseTo(-1.8); |
| 141 | + expect(result.dv).toBeCloseTo(0); |
| 142 | + expect(result.depth).toBeCloseTo(4.4); |
| 143 | + }); |
| 144 | + |
| 145 | + it('missing axes array falls back to index order', () => { |
| 146 | + const result = parseTranslation({ name: 'CUSTOM' }, [2.0, -1.0, 0, 3.0]); |
| 147 | + expect(result.ap).toBeCloseTo(2.0); |
| 148 | + expect(result.ml).toBeCloseTo(-1.0); |
| 149 | + expect(result.depth).toBeCloseTo(3.0); |
| 150 | + }); |
| 151 | + |
| 152 | + it('empty translation returns zeros with null depth', () => { |
| 153 | + const result = parseTranslation(null, []); |
| 154 | + expect(result.ap).toBe(0); |
| 155 | + expect(result.ml).toBe(0); |
| 156 | + expect(result.dv).toBeNull(); |
| 157 | + expect(result.depth).toBeNull(); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +// ── parseDeviceConfigCoords ────────────────────────────────────────────────── |
| 163 | + |
| 164 | +describe('parseDeviceConfigCoords', () => { |
| 165 | + it('reads coordinate_system and first Translation from transform', () => { |
| 166 | + const deviceConfig = { |
| 167 | + coordinate_system: BREGMA_ARID_COORD_SYS, |
| 168 | + transform: [ |
| 169 | + { object_type: 'Translation', translation: [1.3, -1.8, 0, 4.4] }, |
| 170 | + ], |
| 171 | + }; |
| 172 | + const result = parseDeviceConfigCoords(deviceConfig); |
| 173 | + expect(result.ap).toBeCloseTo(1.3); |
| 174 | + expect(result.ml).toBeCloseTo(-1.8); |
| 175 | + expect(result.depth).toBeCloseTo(4.4); |
| 176 | + }); |
| 177 | + |
| 178 | + it('skips non-Translation transform entries to find Translation', () => { |
| 179 | + const deviceConfig = { |
| 180 | + coordinate_system: BREGMA_ARID_COORD_SYS, |
| 181 | + transform: [ |
| 182 | + { object_type: 'Rotation', angles: [0, 0, 0] }, |
| 183 | + { object_type: 'Translation', translation: [1.1, 1.8, 0, -4.4] }, |
| 184 | + ], |
| 185 | + }; |
| 186 | + const result = parseDeviceConfigCoords(deviceConfig); |
| 187 | + expect(result.ap).toBeCloseTo(1.1); |
| 188 | + expect(result.ml).toBeCloseTo(1.8); |
| 189 | + expect(result.depth).toBeCloseTo(4.4); |
| 190 | + }); |
| 191 | + |
| 192 | + it('falls back gracefully when no coordinate_system present', () => { |
| 193 | + const deviceConfig = { |
| 194 | + coordinate_system: null, |
| 195 | + transform: [ |
| 196 | + { object_type: 'Translation', translation: [2.0, 1.0, 0, 3.5] }, |
| 197 | + ], |
| 198 | + }; |
| 199 | + const result = parseDeviceConfigCoords(deviceConfig); |
| 200 | + expect(result.ap).toBeCloseTo(2.0); |
| 201 | + expect(result.ml).toBeCloseTo(1.0); |
| 202 | + expect(result.depth).toBeCloseTo(3.5); |
| 203 | + }); |
| 204 | + |
| 205 | + it('returns zeros when no transform present', () => { |
| 206 | + const deviceConfig = { |
| 207 | + coordinate_system: BREGMA_ARID_COORD_SYS, |
| 208 | + transform: [], |
| 209 | + }; |
| 210 | + const result = parseDeviceConfigCoords(deviceConfig); |
| 211 | + expect(result.ap).toBe(0); |
| 212 | + expect(result.ml).toBe(0); |
| 213 | + expect(result.depth).toBeNull(); |
| 214 | + }); |
| 215 | + |
| 216 | + it('handles null deviceConfig gracefully', () => { |
| 217 | + const result = parseDeviceConfigCoords(null); |
| 218 | + expect(result.ap).toBe(0); |
| 219 | + expect(result.ml).toBe(0); |
| 220 | + expect(result.depth).toBeNull(); |
| 221 | + }); |
| 222 | +}); |
0 commit comments