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 int type.
21 * Provides three int fields packed.
22 */
23public class Int3 {
24    public int x;
25    public int y;
26    public int z;
27
28    public Int3() {
29    }
30
31    /** @hide */
32    public Int3(int i) {
33        this.x = this.y = this.z = i;
34    }
35
36    public Int3(int x, int y, int z) {
37        this.x = x;
38        this.y = y;
39        this.z = z;
40    }
41
42    /** @hide */
43    public Int3(Int3 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     */
54    public void add(Int3 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     */
67    public static Int3 add(Int3 a, Int3 b) {
68        Int3 result = new Int3();
69        result.x = a.x + b.x;
70        result.y = a.y + b.y;
71        result.z = a.z + b.z;
72
73        return result;
74    }
75
76    /** @hide
77     * Vector add
78     *
79     * @param value
80     */
81    public void add(int 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     */
94    public static Int3 add(Int3 a, int b) {
95        Int3 result = new Int3();
96        result.x = a.x + b;
97        result.y = a.y + b;
98        result.z = a.z + b;
99
100        return result;
101    }
102
103    /** @hide
104     * Vector subtraction
105     *
106     * @param a
107     */
108    public void sub(Int3 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     */
121    public static Int3 sub(Int3 a, Int3 b) {
122        Int3 result = new Int3();
123        result.x = a.x - b.x;
124        result.y = a.y - b.y;
125        result.z = a.z - b.z;
126
127        return result;
128    }
129
130    /** @hide
131     * Vector subtraction
132     *
133     * @param value
134     */
135    public void sub(int 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     */
148    public static Int3 sub(Int3 a, int b) {
149        Int3 result = new Int3();
150        result.x = a.x - b;
151        result.y = a.y - b;
152        result.z = a.z - b;
153
154        return result;
155    }
156
157    /** @hide
158     * Vector multiplication
159     *
160     * @param a
161     */
162    public void mul(Int3 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     */
175    public static Int3 mul(Int3 a, Int3 b) {
176        Int3 result = new Int3();
177        result.x = a.x * b.x;
178        result.y = a.y * b.y;
179        result.z = a.z * b.z;
180
181        return result;
182    }
183
184    /** @hide
185     * Vector multiplication
186     *
187     * @param value
188     */
189    public void mul(int 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     */
202    public static Int3 mul(Int3 a, int b) {
203        Int3 result = new Int3();
204        result.x = a.x * b;
205        result.y = a.y * b;
206        result.z = a.z * b;
207
208        return result;
209    }
210
211    /** @hide
212     * Vector division
213     *
214     * @param a
215     */
216    public void div(Int3 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     */
229    public static Int3 div(Int3 a, Int3 b) {
230        Int3 result = new Int3();
231        result.x = a.x / b.x;
232        result.y = a.y / b.y;
233        result.z = a.z / b.z;
234
235        return result;
236    }
237
238    /** @hide
239     * Vector division
240     *
241     * @param value
242     */
243    public void div(int 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     */
256    public static Int3 div(Int3 a, int b) {
257        Int3 result = new Int3();
258        result.x = a.x / b;
259        result.y = a.y / b;
260        result.z = a.z / b;
261
262        return result;
263    }
264
265    /** @hide
266     * Vector Modulo
267     *
268     * @param a
269     */
270    public void mod(Int3 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     */
283    public static Int3 mod(Int3 a, Int3 b) {
284        Int3 result = new Int3();
285        result.x = a.x % b.x;
286        result.y = a.y % b.y;
287        result.z = a.z % b.z;
288
289        return result;
290    }
291
292    /** @hide
293     * Vector Modulo
294     *
295     * @param value
296     */
297    public void mod(int 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     */
310    public static Int3 mod(Int3 a, int b) {
311        Int3 result = new Int3();
312        result.x = a.x % b;
313        result.y = a.y % b;
314        result.z = a.z % b;
315
316        return result;
317    }
318
319    /** @hide
320     * get vector length
321     *
322     * @return
323     */
324    public int length() {
325        return 3;
326    }
327
328    /** @hide
329     * set vector negate
330     */
331    public void negate() {
332        this.x = -x;
333        this.y = -y;
334        this.z = -z;
335    }
336
337    /** @hide
338     * Vector dot Product
339     *
340     * @param a
341     * @return
342     */
343    public int dotProduct(Int3 a) {
344        return (int)((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     */
354    public static int dotProduct(Int3 a, Int3 b) {
355        return (int)((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     */
364    public void addMultiple(Int3 a, int 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 Int3
372     *
373     * @param a
374     */
375    public void set(Int3 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 Int
383     *
384     * @param a
385     * @param b
386     * @param c
387     */
388    public void setValues(int a, int b, int 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     */
399    public int elementSum() {
400        return (int)(x + y + z);
401    }
402
403    /** @hide
404     * get the vector field value by index
405     *
406     * @param i
407     * @return
408     */
409    public int get(int i) {
410        switch (i) {
411        case 0:
412            return (int)(x);
413        case 1:
414            return (int)(y);
415        case 2:
416            return (int)(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     */
428    public void setAt(int i, int 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     */
450    public void addAt(int i, int 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 int array
468     *
469     * @param data
470     * @param offset
471     */
472    public void copyTo(int[] data, int offset) {
473        data[offset] = (int)(x);
474        data[offset + 1] = (int)(y);
475        data[offset + 2] = (int)(z);
476    }
477}
478