Skip to content

Commit cfdd3b2

Browse files
Change function definitions to match other styles (using underscores).
1 parent 31dbcb2 commit cfdd3b2

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/utils/tensor.js

+34-32
Original file line numberDiff line numberDiff line change
@@ -795,57 +795,63 @@ export class Tensor {
795795
}
796796

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

811814
/**
812-
* Performs {@link Tensor._dilate} and returns a new Tensor.
815+
* Returns a new Tensor where the data is mutated through a dilation
816+
* morphological operation.
813817
*
814818
* @param {KernelSize} kernelSize The width and height of the kernel.
815819
* @param {Shape} [shape='RECT'] The shape of the kernel.
816820
* @param {Point} [anchor={x: -1, y: -1}] The central position of the kernel.
817-
* @returns {Promise<Tensor>} The cloned, modified output tensor.
821+
* @returns {Promise<Tensor>} The new Tensor.
818822
*/
819823
async dilate(kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
820-
const data = await this._dilate(this.data, kernelSize, shape, anchor);
821-
return new Tensor(this.type, data, this.dims);
824+
return this.clone().dilate_(kernelSize, shape, anchor);
822825
}
823826

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

838844
/**
839-
* Performs {@link Tensor._erode} and returns a new Tensor.
845+
* Returns a new Tensor where the data is mutated through a erosion
846+
* morphological operation.
840847
*
841848
* @param {KernelSize} kernelSize The width and height of the kernel.
842849
* @param {Shape} [shape='RECT'] The shape of the kernel.
843850
* @param {Point} [anchor={x: -1, y: -1}] The central position of the kernel.
844-
* @returns {Promise<Tensor>} The cloned, modified output tensor.
851+
* @returns {Promise<Tensor>} The new Tensor.
845852
*/
846853
async erode(kernelSize = 3, shape = 'RECT', anchor = { x: -1, y: -1 }) {
847-
const data = await this._erode(this.data, kernelSize, shape, anchor);
848-
return new Tensor(this.type, data, this.dims);
854+
return this.clone().erode_(kernelSize, shape, anchor);
849855
}
850856

851857
/**
@@ -956,19 +962,15 @@ export class Tensor {
956962
case 'DILATE':
957963
return this.dilate(kernelSize, shape, anchor);
958964

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

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

973975
default:
974976
throw new Error("Unknown morphological operation");

0 commit comments

Comments
 (0)