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