Class: CGLM::Quat

Inherits:
Vec4 show all
Defined in:
lib/cglm/quat.rb,
ext/cglm/rb_cglm.c

Instance Attribute Summary

Attributes inherited from Base

#addr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vec4

#+, #-, #/, #==, #=~, #[], #[]=, #add_scalar, #add_scalar!, #add_vec4, #add_vec4!, #addadd, #addadd_vec4, alignment, black, #broadcast!, #clamp, #clamp!, #clamp_scalar, #clamp_scalar!, #clamp_vec4, #clamp_vec4!, #distance, #div_scalar, #div_scalar!, #div_vec4, #div_vec4!, #dot, #equalish_scalar, #equalish_vec4, #equals_all, #equals_scalar, #equals_vec4, #flip_signs, #flip_signs!, #highest, #inf?, #inspect, #lowest, #luminance, #max, #min, #mul_scalar, #mul_scalar!, #mul_vec4, #mul_vec4!, #muladd, #muladd_scalar, #muladd_vec4, #nan?, #norm, #norm2, one, #one!, #resize, #resize!, #signs, size, #size, #sqrt, #sub_scalar, #sub_scalar!, #sub_vec4, #sub_vec4!, #subadd, #subadd_vec4, #to_vec3, #valid?, zero, #zero!

Methods inherited from VectorType

#each, #initialize, #to_a, #to_enum

Methods inherited from Base

#dup, #initialize, #initialize_dup

Constructor Details

This class inherits a constructor from CGLM::VectorType

Class Method Details

.axis_angle(axis, angle[, dest]) ⇒ dest | new Quat

Returns:



15
16
17
18
19
20
21
# File 'ext/cglm/rb_cglm_quat.c', line 15

