Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/vector_math/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.4.3

* Optimizes `normalizeInto`, `min`, `max` and `mix` on `Vector2`, `Vector3`
and `Vector4` to compute their result first and write the `out`/`result`
parameter with a single `setValues` call, and aligns `copyInto` with the
same pattern.

## 2.4.2

* Documents the public geometry filter APIs.
Expand Down
28 changes: 13 additions & 15 deletions packages/vector_math/lib/src/vector_math/vector2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,20 @@ class Vector2 implements Vector {

/// Set the values of [result] to the minimum of [a] and [b] for each line.
static void min(Vector2 a, Vector2 b, Vector2 result) {
result
..x = math.min(a.x, b.x)
..y = math.min(a.y, b.y);
result.setValues(math.min(a.x, b.x), math.min(a.y, b.y));
}

/// Set the values of [result] to the maximum of [a] and [b] for each line.
static void max(Vector2 a, Vector2 b, Vector2 result) {
result
..x = math.max(a.x, b.x)
..y = math.max(a.y, b.y);
result.setValues(math.max(a.x, b.x), math.max(a.y, b.y));
}

/// Interpolate between [min] and [max] with the amount of [a] using a linear
/// interpolation and store the values in [result].
static void mix(Vector2 min, Vector2 max, double a, Vector2 result) {
result
..x = min.x + a * (max.x - min.x)
..y = min.y + a * (max.y - min.y);
final double x = min.x + a * (max.x - min.x);
final double y = min.y + a * (max.y - min.y);
result.setValues(x, y);
}

/// Set the values of the vector.
Expand Down Expand Up @@ -174,9 +170,13 @@ class Vector2 implements Vector {

/// Normalize vector into [out].
Vector2 normalizeInto(Vector2 out) {
out
..setFrom(this)
..normalize();
final double l = length;
if (l == 0.0) {
out.setValues(_v2storage[0], _v2storage[1]);
} else {
final double d = 1.0 / l;
out.setValues(_v2storage[0] * d, _v2storage[1] * d);
}
return out;
}

Expand Down Expand Up @@ -378,9 +378,7 @@ class Vector2 implements Vector {

/// Copy this into [arg]. Returns [arg].
Vector2 copyInto(Vector2 arg) {
final Float32List argStorage = arg._v2storage;
argStorage[1] = _v2storage[1];
argStorage[0] = _v2storage[0];
arg.setValues(_v2storage[0], _v2storage[1]);
return arg;
}

Expand Down
40 changes: 21 additions & 19 deletions packages/vector_math/lib/src/vector_math/vector3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,28 @@ class Vector3 implements Vector {

/// Set the values of [result] to the minimum of [a] and [b] for each line.
static void min(Vector3 a, Vector3 b, Vector3 result) {
result
..x = math.min(a.x, b.x)
..y = math.min(a.y, b.y)
..z = math.min(a.z, b.z);
final double x = math.min(a.x, b.x);
final double y = math.min(a.y, b.y);
final double z = math.min(a.z, b.z);
result.setValues(x, y, z);
}

/// Set the values of [result] to the maximum of [a] and [b] for each line.
static void max(Vector3 a, Vector3 b, Vector3 result) {
result
..x = math.max(a.x, b.x)
..y = math.max(a.y, b.y)
..z = math.max(a.z, b.z);
final double x = math.max(a.x, b.x);
final double y = math.max(a.y, b.y);
final double z = math.max(a.z, b.z);
result.setValues(x, y, z);
}

/// Interpolate between [min] and [max] with the amount of [a] using a linear
/// interpolation and store the values in [result].
static void mix(Vector3 min, Vector3 max, double a, Vector3 result) {
result
..x = min.x + a * (max.x - min.x)
..y = min.y + a * (max.y - min.y)
..z = min.z + a * (max.z - min.z);
result.setValues(
min.x + a * (max.x - min.x),
min.y + a * (max.y - min.y),
min.z + a * (max.z - min.z),
);
}

/// Set the values of the vector.
Expand Down Expand Up @@ -190,9 +191,13 @@ class Vector3 implements Vector {

/// Normalize vector into [out].
Vector3 normalizeInto(Vector3 out) {
out
..setFrom(this)
..normalize();
final double l = length;
if (l == 0.0) {
out.setValues(_v3storage[0], _v3storage[1], _v3storage[2]);
} else {
final double d = 1.0 / l;
out.setValues(_v3storage[0] * d, _v3storage[1] * d, _v3storage[2] * d);
}
return out;
}

Expand Down Expand Up @@ -496,10 +501,7 @@ class Vector3 implements Vector {

/// Copy this into [arg].
Vector3 copyInto(Vector3 arg) {
final Float32List argStorage = arg._v3storage;
argStorage[2] = _v3storage[2];
argStorage[1] = _v3storage[1];
argStorage[0] = _v3storage[0];
arg.setValues(_v3storage[0], _v3storage[1], _v3storage[2]);
return arg;
}

Expand Down
53 changes: 30 additions & 23 deletions packages/vector_math/lib/src/vector_math/vector4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,33 @@ class Vector4 implements Vector {

/// Set the values of [result] to the minimum of [a] and [b] for each line.
static void min(Vector4 a, Vector4 b, Vector4 result) {
result
..x = math.min(a.x, b.x)
..y = math.min(a.y, b.y)
..z = math.min(a.z, b.z)
..w = math.min(a.w, b.w);
result.setValues(
math.min(a.x, b.x),
math.min(a.y, b.y),
math.min(a.z, b.z),
math.min(a.w, b.w),
);
}

/// Set the values of [result] to the maximum of [a] and [b] for each line.
static void max(Vector4 a, Vector4 b, Vector4 result) {
result
..x = math.max(a.x, b.x)
..y = math.max(a.y, b.y)
..z = math.max(a.z, b.z)
..w = math.max(a.w, b.w);
result.setValues(
math.max(a.x, b.x),
math.max(a.y, b.y),
math.max(a.z, b.z),
math.max(a.w, b.w),
);
}

/// Interpolate between [min] and [max] with the amount of [a] using a linear
/// interpolation and store the values in [result].
static void mix(Vector4 min, Vector4 max, double a, Vector4 result) {
result
..x = min.x + a * (max.x - min.x)
..y = min.y + a * (max.y - min.y)
..z = min.z + a * (max.z - min.z)
..w = min.w + a * (max.w - min.w);
result.setValues(
min.x + a * (max.x - min.x),
min.y + a * (max.y - min.y),
min.z + a * (max.z - min.z),
min.w + a * (max.w - min.w),
);
}

/// The components of the vector.
Expand Down Expand Up @@ -211,9 +214,17 @@ class Vector4 implements Vector {

/// Normalize vector into [out].
Vector4 normalizeInto(Vector4 out) {
out
..setFrom(this)
..normalize();
final double l = length;
if (l == 0.0) {
out.setValues(_v4storage[0], _v4storage[1], _v4storage[2], _v4storage[3]);
} else {
final double d = 1.0 / l;
final double x = _v4storage[0] * d;
final double y = _v4storage[1] * d;
final double z = _v4storage[2] * d;
final double w = _v4storage[3] * d;
out.setValues(x, y, z, w);
}
return out;
}

Expand Down Expand Up @@ -414,11 +425,7 @@ class Vector4 implements Vector {

/// Copy this
Vector4 copyInto(Vector4 arg) {
final Float32List argStorage = arg._v4storage;
argStorage[3] = _v4storage[3];
argStorage[2] = _v4storage[2];
argStorage[1] = _v4storage[1];
argStorage[0] = _v4storage[0];
arg.setValues(_v4storage[0], _v4storage[1], _v4storage[2], _v4storage[3]);
return arg;
}

Expand Down
28 changes: 13 additions & 15 deletions packages/vector_math/lib/src/vector_math_64/vector2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,20 @@ class Vector2 implements Vector {

/// Set the values of [result] to the minimum of [a] and [b] for each line.
static void min(Vector2 a, Vector2 b, Vector2 result) {
result
..x = math.min(a.x, b.x)
..y = math.min(a.y, b.y);
result.setValues(math.min(a.x, b.x), math.min(a.y, b.y));
}

/// Set the values of [result] to the maximum of [a] and [b] for each line.
static void max(Vector2 a, Vector2 b, Vector2 result) {
result
..x = math.max(a.x, b.x)
..y = math.max(a.y, b.y);
result.setValues(math.max(a.x, b.x), math.max(a.y, b.y));
}

/// Interpolate between [min] and [max] with the amount of [a] using a linear
/// interpolation and store the values in [result].
static void mix(Vector2 min, Vector2 max, double a, Vector2 result) {
result
..x = min.x + a * (max.x - min.x)
..y = min.y + a * (max.y - min.y);
final double x = min.x + a * (max.x - min.x);
final double y = min.y + a * (max.y - min.y);
result.setValues(x, y);
}

/// Set the values of the vector.
Expand Down Expand Up @@ -174,9 +170,13 @@ class Vector2 implements Vector {

/// Normalize vector into [out].
Vector2 normalizeInto(Vector2 out) {
out
..setFrom(this)
..normalize();
final double l = length;
if (l == 0.0) {
out.setValues(_v2storage[0], _v2storage[1]);
} else {
final double d = 1.0 / l;
out.setValues(_v2storage[0] * d, _v2storage[1] * d);
}
return out;
}

Expand Down Expand Up @@ -378,9 +378,7 @@ class Vector2 implements Vector {

/// Copy this into [arg]. Returns [arg].
Vector2 copyInto(Vector2 arg) {
final Float64List argStorage = arg._v2storage;
argStorage[1] = _v2storage[1];
argStorage[0] = _v2storage[0];
arg.setValues(_v2storage[0], _v2storage[1]);
return arg;
}

Expand Down
40 changes: 21 additions & 19 deletions packages/vector_math/lib/src/vector_math_64/vector3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,28 @@ class Vector3 implements Vector {

/// Set the values of [result] to the minimum of [a] and [b] for each line.
static void min(Vector3 a, Vector3 b, Vector3 result) {
result
..x = math.min(a.x, b.x)
..y = math.min(a.y, b.y)
..z = math.min(a.z, b.z);
final double x = math.min(a.x, b.x);
final double y = math.min(a.y, b.y);
final double z = math.min(a.z, b.z);
result.setValues(x, y, z);
}

/// Set the values of [result] to the maximum of [a] and [b] for each line.
static void max(Vector3 a, Vector3 b, Vector3 result) {
result
..x = math.max(a.x, b.x)
..y = math.max(a.y, b.y)
..z = math.max(a.z, b.z);
final double x = math.max(a.x, b.x);
final double y = math.max(a.y, b.y);
final double z = math.max(a.z, b.z);
result.setValues(x, y, z);
}

/// Interpolate between [min] and [max] with the amount of [a] using a linear
/// interpolation and store the values in [result].
static void mix(Vector3 min, Vector3 max, double a, Vector3 result) {
result
..x = min.x + a * (max.x - min.x)
..y = min.y + a * (max.y - min.y)
..z = min.z + a * (max.z - min.z);
result.setValues(
min.x + a * (max.x - min.x),
min.y + a * (max.y - min.y),
min.z + a * (max.z - min.z),
);
}

/// Set the values of the vector.
Expand Down Expand Up @@ -190,9 +191,13 @@ class Vector3 implements Vector {

/// Normalize vector into [out].
Vector3 normalizeInto(Vector3 out) {
out
..setFrom(this)
..normalize();
final double l = length;
if (l == 0.0) {
out.setValues(_v3storage[0], _v3storage[1], _v3storage[2]);
} else {
final double d = 1.0 / l;
out.setValues(_v3storage[0] * d, _v3storage[1] * d, _v3storage[2] * d);
}
return out;
}

Expand Down Expand Up @@ -496,10 +501,7 @@ class Vector3 implements Vector {

/// Copy this into [arg].
Vector3 copyInto(Vector3 arg) {
final Float64List argStorage = arg._v3storage;
argStorage[2] = _v3storage[2];
argStorage[1] = _v3storage[1];
argStorage[0] = _v3storage[0];
arg.setValues(_v3storage[0], _v3storage[1], _v3storage[2]);
return arg;
}

Expand Down
Loading