/// <summary> /// Multiplies two matrices and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="b">A <see cref="Matrix4D"/> instance.</param> /// <param name="result">A <see cref="Matrix4D"/> instance to hold the result.</param> public static void Multiply(Matrix4D a, Matrix4D b, ref Matrix4D result) { result.M11 = a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41; result.M12 = a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42; result.M13 = a.M11 * b.M13 + a.M12 * b.M23 + a.M13 * b.M33 + a.M14 * b.M43; result.M14 = a.M11 * b.M14 + a.M12 * b.M24 + a.M13 * b.M34 + a.M14 * b.M44; result.M21 = a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31 + a.M24 * b.M41; result.M22 = a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32 + a.M24 * b.M42; result.M23 = a.M21 * b.M13 + a.M22 * b.M23 + a.M23 * b.M33 + a.M24 * b.M43; result.M24 = a.M21 * b.M14 + a.M22 * b.M24 + a.M23 * b.M34 + a.M24 * b.M44; result.M31 = a.M31 * b.M11 + a.M32 * b.M21 + a.M33 * b.M31 + a.M34 * b.M41; result.M32 = a.M31 * b.M12 + a.M32 * b.M22 + a.M33 * b.M32 + a.M34 * b.M42; result.M33 = a.M31 * b.M13 + a.M32 * b.M23 + a.M33 * b.M33 + a.M34 * b.M43; result.M34 = a.M31 * b.M14 + a.M32 * b.M24 + a.M33 * b.M34 + a.M34 * b.M44; result.M41 = a.M41 * b.M11 + a.M42 * b.M21 + a.M43 * b.M31 + a.M44 * b.M41; result.M42 = a.M41 * b.M12 + a.M42 * b.M22 + a.M43 * b.M32 + a.M44 * b.M42; result.M43 = a.M41 * b.M13 + a.M42 * b.M23 + a.M43 * b.M33 + a.M44 * b.M43; result.M44 = a.M41 * b.M14 + a.M42 * b.M24 + a.M43 * b.M34 + a.M44 * b.M44; }
/// <summary> /// Subtracts a scalar from a matrix and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="s">A scalar.</param> /// <param name="result">A <see cref="Matrix4D"/> instance to hold the result.</param> public static void Subtract(Matrix4D a, double s, ref Matrix4D result) { result.M11 = a.M11 - s; result.M12 = a.M12 - s; result.M13 = a.M13 - s; result.M14 = a.M14 - s; result.M21 = a.M21 - s; result.M22 = a.M22 - s; result.M23 = a.M23 - s; result.M24 = a.M24 - s; result.M31 = a.M31 - s; result.M32 = a.M32 - s; result.M33 = a.M33 - s; result.M34 = a.M34 - s; result.M41 = a.M41 - s; result.M42 = a.M42 - s; result.M43 = a.M43 - s; result.M44 = a.M44 - s; }
/// <summary> /// Multiplies two matrices. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="b">A <see cref="Matrix4D"/> instance.</param> /// <returns>A new <see cref="Matrix4D"/> instance containing the result.</returns> public static Matrix4D Multiply(Matrix4D a, Matrix4D b) { return new Matrix4D( a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41, a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42, a.M11 * b.M13 + a.M12 * b.M23 + a.M13 * b.M33 + a.M14 * b.M43, a.M11 * b.M14 + a.M12 * b.M24 + a.M13 * b.M34 + a.M14 * b.M44, a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31 + a.M24 * b.M41, a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32 + a.M24 * b.M42, a.M21 * b.M13 + a.M22 * b.M23 + a.M23 * b.M33 + a.M24 * b.M43, a.M21 * b.M14 + a.M22 * b.M24 + a.M23 * b.M34 + a.M24 * b.M44, a.M31 * b.M11 + a.M32 * b.M21 + a.M33 * b.M31 + a.M34 * b.M41, a.M31 * b.M12 + a.M32 * b.M22 + a.M33 * b.M32 + a.M34 * b.M42, a.M31 * b.M13 + a.M32 * b.M23 + a.M33 * b.M33 + a.M34 * b.M43, a.M31 * b.M14 + a.M32 * b.M24 + a.M33 * b.M34 + a.M34 * b.M44, a.M41 * b.M11 + a.M42 * b.M21 + a.M43 * b.M31 + a.M44 * b.M41, a.M41 * b.M12 + a.M42 * b.M22 + a.M43 * b.M32 + a.M44 * b.M42, a.M41 * b.M13 + a.M42 * b.M23 + a.M43 * b.M33 + a.M44 * b.M43, a.M41 * b.M14 + a.M42 * b.M24 + a.M43 * b.M34 + a.M44 * b.M44 ); }
/// <summary> /// Transposes a matrix. /// </summary> /// <param name="m">A <see cref="Matrix4D"/> instance.</param> /// <returns>A new <see cref="Matrix4D"/> instance containing the transposed matrix.</returns> public static Matrix4D Transpose(Matrix4D m) { Matrix4D t = new Matrix4D(m); t.Transpose(); return t; }
static void Main(string[] args) { Child child = Factory.GimmeAChild(); Parent parent = Factory.GimmeAParent(); Parent parent2 = Factory.GimmeAChildAsAParent(); child.DoEvenMore(); child.DoSomething(3, 4); parent.DoSomething(2, 3); parent2.DoSomething(1, 2); Child child2 = (Child)parent2; // SIZE howbig = SIZE.MEDIUM; // parent2.MethodWithRefSize(ref howbig); Vector3D v3 = new Vector3D(11, 22, 33); Vector4D v4 = new Vector4D(1, 2, 3, 4); Matrix4D mtx = new Matrix4D(11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44); int res; // Handmade Matrix and Vector wrapping test // Note: This tests the mapping of C++ Matrix and Vector types // to exsiting C# Matrix and Vector types (and not wrapper classes) res = HandmadeWrappers.HandMadeVectorTaker(ref v4); //res = HandmadeWrappers.HandMadeMatrixTakerWrapper(ref mtx); Vector4D vRes = HandmadeWrappers.HandMadeVectorReturner(); //Matrix4D mRes = HandmadeWrappers.HandMadeMatrixReturnerWrapper(); Vector4D vRes2 = HandmadeWrappers.HandMadeVectorPtrReturnerWrapper(); // Swig generated Matrix and Vector wrapping test // Note: This tests the mapping of C++ Matrix and Vector types // to exsiting C# Matrix and Vector types (and not wrapper classes generated by Swig). // The mapping of the types is done by Swig, though VectorConsumer vc = new VectorConsumer(); //Matrix4D mRes2 = vc.GimmeSomeMatrix(); res = vc.VectorTakerPtr3(ref v3); res = vc.VectorTakerRef3(ref v3); res = vc.VectorTakerVal3(v3); Vector3D vRet = vc.GimmeSomeVector(); Vector3D vOld = vc.VV; vc.VV = vRet; vOld = vc.VV; res = vc.VectorTakerPtr4(ref v4); res = vc.VectorTakerRef4(ref v4); res = vc.VectorTakerVal4(v4); //res = vc.MatrixTakerPtr(ref mtx); //res = vc.MatrixTakerRef(ref mtx); //res = vc.MatrixTakerVal(mtx); MyVectorConsumer myVc = new MyVectorConsumer(); VectorConsumerCaller.CallVectorConsumer(myVc); AParamType param = null; RefRefTest.ParameterTaker(ref param); int i = 8; }
/// <summary> /// Transforms a given vector by a matrix applying the positional matrix components but omitting the projection components. /// </summary> /// <param name="matrix">A <see cref="Matrix4D"/> instance.</param> /// <param name="vector">A <see cref="Vector3D"/> instance.</param> /// <returns>A new <see cref="Vector3D"/> instance containing the result.</returns> public static Vector3D Transform(Matrix4D matrix, Vector3D vector) { return new Vector3D( (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14, (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24, (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34); }
/// <summary> /// Transforms a given 3D vector by a matrix using perspective division. /// </summary> /// <remarks> /// Before the matrix multiplication the 3D vector is extended to 4D by setting its W component to 1. /// After the matrix multiplication the resulting 4D vector is transformed to 3D by dividing X, Y, and Z by W. /// (perspective division). /// </remarks> /// <param name="matrix">A <see cref="Matrix4D"/> instance.</param> /// <param name="vector">A <see cref="Vector3D"/> instance.</param> /// <returns>A new <see cref="Vector3D"/> instance containing the result.</returns> public static Vector3D TransformPD(Matrix4D matrix, Vector3D vector) { double w = (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + matrix.M44; return new Vector3D( ((matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14) / w, ((matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24) / w, ((matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34) / w); }
/// <summary> /// Adds a matrix and a scalar. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="s">A scalar.</param> /// <returns>A new <see cref="Matrix4D"/> instance containing the sum.</returns> public static Matrix4D Add(Matrix4D a, double s) { return new Matrix4D( a.M11 + s, a.M12 + s, a.M13 + s, a.M14 + s, a.M21 + s, a.M22 + s, a.M23 + s, a.M24 + s, a.M31 + s, a.M32 + s, a.M33 + s, a.M34 + s, a.M41 + s, a.M42 + s, a.M43 + s, a.M44 + s ); }
/// <summary> /// Adds two matrices and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="b">A <see cref="Matrix4D"/> instance.</param> /// <param name="result">A <see cref="Matrix4D"/> instance to hold the result.</param> public static void Add(Matrix4D a, Matrix4D b, ref Matrix4D result) { result.M11 = a.M11 + b.M11; result.M12 = a.M12 + b.M12; result.M13 = a.M13 + b.M13; result.M14 = a.M14 + b.M14; result.M21 = a.M21 + b.M21; result.M22 = a.M22 + b.M22; result.M23 = a.M23 + b.M23; result.M24 = a.M24 + b.M24; result.M31 = a.M31 + b.M31; result.M32 = a.M32 + b.M32; result.M33 = a.M33 + b.M33; result.M34 = a.M34 + b.M34; result.M41 = a.M41 + b.M41; result.M42 = a.M42 + b.M42; result.M43 = a.M43 + b.M43; result.M44 = a.M44 + b.M44; }
/// <summary> /// Initializes a new instance of the <see cref="Matrix4D"/> class using a given matrix. /// </summary> public Matrix4D(Matrix4D m) { _m11 = m.M11; _m12 = m.M12; _m13 = m.M13; _m14 = m.M14; _m21 = m.M21; _m22 = m.M22; _m23 = m.M23; _m24 = m.M24; _m31 = m.M31; _m32 = m.M32; _m33 = m.M33; _m34 = m.M34; _m41 = m.M41; _m42 = m.M42; _m43 = m.M43; _m44 = m.M44; }
/// <summary> /// Adds two matrices. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="b">A <see cref="Matrix4D"/> instance.</param> /// <returns>A new <see cref="Matrix4D"/> instance containing the sum.</returns> public static Matrix4D Add(Matrix4D a, Matrix4D b) { return new Matrix4D( a.M11 + b.M11, a.M12 + b.M12, a.M13 + b.M13, a.M14 + b.M14, a.M21 + b.M21, a.M22 + b.M22, a.M23 + b.M23, a.M24 + b.M24, a.M31 + b.M31, a.M32 + b.M32, a.M33 + b.M33, a.M34 + b.M34, a.M41 + b.M41, a.M42 + b.M42, a.M43 + b.M43, a.M44 + b.M44 ); }
/// <summary> /// Transform the vectors in the given array and put the result in another array. /// </summary> /// <param name="vectors">An array of vectors to transform.</param> /// <param name="transformation">The transformation.</param> /// <param name="result">An array of vectors to put the transformation results in (should be empty).</param> public static void TransformArray(Vector3DArrayList vectors, Matrix4D transformation, Vector3DArrayList result) { for (int i = 0; i < vectors.Count; i++) { result.Add(Matrix4D.Transform(transformation, vectors[i])); } }
/// <summary> /// Transform the vectors in the given array. /// </summary> /// <param name="vectors">An array of vectors to transform.</param> /// <param name="transformation">The transformation.</param> /// <remarks> /// This method changes the vector values in the <paramref name="vectors"/> array. /// </remarks> public static void TransformArray(Vector3DArrayList vectors, Matrix4D transformation) { for (int i = 0; i < vectors.Count; i++) { vectors[i] = Matrix4D.Transform(transformation, vectors[i]); } }
/// <summary> /// Transform the vectors in the given array. /// </summary> /// <param name="vectors">An array of vectors to transform.</param> /// <param name="transformation">The transformation.</param> /// <remarks> /// This method changes the vector values in the <paramref name="vectors"/> array. /// </remarks> public static void TransformArray(Vector4DArrayList vectors, Matrix4D transformation) { for (int i = 0; i < vectors.Count; i++) { vectors[i] = transformation * vectors[i]; } }
/// <summary> /// Transforms a given vector by a matrix. /// </summary> /// <param name="matrix">A <see cref="Matrix4D"/> instance.</param> /// <param name="vector">A <see cref="Vector4D"/> instance.</param> /// <returns>A new <see cref="Vector4D"/> instance containing the result.</returns> public static Vector4D Transform(Matrix4D matrix, Vector4D vector) { return new Vector4D( (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + (matrix.M14 * vector.W), (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + (matrix.M24 * vector.W), (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + (matrix.M34 * vector.W), (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + (matrix.M44 * vector.W)); }
/// <summary> /// Adds a matrix and a scalar and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="s">A scalar.</param> /// <param name="result">A <see cref="Matrix4D"/> instance to hold the result.</param> public static void Add(Matrix4D a, double s, ref Matrix4D result) { result.M11 = a.M11 + s; result.M12 = a.M12 + s; result.M13 = a.M13 + s; result.M14 = a.M14 + s; result.M21 = a.M21 + s; result.M22 = a.M22 + s; result.M23 = a.M23 + s; result.M24 = a.M24 + s; result.M31 = a.M31 + s; result.M32 = a.M32 + s; result.M33 = a.M33 + s; result.M34 = a.M34 + s; result.M41 = a.M41 + s; result.M42 = a.M42 + s; result.M43 = a.M43 + s; result.M44 = a.M44 + s; }
/// <summary> /// Transforms a given vector by a matrix and put the result in a vector. /// </summary> /// <param name="matrix">A <see cref="Matrix4D"/> instance.</param> /// <param name="vector">A <see cref="Vector4D"/> instance.</param> /// <param name="result">A <see cref="Vector4D"/> instance to hold the result.</param> public static void Transform(Matrix4D matrix, Vector4D vector, ref Vector4D result) { result.X = (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + (matrix.M14 * vector.W); result.Y = (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + (matrix.M24 * vector.W); result.Z = (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + (matrix.M34 * vector.W); result.W = (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + (matrix.M44 * vector.W); }
/// <summary> /// Subtracts a matrix from a matrix. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance to subtract from.</param> /// <param name="b">A <see cref="Matrix4D"/> instance to subtract.</param> /// <returns>A new <see cref="Matrix4D"/> instance containing the difference.</returns> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> public static Matrix4D Subtract(Matrix4D a, Matrix4D b) { return new Matrix4D( a.M11 - b.M11, a.M12 - b.M12, a.M13 - b.M13, a.M14 - b.M14, a.M21 - b.M21, a.M22 - b.M22, a.M23 - b.M23, a.M24 - b.M24, a.M31 - b.M31, a.M32 - b.M32, a.M33 - b.M33, a.M34 - b.M34, a.M41 - b.M41, a.M42 - b.M42, a.M43 - b.M43, a.M44 - b.M44 ); }
/// <summary> /// Transforms a given vector by a matrix applying the positional matrix components but omitting the projection components and put the result in a vector. /// </summary> /// <param name="matrix">A <see cref="Matrix4D"/> instance.</param> /// <param name="vector">A <see cref="Vector3D"/> instance.</param> /// <param name="result">A <see cref="Vector3D"/> instance to hold the result.</param> public static void Transform(Matrix4D matrix, Vector3D vector, ref Vector3D result) { result.X = (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14; result.Y = (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24; result.Z = (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34; }
/// <summary> /// Subtracts a scalar from a matrix. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance.</param> /// <param name="s">A <see cref="Matrix4D"/> instance.</param> /// <returns>A new <see cref="Matrix4D"/> instance containing the difference.</returns> public static Matrix4D Subtract(Matrix4D a, double s) { return new Matrix4D( a.M11 - s, a.M12 - s, a.M13 - s, a.M14 - s, a.M21 - s, a.M22 - s, a.M23 - s, a.M24 - s, a.M31 - s, a.M32 - s, a.M33 - s, a.M34 - s, a.M41 - s, a.M42 - s, a.M43 - s, a.M44 - s ); }
/// <summary> /// Transforms a given vector by a matrix using perspective division and put the result in a vector. /// </summary> /// <remarks> /// Before the matrix multiplication the 3D vector is extended to 4D by setting its W component to 1. /// After the matrix multiplication the resulting 4D vector is transformed to 3D by dividing X, Y, and Z by W. /// (perspective division). /// </remarks> /// <param name="matrix">A <see cref="Matrix4D"/> instance.</param> /// <param name="vector">A <see cref="Vector3D"/> instance.</param> /// <param name="result">A <see cref="Vector3D"/> instance to hold the result.</param> public static void TransformPD(Matrix4D matrix, Vector3D vector, ref Vector3D result) { double w = (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + matrix.M44; result.X = ((matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14) / w; result.Y = ((matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24) / w; result.Z = ((matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34) / w; }
/// <summary> /// Subtracts a matrix from a matrix and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4D"/> instance to subtract from.</param> /// <param name="b">A <see cref="Matrix4D"/> instance to subtract.</param> /// <param name="result">A <see cref="Matrix4D"/> instance to hold the result.</param> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> public static void Subtract(Matrix4D a, Matrix4D b, ref Matrix4D result) { result.M11 = a.M11 - b.M11; result.M12 = a.M12 - b.M12; result.M13 = a.M13 - b.M13; result.M14 = a.M14 - b.M14; result.M21 = a.M21 - b.M21; result.M22 = a.M22 - b.M22; result.M23 = a.M23 - b.M23; result.M24 = a.M24 - b.M24; result.M31 = a.M31 - b.M31; result.M32 = a.M32 - b.M32; result.M33 = a.M33 - b.M33; result.M34 = a.M34 - b.M34; result.M41 = a.M41 - b.M41; result.M42 = a.M42 - b.M42; result.M43 = a.M43 - b.M43; result.M44 = a.M44 - b.M44; }
/// <summary> /// Calculates the adjoint of the matrix. /// </summary> /// <returns>A <see cref="Matrix4D"/> instance containing the adjoint of the matrix.</returns> public Matrix4D Adjoint() { Matrix4D result = new Matrix4D(); for (int row = 0; row < 4; row++) { for (int col = 0; col < 4; col++) { if (((col+row) % 2) == 0) result[row, col] = Minor(col, row).Determinant(); else result[row, col] = -Minor(col, row).Determinant(); } } return result; }
//TODO: deleted 'override' public bool MoveHandle(BaseObject op, BaseObject undo, ref Matrix4D tm, int hit_id, QUALIFIER qualifier) { BaseContainer src = undo.GetDataInstance(); BaseContainer dst = op.GetDataInstance(); Vector3D handle_dir = new Vector3D(1.0,0.0,0.0); handle_dir = SwapPoint(handle_dir,src.GetLong(C4dApi.PRIM_PLANE)); double val = Vector3D.Dot(tm.Offset, handle_dir); dst.SetReal(CIRCLEOBJECT_RAD, MathFunctions.Saturate(src.GetReal(CIRCLEOBJECT_RAD)+val, 0.0, C4dApi.MAXRANGE)); return true; }