Class: Snow::Mat4

Inherits:
Data
  • Object
show all
Includes:
ArraySupport, BaseMarshalSupport, FiddlePointerSupport, InspectSupport
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 4x4 matrix. Useful for anything from rotation to projection to almost any other 3D transformation you might need.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseMarshalSupport

#_dump, included

Methods included from InspectSupport

#inspect

Methods included from ArraySupport

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

Methods included from FiddlePointerSupport

#to_ptr

Constructor Details

#initialize(*args) ⇒ Object

Sets the Mat4’s components.

call-seq:

set(m1, m2, ..., m15, m16)   -> new mat4 with components
set([m1, m2, ..., m15, m16]) -> new mat4 with components
set(mat4)                    -> copy of mat4
set(mat3)                    -> new mat4 with mat3's components
set(quat)                    -> quat as mat4
set(Vec4, Vec4, Vec4, Vec4)  -> new mat4 with given row vectors


4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
# File 'ext/snow-math/snow-math.c', line 4904

static VALUE sm_mat4_init(int argc, VALUE *argv, VALUE sm_self)
{
  mat4_t *self = sm_unwrap_mat4(sm_self, NULL);
  size_t arr_index = 0;

  rb_check_frozen(sm_self);

  switch (argc) {

  case 0: {
    /* Identity (handled in _new) */
    break;
  }

  /* Copy Mat4 or provided [Numeric..] */
  case 1: {
    /* Copy Mat4 */
    if (SM_IS_A(argv[0], mat4)) {
      sm_unwrap_mat4(argv[0], *self);
      break;
    }

    /* Copy Mat3 */
    if (SM_IS_A(argv[0], mat3)) {
      mat3_to_mat4(*sm_unwrap_mat4(argv[0], NULL), *self);
      break;
    }

    /* Build from Quaternion */
    if (SM_IS_A(argv[0], quat)) {
      mat4_from_quat(*sm_unwrap_quat(argv[0], NULL), *self);
      break;
    }

    /* Optional offset into array provided */
    if (0) {
      case 2:
      arr_index = NUM2SIZET(argv[1]);
    }

    /* Array of values */
    if (SM_RB_IS_A(argv[0], rb_cArray)) {
      VALUE arrdata = argv[0];
      const size_t arr_end = arr_index + 16;
      s_float_t *mat_elem = *self;
      for (; arr_index < arr_end; ++arr_index, ++mat_elem) {
        *mat_elem = rb_num2dbl(rb_ary_entry(arrdata, (long)arr_index));
      }
      break;
    }

    rb_raise(rb_eArgError, "Expected either an array of Numerics or a Mat4");
    break;
  }

  /* Mat4(Vec4, Vec4, Vec4, Vec4) */
  case 4: {
    size_t arg_index;
    s_float_t *mat_elem = *self;
    for (arg_index = 0; arg_index < 4; ++arg_index, mat_elem += 4) {
      if (!SM_IS_A(argv[arg_index], vec4) && !SM_IS_A(argv[arg_index], quat)) {
        rb_raise(
          rb_eArgError,
          "Argument %d must be a Vec4 or Quat when supplying four arguments to initialize/set",
          (int)(arg_index + 1));
      }

      sm_unwrap_vec4(argv[arg_index], mat_elem);
    }
    break;
  }

  /* Mat4(Numeric m00 .. m16) */
  case 16: {
    s_float_t *mat_elem = *self;
    VALUE *argv_p = argv;
    for (; argc; --argc, ++argv_p, ++mat_elem) {
      *mat_elem = (s_float_t)rb_num2dbl(*argv_p);
    }
    break;
  }

  default: {
    rb_raise(rb_eArgError, "Invalid arguments to initialize/set");
    break;
  }
  } /* switch (argc) */

  return sm_self;
}

Class Method Details

.angle_axis(*args) ⇒ Object

Returns a Mat4 describing a rotation around an axis.

call-seq:

angle_axis(angle_degrees, axis_vec3, output = nil) -> output or new mat4


5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
# File 'ext/snow-math/snow-math.c', line 5033

