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