1/*
2 * Copyright (C) 2013 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 android.renderscript;
18
19/**
20 * Vector version of the basic double type.
21 * Provides two double fields packed.
22 */
23public class Double2 {
24    public double x;
25    public double y;
26
27    public Double2() {
28    }
29
30    /** @hide */
31    public Double2(Double2 data) {
32        this.x = data.x;
33        this.y = data.y;
34    }
35
36    public Double2(double x, double y) {
37        this.x = x;
38        this.y = y;
39    }
40
41    /** @hide
42     * Vector add
43     *
44     * @param a
45     * @param b
46     * @return
47     */
48    public static Double2 add(Double2 a, Double2 b) {
49        Double2 res = new Double2();
50        res.x = a.x + b.x;
51        res.y = a.y + b.y;
52
53        return res;
54    }
55
56    /** @hide
57     * Vector add
58     *
59     * @param value
60     */
61    public void add(Double2 value) {
62        x += value.x;
63        y += value.y;
64    }
65
66    /** @hide
67     * Vector add
68     *
69     * @param value
70     */
71    public void add(double value) {
72        x += value;
73        y += value;
74    }
75
76    /** @hide
77     * Vector add
78     *
79     * @param a
80     * @param b
81     * @return
82     */
83    public static Double2 add(Double2 a, double b) {
84        Double2 res = new Double2();
85        res.x = a.x + b;
86        res.y = a.y + b;
87
88        return res;
89    }
90
91    /** @hide
92     * Vector subtraction
93     *
94     * @param value
95     */
96    public void sub(Double2 value) {
97        x -= value.x;
98        y -= value.y;
99    }
100
101    /** @hide
102     * Vector subtraction
103     *
104     * @param a
105     * @param b
106     * @return
107     */
108    public static Double2 sub(Double2 a, Double2 b) {
109        Double2 res = new Double2();
110        res.x = a.x - b.x;
111        res.y = a.y - b.y;
112
113        return res;
114    }
115
116    /** @hide
117     * Vector subtraction
118     *
119     * @param value
120     */
121    public void sub(double value) {
122        x -= value;
123        y -= value;
124    }
125
126    /** @hide
127     * Vector subtraction
128     *
129     * @param a
130     * @param b
131     * @return
132     */
133    public static Double2 sub(Double2 a, double b) {
134        Double2 res = new Double2();
135        res.x = a.x - b;
136        res.y = a.y - b;
137
138        return res;
139    }
140
141    /** @hide
142     * Vector multiplication
143     *
144     * @param value
145     */
146    public void mul(Double2 value) {
147        x *= value.x;
148        y *= value.y;
149    }
150
151    /** @hide
152     * Vector multiplication
153     *
154     * @param a
155     * @param b
156     * @return
157     */
158    public static Double2 mul(Double2 a, Double2 b) {
159        Double2 res = new Double2();
160        res.x = a.x * b.x;
161        res.y = a.y * b.y;
162
163        return res;
164    }
165
166    /** @hide
167     * Vector multiplication
168     *
169     * @param value
170     */
171    public void mul(double value) {
172        x *= value;
173        y *= value;
174    }
175
176    /** @hide
177     * Vector multiplication
178     *
179     * @param a
180     * @param b
181     * @return
182     */
183    public static Double2 mul(Double2 a, double b) {
184        Double2 res = new Double2();
185        res.x = a.x * b;
186        res.y = a.y * b;
187
188        return res;
189    }
190
191    /** @hide
192     * Vector division
193     *
194     * @param value
195     */
196    public void div(Double2 value) {
197        x /= value.x;
198        y /= value.y;
199    }
200
201    /** @hide
202     * Vector division
203     *
204     * @param a
205     * @param b
206     * @return
207     */
208    public static Double2 div(Double2 a, Double2 b) {
209        Double2 res = new Double2();
210        res.x = a.x / b.x;
211        res.y = a.y / b.y;
212
213        return res;
214    }
215
216    /** @hide
217     * Vector division
218     *
219     * @param value
220     */
221    public void div(double value) {
222        x /= value;
223        y /= value;
224    }
225
226    /** @hide
227     * Vector division
228     *
229     * @param a
230     * @param b
231     * @return
232     */
233    public static Double2 div(Double2 a, double b) {
234        Double2 res = new Double2();
235        res.x = a.x / b;
236        res.y = a.y / b;
237
238        return res;
239    }
240
241    /** @hide
242     * Vector dot Product
243     *
244     * @param a
245     * @return
246     */
247    public double dotProduct(Double2 a) {
248        return (x * a.x) + (y * a.y);
249    }
250
251    /** @hide
252     * Vector dot Product
253     *
254     * @param a
255     * @param b
256     * @return
257     */
258    public static Double dotProduct(Double2 a, Double2 b) {
259        return (b.x * a.x) + (b.y * a.y);
260    }
261
262    /** @hide
263     * Vector add Multiple
264     *
265     * @param a
266     * @param factor
267     */
268    public void addMultiple(Double2 a, double factor) {
269        x += a.x * factor;
270        y += a.y * factor;
271    }
272
273    /** @hide
274     * Set vector value by double2
275     *
276     * @param a
277     */
278    public void set(Double2 a) {
279        this.x = a.x;
280        this.y = a.y;
281    }
282
283    /** @hide
284     * Set vector negate
285     */
286    public void negate() {
287        x = -x;
288        y = -y;
289    }
290
291    /** @hide
292     * Get vector length
293     *
294     * @return
295     */
296    public int length() {
297        return 2;
298    }
299
300    /** @hide
301     * Return the element sum of vector
302     *
303     * @return
304     */
305    public double elementSum() {
306        return x + y;
307    }
308
309    /** @hide
310     * Get the vector field value by index
311     *
312     * @param i
313     * @return
314     */
315    public double get(int i) {
316        switch (i) {
317        case 0:
318            return x;
319        case 1:
320            return y;
321        default:
322            throw new IndexOutOfBoundsException("Index: i");
323        }
324    }
325
326    /** @hide
327     * Set the vector field value by index
328     *
329     * @param i
330     * @param value
331     */
332    public void setAt(int i, double value) {
333        switch (i) {
334        case 0:
335            x = value;
336            return;
337        case 1:
338            y = value;
339            return;
340        default:
341            throw new IndexOutOfBoundsException("Index: i");
342        }
343    }
344
345    /** @hide
346     * Add the vector field value by index
347     *
348     * @param i
349     * @param value
350     */
351    public void addAt(int i, double value) {
352        switch (i) {
353        case 0:
354            x += value;
355            return;
356        case 1:
357            y += value;
358            return;
359        default:
360            throw new IndexOutOfBoundsException("Index: i");
361        }
362    }
363
364    /** @hide
365     * Set the vector field value
366     *
367     * @param x
368     * @param y
369     */
370    public void setValues(double x, double y) {
371        this.x = x;
372        this.y = y;
373    }
374
375    /** @hide
376     * Copy the vector to double array
377     *
378     * @param data
379     * @param offset
380     */
381    public void copyTo(double[] data, int offset) {
382        data[offset] = x;
383        data[offset + 1] = y;
384    }
385}
386