static VALUE sm_mat4_angle_axis(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_angle;
  VALUE sm_axis;
  VALUE sm_out;
  s_float_t angle;
  const vec3_t *axis;

  rb_scan_args(argc, argv, "21", &sm_angle, &sm_axis, &sm_out);
  if (!SM_IS_A(sm_axis, vec3) && !SM_IS_A(sm_axis, vec4) && !SM_IS_A(sm_axis, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_axis));
    return Qnil;
  }

  angle = (s_float_t)rb_num2dbl(sm_angle);
  axis = sm_unwrap_vec3(sm_axis, NULL);

  if (SM_IS_A(sm_out, mat4)) {
    rb_check_frozen(sm_out);
    mat4_t *out = sm_unwrap_mat4(sm_out, NULL);
    mat4_rotation(angle, (*axis)[0], (*axis)[1], (*axis)[2], *out);
  } else {
    mat4_t out;
    mat4_rotation(angle, (*axis)[0], (*axis)[1], (*axis)[2], out);
    sm_out = sm_wrap_mat4(out, self);
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

.frustum(*args) ⇒ Object

Returns a matrix describing a frustum perspective.

call-seq:

frustum(left, right, bottom, top, z_near, z_far, output = nil) -> output or new mat4


5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
# File 'ext/snow-math/snow-math.c', line 5485

static VALUE sm_mat4_frustum(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_left;
  VALUE sm_right;
  VALUE sm_bottom;
  VALUE sm_top;
  VALUE sm_z_near;
  VALUE sm_z_far;
  VALUE sm_out;
  s_float_t left;
  s_float_t right;
  s_float_t bottom;
  s_float_t top;
  s_float_t z_near;
  s_float_t z_far;

  rb_scan_args(argc, argv, "61", &sm_left, &sm_right, &sm_bottom, &sm_top, &sm_z_near, &sm_z_far, &sm_out);

  left = (s_float_t)rb_num2dbl(sm_left);
  right = (s_float_t)rb_num2dbl(sm_right);
  bottom = (s_float_t)rb_num2dbl(sm_bottom);
  top = (s_float_t)rb_num2dbl(sm_top);
  z_near = (s_float_t)rb_num2dbl(sm_z_near);
  z_far = (s_float_t)rb_num2dbl(sm_z_far);

  if (SM_IS_A(sm_out, mat4)) {
    rb_check_frozen(sm_out);
    mat4_t *out = sm_unwrap_mat4(sm_out, NULL);
    mat4_frustum(left, right, bottom, top, z_near, z_far, *out);
  } else {
    mat4_t out;
    mat4_frustum(left, right, bottom, top, z_near, z_far, out);
    sm_out = sm_wrap_mat4(out, Qnil);
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

.look_at(*args) ⇒ Object

Returns a matrix describing a view transformation for an eye looking at center with the given up vector.

call-seq:

look_at(eye, center, up, output = nil) -> output or new mat4


5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
# File 'ext/snow-math/snow-math.c', line 5621

static VALUE sm_mat4_look_at(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_eye;
  VALUE sm_center;
  VALUE sm_up;
  VALUE sm_out;
  const vec3_t *eye;
  const vec3_t *center;
  const vec3_t *up;

  rb_scan_args(argc, argv, "31", &sm_eye, &sm_center, &sm_up, &sm_out);

  eye = sm_unwrap_vec3(sm_eye, NULL);
  center = sm_unwrap_vec3(sm_center, NULL);
  up = sm_unwrap_vec3(sm_up, NULL);

  if (SM_IS_A(sm_out, mat4)) {
    rb_check_frozen(sm_out);
    mat4_t *out = sm_unwrap_mat4(sm_out, NULL);
    mat4_look_at(*eye, *center, *up, *out);
  } else {
    mat4_t out;
    mat4_look_at(*eye, *center, *up, out);
    sm_out = sm_wrap_mat4(out, self);
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

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

Allocates a new Mat4.

call-seq:

new()                        -> identity mat4
new(m1, m2, ..., m15, m16)   -> new mat4 with components
new([m1, m2, ..., m15, m16]) -> new mat4 with components
new(mat4)                    -> copy of mat4
new(mat3)                    -> new mat4 with mat3's components
new(quat)                    -> quat as mat4
new(Vec4, Vec4, Vec4, Vec4)  -> new mat4 with given row vectors


4884
4885
4886
4887
4888
4889
# File 'ext/snow-math/snow-math.c', line 4884

static VALUE sm_mat4_new(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_mat = sm_wrap_mat4(g_mat4_identity, self);
  rb_obj_call_init(sm_mat, argc, argv);
  return sm_mat;
}

.orthographic(*args) ⇒ Object

Returns a matrix describing an orthographic projection.

call-seq:

orthographic(left, right, bottom, top, z_near, z_far, output = nil) -> output or new mat4


5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
# File 'ext/snow-math/snow-math.c', line 5532

static VALUE sm_mat4_orthographic(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_left;
  VALUE sm_right;
  VALUE sm_bottom;
  VALUE sm_top;
  VALUE sm_z_near;
  VALUE sm_z_far;
  VALUE sm_out;
  s_float_t left;
  s_float_t right;
  s_float_t bottom;
  s_float_t top;
  s_float_t z_near;
  s_float_t z_far;

  rb_scan_args(argc, argv, "61", &sm_left, &sm_right, &sm_bottom, &sm_top, &sm_z_near, &sm_z_far, &sm_out);

  left = (s_float_t)rb_num2dbl(sm_left);
  right = (s_float_t)rb_num2dbl(sm_right);
  bottom = (s_float_t)rb_num2dbl(sm_bottom);
  top = (s_float_t)rb_num2dbl(sm_top);
  z_near = (s_float_t)rb_num2dbl(sm_z_near);
  z_far = (s_float_t)rb_num2dbl(sm_z_far);

  if (SM_IS_A(sm_out, mat4)) {
    rb_check_frozen(sm_out);
    mat4_t *out = sm_unwrap_mat4(sm_out, NULL);
    mat4_orthographic(left, right, bottom, top, z_near, z_far, *out);
  } else {
    mat4_t out;
    mat4_orthographic(left, right, bottom, top, z_near, z_far, out);
    sm_out = sm_wrap_mat4(out, self);
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

.perspective(*args) ⇒ Object

Returns a matrix describing a perspective projection.

call-seq:

perspective(fov_y_degrees, aspect, z_near, z_far, output = nil) -> output or new mat4


5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
# File 'ext/snow-math/snow-math.c', line 5579

static VALUE sm_mat4_perspective(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_fov_y;
  VALUE sm_aspect;
  VALUE sm_z_near;
  VALUE sm_z_far;
  VALUE sm_out;
  s_float_t fov_y;
  s_float_t aspect;
  s_float_t z_near;
  s_float_t z_far;

  rb_scan_args(argc, argv, "41", &sm_fov_y, &sm_aspect, &sm_z_near, &sm_z_far, &sm_out);

  fov_y = (s_float_t)rb_num2dbl(sm_fov_y);
  aspect = (s_float_t)rb_num2dbl(sm_aspect);
  z_near = (s_float_t)rb_num2dbl(sm_z_near);
  z_far = (s_float_t)rb_num2dbl(sm_z_far);

  if (SM_IS_A(sm_out, mat4)) {
    rb_check_frozen(sm_out);
    mat4_t *out = sm_unwrap_mat4(sm_out, NULL);
    mat4_perspective(fov_y, aspect, z_near, z_far, *out);
  } else {
    mat4_t out;
    mat4_perspective(fov_y, aspect, z_near, z_far, out);
    sm_out = sm_wrap_mat4(out, self);
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

.translation(*args) ⇒ Object

Returns a translation matrix for the given X, Y, and Z translations (or using the vector’s components as such).

call-seq:

translation(x, y, z, output = nil) -> output or new mat4
translation(vec3, output = nil)    -> output or new mat4


4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
# File 'ext/snow-math/snow-math.c', line 4828

static VALUE sm_mat4_translation(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out = Qnil;
  vec3_t xyz;

  SM_LABEL(argc_reconfig):
  switch (argc) {
  case 2: case 4: {
    sm_out = argv[--argc];
    if (RTEST(sm_out)) {
      SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    }
    goto SM_LABEL(argc_reconfig);
  }

  case 1: {
    sm_unwrap_vec3(argv[0], xyz);
    goto SM_LABEL(get_output);
  }

  case 3: {
    xyz[0] = rb_num2dbl(argv[0]);
    xyz[1] = rb_num2dbl(argv[1]);
    xyz[2] = rb_num2dbl(argv[2]);

    SM_LABEL(get_output):
    if (RTEST(sm_out)) {
      rb_check_frozen(sm_out);
      mat4_t *out = sm_unwrap_mat4(sm_out, NULL);
      mat4_translation(xyz[0], xyz[1], xyz[2], *out);
    } else {
      mat4_t out;
      mat4_translation(xyz[0], xyz[1], xyz[2], out);
      sm_out = sm_wrap_mat4(out, sm_self);
      rb_obj_call_init(sm_out, 0, 0);
    }
  }
  }

  return sm_out;
}

Instance Method Details

#==(sm_other) ⇒ Object

Tests this Mat4 and another Mat4 for equivalency.

call-seq:

mat4 == other_mat4 -> bool


5692
5693
5694
5695
5696
5697
5698
5699
# File 'ext/snow-math/snow-math.c', line 5692

static VALUE sm_mat4_equals(VALUE sm_self, VALUE sm_other)
{
  if (!RTEST(sm_other) || !SM_IS_A(sm_other, mat4)) {
    return Qfalse;
  }

  return mat4_equals(*sm_unwrap_mat4(sm_self, NULL), *sm_unwrap_mat4(sm_other, NULL)) ? Qtrue : Qfalse;
}

#addressObject

Returns the memory address of the object.

call-seq: address -> fixnum



6683
6684
6685
6686
6687
6688
# File 'ext/snow-math/snow-math.c', line 6683

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

#adjoint(*args) ⇒ Object

Returns an adjoint matrix.

call-seq:

adjoint(output = nil) -> output or new mat4


4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
# File 'ext/snow-math/snow-math.c', line 4379

static VALUE sm_mat4_adjoint(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  mat4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (argc == 1) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    mat4_t *output;
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    rb_check_frozen(sm_out);
    output = sm_unwrap_mat4(sm_out, NULL);
    mat4_adjoint (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    mat4_t output;
    mat4_adjoint (*self, output);
    sm_out = sm_wrap_mat4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to adjoint");
  }
  return sm_out;
}

#adjoint!Object

Calls #adjoint(self)

call-seq: adjoint! -> self



61
62
63
# File 'lib/snow-math/mat4.rb', line 61

def adjoint!
  adjoint self
end

#copy(*args) ⇒ Object Also known as: dup, clone

Returns a copy of self.

call-seq:

copy(output = nil) -> output or new mat4


4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
# File 'ext/snow-math/snow-math.c', line 4239

static VALUE sm_mat4_copy(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  mat4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (argc == 1) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    mat4_t *output;
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    rb_check_frozen(sm_out);
    output = sm_unwrap_mat4(sm_out, NULL);
    mat4_copy (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    mat4_t output;
    mat4_copy (*self, output);
    sm_out = sm_wrap_mat4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to copy");
  }
  return sm_out;
}

#determinantObject

Returns the matrix determinant.

call-seq:

determinant -> float


4759
4760
4761
4762
# File 'ext/snow-math/snow-math.c', line 4759

static VALUE sm_mat4_determinant(VALUE sm_self)
{
  return mat4_determinant(*sm_unwrap_mat4(sm_self, NULL));
}

#fetchObject Also known as: []

Gets the component of the Mat4 at the given index.

call-seq: fetch(index) -> float



4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
# File 'ext/snow-math/snow-math.c', line 4172

static VALUE sm_mat4_fetch (VALUE sm_self, VALUE sm_index)
{
  static const int max_index = sizeof(mat4_t) / sizeof(s_float_t);
  const mat4_t *self = sm_unwrap_mat4(sm_self, NULL);
  int index = NUM2INT(sm_index);
  if (index < 0 || index >= max_index) {
    rb_raise(rb_eRangeError,
      "Index %d is out of bounds, must be from 0 through %d", index, max_index - 1);
  }
  return rb_float_new(self[0][NUM2INT(sm_index)]);
}

#get_column3(*args) ⇒ Object

Returns a Vec3 whose components are that of the column at the given index.

call-seq:

get_column3(index, output = nil) -> output or new vec3


5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
# File 'ext/snow-math/snow-math.c', line 5202

static VALUE sm_mat4_get_column3(int argc, VALUE *argv, VALUE sm_self)
{
  mat4_t *self;
  int index;
  VALUE sm_out;

  self = sm_unwrap_mat4(sm_self, NULL);
  index = NUM2INT(argv[0]);
  sm_out = Qnil;

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  switch (argc) {
  case 2: {
    vec3_t *out;

    sm_out = argv[1];

    if (RTEST(sm_out)) {
      if (!SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
        rb_raise(rb_eTypeError,
          kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
          rb_obj_classname(sm_out));
        return Qnil;
      }
      rb_check_frozen(sm_out);
    } else {
      goto SM_LABEL(no_output);
    }

    out = sm_unwrap_vec3(sm_out, NULL);
    mat4_get_column3(*self, index, *out);

    break;
  }

  case 1: SM_LABEL(no_output): {
    vec3_t out;
    mat4_get_column3(*self, index, out);
    sm_out = sm_wrap_vec3(out, Qnil);
    rb_obj_call_init(sm_out, 0, 0);
    break;
  }

  default: {
    rb_raise(rb_eArgError, "Invalid number of arguments to get_column3 - expected 1 or 2");
    break;
  }
  }

  return sm_out;
}

#get_column4(*args) ⇒ Object

Returns a Vec4 whose components are that of the column at the given index.

call-seq:

get_column4(index, output = nil) -> output or new vec4


5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
# File 'ext/snow-math/snow-math.c', line 5266

static VALUE sm_mat4_get_column4(int argc, VALUE *argv, VALUE sm_self)
{
  mat4_t *self;
  int index;
  VALUE sm_out;

  self = sm_unwrap_mat4(sm_self, NULL);
  index = NUM2INT(argv[0]);
  sm_out = Qnil;

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  switch (argc) {
  case 2: {
    vec4_t *out;

    sm_out = argv[1];

    if (RTEST(sm_out)) {
      if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
        rb_raise(rb_eTypeError,
          kSM_WANT_FOUR_FORMAT_LIT,
          rb_obj_classname(sm_out));
        return Qnil;
      }
      rb_check_frozen(sm_out);
    } else {
      goto SM_LABEL(no_output);
    }

    out = sm_unwrap_vec4(sm_out, NULL);
    mat4_get_column4(*self, index, *out);

    break;
  }

  case 1: SM_LABEL(no_output): {
    vec4_t out;
    mat4_get_column4(*self, index, out);
    sm_out = sm_wrap_vec4(out, Qnil);
    rb_obj_call_init(sm_out, 0, 0);
    break;
  }

  default: {
    rb_raise(rb_eArgError, "Invalid number of arguments to get_column4 - expected 1 or 2");
    break;
  }
  }

  return sm_out;
}

#get_row3(*args) ⇒ Object

Returns a Vec3 whose components are that of the row at the given index.

call-seq:

get_row3(index, output = nil) -> output or new vec3


5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
# File 'ext/snow-math/snow-math.c', line 5074

static VALUE sm_mat4_get_row3(int argc, VALUE *argv, VALUE sm_self)
{
  mat4_t *self;
  int index;
  VALUE sm_out;

  self = sm_unwrap_mat4(sm_self, NULL);
  index = NUM2INT(argv[0]);
  sm_out = Qnil;

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  switch (argc) {
  case 2: {
    vec3_t *out;

    sm_out = argv[1];

    if (RTEST(sm_out)) {
      if (!SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
        rb_raise(rb_eTypeError,
          kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
          rb_obj_classname(sm_out));
        return Qnil;
      }
      rb_check_frozen(sm_out);
    } else {
      goto SM_LABEL(no_output);
    }

    out = sm_unwrap_vec3(sm_out, NULL);
    mat4_get_row3(*self, index, *out);

    break;
  }

  case 1: SM_LABEL(no_output): {
    vec3_t out;
    mat4_get_row3(*self, index, out);
    sm_out = sm_wrap_vec3(out, Qnil);
    rb_obj_call_init(sm_out, 0, 0);
    break;
  }

  default: {
    rb_raise(rb_eArgError, "Invalid number of arguments to get_row3 - expected 1 or 2");
    break;
  }
  }

  return sm_out;
}

#get_row4(*args) ⇒ Object

Returns a Vec4 whose components are that of the row at the given index.

call-seq:

get_row4(index, output = nil) -> output or new vec4


5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
# File 'ext/snow-math/snow-math.c', line 5138

static VALUE sm_mat4_get_row4(int argc, VALUE *argv, VALUE sm_self)
{
  mat4_t *self;
  int index;
  VALUE sm_out;

  self = sm_unwrap_mat4(sm_self, NULL);
  index = NUM2INT(argv[0]);
  sm_out = Qnil;

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  switch (argc) {
  case 2: {
    vec4_t *out;

    sm_out = argv[1];

    if (RTEST(sm_out)) {
      if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
        rb_raise(rb_eTypeError,
          kSM_WANT_FOUR_FORMAT_LIT,
          rb_obj_classname(sm_out));
        return Qnil;
      }
      rb_check_frozen(sm_out);
    } else {
      goto SM_LABEL(no_output);
    }

    out = sm_unwrap_vec4(sm_out, NULL);
    mat4_get_row4(*self, index, *out);

    break;
  }

  case 1: SM_LABEL(no_output): {
    vec4_t out;
    mat4_get_row4(*self, index, out);
    sm_out = sm_wrap_vec4(out, Qnil);
    rb_obj_call_init(sm_out, 0, 0);
    break;
  }

  default: {
    rb_raise(rb_eArgError, "Invalid number of arguments to get_row4 - expected 1 or 2");
    break;
  }
  }

  return sm_out;
}

#inverse_affine(*args) ⇒ Object

Returns an inverse affine matrix if successful. Otherwise, returns nil.

call-seq:

inverse_affine(output = nil) -> output, new mat4, or nil


4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
# File 'ext/snow-math/snow-math.c', line 4651

static VALUE sm_mat4_inverse_affine(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out = Qnil;
  mat4_t *self;

  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);

  if (argc == 1) {
    mat4_t *output;

    if (!RTEST(sm_out)) {
      goto SM_LABEL(output_lbl);
    }

    if (!SM_IS_A(sm_out, mat4)) {
      rb_raise(rb_eTypeError,
        "Invalid argument to output of inverse_affine: expected %s, got %s",
        rb_class2name(s_sm_mat4_klass),
        rb_obj_classname(sm_out));
      return Qnil;
    }
    rb_check_frozen(sm_out);

    output = sm_unwrap_mat4(sm_out, NULL);
    if (!mat4_inverse_affine(*self, *output)) {
      return Qnil;
    }

  } else if (argc == 0) {
    SM_LABEL(output_lbl): {
      mat4_t output;
      if (!mat4_inverse_affine(*self, output)) {
        return Qnil;
      }

      sm_out = sm_wrap_mat4(output, rb_obj_class(sm_self));
      rb_obj_call_init(sm_out, 0, 0);
    }
  } else {
    rb_raise(rb_eArgError, "Invalid number of arguments to inverse_affine");
  }

  return sm_out;
}

#inverse_affine!Object

Calls #inverse_affine(self)

call-seq: inverse_affine! -> self



149
150
151
# File 'lib/snow-math/mat4.rb', line 149

def inverse_affine!
  inverse_affine self
end

#inverse_general(*args) ⇒ Object

Returns an generalized inverse matrix if successful. Otherwise, returns nil.

call-seq:

inverse_general(output = nil) -> output, new mat4, or nil


4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
# File 'ext/snow-math/snow-math.c', line 4705

static VALUE sm_mat4_inverse_general(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out = Qnil;
  mat4_t *self;

  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);

  if (argc == 1) {
    mat4_t *output;

    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }

    if (!SM_IS_A(sm_out, mat4)) {
      rb_raise(rb_eTypeError,
        "Invalid argument to output of inverse_general: expected %s, got %s",
        rb_class2name(s_sm_mat4_klass),
        rb_obj_classname(sm_out));
      return Qnil;
    }
    rb_check_frozen(sm_out);

    output = sm_unwrap_mat4(sm_out, NULL);
    if (!mat4_inverse_general(*self, *output)) {
      return Qnil;
    }

  } else if (argc == 0) {
    SM_LABEL(skip_output): {
      mat4_t output;
      if (!mat4_inverse_general(*self, output)) {
        return Qnil;
      }

      sm_out = sm_wrap_mat4(output, rb_obj_class(sm_self));
      rb_obj_call_init(sm_out, 0, 0);
    }
  } else {
    rb_raise(rb_eArgError, "Invalid number of arguments to inverse_general");
  }

  return sm_out;
}

#inverse_general!Object

Calls #inverse_general(self)

call-seq: inverse_general! -> self



156
157
158
# File 'lib/snow-math/mat4.rb', line 156

def inverse_general!
  inverse_general self
end

#inverse_orthogonal(*args) ⇒ Object

Returns an inverse orthogonal matrix.

call-seq:

inverse_orthogonal(output = nil) -> output or new mat4


4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
# File 'ext/snow-math/snow-math.c', line 4344

static VALUE sm_mat4_inverse_orthogonal(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  mat4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (argc == 1) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    mat4_t *output;
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    rb_check_frozen(sm_out);
    output = sm_unwrap_mat4(sm_out, NULL);
    mat4_inverse_orthogonal (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    mat4_t output;
    mat4_inverse_orthogonal (*self, output);
    sm_out = sm_wrap_mat4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to inverse_orthogonal");
  }
  return sm_out;
}

#inverse_orthogonal!Object

Calls #inverse_orthogonal(self)

call-seq: inverse_orthogonal! -> self



54
55
56
# File 'lib/snow-math/mat4.rb', line 54

def inverse_orthogonal!
  inverse_orthogonal self
end

#inverse_rotate_vec3(*args) ⇒ Object

Convenience function to rotate a Vec3 using the inverse of self. Returns the resulting vector.

call-seq:

inv_rotate_vec3(vec3, output = nil) -> output or new vec3


4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
# File 'ext/snow-math/snow-math.c', line 4602

static VALUE sm_mat4_inv_rotate_vec3(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  mat4_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (!SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_rhs));
    return Qnil;
  }
  rhs = sm_unwrap_vec3(sm_rhs, NULL);
  if (argc == 2) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    vec3_t *output;
    if (!SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    rb_check_frozen(sm_out);
    output = sm_unwrap_vec3(sm_out, NULL);
    mat4_inv_rotate_vec3(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    mat4_inv_rotate_vec3(*self, *rhs, output);
    sm_out = sm_wrap_vec3(output, rb_obj_class(sm_rhs));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to inverse_rotate_vec3");
  }
  return sm_out;
}

#inverse_rotate_vec3!(rhs) ⇒ Object

Calls #inverse_rotate_vec3(rhs, rhs)

call-seq: inverse_rotate_vec3!(rhs) -> rhs



96
97
98
# File 'lib/snow-math/mat4.rb', line 96

def inverse_rotate_vec3!(rhs)
  inverse_rotate_vec3 rhs, rhs
end

#lengthObject

Returns the length of the Mat4 in components. Result is always 16.

call-seq: length -> fixnum



4226
4227
4228
4229
# File 'ext/snow-math/snow-math.c', line 4226

static VALUE sm_mat4_length (VALUE self)
{
  return SIZET2NUM(sizeof(mat4_t) / sizeof(s_float_t));
}

#load_identityObject

Sets self to the identity matrix.

call-seq:

load_identity -> self


5470
5471
5472
5473
5474
5475
# File 'ext/snow-math/snow-math.c', line 5470

static VALUE sm_mat4_identity(VALUE sm_self)
{
  mat4_t *self = sm_unwrap_mat4(sm_self, NULL);
  mat4_identity(*self);
  return sm_self;
}

#multiply(rhs, out = nil) ⇒ Object Also known as: *

Calls #multiply_mat4, #multiply_vec4, #transform_vec3, and #scale, respectively.

When calling multiply with scalar as rhs, scalar is passed as the value to scale all columns by.

call-seq:

multiply(mat4, output = nil) -> output or new mat4
multiply(vec4, output = nil) -> output or new vec4
multiply(vec3, output = nil) -> output or new vec3
multiply(scalar, output = nil) -> output or new mat4


111
112
113
114
115
116
117
118
119
# File 'lib/snow-math/mat4.rb', line 111

def multiply(rhs, out = nil)
  case rhs
  when ::Snow::Mat4 then multiply_mat4(rhs, out)
  when ::Snow::Vec4 then multiply_vec4(rhs, out)
  when ::Snow::Vec3 then transform_vec3(rhs, out)
  when Numeric      then scale(rhs, rhs, rhs, out)
  else raise TypeError, "Invalid type for RHS"
  end
end

#multiply!(rhs) ⇒ Object

Calls #multiply(rhs, self) when rhs is a scalar or Mat4, otherwise calls #multiply(rhs, rhs).



123
124
125
126
127
128
129
# File 'lib/snow-math/mat4.rb', line 123

def multiply!(rhs)
  multiply rhs, case rhs
    when Mat4, Numeric then self
    when Vec4, Vec3 then rhs
    else raise TypeError, "Invalid type for RHS"
    end
end

#multiply_mat4(*args) ⇒ Object

Multiplies this and another Mat4 together and returns the result.

call-seq:

multiply_mat4(mat4, output = nil) -> output or new mat4


4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
# File 'ext/snow-math/snow-math.c', line 4414

static VALUE sm_mat4_multiply(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  mat4_t *self;
  mat4_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  SM_RAISE_IF_NOT_TYPE(sm_rhs, mat4);
  rhs = sm_unwrap_mat4(sm_rhs, NULL);
  if (argc == 2) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    mat4_t *output;
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    rb_check_frozen(sm_out);
    output = sm_unwrap_mat4(sm_out, NULL);
    mat4_multiply(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    mat4_t output;
    mat4_multiply(*self, *rhs, output);
    sm_out = sm_wrap_mat4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to multiply_mat4");
  }
  return sm_out;
}

#multiply_mat4!(rhs) ⇒ Object

Calls #multiply_mat4(rhs, self)

call-seq: multiply_mat4!(rhs) -> self



68
69
70
# File 'lib/snow-math/mat4.rb', line 68

def multiply_mat4!(rhs)
  multiply_mat4 rhs, self
end

#multiply_vec4(*args) ⇒ Object

Transforms a Vec4 using self and returns the resulting vector.

call-seq:

multiply_vec4(vec4, output = nil) -> output or new vec4


4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
# File 'ext/snow-math/snow-math.c', line 4453

static VALUE sm_mat4_multiply_vec4(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  mat4_t *self;
  vec4_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (!SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_rhs));
    return Qnil;
  }
  rhs = sm_unwrap_vec4(sm_rhs, NULL);
  if (argc == 2) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    vec4_t *output;
    if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    rb_check_frozen(sm_out);
    output = sm_unwrap_vec4(sm_out, NULL);
    mat4_multiply_vec4(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec4_t output;
    mat4_multiply_vec4(*self, *rhs, output);
    sm_out = sm_wrap_vec4(output, rb_obj_class(sm_rhs));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to multiply_vec4");
  }
  return sm_out;
}

#multiply_vec4!(rhs) ⇒ Object

Calls #multiply_vec4(rhs, rhs)

call-seq: multiply_vec4!(rhs) -> rhs



75
76
77
# File 'lib/snow-math/mat4.rb', line 75

def multiply_vec4!(rhs)
  multiply_vec4 rhs, rhs
end

#rotate_vec3(*args) ⇒ Object

Rotates a Vec3 by self, using only the inner 9x9 matrix to transform the vector. Returns the rotated vector.

call-seq:

rotate_vec3(vec3, output = nil) -> output or new vec3


4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
# File 'ext/snow-math/snow-math.c', line 4552

static VALUE sm_mat4_rotate_vec3(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  mat4_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (!SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_rhs));
    return Qnil;
  }
  rhs = sm_unwrap_vec3(sm_rhs, NULL);
  if (argc == 2) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    vec3_t *output;
    if (!SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    rb_check_frozen(sm_out);
    output = sm_unwrap_vec3(sm_out, NULL);
    mat4_rotate_vec3(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    mat4_rotate_vec3(*self, *rhs, output);
    sm_out = sm_wrap_vec3(output, rb_obj_class(sm_rhs));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to rotate_vec3");
  }
  return sm_out;
}

