Skip to content

Commit 96b133a

Browse files
committed
feat(Matrix): add getMatrixElement, setMatrixElement
When used in the browser, the image.direction loses the methods getElement and getElement. These can be used instead.
1 parent 7a6490c commit 96b133a

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

src/itkgetMatrixElement.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const getMatrixElement = (matrix, row, column) => {
2+
return matrix.data[column + row * matrix.columns]
3+
}
4+
5+
module.exports = getMatrixElement

src/itksetMatrixElement.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Matrix = require('./itkMatrix.js')
2+
3+
const setMatrixElement = (matrix, row, column, value) => {
4+
let newMatrix = new Matrix(matrix)
5+
newMatrix.data[column + row * newMatrix.columns] = value
6+
return newMatrix
7+
}
8+
9+
module.exports = setMatrixElement

test/Browser/itkreadImageFileTest.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const readImageFile = require('itkreadImageFile.js')
55
const IntTypes = require('itkIntTypes.js')
66
const PixelTypes = require('itkPixelTypes.js')
77

8+
const getMatrixElement = require('itkgetMatrixElement.js')
9+
810
const cthead1SmallBase64DataURI = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAAAAABWESUoAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElNRQfhBQYVKw8AZTNIAAADdklEQVQ4y2WTa2wUVRiGp6W7O3POnLmc2VrstokJlrBIUBJigjfSICVCCAo/QKM/FFNRIESJQKAws3M7M2f20t3GthRKQQq0kkoXMIq9oFwCXkg0UpMakGLgR9EmJF4TNOvZhRBb31+TvM955/vO+T6Ou69pAgSwKCCAEPc/lYUhFEUkMgH2ESmbYocEEUmKLIQqBKmEgUlERQhAPhyJi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6MjY6MzQtMDQ6MDCVE0R/AAAAAElFTkSuQmCC'
911
const byteString = window.atob(cthead1SmallBase64DataURI.split(',')[1])
1012
const mimeString = cthead1SmallBase64DataURI.split(',')[0].split(':')[1].split(';')[0]
@@ -25,15 +27,14 @@ test('readImageFile reads a File', t => {
2527
t.is(image.origin[1], 0.0, 'origin[1]')
2628
t.is(image.spacing[0], 1.0, 'spacing[0]')
2729
t.is(image.spacing[1], 1.0, 'spacing[1]')
28-
t.is(image.direction, 'aoeu')
29-
t.is(image.direction.getElement(0, 0), 1.0, 'direction (0, 0)')
30-
t.is(image.direction.getElement(0, 1), 0.0, 'direction (0, 1)')
31-
t.is(image.direction.getElement(1, 0), 0.0, 'direction (1, 0)')
32-
t.is(image.direction.getElement(1, 1), 1.0, 'direction (1, 1)')
30+
t.is(getMatrixElement(image.direction, 0, 0), 1.0, 'direction (0, 0)')
31+
t.is(getMatrixElement(image.direction, 0, 1), 0.0, 'direction (0, 1)')
32+
t.is(getMatrixElement(image.direction, 1, 0), 0.0, 'direction (1, 0)')
33+
t.is(getMatrixElement(image.direction, 1, 1), 1.0, 'direction (1, 1)')
3334
t.is(image.size[0], 32, 'size[0]')
3435
t.is(image.size[1], 32, 'size[1]')
3536
t.is(image.buffer.length, 1024, 'buffer.length')
36-
t.is(image.buffer[512], 1024, 'buffer[512]')
37+
t.is(image.buffer[512], 12, 'buffer[512]')
3738
t.end()
3839
})
3940
})

test/itkMatrixTest.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import test from 'ava'
22
import path from 'path'
33

44
const Matrix = require(path.resolve(__dirname, '..', 'dist', 'itkMatrix.js'))
5+
const getMatrixElement = require(path.resolve(__dirname, '..', 'dist', 'itkgetMatrixElement.js'))
6+
const setMatrixElement = require(path.resolve(__dirname, '..', 'dist', 'itksetMatrixElement.js'))
57

68
test('rows should have the same number of rows as passed into the constructor', t => {
79
let matrix = new Matrix(2, 3)
@@ -40,6 +42,20 @@ test('setElement() should set elements of the matrix', t => {
4042
t.is(matrix.data[3], 5.0)
4143
})
4244

45+
test('setMatrixElement() should set elements of the matrix', t => {
46+
let matrix = new Matrix(2, 2)
47+
matrix.setIdentity()
48+
let newMatrix = null
49+
newMatrix = setMatrixElement(matrix, 0, 0, 2.0)
50+
newMatrix = setMatrixElement(newMatrix, 0, 1, 3.0)
51+
newMatrix = setMatrixElement(newMatrix, 1, 0, 4.0)
52+
newMatrix = setMatrixElement(newMatrix, 1, 1, 5.0)
53+
t.is(newMatrix.data[0], 2.0)
54+
t.is(newMatrix.data[1], 3.0)
55+
t.is(newMatrix.data[2], 4.0)
56+
t.is(newMatrix.data[3], 5.0)
57+
})
58+
4359
test('getElement() should get elements of the matrix', t => {
4460
let matrix = new Matrix(2, 2)
4561
matrix.setElement(0, 0, 2.0)
@@ -52,6 +68,18 @@ test('getElement() should get elements of the matrix', t => {
5268
t.is(matrix.getElement(1, 1), 5.0)
5369
})
5470

71+
test('getMatrixElement() should get elements of the matrix', t => {
72+
let matrix = new Matrix(2, 2)
73+
matrix.setElement(0, 0, 2.0)
74+
matrix.setElement(0, 1, 3.0)
75+
matrix.setElement(1, 0, 4.0)
76+
matrix.setElement(1, 1, 5.0)
77+
t.is(getMatrixElement(matrix, 0, 0), 2.0)
78+
t.is(getMatrixElement(matrix, 0, 1), 3.0)
79+
t.is(getMatrixElement(matrix, 1, 0), 4.0)
80+
t.is(getMatrixElement(matrix, 1, 1), 5.0)
81+
})
82+
5583
test('passing a Matrix to the constructor should create a copy', t => {
5684
let matrix = new Matrix(2, 2)
5785
matrix.setElement(0, 0, 2.0)

0 commit comments

Comments
 (0)