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