Class: Snow::Mat4Array
- Inherits:
-
Data
- Object
- Data
- Snow::Mat4Array
- Defined in:
- lib/snow-math/mat4.rb,
lib/snow-math/ptr.rb,
lib/snow-math/to_a.rb,
lib/snow-math/inspect.rb,
lib/snow-math/marshal.rb,
ext/snow-math/snow-math.c
Overview
A contiguous array of Mat4s. Allocated as a single block of memory so that it can easily be passed back to C libraries (like OpenGL) and to aid with cache locality.
May also be useful to subclass as a stack of Mat4s akin to now-deprecated functionality in OpenGL.
Class Method Summary collapse
-
.new(sm_length_or_copy) ⇒ Object
(also: [])
In the first form, a new typed array of Mat4 elements is allocated and returned.
Instance Method Summary collapse
-
#address ⇒ Object
Returns the memory address of the object.
-
#dup ⇒ Object
(also: #clone)
Duplicates the Mat4Array and returns it.
-
#fetch(sm_index) ⇒ Object
(also: #[])
Fetches a Mat4 from the array at the index and returns it.
-
#freeze ⇒ Object
Freezes the array and its elements.
-
#length ⇒ Object
Returns the array’s length.
-
#resize!(sm_new_length) ⇒ Object
Resizes the array to new_length and returns self.
-
#size ⇒ Object
Returns the length of the array.
-
#store(sm_index, sm_value) ⇒ Object
(also: #[]=)
Stores a Mat4 at the given index.
Methods included from ArrayMarshalSupport
Methods included from InspectSupport
Methods included from ArraySupport
Methods included from FiddlePointerSupport
Class Method Details
.new(sm_length_or_copy) ⇒ Object Also known as: []
In the first form, a new typed array of Mat4 elements is allocated and returned. In the second form, a copy of a typed array of Mat4 objects is made and returned. Copied arrays do not share data.
call-seq:
new(size) -> new mat4_array
new(mat4_array) -> copy of mat4_array
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 |
# File 'ext/snow-math/snow-math.c', line 1312 static VALUE sm_mat4_array_new(VALUE sm_self, VALUE sm_length_or_copy) { size_t length = 0; mat4_t *arr; VALUE sm_type_array; int copy_array = 0; if ((copy_array = SM_IS_A(sm_length_or_copy, mat4_array))) { length = NUM2SIZET(sm_mathtype_array_length(sm_length_or_copy)); } else { length = NUM2SIZET(sm_length_or_copy); } if (length <= 0) { return Qnil; } arr = ALLOC_N(mat4_t, length); if (copy_array) { const mat4_t *source; Data_Get_Struct(sm_length_or_copy, mat4_t, source); MEMCPY(arr, source, mat4_t, length); sm_length_or_copy = sm_mathtype_array_length(sm_length_or_copy); sm_self = rb_obj_class(sm_length_or_copy); } sm_type_array = Data_Wrap_Struct(sm_self, 0, free, arr); rb_ivar_set(sm_type_array, kRB_IVAR_MATHARRAY_LENGTH, sm_length_or_copy); rb_ivar_set(sm_type_array, kRB_IVAR_MATHARRAY_CACHE, rb_ary_new2((long)length)); rb_obj_call_init(sm_type_array, 0, 0); return sm_type_array; } |
Instance Method Details
#address ⇒ Object
Returns the memory address of the object.
call-seq: address -> fixnum
6894 6895 6896 6897 6898 6899 |
# File 'ext/snow-math/snow-math.c', line 6894 static VALUE sm_get_address(VALUE sm_self) { void *data_ptr = NULL; Data_Get_Struct(sm_self, void, data_ptr); return ULL2NUM((unsigned long long)data_ptr); } |
#dup ⇒ Object Also known as: clone
Duplicates the Mat4Array and returns it.
call-seq: dup -> new mat4_array
211 212 213 |
# File 'lib/snow-math/to_a.rb', line 211 def dup self.class.new(self) end |
#fetch(sm_index) ⇒ Object Also known as: []
Fetches a Mat4 from the array at the index and returns it. The returned Mat4 may be a cached object. In all cases, values returned from a typed array are associated with the memory of the array and not given their own memory. So, modifying a Mat4 fetched from an array modifies the array’s data.
As a result, objects returned by a Mat4Array should not be considered thread-safe, nor should manipulating a Mat4Array be considered thread-safe either. If you want to work with data returned from an array without altering the array data, you should call Mat4#dup or Mat4#copy to get a new Mat4 with a copy of the array object’s data.
call-seq: fetch(index) -> mat4
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 |
# File 'ext/snow-math/snow-math.c', line 1434 static VALUE sm_mat4_array_fetch(VALUE sm_self, VALUE sm_index) { mat4_t *arr; size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self)); size_t index = NUM2SIZET(sm_index); VALUE sm_inner; VALUE sm_cache; if (index >= length) { rb_raise(rb_eRangeError, "Index %zu out of bounds for array with length %zu", index, length); } sm_cache = rb_ivar_get(sm_self, kRB_IVAR_MATHARRAY_CACHE); if (!RTEST(sm_cache)) { rb_raise(rb_eRuntimeError, "No cache available"); } sm_inner = rb_ary_entry(sm_cache, (long)index); if (!RTEST(sm_inner)) { /* No cached value, create one. */ Data_Get_Struct(sm_self, mat4_t, arr); sm_inner = Data_Wrap_Struct(s_sm_mat4_klass, 0, 0, arr[index]); rb_ivar_set(sm_inner, kRB_IVAR_MATHARRAY_SOURCE, sm_self); /* Store the Mat4 in the cache */ rb_ary_store(sm_cache, (long)index, sm_inner); } if (OBJ_FROZEN(sm_self)) { rb_funcall2(sm_inner, kRB_NAME_FREEZE, 0, 0); } return sm_inner; } |
#freeze ⇒ Object
Freezes the array and its elements.
call-seq: freeze -> self
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
# File 'ext/snow-math/snow-math.c', line 1348 static VALUE sm_mat4_array_freeze(VALUE sm_self) { VALUE sm_cache; VALUE sm_cached_entry; long length; long index; if (OBJ_FROZEN(sm_self)) { return sm_self; } sm_cache = rb_ivar_get(sm_self, kRB_IVAR_MATHARRAY_CACHE); length = RARRAY_LEN(sm_cache); for (index = 0; index < length; ++index) { sm_cached_entry = rb_ary_entry(sm_cache, index); if (RTEST(sm_cached_entry)) { rb_funcall2(sm_cached_entry, kRB_NAME_FREEZE, 0, 0); } } return rb_call_super(0, 0); } |
#length ⇒ Object
Returns the array’s length.
call-seq: length -> fixnum
85 86 87 88 |
# File 'ext/snow-math/snow-math.c', line 85 static VALUE sm_mathtype_array_length(VALUE sm_self) { return rb_ivar_get(sm_self, kRB_IVAR_MATHARRAY_LENGTH); } |
#resize!(sm_new_length) ⇒ Object
Resizes the array to new_length and returns self.
If resizing to a length smaller than the previous length, excess array elements are discarded and the array is truncated. Otherwise, when resizing the array to a greater length than previous, new elements in the array will contain garbage values.
If new_length is equal to self.length, the call does nothing to the array.
Attempting to resize an array to a new length of zero or less will raise a RangeError. Do not try to resize arrays to zero or less. Do not be that person.
call-seq:
resize!(new_length) -> self
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 |
# File 'ext/snow-math/snow-math.c', line 1391 static VALUE sm_mat4_array_resize(VALUE sm_self, VALUE sm_new_length) { size_t new_length; size_t old_length; rb_check_frozen(sm_self); old_length = NUM2SIZET(sm_mathtype_array_length(sm_self)); new_length = NUM2SIZET(sm_new_length); if (old_length == new_length) { /* No change, done */ return sm_self; } else if (new_length < 1) { /* Someone decided to be that person. */ rb_raise(rb_eRangeError, "Cannot resize array to length less than or equal to 0."); return sm_self; } REALLOC_N(RDATA(sm_self)->data, mat4_t, new_length); rb_ivar_set(sm_self, kRB_IVAR_MATHARRAY_LENGTH, sm_new_length); rb_ary_clear(rb_ivar_get(sm_self, kRB_IVAR_MATHARRAY_CACHE)); return sm_self; } |
#size ⇒ Object
Returns the length of the array.
call-seq: length -> fixnum
1522 1523 1524 1525 1526 |
# File 'ext/snow-math/snow-math.c', line 1522 static VALUE sm_mat4_array_size(VALUE sm_self) { size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self)); return SIZET2NUM(length * sizeof(mat4_t)); } |
#store(sm_index, sm_value) ⇒ Object Also known as: []=
Stores a Mat4 at the given index. If the provided Mat4 is a member of the array and stored at the index, then no copy is done, otherwise the Mat4 is copied to the array.
If the value stored is a Mat3, it will be converted to a Mat4 for storage, though this will not modify the value directly.
call-seq: store(index, value) -> value
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 |
# File 'ext/snow-math/snow-math.c', line 1481 static VALUE sm_mat4_array_store(VALUE sm_self, VALUE sm_index, VALUE sm_value) { mat4_t *arr; size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self)); size_t index = NUM2SIZET(sm_index); int is_mat4 = 0; rb_check_frozen(sm_self); if (index >= length) { rb_raise(rb_eRangeError, "Index %zu out of bounds for array with length %zu", index, length); } else if (!(is_mat4 = SM_IS_A(sm_value, mat4)) && !SM_IS_A(sm_value, mat3)) { rb_raise(rb_eTypeError, "Invalid value to store: expected Mat3 or Mat4, got %s", rb_obj_classname(sm_value)); } Data_Get_Struct(sm_self, mat4_t, arr); if (is_mat4) { mat4_t *value = sm_unwrap_mat4(sm_value, NULL); if (value == &arr[index]) { /* The object's part of the array, don't bother copying */ return sm_value; } mat4_copy(*value, arr[index]); } else { mat3_to_mat4(*sm_unwrap_mat3(sm_value, NULL), arr[index]); } return sm_value; } |