You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This proposal is also available on the Babylon.js forum in this thread.
TL;DR:
Tensor would be added, which provides a standardized API for interacting with vectors, colors, matrices, quaternions, etc.
Rational
This proposal's goals are based on the ideas behind #13699, with the core idea being to create a standard interface from which various math constructs are defined.
Take the current difference in behavior between vectors and colors:
And the lack of a method that should exist in Color3:
constv1=Vector3.Random(),v2=Vector3.Random(),c1=Color3.Random(),c2=Color3.Random();v1.addInPlace(v2)// okc1.addInPlace(c2)// ReferenceError! addInPlace does not exist
Tensor provides a single interface for tensor-like objects which users can depend on. By having tensor-like classes implement Tensor, it ensures that all the methods exist and have the same and correct behavior. It is meant for organization and standardization.
Semantics
Note:
CurrentClass is a placeholder for a class that implements Tensor. In fields, it is the current class.
Tensor and its related types will be defined in src/Maths/tensor.ts unless otherwise noted
This proposal also includes Vector from #13699. Vector extends Tensor and adds normalization methods and length / lengthSquared. It is implemented by Vector2, Vector3, and Vector4.
MultidimensionalArray
Defined in src/types.ts
typeMultidimensionalArray<T,Dextendsnumber>=Dextends0
? T
: MultidimensionalArray<T,Decrement<D>>[];
Represents a multidimensional array of T with depth D.
From creates a new instance of CurrentClass from source. source: The tensor to copy data from. fillValue: The value to use for filling empty parts of the resulting CurrentClass.
Example:
constvec3=newVector3(1,2,3);constvec4=Vector4.From(vec3,4);// { 1, 2, 3, 4 }constmatrix=Matrix.From(vec3,0);// this is possible!
Additionally, From could be changed to be From(...args: [...Tensor[], number]): CurrentClass. This is better understood by this invalid Typescript signature (since rest parameters must be last):
as creates an instance of type from an instance of CurrentClass. type: The class to create an instance of. fillValue: The value to use for filling empty parts of the resulting instance.
public get value(): V;
public set value(value: V): void;
value is the values of the tensor in a multidimensional array with the type V (the type parameter of the class). Unlike dimension, this must be implemented as a getter and setter.
getDimension returns the mathimatical dimension [2] of value, similar to Tensor.dimension. If value is not a Tensor or TensorValue, it will throw a TypeError
🛈 Tensor based classes can use getDimension(this.value) for their dimension implementations.
Considerations
Static methods related to data manipulation (e.g. Add, Normalize, Lerp) and non-static normalization methods are outside the scope of this proposal. While Tensor may include them in the future, this proposal does not
This proposal does not provide for a dimensionally dynamic Tensor.
Performance: Since Tensor is defined using the declare class and classes that follow Tensor do so using the implements keyword, there are no runtime changes to Tensor-based classes.
Bundle size: The JS bundle size will increase only by the size of the added functions (isTensor, getDimension, etc.). The TS declaration size will also increase slightly due to Tensor.
Maintenance: Since there is no implementation (just a class declaration), the extra maintenance is minimized to member signatures.
Questions
Should Scalar be included in this proposal? As a rank 0 tensor, the benefits of standardizing Scalar may not be worth it, especially considering it is not currently meant to be instantiated.
How should incompatibility be managed? Should incompatible classes be dropped or should they be modified to follow Tensor?
What other classes (e.g. Size) would be included and standardized?
FAQs
What use cases does this proposal have?
Tensor should not be used by end users. It is meant for organization and standardization. The functions (isTensor, isTensorValue, getDimension, etc.) may be used by the end user. isTensor and isTensorValue can be used for improved type safety, type checking at runtime. getDimension can be used for iteration and for getting information.
The capabilities of the type. VectorLike supports any rank 1 tensor. Tensor generalizes VectorLike, and support any rank tensor. A mathematical vector can be represented as number[]. A mathematical tensor can be represented as a number, number[], number[][], and so on. This table highlights the capability:
I'm closing #13699 since having it as well as this would create some conflicts. I will include Vector (from #13699) in the PR for this issue. In addition, I'm including some new types for compile-time math (which is needed for MultidimensionalArray). I've made edits to my initial comment with the proposal.
@thomlucc Could you please move this back to the 7.0 milestone? Me and the BJS team are trying to get it merged for the big 7.0 release, and it would be nice if the issue reflected the milestone correctly. Thanks!
This proposal is also available on the Babylon.js forum in this thread.
TL;DR:
Tensor
would be added, which provides a standardized API for interacting with vectors, colors, matrices, quaternions, etc.Rational
This proposal's goals are based on the ideas behind #13699, with the core idea being to create a standard interface from which various math constructs are defined.
Take the current difference in behavior between vectors and colors:
And the lack of a method that should exist in Color3:
Tensor
provides a single interface for tensor-like objects which users can depend on. By having tensor-like classes implementTensor
, it ensures that all the methods exist and have the same and correct behavior. It is meant for organization and standardization.Semantics
Note:
CurrentClass
is a placeholder for a class that implementsTensor
. In fields, it is the current class.Tensor
and its related types will be defined insrc/Maths/tensor.ts
unless otherwise notedVector (from #13699 )
Defined in
src/Maths/math.vector.ts
This proposal also includes
Vector
from #13699.Vector
extendsTensor
and adds normalization methods andlength
/lengthSquared
. It is implemented byVector2
,Vector3
, andVector4
.MultidimensionalArray
Defined in
src/types.ts
Represents a multidimensional array of
T
with depthD
.TensorValue
Tensor
Tensor
is adeclare class
for tensor-like classes to implement.Tensor
includes all of the methods inVectorLike
. This includesadd
,subtract
,multiply
,divide
,scale
, ...)fromArray
,toArray
,asArray
, ...)number[]
, the return type for array-related methods will be changed tonumber[]
Tensor.value
clone
,copyFrom
,copyFromFloats
, ...)InPlace
,ToRef
,FromFloats
, etc.It adds or changes the following methods:
Tensor.From
From
creates a new instance ofCurrentClass
fromsource
.source
: The tensor to copy data from.fillValue
: The value to use for filling empty parts of the resultingCurrentClass
.Example:
Additionally,
From
could be changed to beFrom(...args: [...Tensor[], number]): CurrentClass
. This is better understood by this invalid Typescript signature (since rest parameters must be last):Tensor.as
as
creates an instance oftype
from an instance ofCurrentClass
.type
: The class to create an instance of.fillValue
: The value to use for filling empty parts of the resulting instance.Example:
Tensor.sum
sum
return the sum of the components of theTensor
.Example:
Tensor.rank
The
rank
of a tensor is the number of indices required to uniquely select each element of the tensor.Example:
Tensor.dimension
dimension
is the mathematical dimension [2] of the tensor. In dynamic tensor types, it can be defined as a getter.Example:
Tensor.value
value
is the values of the tensor in a multidimensional array with the typeV
(the type parameter of the class). Unlikedimension
, this must be implemented as a getter and setter.Example:
isTensor
Checks if a value is a
Tensor
.Example:
isTensorValue
Checks if a value is a
TensorValue
.Example:
getDimension
getDimension
returns the mathimatical dimension [2] ofvalue
, similar toTensor.dimension
. Ifvalue
is not aTensor
orTensorValue
, it will throw aTypeError
Considerations
Add
,Normalize
,Lerp
) and non-static normalization methods are outside the scope of this proposal. WhileTensor
may include them in the future, this proposal does notTensor
.Tensor
is defined using thedeclare class
and classes that followTensor
do so using theimplements
keyword, there are no runtime changes toTensor
-based classes.isTensor
,getDimension
, etc.). The TS declaration size will also increase slightly due toTensor
.Questions
Scalar
be included in this proposal? As a rank 0 tensor, the benefits of standardizingScalar
may not be worth it, especially considering it is not currently meant to be instantiated.Tensor
?Size
) would be included and standardized?FAQs
What use cases does this proposal have?
Tensor
should not be used by end users. It is meant for organization and standardization. The functions (isTensor
,isTensorValue
,getDimension
, etc.) may be used by the end user.isTensor
andisTensorValue
can be used for improved type safety, type checking at runtime.getDimension
can be used for iteration and for getting information.What is the difference between this and #13699?
The capabilities of the type.
VectorLike
supports any rank 1 tensor.Tensor
generalizesVectorLike
, and support any rank tensor. A mathematical vector can be represented asnumber[]
. A mathematical tensor can be represented as anumber
,number[]
,number[][]
, and so on. This table highlights the capability:number
Scalar
number[]
Vector
,DynamicVector
[number, number]
Vector2
[number, number, number]
Vector3
,Color3
[number, number, number, number]
Vector4
,Color4
,Quaternion
number[][]
Matrix
number[][][]
number[][][][]
TensorValue
Further reading
The text was updated successfully, but these errors were encountered: