1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.rs.vr.engine;
18
19/**
20 * Some minimal utilities for Vector math
21 */
22public class VectorUtil {
23    public static void sub(double[] a, double[] b, double[] out) {
24        out[0] = a[0] - b[0];
25        out[1] = a[1] - b[1];
26        out[2] = a[2] - b[2];
27    }
28
29    public static void mult(double[] a, double b, double[] out) {
30        out[0] = a[0] * b;
31        out[1] = a[1] * b;
32        out[2] = a[2] * b;
33    }
34
35    public static double dot(double[] a, double[] b) {
36        return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
37    }
38
39    public static double norm(double[] a) {
40        return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
41    }
42
43    public static void cross(double[] a, double[] b, double[] out) {
44        double out0 = a[1] * b[2] - b[1] * a[2];
45        double out1 = a[2] * b[0] - b[2] * a[0];
46        double out2 = a[0] * b[1] - b[0] * a[1];
47        out[0] = out0;
48        out[1] = out1;
49        out[2] = out2;
50    }
51
52    public static void normalize(double[] a) {
53        double norm = norm(a);
54        a[0] /= norm;
55        a[1] /= norm;
56        a[2] /= norm;
57    }
58
59    public static void add(double[] a, double[] b,
60                           double[] out) {
61        out[0] = a[0] + b[0];
62        out[1] = a[1] + b[1];
63        out[2] = a[2] + b[2];
64    }
65
66}
67