#rotate_vec3!(rhs) ⇒ Object

Calls #inverse_transform_vec3(rhs, rhs)

call-seq: inverse_transform_vec3!(rhs) -> rhs



89
90
91
# File 'lib/snow-math/mat4.rb', line 89

def rotate_vec3!(rhs)
  inverse_transform_vec3 rhs, rhs
end

#scale(*args) ⇒ Object Also known as: **

Scales the inner 9x9 matrix’s columns by X, Y, and Z and returns the result.

call-seq:

scale(x, y, z, output = nil) -> output or new mat4


5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
# File 'ext/snow-math/snow-math.c', line 5659

static VALUE sm_mat4_scale(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  VALUE sm_x, sm_y, sm_z;
  s_float_t x, y, z;
  mat4_t *self = sm_unwrap_mat4(sm_self, NULL);

  rb_scan_args(argc, argv, "31", &sm_x, &sm_y, &sm_z, &sm_out);
  x = rb_num2dbl(sm_x);
  y = rb_num2dbl(sm_y);
  z = rb_num2dbl(sm_z);

  if (SM_IS_A(sm_out, mat4)) {
    rb_check_frozen(sm_out);
    mat4_scale(*self, x, y, z, *sm_unwrap_mat4(sm_out, NULL));
  } else {
    mat4_t out;
    mat4_scale(*self, x, y, z, out);
    sm_out = sm_wrap_mat4(out, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

#scale!(x, y, z) ⇒ Object

Calls #scale(x, y, z, self)

call-seq: scale!(x, y, z) -> self



134
135
136
# File 'lib/snow-math/mat4.rb', line 134

def scale!(x, y, z)
  scale x, y, z, self
end

#set(*args) ⇒ Object

Sets the Mat4’s components.

call-seq:

set(m1, m2, ..., m15, m16)   -> new mat4 with components
set([m1, m2, ..., m15, m16]) -> new mat4 with components
set(mat4)                    -> copy of mat4
set(mat3)                    -> new mat4 with mat3's components
set(quat)                    -> quat as mat4
set(Vec4, Vec4, Vec4, Vec4)  -> new mat4 with given row vectors


4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
# File 'ext/snow-math/snow-math.c', line 4904

static VALUE sm_mat4_init(int argc, VALUE *argv, VALUE sm_self)
{
  mat4_t *self = sm_unwrap_mat4(sm_self, NULL);
  size_t arr_index = 0;

  rb_check_frozen(sm_self);

  switch (argc) {

  case 0: {
    /* Identity (handled in _new) */
    break;
  }

  /* Copy Mat4 or provided [Numeric..] */
  case 1: {
    /* Copy Mat4 */
    if (SM_IS_A(argv[0], mat4)) {
      sm_unwrap_mat4(argv[0], *self);
      break;
    }

    /* Copy Mat3 */
    if (SM_IS_A(argv[0], mat3)) {
      mat3_to_mat4(*sm_unwrap_mat4(argv[0], NULL), *self);
      break;
    }

    /* Build from Quaternion */
    if (SM_IS_A(argv[0], quat)) {
      mat4_from_quat(*sm_unwrap_quat(argv[0], NULL), *self);
      break;
    }

    /* Optional offset into array provided */
    if (0) {
      case 2:
      arr_index = NUM2SIZET(argv[1]);
    }

    /* Array of values */
    if (SM_RB_IS_A(argv[0], rb_cArray)) {
      VALUE arrdata = argv[0];
      const size_t arr_end = arr_index + 16;
      s_float_t *mat_elem = *self;
      for (; arr_index < arr_end; ++arr_index, ++mat_elem) {
        *mat_elem = rb_num2dbl(rb_ary_entry(arrdata, (long)arr_index));
      }
      break;
    }

    rb_raise(rb_eArgError, "Expected either an array of Numerics or a Mat4");
    break;
  }

  /* Mat4(Vec4, Vec4, Vec4, Vec4) */
  case 4: {
    size_t arg_index;
    s_float_t *mat_elem = *self;
    for (arg_index = 0; arg_index < 4; ++arg_index, mat_elem += 4) {
      if (!SM_IS_A(argv[arg_index], vec4) && !SM_IS_A(argv[arg_index], quat)) {
        rb_raise(
          rb_eArgError,
          "Argument %d must be a Vec4 or Quat when supplying four arguments to initialize/set",
          (int)(arg_index + 1));
      }

      sm_unwrap_vec4(argv[arg_index], mat_elem);
    }
    break;
  }

  /* Mat4(Numeric m00 .. m16) */
  case 16: {
    s_float_t *mat_elem = *self;
    VALUE *argv_p = argv;
    for (; argc; --argc, ++argv_p, ++mat_elem) {
      *mat_elem = (s_float_t)rb_num2dbl(*argv_p);
    }
    break;
  }

  default: {
    rb_raise(rb_eArgError, "Invalid arguments to initialize/set");
    break;
  }
  } /* switch (argc) */

  return sm_self;
}

#set_column3(sm_index, sm_value) ⇒ Object

Sets the matrix’s column at the given index to the given vector.

call-seq:

set_column3(index, vec3) -> self


5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
# File 'ext/snow-math/snow-math.c', line 5365

static VALUE sm_mat4_set_column3(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  const vec3_t *value;
  int index;
  mat4_t *self;

  if (!SM_IS_A(sm_value, vec3) && !SM_IS_A(sm_value, vec4) && !SM_IS_A(sm_value, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_value));
    return Qnil;
  }

  self = sm_unwrap_mat4(sm_self, NULL);
  value = sm_unwrap_vec3(sm_value, NULL);
  index = NUM2INT(sm_index);

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  mat4_set_column3(index, *value, *self);

  return sm_self;
}

#set_column4(sm_index, sm_value) ⇒ Object

Sets the matrix’s column at the given index to the given vector.

call-seq:

set_column4(index, vec4) -> self


5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
# File 'ext/snow-math/snow-math.c', line 5435

static VALUE sm_mat4_set_column4(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  const vec4_t *value;
  int index;
  mat4_t *self;

  if (!SM_IS_A(sm_value, vec4) && !SM_IS_A(sm_value, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_value));
      return Qnil;
    }

  self = sm_unwrap_mat4(sm_self, NULL);
  value = sm_unwrap_vec4(sm_value, NULL);
  index = NUM2INT(sm_index);

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  mat4_set_column4(index, *value, *self);

  return sm_self;
}

#set_row3(sm_index, sm_value) ⇒ Object

Sets the matrix’s row at the given index to the given vector.

call-seq:

set_row3(index, vec3) -> self


5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
# File 'ext/snow-math/snow-math.c', line 5330

static VALUE sm_mat4_set_row3(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  const vec3_t *value;
  int index;
  mat4_t *self;

  if (!SM_IS_A(sm_value, vec3) && !SM_IS_A(sm_value, vec4) && !SM_IS_A(sm_value, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_value));
    return Qnil;
  }

  self = sm_unwrap_mat4(sm_self, NULL);
  value = sm_unwrap_vec3(sm_value, NULL);
  index = NUM2INT(sm_index);

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  mat4_set_row3(index, *value, *self);

  return sm_self;
}

