Class: Snow::Vec2Array

Inherits:
Data
  • Object
show all
Includes:
ArrayMarshalSupport, ArraySupport, FiddlePointerSupport, InspectSupport
Defined in:
lib/snow-math/vec2.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 Vec2s. 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 texture coordinates and 2D positions and displacements.

Constant Summary collapse

TYPE =
s_sm_vec2_klass

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArrayMarshalSupport

#_dump, included

Methods included from InspectSupport

#inspect

Methods included from ArraySupport

#each, #map, #map!, #to_a

Methods included from FiddlePointerSupport

#to_ptr

Class Method Details

.new(sm_length_or_copy) ⇒ Object Also known as: []

In the first form, a new typed array of Vec2 elements is allocated and returned. In the second form, a copy of a typed array of Vec2 objects is made and returned. Copied arrays do not share data.

call-seq:

new(size)       -> new vec2_array
new(vec2_array) -> copy of vec2_array


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'ext/snow-math/snow-math.c', line 191

static VALUE sm_vec2_array_new(VALUE sm_self, VALUE sm_length_or_copy)
{
  size_t length = 0;
  vec2_t *arr;
  VALUE sm_type_array;
  int copy_array = 0;
  if ((copy_array = SM_IS_A(sm_length_or_copy, vec2_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(vec2_t, length);
  if (copy_array) {
    const vec2_t *source;
    Data_Get_Struct(sm_length_or_copy, vec2_t, source);
    MEMCPY(arr, source, vec2_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

#addressObject

Returns the memory address of the object.

call-seq: address -> fixnum



6739
6740
6741
6742
6743
6744
# File 'ext/snow-math/snow-math.c', line 6739

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);
}

#dupObject Also known as: clone

Duplicates the Vec2Array and returns it.

call-seq: dup -> new vec3_array



126
127
128
# File 'lib/snow-math/to_a.rb', line 126

def dup
  self.class.new(self)
end

#fetch(sm_index) ⇒ Object Also known as: []

Fetches a Vec2 from the array at the index and returns it. The returned Vec2 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 Vec2 fetched from an array modifies the array’s data.

As a result, objects returned by a Vec2Array should not be considered thread-safe, nor should manipulating a Vec2Array be considered thread-safe either. If you want to work with data returned from an array without altering the array data, you should call Vec2#dup or Vec2#copy to get a new Vec2 with a copy of the array object’s data.

call-seq: fetch(index) -> vec2



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/snow-math/snow-math.c', line 282

static VALUE sm_vec2_array_fetch(VALUE sm_self, VALUE sm_index)
{
  vec2_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, vec2_t, arr);
    sm_inner = Data_Wrap_Struct(s_sm_vec2_klass, 0, 0, arr[index]);
    rb_ivar_set(sm_inner, kRB_IVAR_MATHARRAY_SOURCE, sm_self);
    /* Store the Vec2 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;
}

#freezeObject

Freezes the array and its elements.

call-seq: freeze -> self



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/snow-math/snow-math.c', line 97

static VALUE sm_mathtype_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);
}

#lengthObject

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


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'ext/snow-math/snow-math.c', line 239

static VALUE sm_vec2_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, vec2_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;
}

#sizeObject

Returns the length of the array.

call-seq: length -> fixnum



364
365
366
367
368
# File 'ext/snow-math/snow-math.c', line 364

static VALUE sm_vec2_array_size(VALUE sm_self)
{
  size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self));
  return SIZET2NUM(length * sizeof(vec2_t));
}

#store(sm_index, sm_value) ⇒ Object Also known as: []=

Stores a Vec2 at the given index. If the provided Vec2 is a member of the array and stored at the index, then no copy is done, otherwise the Vec2 is copied to the array.

call-seq: store(index, value) -> value



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'ext/snow-math/snow-math.c', line 326

static VALUE sm_vec2_array_store(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  vec2_t *arr;
  vec2_t *value;
  size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self));
  size_t index = NUM2SIZET(sm_index);

  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 (!SM_IS_A(sm_value, vec2) && !SM_IS_A(sm_value, vec3) && !SM_IS_A(sm_value, vec4) && !SM_IS_A(sm_value, quat)) {
    rb_raise(rb_eTypeError,
      "Invalid value to store: expected Vec2, Vec3, Vec4, or Quat, got %s",
      rb_obj_classname(sm_value));
  }

  Data_Get_Struct(sm_self, vec2_t, arr);
  value = sm_unwrap_vec2(sm_value, NULL);

  if (value == &arr[index]) {
    /* The object's part of the array, don't bother copying */
    return sm_value;
  }

  vec2_copy(*value, arr[index]);
  return sm_value;
}