Class: Snow::Vec3

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

Constant Summary collapse

POS_X =
self.new(1, 0, 0).freeze
POS_Y =
self.new(0, 1, 0).freeze
POS_Z =
self.new(0, 0, 1).freeze
NEG_X =
self.new(-1, 0, 0).freeze
NEG_Y =
self.new(0, -1, 0).freeze
NEG_Z =
self.new(0, 0, -1).freeze
ONE =
self.new(1, 1, 1).freeze
ZERO =
self.new.freeze
SIZE =
INT2FIX(sizeof(vec3_t))
LENGTH =
INT2FIX(sizeof(vec3_t) / sizeof(s_float_t))
@@SWIZZLE_CHARS =
/^[xyz]{2,4}$/
@@SWIZZLE_MAPPING =
{ 2 => ::Snow::Vec2, 3 => self, 4 => ::Snow::Vec4, 'x' => 0, 'y' => 1, 'z' => 2 }

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

call-seq:

set(x, y, z)   -> vec3 with components [x, y, z]
set([x, y, z]) -> vec3 with components [x, y, z]
set(vec2)      -> vec3 with components [vec2.xy, 0]
set(vec3)      -> copy of vec3
set(vec4)      -> vec3 with components [vec4.xyz]
set(quat)      -> vec3 with components [quat.xyz]


2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
# File 'ext/snow-math/snow-math.c', line 2745

static VALUE sm_vec3_init(int argc, VALUE *argv, VALUE sm_self)
{
  vec3_t *self = sm_unwrap_vec3(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], vec3) ||
        SM_IS_A(argv[0], vec4) ||
        SM_IS_A(argv[0], quat)) {
      sm_unwrap_vec3(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);
      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)NUM2DBL(rb_ary_entry(arrdata, (long)arr_index));
      }
      break;
    }

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

  /* X, Y, Z */
  case 3: {
    self[0][0] = (s_float_t)NUM2DBL(argv[0]);
    self[0][1] = (s_float_t)NUM2DBL(argv[1]);
    self[0][2] = (s_float_t)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

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

Allocates a Vec3.

call-seq:

new()          -> vec3 with components [0, 0, 0]
new(x, y, z)   -> vec3 with components [x, y, z]
new([x, y, z]) -> vec3 with components [x, y, z]
new(vec3)      -> copy of vec3
new(vec4)      -> vec3 of vec4's x, y, and z components
new(quat)      -> vec3 of quat's x, y, and z components


2725
2726
2727
2728
2729
2730
# File 'ext/snow-math/snow-math.c', line 2725

static VALUE sm_vec3_new(int argc, VALUE *argv, VALUE self)
{
  VALUE sm_vec = sm_wrap_vec3(g_vec3_zero, self);
  rb_obj_call_init(sm_vec, argc, argv);
  return sm_vec;
}

Instance Method Details

#==(sm_other) ⇒ Object

Tests whether a Vec3 is equivalent to another Vec3, a Vec4, or a Quat. When testing for equivalency against 4-component objects, only the first three components are compared.

call-seq:

vec3 == other_vec3 -> bool
vec3 == vec4       -> bool
vec3 == quat       -> bool


2931
2932
2933
2934
2935
2936
2937
2938
# File 'ext/snow-math/snow-math.c', line 2931

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

  return vec3_equals(*sm_unwrap_vec3(sm_self, NULL), *sm_unwrap_vec3(sm_other, NULL)) ? Qtrue : Qfalse;
}

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

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

call-seq:

add(vec3, output = nil) -> output or new vec3


2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
# File 'ext/snow-math/snow-math.c', line 2594