#set_row4(sm_index, sm_value) ⇒ Object

Sets the matrix’s row at the given index to the given vector.

call-seq:

set_row4(index, vec4) -> self


5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
# File 'ext/snow-math/snow-math.c', line 5400

static VALUE sm_mat4_set_row4(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  const vec4_t *value;
  int index;
  mat4_t *self;

  if (!SM_IS_A(sm_value, vec4) && !SM_IS_A(sm_value, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_value));
    return Qnil;
  }

  self = sm_unwrap_mat4(sm_self, NULL);
  value = sm_unwrap_vec4(sm_value, NULL);
  index = NUM2INT(sm_index);

  if (index < 0 || index > 3) {
    rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 3)", index);
    return Qnil;
  }

  mat4_set_row4(index, *value, *self);

  return sm_self;
}

#sizeObject

Returns the length in bytes of the Mat4. When compiled to use doubles as the base type, this is always 128. Otherwise, when compiled to use floats, it’s always 64.

call-seq: size -> fixnum



4214
4215
4216
4217
# File 'ext/snow-math/snow-math.c', line 4214

static VALUE sm_mat4_size (VALUE self)
{
  return SIZET2NUM(sizeof(mat4_t));
}

#storeObject Also known as: []=

Sets the Mat4’s component at the index to the value.

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



