Class: Snow::Quat

Inherits:
Data
  • Object
show all
Includes:
ArraySupport, BaseMarshalSupport, FiddlePointerSupport, InspectSupport, SwizzleSupport
Defined in:
lib/snow-math/quat.rb,
lib/snow-math/ptr.rb,
lib/snow-math/to_a.rb,
lib/snow-math/inspect.rb,
lib/snow-math/marshal.rb,
lib/snow-math/swizzle.rb,
ext/snow-math/snow-math.c

Overview

A simple quaternion class for representation rotations.

Constant Summary collapse

POS_X =
self.new(1, 0, 0, 1).freeze
POS_Y =
self.new(0, 1, 0, 1).freeze
POS_Z =
self.new(0, 0, 1, 1).freeze
NEG_X =
self.new(-1, 0, 0, 1).freeze
NEG_Y =
self.new(0, -1, 0, 1).freeze
NEG_Z =
self.new(0, 0, -1, 1).freeze
ONE =
self.new(1, 1, 1, 1).freeze
ZERO =
self.new(0, 0, 0, 0).freeze
IDENTITY =
self.new(0, 0, 0, 1).freeze
@@SWIZZLE_CHARS =
/^[xyzw]{2,4}$/
@@SWIZZLE_MAPPING =
{ 2 => ::Snow::Vec2, 3 => ::Snow::Vec3, 4 => self, 'x' => 0, 'y' => 1, 'z' => 2, 'w' => 3 }

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SwizzleSupport

#__under_method_missing__, #method_missing

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 Quat’s components.

call-seq:

set(x, y, z, w = 1) -> new quaternion with components [x, y, z, w]
set([x, y, z, w])   -> new quaternion with components [x, y, z, w]
set(quat)           -> copy of quat
set(vec2)           -> new quaternion with the components [vec2.xy, 0, 1]
set(vec3)           -> new quaternion with the components [vec3.xyz, 1]
set(vec4)           -> new quaternion with the components of vec4
set(mat3)           -> new quaternion from mat3
set(mat4)           -> new quaternion from mat4


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
4170
4171
4172
4173
4174
4175
4176
4177
4178
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
# File 'ext/snow-math/snow-math.c', line 4138

