vec4.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. /*
  8. * This code has been modified from https://github.com/toji/gl-matrix/,
  9. * copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. */
  18. import Mat4 from './mat4';
  19. interface Vec4 extends Array<number> { [d: number]: number, '@type': 'vec4', length: 4 }
  20. namespace Vec4 {
  21. export function zero(): Vec4 {
  22. // force double backing array by 0.1.
  23. const ret = [0.1, 0, 0, 0];
  24. ret[0] = 0.0;
  25. return ret as any;
  26. }
  27. export function clone(a: Vec4) {
  28. const out = zero();
  29. out[0] = a[0];
  30. out[1] = a[1];
  31. out[2] = a[2];
  32. out[3] = a[3];
  33. return out;
  34. }
  35. export function create(x: number, y: number, z: number, w: number) {
  36. const out = zero();
  37. out[0] = x;
  38. out[1] = y;
  39. out[2] = z;
  40. out[3] = w;
  41. return out;
  42. }
  43. export function toArray(a: Vec4, out: Helpers.NumberArray, offset: number) {
  44. out[offset + 0] = a[0];
  45. out[offset + 1] = a[1];
  46. out[offset + 2] = a[2];
  47. out[offset + 3] = a[3];
  48. }
  49. export function fromArray(a: Vec4, array: Helpers.NumberArray, offset: number) {
  50. a[0] = array[offset + 0]
  51. a[1] = array[offset + 1]
  52. a[2] = array[offset + 2]
  53. a[3] = array[offset + 3]
  54. return a
  55. }
  56. export function toVec3Array(a: Vec4, out: Helpers.NumberArray, offset: number) {
  57. out[offset + 0] = a[0];
  58. out[offset + 1] = a[1];
  59. out[offset + 2] = a[2];
  60. }
  61. export function fromVec3Array(a: Vec4, array: Helpers.NumberArray, offset: number) {
  62. a[0] = array[offset + 0]
  63. a[1] = array[offset + 1]
  64. a[2] = array[offset + 2]
  65. a[3] = 0
  66. return a
  67. }
  68. export function copy(out: Vec4, a: Vec4) {
  69. out[0] = a[0];
  70. out[1] = a[1];
  71. out[2] = a[2];
  72. out[3] = a[3];
  73. return out;
  74. }
  75. export function set(out: Vec4, x: number, y: number, z: number, w: number) {
  76. out[0] = x;
  77. out[1] = y;
  78. out[2] = z;
  79. out[3] = w;
  80. return out;
  81. }
  82. export function add(out: Vec4, a: Vec4, b: Vec4) {
  83. out[0] = a[0] + b[0];
  84. out[1] = a[1] + b[1];
  85. out[2] = a[2] + b[2];
  86. out[3] = a[3] + b[3];
  87. return out;
  88. }
  89. export function distance(a: Vec4, b: Vec4) {
  90. const x = b[0] - a[0],
  91. y = b[1] - a[1],
  92. z = b[2] - a[2],
  93. w = b[3] - a[3];
  94. return Math.sqrt(x * x + y * y + z * z + w * w);
  95. }
  96. export function squaredDistance(a: Vec4, b: Vec4) {
  97. const x = b[0] - a[0],
  98. y = b[1] - a[1],
  99. z = b[2] - a[2],
  100. w = b[3] - a[3];
  101. return x * x + y * y + z * z + w * w;
  102. }
  103. export function norm(a: Vec4) {
  104. const x = a[0],
  105. y = a[1],
  106. z = a[2],
  107. w = a[3];
  108. return Math.sqrt(x * x + y * y + z * z + w * w);
  109. }
  110. export function squaredNorm(a: Vec4) {
  111. const x = a[0],
  112. y = a[1],
  113. z = a[2],
  114. w = a[3];
  115. return x * x + y * y + z * z + w * w;
  116. }
  117. export function transformMat4(out: Vec4, a: Vec4, m: Mat4) {
  118. const x = a[0], y = a[1], z = a[2], w = a[3];
  119. out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
  120. out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
  121. out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
  122. out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
  123. return out;
  124. }
  125. export function dot(a: Vec4, b: Vec4) {
  126. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  127. }
  128. }
  129. export default Vec4