aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowLegacyMatrix.java
blob: 1401816dfe70968fc48b2a646347c73c53641bd2 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.KITKAT;
import static android.os.Build.VERSION_CODES.LOLLIPOP;

import android.graphics.Matrix;
import android.graphics.Matrix.ScaleToFit;
import android.graphics.PointF;
import android.graphics.RectF;
import java.awt.geom.AffineTransform;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(value = Matrix.class, isInAndroidSdk = false)
public class ShadowLegacyMatrix extends ShadowMatrix {

  private static final float EPSILON = 1e-3f;

  private final Deque<String> preOps = new ArrayDeque<>();
  private final Deque<String> postOps = new ArrayDeque<>();
  private final Map<String, String> setOps = new LinkedHashMap<>();

  private SimpleMatrix simpleMatrix = SimpleMatrix.newIdentityMatrix();

  @Implementation
  protected void __constructor__(Matrix src) {
    set(src);
  }

  /**
   * A list of all 'pre' operations performed on this Matrix. The last operation performed will be
   * first in the list.
   *
   * @return A list of all 'pre' operations performed on this Matrix.
   */
  @Override
  public List<String> getPreOperations() {
    return Collections.unmodifiableList(new ArrayList<>(preOps));
  }

  /**
   * A list of all 'post' operations performed on this Matrix. The last operation performed will be
   * last in the list.
   *
   * @return A list of all 'post' operations performed on this Matrix.
   */
  @Override
  public List<String> getPostOperations() {
    return Collections.unmodifiableList(new ArrayList<>(postOps));
  }

  /**
   * A map of all 'set' operations performed on this Matrix.
   *
   * @return A map of all 'set' operations performed on this Matrix.
   */
  @Override
  public Map<String, String> getSetOperations() {
    return Collections.unmodifiableMap(new LinkedHashMap<>(setOps));
  }

  @Implementation
  protected boolean isIdentity() {
    return simpleMatrix.equals(SimpleMatrix.IDENTITY);
  }

  @Implementation(minSdk = LOLLIPOP)
  protected boolean isAffine() {
    return simpleMatrix.isAffine();
  }

  @Implementation
  protected boolean rectStaysRect() {
    return simpleMatrix.rectStaysRect();
  }

  @Implementation
  protected void getValues(float[] values) {
    simpleMatrix.getValues(values);
  }

  @Implementation
  protected void setValues(float[] values) {
    simpleMatrix = new SimpleMatrix(values);
  }

  @Implementation
  protected void set(Matrix src) {
    reset();
    if (src != null) {
      ShadowLegacyMatrix shadowMatrix = Shadow.extract(src);
      preOps.addAll(shadowMatrix.preOps);
      postOps.addAll(shadowMatrix.postOps);
      setOps.putAll(shadowMatrix.setOps);
      simpleMatrix = new SimpleMatrix(getSimpleMatrix(src));
    }
  }

  @Implementation
  protected void reset() {
    preOps.clear();
    postOps.clear();
    setOps.clear();
    simpleMatrix = SimpleMatrix.newIdentityMatrix();
  }

  @Implementation
  protected void setTranslate(float dx, float dy) {
    setOps.put(TRANSLATE, dx + " " + dy);
    simpleMatrix = SimpleMatrix.translate(dx, dy);
  }

  @Implementation
  protected void setScale(float sx, float sy, float px, float py) {
    setOps.put(SCALE, sx + " " + sy + " " + px + " " + py);
    simpleMatrix = SimpleMatrix.scale(sx, sy, px, py);
  }

  @Implementation
  protected void setScale(float sx, float sy) {
    setOps.put(SCALE, sx + " " + sy);
    simpleMatrix = SimpleMatrix.scale(sx, sy);
  }

  @Implementation
  protected void setRotate(float degrees, float px, float py) {
    setOps.put(ROTATE, degrees + " " + px + " " + py);
    simpleMatrix = SimpleMatrix.rotate(degrees, px, py);
  }

  @Implementation
  protected void setRotate(float degrees) {
    setOps.put(ROTATE, Float.toString(degrees));
    simpleMatrix = SimpleMatrix.rotate(degrees);
  }

  @Implementation
  protected void setSinCos(float sinValue, float cosValue, float px, float py) {
    setOps.put(SINCOS, sinValue + " " + cosValue + " " + px + " " + py);
    simpleMatrix = SimpleMatrix.sinCos(sinValue, cosValue, px, py);
  }

