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
SIZE =
INT2FIX(sizeof(mat4_t))
LENGTH =
INT2FIX(sizeof(mat4_t) / sizeof(s_float_t))

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


4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
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
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
# File 'ext/snow-math/snow-math.c', line 4960

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


5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
# File 'ext/snow-math/snow-math.c', line 5089

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


5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
# File 'ext/snow-math/snow-math.c', line 5541

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)NUM2DBL(sm_left);
  right = (s_float_t)NUM2DBL(sm_right);
  bottom = (s_float_t)NUM2DBL(sm_bottom);
  top = (s_float_t)NUM2DBL(sm_top);
  z_near = (s_float_t)NUM2DBL(sm_z_near);
  z_far = (s_float_t)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


5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
# File 'ext/snow-math/snow-math.c', line 5677

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


4940
4941
4942
4943
4944
4945
# File 'ext/snow-math/snow-math.c', line 4940

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


5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
# File 'ext/snow-math/snow-math.c', line 5588

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)NUM2DBL(sm_left);
  right = (s_float_t)NUM2DBL(sm_right);
  bottom = (s_float_t)NUM2DBL(sm_bottom);
  top = (s_float_t)NUM2DBL(sm_top);
  z_near = (s_float_t)NUM2DBL(sm_z_near);
  z_far = (s_float_t)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


5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
# File 'ext/snow-math/snow-math.c', line 5635

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)NUM2DBL(sm_fov_y);
  aspect = (s_float_t)NUM2DBL(sm_aspect);
  z_near = (s_float_t)NUM2DBL(sm_z_near);
  z_far = (s_float_t)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


4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
# File 'ext/snow-math/snow-math.c', line 4884

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] = NUM2DBL(argv[0]);
    xyz[1] = NUM2DBL(argv[1]);
    xyz[2] = 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


5748
5749
5750
5751
5752
5753
5754
5755
# File 'ext/snow-math/snow-math.c', line 5748

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



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

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

#adjoint(*args) ⇒ Object

Returns an adjoint matrix.

call-seq:

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


4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
# File 'ext/snow-math/snow-math.c', line 4435

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


4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
# File 'ext/snow-math/snow-math.c', line 4295

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


4815
4816
4817
4818
# File 'ext/snow-math/snow-math.c', line 4815

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



4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
# File 'ext/snow-math/snow-math.c', line 4228

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 DBL2NUM(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


5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
# File 'ext/snow-math/snow-math.c', line 5258

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


5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
# File 'ext/snow-math/snow-math.c', line 5322

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


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

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


5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
# File 'ext/snow-math/snow-math.c', line 5194

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


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

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


4761
4762
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
4803
4804
4805
# File 'ext/snow-math/snow-math.c', line 4761

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


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

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


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

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



4282
4283
4284
4285
# File 'ext/snow-math/snow-math.c', line 4282

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


5526
5527
5528
5529
5530
5531
# File 'ext/snow-math/snow-math.c', line 5526

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


4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
# File 'ext/snow-math/snow-math.c', line 4470

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


4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
# File 'ext/snow-math/snow-math.c', line 4509

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

#orthogonal?Boolean

call-seq:

orthogonal? -> true or false

Returns whether self is an orthogonal matrix (its columns and rows are all unit vectors). Note that this allocates a new matrix.

Returns:

  • (Boolean)


200
201
202
203
# File 'lib/snow-math/mat4.rb', line 200

def orthogonal?
  temp = self.transpose
  multiply_mat4(temp, temp) == IDENTITY
end

#pitchObject

Returns the pitch (X-axis rotation) of this matrix in degrees. This assumes the matrix is orthogonal.



169
170
171
172
173
174
175
# File 'lib/snow-math/mat4.rb', line 169

def pitch
  tx = self[8]
  tz = self[10]
  Math::atan2(
    self[9],
    Math::sqrt(tx * tx + tz * tz)) * ::Snow::RADIANS_TO_DEGREES
end

#rollObject

Returns the roll (Z-axis rotation) of this matrix in degrees. This assumes the matrix is orthogonal.



189
190
191
# File 'lib/snow-math/mat4.rb', line 189

def roll
  Math::atan2(self[1], self[5]) * ::Snow::RADIANS_TO_DEGREES
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


4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
# File 'ext/snow-math/snow-math.c', line 4608

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


5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
# File 'ext/snow-math/snow-math.c', line 5715

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 = NUM2DBL(sm_x);
  y = NUM2DBL(sm_y);
  z = 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


4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
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
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
# File 'ext/snow-math/snow-math.c', line 4960

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


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

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


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

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


5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
# File 'ext/snow-math/snow-math.c', line 5386

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


5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
# File 'ext/snow-math/snow-math.c', line 5456

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



4270
4271
4272
4273
# File 'ext/snow-math/snow-math.c', line 4270

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



4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
# File 'ext/snow-math/snow-math.c', line 4247

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


4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
# File 'ext/snow-math/snow-math.c', line 4330

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


5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
# File 'ext/snow-math/snow-math.c', line 5064

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


4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
# File 'ext/snow-math/snow-math.c', line 4558

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


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

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] = NUM2DBL(argv[0]);
    xyz[1] = NUM2DBL(argv[1]);
    xyz[2] = 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


4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
# File 'ext/snow-math/snow-math.c', line 4365

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

#yawObject

Returns the yaw (Y-axis rotation) of this matrix in degrees. This assumes the matrix is orthogonal.



181
182
183
# File 'lib/snow-math/mat4.rb', line 181

def yaw
  -Math::atan2(self[8], self[10]) * ::Snow::RADIANS_TO_DEGREES
end