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