array.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math32
  5. import (
  6. "unsafe"
  7. )
  8. // ArrayF32 is a slice of float32 with additional convenience methods
  9. type ArrayF32 []float32
  10. // NewArrayF32 creates a returns a new array of floats
  11. // with the specified initial size and capacity
  12. func NewArrayF32(size, capacity int) ArrayF32 {
  13. return make([]float32, size, capacity)
  14. }
  15. // Bytes returns the size of the array in bytes
  16. func (a *ArrayF32) Bytes() int {
  17. return len(*a) * int(unsafe.Sizeof(float32(0)))
  18. }
  19. // Size returns the number of float32 elements in the array
  20. func (a *ArrayF32) Size() int {
  21. return len(*a)
  22. }
  23. // Len returns the number of float32 elements in the array
  24. // It is equivalent to Size()
  25. func (a *ArrayF32) Len() int {
  26. return len(*a)
  27. }
  28. // Append appends any number of values to the array
  29. func (a *ArrayF32) Append(v ...float32) {
  30. *a = append(*a, v...)
  31. }
  32. // AppendVector2 appends any number of Vector2 to the array
  33. func (a *ArrayF32) AppendVector2(v ...*Vector2) {
  34. for i := 0; i < len(v); i++ {
  35. *a = append(*a, v[i].X, v[i].Y)
  36. }
  37. }
  38. // AppendVector3 appends any number of Vector3 to the array
  39. func (a *ArrayF32) AppendVector3(v ...*Vector3) {
  40. for i := 0; i < len(v); i++ {
  41. *a = append(*a, v[i].X, v[i].Y, v[i].Z)
  42. }
  43. }
  44. // AppendVector4 appends any number of Vector4 to the array
  45. func (a *ArrayF32) AppendVector4(v ...*Vector4) {
  46. for i := 0; i < len(v); i++ {
  47. *a = append(*a, v[i].X, v[i].Y, v[i].Z, v[i].W)
  48. }
  49. }
  50. // AppendColor appends any number of Color to the array
  51. func (a *ArrayF32) AppendColor(v ...*Color) {
  52. for i := 0; i < len(v); i++ {
  53. *a = append(*a, v[i].R, v[i].G, v[i].B)
  54. }
  55. }
  56. // AppendColor4 appends any number of Color4 to the array
  57. func (a *ArrayF32) AppendColor4(v ...*Color4) {
  58. for i := 0; i < len(v); i++ {
  59. *a = append(*a, v[i].R, v[i].G, v[i].B, v[i].A)
  60. }
  61. }
  62. // GetVector2 stores in the specified Vector2 the
  63. // values from the array starting at the specified pos.
  64. func (a ArrayF32) GetVector2(pos int, v *Vector2) {
  65. v.X = a[pos]
  66. v.Y = a[pos+1]
  67. }
  68. // GetVector3 stores in the specified Vector3 the
  69. // values from the array starting at the specified pos.
  70. func (a ArrayF32) GetVector3(pos int, v *Vector3) {
  71. v.X = a[pos]
  72. v.Y = a[pos+1]
  73. v.Z = a[pos+2]
  74. }
  75. // GetVector4 stores in the specified Vector4 the
  76. // values from the array starting at the specified pos.
  77. func (a ArrayF32) GetVector4(pos int, v *Vector4) {
  78. v.X = a[pos]
  79. v.Y = a[pos+1]
  80. v.Z = a[pos+2]
  81. v.W = a[pos+3]
  82. }
  83. // GetMatrix4 stores in the specified Matrix4 the
  84. // values from the array starting at the specified pos.
  85. func (a ArrayF32) GetMatrix4(pos int, m *Matrix4) {
  86. m[0] = a[pos]
  87. m[1] = a[pos+1]
  88. m[2] = a[pos+2]
  89. m[3] = a[pos+3]
  90. m[4] = a[pos+4]
  91. m[5] = a[pos+5]
  92. m[6] = a[pos+6]
  93. m[7] = a[pos+7]
  94. m[8] = a[pos+8]
  95. m[9] = a[pos+9]
  96. m[10] = a[pos+10]
  97. m[11] = a[pos+11]
  98. m[12] = a[pos+12]
  99. m[13] = a[pos+13]
  100. m[14] = a[pos+14]
  101. m[15] = a[pos+15]
  102. }
  103. // GetColor stores in the specified Color the
  104. // values from the array starting at the specified pos
  105. func (a ArrayF32) GetColor(pos int, v *Color) {
  106. v.R = a[pos]
  107. v.G = a[pos+1]
  108. v.B = a[pos+2]
  109. }
  110. // GetColor4 stores in the specified Color the
  111. // values from the array starting at the specified pos
  112. func (a ArrayF32) GetColor4(pos int, v *Color4) {
  113. v.R = a[pos]
  114. v.G = a[pos+1]
  115. v.B = a[pos+2]
  116. v.A = a[pos+3]
  117. }
  118. // Set sets the values of the array starting at the specified pos
  119. // from the specified values
  120. func (a ArrayF32) Set(pos int, v ...float32) {
  121. for i := 0; i < len(v); i++ {
  122. a[pos+i] = v[i]
  123. }
  124. }
  125. // SetVector2 sets the values of the array at the specified pos
  126. // from the XY values of the specified Vector2
  127. func (a ArrayF32) SetVector2(pos int, v *Vector2) {
  128. a[pos] = v.X
  129. a[pos+1] = v.Y
  130. }
  131. // SetVector3 sets the values of the array at the specified pos
  132. // from the XYZ values of the specified Vector3
  133. func (a ArrayF32) SetVector3(pos int, v *Vector3) {
  134. a[pos] = v.X
  135. a[pos+1] = v.Y
  136. a[pos+2] = v.Z
  137. }
  138. // SetVector4 sets the values of the array at the specified pos
  139. // from the XYZ values of the specified Vector4
  140. func (a ArrayF32) SetVector4(pos int, v *Vector4) {
  141. a[pos] = v.X
  142. a[pos+1] = v.Y
  143. a[pos+2] = v.Z
  144. a[pos+3] = v.W
  145. }
  146. // SetColor sets the values of the array at the specified pos
  147. // from the RGB values of the specified Color
  148. func (a ArrayF32) SetColor(pos int, v *Color) {
  149. a[pos] = v.R
  150. a[pos+1] = v.G
  151. a[pos+2] = v.B
  152. }
  153. // SetColor4 sets the values of the array at the specified pos
  154. // from the RGBA values of specified Color4
  155. func (a ArrayF32) SetColor4(pos int, v *Color4) {
  156. a[pos] = v.R
  157. a[pos+1] = v.G
  158. a[pos+2] = v.B
  159. a[pos+3] = v.A
  160. }
  161. // ArrayU32 is a slice of uint32 with additional convenience methods
  162. type ArrayU32 []uint32
  163. // NewArrayU32 creates a returns a new array of uint32
  164. // with the specified initial size and capacity
  165. func NewArrayU32(size, capacity int) ArrayU32 {
  166. return make([]uint32, size, capacity)
  167. }
  168. // Bytes returns the size of the array in bytes
  169. func (a *ArrayU32) Bytes() int {
  170. return len(*a) * int(unsafe.Sizeof(uint32(0)))
  171. }
  172. // Size returns the number of float32 elements in the array
  173. func (a *ArrayU32) Size() int {
  174. return len(*a)
  175. }
  176. // Len returns the number of float32 elements in the array
  177. func (a *ArrayU32) Len() int {
  178. return len(*a)
  179. }
  180. // Append appends n elements to the array updating the slice if necessary
  181. func (a *ArrayU32) Append(v ...uint32) {
  182. *a = append(*a, v...)
  183. }