Class: Snow::Vec4Array
- Inherits:
-
Object
- Object
- Snow::Vec4Array
- Defined in:
- lib/snow-math/vec4.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 Vec4s. 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.
Useful also to represent color buffers, vertices, and other miscellanea.
Class Method Summary collapse
-
.new(sm_length_or_copy) ⇒ Object
(also: [])
In the first form, a new typed array of Vec4 elements is allocated and returned.
Instance Method Summary collapse
-
#address ⇒ Object
Returns the memory address of the object.
-
#dup ⇒ Object
Duplicates the Vec4Array and returns it.
-
#fetch(sm_index) ⇒ Object
(also: #[])
Fetches a Vec4 from the array at the index and returns it.
-
#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 Vec4 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 Vec4 elements is allocated and returned. In the second form, a copy of a typed array of Vec4 objects is made and returned. Copied arrays do not share data.
call-seq:
new(size) -> new vec4_array
new(vec4_array) -> copy of vec4_array
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'ext/snow-math/snow-math.c', line 345
static VALUE sm_vec4_array_new(VALUE sm_self, VALUE sm_length_or_copy)
{
size_t length = 0;
vec4_t *arr;
VALUE sm_type_array;
int copy_array = 0;
if ((copy_array = SM_IS_A(sm_length_or_copy, vec4_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(vec4_t, length);
if (copy_array) {
const vec4_t *source;
Data_Get_Struct(sm_length_or_copy, vec4_t, source);
MEMCPY(arr, source, vec4_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
5564 5565 5566 5567 5568 5569 |
# File 'ext/snow-math/snow-math.c', line 5564
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
Duplicates the Vec4Array and returns it.
call-seq: dup -> new vec4_array
140 141 142 |
# File 'lib/snow-math/to_a.rb', line 140 def dup self.class.new(self) end |
#fetch(sm_index) ⇒ Object Also known as: []
Fetches a Vec4 from the array at the index and returns it. The returned Vec4 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 Vec4 fetched from an array modifies the array’s data.
As a result, objects returned by a Vec4Array should not be considered thread-safe, nor should manipulating a Vec4Array be considered thread-safe either. If you want to work with data returned from an array without altering the array data, you should call Vec4#dup or Vec4#copy to get a new Vec4 with a copy of the array object’s data.
call-seq: fetch(index) -> vec4
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'ext/snow-math/snow-math.c', line 434
static VALUE sm_vec4_array_fetch(VALUE sm_self, VALUE sm_index)
{
vec4_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, vec4_t, arr);
sm_inner = Data_Wrap_Struct(s_sm_vec4_klass, 0, 0, arr[index]);
rb_ivar_set(sm_inner, kRB_IVAR_MATHARRAY_SOURCE, sm_self);
/* Store the Vec4 in the cache */
rb_ary_store(sm_cache, (long)index, sm_inner);
}
return sm_inner;
}
|
#length ⇒ Object
Returns the array’s length.
call-seq: length -> fixnum
83 84 85 86 |
# File 'ext/snow-math/snow-math.c', line 83
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
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'ext/snow-math/snow-math.c', line 393
static VALUE sm_vec4_array_resize(VALUE sm_self, VALUE sm_new_length)
{
size_t new_length;
size_t old_length;
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, vec4_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
510 511 512 513 514 |
# File 'ext/snow-math/snow-math.c', line 510
static VALUE sm_vec4_array_size(VALUE sm_self)
{
size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self));
return SIZET2NUM(length * sizeof(vec4_t));
}
|
#store(sm_index, sm_value) ⇒ Object Also known as: []=
Stores a Vec4 at the given index. If the provided Vec4 is a member of the array and stored at the index, then no copy is done, otherwise the Vec4 is copied to the array.
call-seq: store(index, value) -> value
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'ext/snow-math/snow-math.c', line 474
static VALUE sm_vec4_array_store(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
vec4_t *arr;
vec4_t *value;
size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self));
size_t index = NUM2SIZET(sm_index);
if (index >= length) {
rb_raise(rb_eRangeError,
"Index %zu out of bounds for array with length %zu",
index, length);
} else if (!SM_IS_A(sm_value, vec4) || !(SM_IS_A(sm_value, quat))) {
rb_raise(rb_eTypeError,
"Invalid value to store: expected Quat or Vec4, got %s",
rb_obj_classname(sm_value));
}
Data_Get_Struct(sm_self, vec4_t, arr);
value = sm_unwrap_vec4(sm_value, NULL);
if (value == &arr[index]) {
/* The object's part of the array, don't bother copying */
return sm_value;
}
vec4_copy(*value, arr[index]);
return sm_value;
}
|