array.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // AppendColor appends any number of Color to the array
  45. func (a *ArrayF32) AppendColor(v ...*Color) {
  46. for i := 0; i < len(v); i++ {
  47. *a = append(*a, v[i].R, v[i].G, v[i].B)
  48. }
  49. }
  50. // AppendColor4 appends any number of Color4 to the array
  51. func (a *ArrayF32) AppendColor4(v ...*Color4) {
  52. for i := 0; i < len(v); i++ {
  53. *a = append(*a, v[i].R, v[i].G, v[i].B, v[i].A)
  54. }
  55. }
  56. // GetVector2 stores in the specified Vector2 the
  57. // values from the array starting at the specified pos.
  58. func (a ArrayF32) GetVector2(pos int, v *Vector2) {
  59. v.X = a[pos]
  60. v.Y = a[pos+1]
  61. }
  62. // GetVector3 stores in the specified Vector3 the
  63. // values from the array starting at the specified pos.
  64. func (a ArrayF32) GetVector3(pos int, v *Vector3) {
  65. v.X = a[pos]
  66. v.Y = a[pos+1]
  67. v.Z = a[pos+2]
  68. }
  69. // GetColor stores in the specified Color the
  70. // values from the array starting at the specified pos
  71. func (a ArrayF32) GetColor(pos int, v *Color) {
  72. v.R = a[pos]
  73. v.G = a[pos+1]
  74. v.B = a[pos+2]
  75. }
  76. // Set sets the values of the array starting at the specified pos
  77. // from the specified values
  78. func (a ArrayF32) Set(pos int, v ...float32) {
  79. for i := 0; i < len(v); i++ {
  80. a[pos+i] = v[i]
  81. }
  82. }
  83. // SetVector2 sets the values of the array at the specified pos
  84. // from the XY values of the specified Vector2
  85. func (a ArrayF32) SetVector2(pos int, v *Vector2) {
  86. a[pos] = v.X
  87. a[pos+1] = v.Y
  88. }
  89. // SetVector3 sets the values of the array at the specified pos
  90. // from the XYZ values of the specified Vector3
  91. func (a ArrayF32) SetVector3(pos int, v *Vector3) {
  92. a[pos] = v.X
  93. a[pos+1] = v.Y
  94. a[pos+2] = v.Z
  95. }
  96. // SetColor sets the values of the array at the specified pos
  97. // from the RGB values of the specified Color
  98. func (a ArrayF32) SetColor(pos int, v *Color) {
  99. a[pos] = v.R
  100. a[pos+1] = v.G
  101. a[pos+2] = v.B
  102. }
  103. // SetColor4 sets the values of the array at the specified pos
  104. // from the RGBA values of specified Color4
  105. func (a ArrayF32) SetColor4(pos int, v *Color4) {
  106. a[pos] = v.R
  107. a[pos+1] = v.G
  108. a[pos+2] = v.B
  109. a[pos+3] = v.A
  110. }
  111. // ArrayU32 is a slice of uint32 with additional convenience methods
  112. type ArrayU32 []uint32
  113. // NewArrayU32 creates a returns a new array of uint32
  114. // with the specified initial size and capacity
  115. func NewArrayU32(size, capacity int) ArrayU32 {
  116. return make([]uint32, size, capacity)
  117. }
  118. // Bytes returns the size of the array in bytes
  119. func (a *ArrayU32) Bytes() int {
  120. return len(*a) * int(unsafe.Sizeof(uint32(0)))
  121. }
  122. // Size returns the number of float32 elements in the array
  123. func (a *ArrayU32) Size() int {
  124. return len(*a)
  125. }
  126. // Len returns the number of float32 elements in the array
  127. func (a *ArrayU32) Len() int {
  128. return len(*a)
  129. }
  130. // Append appends n elements to the array updating the slice if necessary
  131. func (a *ArrayU32) Append(v ...uint32) {
  132. *a = append(*a, v...)
  133. }