internal static HomogenMatrix3 ConvertMatrixToHomogenMatrix3(double[,] matrix) { HomogenMatrix3 m = new HomogenMatrix3(); if (matrix != null) { try { var HomogenMatrix3Type = typeof(HomogenMatrix3); var HomogenMatrixLine3Type = typeof(HomogenMatrixLine3); for (int i = 0; i < 3 && i < matrix.GetLength(0); i++) { var lineField = HomogenMatrix3Type.GetField("Line" + (i + 1)); if (lineField != null) { HomogenMatrixLine3 line = lineField.GetValue(m) as HomogenMatrixLine3; for (int j = 0; j < 3 && j < matrix.GetLength(1); j++) { var colField = HomogenMatrixLine3Type.GetField("Column" + (j + 1)); if (colField != null) { colField.SetValue(line, matrix[i, j]); } } } } } catch (System.Exception) { } } return m; }
/// <summary> /// Applies an affine transformation, given by the transformation matrix to a given rectangle. /// The resulting bounding box might be larger than the original rectangle if the object is rotated. /// </summary> /// <param name="rect">The original bounding rectangle.</param> /// <param name="homogenMatrix">The transformation matrix.</param> /// <returns>The bounding box of the transformed rectangle.</returns> internal static Rectangle TransformBoundingBox(Rectangle rect, HomogenMatrix3 homogenMatrix) { // transform all corners // p1 p2 // ┌────────┐ // │ │ // └────────┘ // p3 p4 Point p1 = Transform(new Point(rect.X, rect.Y), homogenMatrix); Point p2 = Transform(new Point(rect.X + rect.Width, rect.Y), homogenMatrix); Point p3 = Transform(new Point(rect.X + rect.Width, rect.Y + rect.Height), homogenMatrix); Point p4 = Transform(new Point(rect.X, rect.Y + rect.Height), homogenMatrix); int minX = Math.Min(Math.Min(Math.Min(p1.X, p2.X), p3.X), p4.X); int maxX = Math.Max(Math.Max(Math.Max(p1.X, p2.X), p3.X), p4.X); int minY = Math.Min(Math.Min(Math.Min(p1.Y, p2.Y), p3.Y), p4.Y); int maxY = Math.Max(Math.Max(Math.Max(p1.Y, p2.Y), p3.Y), p4.Y); return new Rectangle(minX, minY, maxX - minX, maxY - minY); }
internal static double[,] ConvertHomogenMatrix3ToMatrix(HomogenMatrix3 transformProp) { // initialization of a two dimensional array // matrix [m,n] = array[rows,cols] or array[y,x] // concatenation of row initializations // build the standard basis for a transformation matrix // 1 0 0 // 0 1 0 // 0 0 1 double[,] matrix = new double[,] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }; if (transformProp != null) { if (transformProp.Line1 != null) { matrix[0, 0] = transformProp.Line1.Column1; matrix[0, 1] = transformProp.Line1.Column2; matrix[0, 2] = transformProp.Line1.Column3; } if (transformProp.Line2 != null) { matrix[1, 0] = transformProp.Line2.Column1; matrix[1, 1] = transformProp.Line2.Column2; matrix[1, 2] = transformProp.Line2.Column3; } if (transformProp.Line3 != null) { matrix[2, 0] = transformProp.Line3.Column1; matrix[2, 1] = transformProp.Line3.Column2; matrix[2, 2] = transformProp.Line3.Column3; } } return matrix; }
/// <summary> /// Applies an affine transformation, given by the transformation matrix to a given coordinate /// </summary> /// <param name="pos">The original coordinate.</param> /// <param name="homogenMatrix">The transformation matrix.</param> /// <returns>The transformed matrix.</returns> internal static Point Transform(Point pos, HomogenMatrix3 homogenMatrix) { // x' = a11 * x + a12 * y + a13 // y' = a21 * x + a22 * y + a23 // a31 a32 a33 // 3rd line is only to allow matrix inversion, has no additional information, should always be [0, 0, 1] return new Point( (int)(homogenMatrix.Line1.Column1 * pos.X + homogenMatrix.Line1.Column2 * pos.Y + homogenMatrix.Line1.Column3), (int)(homogenMatrix.Line2.Column1 * pos.X + homogenMatrix.Line2.Column2 * pos.Y + homogenMatrix.Line2.Column3) ); }