static VALUE sm_quat_init(int argc, VALUE *argv, VALUE sm_self)
{
  quat_t *self = sm_unwrap_quat(sm_self, NULL);
  size_t arr_index = 0;

  rb_check_frozen(sm_self);

  switch(argc) {

  /* Default value */
  case 0: { break; }

  /* Copy or by-array */
  case 1: {
    if (SM_IS_A(argv[0], quat) ||
        SM_IS_A(argv[0], vec4)) {
      sm_unwrap_quat(argv[0], *self);
      break;
    }

    if (SM_IS_A(argv[0], vec2)) {
      sm_unwrap_vec2(argv[0], *self);
      self[0][2] = s_float_lit(0.0);
      self[0][3] = s_float_lit(1.0);
      break;
    }

    if (SM_IS_A(argv[0], vec3)) {
      sm_unwrap_vec3(argv[0], *self);
      self[0][3] = s_float_lit(1.0);
      break;
    }

    if (SM_IS_A(argv[0], mat4)) {
      const mat4_t *mat = sm_unwrap_mat4(argv[0], NULL);
      quat_from_mat4(*mat, *self);
      break;
    }

    if (SM_IS_A(argv[0], mat3)) {
      const mat3_t *mat = sm_unwrap_mat3(argv[0], NULL);
      quat_from_mat3(*mat, *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 + 3;
      s_float_t *vec_elem = *self;
      for (; arr_index < arr_end; ++arr_index, ++vec_elem) {
        *vec_elem = (s_float_t)rb_num2dbl(rb_ary_entry(arrdata, (long)arr_index));
      }
      break;
    }

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

  /* W */
  case 4: {
    self[0][3] = (s_float_t)rb_num2dbl(argv[3]);
    case 3: /* X, Y, Z */
    self[0][0] = (s_float_t)rb_num2dbl(argv[0]);
    self[0][1] = (s_float_t)rb_num2dbl(argv[1]);
    self[0][2] = (s_float_t)rb_num2dbl(argv[2]);
    break;
  }

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

  return sm_self;
}

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Snow::SwizzleSupport

Class Method Details

.angle_axis(*args) ⇒ Object

Returns a quaternion describing a rotation around a given axis.

call-seq:

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


4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
# File 'ext/snow-math/snow-math.c', line 4252

static VALUE sm_quat_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, quat) || SM_IS_A(sm_out, vec4)) {
    rb_check_frozen(sm_out);
    quat_t *out = sm_unwrap_quat(sm_out, NULL);
    quat_from_angle_axis(angle, (*axis)[0], (*axis)[1], (*axis)[2], *out);
  } else {
    quat_t out;
    quat_from_angle_axis(angle, (*axis)[0], (*axis)[1], (*axis)[2], out);
    sm_out = sm_wrap_quat(out, self);
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

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

Allocates a new Quat.

call-seq:

new()               -> new identity quaternion
new(x, y, z, w = 1) -> new quaternion with components [x, y, z, w]
new([x, y, z, w])   -> new quaternion with components [x, y, z, w]
new(quat)           -> copy of quat
new(vec3)           -> new quaternion with the components [vec3.xyz, 1]
new(vec4)           -> new quaternion with the components of vec4
new(mat3)           -> new quaternion from mat3
new(mat4)           -> new quaternion from mat4


4116
4117
4118
4119
4120
4121
# File 'ext/snow-math/snow-math.c', line 4116

static VALUE sm_quat_new(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_quat = sm_wrap_quat(g_quat_identity, self);
  rb_obj_call_init(sm_quat, argc, argv);
  return sm_quat;
}

Instance Method Details

#==(sm_other) ⇒ Object

Tests this Vec4 or Quat and another Vec4 or Quat for equivalency.

call-seq:

quat == other_quat -> bool
vec4 == other_vec4 -> bool
quat == vec4       -> bool
vec4 == quat       -> bool


3855
3856
3857
3858
3859
3860
3861
3862
# File 'ext/snow-math/snow-math.c', line 3855

static VALUE sm_vec4_equals(VALUE sm_self, VALUE sm_other)
{
  if (!RTEST(sm_other) || (!SM_IS_A(sm_other, vec4) && !SM_IS_A(sm_other, quat))) {
    return Qfalse;
  }

  return vec4_equals(*sm_unwrap_vec4(sm_self, NULL), *sm_unwrap_vec4(sm_other, NULL)) ? Qtrue : Qfalse;
}

#add(*args) ⇒ Object Also known as: +

Adds this and another vector or quaternion’s components together and returns the result. The result type is that of the receiver.

call-seq:

add(vec4, output = nil) -> output or new vec4 or quat


3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
# File 'ext/snow-math/snow-math.c', line 3511

static VALUE sm_vec4_add(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec4_t *self;
  vec4_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec4(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_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_rhs));
      return Qnil;
    }
    rb_check_frozen(sm_out);
    output = sm_unwrap_vec4(sm_out, NULL);
    vec4_add(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_add(*self, *rhs, output);
    sm_out = sm_wrap_vec4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to add");
  }
  return sm_out;
}

#add!(rhs) ⇒ Object

Calls #add(rhs, self)

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



193
194
195
# File 'lib/snow-math/quat.rb', line 193

def add!(rhs)
  add rhs, self
end

#addressObject

Returns the memory address of the object.

call-seq: address -> fixnum



6894
6895
6896
6897
6898
6899
# File 'ext/snow-math/snow-math.c', line 6894

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

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

Returns a copy of self.

call-seq:

copy(output = nil) -> output or new vec4 / quat


3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
# File 'ext/snow-math/snow-math.c', line 3201

