array.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. type ArrayF32 []float32
  9. // NewArrayF32 creates a returns a new array of floats
  10. // with the specified initial size and capacity
  11. func NewArrayF32(size, capacity int) ArrayF32 {
  12. return make([]float32, size, capacity)
  13. }
  14. // Bytes returns the size of the array in bytes
  15. func (a *ArrayF32) Bytes() int {
  16. return len(*a) * int(unsafe.Sizeof(float32(0)))
  17. }
  18. // Size returns the number of float32 elements in the array
  19. func (a *ArrayF32) Size() int {
  20. return len(*a)
  21. }
  22. // Len returns the number of float32 elements in the array
  23. func (a *ArrayF32) Len() int {
  24. return len(*a)
  25. }
  26. func (a *ArrayF32) Append(v ...float32) {
  27. *a = append(*a, v...)
  28. }
  29. func (a *ArrayF32) AppendVector2(v ...*Vector2) {
  30. for i := 0; i < len(v); i++ {
  31. *a = append(*a, v[i].X, v[i].Y)
  32. }
  33. }
  34. func (a *ArrayF32) AppendVector3(v ...*Vector3) {
  35. for i := 0; i < len(v); i++ {
  36. *a = append(*a, v[i].X, v[i].Y, v[i].Z)
  37. }
  38. }
  39. func (a *ArrayF32) AppendColor(v ...*Color) {
  40. for i := 0; i < len(v); i++ {
  41. *a = append(*a, v[i].R, v[i].G, v[i].B)
  42. }
  43. }
  44. func (a *ArrayF32) AppendColor4(v ...*Color4) {
  45. for i := 0; i < len(v); i++ {
  46. *a = append(*a, v[i].R, v[i].G, v[i].B, v[i].A)
  47. }
  48. }
  49. func (a ArrayF32) GetVector2(pos int, v *Vector2) {
  50. v.X = a[pos]
  51. v.Y = a[pos+1]
  52. }
  53. func (a ArrayF32) GetVector3(pos int, v *Vector3) {
  54. v.X = a[pos]
  55. v.Y = a[pos+1]
  56. v.Z = a[pos+2]
  57. }
  58. func (a ArrayF32) GetColor(pos int, v *Color) {
  59. v.R = a[pos]
  60. v.G = a[pos+1]
  61. v.B = a[pos+2]
  62. }
  63. func (a ArrayF32) Set(pos int, v ...float32) {
  64. for i := 0; i < len(v); i++ {
  65. a[pos+i] = v[i]
  66. }
  67. }
  68. func (a ArrayF32) SetVector2(pos int, v *Vector2) {
  69. a[pos] = v.X
  70. a[pos+1] = v.Y
  71. }
  72. func (a ArrayF32) SetVector3(pos int, v *Vector3) {
  73. a[pos] = v.X
  74. a[pos+1] = v.Y
  75. a[pos+2] = v.Z
  76. }
  77. func (a ArrayF32) SetColor(pos int, v *Color) {
  78. a[pos] = v.R
  79. a[pos+1] = v.G
  80. a[pos+2] = v.B
  81. }
  82. func (a ArrayF32) SetColor4(pos int, v *Color4) {
  83. a[pos] = v.R
  84. a[pos+1] = v.G
  85. a[pos+2] = v.B
  86. a[pos+3] = v.A
  87. }
  88. //
  89. //
  90. //
  91. type ArrayU32 []uint32
  92. // NewArrayU32 creates a returns a new array of uint32
  93. // with the specified initial size and capacity
  94. func NewArrayU32(size, capacity int) ArrayU32 {
  95. return make([]uint32, size, capacity)
  96. }
  97. // Bytes returns the size of the array in bytes
  98. func (a *ArrayU32) Bytes() int {
  99. return len(*a) * int(unsafe.Sizeof(uint32(0)))
  100. }
  101. // Size returns the number of float32 elements in the array
  102. func (a *ArrayU32) Size() int {
  103. return len(*a)
  104. }
  105. // Len returns the number of float32 elements in the array
  106. func (a *ArrayU32) Len() int {
  107. return len(*a)
  108. }
  109. // Append appends n elements to the array updating the slice if necessary
  110. func (a *ArrayU32) Append(v ...uint32) {
  111. *a = append(*a, v...)
  112. }