  @Implementation
  protected void setSinCos(float sinValue, float cosValue) {
    setOps.put(SINCOS, sinValue + " " + cosValue);
    simpleMatrix = SimpleMatrix.sinCos(sinValue, cosValue);
  }

  @Implementation
  protected void setSkew(float kx, float ky, float px, float py) {
    setOps.put(SKEW, kx + " " + ky + " " + px + " " + py);
    simpleMatrix = SimpleMatrix.skew(kx, ky, px, py);
  }

  @Implementation
  protected void setSkew(float kx, float ky) {
    setOps.put(SKEW, kx + " " + ky);
    simpleMatrix = SimpleMatrix.skew(kx, ky);
  }

  @Implementation
  protected boolean setConcat(Matrix a, Matrix b) {
    simpleMatrix = getSimpleMatrix(a).multiply(getSimpleMatrix(b));
    return true;
  }

  @Implementation
  protected boolean preTranslate(float dx, float dy) {
    preOps.addFirst(TRANSLATE + " " + dx + " " + dy);
    return preConcat(SimpleMatrix.translate(dx, dy));
  }

  @Implementation
  protected boolean preScale(float sx, float sy, float px, float py) {
    preOps.addFirst(SCALE + " " + sx + " " + sy + " " + px + " " + py);
    return preConcat(SimpleMatrix.scale(sx, sy, px, py));
  }

  @Implementation
  protected boolean preScale(float sx, float sy) {
    preOps.addFirst(SCALE + " " + sx + " " + sy);
    return preConcat(SimpleMatrix.scale(sx, sy));
  }

  @Implementation
  protected boolean preRotate(float degrees, float px, float py) {
    preOps.addFirst(ROTATE + " " + degrees + " " + px + " " + py);
    return preConcat(SimpleMatrix.rotate(degrees, px, py));
  }

  @Implementation
  protected boolean preRotate(float degrees) {
    preOps.addFirst(ROTATE + " " + Float.toString(degrees));
    return preConcat(SimpleMatrix.rotate(degrees));
  }

  @Implementation
  protected boolean preSkew(float kx, float ky, float px, float py) {
    preOps.addFirst(SKEW + " " + kx + " " + ky + " " + px + " " + py);
    return preConcat(SimpleMatrix.skew(kx, ky, px, py));
  }

  @Implementation
  protected boolean preSkew(float kx, float ky) {
    preOps.addFirst(SKEW + " " + kx + " " + ky);
    return preConcat(SimpleMatrix.skew(kx, ky));
  }

  @Implementation
  protected boolean preConcat(Matrix other) {
    preOps.addFirst(MATRIX + " " + other);
    return preConcat(getSimpleMatrix(other));
  }

  @Implementation
  protected boolean postTranslate(float dx, float dy) {
    postOps.addLast(TRANSLATE + " " + dx + " " + dy);
    return postConcat(SimpleMatrix.translate(dx, dy));
  }

  @Implementation
  protected boolean postScale(float sx, float sy, float px, float py) {
    postOps.addLast(SCALE + " " + sx + " " + sy + " " + px + " " + py);
    return postConcat(SimpleMatrix.scale(sx, sy, px, py));
  }

  @Implementation
  protected boolean postScale(float sx, float sy) {
    postOps.addLast(SCALE + " " + sx + " " + sy);
    return postConcat(SimpleMatrix.scale(sx, sy));
  }

  @Implementation
  protected boolean postRotate(float degrees, float px, float py) {
    postOps.addLast(ROTATE + " " + degrees + " " + px + " " + py);
    return postConcat(SimpleMatrix.rotate(degrees, px, py));
  }

  @Implementation
  protected boolean postRotate(float degrees) {
    postOps.addLast(ROTATE + " " + Float.toString(degrees));
    return postConcat(SimpleMatrix.rotate(degrees));
  }

  @Implementation
  protected boolean postSkew(float kx, float ky, float px, float py) {
    postOps.addLast(SKEW + " " + kx + " " + ky + " " + px + " " + py);
    return postConcat(SimpleMatrix.skew(kx, ky, px, py));
  }

  @Implementation
  protected boolean postSkew(float kx, float ky) {
    postOps.addLast(SKEW + " " + kx + " " + ky);
    return postConcat(SimpleMatrix.skew(kx, ky));
  }

  @Implementation
  protected boolean postConcat(Matrix other) {
    postOps.addLast(MATRIX + " " + other);
    return postConcat(getSimpleMatrix(other));
  }