static VALUE sm_vec3_add(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec3_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec3(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);
    vec3_add(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_add(*self, *rhs, output);
    sm_out = sm_wrap_vec3(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



164
165
166
# File 'lib/snow-math/vec3.rb', line 164

def add!(rhs)
  add rhs, self
end

#addressObject

Returns the memory address of the object.

call-seq: address -> fixnum



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

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

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

Returns a copy of self.

call-seq:

copy(output = nil) -> output or new vec3


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

static VALUE sm_vec3_copy(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  vec3_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_vec3(sm_self, NULL);
  if (argc == 1) {
    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);
    vec3_copy (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_copy (*self, output);
    sm_out = sm_wrap_vec3(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;
}

#cross_product(*args) ⇒ Object Also known as: ^

Returns the cross product of this vector and another Vec3.

call-seq:

cross_product(vec3, output = nil) -> output or new vec3


2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
# File 'ext/snow-math/snow-math.c', line 2495

static VALUE sm_vec3_cross_product(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec3_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec3(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);
    vec3_cross_product(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_cross_product(*self, *rhs, output);
    sm_out = sm_wrap_vec3(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to cross_product");
  }
  return sm_out;
}

#cross_product!(rhs) ⇒ Object

Calls #cross_product(rhs, self)

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



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

def cross_product!(rhs)
  cross_product rhs, self
end

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

Divides this vector’s components by a scalar value and returns the result.

call-seq:

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


2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
# File 'ext/snow-math/snow-math.c', line 2896

static VALUE sm_vec3_divide(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  VALUE sm_scalar;
  s_float_t scalar;
  vec3_t *self = sm_unwrap_vec3(sm_self, NULL);

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

  if (SM_IS_A(sm_out, vec3) || SM_IS_A(sm_out, vec4) || SM_IS_A(sm_out, quat)) {
    rb_check_frozen(sm_out);
    vec3_divide(*self, scalar, *sm_unwrap_vec3(sm_out, NULL));
  } else {
    vec3_t out;
    vec3_divide(*self, scalar, out);
    sm_out = sm_wrap_vec3(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



185
186
187
# File 'lib/snow-math/vec3.rb', line 185

def divide!(rhs)
  divide rhs, self
end

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

Returns the dot product of this and another Vec3 or the XYZ components of a Vec4 or Quat.

call-seq:

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


2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
# File 'ext/snow-math/snow-math.c', line 2696

static VALUE sm_vec3_dot_product(VALUE sm_self, VALUE sm_other)
{
  if (!SM_IS_A(sm_other, vec3) &&
      !SM_IS_A(sm_other, vec4) &&
      !SM_IS_A(sm_other, quat)) {
    rb_raise(rb_eArgError,
      "Expected a Quat, Vec3, or Vec4, got %s",
      rb_obj_classname(sm_other));
    return Qnil;
  }
  return DBL2NUM(
    vec3_dot_product(
      *sm_unwrap_vec3(sm_self, NULL),
      *sm_unwrap_vec3(sm_other, NULL)));
}

#fetchObject Also known as: []

Gets the component of the Vec3 at the given index.

call-seq: fetch(index) -> float



2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
# File 'ext/snow-math/snow-math.c', line 2168

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

#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 vec3


2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
# File 'ext/snow-math/snow-math.c', line 2317

static VALUE sm_vec3_inverse(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  vec3_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_vec3(sm_self, NULL);
  if (argc == 1) {
    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);
    vec3_inverse (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_inverse (*self, output);
    sm_out = sm_wrap_vec3(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



116
117
118
# File 'lib/snow-math/vec3.rb', line 116

def inverse!
  inverse self
end

#lengthObject

Returns the length of the Vec3 in components. Result is always 3.

call-seq: length -> fixnum



2222
2223
2224
2225
# File 'ext/snow-math/snow-math.c', line 2222

static VALUE sm_vec3_length (VALUE self)
{
  return SIZET2NUM(sizeof(vec3_t) / sizeof(s_float_t));
}

#magnitudeObject

Returns the magnitude of self.

call-seq:

magnitude -> float


2852
2853
2854
2855
# File 'ext/snow-math/snow-math.c', line 2852

static VALUE sm_vec3_magnitude(VALUE sm_self)
{
  return DBL2NUM(vec3_length(*sm_unwrap_vec3(sm_self, NULL)));
}

#magnitude_squaredObject

Returns the squared magnitude of self.

call-seq:

magnitude_squared -> float


2839
2840
2841
2842
# File 'ext/snow-math/snow-math.c', line 2839

static VALUE sm_vec3_magnitude_squared(VALUE sm_self)
{
  return DBL2NUM(vec3_length_squared(*sm_unwrap_vec3(sm_self, NULL)));
}

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

Calls #multiply_vec3 and #scale, respectively.

call-seq:

multiply(vec3, output) -> output or new vec3
multiply(scalar, output) -> output or new vec3


146
147
148
149
150
151
152
# File 'lib/snow-math/vec3.rb', line 146

def multiply(rhs, output = nil)
  case rhs
  when ::Snow::Vec3, ::Snow::Vec4, ::Snow::Quat 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)

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



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

def multiply!(rhs)
  multiply rhs, self
end

#multiply_vec3(*args) ⇒ Object

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

call-seq:

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


2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
# File 'ext/snow-math/snow-math.c', line 2545

static VALUE sm_vec3_multiply(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec3_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec3(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);
    vec3_multiply(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_multiply(*self, *rhs, output);
    sm_out = sm_wrap_vec3(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_vec3");
  }
  return sm_out;
}

#multiply_vec3!(rhs) ⇒ Object

Calls #multiply_vec3(rhs, self)

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



137
138
139
# File 'lib/snow-math/vec3.rb', line 137

def multiply_vec3!(rhs)
  multiply_vec3 rhs, self
end

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

Negates this vector’s components and returns the result.

call-seq:

negate(output = nil) -> output or new vec3


2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
# File 'ext/snow-math/snow-math.c', line 2357

static VALUE sm_vec3_negate(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  vec3_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_vec3(sm_self, NULL);
  if (argc == 1) {
    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);
    vec3_negate (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_negate (*self, output);
    sm_out = sm_wrap_vec3(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



123
124
125
# File 'lib/snow-math/vec3.rb', line 123

def negate!
  negate self
end

#normalize(*args) ⇒ Object

Returns a vector whose components are the multiplicative inverse of this vector’s.

call-seq:

normalize(output = nil) -> output or new vec3


2276
2277
2278
2279
2280
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
# File 'ext/snow-math/snow-math.c', line 2276

static VALUE sm_vec3_normalize(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  vec3_t *self;
  rb_scan_args(argc, argv, "01", &sm_out);
  self = sm_unwrap_vec3(sm_self, NULL);
  if (argc == 1) {
    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);
    vec3_normalize (*self, *output);
  }} else if (argc == 0) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_normalize (*self, output);
    sm_out = sm_wrap_vec3(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



109
110
111
# File 'lib/snow-math/vec3.rb', line 109

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 vec3


2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
# File 'ext/snow-math/snow-math.c', line 2397

static VALUE sm_vec3_project(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec3_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec3(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);
    vec3_project(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_project(*self, *rhs, output);
    sm_out = sm_wrap_vec3(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to project");
  }
  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 vec3


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

static VALUE sm_vec3_reflect(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec3_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec3(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);
    vec3_reflect(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_reflect(*self, *rhs, output);
    sm_out = sm_wrap_vec3(output, rb_obj_class(sm_self));
    rb_obj_call_init(sm_out, 0, 0);
  }} else {
    rb_raise(rb_eArgError, "Invalid number of arguments to reflect");
  }
  return sm_out;
}

#scale(*args) ⇒ Object

Scales this vector’s components by a scalar value and returns the result.

call-seq:

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


2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
# File 'ext/snow-math/snow-math.c', line 2865

static VALUE sm_vec3_scale(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_out;
  VALUE sm_scalar;
  s_float_t scalar;
  vec3_t *self = sm_unwrap_vec3(sm_self, NULL);

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

  if (SM_IS_A(sm_out, vec3) || SM_IS_A(sm_out, vec4) || SM_IS_A(sm_out, quat)) {
    rb_check_frozen(sm_out);
    vec3_scale(*self, scalar, *sm_unwrap_vec3(sm_out, NULL));
  } else {
    vec3_t out;
    vec3_scale(*self, scalar, out);
    sm_out = sm_wrap_vec3(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



178
179
180
# File 'lib/snow-math/vec3.rb', line 178

def scale!(rhs)
  scale rhs, self
end

#set(*args) ⇒ Object

Sets the Vec3’s components.

call-seq:

set(x, y, z)   -> vec3 with components [x, y, z]
set([x, y, z]) -> vec3 with components [x, y, z]
set(vec2)      -> vec3 with components [vec2.xy, 0]
set(vec3)      -> copy of vec3
set(vec4)      -> vec3 with components [vec4.xyz]
set(quat)      -> vec3 with components [quat.xyz]


2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
# File 'ext/snow-math/snow-math.c', line 2745

static VALUE sm_vec3_init(int argc, VALUE *argv, VALUE sm_self)
{
  vec3_t *self = sm_unwrap_vec3(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], vec3) ||
        SM_IS_A(argv[0], vec4) ||
        SM_IS_A(argv[0], quat)) {
      sm_unwrap_vec3(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);
      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)NUM2DBL(rb_ary_entry(arrdata, (long)arr_index));
      }
      break;
    }

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

  /* X, Y, Z */
  case 3: {
    self[0][0] = (s_float_t)NUM2DBL(argv[0]);
    self[0][1] = (s_float_t)NUM2DBL(argv[1]);
    self[0][2] = (s_float_t)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 Vec3. When compiled to use doubles as the base type, this is always 24. Otherwise, when compiled to use floats, it’s always 12.

call-seq: size -> fixnum



2210
2211
2212
2213
# File 'ext/snow-math/snow-math.c', line 2210

static VALUE sm_vec3_size (VALUE self)
{
  return SIZET2NUM(sizeof(vec3_t));
}

#storeObject Also known as: []=

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

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



2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
# File 'ext/snow-math/snow-math.c', line 2187

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

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

Subtracts another vector’s components from this vector’s and returns the result.

call-seq:

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


2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
# File 'ext/snow-math/snow-math.c', line 2644

static VALUE sm_vec3_subtract(int argc, VALUE *argv, VALUE sm_self)
{
  VALUE sm_rhs;
  VALUE sm_out;
  vec3_t *self;
  vec3_t *rhs;
  rb_scan_args(argc, argv, "11", &sm_rhs, &sm_out);
  self = sm_unwrap_vec3(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);
    vec3_subtract(*self, *rhs, *output);
  }} else if (argc == 1) {
SM_LABEL(skip_output): {
    vec3_t output;
    vec3_subtract(*self, *rhs, output);
    sm_out = sm_wrap_vec3(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



171
172
173
# File 'lib/snow-math/vec3.rb', line 171

def subtract!(rhs)
  subtract rhs, self
end

#to_quatObject



60
61
62
# File 'lib/snow-math/vec3.rb', line 60

def to_quat
  Quat.new(self)
end

#to_sObject

Returns a string representation of self.

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

call-seq:

to_s -> string


2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
# File 'ext/snow-math/snow-math.c', line 2820

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

#to_vec2Object



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

def to_vec2
  Vec2.new(self)
end

#to_vec3Object



52
53
54
# File 'lib/snow-math/vec3.rb', line 52

def to_vec3
  Vec3.new(self)
end

#to_vec4Object



56
57
58
# File 'lib/snow-math/vec3.rb', line 56

def to_vec4
  Vec4.new(self)
end

#xObject

Returns the X component of the vector.

call-seq: x -> float



67
68
69
# File 'lib/snow-math/vec3.rb', line 67

def x
  self[0]
end

#x=(value) ⇒ Object

Sets the X component of the vector.

call-seq: x = value -> value



74
75
76
# File 'lib/snow-math/vec3.rb', line 74

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

#yObject

Returns the Y component of the vector.

call-seq: y -> float



81
82
83
# File 'lib/snow-math/vec3.rb', line 81

def y
  self[1]
end

#y=(value) ⇒ Object

Sets the Y component of the vector.

call-seq: y = value -> value



88
89
90
# File 'lib/snow-math/vec3.rb', line 88

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

#zObject

Returns the Z component of the vector.

call-seq: z -> float



95
96
97
# File 'lib/snow-math/vec3.rb', line 95

def z
  self[2]
end

#z=(value) ⇒ Object

Sets the Z component of the vector.

call-seq: z = value -> value



102
103
104
# File 'lib/snow-math/vec3.rb', line 102

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