30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
33 using SiliconStudio.Core.Serialization;
35 namespace SiliconStudio.Core.Mathematics
41 [StructLayout(LayoutKind.Sequential, Pack = 4)]
60 Normal.X = Normal.Y = Normal.Z = D = value;
70 public Plane(
float a,
float b,
float c,
float d)
86 this.D = -Vector3.Dot(normal, point);
108 float x1 = point2.X - point1.X;
109 float y1 = point2.Y - point1.Y;
110 float z1 = point2.Z - point1.Z;
111 float x2 = point3.X - point1.X;
112 float y2 = point3.Y - point1.Y;
113 float z2 = point3.Z - point1.Z;
114 float yz = (y1 * z2) - (z1 * y2);
115 float xz = (z1 * x2) - (x1 * z2);
116 float xy = (x1 * y2) - (y1 * x2);
117 float invPyth = 1.0f / (float)(Math.Sqrt((yz * yz) + (xz * xz) + (xy * xy)));
119 Normal.X = yz * invPyth;
120 Normal.Y = xz * invPyth;
121 Normal.Z = xy * invPyth;
122 D = -((Normal.X * point1.X) + (
Normal.Y * point1.
Y) + (Normal.Z * point1.Z));
134 throw new ArgumentNullException(
"values");
135 if (values.Length != 4)
136 throw new ArgumentOutOfRangeException(
"values",
"There must be four and only four input values for Plane.");
138 Normal.X = values[0];
139 Normal.Y = values[1];
140 Normal.Z = values[2];
151 public float this[
int index]
157 case 0:
return Normal.X;
158 case 1:
return Normal.Y;
159 case 2:
return Normal.Z;
163 throw new ArgumentOutOfRangeException(
"index",
"Indices for Plane run from 0 to 3, inclusive.");
170 case 0: Normal.X = value;
break;
171 case 1: Normal.Y = value;
break;
172 case 2: Normal.Z = value;
break;
173 case 3: D = value;
break;
174 default:
throw new ArgumentOutOfRangeException(
"index",
"Indices for Plane run from 0 to 3, inclusive.");
186 Normal.X *= magnitude;
187 Normal.Y *= magnitude;
188 Normal.Z *= magnitude;
198 return new float[] { Normal.X, Normal.Y, Normal.Z, D };
208 return Collision.PlaneIntersectsPoint(ref
this, ref point);
219 return Collision.RayIntersectsPlane(ref ray, ref
this, out distance);
231 return Collision.RayIntersectsPlane(ref ray, ref
this, out distance);
243 return Collision.RayIntersectsPlane(ref ray, ref
this, out point);
253 return Collision.PlaneIntersectsPlane(ref
this, ref plane);
265 return Collision.PlaneIntersectsPlane(ref
this, ref plane, out line);
277 return Collision.PlaneIntersectsTriangle(ref
this, ref vertex1, ref vertex2, ref vertex3);
287 return Collision.PlaneIntersectsBox(ref
this, ref box);
297 return Collision.PlaneIntersectsSphere(ref
this, ref sphere);
308 result.Normal.X = value.Normal.X * scale;
309 result.Normal.Y = value.Normal.Y * scale;
310 result.Normal.Z = value.Normal.Z * scale;
311 result.D = value.D * scale;
333 result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + (left.D * right.W);
344 return (left.
Normal.
X * right.
X) + (left.Normal.Y * right.Y) + (left.
Normal.
Z * right.
Z) + (left.D * right.W);
355 result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + left.D;
366 return (left.
Normal.
X * right.
X) + (left.Normal.Y * right.Y) + (left.
Normal.
Z * right.
Z) + left.D;
377 result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z);
388 return (left.
Normal.
X * right.
X) + (left.Normal.Y * right.Y) + (left.
Normal.
Z * right.
Z);
398 float magnitude = 1.0f / (float)(Math.Sqrt((plane.Normal.X * plane.Normal.X) + (plane.Normal.Y * plane.Normal.Y) + (plane.Normal.Z * plane.Normal.Z)));
400 result.Normal.X = plane.Normal.X * magnitude;
401 result.Normal.Y = plane.Normal.Y * magnitude;
402 result.Normal.Z = plane.Normal.Z * magnitude;
403 result.D = plane.D * magnitude;
425 float x2 = rotation.X + rotation.X;
426 float y2 = rotation.Y + rotation.Y;
427 float z2 = rotation.Z + rotation.Z;
428 float wx = rotation.W * x2;
429 float wy = rotation.W * y2;
430 float wz = rotation.W * z2;
431 float xx = rotation.X * x2;
432 float xy = rotation.X * y2;
433 float xz = rotation.X * z2;
434 float yy = rotation.Y * y2;
435 float yz = rotation.Y * z2;
436 float zz = rotation.Z * z2;
438 float x = plane.Normal.X;
439 float y = plane.Normal.Y;
440 float z = plane.Normal.Z;
442 result.Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
443 result.Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
444 result.Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
457 float x2 = rotation.X + rotation.X;
458 float y2 = rotation.Y + rotation.Y;
459 float z2 = rotation.Z + rotation.Z;
460 float wx = rotation.W * x2;
461 float wy = rotation.W * y2;
462 float wz = rotation.W * z2;
463 float xx = rotation.X * x2;
464 float xy = rotation.X * y2;
465 float xz = rotation.X * z2;
466 float yy = rotation.Y * y2;
467 float yz = rotation.Y * z2;
468 float zz = rotation.Z * z2;
470 float x = plane.Normal.X;
471 float y = plane.Normal.Y;
472 float z = plane.Normal.Z;
474 result.Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
475 result.Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
476 result.Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
491 throw new ArgumentNullException(
"planes");
493 float x2 = rotation.X + rotation.X;
494 float y2 = rotation.Y + rotation.Y;
495 float z2 = rotation.Z + rotation.Z;
496 float wx = rotation.W * x2;
497 float wy = rotation.W * y2;
498 float wz = rotation.W * z2;
499 float xx = rotation.X * x2;
500 float xy = rotation.X * y2;
501 float xz = rotation.X * z2;
502 float yy = rotation.Y * y2;
503 float yz = rotation.Y * z2;
504 float zz = rotation.Z * z2;
506 for (
int i = 0; i < planes.Length; ++i)
508 float x = planes[i].Normal.X;
509 float y = planes[i].Normal.Y;
510 float z = planes[i].Normal.Z;
516 planes[i].Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
517 planes[i].Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
518 planes[i].Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
530 float x = plane.Normal.X;
531 float y = plane.Normal.Y;
532 float z = plane.Normal.Z;
536 Matrix.Invert(ref transformation, out inverse);
538 result.Normal.X = (((x * inverse.M11) + (y * inverse.
M12)) + (z * inverse.
M13)) + (d * inverse.
M14);
539 result.Normal.Y = (((x * inverse.M21) + (y * inverse.
M22)) + (z * inverse.
M23)) + (d * inverse.
M24);
540 result.Normal.Z = (((x * inverse.M31) + (y * inverse.
M32)) + (z * inverse.
M33)) + (d * inverse.
M34);
541 result.D = (((x * inverse.M41) + (y * inverse.
M42)) + (z * inverse.
M43)) + (d * inverse.
M44);
553 float x = plane.Normal.X;
554 float y = plane.Normal.Y;
555 float z = plane.Normal.Z;
558 transformation.Invert();
559 result.Normal.X = (((x * transformation.M11) + (y * transformation.
M12)) + (z * transformation.
M13)) + (d * transformation.
M14);
560 result.Normal.Y = (((x * transformation.M21) + (y * transformation.
M22)) + (z * transformation.
M23)) + (d * transformation.
M24);
561 result.Normal.Z = (((x * transformation.M31) + (y * transformation.
M32)) + (z * transformation.
M33)) + (d * transformation.
M34);
562 result.D = (((x * transformation.M41) + (y * transformation.
M42)) + (z * transformation.
M43)) + (d * transformation.
M44);
576 throw new ArgumentNullException(
"planes");
579 Matrix.Invert(ref transformation, out inverse);
581 for (
int i = 0; i < planes.Length; ++i)
583 Transform(ref planes[i], ref transformation, out planes[i]);
617 return left.Equals(right);
628 return !left.Equals(right);
639 return string.Format(CultureInfo.CurrentCulture,
"A:{0} B:{1} C:{2} D:{3}", Normal.X, Normal.Y, Normal.Z, D);
651 return string.Format(CultureInfo.CurrentCulture,
"A:{0} B:{1} C:{2} D:{3}", Normal.X.ToString(
format, CultureInfo.CurrentCulture),
652 Normal.Y.ToString(format, CultureInfo.CurrentCulture), Normal.Z.ToString(
format, CultureInfo.CurrentCulture), D.ToString(format, CultureInfo.CurrentCulture));
662 public string ToString(IFormatProvider formatProvider)
664 return string.Format(formatProvider,
"A:{0} B:{1} C:{2} D:{3}", Normal.X, Normal.Y, Normal.Z, D);
677 return string.Format(formatProvider,
"A:{0} B:{1} C:{2} D:{3}", Normal.X.ToString(
format, formatProvider),
678 Normal.Y.ToString(format, formatProvider), Normal.Z.ToString(
format, formatProvider), D.ToString(format, formatProvider));
689 return Normal.GetHashCode() + D.GetHashCode();
701 return Normal == value.Normal && D == value.D;
711 public override bool Equals(
object value)
716 if (value.GetType() != GetType())
719 return Equals((
Plane)value);
728 public static implicit
operator SlimDX.Plane(
Plane value)
730 return new SlimDX.Plane(value.Normal, value.D);
738 public static implicit
operator Plane(SlimDX.Plane value)
740 return new Plane(value.Normal, value.D);
750 public static implicit
operator Microsoft.Xna.Framework.Plane(Plane value)
752 return new Microsoft.Xna.Framework.Plane(value.Normal, value.D);
760 public static implicit
operator Plane(Microsoft.Xna.Framework.Plane value)
762 return new Plane(value.Normal, value.D);
Represents an axis-aligned bounding box in three dimensional space.
static void DotCoordinate(ref Plane left, ref Vector3 right, out float result)
Calculates the dot product of a specified vector and the normal of the plane plus the distance value ...
override int GetHashCode()
Returns a hash code for this instance.
static void Normalize(ref Plane plane, out Plane result)
Changes the coefficients of the normal vector of the plane to make it of unit length.
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
FbxDouble3 operator*(double factor, FbxDouble3 vector)
float Y
The Y component of the vector.
float M22
Value at row 2 column 2 of the matrix.
static void Dot(ref Plane left, ref Vector4 right, out float result)
Calculates the dot product of the specified vector and plane.
static void Multiply(ref Plane value, float scale, out Plane result)
Scales the plane by the given scaling factor.
bool Equals(Plane value)
Determines whether the specified SiliconStudio.Core.Mathematics.Vector4 is equal to this instance...
float D
The distance of the plane along its normal from the origin.
static void DotNormal(ref Plane left, ref Vector3 right, out float result)
Calculates the dot product of the specified vector and the normal of the plane.
override string ToString()
Returns a System.String that represents this instance.
static float DotNormal(Plane left, Vector3 right)
Calculates the dot product of the specified vector and the normal of the plane.
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
float[] ToArray()
Creates an array containing the elements of the plane.
static Plane Multiply(Plane value, float scale)
Scales the plane by the given scaling factor.
float X
The X component of the vector.
Plane(Vector3 point, Vector3 normal)
Initializes a new instance of the T:SiliconStudio.Core.Mathematics.Plane class.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
static float DotCoordinate(Plane left, Vector3 right)
Calculates the dot product of a specified vector and the normal of the plane plus the distance value ...
bool Intersects(ref Ray ray, out float distance)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
void Normalize()
Changes the coefficients of the normal vector of the plane to make it of unit length.
float X
The X component of the vector.
float M13
Value at row 1 column 3 of the matrix.
Represents a three dimensional mathematical vector.
static void Transform(Plane[] planes, ref Matrix transformation)
Transforms an array of normalized planes by a matrix.
float M23
Value at row 2 column 3 of the matrix.
float M42
Value at row 4 column 2 of the matrix.
bool Intersects(ref Ray ray, out Vector3 point)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
Represents a bounding sphere in three dimensional space.
static Plane Transform(Plane plane, Quaternion rotation)
Transforms a normalized plane by a quaternion rotation.
float M44
Value at row 4 column 4 of the matrix.
Represents a plane in three dimensional space.
Represents a four dimensional mathematical vector.
float M14
Value at row 1 column 4 of the matrix.
Represents a four dimensional mathematical quaternion.
static void Transform(ref Plane plane, ref Quaternion rotation, out Plane result)
Transforms a normalized plane by a quaternion rotation.
Vector3 Normal
The normal vector of the plane.
The normal style (One line per item, structured by space).
Represents a three dimensional line based on a point in space and a direction.
string ToString(string format)
Returns a System.String that represents this instance.
Plane(Vector3 value, float d)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
bool Intersects(ref Ray ray)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
PlaneIntersectionType
Describes the result of an intersection with a plane in three dimensions.
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Plane(float a, float b, float c, float d)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
static float Dot(Plane left, Vector4 right)
Calculates the dot product of the specified vector and plane.
float M12
Value at row 1 column 2 of the matrix.
bool Intersects(ref Plane plane, out Ray line)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
static void Transform(Plane[] planes, ref Quaternion rotation)
Transforms an array of normalized planes by a quaternion rotation.
PlaneIntersectionType Intersects(ref BoundingBox box)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingBox.
static Plane Normalize(Plane plane)
Changes the coefficients of the normal vector of the plane to make it of unit length.
Plane(float value)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
static Plane Transform(Plane plane, Matrix transformation)
Transforms a normalized plane by a matrix.
float M34
Value at row 3 column 4 of the matrix.
bool Intersects(ref Plane plane)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
float M33
Value at row 3 column 3 of the matrix.
float M43
Value at row 4 column 3 of the matrix.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
float Z
The Z component of the vector.
static void Transform(ref Plane plane, ref Matrix transformation, out Plane result)
Transforms a normalized plane by a matrix.
float Z
The Z component of the vector.
PlaneIntersectionType Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Determines if there is an intersection between the current object and a triangle. ...
PlaneIntersectionType Intersects(ref BoundingSphere sphere)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingSphere.
PlaneIntersectionType Intersects(ref Vector3 point)
Determines if there is an intersection between the current object and a point.
float M32
Value at row 3 column 2 of the matrix.
Plane(Vector3 point1, Vector3 point2, Vector3 point3)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t size_t z
Plane(float[] values)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
float M24
Value at row 2 column 4 of the matrix.
Represents a 4x4 mathematical matrix.