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