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