4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
# File 'ext/snow-math/snow-math.c', line 4191

static VALUE sm_mat4_store (VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  static const int max_index = sizeof(mat4_t) / sizeof(s_float_t);
  mat4_t *self = sm_unwrap_mat4(sm_self, NULL);
  int index = NUM2INT(sm_index);
  rb_check_frozen(sm_self);
  if (index < 0 || index >= max_index) {
    rb_raise(rb_eRangeError,
      "Index %d is out of bounds, must be from 0 through %d", index, max_index - 1);
  }
  self[0][index] = (s_float_t)rb_num2dbl(sm_value);
  return sm_value;
}

#to_mat3(*args) ⇒ Object

Converts the Mat4 to a Mat3.

call-seq:

to_mat3(output = nil) -> output or new mat3


4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
# File 'ext/snow-math/snow-math.c', line 4274

static VALUE sm_mat4_to_mat3(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  mat4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (argc == 1) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    mat3_t *output;
    SM_RAISE_IF_NOT_TYPE(sm_out, mat3);
    rb_check_frozen(sm_out);
    output = sm_unwrap_mat3(sm_out, NULL);
    mat4_to_mat3 (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    mat3_t output;
    mat4_to_mat3 (*self, output);
    sm_out = sm_wrap_mat3(output, s_sm_mat4_klass);
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to to_mat3");
  }
  return sm_out;
}

