| Jason Sams | 6cc888e | 2011-04-22 17:05:25 -0700 | [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 | 6cc888e | 2011-04-22 17:05:25 -0700 | [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 double type. | 
|  | 21 | * Provides two double fields packed. | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 22 | * | 
|  | 23 | * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a | 
|  | 24 | * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration | 
|  | 25 | * guide</a> for the proposed alternatives. | 
| Matthieu Delahaye | 6a5875c | 2013-09-10 15:11:35 -0500 | [diff] [blame] | 26 | */ | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 27 | @Deprecated | 
| Jason Sams | 6cc888e | 2011-04-22 17:05:25 -0700 | [diff] [blame] | 28 | public class Double2 { | 
| Matthieu Delahaye | 6a5875c | 2013-09-10 15:11:35 -0500 | [diff] [blame] | 29 | public double x; | 
|  | 30 | public double y; | 
|  | 31 |  | 
| Jason Sams | 6cc888e | 2011-04-22 17:05:25 -0700 | [diff] [blame] | 32 | public Double2() { | 
|  | 33 | } | 
|  | 34 |  | 
| Matthieu Delahaye | 6a5875c | 2013-09-10 15:11:35 -0500 | [diff] [blame] | 35 | /** @hide */ | 
|  | 36 | public Double2(Double2 data) { | 
|  | 37 | this.x = data.x; | 
|  | 38 | this.y = data.y; | 
| Jason Sams | 6cc888e | 2011-04-22 17:05:25 -0700 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
| Matthieu Delahaye | 6a5875c | 2013-09-10 15:11:35 -0500 | [diff] [blame] | 41 | public Double2(double x, double y) { | 
|  | 42 | this.x = x; | 
|  | 43 | this.y = y; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | /** @hide | 
|  | 47 | * Vector add | 
|  | 48 | * | 
|  | 49 | * @param a | 
|  | 50 | * @param b | 
|  | 51 | * @return | 
|  | 52 | */ | 
|  | 53 | public static Double2 add(Double2 a, Double2 b) { | 
|  | 54 | Double2 res = new Double2(); | 
|  | 55 | res.x = a.x + b.x; | 
|  | 56 | res.y = a.y + b.y; | 
|  | 57 |  | 
|  | 58 | return res; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | /** @hide | 
|  | 62 | * Vector add | 
|  | 63 | * | 
|  | 64 | * @param value | 
|  | 65 | */ | 
|  | 66 | public void add(Double2 value) { | 
|  | 67 | x += value.x; | 
|  | 68 | y += value.y; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | /** @hide | 
|  | 72 | * Vector add | 
|  | 73 | * | 
|  | 74 | * @param value | 
|  | 75 | */ | 
|  | 76 | public void add(double 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 Double2 add(Double2 a, double b) { | 
|  | 89 | Double2 res = new Double2(); | 
|  | 90 | res.x = a.x + b; | 
|  | 91 | res.y = a.y + b; | 
|  | 92 |  | 
|  | 93 | return res; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | /** @hide | 
|  | 97 | * Vector subtraction | 
|  | 98 | * | 
|  | 99 | * @param value | 
|  | 100 | */ | 
|  | 101 | public void sub(Double2 value) { | 
|  | 102 | x -= value.x; | 
|  | 103 | y -= value.y; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | /** @hide | 
|  | 107 | * Vector subtraction | 
|  | 108 | * | 
|  | 109 | * @param a | 
|  | 110 | * @param b | 
|  | 111 | * @return | 
|  | 112 | */ | 
|  | 113 | public static Double2 sub(Double2 a, Double2 b) { | 
|  | 114 | Double2 res = new Double2(); | 
|  | 115 | res.x = a.x - b.x; | 
|  | 116 | res.y = a.y - b.y; | 
|  | 117 |  | 
|  | 118 | return res; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | /** @hide | 
|  | 122 | * Vector subtraction | 
|  | 123 | * | 
|  | 124 | * @param value | 
|  | 125 | */ | 
|  | 126 | public void sub(double 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 Double2 sub(Double2 a, double b) { | 
|  | 139 | Double2 res = new Double2(); | 
|  | 140 | res.x = a.x - b; | 
|  | 141 | res.y = a.y - b; | 
|  | 142 |  | 
|  | 143 | return res; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | /** @hide | 
|  | 147 | * Vector multiplication | 
|  | 148 | * | 
|  | 149 | * @param value | 
|  | 150 | */ | 
|  | 151 | public void mul(Double2 value) { | 
|  | 152 | x *= value.x; | 
|  | 153 | y *= value.y; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | /** @hide | 
|  | 157 | * Vector multiplication | 
|  | 158 | * | 
|  | 159 | * @param a | 
|  | 160 | * @param b | 
|  | 161 | * @return | 
|  | 162 | */ | 
|  | 163 | public static Double2 mul(Double2 a, Double2 b) { | 
|  | 164 | Double2 res = new Double2(); | 
|  | 165 | res.x = a.x * b.x; | 
|  | 166 | res.y = a.y * b.y; | 
|  | 167 |  | 
|  | 168 | return res; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | /** @hide | 
|  | 172 | * Vector multiplication | 
|  | 173 | * | 
|  | 174 | * @param value | 
|  | 175 | */ | 
|  | 176 | public void mul(double 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 Double2 mul(Double2 a, double b) { | 
|  | 189 | Double2 res = new Double2(); | 
|  | 190 | res.x = a.x * b; | 
|  | 191 | res.y = a.y * b; | 
|  | 192 |  | 
|  | 193 | return res; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | /** @hide | 
|  | 197 | * Vector division | 
|  | 198 | * | 
|  | 199 | * @param value | 
|  | 200 | */ | 
|  | 201 | public void div(Double2 value) { | 
|  | 202 | x /= value.x; | 
|  | 203 | y /= value.y; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | /** @hide | 
|  | 207 | * Vector division | 
|  | 208 | * | 
|  | 209 | * @param a | 
|  | 210 | * @param b | 
|  | 211 | * @return | 
|  | 212 | */ | 
|  | 213 | public static Double2 div(Double2 a, Double2 b) { | 
|  | 214 | Double2 res = new Double2(); | 
|  | 215 | res.x = a.x / b.x; | 
|  | 216 | res.y = a.y / b.y; | 
|  | 217 |  | 
|  | 218 | return res; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | /** @hide | 
|  | 222 | * Vector division | 
|  | 223 | * | 
|  | 224 | * @param value | 
|  | 225 | */ | 
|  | 226 | public void div(double 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 Double2 div(Double2 a, double b) { | 
|  | 239 | Double2 res = new Double2(); | 
|  | 240 | res.x = a.x / b; | 
|  | 241 | res.y = a.y / b; | 
|  | 242 |  | 
|  | 243 | return res; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | /** @hide | 
|  | 247 | * Vector dot Product | 
|  | 248 | * | 
|  | 249 | * @param a | 
|  | 250 | * @return | 
|  | 251 | */ | 
|  | 252 | public double dotProduct(Double2 a) { | 
|  | 253 | return (x * a.x) + (y * a.y); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | /** @hide | 
|  | 257 | * Vector dot Product | 
|  | 258 | * | 
|  | 259 | * @param a | 
|  | 260 | * @param b | 
|  | 261 | * @return | 
|  | 262 | */ | 
|  | 263 | public static Double dotProduct(Double2 a, Double2 b) { | 
|  | 264 | return (b.x * a.x) + (b.y * a.y); | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | /** @hide | 
|  | 268 | * Vector add Multiple | 
|  | 269 | * | 
|  | 270 | * @param a | 
|  | 271 | * @param factor | 
|  | 272 | */ | 
|  | 273 | public void addMultiple(Double2 a, double factor) { | 
|  | 274 | x += a.x * factor; | 
|  | 275 | y += a.y * factor; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | /** @hide | 
|  | 279 | * Set vector value by double2 | 
|  | 280 | * | 
|  | 281 | * @param a | 
|  | 282 | */ | 
|  | 283 | public void set(Double2 a) { | 
|  | 284 | this.x = a.x; | 
|  | 285 | this.y = a.y; | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | /** @hide | 
|  | 289 | * Set vector negate | 
|  | 290 | */ | 
|  | 291 | public void negate() { | 
|  | 292 | x = -x; | 
|  | 293 | y = -y; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | /** @hide | 
|  | 297 | * Get vector length | 
|  | 298 | * | 
|  | 299 | * @return | 
|  | 300 | */ | 
|  | 301 | public int length() { | 
|  | 302 | return 2; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | /** @hide | 
|  | 306 | * Return the element sum of vector | 
|  | 307 | * | 
|  | 308 | * @return | 
|  | 309 | */ | 
|  | 310 | public double elementSum() { | 
|  | 311 | return x + y; | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | /** @hide | 
|  | 315 | * Get the vector field value by index | 
|  | 316 | * | 
|  | 317 | * @param i | 
|  | 318 | * @return | 
|  | 319 | */ | 
|  | 320 | public double get(int i) { | 
|  | 321 | switch (i) { | 
|  | 322 | case 0: | 
|  | 323 | return x; | 
|  | 324 | case 1: | 
|  | 325 | return y; | 
|  | 326 | default: | 
|  | 327 | throw new IndexOutOfBoundsException("Index: i"); | 
|  | 328 | } | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | /** @hide | 
|  | 332 | * Set the vector field value by index | 
|  | 333 | * | 
|  | 334 | * @param i | 
|  | 335 | * @param value | 
|  | 336 | */ | 
|  | 337 | public void setAt(int i, double value) { | 
|  | 338 | switch (i) { | 
|  | 339 | case 0: | 
|  | 340 | x = value; | 
|  | 341 | return; | 
|  | 342 | case 1: | 
|  | 343 | y = value; | 
|  | 344 | return; | 
|  | 345 | default: | 
|  | 346 | throw new IndexOutOfBoundsException("Index: i"); | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | /** @hide | 
|  | 351 | * Add the vector field value by index | 
|  | 352 | * | 
|  | 353 | * @param i | 
|  | 354 | * @param value | 
|  | 355 | */ | 
|  | 356 | public void addAt(int i, double value) { | 
|  | 357 | switch (i) { | 
|  | 358 | case 0: | 
|  | 359 | x += value; | 
|  | 360 | return; | 
|  | 361 | case 1: | 
|  | 362 | y += value; | 
|  | 363 | return; | 
|  | 364 | default: | 
|  | 365 | throw new IndexOutOfBoundsException("Index: i"); | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | /** @hide | 
|  | 370 | * Set the vector field value | 
|  | 371 | * | 
|  | 372 | * @param x | 
|  | 373 | * @param y | 
|  | 374 | */ | 
|  | 375 | public void setValues(double x, double y) { | 
|  | 376 | this.x = x; | 
|  | 377 | this.y = y; | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | /** @hide | 
|  | 381 | * Copy the vector to double array | 
|  | 382 | * | 
|  | 383 | * @param data | 
|  | 384 | * @param offset | 
|  | 385 | */ | 
|  | 386 | public void copyTo(double[] data, int offset) { | 
|  | 387 | data[offset] = x; | 
|  | 388 | data[offset + 1] = y; | 
|  | 389 | } | 
| Jason Sams | 6cc888e | 2011-04-22 17:05:25 -0700 | [diff] [blame] | 390 | } |