VALUE rb_cglm_quat_new_axis_angle(int argc, VALUE *argv, VALUE self) {
  VALUE axis, angle, dest;
  rb_scan_args(argc, argv, "21", &axis, &angle, &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quatv(VAL2QUAT(dest), NUM2FLT(angle), VAL2VEC3(axis));
  return dest;
}

.identity([dest]) ⇒ dest | new Quat

Returns:



4
5
6
7
8
9
10
# File 'ext/cglm/rb_cglm_quat.c', line 4

VALUE rb_cglm_quat_new_identity(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quat_identity(VAL2QUAT(dest));
  return dest;
}

.look(dir, fwd, up[, dest]) ⇒ dest | new Quat

Creates a rotation quaternion that has the specified orientation. Places the result in dest, or creates a new Quat if dest was omitted. Returns dest.

  • dir is the direction to look (a Vec3).

  • fwd is the forward vector (a Vec3).

  • up is the up vector (a Vec3).

Returns:



242
243
244
245
246
247
248
# File 'ext/cglm/rb_cglm_quat.c', line 242

VALUE rb_cglm_quat_new_look(int argc, VALUE *argv, VALUE self) {
  VALUE dir, fwd, up, dest;
  rb_scan_args(argc, argv, "31", &dir, &fwd, &up, &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quat_for(VAL2VEC3(dir), VAL2VEC3(fwd), VAL2VEC3(up), VAL2QUAT(dest));
  return dest;
}

.look_at(position, target, fwd, up[, dest]) ⇒ dest | new Quat

Creates a rotation quaternion that has the specified orientation. Places the result in dest, or creates a new Quat if dest was omitted. Returns dest.

  • position is the current position to look from (a Vec3).

  • target is the position to look toward (a Vec3).

  • fwd is the forward vector (a Vec3).

  • up is the up vector (a Vec3).

Returns:



264
265
266
267
268
269
270
# File 'ext/cglm/rb_cglm_quat.c', line 264

VALUE rb_cglm_quat_new_look_at(int argc, VALUE *argv, VALUE self) {
  VALUE position, target, fwd, up, dest;
  rb_scan_args(argc, argv, "41", &position, &target, &fwd, &up, &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quat_forp(VAL2VEC3(position), VAL2VEC3(target), VAL2VEC3(fwd), VAL2VEC3(up), VAL2QUAT(dest));
  return dest;
}

.random([dest]) ⇒ dest | new Quat

Fills dest or a new Quat with a random orientation, and returns it.

Returns:



317
318
319
320
321
322
323
324
325
326
# File 'ext/cglm/rb_cglm_quat.c', line 317

VALUE rb_cglm_quat_new_random(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);

  glm_quat(VAL2QUAT(dest), drand48(), drand48(), drand48(), drand48());
  glm_quat_normalize(VAL2QUAT(dest));

  return dest;
}

.random([dest]) ⇒ dest | new Quat

Fills dest or a new Quat with a random orientation, and returns it.

Returns:



317
318
319
320
321
322
323
324
325
326
# File 'ext/cglm/rb_cglm_quat.c', line 317

VALUE rb_cglm_quat_new_random(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);

  glm_quat(VAL2QUAT(dest), drand48(), drand48(), drand48(), drand48());
  glm_quat_normalize(VAL2QUAT(dest));

  return dest;
}

Instance Method Details

#*(other) ⇒ Object



3
4
5
6
7
8
# File 'lib/cglm/quat.rb', line 3

def *(other)
  case other
  when Quat then mul_quat(other)
  else Quat.new(super)
  end
end

#angleNumeric

Returns the angle of the quaternion.

Returns:



112
113
114
# File 'ext/cglm/rb_cglm_quat.c', line 112

VALUE rb_cglm_quat_angle(VALUE self) {
  return DBL2NUM(glm_quat_angle(VAL2QUAT(self)));
}

#axis([dest]) ⇒ dest | new Vec3

Returns the axis of the quaternion in dest.

Returns:



120
121
122
123
124
125
126
# File 'ext/cglm/rb_cglm_quat.c', line 120

VALUE rb_cglm_quat_axis(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = VEC3_NEW(ALLOC_VEC3);
  glm_quat_axis(VAL2QUAT(self), VAL2VEC3(dest));
  return dest;
}

#conjugate([dest]) ⇒ dest | new Quat

Returns:



45
46
47
48
49
50
51
# File 'ext/cglm/rb_cglm_quat.c', line 45

VALUE rb_cglm_quat_conjugate(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quat_conjugate(VAL2QUAT(self), VAL2QUAT(dest));
  return dest;
}

#copy_to(dest) ⇒ Object



24
25
26
27
# File 'ext/cglm/rb_cglm_quat.c', line 24

VALUE rb_cglm_quat_copy_to(VALUE self, VALUE dest) {
  glm_quat_copy(VAL2QUAT(self), VAL2QUAT(dest));
  return dest;
}

#imag([dest]) ⇒ dest | new Vec3

Returns the imaginary part of the quaternion in dest.

Returns:



80
81
82
83
84
85
86
# File 'ext/cglm/rb_cglm_quat.c', line 80

VALUE rb_cglm_quat_imag(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = VEC3_NEW(ALLOC_VEC3);
  glm_quat_imag(VAL2QUAT(self), VAL2VEC3(dest));
  return dest;
}

#imaglenNumeric

Returns the length of the imaginary part of the quaternion.

Returns:



104
105
106
# File 'ext/cglm/rb_cglm_quat.c', line 104

VALUE rb_cglm_quat_imaglen(VALUE self) {
  return DBL2NUM(glm_quat_imaglen(VAL2QUAT(self)));
}

#imagn([dest]) ⇒ dest | new Vec3

Returns the imaginary part of the quaternion in dest, normalized.

Returns:



92
93
94
95
96
97
98
# File 'ext/cglm/rb_cglm_quat.c', line 92

VALUE rb_cglm_quat_imagn(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = VEC3_NEW(ALLOC_VEC3);
  glm_quat_imagn(VAL2QUAT(self), VAL2VEC3(dest));
  return dest;
}

#invert([dest]) ⇒ dest | new Quat

Returns:



54
55
56
57
58
59
60
# File 'ext/cglm/rb_cglm_quat.c', line 54

VALUE rb_cglm_quat_invert(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quat_inv(VAL2QUAT(self), VAL2QUAT(dest));
  return dest;
}

#invert!self

Returns:

  • (self)


63
64
65
66
# File 'ext/cglm/rb_cglm_quat.c', line 63

VALUE rb_cglm_quat_invert_self(VALUE self) {
  glm_quat_inv(VAL2QUAT(self), VAL2QUAT(self));
  return self;
}

#lerp!(from, to, amount) ⇒ self

Returns:

  • (self)


205
206
207
208
# File 'ext/cglm/rb_cglm_quat.c', line 205

VALUE rb_cglm_quat_lerp_self(VALUE self, VALUE from, VALUE to, VALUE amount) {
  glm_quat_lerp(VAL2QUAT(from), VAL2QUAT(to), NUM2FLT(amount), VAL2QUAT(self));
  return self;
}

#look(position[, dest]) ⇒ dest | new Mat4

Creates a view matrix, stored in dest, using this quaternion as the camera orientation. Returns dest. Creates a new Mat4 if dest is omitted.

Returns:



222
223
224
225
226
227
228
# File 'ext/cglm/rb_cglm_quat.c', line 222

VALUE rb_cglm_quat_look(int argc, VALUE *argv, VALUE self) {
  VALUE position, dest;
  rb_scan_args(argc, argv, "11", &position, &dest);
  if (NIL_P(dest)) dest = MAT4_NEW(ALLOC_MAT4);
  glm_quat_look(VAL2VEC3(position), VAL2QUAT(self), VAL2MAT4(dest));
  return dest;
}

#mul_quat(other[, dest]) ⇒ dest | new Quat

Multiplies self by other and stores the result in dest. If dest is omitted, a new Quat is created. dest or the new Quat is returned.

Returns:



133
134
135
136
137
138
139
# File 'ext/cglm/rb_cglm_quat.c', line 133

VALUE rb_cglm_quat_mul_quat(int argc, VALUE *argv, VALUE self) {
  VALUE other, dest;
  rb_scan_args(argc, argv, "11", &other, &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quat_mul(VAL2QUAT(self), VAL2QUAT(other), VAL2QUAT(dest));
  return dest;
}

#mul_quat!(other) ⇒ self

Multiplies self by other. Modifies self in-place and returns self.

Returns:

  • (self)


145
146
147
148
149
150
# File 'ext/cglm/rb_cglm_quat.c', line 145

VALUE rb_cglm_quat_mul_quat_self(VALUE self, VALUE other) {
  quat dest;
  glm_quat_mul(VAL2QUAT(self), VAL2QUAT(other), dest);
  memcpy(&VAL2QUAT(self), &dest, sizeof(quat));
  return self;
}

#normalize([dest]) ⇒ Object



30
31
32
33
34
35
36
# File 'ext/cglm/rb_cglm_quat.c', line 30

VALUE rb_cglm_quat_normalize(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = QUAT_NEW(ALLOC_QUAT);
  glm_quat_normalize_to(VAL2QUAT(self), VAL2QUAT(dest));
  return dest;
}

#normalize!self

Returns:

  • (self)


39
40
41
42
# File 'ext/cglm/rb_cglm_quat.c', line 39

VALUE rb_cglm_quat_normalize_self(VALUE self) {
  glm_quat_normalize(VAL2QUAT(self));
  return self;
}

#pivot_mat4(mat, pivot_point[, dest]) ⇒ dest | new Mat4

Rotates the given transform matrix using this quaternion at the pivot point (a Vec3). Places the resultant Mat4 into dest, or creates a new Mat4 if dest is omitted. Returns dest.

Returns:



304
305
306
307
308
309
310
311
# File 'ext/cglm/rb_cglm_quat.c', line 304

VALUE rb_cglm_quat_pivot_mat4(int argc, VALUE *argv, VALUE self) {
  VALUE mat, pivot, dest;
  rb_scan_args(argc, argv, "21", &mat, &pivot, &dest);
  if (NIL_P(dest)) dest = MAT4_NEW(ALLOC_MAT4);
  memcpy(&VAL2MAT4(dest), &VAL2MAT4(mat), sizeof(mat4));
  glm_quat_rotate_at(VAL2MAT4(dest), VAL2QUAT(self), VAL2VEC3(pivot));
  return dest;
}

#realNumeric

Returns the real part of the quaternion.

Returns:



72
73
74
# File 'ext/cglm/rb_cglm_quat.c', line 72

VALUE rb_cglm_quat_real(VALUE self) {
  return DBL2NUM(glm_quat_real(VAL2QUAT(self)));
}

#rotate_mat4(vec[, dest]) ⇒ dest | new Mat4

Rotates the given Mat4 by this quaternion. Returns the result in dest, or a new Mat4 if dest was omitted.

Returns:



290
291
292
293
294
295
296
# File 'ext/cglm/rb_cglm_quat.c', line 290

VALUE rb_cglm_quat_rotate_mat4(int argc, VALUE *argv, VALUE self) {
  VALUE mat, dest;
  rb_scan_args(argc, argv, "11", &mat, &dest);
  if (NIL_P(dest)) dest = MAT4_NEW(ALLOC_MAT4);
  glm_quat_rotate(VAL2MAT4(mat), VAL2QUAT(self), VAL2MAT4(dest));
  return dest;
}

#rotate_vec3(vec[, dest]) ⇒ dest | new Vec3

Rotates the given Vec3 by this quaternion. Returns the result in dest, or a new Vec3 if dest was omitted.

Returns:



277
278
279
280
281
282
283
# File 'ext/cglm/rb_cglm_quat.c', line 277

VALUE rb_cglm_quat_rotate_vec3(int argc, VALUE *argv, VALUE self) {
  VALUE vec, dest;
  rb_scan_args(argc, argv, "11", &vec, &dest);
  if (NIL_P(dest)) dest = VEC3_NEW(ALLOC_VEC3);
  glm_quat_rotatev(VAL2QUAT(self), VAL2VEC3(vec), VAL2VEC3(dest));
  return dest;
}

#slerp!(from, to, amount) ⇒ self

Returns:

  • (self)


211
212
213
214
# File 'ext/cglm/rb_cglm_quat.c', line 211

VALUE rb_cglm_quat_slerp_self(VALUE self, VALUE from, VALUE to, VALUE amount) {
  glm_quat_slerp(VAL2QUAT(from), VAL2QUAT(to), NUM2FLT(amount), VAL2QUAT(self));
  return self;
}

#to_mat3([dest]) ⇒ dest | new Mat3

Converts this quaternion into a Mat3. Places the result into dest or creates a new Mat3.

Returns:



183
184
185
186
187
188
189
# File 'ext/cglm/rb_cglm_quat.c', line 183

VALUE rb_cglm_quat_to_mat3(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = MAT3_NEW(ALLOC_MAT3);
  glm_quat_mat3(VAL2QUAT(self), VAL2MAT3(dest));
  return dest;
}

#to_mat4([dest]) ⇒ dest | new Mat4

Converts this quaternion into a Mat4. Places the result into dest or creates a new Mat4.

Returns:



157
158
159
160
161
162
163
# File 'ext/cglm/rb_cglm_quat.c', line 157

VALUE rb_cglm_quat_to_mat4(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = MAT4_NEW(ALLOC_MAT4);
  glm_quat_mat4(VAL2QUAT(self), VAL2MAT4(dest));
  return dest;
}

#to_transposed_mat3([dest]) ⇒ dest | new Mat3

Converts this quaternion into a transposed Mat3. Places the result into dest or creates a new Mat3.

Returns:



196
197
198
199
200
201
202
# File 'ext/cglm/rb_cglm_quat.c', line 196

VALUE rb_cglm_quat_to_mat3t(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = MAT3_NEW(ALLOC_MAT3);
  glm_quat_mat3t(VAL2QUAT(self), VAL2MAT3(dest));
  return dest;
}

#to_transposed_mat4([dest]) ⇒ dest | new Mat4

Converts this quaternion into a transposed Mat4. Places the result into dest or creates a new Mat4.

Returns:



170
171
172
173
174
175
176
# File 'ext/cglm/rb_cglm_quat.c', line 170

VALUE rb_cglm_quat_to_mat4t(int argc, VALUE *argv, VALUE self) {
  VALUE dest;
  rb_scan_args(argc, argv, "01", &dest);
  if (NIL_P(dest)) dest = MAT4_NEW(ALLOC_MAT4);
  glm_quat_mat4t(VAL2QUAT(self), VAL2MAT4(dest));
  return dest;
}