#to_quatObject



40
41
42
# File 'lib/snow-math/mat4.rb', line 40

def to_quat
  Quat.new(self)
end

#to_sObject

Returns a string representation of self.

Mat4[].to_s     # => "{ 1.0, 0.0, 0.0, 0.0,\n
                #       0.0, 1.0, 0.0, 0.0,\n"
                #       0.0, 0.0, 1.0, 0.0,\n"
                #       0.0, 0.0, 0.0, 1.0 }"

call-seq:

to_s -> string


5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
# File 'ext/snow-math/snow-math.c', line 5008

static VALUE sm_mat4_to_s(VALUE self)
{
  const s_float_t *v;
  v = (const s_float_t *)*sm_unwrap_mat4(self, NULL);
  return rb_sprintf(
    "{ "
    "%f, %f, %f, %f" ",\n  "
    "%f, %f, %f, %f" ",\n  "
    "%f, %f, %f, %f" ",\n  "
    "%f, %f, %f, %f"
    " }",
    v[0], v[1], v[2], v[3],
    v[4], v[5], v[6], v[7],
    v[8], v[9], v[10], v[11],
    v[12], v[13], v[14], v[15]);
}

#transform_vec3(*args) ⇒ Object

Transforms a Vec3 using self and returns the resulting vector.