  @Implementation
  protected boolean invert(Matrix inverse) {
    final SimpleMatrix inverseMatrix = simpleMatrix.invert();
    if (inverseMatrix != null) {
      if (inverse != null) {
        final ShadowLegacyMatrix shadowInverse = Shadow.extract(inverse);
        shadowInverse.simpleMatrix = inverseMatrix;
      }
      return true;
    }
    return false;
  }

  boolean hasPerspective() {
    return (simpleMatrix.mValues[6] != 0 || simpleMatrix.mValues[7] != 0 || simpleMatrix.mValues[8] != 1);
  }

  protected AffineTransform getAffineTransform() {
    // the AffineTransform constructor takes the value in a different order
    // for a matrix [ 0 1 2 ]
    //              [ 3 4 5 ]
    // the order is 0, 3, 1, 4, 2, 5...
    return new AffineTransform(
        simpleMatrix.mValues[0],
        simpleMatrix.mValues[3],
        simpleMatrix.mValues[1],
        simpleMatrix.mValues[4],
        simpleMatrix.mValues[2],
        simpleMatrix.mValues[5]);
  }

  public PointF mapPoint(float x, float y) {
    return simpleMatrix.transform(new PointF(x, y));
  }

  public PointF mapPoint(PointF point) {
    return simpleMatrix.transform(point);
  }

  @Implementation
  protected boolean mapRect(RectF destination, RectF source) {
    final PointF leftTop = mapPoint(source.left, source.top);
    final PointF rightBottom = mapPoint(source.right, source.bottom);
    destination.set(
        Math.min(leftTop.x, rightBottom.x),
        Math.min(leftTop.y, rightBottom.y),
        Math.max(leftTop.x, rightBottom.x),
        Math.max(leftTop.y, rightBottom.y));
    return true;
  }

  @Implementation
  protected void mapPoints(float[] dst, int dstIndex, float[] src, int srcIndex, int pointCount) {
    for (int i = 0; i < pointCount; i++) {
      final PointF mapped = mapPoint(src[srcIndex + i * 2], src[srcIndex + i * 2 + 1]);
      dst[dstIndex + i * 2] = mapped.x;
      dst[dstIndex + i * 2 + 1] = mapped.y;
    }
  }

  @Implementation
  protected void mapVectors(float[] dst, int dstIndex, float[] src, int srcIndex, int vectorCount) {
    final float transX = simpleMatrix.mValues[Matrix.MTRANS_X];
    final float transY = simpleMatrix.mValues[Matrix.MTRANS_Y];

    simpleMatrix.mValues[Matrix.MTRANS_X] = 0;
    simpleMatrix.mValues[Matrix.MTRANS_Y] = 0;

    for (int i = 0; i < vectorCount; i++) {
      final PointF mapped = mapPoint(src[srcIndex + i * 2], src[srcIndex + i * 2 + 1]);
      dst[dstIndex + i * 2] = mapped.x;
      dst[dstIndex + i * 2 + 1] = mapped.y;
    }

    simpleMatrix.mValues[Matrix.MTRANS_X] = transX;
    simpleMatrix.mValues[Matrix.MTRANS_Y] = transY;
  }

  @Implementation
  protected float mapRadius(float radius) {
    float[] src = new float[] {radius, 0.f, 0.f, radius};
    mapVectors(src, 0, src, 0, 2);

    float l1 = (float) Math.hypot(src[0], src[1]);
    float l2 = (float) Math.hypot(src[2], src[3]);
    return (float) Math.sqrt(l1 * l2);
  }

  @Implementation
  protected boolean setRectToRect(RectF src, RectF dst, Matrix.ScaleToFit stf) {
    if (src.isEmpty()) {
      reset();
      return false;
    }
    return simpleMatrix.setRectToRect(src, dst, stf);
  }

  @Implementation
  @Override
  public boolean equals(Object obj) {
    if (obj instanceof Matrix) {
        return getSimpleMatrix(((Matrix) obj)).equals(simpleMatrix);
    } else {
        return obj instanceof ShadowMatrix && obj.equals(simpleMatrix);
    }
  }

  @Implementation(minSdk = KITKAT)
  @Override
  public int hashCode() {
      return Objects.hashCode(simpleMatrix);
  }

  @Override
  public String getDescription() {
    return "Matrix[pre=" + preOps + ", set=" + setOps + ", post=" + postOps + "]";
  }

