/
vector.dart
131 lines (119 loc) · 3.75 KB
/
vector.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright (C) 2013 John McCutchan <john@johnmccutchan.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
part of vector_math;
/// Returns the dot product between vectors [x] and [y]. The dimension of [x] and [y] must match.
double dot(dynamic x, dynamic y) {
return x.dot(y);
}
/// Returns the length of vector [x]
double length(dynamic x) {
return x.length;
}
/// Returns the length squared of vector [x]
double length2(dynamic x) {
return x.length2;
}
/// Returns the distance between vectors [x] and [y]. The dimension of [x] and [y] must match.
double distance(dynamic x, dynamic y) {
return length(x - y);
}
/// Returns the distance squared between vectors [x] and [y].
double distance2(dynamic x, dynamic y) {
return length2(x - y);
}
/// Returns the cross product between [x] and [y]. [x] and [y] can be vec2, vec3 or double, but not all combinations are supported.
dynamic cross(dynamic x, dynamic y, [dynamic out=null]) {
if (x is vec3 && y is vec3) {
return x.cross(y, out);
} else if (x is vec2 && y is vec2) {
assert(out == null);
return x.cross(y);
} else if (x is num && y is vec2) {
x = x.toDouble();
if (out == null) {
out = new vec2.zero();
}
var tempy = x * y.x;
out.x = -x * y.y;
out.y = tempy;
return out;
} else if (x is vec2 && y is num) {
y = y.toDouble();
if (out == null) {
out = new vec2.zero();
}
var tempy = -y * x.x;
out.x = y * x.y;
out.y = tempy;
return out;
} else {
assert(false);
}
return null;
}
/// Returns [x] normalized. Supports [num], [vec2], [vec3], and [vec4] input types. The return type will match the type of [x]
dynamic normalize(dynamic x, [dynamic out=null]) {
if (x is num) {
return 1.0 * sign(x.toDouble());
}
if (x is vec2) {
if (out == null) {
out = new vec2.copy(x);
}
(x as vec2).normalize();
return out;
}
if (x is vec3) {
if (out == null) {
out = new vec3.copy(x);
}
(x as vec3).normalize();
return out;
}
if (x is vec4) {
if (out == null) {
out = new vec4.copy(x);
}
(x as vec4).normalize();
return out;
}
return null;
}
/// Sets [u] and [v] to be two vectors orthogonal to each other and [planeNormal]
void buildPlaneVectors(final vec3 planeNormal, vec3 u, vec3 v) {
if (planeNormal.z.abs() > _ScalerHelpers._sqrtOneHalf) {
// choose u in y-z plane
double a = planeNormal.y*planeNormal.y + planeNormal.z*planeNormal.z;
double k = 1.0/Math.sqrt(a);
u.x = 0.0;
u.y = -planeNormal.z*k;
u.z = planeNormal.y*k;
v.x = a*k;
v.y = -planeNormal[0]*(planeNormal[1]*k);
v.z = planeNormal[0]*(-planeNormal[2]*k);
} else {
// choose u in x-y plane
double a = planeNormal.x*planeNormal.x + planeNormal.y*planeNormal.y;
double k = 1.0/Math.sqrt(a);
u.x = -planeNormal[1]*k;
u.y = planeNormal[0]*k;
u.z = 0.0;
v.x = -planeNormal[2]*(planeNormal[0]*k);
v.y = planeNormal[2]*(-planeNormal[1]*k);
v.z = a*k;
}
}