/rs/java/android/renderscript/Int3.java

https://github.com/aizuzi/platform_frameworks_base · Java · 477 lines · 220 code · 48 blank · 209 comment · 3 complexity · 9e12338576492a8a8a2ae45988a50d3f MD5 · raw file

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