  private static SimpleMatrix getSimpleMatrix(Matrix matrix) {
    final ShadowLegacyMatrix otherMatrix = Shadow.extract(matrix);
    return otherMatrix.simpleMatrix;
  }

  private boolean postConcat(SimpleMatrix matrix) {
    simpleMatrix = matrix.multiply(simpleMatrix);
    return true;
  }

  private boolean preConcat(SimpleMatrix matrix) {
    simpleMatrix = simpleMatrix.multiply(matrix);
    return true;
  }

  /**
   * A simple implementation of an immutable matrix.
   */
  private static class SimpleMatrix {
    private static final SimpleMatrix IDENTITY = newIdentityMatrix();

    private static SimpleMatrix newIdentityMatrix() {
      return new SimpleMatrix(
          new float[] {
            1.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 1.0f,
          });
    }

    private final float[] mValues;

    SimpleMatrix(SimpleMatrix matrix) {
      mValues = Arrays.copyOf(matrix.mValues, matrix.mValues.length);
    }

    private SimpleMatrix(float[] values) {
      if (values.length < 9) {
        throw new ArrayIndexOutOfBoundsException();
      }
      mValues = Arrays.copyOf(values, 9);
    }

    public boolean isAffine() {
      return mValues[6] == 0.0f && mValues[7] == 0.0f && mValues[8] == 1.0f;
    }

    public boolean rectStaysRect() {
      final float m00 = mValues[0];
      final float m01 = mValues[1];
      final float m10 = mValues[3];
      final float m11 = mValues[4];
      return (m00 == 0 && m11 == 0 && m01 != 0 && m10 != 0) || (m00 != 0 && m11 != 0 && m01 == 0 && m10 == 0);
    }

    public void getValues(float[] values) {
      if (values.length < 9) {
        throw new ArrayIndexOutOfBoundsException();
      }
      System.arraycopy(mValues, 0, values, 0, 9);
    }

    public static SimpleMatrix translate(float dx, float dy) {
      return new SimpleMatrix(new float[] {
          1.0f, 0.0f, dx,
          0.0f, 1.0f, dy,
          0.0f, 0.0f, 1.0f,
      });
    }

    public static SimpleMatrix scale(float sx, float sy, float px, float py) {
      return new SimpleMatrix(new float[] {
          sx,   0.0f, px * (1 - sx),
          0.0f, sy,   py * (1 - sy),
          0.0f, 0.0f, 1.0f,
      });
    }

    public static SimpleMatrix scale(float sx, float sy) {
      return new SimpleMatrix(new float[] {
          sx,   0.0f, 0.0f,
          0.0f, sy,   0.0f,
          0.0f, 0.0f, 1.0f,
      });
    }

    public static SimpleMatrix rotate(float degrees, float px, float py) {
      final double radians = Math.toRadians(degrees);
      final float sin = (float) Math.sin(radians);
      final float cos = (float) Math.cos(radians);
      return sinCos(sin, cos, px, py);
    }

    public static SimpleMatrix rotate(float degrees) {
      final double radians = Math.toRadians(degrees);
      final float sin = (float) Math.sin(radians);
      final float cos = (float) Math.cos(radians);
      return sinCos(sin, cos);
    }

    public static SimpleMatrix sinCos(float sin, float cos, float px, float py) {
      return new SimpleMatrix(new float[] {
          cos,  -sin, sin * py + (1 - cos) * px,
          sin,  cos,  -sin * px + (1 - cos) * py,
          0.0f, 0.0f, 1.0f,
      });
    }

    public static SimpleMatrix sinCos(float sin, float cos) {
      return new SimpleMatrix(new float[] {
          cos,  -sin, 0.0f,
          sin,  cos,  0.0f,
          0.0f, 0.0f, 1.0f,
      });
    }

    public static SimpleMatrix skew(float kx, float ky, float px, float py) {
      return new SimpleMatrix(new float[] {
          1.0f, kx,   -kx * py,
          ky,   1.0f, -ky * px,
          0.0f, 0.0f, 1.0f,
      });
    }

    public static SimpleMatrix skew(float kx, float ky) {
      return new SimpleMatrix(new float[] {
          1.0f, kx,   0.0f,
          ky,   1.0f, 0.0f,
          0.0f, 0.0f, 1.0f,
      });
    }

    public SimpleMatrix multiply(SimpleMatrix matrix) {
      final float[] values = new float[9];
      for (int i = 0; i < values.length; ++i) {
        final int row = i / 3;
        final int col = i % 3;
        for (int j = 0; j < 3; ++j) {
          values[i] += mValues[row * 3 + j] * matrix.mValues[j * 3 + col];
        }
      }
      return new SimpleMatrix(values);
    }

