2 * Copyright (c) 2007-2010 SlimDX Group
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "../design/QuaternionConverter.h"
26 using System::Runtime::InteropServices::OutAttribute;
34 /// Defines a four dimensional mathematical quaternion.
36 /// <unmanaged>D3DXQUATERNION</unmanaged>
37 [System::Serializable]
38 [System::Runtime::InteropServices::StructLayout( System::Runtime::InteropServices::LayoutKind::Sequential )]
39 [System::ComponentModel::TypeConverter( SlimDX::Design::QuaternionConverter::typeid )]
40 public value class Quaternion : System::IEquatable<Quaternion>
44 /// Gets or sets the X component of the quaternion.
46 /// <value>The X component of the quaternion.</value>
50 /// Gets or sets the Y component of the quaternion.
52 /// <value>The Y component of the quaternion.</value>
56 /// Gets or sets the Z component of the quaternion.
58 /// <value>The Z component of the quaternion.</value>
62 /// Gets or sets the W component of the quaternion.
64 /// <value>The W component of the quaternion.</value>
68 /// Initializes a new instance of the <see cref="Quaternion"/> structure.
70 /// <param name="x">The X component of the quaternion.</param>
71 /// <param name="y">The Y component of the quaternion.</param>
72 /// <param name="z">The Z component of the quaternion.</param>
73 /// <param name="w">The W component of the quaternion.</param>
74 Quaternion( float x, float y, float z, float w );
77 /// Initializes a new instance of the <see cref="Quaternion"/> structure.
79 /// <param name="value">A <see cref="Vector3"/> containing the first three values of the quaternion.</param>
80 /// <param name="w">The W component of the quaternion.</param>
81 Quaternion( Vector3 value, float w );
84 /// Gets the identity <see cref="Quaternion"/> (0, 0, 0, 1).
86 static property Quaternion Identity { Quaternion get(); }
89 /// Gets a value indicating whether this instance is an identity <see cref="Quaternion"/>.
91 [System::ComponentModel::Browsable(false)]
92 property bool IsIdentity { bool get(); }
95 /// Gets the axis components of the quaternion.
97 property Vector3 Axis { Vector3 get(); }
100 /// Gets the angle of the quaternion.
102 property float Angle { float get(); }
105 /// Calculates the length of the quaternion.
107 /// <returns>The length of the quaternion.</returns>
111 /// Calculates the squared length of the quaternion.
113 /// <returns>The squared length of the quaternion.</returns>
114 float LengthSquared();
117 /// Converts the quaternion into a unit quaternion.
122 /// Conjugates the quaternion.
127 /// Conjugates and renormalizes the quaternion.
132 /// Adds two quaternions.
134 /// <param name="left">The first quaternion to add.</param>
135 /// <param name="right">The second quaternion to add.</param>
136 /// <returns>The sum of the two quaternions.</returns>
137 static Quaternion Add( Quaternion left, Quaternion right );
140 /// Adds two quaternions.
142 /// <param name="left">The first quaternion to add.</param>
143 /// <param name="right">The second quaternion to add.</param>
144 /// <param name="result">When the method completes, contains the sum of the two quaternions.</param>
145 static void Add( Quaternion% left, Quaternion% right, [Out] Quaternion% result );
148 /// Returns a <see cref="Quaternion"/> containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 2D triangle.
150 /// <param name="source1">A <see cref="Quaternion"/> containing the 4D Cartesian coordinates of vertex 1 of the triangle.</param>
151 /// <param name="source2">A <see cref="Quaternion"/> containing the 4D Cartesian coordinates of vertex 2 of the triangle.</param>
152 /// <param name="source3">A <see cref="Quaternion"/> containing the 4D Cartesian coordinates of vertex 3 of the triangle.</param>
153 /// <param name="weight1">Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in <paramref name="source2"/>).</param>
154 /// <param name="weight2">Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in <paramref name="source3"/>).</param>
155 /// <returns>A new <see cref="Quaternion"/> containing the 4D Cartesian coordinates of the specified point.</returns>
156 static Quaternion Barycentric( Quaternion source1, Quaternion source2, Quaternion source3, float weight1, float weight2 );
159 /// Returns a <see cref="Quaternion"/> containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 2D triangle.
161 /// <param name="source1">A <see cref="Quaternion"/> containing the 4D Cartesian coordinates of vertex 1 of the triangle.</param>
162 /// <param name="source2">A <see cref="Quaternion"/> containing the 4D Cartesian coordinates of vertex 2 of the triangle.</param>
163 /// <param name="source3">A <see cref="Quaternion"/> containing the 4D Cartesian coordinates of vertex 3 of the triangle.</param>
164 /// <param name="weight1">Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in <paramref name="source2"/>).</param>
165 /// <param name="weight2">Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in <paramref name="source3"/>).</param>
166 /// <param name="result">When the method completes, contains a new <see cref="Quaternion"/> containing the 4D Cartesian coordinates of the specified point.</param>
167 static void Barycentric( Quaternion% source1, Quaternion% source2, Quaternion% source3, float weight1, float weight2, [Out] Quaternion% result );
170 /// Conjugates a quaternion.
172 /// <param name="quaternion">The quaternion to conjugate.</param>
173 /// <returns>The conjugated quaternion.</returns>
174 static Quaternion Conjugate( Quaternion quaternion );
177 /// Conjugates a quaternion.
179 /// <param name="quaternion">The quaternion to conjugate.</param>
180 /// <param name="result">When the method completes, contains the conjugated quaternion.</param>
181 static void Conjugate( Quaternion% quaternion, [Out] Quaternion% result );
184 /// Divides a quaternion by another.
186 /// <param name="left">The first quaternion to divide.</param>
187 /// <param name="right">The second quaternion to divide.</param>
188 /// <returns>The divided quaternion.</returns>
189 static Quaternion Divide( Quaternion left, Quaternion right );
192 /// Divides a quaternion by another.
194 /// <param name="left">The first quaternion to divide.</param>
195 /// <param name="right">The second quaternion to divide.</param>
196 /// <returns>The divided quaternion.</returns>
197 static void Divide( Quaternion% left, Quaternion% right, [Out] Quaternion% result );
200 /// Calculates the dot product of two quaternions.
202 /// <param name="left">First source quaternion.</param>
203 /// <param name="right">Second source quaternion.</param>
204 /// <returns>The dot product of the two quaternions.</returns>
205 static float Dot( Quaternion left, Quaternion right );
208 /// Exponentiates a quaternion.
210 /// <param name="quaternion">The quaternion to exponentiate.</param>
211 /// <returns>The exponentiated quaternion.</returns>
212 static Quaternion Exponential( Quaternion quaternion );
215 /// Exponentiates a quaternion.
217 /// <param name="quaternion">The quaternion to exponentiate.</param>
218 /// <param name="result">When the method completes, contains the exponentiated quaternion.</param>
219 static void Exponential( Quaternion% quaternion, [Out] Quaternion% result );
222 /// Conjugates and renormalizes the quaternion.
224 /// <param name="quaternion">The quaternion to conjugate and renormalize.</param>
225 /// <returns>The conjugated and renormalized quaternion.</returns>
226 static Quaternion Invert( Quaternion quaternion );
229 /// Conjugates and renormalizes the quaternion.
231 /// <param name="quaternion">The quaternion to conjugate and renormalize.</param>
232 /// <param name="result">When the method completes, contains the conjugated and renormalized quaternion.</param>
233 static void Invert( Quaternion% quaternion, [Out] Quaternion% result );
236 /// Performs a linear interpolation between two quaternion.
238 /// <param name="start">Start quaternion.</param>
239 /// <param name="end">End quaternion.</param>
240 /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
241 /// <returns>The linear interpolation of the two quaternions.</returns>
243 /// This method performs the linear interpolation based on the following formula.
244 /// <code>start + (end - start) * amount</code>
245 /// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
247 static Quaternion Lerp( Quaternion start, Quaternion end, float amount );
250 /// Performs a linear interpolation between two quaternions.
252 /// <param name="start">Start quaternion.</param>
253 /// <param name="end">End quaternion.</param>
254 /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
255 /// <param name="result">When the method completes, contains the linear interpolation of the two quaternions.</param>
257 /// This method performs the linear interpolation based on the following formula.
258 /// <code>start + (end - start) * amount</code>
259 /// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
261 static void Lerp( Quaternion% start, Quaternion% end, float amount, [Out] Quaternion% result );
264 /// Calculates the natural logarithm of the specified quaternion.
266 /// <param name="quaternion">The quaternion whose logarithm will be calculated.</param>
267 /// <returns>The natural logarithm of the quaternion.</returns>
268 static Quaternion Logarithm( Quaternion quaternion );
271 /// Calculates the natural logarithm of the specified quaternion.
273 /// <param name="quaternion">The quaternion whose logarithm will be calculated.</param>
274 /// <param name="result">When the method completes, contains the natural logarithm of the quaternion.</param>
275 static void Logarithm( Quaternion% quaternion, [Out] Quaternion% result );
278 /// Modulates a quaternion by another.
280 /// <param name="left">The first quaternion to modulate.</param>
281 /// <param name="right">The second quaternion to modulate.</param>
282 /// <returns>The modulated quaternion.</returns>
283 static Quaternion Multiply( Quaternion left, Quaternion right );
286 /// Modulates a quaternion by another.
288 /// <param name="left">The first quaternion to modulate.</param>
289 /// <param name="right">The second quaternion to modulate.</param>
290 /// <param name="result">When the moethod completes, contains the modulated quaternion.</param>
291 static void Multiply( Quaternion% left, Quaternion% right, [Out] Quaternion% result );
294 /// Scales a quaternion by the given value.
296 /// <param name="quaternion">The quaternion to scale.</param>
297 /// <param name="scale">The amount by which to scale the quaternion.</param>
298 /// <returns>The scaled quaternion.</returns>
299 static Quaternion Multiply( Quaternion quaternion, float scale );
302 /// Scales a quaternion by the given value.
304 /// <param name="quaternion">The quaternion to scale.</param>
305 /// <param name="scale">The amount by which to scale the quaternion.</param>
306 /// <param name="result">When the method completes, contains the scaled quaternion.</param>
307 static void Multiply( Quaternion% quaternion, float scale, [Out] Quaternion% result );
310 /// Reverses the direction of a given quaternion.
312 /// <param name="quaternion">The quaternion to negate.</param>
313 /// <returns>A quaternion facing in the opposite direction.</returns>
314 static Quaternion Negate( Quaternion quaternion );
317 /// Reverses the direction of a given quaternion.
319 /// <param name="quaternion">The quaternion to negate.</param>
320 /// <param name="result">When the method completes, contains a quaternion facing in the opposite direction.</param>
321 static void Negate( Quaternion% quaternion, [Out] Quaternion% result );
324 /// Converts the quaternion into a unit quaternion.
326 /// <param name="quaternion">The quaternion to normalize.</param>
327 /// <returns>The normalized quaternion.</returns>
328 static Quaternion Normalize( Quaternion quaternion );
331 /// Converts the quaternion into a unit quaternion.
333 /// <param name="quaternion">The quaternion to normalize.</param>
334 /// <param name="result">When the method completes, contains the normalized quaternion.</param>
335 static void Normalize( Quaternion% quaternion, [Out] Quaternion% result );
338 /// Creates a quaternion given a rotation and an axis.
340 /// <param name="axis">The axis of rotation.</param>
341 /// <param name="angle">The angle of rotation.</param>
342 /// <returns>The newly created quaternion.</returns>
343 static Quaternion RotationAxis( Vector3 axis, float angle );
346 /// Creates a quaternion given a rotation and an axis.
348 /// <param name="axis">The axis of rotation.</param>
349 /// <param name="angle">The angle of rotation.</param>
350 /// <param name="result">When the method completes, contains the newly created quaternion.</param>
351 static void RotationAxis( Vector3% axis, float angle, [Out] Quaternion% result );
354 /// Creates a quaternion given a rotation matrix.
356 /// <param name="matrix">The rotation matrix.</param>
357 /// <returns>The newly created quaternion.</returns>
358 static Quaternion RotationMatrix( Matrix matrix );
361 /// Creates a quaternion given a rotation matrix.
363 /// <param name="matrix">The rotation matrix.</param>
364 /// <param name="result">When the method completes, contains the newly created quaternion.</param>
365 static void RotationMatrix( Matrix% matrix, [Out] Quaternion% result );
368 /// Creates a quaternion given a yaw, pitch, and roll value.
370 /// <param name="yaw">The yaw of rotation.</param>
371 /// <param name="pitch">The pitch of rotation.</param>
372 /// <param name="roll">The roll of rotation.</param>
373 /// <returns>The newly created quaternion.</returns>
374 static Quaternion RotationYawPitchRoll( float yaw, float pitch, float roll );
377 /// Creates a quaternion given a yaw, pitch, and roll value.
379 /// <param name="yaw">The yaw of rotation.</param>
380 /// <param name="pitch">The pitch of rotation.</param>
381 /// <param name="roll">The roll of rotation.</param>
382 /// <param name="result">When the method completes, contains the newly created quaternion.</param>
383 static void RotationYawPitchRoll( float yaw, float pitch, float roll, [Out] Quaternion% result );
386 /// Interpolates between two quaternions, using spherical linear interpolation.
388 /// <param name="start">Start quaternion.</param>
389 /// <param name="end">End quaternion.</param>
390 /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
391 /// <returns>The spherical linear interpolation of the two quaternions.</returns>
392 static Quaternion Slerp( Quaternion start, Quaternion end, float amount );
395 /// Interpolates between two quaternions, using spherical linear interpolation.
397 /// <param name="start">Start quaternion.</param>
398 /// <param name="end">End quaternion.</param>
399 /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
400 /// <param name="result">When the method completes, contains the spherical linear interpolation of the two quaternions.</param>
401 static void Slerp( Quaternion% start, Quaternion% end, float amount, [Out] Quaternion% result );
404 /// Interpolates between quaternions, using spherical quadrangle interpolation.
406 /// <param name="source1">First source quaternion.</param>
407 /// <param name="source2">Second source quaternion.</param>
408 /// <param name="source3">Thrid source quaternion.</param>
409 /// <param name="source4">Fourth source quaternion.</param>
410 /// <param name="amount">Value between 0 and 1 indicating the weight of interpolation.</param>
411 /// <returns>The spherical quadrangle interpolation of the quaternions.</returns>
412 static Quaternion Squad( Quaternion source1, Quaternion source2, Quaternion source3, Quaternion source4, float amount );
415 /// Interpolates between quaternions, using spherical quadrangle interpolation.
417 /// <param name="source1">First source quaternion.</param>
418 /// <param name="source2">Second source quaternion.</param>
419 /// <param name="source3">Thrid source quaternion.</param>
420 /// <param name="source4">Fourth source quaternion.</param>
421 /// <param name="amount">Value between 0 and 1 indicating the weight of interpolation.</param>
422 /// <param name="result">When the method completes, contains the spherical quadrangle interpolation of the quaternions.</param>
423 static void Squad( Quaternion% source1, Quaternion% source2, Quaternion% source3, Quaternion% source4, float amount, [Out] Quaternion% result );
426 /// Sets up control points for spherical quadrangle interpolation.
428 /// <param name="source1">First source quaternion.</param>
429 /// <param name="source2">Second source quaternion.</param>
430 /// <param name="source3">Third source quaternion.</param>
431 /// <param name="source4">Fourth source quaternion.</param>
432 /// <returns>An array of three quaternions that represent control points for spherical quadrangle interpolation.</returns>
433 static array<Quaternion>^ SquadSetup( Quaternion source1, Quaternion source2, Quaternion source3, Quaternion source4 );
436 /// Subtracts two quaternions.
438 /// <param name="left">The first quaternion to subtract.</param>
439 /// <param name="right">The second quaternion to subtract.</param>
440 /// <returns>The difference of the two quaternions.</returns>
441 static Quaternion Subtract( Quaternion left, Quaternion right );
444 /// Subtracts two quaternions.
446 /// <param name="left">The first quaternion to subtract.</param>
447 /// <param name="right">The second quaternion to subtract.</param>
448 /// <param name="result">When the method completes, contains the difference of the two quaternions.</param>
449 static void Subtract( Quaternion% left, Quaternion% right, [Out] Quaternion% result );
452 /// Multiplies a quaternion by another.
454 /// <param name="left">The first quaternion to multiply.</param>
455 /// <param name="right">The second quaternion to multiply.</param>
456 /// <returns>The multiplied quaternion.</returns>
457 static Quaternion operator * ( Quaternion left, Quaternion right );
460 /// Scales a quaternion by the given value.
462 /// <param name="quaternion">The quaternion to scale.</param>
463 /// <param name="scale">The amount by which to scale the quaternion.</param>
464 /// <returns>The scaled quaternion.</returns>
465 static Quaternion operator * ( Quaternion quaternion, float scale );
468 /// Scales a quaternion by the given value.
470 /// <param name="quaternion">The quaternion to scale.</param>
471 /// <param name="scale">The amount by which to scale the quaternion.</param>
472 /// <returns>The scaled quaternion.</returns>
473 static Quaternion operator * ( float scale, Quaternion quaternion );
476 /// Divides a quaternion by another.
478 /// <param name="left">The first quaternion to divide.</param>
479 /// <param name="right">The second quaternion to divide.</param>
480 /// <returns>The divided quaternion.</returns>
481 static Quaternion operator / ( Quaternion left, float right );
484 /// Adds two quaternions.
486 /// <param name="left">The first quaternion to add.</param>
487 /// <param name="right">The second quaternion to add.</param>
488 /// <returns>The sum of the two quaternions.</returns>
489 static Quaternion operator + ( Quaternion left, Quaternion right );
492 /// Subtracts two quaternions.
494 /// <param name="left">The first quaternion to subtract.</param>
495 /// <param name="right">The second quaternion to subtract.</param>
496 /// <returns>The difference of the two quaternions.</returns>
497 static Quaternion operator - ( Quaternion left, Quaternion right );
500 /// Reverses the direction of a given quaternion.
502 /// <param name="quaternion">The quaternion to negate.</param>
503 /// <returns>A quaternion facing in the opposite direction.</returns>
504 static Quaternion operator - ( Quaternion quaternion );
507 /// Tests for equality between two objects.
509 /// <param name="left">The first value to compare.</param>
510 /// <param name="right">The second value to compare.</param>
511 /// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
512 static bool operator == ( Quaternion left, Quaternion right );
515 /// Tests for inequality between two objects.
517 /// <param name="left">The first value to compare.</param>
518 /// <param name="right">The second value to compare.</param>
519 /// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
520 static bool operator != ( Quaternion left, Quaternion right );
523 /// Converts the value of the object to its equivalent string representation.
525 /// <returns>The string representation of the value of this instance.</returns>
526 virtual System::String^ ToString() override;
529 /// Returns the hash code for this instance.
531 /// <returns>A 32-bit signed integer hash code.</returns>
532 virtual int GetHashCode() override;
535 /// Returns a value that indicates whether the current instance is equal to a specified object.
537 /// <param name="obj">Object to make the comparison with.</param>
538 /// <returns><c>true</c> if the current instance is equal to the specified object; <c>false</c> otherwise.</returns>
539 virtual bool Equals( System::Object^ obj ) override;
542 /// Returns a value that indicates whether the current instance is equal to the specified object.
544 /// <param name="other">Object to make the comparison with.</param>
545 /// <returns><c>true</c> if the current instance is equal to the specified object; <c>false</c> otherwise.</returns>
546 virtual bool Equals( Quaternion other );
549 /// Determines whether the specified object instances are considered equal.
551 /// <param name="value1"></param>
552 /// <param name="value2"></param>
553 /// <returns><c>true</c> if <paramref name="value1"/> is the same instance as <paramref name="value2"/> or
554 /// if both are <c>null</c> references or if <c>value1.Equals(value2)</c> returns <c>true</c>; otherwise, <c>false</c>.</returns>
555 static bool Equals( Quaternion% value1, Quaternion% value2 );