Skip to content

Commit 22a551c

Browse files
Change function definitions to match other styles (using underscores).
1 parent 7aa5e08 commit 22a551c

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/utils/tensor.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -797,57 +797,63 @@ export class Tensor {
797797
}
798798

799799
/**
800-
* A morphological operation that performs a dilation on the input tensor.
801-
* A kernel will be applied to each element and maximum value will be used.
800+
* Mutates the data through a dilation morphological operation.
802801
*
803-
* @param {DataArray} data The input tensor data.
804802
* @param {KernelSize} kernelSize The width and height of the kernel.
805803
* @param {Shape} [shape='RECT'] The shape of the kernel.
806804
* @param {Point} [anchor={x: -1, y: -1}] The central position of the kernel.
807-
* @returns {Promise<DataArray>} The cloned, modified output tensor.
805+
* @returns {Promise<Tensor>} Returns `this`.
808806
*/
809-
async _dilate(data, kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
810-
return this.morphologicalOperation('DILATE', data, kernelSize, shape, anchor);
807+
async dilate_(kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
808+
const this_data = this.data;
809+
const data = await this.morphologicalOperation('DILATE', this_data, kernelSize, shape, anchor);
810+
for (let i = 0; i < this_data.length; ++i) {
811+
this.data[i] = data[i];
812+
}
813+
return this;
811814
}
812815

813816
/**
814-
* Performs {@link Tensor._dilate} and returns a new Tensor.
817+
* Returns a new Tensor where the data is mutated through a dilation
818+
* morphological operation.
815819
*
816820
* @param {KernelSize} kernelSize The width and height of the kernel.
817821
* @param {Shape} [shape='RECT'] The shape of the kernel.
818822
* @param {Point} [anchor={x: -1, y: -1}] The central position of the kernel.
819-
* @returns {Promise<Tensor>} The cloned, modified output tensor.
823+
* @returns {Promise<Tensor>} The new Tensor.
820824
*/
821825
async dilate(kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
822-
const data = await this._dilate(this.data, kernelSize, shape, anchor);
823-
return new Tensor(this.type, data, this.dims);
826+
return this.clone().dilate_(kernelSize, shape, anchor);
824827
}
825828

826829
/**
827-
* A morphological operation that performs an erosion on the input tensor.
828-
* A kernel will be applied to each element and minimum value will be used.
830+
* * Mutates the data through a erosion morphological operation.
829831
*
830-
* @param {DataArray} data The input tensor data.
831832
* @param {KernelSize} kernelSize The width and height of the kernel.
832833
* @param {Shape} [shape='RECT'] The shape of the kernel.
833834
* @param {Point} [anchor={x: -1, y: -1}] The central position of the kernel.
834-
* @returns {Promise<DataArray>} The cloned, modified output tensor.
835+
* @returns {Promise<Tensor>} Returns `this`.
835836
*/
836-
async _erode(data, kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
837-
return this.morphologicalOperation('ERODE', data, kernelSize, shape, anchor);
837+
async erode_(kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
838+
const this_data = this.data;
839+
const data = await this.morphologicalOperation('ERODE', this_data, kernelSize, shape, anchor);
840+
for (let i = 0; i < this_data.length; ++i) {
841+
this.data[i] = data[i];
842+
}
843+
return this;
838844
}
839845

840846
/**
841-
* Performs {@link Tensor._erode} and returns a new Tensor.
847+
* Returns a new Tensor where the data is mutated through a erosion
848+
* morphological operation.
842849
*
843850
* @param {KernelSize} kernelSize The width and height of the kernel.
844851
* @param {Shape} [shape='RECT'] The shape of the kernel.
845852
* @param {Point} [anchor={x: -1, y: -1}] The central position of the kernel.
846-
* @returns {Promise<Tensor>} The cloned, modified output tensor.
853+
* @returns {Promise<Tensor>} The new Tensor.
847854
*/
848855
async erode(kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
849-
const data = await this._erode(this.data, kernelSize, shape, anchor);
850-
return new Tensor(this.type, data, this.dims);
856+
return this.clone().erode_(kernelSize, shape, anchor);
851857
}
852858

853859
/**
@@ -958,19 +964,15 @@ export class Tensor {
958964
case 'DILATE':
959965
return this.dilate(kernelSize, shape, anchor);
960966

961-
case 'OPEN': {
962-
let data = this.data;
963-
data = await this._erode(data, kernelSize, shape, anchor);
964-
data = await this._dilate(data, kernelSize, shape, anchor);
965-
return new Tensor(this.type, data, this.dims);
966-
}
967+
case 'OPEN':
968+
return (await this
969+
.erode_(kernelSize, shape, anchor))
970+
.dilate_(kernelSize, shape, anchor);
967971

968-
case 'CLOSE': {
969-
let data = this.data;
970-
data = await this._dilate(data, kernelSize, shape, anchor);
971-
data = await this._erode(data, kernelSize, shape, anchor);
972-
return new Tensor(this.type, data, this.dims);
973-
}
972+
case 'CLOSE':
973+
return (await this
974+
.dilate_(kernelSize, shape, anchor))
975+
.erode_(kernelSize, shape, anchor);
974976

975977
default:
976978
throw new Error("Unknown morphological operation");

0 commit comments

Comments
 (0)