Class: Snow::Vec4

Inherits:
Object
  • Object
show all
Includes:
ArraySupport, BaseMarshalSupport, FiddlePointerSupport, InspectSupport, SwizzleSupport
Defined in:
lib/snow-math/vec4.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 4-component vector class.

Constant Summary collapse

@@SWIZZLE_CHARS =
/^[xyzw]{3,4}$/
@@SWIZZLE_MAPPING =
{ 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 Vec4’s components.

call-seq:

set(x, y, z, w = 1) -> new vec4 with components [x, y, z, w]
set([x, y, z, w])   -> new vec4 with components [x, y, z, w]
set(vec4)           -> copy of vec4
set(vec3)           -> copy of vec3 with w component of 1
set(quat)           -> copy of quat as vec4


2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
# File 'ext/snow-math/snow-math.c', line 2425

static VALUE sm_vec4_init(int argc, VALUE *argv, VALUE sm_self)
{
  vec4_t *self = sm_unwrap_vec4(sm_self, NULL);
  size_t arr_index = 0;

  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], vec3)) {
      sm_unwrap_vec3(argv[0], *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 + 4;
      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 Vec4");
    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 Vec4.initialize");
    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

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

Allocates a new Vec4.

call-seq:

new()               -> new vec4 with components [0, 0, 0, 1]
new(x, y, z, w = 1) -> new vec4 with components [x, y, z, w]
new([x, y, z, w])   -> new vec4 with components [x, y, z, w]
new(vec4)           -> copy of vec4
new(vec3)           -> copy of vec3 with w component of 1
new(quat)           -> copy of quat as vec4


2406
2407
2408
2409
2410
2411
# File 'ext/snow-math/snow-math.c', line 2406

static VALUE sm_vec4_new(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_vec = sm_wrap_vec4(g_vec4_identity, self);
  rb_obj_call_init(sm_vec, argc, argv);
  return sm_vec;
}

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


2608
2609
2610
2611
2612
2613
2614
2615
# File 'ext/snow-math/snow-math.c', line 2608

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


2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
# File 'ext/snow-math/snow-math.c', line 2281

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

#add!(rhs) ⇒ Object

Calls #add(rhs, self)

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



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

def add!(rhs)
  add rhs, self
end

#addressObject

Returns the memory address of the object.

call-seq: address -> fixnum



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

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

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

Returns a copy of self.

call-seq:

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


1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
# File 'ext/snow-math/snow-math.c', line 1985

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);
    }{
    if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    vec4_t *output = sm_unwrap_vec4(sm_out, NULL);
    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


2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
# File 'ext/snow-math/snow-math.c', line 2575

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



166
167
168
# File 'lib/snow-math/vec4.rb', line 166

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


2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
# File 'ext/snow-math/snow-math.c', line 2378

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 Vec4 at the given index.

call-seq: fetch(index) -> float



1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
# File 'ext/snow-math/snow-math.c', line 1919

static VALUE sm_vec4_fetch (VALUE sm_self, VALUE sm_index)
{
  static const int max_index = sizeof(vec4_t) / sizeof(s_float_t);
  const vec4_t *self = sm_unwrap_vec4(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 a vector whose components are the multiplicative inverse of this vector’s.

call-seq:

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


2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
# File 'ext/snow-math/snow-math.c', line 2062

static VALUE sm_vec4_inverse(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);
    }{
    if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    vec4_t *output = sm_unwrap_vec4(sm_out, NULL);
    vec4_inverse (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_inverse (*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 inverse");
  }
  return sm_out;
}

#inverse!Object

Calls #inverse(self)

call-seq: inverse! -> self



104
105
106
# File 'lib/snow-math/vec4.rb', line 104

def inverse!
  inverse self
end

#lengthObject

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

call-seq: length -> fixnum



1972
1973
1974
1975
# File 'ext/snow-math/snow-math.c', line 1972

static VALUE sm_vec4_length (VALUE self)
{
  return SIZET2NUM(sizeof(vec4_t) / sizeof(s_float_t));
}

#magnitudeObject

Returns the magnitude of self.

call-seq:

magnitude -> float


2530
2531
2532
2533
# File 'ext/snow-math/snow-math.c', line 2530

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


2517
2518
2519
2520
# File 'ext/snow-math/snow-math.c', line 2517

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: *

Calls #multiply_vec4 and #scale, respectively.

call-seq:

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


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

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

#multiply!(rhs) ⇒ Object

Calls #multiply(rhs, self)

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



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

def multiply!(rhs)
  multiply rhs, self
end

#multiply_vec4(*args) ⇒ Object

Multiplies this and another vector’s components together and returns the result.

call-seq:

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


2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
# File 'ext/snow-math/snow-math.c', line 2233

static VALUE sm_vec4_multiply(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);
    }{
    if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    vec4_t *output = sm_unwrap_vec4(sm_out, NULL);
    vec4_multiply(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_multiply(*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 vec4");
  }
  return sm_out;
}

#multiply_vec4!(rhs) ⇒ Object

Calls #multiply_vec4(rhs, self)

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



118
119
120
# File 'lib/snow-math/vec4.rb', line 118

def multiply_vec4!(rhs)
  multiply_vec4 rhs, self
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


2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
# File 'ext/snow-math/snow-math.c', line 2100

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);
    }{
    if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    vec4_t *output = sm_unwrap_vec4(sm_out, NULL);
    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



111
112
113
# File 'lib/snow-math/vec4.rb', line 111

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


2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
# File 'ext/snow-math/snow-math.c', line 2024

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);
    }{
    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));
    }
    vec4_t *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



