• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / vector_math.dart / 17181727464

04 Aug 2025 07:19PM UTC coverage: 26.702% (+0.3%) from 26.388%
17181727464

push

github

web-flow
Bump min SDK to 3.7, update dependencies, reformat (#348)

496 of 1182 new or added lines in 55 files covered. (41.96%)

18 existing lines in 8 files now uncovered.

4463 of 16714 relevant lines covered (26.7%)

1.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.63
/lib/src/vector_math/ray.dart
1
// Copyright (c) 2015, Google Inc. Please see the AUTHORS file for details.
2
// All rights reserved. Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4

5
part of '../../vector_math.dart';
6

7
/// Defines a [Ray] by an [origin] and a [direction].
8
class Ray {
9
  final Vector3 _origin;
10
  final Vector3 _direction;
11

12
  /// The [origin] of the ray.
13
  Vector3 get origin => _origin;
×
14

15
  /// The [direction] of the ray.
16
  Vector3 get direction => _direction;
×
17

18
  /// Create a new, uninitialized ray.
NEW
19
  Ray() : _origin = Vector3.zero(), _direction = Vector3.zero();
×
20

21
  /// Create a ray as a copy of [other].
22
  Ray.copy(Ray other)
×
NEW
23
    : _origin = Vector3.copy(other._origin),
×
NEW
24
      _direction = Vector3.copy(other._direction);
×
25

26
  /// Create a ray with an [origin] and a [direction].
27
  Ray.originDirection(Vector3 origin, Vector3 direction)
2✔
28
    : _origin = Vector3.copy(origin),
2✔
29
      _direction = Vector3.copy(direction);
2✔
30

31
  /// Copy the [origin] and [direction] from [other] into this.
32
  void copyFrom(Ray other) {
×
33
    _origin.setFrom(other._origin);
×
34
    _direction.setFrom(other._direction);
×
35
  }
36

37
  /// Returns the position on this with a distance of [t] from [origin].
38
  Vector3 at(double t) => _direction.scaled(t)..add(_origin);
10✔
39

40
  /// Copy the position on this with a distance of [t] from [origin] into
41
  /// [other].
42
  void copyAt(Vector3 other, double t) {
2✔
43
    other
44
      ..setFrom(_direction)
4✔
45
      ..scale(t)
2✔
46
      ..add(_origin);
4✔
47
  }
48

49
  /// Return the distance from the origin of this to the intersection with
50
  /// [other] if this intersects with [other], or null if the don't intersect.
51
  double? intersectsWithSphere(Sphere other) {
1✔
52
    final r = other.radius;
1✔
53
    final r2 = r * r;
1✔
54
    final l = other._center.clone()..sub(_origin);
4✔
55
    final s = l.dot(_direction);
2✔
56
    final l2 = l.dot(l);
1✔
57
    if (s < 0 && l2 > r2) {
2✔
58
      return null;
59
    }
60
    final m2 = l2 - s * s;
2✔
61
    if (m2 > r2) {
1✔
62
      return null;
63
    }
64
    final q = math.sqrt(r2 - m2);
2✔
65

66
    return (l2 > r2) ? s - q : s + q;
3✔
67
  }
68

69
  // Some varaibles that are used for intersectsWithTriangle and
70
  // intersectsWithQuad. The performance is better in Dart and JS if we avoid
71
  // to create temporary instance over and over. Also reduce GC.
72
  static final _e1 = Vector3.zero();
3✔
73
  static final _e2 = Vector3.zero();
3✔
74
  static final _q = Vector3.zero();
3✔
75
  static final _s = Vector3.zero();
3✔
76
  static final _r = Vector3.zero();
3✔
77

78
  /// Return the distance from the origin of this to the intersection with
79
  /// [other] if this intersects with [other], or null if the don't intersect.
80
  double? intersectsWithTriangle(Triangle other) {
1✔
81
    const epsilon = 10e-6;
82

83
    final point0 = other._point0;
1✔
84
    final point1 = other._point1;
1✔
85
    final point2 = other._point2;
1✔
86

87
    _e1
1✔
88
      ..setFrom(point1)
1✔
89
      ..sub(point0);
1✔
90
    _e2
1✔
91
      ..setFrom(point2)
1✔
92
      ..sub(point0);
1✔
93

94
    _direction.crossInto(_e2, _q);
4✔
95
    final a = _e1.dot(_q);
3✔
96

97
    if (a > -epsilon && a < epsilon) {
3✔
98
      return null;
99
    }
100

101
    final f = 1 / a;
1✔
102
    _s
1✔
103
      ..setFrom(_origin)
2✔
104
      ..sub(point0);
1✔
105
    final u = f * (_s.dot(_q));
4✔
106

107
    if (u < 0.0) {
1✔
108
      return null;
109
    }
110

111
    _s.crossInto(_e1, _r);
4✔
112
    final v = f * (_direction.dot(_r));
4✔
113

114
    if (v < -epsilon || u + v > 1.0 + epsilon) {
5✔
115
      return null;
116
    }
117

118
    final t = f * (_e2.dot(_r));
4✔
119

120
    return t;
121
  }
122

123
  /// Return the distance from the origin of this to the intersection with
124
  /// [other] if this intersects with [other], or null if the don't intersect.
125
  double? intersectsWithQuad(Quad other) {
×
126
    const epsilon = 10e-6;
127

128
    // First triangle
129
    var point0 = other._point0;
×
130
    var point1 = other._point1;
×
131
    var point2 = other._point2;
×
132

133
    _e1
×
134
      ..setFrom(point1)
×
135
      ..sub(point0);
×
136
    _e2
×
137
      ..setFrom(point2)
×
138
      ..sub(point0);
×
139

140
    _direction.crossInto(_e2, _q);
×
141
    final a0 = _e1.dot(_q);
×
142

143
    if (!(a0 > -epsilon && a0 < epsilon)) {
×
144
      final f = 1 / a0;
×
145
      _s
×
146
        ..setFrom(_origin)
×
147
        ..sub(point0);
×
148
      final u = f * (_s.dot(_q));
×
149

150
      if (u >= 0.0) {
×
151
        _s.crossInto(_e1, _r);
×
152
        final v = f * (_direction.dot(_r));
×
153

154
        if (!(v < -epsilon || u + v > 1.0 + epsilon)) {
×
155
          final t = f * (_e2.dot(_r));
×
156

157
          return t;
158
        }
159
      }
160
    }
161

162
    // Second triangle
163
    point0 = other._point3;
×
164
    point1 = other._point0;
×
165
    point2 = other._point2;
×
166

167
    _e1
×
168
      ..setFrom(point1)
×
169
      ..sub(point0);
×
170
    _e2
×
171
      ..setFrom(point2)
×
172
      ..sub(point0);
×
173

174
    _direction.crossInto(_e2, _q);
×
175
    final a1 = _e1.dot(_q);
×
176

177
    if (!(a1 > -epsilon && a1 < epsilon)) {
×
178
      final f = 1 / a1;
×
179
      _s
×
180
        ..setFrom(_origin)
×
181
        ..sub(point0);
×
182
      final u = f * (_s.dot(_q));
×
183

184
      if (u >= 0.0) {
×
185
        _s.crossInto(_e1, _r);
×
186
        final v = f * (_direction.dot(_r));
×
187

188
        if (!(v < -epsilon || u + v > 1.0 + epsilon)) {
×
189
          final t = f * (_e2.dot(_r));
×
190

191
          return t;
192
        }
193
      }
194
    }
195

196
    return null;
197
  }
198

199
  /// Return the distance from the origin of this to the intersection with
200
  /// [other] if this intersects with [other], or null if the don't intersect.
201
  double? intersectsWithAabb3(Aabb3 other) {
1✔
202
    final otherMin = other.min;
1✔
203
    final otherMax = other.max;
1✔
204

205
    var tNear = -double.maxFinite;
1✔
206
    var tFar = double.maxFinite;
207

208
    for (var i = 0; i < 3; ++i) {
2✔
209
      if (_direction[i] == 0.0) {
3✔
210
        if (_origin[i] < otherMin[i] || _origin[i] > otherMax[i]) {
8✔
211
          return null;
212
        }
213
      } else {
214
        var t1 = (otherMin[i] - _origin[i]) / _direction[i];
7✔
215
        var t2 = (otherMax[i] - _origin[i]) / _direction[i];
7✔
216

217
        if (t1 > t2) {
1✔
218
          final temp = t1;
219
          t1 = t2;
220
          t2 = temp;
221
        }
222

223
        if (t1 > tNear) {
1✔
224
          tNear = t1;
225
        }
226

227
        if (t2 < tFar) {
1✔
228
          tFar = t2;
229
        }
230

231
        if (tNear > tFar || tFar < 0) {
2✔
232
          return null;
233
        }
234
      }
235
    }
236

237
    return tNear;
238
  }
239
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc