array.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // GetColor stores in the specified Color the
  84. // values from the array starting at the specified pos
  85. func (a ArrayF32) GetColor(pos int, v *Color) {
  86. v.R = a[pos]
  87. v.G = a[pos+1]
  88. v.B = a[pos+2]
  89. }
  90. // GetColor4 stores in the specified Color the
  91. // values from the array starting at the specified pos
  92. func (a ArrayF32) GetColor4(pos int, v *Color4) {
  93. v.R = a[pos]
  94. v.G = a[pos+1]
  95. v.B = a[pos+2]
  96. v.A = a[pos+3]
  97. }
  98. // Set sets the values of the array starting at the specified pos
  99. // from the specified values
  100. func (a ArrayF32) Set(pos int, v ...float32) {
  101. for i := 0; i < len(v); i++ {
  102. a[pos+i] = v[i]
  103. }
  104. }
  105. // SetVector2 sets the values of the array at the specified pos
  106. // from the XY values of the specified Vector2
  107. func (a ArrayF32) SetVector2(pos int, v *Vector2) {
  108. a[pos] = v.X
  109. a[pos+1] = v.Y
  110. }
  111. // SetVector3 sets the values of the array at the specified pos
  112. // from the XYZ values of the specified Vector3
  113. func (a ArrayF32) SetVector3(pos int, v *Vector3) {
  114. a[pos] = v.X
  115. a[pos+1] = v.Y
  116. a[pos+2] = v.Z
  117. }
  118. // SetVector4 sets the values of the array at the specified pos
  119. // from the XYZ values of the specified Vector4
  120. func (a ArrayF32) SetVector4(pos int, v *Vector4) {
  121. a[pos] = v.X
  122. a[pos+1] = v.Y
  123. a[pos+2] = v.Z
  124. a[pos+3] = v.W
  125. }
  126. // SetColor sets the values of the array at the specified pos
  127. // from the RGB values of the specified Color
  128. func (a ArrayF32) SetColor(pos int, v *Color) {
  129. a[pos] = v.R
  130. a[pos+1] = v.G
  131. a[pos+2] = v.B
  132. }
  133. // SetColor4 sets the values of the array at the specified pos
  134. // from the RGBA values of specified Color4
  135. func (a ArrayF32) SetColor4(pos int, v *Color4) {
  136. a[pos] = v.R
  137. a[pos+1] = v.G
  138. a[pos+2] = v.B
  139. a[pos+3] = v.A
  140. }
  141. // ArrayU32 is a slice of uint32 with additional convenience methods
  142. type ArrayU32 []uint32
  143. // NewArrayU32 creates a returns a new array of uint32
  144. // with the specified initial size and capacity
  145. func NewArrayU32(size, capacity int) ArrayU32 {
  146. return make([]uint32, size, capacity)
  147. }
  148. // Bytes returns the size of the array in bytes
  149. func (a *ArrayU32) Bytes() int {
  150. return len(*a) * int(unsafe.Sizeof(uint32(0)))
  151. }
  152. // Size returns the number of float32 elements in the array
  153. func (a *ArrayU32) Size() int {
  154. return len(*a)
  155. }
  156. // Len returns the number of float32 elements in the array
  157. func (a *ArrayU32) Len() int {
  158. return len(*a)
  159. }
  160. // Append appends n elements to the array updating the slice if necessary
  161. func (a *ArrayU32) Append(v ...uint32) {
  162. *a = append(*a, v...)
  163. }