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 double type. 21 * Provides three double fields packed. 22 */ 23 public class Double3 { 24 public double x; 25 public double y; 26 public double z; 27 28 public Double3() { 29 } 30 /** @hide */ 31 public Double3(Double3 data) { 32 this.x = data.x; 33 this.y = data.y; 34 this.z = data.z; 35 } 36 37 public Double3(double x, double y, double 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 Double3 add(Double3 a, Double3 b) { 51 Double3 res = new Double3(); 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(Double3 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(double 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 Double3 add(Double3 a, double b) { 89 Double3 res = new Double3(); 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(Double3 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 Double3 sub(Double3 a, Double3 b) { 116 Double3 res = new Double3(); 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(double 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 Double3 sub(Double3 a, double b) { 143 Double3 res = new Double3(); 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(Double3 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 Double3 mul(Double3 a, Double3 b) { 170 Double3 res = new Double3(); 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(double 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 Double3 mul(Double3 a, double b) { 197 Double3 res = new Double3(); 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(Double3 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 Double3 div(Double3 a, Double3 b) { 224 Double3 res = new Double3(); 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(double 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 Double3 div(Double3 a, double b) { 251 Double3 res = new Double3(); 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 double dotProduct(Double3 a) { 266 return (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 double dotProduct(Double3 a, Double3 b) { 277 return (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(Double3 a, double 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 double3 294 * 295 * @param a 296 */ 297 public void set(Double3 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 double elementSum() { 327 return x + y + z; 328 } 329 330 /** @hide 331 * Get the vector field value by index 332 * 333 * @param i 334 * @return 335 */ 336 public double 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, double 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, double 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(double x, double y, double z) { 401 this.x = x; 402 this.y = y; 403 this.z = z; 404 } 405 406 /** @hide 407 * Copy the vector to double array 408 * 409 * @param data 410 * @param offset 411 */ 412 public void copyTo(double[] data, int offset) { 413 data[offset] = x; 414 data[offset + 1] = y; 415 data[offset + 2] = z; 416 } 417 } 418