static VALUE sm_vec4_copy(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  vec4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_vec4(sm_self, NULL);
  if (argc == 1) {
    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);
    vec4_copy (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_copy (*self, output);
    sm_out = sm_wrap_vec4(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;
}

#divide(*args) ⇒ Object Also known as: /

Divides this vector or quaternion’s components by a scalar value and returns the result. The return type is that of the receiver.

call-seq:

divide(scalar, output = nil) -> output or new vec4


3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
# File 'ext/snow-math/snow-math.c', line 3821

static VALUE sm_vec4_divide(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  VALUE sm_scalar;
  s_float_t scalar;
  vec4_t *self = sm_unwrap_vec4(sm_self, NULL);

  rb_scan_args(argc, argv, "11", &sm_scalar, &sm_out);
  scalar = rb_num2dbl(sm_scalar);

  if ((SM_IS_A(sm_out, vec4) || SM_IS_A(sm_out, quat))) {
    rb_check_frozen(sm_out);
    vec4_divide(*self, scalar, *sm_unwrap_vec4(sm_out, NULL));
  } else {
    vec4_t out;
    vec4_divide(*self, scalar, out);
    sm_out = sm_wrap_vec4(out, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

#divide!(rhs) ⇒ Object

Calls #divide(rhs, self)

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



214
215
216
# File 'lib/snow-math/quat.rb', line 214

def divide!(rhs)
  divide rhs, self
end

#dot_product(sm_other) ⇒ Object Also known as: **

Returns the dot product of self and another Vec4 or Quat.

call-seq:

dot_product(vec4) -> float
dot_product(quat) -> float


3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
# File 'ext/snow-math/snow-math.c', line 3612

static VALUE sm_vec4_dot_product(VALUE sm_self, VALUE sm_other)
{
  if (!SM_IS_A(sm_other, vec4) &&
      !SM_IS_A(sm_other, quat)) {
    rb_raise(rb_eArgError,
      kSM_WANT_FOUR_FORMAT_LIT,
      rb_obj_classname(sm_other));
    return Qnil;
  }
  return rb_float_new(
    vec4_dot_product(
      *sm_unwrap_vec4(sm_self, NULL),
      *sm_unwrap_vec4(sm_other, NULL)));
}

#fetchObject Also known as: []

Gets the component of the Quat at the given index.

call-seq: fetch(index) -> float



3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
# File 'ext/snow-math/snow-math.c', line 3903

static VALUE sm_quat_fetch (VALUE sm_self, VALUE sm_index)
{
  static const int max_index = sizeof(quat_t) / sizeof(s_float_t);
  const quat_t *self = sm_unwrap_quat(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)]);
}

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

Returns the inverse of this Quat. Note that this is not the same as the inverse of, for example, a Vec4.

call-seq:

inverse(output = nil) -> output or new quat


3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
# File 'ext/snow-math/snow-math.c', line 3971

static VALUE sm_quat_inverse(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  quat_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_quat(sm_self, NULL);
  if (argc == 1) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    quat_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_quat(sm_out, NULL);
    quat_inverse (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    quat_t output;
    quat_inverse (*self, output);
    sm_out = sm_wrap_quat(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");
  }
  return sm_out;
}

#inverse!Object

Calls #inverse(self)

call-seq: inverse! -> self



136
137
138
# File 'lib/snow-math/quat.rb', line 136

def inverse!
  inverse self
end

#lengthObject

Returns the length of the Quat in components. Result is always 4.

call-seq: length -> fixnum



3957
3958
3959
3960
# File 'ext/snow-math/snow-math.c', line 3957

static VALUE sm_quat_length (VALUE self)
{
  return SIZET2NUM(sizeof(quat_t) / sizeof(s_float_t));
}

#load_identityObject

Sets self to the identity quaternion.

call-seq:

load_identity -> self


4293
4294
4295
4296
4297
4298
# File 'ext/snow-math/snow-math.c', line 4293

static VALUE sm_quat_identity(VALUE sm_self)
{
  quat_t *self = sm_unwrap_quat(sm_self, NULL);
  quat_identity(*self);
  return sm_self;
}

#magnitudeObject

Returns the magnitude of self.

call-seq:

magnitude -> float


3775
3776
3777
3778
# File 'ext/snow-math/snow-math.c', line 3775

static VALUE sm_vec4_magnitude(VALUE sm_self)
{
  return rb_float_new(vec4_length(*sm_unwrap_vec4(sm_self, NULL)));
}

#magnitude_squaredObject

Returns the squared magnitude of self.

call-seq:

magnitude_squared -> float


3762
3763
3764
3765
# File 'ext/snow-math/snow-math.c', line 3762

static VALUE sm_vec4_magnitude_squared(VALUE sm_self)
{
  return rb_float_new(vec4_length_squared(*sm_unwrap_vec4(sm_self, NULL)));
}

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

Wrapper around #multiply_quat, #multiply_vec3, and #scale respectively.

call-seq:

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


167
168
169
170
171
172
173
174
# File 'lib/snow-math/quat.rb', line 167

def multiply(rhs, output = nil)
  case rhs
  when ::Snow::Quat then multiply_quat(rhs, output)
  when ::Snow::Vec3 then multiply_vec3(rhs, output)
  when Numeric then scale(rhs, output)
  else raise TypeError, "Invalid type for RHS"
  end
end

#multiply!(rhs) ⇒ Object

Calls #multiply(rhs, self) for scaling and Quat multiplication, otherwise calls #multiply(rhs, rhs) for Vec3 multiplication.

call-seq:

multiply!(quat) -> self
multiply!(scalar) -> self
multiply!(vec3) -> vec3


183
184
185
186
187
188
# File 'lib/snow-math/quat.rb', line 183

def multiply!(rhs)
  case rhs
  when ::Snow::Vec3 then multiply(rhs, rhs)
  else multiply(rhs, self)
  end
end

#multiply_quat(*args) ⇒ Object

Concatenates this quaternion and another and returns the result.

call-seq:

multiply_quat(quat, output = nil) -> output or new quat


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
4044
4045
4046
4047
4048
4049
4050
# File 'ext/snow-math/snow-math.c', line 4011

static VALUE sm_quat_multiply(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  quat_t *self;
  quat_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_quat(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_quat(sm_rhs, NULL);
  if (argc == 2) {
    if (!RTEST(sm_out)) {
      goto SM_LABEL(skip_output);
    }{
    quat_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_quat(sm_out, NULL);
    quat_multiply(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    quat_t output;
    quat_multiply(*self, *rhs, output);
    sm_out = sm_wrap_quat(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_quat");
  }
  return sm_out;
}

#multiply_quat!(rhs) ⇒ Object

Calls #multiply_quat(rhs, self)

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



150
151
152
# File 'lib/snow-math/quat.rb', line 150

def multiply_quat!(rhs)
  multiply_quat rhs, self
end

#multiply_vec3(*args) ⇒ Object

Multiplies a quaternion and vec3, returning the rotated vec3.

call-seq:

multiply_vec3(quat, output = nil) -> output or new quat


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

static VALUE sm_quat_multiply_vec3(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  quat_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_quat(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);
    quat_multiply_vec3(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    quat_multiply_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 multiply_vec3");
  }
  return sm_out;
}

#multiply_vec3!(rhs) ⇒ Object

Calls #multiply_vec3(rhs, rhs)

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



157
158
159
# File 'lib/snow-math/quat.rb', line 157

def multiply_vec3!(rhs)
  multiply_vec3 rhs, rhs
end

#negate(*args) ⇒ Object Also known as: -@

Negates this vector or quaternions’s components and returns the result.

call-seq:

negate(output = nil) -> output or new vec4 or quat


3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
# File 'ext/snow-math/snow-math.c', line 3322

static VALUE sm_vec4_negate(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  vec4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_vec4(sm_self, NULL);
  if (argc == 1) {
    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);
    vec4_negate (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_negate (*self, output);
    sm_out = sm_wrap_vec4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to negate");
  }
  return sm_out;
}

#negate!Object

Calls #negate(self)

call-seq: negate! -> self



143
144
145
# File 'lib/snow-math/quat.rb', line 143

def negate!
  negate self
end

#normalize(*args) ⇒ Object

Returns a normalized Vec4 or Quat, depending on the type of the receiver and output.

call-seq:

normalize(output = nil) -> output or new vec4 / quat


3242
3243
3244
3245
3246
3247
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 3242

static VALUE sm_vec4_normalize(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  vec4_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_vec4(sm_self, NULL);
  if (argc == 1) {
    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));
    }
    rb_check_frozen(sm_out);
    output = sm_unwrap_vec4(sm_out, NULL);
    vec4_normalize (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_normalize (*self, output);
    sm_out = sm_wrap_vec4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to normalize");
  }
  return sm_out;
}

#normalize!Object

Calls #normalize(self)

call-seq: normalize! -> self



129
130
131
# File 'lib/snow-math/quat.rb', line 129

def normalize!
  normalize self
end

#scale(*args) ⇒ Object

Scales this vector or quaternion’s components by a scalar value and returns the result. The return type is that of the receiver.

call-seq:

scale(scalar, output = nil) -> output or new vec4


3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
# File 'ext/snow-math/snow-math.c', line 3789

static VALUE sm_vec4_scale(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  VALUE sm_scalar;
  s_float_t scalar;
  vec4_t *self = sm_unwrap_vec4(sm_self, NULL);

  rb_scan_args(argc, argv, "11", &sm_scalar, &sm_out);
  scalar = rb_num2dbl(sm_scalar);

  if ((SM_IS_A(sm_out, vec4) || SM_IS_A(sm_out, quat))) {
    rb_check_frozen(sm_out);
    vec4_scale(*self, scalar, *sm_unwrap_vec4(sm_out, NULL));
  } else {
    vec4_t out;
    vec4_scale(*self, scalar, out);
    sm_out = sm_wrap_vec4(out, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

#scale!(rhs) ⇒ Object

Calls #scale(rhs, self)

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



207
208
209
# File 'lib/snow-math/quat.rb', line 207

def scale!(rhs)
  scale rhs, self
end

#set(*args) ⇒ Object

Sets the Quat’s components.

call-seq:

set(x, y, z, w = 1) -> new quaternion with components [x, y, z, w]
set([x, y, z, w])   -> new quaternion with components [x, y, z, w]
set(quat)           -> copy of quat
set(vec2)           -> new quaternion with the components [vec2.xy, 0, 1]
set(vec3)           -> new quaternion with the components [vec3.xyz, 1]
set(vec4)           -> new quaternion with the components of vec4
set(mat3)           -> new quaternion from mat3
set(mat4)           -> new quaternion from mat4


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
4170
4171
4172
4173
4174
4175
4176
4177
4178
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
# File 'ext/snow-math/snow-math.c', line 4138

static VALUE sm_quat_init(int argc, VALUE *argv, VALUE sm_self)
{
  quat_t *self = sm_unwrap_quat(sm_self, NULL);
  size_t arr_index = 0;

  rb_check_frozen(sm_self);

  switch(argc) {

  /* Default value */
  case 0: { break; }

  /* Copy or by-array */
  case 1: {
    if (SM_IS_A(argv[0], quat) ||
        SM_IS_A(argv[0], vec4)) {
      sm_unwrap_quat(argv[0], *self);
      break;
    }

    if (SM_IS_A(argv[0], vec2)) {
      sm_unwrap_vec2(argv[0], *self);
      self[0][2] = s_float_lit(0.0);
      self[0][3] = s_float_lit(1.0);
      break;
    }

    if (SM_IS_A(argv[0], vec3)) {
      sm_unwrap_vec3(argv[0], *self);
      self[0][3] = s_float_lit(1.0);
      break;
    }

    if (SM_IS_A(argv[0], mat4)) {
      const mat4_t *mat = sm_unwrap_mat4(argv[0], NULL);
      quat_from_mat4(*mat, *self);
      break;
    }

    if (SM_IS_A(argv[0], mat3)) {
      const mat3_t *mat = sm_unwrap_mat3(argv[0], NULL);
      quat_from_mat3(*mat, *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 + 3;
      s_float_t *vec_elem = *self;
      for (; arr_index < arr_end; ++arr_index, ++vec_elem) {
        *vec_elem = (s_float_t)rb_num2dbl(rb_ary_entry(arrdata, (long)arr_index));
      }
      break;
    }

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

  /* W */
  case 4: {
    self[0][3] = (s_float_t)rb_num2dbl(argv[3]);
    case 3: /* X, Y, Z */
    self[0][0] = (s_float_t)rb_num2dbl(argv[0]);
    self[0][1] = (s_float_t)rb_num2dbl(argv[1]);
    self[0][2] = (s_float_t)rb_num2dbl(argv[2]);
    break;
  }

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

  return sm_self;
}

#sizeObject

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

call-seq: size -> fixnum



3945
3946
3947
3948
# File 'ext/snow-math/snow-math.c', line 3945

static VALUE sm_quat_size (VALUE self)
{
  return SIZET2NUM(sizeof(quat_t));
}

#slerp(*args) ⇒ Object

Returns a quaternion interpolated between self and destination using spherical linear interpolation. Alpha is the interpolation value and must be clamped from 0 to 1.

call-seq:

slerp(destination, alpha, output = nil) -> output or new quat


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

static VALUE sm_quat_slerp(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  VALUE sm_destination;
  VALUE sm_alpha;
  quat_t *destination;
  quat_t *self = sm_unwrap_vec4(sm_self, NULL);
  s_float_t alpha;

  rb_scan_args(argc, argv, "21", &sm_destination, &sm_alpha, &sm_out);
  alpha = rb_num2dbl(sm_alpha);

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

  destination = sm_unwrap_quat(sm_destination, NULL);

  if ((SM_IS_A(sm_out, vec4) || SM_IS_A(sm_out, quat))) {
    rb_check_frozen(sm_out);
    quat_slerp(*self, *destination, alpha, *sm_unwrap_quat(sm_out, NULL));
  } else {
    quat_t out;
    quat_slerp(*self, *destination, alpha, out);
    sm_out = sm_wrap_quat(out, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }

  return sm_out;
}

#slerp!(destination, alpha) ⇒ Object

Calls #slerp(destination, alpha, self)

call-seq: slerp!(destination, alpha) -> self



221
222
223
# File 'lib/snow-math/quat.rb', line 221

def slerp!(destination, alpha)
  slerp(destination, alpha, self)
end

#storeObject Also known as: []=

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

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



3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
# File 'ext/snow-math/snow-math.c', line 3922

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

#subtract(*args) ⇒ Object Also known as: -

Subtracts another vector or quaternion’s components from this vector’s and returns the result. The return type is that of the receiver.

call-seq:

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


3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
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
# File 'ext/snow-math/snow-math.c', line 3561

static VALUE sm_vec4_subtract(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec4_t *self;
  vec4_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec4(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_rhs, vec4) && !SM_IS_A(sm_rhs, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_rhs));
      return Qnil;
    }
    rb_check_frozen(sm_out);
    output = sm_unwrap_vec4(sm_out, NULL);
    vec4_subtract(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_subtract(*self, *rhs, output);
    sm_out = sm_wrap_vec4(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to subtract");
  }
  return sm_out;
}

#subtract!(rhs) ⇒ Object

Calls #subtract(rhs, self)

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



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

def subtract!(rhs)
  subtract rhs, self
end

#to_mat3Object



62
63
64
# File 'lib/snow-math/quat.rb', line 62

def to_mat3
  Mat3.new(self)
end

#to_mat4Object



66
67
68
# File 'lib/snow-math/quat.rb', line 66

def to_mat4
  Mat4.new(self)
end

#to_quatObject



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

def to_quat
  Quat.new(self)
end

#to_sObject

Returns a string representation of self.

Quat[].to_s     # => "{ 0.0, 0.0, 0.0, 1.0 }"

call-seq:

to_s -> string


4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
# File 'ext/snow-math/snow-math.c', line 4233

static VALUE sm_quat_to_s(VALUE self)
{
  const s_float_t *v;
  v = (const s_float_t *)*sm_unwrap_quat(self, NULL);
  return rb_sprintf(
    "{ "
    "%f, %f, %f, %f"
    " }",
    v[0], v[1], v[2], v[3]);
}

#to_vec2Object



46
47
48
# File 'lib/snow-math/quat.rb', line 46

def to_vec2
  Vec2.new(self)
end

#to_vec3Object



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

def to_vec3
  Vec3.new(self)
end

#to_vec4Object



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

def to_vec4
  Vec4.new(self)
end

#wObject

Returns the W component of the quaternion.

call-seq: w -> float



115
116
117
# File 'lib/snow-math/quat.rb', line 115

def w
  self[3]
end

#w=(value) ⇒ Object

Sets the W component of the quaternion.

call-seq: w = value -> value



122
123
124
# File 'lib/snow-math/quat.rb', line 122

def w=(value)
  self[3] = value
end

#xObject

Returns the X component of the quaternion.

call-seq: x -> float



73
74
75
# File 'lib/snow-math/quat.rb', line 73

def x
  self[0]
end

#x=(value) ⇒ Object

Sets the X component of the quaternion.

call-seq: x = value -> value



80
81
82
# File 'lib/snow-math/quat.rb', line 80

def x=(value)
  self[0] = value
end

#yObject

Returns the Y component of the quaternion.

call-seq: y -> float



87
88
89
# File 'lib/snow-math/quat.rb', line 87

def y
  self[1]
end

#y=(value) ⇒ Object

Sets the Y component of the quaternion.

call-seq: y = value -> value



94
95
96
# File 'lib/snow-math/quat.rb', line 94

def y=(value)
  self[1] = value
end

#zObject

Returns the Z component of the quaternion.

call-seq: z -> float



101
102
103
# File 'lib/snow-math/quat.rb', line 101

def z
  self[2]
end

#z=(value) ⇒ Object

Sets the Z component of the quaternion.

call-seq: z = value -> value



108
109
110
# File 'lib/snow-math/quat.rb', line 108

def z=(value)
  self[2] = value
end