Class: Snow::Mat3
- Inherits:
-
Object
- Object
- Snow::Mat3
- Includes:
- ArraySupport, BaseMarshalSupport, FiddlePointerSupport, InspectSupport
- Defined in:
- lib/snow-math/mat3.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 3x3 matrix class. Often useful for representation rotations.
Class Method Summary collapse
-
.angle_axis(*args) ⇒ Object
Returns a Mat3 describing a rotation around the given axis.
-
.new(*args) ⇒ Object
(also: [])
Allocates a new Mat3.
Instance Method Summary collapse
-
#==(sm_other) ⇒ Object
Tests this Mat3 and another Mat3 for equivalency.
-
#address ⇒ Object
Returns the memory address of the object.
-
#adjoint(*args) ⇒ Object
Returns an ajoint matrix.
-
#adjoint! ⇒ Object
Calls #adjoint(self).
-
#cofactor(*args) ⇒ Object
Returns a cofactor matrix.
-
#cofactor! ⇒ Object
Calls #cofactor(self).
-
#copy(*args) ⇒ Object
(also: #dup, #clone)
Returns a copy of self.
-
#determinant ⇒ Object
Returns the matrix determinant.
-
#fetch ⇒ Object
(also: #[])
Gets the component of the Mat3 at the given index.
-
#get_column3(*args) ⇒ Object
Returns a Vec3 whose components are that of the column at the given index.
-
#get_row3(*args) ⇒ Object
Returns a Vec3 whose components are that of the row at the given index.
-
#initialize(*args) ⇒ Object
constructor
Sets the Mat3’s components.
-
#inverse(*args) ⇒ Object
Returns the matrix inverse on success, nil on failure.
-
#inverse! ⇒ Object
Calls #inverse(self).
-
#inverse_rotate_vec3(*args) ⇒ Object
Convenience function to rotate a Vec3 by an inverse matrix and return the result.
-
#length ⇒ Object
Returns the length of the Mat3 in components.
-
#load_identity ⇒ Object
Sets self to the identity matrix.
-
#multiply(rhs, out = nil) ⇒ Object
(also: #*)
Multiplies self and RHS and returns the result.
-
#multiply!(rhs) ⇒ Object
Calls #multiply(rhs, self).
-
#multiply_mat3(*args) ⇒ Object
Multiplies this Mat3 and another and returns the result.
-
#multiply_mat3!(rhs) ⇒ Object
Calls #multiply_mat3(rhs, self).
-
#orthogonal(*args) ⇒ Object
Returns an orthogonal matrix.
-
#rotate_vec3(*args) ⇒ Object
Rotates a Vec3 using self and returns the result.
-
#scale(*args) ⇒ Object
(also: #**)
Scales Mat3’s columns by X, Y, and Z and returns the result.
-
#scale!(x, y, z) ⇒ Object
Calls scale(x, y, z, self).
-
#set(*args) ⇒ Object
Sets the Mat3’s components.
-
#set_column3(sm_index, sm_value) ⇒ Object
Sets the matrix’s column at the given index to the given vector.
-
#set_row3(sm_index, sm_value) ⇒ Object
Sets the matrix’s row at the given index to the given vector.
-
#size ⇒ Object
Returns the length in bytes of the Mat3.
-
#store ⇒ Object
(also: #[]=)
Sets the Mat3’s component at the index to the value.
-
#to_mat4(*args) ⇒ Object
Returns a Mat4 converted from the Mat3.
-
#to_s ⇒ Object
Returns a string representation of self.
-
#transpose(*args) ⇒ Object
(also: #~)
Transposes this matrix and returns the result.
-
#transpose! ⇒ Object
Calls #transpose(self).
Methods included from BaseMarshalSupport
Methods included from InspectSupport
Methods included from ArraySupport
Methods included from FiddlePointerSupport
Constructor Details
#initialize(*args) ⇒ Object
Sets the Mat3’s components.
call-seq:
set(m1, m2, ..., m8, m9) -> new mat3 with components
set([m1, m2, ..., m8, m9]) -> new mat3 with components
set(mat3) -> copy of mat3
set(mat4) -> new mat3 from mat4's inner 9x9 matrix
set(quat) -> quat as mat3
set(Vec3, Vec3, Vec3) -> new mat3 with given row vectors
5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 |
# File 'ext/snow-math/snow-math.c', line 5135
static VALUE sm_mat3_init(int argc, VALUE *argv, VALUE sm_self)
{
mat3_t *self = sm_unwrap_mat3(sm_self, NULL);
size_t arr_index = 0;
switch (argc) {
case 0: {
// Identity (handled in _new)
break;
}
// Copy Mat3 or provided [Numeric..]
case 1: {
// Copy Mat3
if (SM_IS_A(argv[0], mat3)) {
sm_unwrap_mat3(argv[0], *self);
break;
}
// Copy Mat4
if (SM_IS_A(argv[0], mat4)) {
mat4_to_mat3(*sm_unwrap_mat4(argv[0], NULL), *self);
break;
}
// Build from Quaternion
if (SM_IS_A(argv[0], quat)) {
mat3_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 + 9;
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 Mat3");
break;
}
// Mat3(Vec3, Vec3, Vec3)
case 3: {
size_t arg_index;
s_float_t *mat_elem = *self;
for (arg_index = 0; arg_index < 3; ++arg_index, mat_elem += 3) {
if (!SM_IS_A(argv[arg_index], vec3)) {
rb_raise(
rb_eArgError,
"Argument %d must be a Vec3 when supplying three arguments to Mat3.initialize",
(int)(arg_index + 1));
}
sm_unwrap_vec3(argv[arg_index], mat_elem);
}
break;
}
// Mat3(Numeric m00 .. m16)
case 9: {
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 Mat3.initialize");
break;
}
} // swtich (argc)
return sm_self;
}
|
Class Method Details
.angle_axis(*args) ⇒ Object
Returns a Mat3 describing a rotation around the given axis.
call-seq:
angle_axis(angle_degrees, axis_vec3, output = nil) -> output or new mat3
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 |
# File 'ext/snow-math/snow-math.c', line 5259
static VALUE sm_mat3_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, mat3)) {
mat3_t *out = sm_unwrap_mat3(sm_out, NULL);
mat3_rotation(angle, (*axis)[0], (*axis)[1], (*axis)[2], *out);
} else {
mat3_t out;
mat3_rotation(angle, (*axis)[0], (*axis)[1], (*axis)[2], out);
sm_out = sm_wrap_mat3(out, self);
rb_obj_call_init(sm_out, 0, 0);
}
return sm_out;
}
|
.new(*args) ⇒ Object Also known as: []
Allocates a new Mat3.
call-seq:
new() -> identity mat3
new(m1, m2, ..., m8, m9) -> new mat3 with components
new([m1, m2, ..., m8, m9]) -> new mat3 with components
new(mat3) -> copy of mat3
new(mat4) -> new mat3 from mat4's inner 9x9 matrix
new(quat) -> quat as mat3
new(Vec3, Vec3, Vec3) -> new mat3 with given row vectors
5115 5116 5117 5118 5119 5120 |
# File 'ext/snow-math/snow-math.c', line 5115
static VALUE sm_mat3_new(int argc, VALUE *argv, VALUE self)
{
VALUE sm_mat = sm_wrap_mat3(g_mat3_identity, self);
rb_obj_call_init(sm_mat, argc, argv);
return sm_mat;
}
|
Instance Method Details
#==(sm_other) ⇒ Object
Tests this Mat3 and another Mat3 for equivalency.
call-seq:
mat3 == other_mat3 -> bool
5542 5543 5544 5545 5546 5547 5548 5549 |
# File 'ext/snow-math/snow-math.c', line 5542
static VALUE sm_mat3_equals(VALUE sm_self, VALUE sm_other)
{
if (!RTEST(sm_other) || !SM_IS_A(sm_other, mat3)) {
return Qfalse;
}
return mat3_equals(*sm_unwrap_mat3(sm_self, NULL), *sm_unwrap_mat3(sm_other, NULL)) ? Qtrue : Qfalse;
}
|
#address ⇒ Object
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 ajoint matrix.
call-seq:
adjoint(output = nil) -> output or new mat3
4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 |
# File 'ext/snow-math/snow-math.c', line 4812
static VALUE sm_mat3_adjoint(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
mat3_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_mat3(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);
mat3_adjoint (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
mat3_t output;
mat3_adjoint (*self, output);
sm_out = sm_wrap_mat3(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
62 63 64 |
# File 'lib/snow-math/mat3.rb', line 62 def adjoint! adjoint self end |
#cofactor(*args) ⇒ Object
Returns a cofactor matrix.
call-seq:
cofactor(output = nil) -> output or new mat3
4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 |
# File 'ext/snow-math/snow-math.c', line 4878
static VALUE sm_mat3_cofactor(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
mat3_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_mat3(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);
mat3_cofactor (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
mat3_t output;
mat3_cofactor (*self, output);
sm_out = sm_wrap_mat3(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to cofactor");
}
return sm_out;
}
|
#cofactor! ⇒ Object
Calls #cofactor(self)
call-seq: cofactor! -> self
71 72 73 |
# File 'lib/snow-math/mat3.rb', line 71 def cofactor! cofactor self end |
#copy(*args) ⇒ Object Also known as: dup, clone
Returns a copy of self.
call-seq:
copy(output = nil) -> output or new mat3
4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 |
# File 'ext/snow-math/snow-math.c', line 4713
static VALUE sm_mat3_copy(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
mat3_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_mat3(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);
mat3_copy (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
mat3_t output;
mat3_copy (*self, output);
sm_out = sm_wrap_mat3(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;
}
|
#determinant ⇒ Object
Returns the matrix determinant.
call-seq:
determinant -> float
5043 5044 5045 5046 |
# File 'ext/snow-math/snow-math.c', line 5043
static VALUE sm_mat3_determinant(VALUE sm_self)
{
return mat3_determinant(*sm_unwrap_mat3(sm_self, NULL));
}
|
#fetch ⇒ Object Also known as: []
Gets the component of the Mat3 at the given index.
call-seq: fetch(index) -> float
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 |
# File 'ext/snow-math/snow-math.c', line 4647
static VALUE sm_mat3_fetch (VALUE sm_self, VALUE sm_index)
{
static const int max_index = sizeof(mat3_t) / sizeof(s_float_t);
const mat3_t *self = sm_unwrap_mat3(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
5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 |
# File 'ext/snow-math/snow-math.c', line 5362
static VALUE sm_mat3_get_column3(int argc, VALUE *argv, VALUE sm_self)
{
mat3_t *self;
int index;
VALUE sm_out;
self = sm_unwrap_mat3(sm_self, NULL);
index = NUM2INT(argv[0]);
sm_out = Qnil;
if (index < 0 || index > 2) {
rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 2)", 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);
mat3_get_column3(*self, index, *out);
break;
}
case 1: SM_LABEL(no_output): {
vec3_t out;
mat3_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_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
5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 |
# File 'ext/snow-math/snow-math.c', line 5299
static VALUE sm_mat3_get_row3(int argc, VALUE *argv, VALUE sm_self)
{
mat3_t *self;
int index;
VALUE sm_out;
self = sm_unwrap_mat3(sm_self, NULL);
index = NUM2INT(argv[0]);
sm_out = Qnil;
if (index < 0 || index > 2) {
rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 2)", 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);
mat3_get_row3(*self, index, *out);
break;
}
case 1: SM_LABEL(no_output): {
vec3_t out;
mat3_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;
}
|
#inverse(*args) ⇒ Object
Returns the matrix inverse on success, nil on failure.
call-seq:
inverse(output = nil) -> output, new mat3, or nil
5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 |
# File 'ext/snow-math/snow-math.c', line 5056
static VALUE sm_mat3_inverse(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out = Qnil;
mat3_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_mat3(sm_self, NULL);
if (argc == 1) {
mat3_t *output;
if (!RTEST(sm_out)) {
goto SM_LABEL(skip_output);
}
if (!SM_IS_A(sm_out, mat3)) {
rb_raise(rb_eTypeError,
"Invalid argument to output of inverse_general: expected %s, got %s",
rb_class2name(s_sm_mat3_klass),
rb_obj_classname(sm_out));
return Qnil;
}
output = sm_unwrap_mat3(sm_out, NULL);
if (!mat3_inverse(*self, *output)) {
return Qnil;
}
} else if (argc == 0) {
SM_LABEL(skip_output): {
mat3_t output;
if (!mat3_inverse(*self, output)) {
return Qnil;
}
sm_out = sm_wrap_mat3(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
53 54 55 |
# File 'lib/snow-math/mat3.rb', line 53 def inverse! inverse self end |
#inverse_rotate_vec3(*args) ⇒ Object
Convenience function to rotate a Vec3 by an inverse matrix and return the result.
call-seq:
inv_rotate_vec3(vec3, output = nil) -> output or new vec3
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 |
# File 'ext/snow-math/snow-math.c', line 4996
static VALUE sm_mat3_inv_rotate_vec3(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
mat3_t *self;
vec3_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_mat3(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);
mat3_inv_rotate_vec3(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
vec3_t output;
mat3_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;
}
|
#length ⇒ Object
Returns the length of the Mat3 in components. Result is always 9.
call-seq: length -> fixnum
4700 4701 4702 4703 |
# File 'ext/snow-math/snow-math.c', line 4700
static VALUE sm_mat3_length (VALUE self)
{
return SIZET2NUM(sizeof(mat3_t) / sizeof(s_float_t));
}
|
#load_identity ⇒ Object
Sets self to the identity matrix.
call-seq:
load_identity -> self
5495 5496 5497 5498 5499 5500 |
# File 'ext/snow-math/snow-math.c', line 5495
static VALUE sm_mat3_identity(VALUE sm_self)
{
mat3_t *self = sm_unwrap_mat3(sm_self, NULL);
mat3_identity(*self);
return sm_self;
}
|
#multiply(rhs, out = nil) ⇒ Object Also known as: *
Multiplies self and RHS and returns the result. This is a wrapper around other multiply methods. See multiply_mat3, rotate_vec3, and #scale for more reference.
In the third form, the scalar value provided is passed for all three columns when calling scale.
call-seq:
multiply(mat3, output = nil) -> output or new mat3
multiply(vec3, output = nil) -> output or new vec3
multiply(scalar, output = nil) -> output or new mat3
97 98 99 100 101 102 103 104 |
# File 'lib/snow-math/mat3.rb', line 97 def multiply(rhs, out = nil) case rhs when ::Snow::Mat3 then multiply_mat3(rhs, out) when ::Snow::Vec3 then rotate_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).
call-seq: multiply!(mat3) -> self call-seq: multiply!(vec3) -> vec3 call-seq: multiply!(scalar) -> self
113 114 115 116 117 118 119 |
# File 'lib/snow-math/mat3.rb', line 113 def multiply!(rhs) multiply rhs, case rhs when Mat3, Numeric then self when Vec3 then rhs else raise TypeError, "Invalid type for RHS" end end |
#multiply_mat3(*args) ⇒ Object
Multiplies this Mat3 and another and returns the result.
call-seq:
multiply_mat3(mat3, output = nil) -> output or new mat3
4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 |
# File 'ext/snow-math/snow-math.c', line 4911
static VALUE sm_mat3_multiply(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
mat3_t *self;
mat3_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_mat3(sm_self, NULL);
SM_RAISE_IF_NOT_TYPE(sm_rhs, mat3);
rhs = sm_unwrap_mat3(sm_rhs, NULL);
if (argc == 2) {
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);
mat3_multiply(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
mat3_t output;
mat3_multiply(*self, *rhs, output);
sm_out = sm_wrap_mat3(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to mat3");
}
return sm_out;
}
|
#multiply_mat3!(rhs) ⇒ Object
Calls #multiply_mat3(rhs, self)
call-seq: multiply_mat3!(rhs) -> self
80 81 82 |
# File 'lib/snow-math/mat3.rb', line 80 def multiply_mat3!(rhs) multiply_mat3 rhs, self end |
#orthogonal(*args) ⇒ Object
Returns an orthogonal matrix.
call-seq:
orthogonal(output = nil) -> output or new mat3
4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 |
# File 'ext/snow-math/snow-math.c', line 4845
static VALUE sm_mat3_orthogonal(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
mat3_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_mat3(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);
mat3_orthogonal (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
mat3_t output;
mat3_orthogonal (*self, output);
sm_out = sm_wrap_mat3(output, rb_obj_class(sm_self));
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to orthogonal");
}
return sm_out;
}
|
#rotate_vec3(*args) ⇒ Object
Rotates a Vec3 using self and returns the result.
call-seq:
rotate_vec3(vec3, output = nil) -> output or new vec3
4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 |
# File 'ext/snow-math/snow-math.c', line 4948
static VALUE sm_mat3_rotate_vec3(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_rhs;
VALUE sm_out;
mat3_t *self;
vec3_t *rhs;
rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
self = sm_unwrap_mat3(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);
mat3_rotate_vec3(*self, *rhs, *output);
}} else if (argc == 1) {
SM_LABEL(skip_output): {
vec3_t output;
mat3_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;
}
|
#scale(*args) ⇒ Object Also known as: **
Scales Mat3’s columns by X, Y, and Z and returns the result.
call-seq:
scale(x, y, z, output = nil) -> output or new mat3
5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 |
# File 'ext/snow-math/snow-math.c', line 5510
static VALUE sm_mat3_scale(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
VALUE sm_x, sm_y, sm_z;
s_float_t x, y, z;
mat3_t *self = sm_unwrap_mat3(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, mat3)) {
mat3_scale(*self, x, y, z, *sm_unwrap_mat3(sm_out, NULL));
} else {
mat3_t out;
mat3_scale(*self, x, y, z, out);
sm_out = sm_wrap_mat3(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
126 127 128 |
# File 'lib/snow-math/mat3.rb', line 126 def scale!(x, y, z) scale x, y, z, self end |
#set(*args) ⇒ Object
Sets the Mat3’s components.
call-seq:
set(m1, m2, ..., m8, m9) -> new mat3 with components
set([m1, m2, ..., m8, m9]) -> new mat3 with components
set(mat3) -> copy of mat3
set(mat4) -> new mat3 from mat4's inner 9x9 matrix
set(quat) -> quat as mat3
set(Vec3, Vec3, Vec3) -> new mat3 with given row vectors
5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 |
# File 'ext/snow-math/snow-math.c', line 5135
static VALUE sm_mat3_init(int argc, VALUE *argv, VALUE sm_self)
{
mat3_t *self = sm_unwrap_mat3(sm_self, NULL);
size_t arr_index = 0;
switch (argc) {
case 0: {
// Identity (handled in _new)
break;
}
// Copy Mat3 or provided [Numeric..]
case 1: {
// Copy Mat3
if (SM_IS_A(argv[0], mat3)) {
sm_unwrap_mat3(argv[0], *self);
break;
}
// Copy Mat4
if (SM_IS_A(argv[0], mat4)) {
mat4_to_mat3(*sm_unwrap_mat4(argv[0], NULL), *self);
break;
}
// Build from Quaternion
if (SM_IS_A(argv[0], quat)) {
mat3_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 + 9;
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 Mat3");
break;
}
// Mat3(Vec3, Vec3, Vec3)
case 3: {
size_t arg_index;
s_float_t *mat_elem = *self;
for (arg_index = 0; arg_index < 3; ++arg_index, mat_elem += 3) {
if (!SM_IS_A(argv[arg_index], vec3)) {
rb_raise(
rb_eArgError,
"Argument %d must be a Vec3 when supplying three arguments to Mat3.initialize",
(int)(arg_index + 1));
}
sm_unwrap_vec3(argv[arg_index], mat_elem);
}
break;
}
// Mat3(Numeric m00 .. m16)
case 9: {
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 Mat3.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, value) -> self
5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 |
# File 'ext/snow-math/snow-math.c', line 5460
static VALUE sm_mat3_set_column3(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
const vec3_t *value;
int index;
mat3_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_mat3(sm_self, NULL);
value = sm_unwrap_vec3(sm_value, NULL);
index = NUM2INT(sm_index);
if (index < 0 || index > 2) {
rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 2)", index);
return Qnil;
}
mat3_set_column3(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
5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 |
# File 'ext/snow-math/snow-math.c', line 5425
static VALUE sm_mat3_set_row3(VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
const vec3_t *value;
int index;
mat3_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_mat3(sm_self, NULL);
value = sm_unwrap_vec3(sm_value, NULL);
index = NUM2INT(sm_index);
if (index < 0 || index > 2) {
rb_raise(rb_eRangeError, "Index %d is out of range, must be (0 .. 2)", index);
return Qnil;
}
mat3_set_row3(index, *value, *self);
return sm_self;
}
|
#size ⇒ Object
Returns the length in bytes of the Mat3. When compiled to use doubles as the base type, this is always 72. Otherwise, when compiled to use floats, it’s always 36.
call-seq: size -> fixnum
4688 4689 4690 4691 |
# File 'ext/snow-math/snow-math.c', line 4688
static VALUE sm_mat3_size (VALUE self)
{
return SIZET2NUM(sizeof(mat3_t));
}
|
#store ⇒ Object Also known as: []=
Sets the Mat3’s component at the index to the value.
call-seq: store(index, value) -> value
4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 |
# File 'ext/snow-math/snow-math.c', line 4666
static VALUE sm_mat3_store (VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
static const int max_index = sizeof(mat3_t) / sizeof(s_float_t);
mat3_t *self = sm_unwrap_mat3(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_mat4(*args) ⇒ Object
Returns a Mat4 converted from the Mat3.
call-seq:
to_mat4(output = nil) -> output or new mat4
4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 |
# File 'ext/snow-math/snow-math.c', line 4746
static VALUE sm_mat3_to_mat4(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
mat3_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_mat3(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);
mat3_to_mat4 (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
mat4_t output;
mat3_to_mat4 (*self, output);
sm_out = sm_wrap_mat4(output, s_sm_mat3_klass);
rb_obj_call_init(sm_out, 0, 0);
}} else {
rb_raise(rb_eArgError, "Invalid number of arguments to to_mat4");
}
return sm_out;
}
|
#to_s ⇒ Object
Returns a string representation of self.
Mat3[].to_s # => "{ 1.0, 0.0, 0.0,\n
# 0.0, 1.0, 0.0,\n"
# 0.0, 0.0, 1.0 }"
call-seq:
to_s -> string
5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 |
# File 'ext/snow-math/snow-math.c', line 5236
static VALUE sm_mat3_to_s(VALUE self)
{
const s_float_t *v;
v = (const s_float_t *)*sm_unwrap_mat3(self, NULL);
return rb_sprintf(
"{ "
"%f, %f, %f" ",\n "
"%f, %f, %f" ",\n "
"%f, %f, %f"
" }",
v[0], v[1], v[2],
v[3], v[4], v[5],
v[6], v[7], v[8] );
}
|
#transpose(*args) ⇒ Object Also known as: ~
Transposes this matrix and returns the result.
call-seq:
transpose(output = nil) -> output or new mat3
4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 |
# File 'ext/snow-math/snow-math.c', line 4779
static VALUE sm_mat3_transpose(int argc, VALUE *argv, VALUE sm_self)
{
VALUE sm_out;
mat3_t *self;
rb_scan_args(argc, argv, "01", &sm_out);
self = sm_unwrap_mat3(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);
mat3_transpose (*self, *output);
}} else if (argc == 0) {
SM_LABEL(skip_output): {
mat3_t output;
mat3_transpose (*self, output);
sm_out = sm_wrap_mat3(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
44 45 46 |
# File 'lib/snow-math/mat3.rb', line 44 def transpose! transpose self end |