call-seq:

transform_vec3(vec3, output = nil) -> output or new vec3


4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
# File 'ext/snow-math/snow-math.c', line 4502

static VALUE sm_mat4_transform_vec3(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  mat4_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (!SM_IS_A(sm_rhs, vec3) && !SM_IS_A(sm_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
    rb_raise(rb_eTypeError,
      kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_rhs));
    return Qnil;
  }
  rhs = sm_unwrap_vec3(sm_rhs, NULL);
  if (argc == 2) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    vec3_t *output;
    if (!SM_IS_A(sm_out, vec3) && !SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_THREE_OR_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    rb_check_frozen(sm_out);
    output = sm_unwrap_vec3(sm_out, NULL);
    mat4_transform_vec3(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    mat4_transform_vec3(*self, *rhs, output);
    sm_out = sm_wrap_vec3(output, rb_obj_class(sm_rhs));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to transform_vec3");
  }
  return sm_out;
}

#transform_vec3!(rhs) ⇒ Object

Calls #transform_vec3(rhs, rhs)

call-seq: transform_vec3!(rhs) -> rhs



82
83
84
# File 'lib/snow-math/mat4.rb', line 82

def transform_vec3!(rhs)
  transform_vec3 rhs, rhs
end

#translate(*args) ⇒ Object

Translates this matrix by X, Y, and Z (or a Vec3’s X, Y, and Z components) and returns the result. Essentially the same as multiplying this matrix by a translation matrix, but slightly more convenient.

call-seq:

translate(x, y, z, output = nil) -> output or new mat4
translate(vec3, output = nil)    -> output or new mat4


4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
# File 'ext/snow-math/snow-math.c', line 4775

static VALUE sm_mat4_translate(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out = Qnil;
  mat4_t *self = sm_unwrap_mat4(sm_self, NULL);
  vec3_t xyz;

  SM_LABEL(argc_reconfig):
  switch (argc) {
  case 2: case 4: {
    sm_out = argv[--argc];
    if (RTEST(sm_out)) {
      SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    }
    goto SM_LABEL(argc_reconfig);
  }

  case 1: {
    sm_unwrap_vec3(argv[0], xyz);
    goto SM_LABEL(get_output);
  }

  case 3: {
    xyz[0] = rb_num2dbl(argv[0]);
    xyz[1] = rb_num2dbl(argv[1]);
    xyz[2] = rb_num2dbl(argv[2]);

    SM_LABEL(get_output):
    if (RTEST(sm_out)) {
      rb_check_frozen(sm_out);
      mat4_t *out = sm_unwrap_mat4(sm_out, NULL);
      mat4_translate(xyz[0], xyz[1], xyz[2], *self, *out);
    } else {
      mat4_t out;
      mat4_translate(xyz[0], xyz[1], xyz[2], *self, out);
      sm_out = sm_wrap_mat4(out, rb_obj_class(sm_self));
      rb_obj_call_init(sm_out, 0, 0);
    }
  }
  }

  return sm_out;
}

#translate!(*args) ⇒ Object

Calls #translate(*args, self)

call-seq: translate!(vec3) -> self call-seq: translate!(x, y, z) -> self



142
143
144
# File 'lib/snow-math/mat4.rb', line 142

def translate!(*args)
  translate(*args, self)
end

#transpose(*args) ⇒ Object Also known as: ~

Transposes this matrix and returns the result.

call-seq:

transpose(output = nil) -> output or new mat4


4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
# File 'ext/snow-math/snow-math.c', line 4309

static VALUE sm_mat4_transpose(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  mat4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_mat4(sm_self, NULL);
  if (argc == 1) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    mat4_t *output;
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    rb_check_frozen(sm_out);
    output = sm_unwrap_mat4(sm_out, NULL);
    mat4_transpose (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    mat4_t output;
    mat4_transpose (*self, output);
    sm_out = sm_wrap_mat4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to transpose");
  }
  return sm_out;
}

#transpose!Object

Calls #transpose(self)

call-seq: transpose! -> self



47
48
49
# File 'lib/snow-math/mat4.rb', line 47

def transpose!
  transpose self
end