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
@@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]


2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
# File 'ext/snow-math/snow-math.c', line 2900

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)rb_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)rb_num2dbl(argv[0]);
    self[0][1] = (s_float_t)rb_num2dbl(argv[1]);
    self[0][2] = (s_float_t)rb_num2dbl(argv[2]);
    break;
  }

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

  return sm_self;
}

Dynamic Method Handling

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

Class Method Details

.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


2880
2881
2882
2883
2884
2885
# File 'ext/snow-math/snow-math.c', line 2880

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


3086
3087
3088
3089
3090
3091
3092
3093
# File 'ext/snow-math/snow-math.c', line 3086

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


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

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



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

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

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

Returns a copy of self.

call-seq:

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


2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
# File 'ext/snow-math/snow-math.c', line 2390

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


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
2684
2685
2686
2687
2688
2689
# File 'ext/snow-math/snow-math.c', line 2650

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


3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
# File 'ext/snow-math/snow-math.c', line 3051

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


2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
# File 'ext/snow-math/snow-math.c', line 2851

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



2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
# File 'ext/snow-math/snow-math.c', line 2323

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


2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
# File 'ext/snow-math/snow-math.c', line 2472

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



2377
2378
2379
2380
# File 'ext/snow-math/snow-math.c', line 2377

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


3007
3008
3009
3010
# File 'ext/snow-math/snow-math.c', line 3007

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

#magnitude_squaredObject

Returns the squared magnitude of self.

call-seq:

magnitude_squared -> float


2994
2995
2996
2997
# File 'ext/snow-math/snow-math.c', line 2994

static VALUE sm_vec3_magnitude_squared(VALUE sm_self)
{
  return rb_float_new(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


2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
# File 'ext/snow-math/snow-math.c', line 2700

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


2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
# File 'ext/snow-math/snow-math.c', line 2512

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


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

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


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
2585
2586
2587
2588
2589
2590
2591
# File 'ext/snow-math/snow-math.c', line 2552

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


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
2634
2635
2636
2637
2638
2639
2640
# File 'ext/snow-math/snow-math.c', line 2601

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


3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
# File 'ext/snow-math/snow-math.c', line 3020

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 = rb_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]


2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
# File 'ext/snow-math/snow-math.c', line 2900

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)rb_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)rb_num2dbl(argv[0]);
    self[0][1] = (s_float_t)rb_num2dbl(argv[1]);
    self[0][2] = (s_float_t)rb_num2dbl(argv[2]);
    break;
  }

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

  return sm_self;
}

#sizeObject

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



2365
2366
2367
2368
# File 'ext/snow-math/snow-math.c', line 2365

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



2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
# File 'ext/snow-math/snow-math.c', line 2342

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


2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
# File 'ext/snow-math/snow-math.c', line 2799

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


2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
# File 'ext/snow-math/snow-math.c', line 2975

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