    public SimpleMatrix invert() {
      final float invDet = inverseDeterminant();
      if (invDet == 0) {
        return null;
      }

      final float[] src = mValues;
      final float[] dst = new float[9];
      dst[0] = cross_scale(src[4], src[8], src[5], src[7], invDet);
      dst[1] = cross_scale(src[2], src[7], src[1], src[8], invDet);
      dst[2] = cross_scale(src[1], src[5], src[2], src[4], invDet);

      dst[3] = cross_scale(src[5], src[6], src[3], src[8], invDet);
      dst[4] = cross_scale(src[0], src[8], src[2], src[6], invDet);
      dst[5] = cross_scale(src[2], src[3], src[0], src[5], invDet);

      dst[6] = cross_scale(src[3], src[7], src[4], src[6], invDet);
      dst[7] = cross_scale(src[1], src[6], src[0], src[7], invDet);
      dst[8] = cross_scale(src[0], src[4], src[1], src[3], invDet);
      return new SimpleMatrix(dst);
    }

    public PointF transform(PointF point) {
      return new PointF(
          point.x * mValues[0] + point.y * mValues[1] + mValues[2],
          point.x * mValues[3] + point.y * mValues[4] + mValues[5]);
    }

    // See: https://android.googlesource.com/platform/frameworks/base/+/6fca81de9b2079ec88e785f58bf49bf1f0c105e2/tools/layoutlib/bridge/src/android/graphics/Matrix_Delegate.java
    protected boolean setRectToRect(RectF src, RectF dst, ScaleToFit stf) {
      if (dst.isEmpty()) {
        mValues[0] =
            mValues[1] =
                mValues[2] = mValues[3] = mValues[4] = mValues[5] = mValues[6] = mValues[7] = 0;
        mValues[8] = 1;
      } else {
        float tx = dst.width() / src.width();
        float sx = dst.width() / src.width();
        float ty = dst.height() / src.height();
        float sy = dst.height() / src.height();
        boolean xLarger = false;

        if (stf != ScaleToFit.FILL) {
          if (sx > sy) {
            xLarger = true;
            sx = sy;
          } else {
            sy = sx;
          }
        }

        tx = dst.left - src.left * sx;
        ty = dst.top - src.top * sy;
        if (stf == ScaleToFit.CENTER || stf == ScaleToFit.END) {
          float diff;

          if (xLarger) {
            diff = dst.width() - src.width() * sy;
          } else {
            diff = dst.height() - src.height() * sy;
          }

          if (stf == ScaleToFit.CENTER) {
            diff = diff / 2;
          }

          if (xLarger) {
            tx += diff;
          } else {
            ty += diff;
          }
        }

        mValues[0] = sx;
        mValues[4] = sy;
        mValues[2] = tx;
        mValues[5] = ty;
        mValues[1] = mValues[3] = mValues[6] = mValues[7] = 0;
      }
      // shared cleanup
      mValues[8] = 1;
      return true;
    }

    @Override
    public boolean equals(Object o) {
      return this == o || (o instanceof SimpleMatrix && equals((SimpleMatrix) o));
    }

    @SuppressWarnings("NonOverridingEquals")
    public boolean equals(SimpleMatrix matrix) {
      if (matrix == null) {
        return false;
      }
      for (int i = 0; i < mValues.length; i++) {
        if (!isNearlyZero(matrix.mValues[i] - mValues[i])) {
          return false;
        }
      }
      return true;
    }

    @Override
    public int hashCode() {
      return Arrays.hashCode(mValues);
    }

    private static boolean isNearlyZero(float value) {
      return Math.abs(value) < EPSILON;
    }

    private static float cross(float a, float b, float c, float d) {
      return a * b - c * d;
    }

    private static float cross_scale(float a, float b, float c, float d, float scale) {
      return cross(a, b, c, d) * scale;
    }

    private float inverseDeterminant() {
      final float determinant = mValues[0] * cross(mValues[4], mValues[8], mValues[5], mValues[7]) +
          mValues[1] * cross(mValues[5], mValues[6], mValues[3], mValues[8]) +
          mValues[2] * cross(mValues[3], mValues[7], mValues[4], mValues[6]);
      return isNearlyZero(determinant) ? 0.0f : 1.0f / determinant;
    }
  }
}