Skip to content

Commit

Permalink
Merge branch 'master' into changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew authored Oct 18, 2023
2 parents d8676bc + 2d6311e commit 923e065
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 123 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ jobs:
strategy:
fail-fast: false
matrix:
sdk: [2.19.0, dev]
sdk: [3.0.0, dev]

steps:
# These are the latest versions of the github actions; dependabot will
# send PRs to keep these up-to-date.
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c
- uses: dart-lang/setup-dart@a57a6c04cf7d4840e88432aad6281d1e125f0d46
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
with:
sdk: ${{ matrix.sdk }}
- run: dart pub get
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Added an operator== to Quaternion so that two instances of quaternions can
be evaluated for equality.
- Require Dart 2.19
- Require Dart 3.0

## 2.1.4

Expand Down
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ A Vector math library for 2D and 3D applications.
import 'package:vector_math/vector_math.dart';
void main() {
Vector3 x = new Vector3.zero(); // Zero vector
Vector4 y = new Vector4.all(4.0); // Vector with 4.0 in all lanes
Vector3 x = Vector3.zero(); // Zero vector
Vector4 y = Vector4.all(4.0); // Vector with 4.0 in all lanes
x.zyx = y.xzz; // Sets z,y,x the values in x,z,z
}
```
Expand All @@ -47,9 +47,9 @@ import 'package:vector_math/vector_math.dart';
void main() {
// Rotation of PI/2 degrees around the Y axis followed by a
// translation of (5.0, 2.0, 3.0).
Matrix4 T = new Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
Matrix4 T = Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
// A point.
Vector3 position = new Vector3(1.0, 1.0, 1.0);
Vector3 position = Vector3(1.0, 1.0, 1.0);
// Transform position by T.
T.transform3(position);
}
Expand All @@ -64,7 +64,7 @@ import 'package:vector_math/vector_math.dart';
void main() {
// Rotation of 90 degrees around the Y axis followed by a
// translation of (5.0, 2.0, 3.0).
Matrix4 T = new Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
Matrix4 T = Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
// Invert T.
T.invert();
// Invert just the rotation in T.
Expand All @@ -80,13 +80,13 @@ import 'package:vector_math/vector_math.dart';
void main() {
// The X axis.
Vector3 axis = new Vector3(1.0, 0.0, 0.0);
Vector3 axis = Vector3(1.0, 0.0, 0.0);
// 90 degrees.
double angle = PI / 2.0;
// Quaternion encoding a 90 degree rotation along the X axis.
Quaternion q = new Quaternion.axisAngle(axis, angle);
Quaternion q = Quaternion.axisAngle(axis, angle);
// A point.
Vector3 point = new Vector3(1.0, 1.0, 1.0);
Vector3 point = Vector3(1.0, 1.0, 1.0);
// Rotate point by q.
q.rotate(point);
}
Expand All @@ -99,12 +99,11 @@ import 'package:vector_math/vector_math.dart';
void main() {
// Define the first box with a minimum and a maximum.
Aabb2 aabbOne = new Aabb2.minMax(new Vector2.zero(), new Vector2(4.0, 4.0));
Aabb2 aabbOne = Aabb2.minMax(Vector2.zero(), Vector2(4.0, 4.0));
// Define the second box
Aabb2 aabbTwo =
new Aabb2.minMax(new Vector2(5.0, 5.0), new Vector2(6.0, 6.0));
Aabb2 aabbTwo = Aabb2.minMax(Vector2(5.0, 5.0), Vector2(6.0, 6.0));
// Extend the second box to contain a point
aabbTwo.hullPoint(new Vector2(3.0, 3.0));
aabbTwo.hullPoint(Vector2(3.0, 3.0));
// Check if the two boxes intersect, returns true in this case.
bool intersect = aabbOne.intersectsWithAabb2(aabbTwo);
}
Expand All @@ -117,9 +116,9 @@ import 'package:vector_math/vector_math.dart';
void main() {
// Define a ray starting at the origin and going into positive x-direction.
Ray ray = new Ray.originDirection(new Vector3.zero(), new Vector3(1.0, 0.0, 0.0));
Ray ray = Ray.originDirection(Vector3.zero(), Vector3(1.0, 0.0, 0.0));
// Defines a sphere with the center (5.0 0.0 0.0) and a radius of 2.
Sphere sphere = new Sphere.centerRadius(new Vector3(5.0, 0.0, 0.0), 2.0);
Sphere sphere = Sphere.centerRadius(Vector3(5.0, 0.0, 0.0), 2.0);
// Checks if the ray intersect with the sphere and returns the distance of the
// intersection from the origin of the ray. Would return null if no intersection
// is found.
Expand All @@ -137,11 +136,11 @@ import 'package:vector_math/vector_math.dart';
void main() {
// Access a build-in color, colors are stored in 4-dimensional vectors.
Vector4 red = Colors.red;
Vector4 gray = new Vector4.zero();
Vector4 gray = Vector4.zero();
// Convert the red color to a grayscaled color.
Colors.toGrayscale(red, gray);
// Parse a blue color from a hex string.
Vector4 blue = new Vector4.zero();
Vector4 blue = Vector4.zero();
Colors.fromHexString('#0000FF', blue);
// Convert the blue color from RGB to HSL.
Colors.rgbToHsl(blue, blue);
Expand Down
180 changes: 180 additions & 0 deletions benchmark/vector2_bench.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright (c) 2015, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import 'dart:math' as math;
import 'dart:typed_data';

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:vector_math/vector_math.dart';

class ConstructorBenchmark extends BenchmarkBase {
ConstructorBenchmark() : super('Vector2()');

static void main() {
ConstructorBenchmark().report();
}

@override
void run() {
for (var i = 0; i < 100000; i++) {
Vector2(100, 100);
}
}
}

class ConstructorZeroBenchmark extends BenchmarkBase {
ConstructorZeroBenchmark() : super('Vector2.zero()');

static void main() {
ConstructorZeroBenchmark().report();
}

@override
void run() {
for (var i = 0; i < 100000; i++) {
Vector2.zero();
}
}
}

class ConstructorArrayBenchmark extends BenchmarkBase {
ConstructorArrayBenchmark() : super('Vector2.array()');

static void main() {
ConstructorArrayBenchmark().report();
}

@override
void run() {
for (var i = 0.0; i < 100000; i++) {
Vector2.array([i, i]);
}
}
}

class ConstructorAllBenchmark extends BenchmarkBase {
ConstructorAllBenchmark() : super('Vector2.all()');

static void main() {
ConstructorAllBenchmark().report();
}

@override
void run() {
for (var i = 0.0; i < 100000; i++) {
Vector2.all(i);
}
}
}

class ConstructorCopyBenchmark extends BenchmarkBase {
ConstructorCopyBenchmark() : super('Vector2.copy()');

static void main() {
ConstructorCopyBenchmark().report();
}

@override
void run() {
final copyFrom = Vector2(1, 1);
for (var i = 0.0; i < 100000; i++) {
Vector2.copy(copyFrom);
}
}
}

class ConstructorFromFloat32ListBenchmark extends BenchmarkBase {
ConstructorFromFloat32ListBenchmark() : super('Vector2.fromFloat32List()');

static void main() {
ConstructorFromFloat32ListBenchmark().report();
}

@override
void run() {
final list = Float32List.fromList([0.0, 0.0]);
for (var i = 0.0; i < 100000; i++) {
Vector2.fromFloat32List(list);
}
}
}

class ConstructorFromBufferBenchmark extends BenchmarkBase {
ConstructorFromBufferBenchmark() : super('Vector2.fromBuffer()');

static void main() {
ConstructorFromBufferBenchmark().report();
}

@override
void run() {
final buffer = Uint32List(2).buffer;
for (var i = 0.0; i < 100000; i++) {
Vector2.fromBuffer(buffer, 0);
}
}
}

class ConstructorRandomBenchmark extends BenchmarkBase {
ConstructorRandomBenchmark() : super('Vector2.random()');

static void main() {
ConstructorRandomBenchmark().report();
}

@override
void run() {
final random = math.Random();
for (var i = 0.0; i < 100000; i++) {
Vector2.random(random);
}
}
}

class SetFromBenchmark extends BenchmarkBase {
SetFromBenchmark() : super('Vector2.setFrom()');
final Vector2 v1 = Vector2(100, 100);
final Vector2 v2 = Vector2.zero();

static void main() {
SetFromBenchmark().report();
}

@override
void run() {
for (var i = 0; i < 100000; i++) {
v2.setFrom(v1);
}
}
}

class DotProductBenchmark extends BenchmarkBase {
DotProductBenchmark() : super('Vector2.dot()');
final Vector2 v1 = Vector2(100, 100);
final Vector2 v2 = Vector2(100, 200);

static void main() {
DotProductBenchmark().report();
}

@override
void run() {
for (var i = 0; i < 100000; i++) {
v1.dot(v2);
}
}
}

void main() {
ConstructorBenchmark.main();
ConstructorZeroBenchmark.main();
ConstructorArrayBenchmark.main();
ConstructorAllBenchmark.main();
ConstructorCopyBenchmark.main();
ConstructorFromFloat32ListBenchmark.main();
ConstructorFromBufferBenchmark.main();
ConstructorRandomBenchmark.main();
SetFromBenchmark.main();
DotProductBenchmark.main();
}
2 changes: 1 addition & 1 deletion lib/src/vector_math/quaternion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class Quaternion {
/// [axis] of rotation.
Vector3 get axis {
final den = 1.0 - (_qStorage[3] * _qStorage[3]);
if (den < 0.0005) {
if (den < 0.00002) {
// 0-angle rotation, so axis does not matter
return Vector3.zero();
}
Expand Down
Loading

0 comments on commit 923e065

Please sign in to comment.