Class: Snow::Mat4

Inherits:
Object
  • 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


3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
# File 'ext/snow-math/snow-math.c', line 3823

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;

  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 Mat4.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 Mat4.initialize");
    break;
  }
  } // swtich (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


3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
# File 'ext/snow-math/snow-math.c', line 3950

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


4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
# File 'ext/snow-math/snow-math.c', line 4397

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


4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
# File 'ext/snow-math/snow-math.c', line 4530

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


3803
3804
3805
3806
3807
3808
# File 'ext/snow-math/snow-math.c', line 3803

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


4443
4444
4445
4446
4447
4448
4449
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
4476
4477
4478
4479
# File 'ext/snow-math/snow-math.c', line 4443

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


4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
# File 'ext/snow-math/snow-math.c', line 4489

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


3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
# File 'ext/snow-math/snow-math.c', line 3748

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


4599
4600
4601
4602
4603
4604
4605
4606
# File 'ext/snow-math/snow-math.c', line 4599

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



5564
5565
5566
5567
5568
5569
# File 'ext/snow-math/snow-math.c', line 5564

static VALUE sm_get_address(VALUE sm_self)
{
  void *data_ptr = NULL;
  Data_Get_Struct(sm_self, void, data_ptr);
  return ULL2NUM((unsigned long long)data_ptr);
}

#adjoint(*args) ⇒ Object

Returns an adjoint matrix.

call-seq:

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


3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
# File 'ext/snow-math/snow-math.c', line 3314

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);
    }{
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    mat4_t *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



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

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


3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
# File 'ext/snow-math/snow-math.c', line 3182

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);
    }{
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    mat4_t *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


3680
3681
3682
3683
# File 'ext/snow-math/snow-math.c', line 3680

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



3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
# File 'ext/snow-math/snow-math.c', line 3116

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


4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
# File 'ext/snow-math/snow-math.c', line 4116

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


4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
# File 'ext/snow-math/snow-math.c', line 4179

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


3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
# File 'ext/snow-math/snow-math.c', line 3990

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


4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
# File 'ext/snow-math/snow-math.c', line 4053

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


3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
# File 'ext/snow-math/snow-math.c', line 3574

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

    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



145
146
147
# File 'lib/snow-math/mat4.rb', line 145

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


3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
# File 'ext/snow-math/snow-math.c', line 3627

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

    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



152
153
154
# File 'lib/snow-math/mat4.rb', line 152

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


3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
# File 'ext/snow-math/snow-math.c', line 3281

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);
    }{
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    mat4_t *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



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

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


3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
# File 'ext/snow-math/snow-math.c', line 3527

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);
    }{
    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;
    }
    vec3_t *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 vec3");
  }
  return sm_out;
}

#inverse_rotate_vec3!(rhs) ⇒ Object

Calls #inverse_rotate_vec3(rhs, rhs)

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



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

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



3169
3170
3171
3172
# File 'ext/snow-math/snow-math.c', line 3169

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


4382
4383
4384
4385
4386
4387
# File 'ext/snow-math/snow-math.c', line 4382

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


107
108
109
110
111
112
113
114
115
# File 'lib/snow-math/mat4.rb', line 107

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



119
120
121
122
123
124
125
# File 'lib/snow-math/mat4.rb', line 119

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


3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
# File 'ext/snow-math/snow-math.c', line 3347

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);
    }{
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    mat4_t *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 mat4");
  }
  return sm_out;
}

#multiply_mat4!(rhs) ⇒ Object

Calls #multiply_mat4(rhs, self)

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



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

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


3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
# File 'ext/snow-math/snow-math.c', line 3384

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);
    }{
    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;
    }
    vec4_t *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 vec4");
  }
  return sm_out;
}

#multiply_vec4!(rhs) ⇒ Object

Calls #multiply_vec4(rhs, rhs)

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



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

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


3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
# File 'ext/snow-math/snow-math.c', line 3479

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);
    }{
    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;
    }
    vec3_t *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 vec3");
  }
  return sm_out;
}

#rotate_vec3!(rhs) ⇒ Object

Calls #inverse_transform_vec3(rhs, rhs)

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



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

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


4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
# File 'ext/snow-math/snow-math.c', line 4567

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



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

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


3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
# File 'ext/snow-math/snow-math.c', line 3823

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;

  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 Mat4.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 Mat4.initialize");
    break;
  }
  } // swtich (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


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

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


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

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


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

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


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

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



3157
3158
3159
3160
# File 'ext/snow-math/snow-math.c', line 3157

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



3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
# File 'ext/snow-math/snow-math.c', line 3135

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


3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
# File 'ext/snow-math/snow-math.c', line 3215

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);
    }{
    SM_RAISE_IF_NOT_TYPE(sm_out, mat3);
    mat3_t *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_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


3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
# File 'ext/snow-math/snow-math.c', line 3925

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


3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
# File 'ext/snow-math/snow-math.c', line 3431

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);
    }{
    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;
    }
    vec3_t *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 vec3");
  }
  return sm_out;
}

#transform_vec3!(rhs) ⇒ Object

Calls #transform_vec3(rhs, rhs)

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



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

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


3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
# File 'ext/snow-math/snow-math.c', line 3696

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



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

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


3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
# File 'ext/snow-math/snow-math.c', line 3248

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);
    }{
    SM_RAISE_IF_NOT_TYPE(sm_out, mat4);
    mat4_t *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



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

def transpose!
  transpose self
end