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