array.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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) GetVector2(pos int, v *Vector2) {
  45. v.X = a[pos]
  46. v.Y = a[pos+1]
  47. }
  48. func (a ArrayF32) GetVector3(pos int, v *Vector3) {
  49. v.X = a[pos]
  50. v.Y = a[pos+1]
  51. v.Z = a[pos+2]
  52. }
  53. func (a ArrayF32) GetColor(pos int, v *Color) {
  54. v.R = a[pos]
  55. v.G = a[pos+1]
  56. v.B = a[pos+2]
  57. }
  58. func (a ArrayF32) Set(pos int, v ...float32) {
  59. for i := 0; i < len(v); i++ {
  60. a[pos+i] = v[i]
  61. }
  62. }
  63. func (a ArrayF32) SetVector2(pos int, v *Vector2) {
  64. a[pos] = v.X
  65. a[pos+1] = v.Y
  66. }
  67. func (a ArrayF32) SetVector3(pos int, v *Vector3) {
  68. a[pos] = v.X
  69. a[pos+1] = v.Y
  70. a[pos+2] = v.Z
  71. }
  72. func (a ArrayF32) SetColor(pos int, v *Color) {
  73. a[pos] = v.R
  74. a[pos+1] = v.G
  75. a[pos+2] = v.B
  76. }
  77. //
  78. //
  79. //
  80. type ArrayU32 []uint32
  81. // NewArrayU32 creates a returns a new array of uint32
  82. // with the specified initial size and capacity
  83. func NewArrayU32(size, capacity int) ArrayU32 {
  84. return make([]uint32, size, capacity)
  85. }
  86. // Bytes returns the size of the array in bytes
  87. func (a *ArrayU32) Bytes() int {
  88. return len(*a) * int(unsafe.Sizeof(uint32(0)))
  89. }
  90. // Size returns the number of float32 elements in the array
  91. func (a *ArrayU32) Size() int {
  92. return len(*a)
  93. }
  94. // Len returns the number of float32 elements in the array
  95. func (a *ArrayU32) Len() int {
  96. return len(*a)
  97. }
  98. // Append appends n elements to the array updating the slice if necessary
  99. func (a *ArrayU32) Append(v ...uint32) {
  100. *a = append(*a, v...)
  101. }