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.

Constant Summary collapse

IDENTITY =
self.new.freeze
ONE =
self.new(Array.new(16, 1)).freeze
ZERO =
self.new(Array.new(16, 0)).freeze

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


5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
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
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
# File 'ext/snow-math/snow-math.c', line 5115

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


5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
# File 'ext/snow-math/snow-math.c', line 5244

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


5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
# File 'ext/snow-math/snow-math.c', line 5696

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


5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
# File 'ext/snow-math/snow-math.c', line 5832

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


5095
5096
5097
5098
5099
5100
# File 'ext/snow-math/snow-math.c', line 5095

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


5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
# File 'ext/snow-math/snow-math.c', line 5743

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


5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
# File 'ext/snow-math/snow-math.c', line 5790

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


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
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
# File 'ext/snow-math/snow-math.c', line 5039

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


5903
5904
5905
5906
5907
5908
5909
5910
# File 'ext/snow-math/snow-math.c', line 5903

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



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

#adjoint(*args) ⇒ Object

Returns an adjoint matrix.

call-seq:

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


4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
# File 'ext/snow-math/snow-math.c', line 4590

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



65
66
67
# File 'lib/snow-math/mat4.rb', line 65

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


4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
# File 'ext/snow-math/snow-math.c', line 4450

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


4970
4971
4972
4973
# File 'ext/snow-math/snow-math.c', line 4970

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



4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
# File 'ext/snow-math/snow-math.c', line 4383

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


5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
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
5461
5462
5463
5464
5465
5466
5467
# File 'ext/snow-math/snow-math.c', line 5413

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


5477
5478
5479
5480
5481
5482
5483
5484
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
5523
5524
5525
5526
5527
5528
5529
5530
5531
# File 'ext/snow-math/snow-math.c', line 5477

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


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
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
# File 'ext/snow-math/snow-math.c', line 5285

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


5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
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
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
# File 'ext/snow-math/snow-math.c', line 5349

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


4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
# File 'ext/snow-math/snow-math.c', line 4862

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



154
155
156
# File 'lib/snow-math/mat4.rb', line 154

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


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
# File 'ext/snow-math/snow-math.c', line 4916

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



161
162
163
# File 'lib/snow-math/mat4.rb', line 161

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


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
# File 'ext/snow-math/snow-math.c', line 4555

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



58
59
60
# File 'lib/snow-math/mat4.rb', line 58

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


4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
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
# File 'ext/snow-math/snow-math.c', line 4813

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



100
101
102
# File 'lib/snow-math/mat4.rb', line 100

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



4437
4438
4439
4440
# File 'ext/snow-math/snow-math.c', line 4437

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


5681
5682
5683
5684
5685
5686
# File 'ext/snow-math/snow-math.c', line 5681

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


115
116
117
118
119
120
121
122
123
# File 'lib/snow-math/mat4.rb', line 115

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).



127
128
129
130
131
132
133
# File 'lib/snow-math/mat4.rb', line 127

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


4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
# File 'ext/snow-math/snow-math.c', line 4625

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



72
73
74
# File 'lib/snow-math/mat4.rb', line 72

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


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
4696
4697
4698
4699
4700
4701
4702
4703
# File 'ext/snow-math/snow-math.c', line 4664

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



79
80
81
# File 'lib/snow-math/mat4.rb', line 79

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


4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
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
# File 'ext/snow-math/snow-math.c', line 4763

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



93
94
95
# File 'lib/snow-math/mat4.rb', line 93

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


5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
# File 'ext/snow-math/snow-math.c', line 5870

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



138
139
140
# File 'lib/snow-math/mat4.rb', line 138

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


5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
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
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
# File 'ext/snow-math/snow-math.c', line 5115

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


5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
# File 'ext/snow-math/snow-math.c', line 5576

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


5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
# File 'ext/snow-math/snow-math.c', line 5646

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


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
# File 'ext/snow-math/snow-math.c', line 5541

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


5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
# File 'ext/snow-math/snow-math.c', line 5611

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



4425
4426
4427
4428
# File 'ext/snow-math/snow-math.c', line 4425

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



4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
# File 'ext/snow-math/snow-math.c', line 4402

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


4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
# File 'ext/snow-math/snow-math.c', line 4485

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



44
45
46
# File 'lib/snow-math/mat4.rb', line 44

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


5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
# File 'ext/snow-math/snow-math.c', line 5219

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


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
4750
4751
4752
# File 'ext/snow-math/snow-math.c', line 4713

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



86
87
88
# File 'lib/snow-math/mat4.rb', line 86

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


4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
# File 'ext/snow-math/snow-math.c', line 4986

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
translate!(x, y, z) -> self


147
148
149
# File 'lib/snow-math/mat4.rb', line 147

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


4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
# File 'ext/snow-math/snow-math.c', line 4520

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



51
52
53
# File 'lib/snow-math/mat4.rb', line 51

def transpose!
  transpose self
end