p-hatan/ mirror

pub repository

mirror.p-hatan.ir

vector_math

2.1.3

A Vector Math library for 2D and 3D applications.

Download archive

Readme

CI Coverage Status

Introduction

A Vector math library for 2D and 3D applications.

Features

  • 2D, 3D, and 4D vector and matrix types.
  • Quaternion type for animating rotations.
  • Collision detection: AABB, rays, spheres, ...
  • Utilities like color and common rendering related operations
  • Flexible getters and setters, for example, position.xwz = color.grb;.
  • Fully documented.
  • Well tested.
  • Heavily optimized.

Libraries using vector_math

Examples

1. Using the GLSL getter and setter syntax.

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
  x.zyx = y.xzz; // Sets z,y,x the values in x,z,z
}

2. Transforming a vector.

import 'dart:math';
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);
  // A point.
  Vector3 position = new Vector3(1.0, 1.0, 1.0);
  // Transform position by T.
  T.transform3(position);
}

3. Invert a matrix

import 'dart:math';
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);
  // Invert T.
  T.invert();
  // Invert just the rotation in T.
  T.invertRotation();
}

4. Rotate a vector using a quaternion

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

void main() {
  // The X axis.
  Vector3 axis = new 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);
  // A point.
  Vector3 point = new Vector3(1.0, 1.0, 1.0);
  // Rotate point by q.
  q.rotate(point);
}

5. Check if two axis aligned bounding boxes intersect

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));
  // Define the second box
  Aabb2 aabbTwo =
      new Aabb2.minMax(new Vector2(5.0, 5.0), new Vector2(6.0, 6.0));
  // Extend the second box to contain a point
  aabbTwo.hullPoint(new Vector2(3.0, 3.0));
  // Check if the two boxes intersect, returns true in this case.
  bool intersect = aabbOne.intersectsWithAabb2(aabbTwo);
}

6. Check where a ray and a sphere intersect

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));
  // 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);
  // 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.
  double distanceFromOrigin = ray.intersectsWithSphere(sphere);
  // Evaluate the position of the intersection, in this case (3.0 0.0 0.0).
  Vector3 position = ray.at(distanceFromOrigin);
}

7. Work with colors

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();
  // 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();
  Colors.fromHexString('#0000FF', blue);
  // Convert the blue color from RGB to HSL.
  Colors.rgbToHsl(blue, blue);
  // Reduce the lightness of the color by 50%.
  blue.z *= 0.5;
  // Convert the HSL color back to RGB.
  Colors.hslToRgb(blue, blue);
}

Development

To run test cases:

~/src/vector_math/> pub run test:test

To automatically generate the latest version of vector_math_64:

~/src/vector_math/> dart tool/generate_vector_math_64.dart
Changelog

2.1.3

  • operator == overrides no longer take nullable arguments. This is only visible for classes that implement the interfaces defined in this package which no longer need to also widen the argument type.
  • Deprecate SimplexNoise; see https:github.com/google/vector_math.dart/issues/270 for more information.

