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