97
98
99
# File 'lib/snow-math/vec4.rb', line 97

def normalize!
  normalize self
end

#project(*args) ⇒ Object

Projects this vector onto a normal vector and returns the result.

call-seq:

project(normal, output = nil) -> output or new vec4


2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
# File 'ext/snow-math/snow-math.c', line 2138

static VALUE sm_vec4_project(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);
    }{
    if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    vec4_t *output = sm_unwrap_vec4(sm_out, NULL);
    vec4_project(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_project(*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 vec4");
  }
  return sm_out;
}

#reflect(*args) ⇒ Object

Reflects this vector against a normal vector and returns the result.

call-seq:

reflect(normal, output = nil) -> output or new vec4


2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
# File 'ext/snow-math/snow-math.c', line 2185

static VALUE sm_vec4_reflect(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);
    }{
    if (!SM_IS_A(sm_out, vec4) && !SM_IS_A(sm_out, quat)) {
      rb_raise(rb_eTypeError,
        kSM_WANT_FOUR_FORMAT_LIT,
        rb_obj_classname(sm_out));
      return Qnil;
    }
    vec4_t *output = sm_unwrap_vec4(sm_out, NULL);
    vec4_reflect(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec4_t output;
    vec4_reflect(*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 vec4");
  }
  return sm_out;
}

#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


2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
# File 'ext/snow-math/snow-math.c', line 2544

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



159
160
161
# File 'lib/snow-math/vec4.rb', line 159

def scale!(rhs)
  scale rhs, self
end

#set(*args) ⇒ Object

Sets the Vec4’s components.

call-seq:

set(x, y, z, w = 1) -> new vec4 with components [x, y, z, w]
set([x, y, z, w])   -> new vec4 with components [x, y, z, w]
set(vec4)           -> copy of vec4
set(vec3)           -> copy of vec3 with w component of 1
set(quat)           -> copy of quat as vec4


2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
# File 'ext/snow-math/snow-math.c', line 2425

static VALUE sm_vec4_init(int argc, VALUE *argv, VALUE sm_self)
{
  vec4_t *self = sm_unwrap_vec4(sm_self, NULL);
  size_t arr_index = 0;

  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], vec3)) {
      sm_unwrap_vec3(argv[0], *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 + 4;
      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 Vec4");
    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 Vec4.initialize");
    break;
  }
  } // switch (argc)

  return sm_self;
}

#sizeObject

Returns the length in bytes of the Vec4. 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



1960
1961
1962
1963
# File 'ext/snow-math/snow-math.c', line 1960

static VALUE sm_vec4_size (VALUE self)
{
  return SIZET2NUM(sizeof(vec4_t));
}

#storeObject Also known as: []=

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

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



1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
# File 'ext/snow-math/snow-math.c', line 1938

static VALUE sm_vec4_store (VALUE sm_self, VALUE sm_index, VALUE sm_value)
{
  static const int max_index = sizeof(vec4_t) / sizeof(s_float_t);
  vec4_t *self = sm_unwrap_vec4(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;
}

#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


2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
# File 'ext/snow-math/snow-math.c', line 2329

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

#subtract!(rhs) ⇒ Object

Calls #subtract(rhs, self)

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



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

def subtract!(rhs)
  subtract rhs, self
end

#to_sObject

Returns a string representation of self.

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

call-seq:

to_s -> string


2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
# File 'ext/snow-math/snow-math.c', line 2498

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

#wObject

Returns the W component of the vector.

call-seq: w -> float



83
84
85
# File 'lib/snow-math/vec4.rb', line 83

def w
  self[3]
end

#w=(value) ⇒ Object

Sets the W component of the vector.

call-seq: w = value -> value



90
91
92
# File 'lib/snow-math/vec4.rb', line 90

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

#xObject

Returns the X component of the vector.

call-seq: x -> float



41
42
43
# File 'lib/snow-math/vec4.rb', line 41

def x
  self[0]
end

#x=(value) ⇒ Object

Sets the X component of the vector.

call-seq: x = value -> value



48
49
50
# File 'lib/snow-math/vec4.rb', line 48

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

#yObject

Returns the Y component of the vector.

call-seq: y -> float



55
56
57
# File 'lib/snow-math/vec4.rb', line 55

def y
  self[1]
end

#y=(value) ⇒ Object

Sets the Y component of the vector.

call-seq: y = value -> value



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

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

#zObject

Returns the Z component of the vector.

call-seq: z -> float



69
70
71
# File 'lib/snow-math/vec4.rb', line 69

def z
  self[2]
end

#z=(value) ⇒ Object

Sets the Z component of the vector.

call-seq: z = value -> value



76
77
78
# File 'lib/snow-math/vec4.rb', line 76

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