2.1.2

  • Fix to Quad.copy (#221)

2.1.1

  • Deprecate hash.dart.
  • Require Dart v2.14 or greater.

2.1.0

  • Stable release for null safety.
  • Improve performance of Matrix4.decompose by reusing objects.

2.0.8 - July 2018

  • Internal fix to use Dart 2 core library constant names.

2.0.7 - April 2018

  • Fixed indexing bug in MeshGeometry.combine

2.0.6 - March 2018

  • Fixed angleTo for vectors that do not have unit length
  • Added Matrix4.tryInvert.

2.0.5 - July 2017

  • Strong mode clean

2.0.4 - February 2017

  • Added Matrix4.isIdentity()
  • Added Matrix4.isZero()
  • Added Matrix3.isIdentity()
  • Added Matrix3.isZero()
  • Added Vector2.angleTo and Vector2.angleToSigned

2.0.3 - May 2016

  • Synchronize Float64 version

2.0.2 - May 2016

  • Add Matrix4.leftTranslate

2.0.1 - April 2016

  • Add Matrix4.SkewX, Matrix4.SkewY, and Matrix4.Skew constructors

2.0.0 - March 2016

  • Remove call chaining, methods don't return this anymore. You can use the method cascade operator instead.
  • Remove dependency on quiver package

1.4.7 - February 2016

  • Fixed ArgumentError usage in Matrix4.inverted constructor

1.4.6 - January 2016

  • Added MatrixX.fromList constructor

1.4.5 - January 2016

  • Added Matrix4.inverted constructor

1.4.4 - December 2014

  • Updated dependencies.
  • Moved benchmark code into benchmark/
  • Updated vector_math_64.

1.4.3 - February 2014

  • Add color conversion routines (Contributed by Oliver Sand)
  • More collision and geometry routines (Contributed by Oliver Sand)
  • More tests (Contributed by Oliver Sand)
  • v 1.4.3 pub release

1.4.1 - January 2014

  • Better mesh generators (contributed by Brandon Jones)
  • Fix bug in ray v. triangle intersection test (contributed by @AMagill)

1.4.0 - November 2013

  • Add basic mesh generators (contributed by Brandon Jones)
  • Add more collision detection objects (contributed by Oliver Sand)

1.3.5 - July 2013

  • Class names now start with upper case, following Dart style guide.
  • Performance audit.
  • New vector_math_operations library.
  • New vector_math_lists library.
  • Added Aabb2 and fleshed out Aabb3 (thanks to Laszlo Korte)
  • Added Matrix solve methods (thanks to Laszlo Korte)
  • Added Methods needed for Three.dart (thanks to Anders Forsell)

1.1.0 - April 2013

  • Large refactoring.

0.9.7 - March 2013

  • Refactor generated constructor code into small functions.
  • Refactor generated operator* code into small functions.
  • Fix typo in quaternion code.

0.9.6 - March 2013

  • Update to latest String library.
  • Fix holding references in matrix constructor.
  • Replace double type with num in rotationY, and rotationZ.

0.9.5 - February 2013

  • Remove remaining double type tests and replace them with num.
  • Don't throw in the default matrix and vector constructors.

0.9.4 - February 2013

  • Remove unnecessary dart:scalarlist import.
  • Now that dart2js is fixed, rename negate_ back to negate.
  • Fix library imports in test suites.
  • Flexible constructor fix added to the generator.
  • Tested library under dart2js

0.9.3 - February 2013

  • Revert to using a single library!
  • Public API reverted to taking nums instead of doubles.
  • Fixed all warnings/errors introduced by M3.
  • drone.io integrated tests.
  • Fixed bug in rotation construction.
  • Fixed bug in orthographic matrix construction.
  • External contribution by fkleon fixing flexible constructors.
  • External contribution by donny-dont adding a missing cast .toDouble().

0.9.0 - October 2012

  • Pub: Dart Vector Math now fully supports the pub package management system!
  • API++: Minor changes to the API everywhere. The changes improve the aesthetics and performance of the library.
  • Faster: Lots of performance tweaks resulting in the library getting faster and generating less garbage.
  • 2D Cross Product: The Box2D Dart port requires a 2D cross product.
  • Library Split: Two libraries, one for browser applications and console applications.

0.8.5 - July 29 2012

  • 33% faster matrix matrix multiply
  • Fix generated operator[]=
  • Fix OpenGL lookat and perspective matrix constructors
  • Fix mat4x4 rotation constructors
  • Fix mat4x4 multiplied with vector3 not applying translation
  • Add utility methods for moving between Dart Vector Math types and Float32Array/Float32List types
  • Add mat4x4 translation constructor
  • Fixed buildPlaneVectors method
  • Fix mat4x4 transformDirect3 not applying translation
  • Add a new variant of mix() that takes the parameter t as a num or a vector
  • Large code reorganization to make it fit the 'dartblanklib' template

0.8.0 - June 10 2012

  • Inverse of 2x2,3x3,4x4 matrices
  • Inverse of upper 3x3 of a 4x4 matrix
  • Added zero, copy and raw specialized (and branchless) vector constructors
  • Added specialized copy matrix constructor
  • Added specialized rotation matrix constructors for mat2x2, mat3x3, and mat4x4
  • Added setRotation(num radians) to mat2x2
  • Added setRotationAround[x,y,z](num radians) to mat3x3 and mat4x4
  • Added buildPlaneVectors which constructs the spanning vectors given a plane normal
  • Added Adjoint of 2x2,3x3, and 4x4 matrices
  • Fixed many bugs in quaternion class
  • Fixed adjoint matrix code generation
  • Added selfAdd, selfSub, selfScale and selfNegate to matrix classes
  • Added serialization support for Float32Array and Vectors/Matrices

0.0.0 - March 20 2012

  • Initial release

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add vector_math
pubspec.yaml
name: vector_math
version: 2.1.3
description: A Vector Math library for 2D and 3D applications.
repository: https://github.com/google/vector_math.dart

environment:
  sdk: ">=2.14.0 <3.0.0"

dev_dependencies:
  benchmark_harness: ^2.0.0
  build_runner: ^2.0.0
  build_test: ^2.0.0
  build_web_compilers: ^3.0.0
  lints: ^1.0.0
  path: ^1.8.0
  test: ^1.16.0

Version history

VersionPublishedArchiveSHA-256
0.9.02012-10-18T18:49:46.598600ZDownload82a6be5175416758b0b52aeca25d87b69a2d148d2ad441f026199f08b55a08af
0.9.12012-11-08T20:10:10.597530ZDownload7db01791691e59a11d15dc4879331c74d969e02442d83a5dc67fc78b7bba76b3
0.9.22012-12-11T20:07:45.374350ZDownloadbaf0eef11206a1a6c79ee3ea8ed9604b97f6f14a6a381e0b71e2796484eff6be
0.9.32013-02-20T14:54:52.970670ZDownload7689ca3c7397665b179507fa1fae061fa14b572c9a3308313824ce16e94a04c4
0.9.42013-02-21T17:00:38.793310ZDownloadcad0aee49e8a6a66ca29fd09900e1ffe3eb762396fdbddaa95d08cb25d957257
0.9.52013-02-22T00:47:58.756840ZDownloadc16865fa2caccf1258b7d922d8960f06815930eb76915ac8852d7e662e974c43
0.9.62013-03-12T01:25:17.723340ZDownload46d879b9009037dcf0ec1d8192f32d6b8f9e111bd83f4d35ac71622ae374a04c
0.9.72013-03-17T19:02:07.243430ZDownload893f3f4e4e40fa6797299b4cd966521e492668282c133b676bb09e52e865aee2
1.1.02013-04-26T14:24:25.927ZDownload5ad5dd69dd45f24093b9bf05e06e54c4d991850fc554eb09ea53d26e44b99e18
1.3.02013-05-27T20:21:06.278590ZDownload1f7c52ef9d0c448c4982445c7c4cf3b792d0bfbe71d7f13da6322e904f82b270
1.3.12013-06-26T22:10:27.489130ZDownload91d86b758f2873aa2badadfe78f137dca9278c9d7e22858fa17ccc743d48865e
1.3.22013-06-28T23:40:40.102990ZDownloadf0009ec8d4b0ddca737e6a7bfe6afbee5e62a62fe9cf4ca8c3f458e23b843449
1.3.32013-07-12T17:24:46.182570ZDownload9d44787837a914b374b09a11c8db20477b9e8f2851180286ba560216b68e8b84
1.3.52013-07-19T21:25:57.736110ZDownload7432e57d63f76cb24a95c7d00fadcf688dc754e2b7ad43f69a57deb8b42d85bd
1.4.12014-01-07T15:43:59.300120ZDownloadff480189ffb5f129f06402c8ae89693b31d119004294cc4ab1b4caebe9d582e7
1.4.22014-01-24T15:57:12.843940ZDownload636753f12d78dbf8413969adade592d2c5c1a340d36e830e1d141a452c81ec72
1.4.2+12014-01-29T19:31:53.152020ZDownload30279f73d918729380ab5d485f4b54013cdd32d5de9df08ab2ebba7a458478e9
1.4.32014-02-25T00:39:47.506090ZDownloadcdd03d84f1f771ff5994ff32c097c537f3b6a65dc106dc2cdba17964f82fec33
1.4.42015-12-29T23:02:15.277ZDownload6ec3e64c988f4068952cc8f852e5a68593e68d988a10dd89f6734898fdda148f
1.4.52016-01-13T15:31:29.161ZDownload35b3bc05a03984608e6180e03a27ff63243c24239a00f9842a3c986129419877
1.4.62016-01-26T18:32:00.199ZDownloadfb2a6df7b14cede67268111072d77cb6d9c92297b4126f9e5b132264e4a8b94f
1.4.72016-02-01T15:01:05.975ZDownload13ee4ee3b1e4acb6ea820ab6a88d72ff5f0a36435e9f0ddb454ee1f6b5e3d088
2.0.02016-03-15T16:11:13.035ZDownloadfd1d3b111705104255b2be99653c0097910ce351b09efbb1618065bc1619b572
2.0.12016-04-25T20:08:01.791ZDownloada6a2bf1d49d216812bb5ee9791305adef9ad7f71af12179df0e4095621944648
2.0.22016-05-17T17:49:46.522ZDownload6b5e189467be26e35bc2edca9905207438ec6b960bfe628d0b5ab4ad80b22849
2.0.32016-05-17T17:51:13.894ZDownloadccb6d6bae309bee340e1316f7e2b4a1eaee247fb9cda529a31f7609fb4436a54
2.0.42017-02-06T22:06:37.739ZDownload5f795b6a1b110a059e4eb12c6acee528d91672b38851939a19af9cf5dcf76888
2.0.52017-07-06T19:30:40.867465ZDownloadc94e3c3faf38eccff63d0e162553f2897d40ea0c2a4b90f0fa85aaee994f24f3
2.0.62018-03-20T22:02:56.792393ZDownload1223db8bfd2626cf3f11c1b05c9b4fa50d56f63f17c62dae5270d87666051f26
2.0.72018-04-25T16:43:46.718584ZDownload0a4aa9bff3f0f3b15b01975a3cbdb7d87480283854a0d3626c9985654f767777
2.0.82018-07-23T15:59:51.797291ZDownloade7afd4f536333d8ecd3e94ba87580dbabdc26d4b296b6aaf420eb0170d334a4d
2.1.0-nullsafety2020-07-08T22:05:39.496944ZDownload2ae18f86d5372a60df2f609fcf11f4206c30063d8e6eb5b311f062f4d3c970ce
2.1.0-nullsafety.12020-07-17T17:34:22.829814ZDownload863b3add11a559024de3a1d270e25569532beaadf40cbc4df3f720f7a772a2f0
2.1.0-nullsafety.22020-07-22T15:45:45.914783ZDownload9b8050a88c95fb5c59837085834d951d324aaa1718eacb42ccfae474a56517e0
2.1.0-nullsafety.32020-09-22T15:39:35.086691ZDownload02835b2b5ca7cb8b1333eaf9ae982bd89cb54ae6ecbf928b1b5721afb9c40491
2.1.0-nullsafety.42020-10-23T21:36:50.368570ZDownload66292d20fe3367024f5e6dfca417bf607b75d9b41506d3d49e19b05742ae1dbc
2.1.0-nullsafety.52020-11-03T22:11:26.048199ZDownload5c5ce6f4a21f0c0e80d9c4584f28dd06d115e70e3e5eb86922bd97c50054c59f
2.1.02021-02-03T23:18:50.015302ZDownload15cdf04f64b23c2ff6b999c193cbb0c928698dc602ed73075b9562527fcb5bb5
2.1.12021-09-21T23:15:27.071654ZDownload9af8001345e669a41655776b98a4621a6cdf896ff9740dc98d81e38c5a68a776
2.1.22022-02-01T23:01:44.408099ZDownloadac2b8dd510af91a82235e6555c64d3a5154a61717cd7094272a61705851f9b06
2.1.32022-08-18T16:03:52.400762ZDownloadbcf21e8cb927e9db17527b36980cbfdac70aed245b6495aa2712f7566837c7b0
2.1.42022-09-21T17:42:10.626796ZDownload80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803
2.1.52025-04-15T21:06:53.003440ZDownloadef221bda8445650f9cdf9ab0c6bd9521c0608ba9db5816f7a1ca1cdae372c75f
2.2.02025-05-22T00:41:49.994903ZDownloadd530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b
2.3.02026-03-18T22:05:37.959330ZDownload47a1b32ee755c3fcffa33db52a7258c137f97bdb2209a1075be847809fac4ccf
2.4.02026-06-03T20:29:49.519513ZDownload1d774bbdf6b72a0b12122fc1560c9c2d2a67db5a4a4cc2bd8a5c990ab20e3188
2.4.12026-07-22T13:38:00.704618ZDownload99b6fb0d1881dd1302cd42e0f53bcce99ea246e0a85c8aeb79fb38b283852e88
2.4.22026-07-29T23:23:00.464489ZDownloadf36f9f3be64c6198714492bb455c11056e33e2f85d9a0b676a48301e44fdcf47
2.4.32026-09-18T23:20:34.571704ZDownload92b9910f66ed1057fd4da7b040ae7c74cafacf885bdc81be496928d5049b032d
2.4.42026-10-26T22:12:37.036663ZDownloadfa49e1674cb84acb900141714e6c6e1f9ae099827c24d141430ff6819db4e1bc

Dependencies

No direct dependencies declared.