private static double[][] commonTools(Recatangle origion, List <Matrix> ctms, int startIndex, int endIndex) { if (startIndex == 0) { String funName = (new StackTrace().GetFrame(1).GetMethod()).Name; Console.WriteLine("**********" + funName + "************"); } double ALXO = origion.P.X; double ALYO = origion.P.Y; double ARXO = origion.P.X + origion.Width; double ARYO = origion.P.Y - origion.Height; if (startIndex == 0) { Console.WriteLine("ALXO={0},ALYO={1},ARXO={2},ARYO={3}", ALXO, ALYO, ARXO, ARYO); } double[][] points = new double[][] { new double[] { ALXO, ALYO, 1 }, //左上角的点 new double[] { ARXO, ARYO, 1 }, //右下角的点 }; double[][] results = new double[][] { new double[] { 0, 0, 1 }, //左上角的点 new double[] { 0, 0, 1 }, //右下角的点 }; Matrix ctm = null; double[][] _ctm = null; for (int i = startIndex; i <= endIndex; i++) { ctm = ctms[i]; _ctm = ctm.Points; Console.WriteLine("origion matrix"); Matrix.PrintMatrix(points); Console.WriteLine(); Console.WriteLine("CTM matrix"); Matrix.PrintMatrix(_ctm); Console.WriteLine(); results = Matrix.MatrixMult(points, _ctm); Console.WriteLine("result matrix"); Matrix.PrintMatrix(results); Console.WriteLine(); Console.WriteLine("********************"); points = results; //update the matrix } return(points); }
/// <summary> /// 此方法用来判断源矩形区域中是否包含目标矩形区域,即是否可以剪切 /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <returns></returns> public static bool canClip(Recatangle source, Recatangle target) { bool canClip = false; //首先判断两个矩形区域是否重合 if (isOverlap(source, target) && isIdentical(source, target))//首先判断两个矩形是否完全重合 { Console.WriteLine("两个图形完全重合了!!!可以剪切"); canClip = true; return(canClip); } //第1个矩形左上角的坐标 double ALX = source.P.X; double ALY = source.P.Y; //第1个矩形右下角的坐标 double ARX = ALX + source.Width; double ARY = ALY - source.Height; //第2个矩形左上角的坐标 double BLX = target.P.X; double BLY = target.P.Y; //第2个矩形右下角的坐标 double BRX = BLX + target.Width; double BRY = BLY - target.Height; double min1 = getMax(getMin(ALX, ARX), getMin(BLX, BRX)); double min2 = getMax(getMin(ALY, ARY), getMin(BLY, BRY)); double max1 = getMin(getMax(ALX, ARX), getMax(BLX, BRX)); double max2 = getMin(getMax(ALY, ARY), getMax(BLY, BRY)); Console.WriteLine("ALX={0},ALY={1},ARX={2},ARY={3}", ALX, ALY, ARX, ARY); Console.WriteLine("BLX={0},BLY={1},BRX={2},BRY={3}", BLX, BLY, BRX, BRY); Console.WriteLine("min1={0},min2={1},max1={2},max2={3}", min1, min2, max1, max2); Console.WriteLine("max1 - min1={0},max2 - min2={1},(max1 - min1) * (max2 - min2)={2}", max1 - min1, max2 - min2, (max1 - min1) * (max2 - min2)); if (min1 < max1 && max2 > min2) {//说明两个矩形相交 canClip = false; Console.WriteLine("两个矩形有交集!!!"); } else { //说明这两个矩形有相互包含的情况 if (source.Width * source.Height != target.Width * target.Height && (max1 - min1) * (max2 - min2) > 0) //说明recA包含recB { canClip = true; Console.WriteLine("两个矩形相互包含。"); } else { canClip = false; Console.WriteLine("两个矩形相离"); Console.WriteLine("出现异常,请检查输入参数是否正确。"); } } return(canClip); }
//public static void fun() { // TestMatrix(); //} /// <summary> /// Test the matrix values. /// </summary> private static void TestMatrix() { Console.WriteLine("hello.........."); Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); //获得当前方法名 Console.WriteLine((new StackTrace().GetFrame(1).GetMethod()).Name); //获得调用当前方法的父方法名 Recatangle target = new Recatangle(new Point(0, 0), 0, 0); target = ConversionCoordinates.Image2UserCoordinateSystem(target); Console.WriteLine(target); Console.ReadKey(); }
public static double[][] ConvertRect2Arr(Recatangle rect) { double rlx = rect.P.X; double rly = rect.P.Y; double rrx = rect.P.X + rect.Width; double rry = rect.P.Y - rect.Height; double[][] result = new double[][] { new double[] { rlx, rly, 1 }, new double[] { rrx, rry, 1 } }; return(result); }
public static bool isIdentical(Recatangle rc1, Recatangle rc2) { if (rc1.P.X == rc2.P.X && rc2.P.Y == rc1.P.Y && rc1.Height == rc2.Height && rc2.Width == rc1.Width ) { return(true);//isSame } else { return(false);//no overlap } }
/// <summary> /// 判断两个轴对齐的矩形是否重叠 /// </summary> /// <param name="rc1">第1个矩阵的位置</param> /// <param name="rc2">第2个矩阵的位置</param> /// <returns>两个矩阵是否重叠(边沿重叠,也认为是重叠)</returns> public static bool isOverlap(Recatangle rc1, Recatangle rc2) { if (rc1.P.X + rc1.Width > rc2.P.X && rc2.P.X + rc2.Width > rc1.P.X && rc1.P.Y + rc1.Height > rc2.P.Y && rc2.P.Y + rc2.Height > rc1.P.Y ) { return(true);//overlap } else { return(false);//no overlap } }
/// <summary> /// this method contains the value of User坐标映射 /// </summary> /// <param name="origion">原始矩形区域</param> /// <param name="ctms">转换的矩阵</param> /// <param name="target">需要返回的矩形区域</param> /// <returns></returns> private static Recatangle Plot2UserCoordinateSystem(Recatangle origion, List <Matrix> ctms, Recatangle target) { double[][] points = new double[][] { new double[] { 0, 0, 1 }, //左上角的点 new double[] { 0, 0, 1 }, //右下角的点 }; points = commonTools(origion, ctms, 0, 1); #region clip operation /* * 中间截取一段执行Clip操作 */ Recatangle source = new Recatangle(new Point(points[0][0], points[0][1]), points[1][0] - points[0][0], points[0][1] - points[1][1]); target = new Recatangle(new Point(0, 0), 612, 792); bool canClipse = commonUtils.canClip(source, target); //有交集 canClipse = true; //Clip操作执行完成 #endregion points = commonTools(Matrix.ConvertArr2Rect(points), ctms, 2, 2); //将计算出来的矩形区域的主对角线的两点分别给相应的(BLX,BLY)(BRX,BRY)赋值,并输出结果矩形 double BLX = points[0][0]; double BLY = points[0][1]; double BRX = points[1][0]; double BRY = points[1][1]; target.P.X = BLX; target.P.Y = BLY; target.Width = BRX - BLX; target.Height = BLY - BRY; return(target); }
/// <summary> /// Image -> patternSystem /// </summary> /// <param name="origion">原始矩形区域</param> /// <param name="ctms">转换矩阵</param> /// <param name="target">要返回的目标矩形区域</param> /// <returns></returns> private static Recatangle Image2patternSystem(Recatangle origion, List <Matrix> ctms, Recatangle target) { double[][] points = new double[][] { new double[] { 0, 0, 1 }, //左上角的点 new double[] { 0, 0, 1 }, //右下角的点 }; points = commonTools(origion, ctms, 0, 0); #region clip operation /* * 中间截取一段执行Clip操作,截取可见区域 */ Recatangle source = new Recatangle(new Point(points[0][0], points[0][1]), points[1][0] - points[0][0], points[0][1] - points[1][1]); target = new Recatangle(new Point(0, 0), 404.5725, 213.4275); bool canClipse = commonUtils.canClip(source, target); //判断是否可以截取该区域 points[1][1] = -213.4275; //更新可见区域 canClipse = true; //放行 //Clip操作执行完成 #endregion if (canClipse) { points = commonTools(Matrix.ConvertArr2Rect(points), ctms, 1, ctms.Count - 1); } //将计算出来的矩形区域的主对角线的两点分别给相应的(BLX,BLY)(BRX,BRY)赋值,并输出结果矩形 double BLX = points[0][0]; double BLY = points[0][1]; double BRX = points[1][0]; double BRY = points[1][1]; target.P.X = BLX; target.P.Y = BLY; target.Width = BRX - BLX; target.Height = BLY - BRY; return(target); }
/// <summary> /// Pattern System ->User coordinate System /// </summary> /// <param name="origion">原始矩形区域</param> /// <param name="ctms">转换矩阵</param> /// <param name="target">返回的目标矩形</param> /// <returns></returns> private static Recatangle Pattern2UserCoordinateSystem(Recatangle origion, List <Matrix> ctms, Recatangle target) { double[][] points = new double[][] { new double[] { 0, 0, 1 }, //左上角的点 new double[] { 0, 0, 1 }, //右下角的点 }; points = commonTools(origion, ctms, 0, 0); //将计算出来的矩形区域的主对角线的两点分别给相应的(BLX,BLY)(BRX,BRY)赋值,并输出结果矩形 double BLX = points[0][0]; double BLY = points[0][1]; double BRX = points[1][0]; double BRY = points[1][1]; target = new Recatangle(new Point(0, 0), 0, 0); target.P.X = BLX; target.P.Y = BLY; target.Width = BRX - BLX; target.Height = BLY - BRY; return(target); }
/// <summary> /// /// </summary> /// <param name="target">原矩形区域</param> /// <returns></returns> public static Recatangle Image2UserCoordinateSystem(Recatangle target) { List <Matrix> ctms = new List <Matrix>(); Recatangle rect_10 = new Recatangle(new Point(0, 1), 1, 1); double[][] p1 = new double[][] { new double[] { 404.5714, 0, 0 }, new double[] { 0, 213.4286, 0 }, new double[] { 0, -213.4286, 1 } }; double[][] p2 = new double[][] { new double[] { 0.00185381, 0, 0 }, new double[] { 0, 0.00351407, 0 }, new double[] { 0, 0, 1 } }; double[][] p21 = new double[][] { new double[] { 1, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, 0, 1 } }; double[][] p3 = new double[][] { new double[] { 1, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, -257.785, 1 } }; double[][] p4 = new double[][] { new double[] { 1, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, 257.785, 1 } }; Matrix m1 = new Matrix(p1); Matrix m2 = new Matrix(p2); Matrix m21 = new Matrix(p21); Matrix m3 = new Matrix(p3); Matrix m4 = new Matrix(p4); ctms.Add(m1); ctms.Add(m2); ctms.Add(m21); ctms.Add(m3); ctms.Add(m4); Console.WriteLine("###########Image2PatternCoordinateSystem##############"); //本类中调用,得到的ImageRect结果是没有映射到用户坐标空间中,只是映射到了Pattern坐标空间中 Recatangle imageRect = ConversionCoordinates.Image2patternSystem(rect_10, ctms, target); Console.WriteLine("###########Image Transform finished!!!##############"); Recatangle BBOX = new Recatangle(new Point(-5, 5), 387.19, 257.785); List <Matrix> ctms1 = new List <Matrix>(); ctms1.Add(new Matrix(new double[][] { new double[] { 4 / 3.0000, 0, 0 }, new double[] { 0, 4 / 3.0000, 0 }, new double[] { 0, 792, 1 } })); Recatangle imageRect1 = null; //在本类中调用 imageRect1 = ConversionCoordinates.Pattern2UserCoordinateSystem(imageRect, ctms1, target); Console.WriteLine("================" + imageRect1); Console.WriteLine("imageRect"); Matrix.PrintMatrix(Matrix.ConvertRect2Arr(imageRect1)); BBOX = ConversionCoordinates.Pattern2UserCoordinateSystem(BBOX, ctms1, target); Console.WriteLine(BBOX); Console.WriteLine("###########Pattern Transform finished!!!##############"); Console.WriteLine("imageRect"); Console.WriteLine("<<<<<<<<<<<<<<<<" + imageRect1); Matrix.PrintMatrix(Matrix.ConvertRect2Arr(imageRect1)); Console.WriteLine("BBOX"); Matrix.PrintMatrix(Matrix.ConvertRect2Arr(BBOX)); Console.WriteLine("相交面积"); Console.WriteLine(commonUtils.getSequare(BBOX, imageRect1)); Console.WriteLine(); return(imageRect); }
public static Recatangle ConvertArr2Rect(double[][] points) { Recatangle result = new Recatangle(new Point(points[0][0], points[0][1]), new Point(points[1][0], points[1][1])); return(result); }
/// <summary> /// Pattern System ->User coordinate System /// </summary> /// <param name="origion">原始矩形区域</param> /// <param name="ctms">转换矩阵</param> /// <param name="target">返回的目标矩形</param> /// <returns></returns> private static Recatangle Pattern2UserCoordinateSystem(Recatangle origion, List <Matrix> ctms, Recatangle target) { double[][] points = new double[][] { new double[] { 0, 0, 1 }, //左上角的点 new double[] { 0, 0, 1 }, //右下角的点 }; points = commonTools(origion, ctms, 0, 0); //Console.WriteLine("**********BBOX2UserCoordinateSystem************"); //double ALXO = origion.P.X; //double ALYO = origion.P.Y; //double ARXO = origion.P.X + origion.Width; //double ARYO = origion.P.Y - origion.Height; //Console.WriteLine("ALXO={0},ALYO={1},ARXO={2},ARYO={3}", ALXO, ALYO, ARXO, ARYO); //double[][] points = new double[][] //{ // new double[] { ALXO, ALYO, 1 },//左上角的点 // new double[] { ARXO, ARYO, 1 },//右下角的点 //}; //double[][] results = new double[][] //{ // new double[] { 0,0, 1 },//左上角的点 // new double[] { 0, 0, 1 },//右下角的点 //}; //Matrix ctm = null; //double[][] _ctm = null; //ctm = ctms[0]; //_ctm = ctm.Points; //Console.WriteLine("origion matrix"); //Matrix.PrintMatrix(points); //Console.WriteLine(); //Console.WriteLine("CTM matrix"); //Matrix.PrintMatrix(_ctm); //Console.WriteLine(); //results = Matrix.MatrixMult(points, _ctm); //Console.WriteLine("result matrix"); //Matrix.PrintMatrix(results); //Console.WriteLine(); //Console.WriteLine("********************"); //points = results;//update the matrix //将计算出来的矩形区域的主对角线的两点分别给相应的(BLX,BLY)(BRX,BRY)赋值,并输出结果矩形 double BLX = points[0][0]; double BLY = points[0][1]; double BRX = points[1][0]; double BRY = points[1][1]; target = new Recatangle(new Point(0, 0), 0, 0); target.P.X = BLX; target.P.Y = BLY; target.Width = BRX - BLX; target.Height = BLY - BRY; return(target); }
/// <summary> /// this method contains the value of User坐标映射 /// </summary> /// <param name="origion">原始矩形区域</param> /// <param name="ctms">转换的矩阵</param> /// <param name="target">需要返回的矩形区域</param> /// <returns></returns> private static Recatangle Plot2UserCoordinateSystem(Recatangle origion, List <Matrix> ctms, Recatangle target) { //double ALXO = origion.P.X; //double ALYO = origion.P.Y; //double ARXO = origion.P.X + origion.Width; //double ARYO = origion.P.Y - origion.Height; //Console.WriteLine("ALXO={0},ALYO={1},ARXO={2},ARYO={3}", ALXO, ALYO, ARXO, ARYO); //double[][] points = new double[][] //{ // new double[] { ALXO, ALYO, 1 },//左上角的点 // new double[] { ARXO, ARYO, 1 },//右下角的点 //}; //double[][] results = new double[][] //{ // new double[] { 0,0, 1 },//左上角的点 // new double[] { 0, 0, 1 },//右下角的点 //}; //Matrix ctm = null; //double[][] _ctm = null; //for (int i = 0; i <= 1; i++) //{ // //first invoke // ctm = ctms[i]; // _ctm = ctm.Points; // Console.WriteLine("origion matrix"); // Matrix.PrintMatrix(points); // Console.WriteLine(); // Console.WriteLine("CTM matrix"); // Matrix.PrintMatrix(_ctm); // Console.WriteLine(); // results = Matrix.MatrixMult(points, _ctm); // Console.WriteLine("result matrix"); // Matrix.PrintMatrix(results); // Console.WriteLine(); // Console.WriteLine("********************"); // points = results;//update the matrix //} double[][] points = new double[][] { new double[] { 0, 0, 1 }, //左上角的点 new double[] { 0, 0, 1 }, //右下角的点 }; points = commonTools(origion, ctms, 0, 1); #region clip operation /* * 中间截取一段执行Clip操作 */ Recatangle source = new Recatangle(new Point(points[0][0], points[0][1]), points[1][0] - points[0][0], points[0][1] - points[1][1]); target = new Recatangle(new Point(0, 0), 612, 792); bool canClipse = commonUtils.canClip(source, target); //有交集 canClipse = true; //Clip操作执行完成 #endregion //ctm = ctms[2]; //_ctm = ctm.Points; //Console.WriteLine("origion matrix"); //Matrix.PrintMatrix(points); //Console.WriteLine(); //Console.WriteLine("CTM matrix"); //Matrix.PrintMatrix(_ctm); //Console.WriteLine(); //results = Matrix.MatrixMult(points, _ctm); //Console.WriteLine("result matrix"); //Matrix.PrintMatrix(results); //Console.WriteLine(); //Console.WriteLine("********************"); //points = results;//update the matrix points = commonTools(Matrix.ConvertArr2Rect(points), ctms, 2, 2); //将计算出来的矩形区域的主对角线的两点分别给相应的(BLX,BLY)(BRX,BRY)赋值,并输出结果矩形 double BLX = points[0][0]; double BLY = points[0][1]; double BRX = points[1][0]; double BRY = points[1][1]; target.P.X = BLX; target.P.Y = BLY; target.Width = BRX - BLX; target.Height = BLY - BRY; return(target); }
/// <summary> /// Image -> patternSystem /// </summary> /// <param name="origion">原始矩形区域</param> /// <param name="ctms">转换矩阵</param> /// <param name="target">要返回的目标矩形区域</param> /// <returns></returns> private static Recatangle Image2patternSystem(Recatangle origion, List <Matrix> ctms, Recatangle target) { double[][] points = new double[][] { new double[] { 0, 0, 1 }, //左上角的点 new double[] { 0, 0, 1 }, //右下角的点 }; points = commonTools(origion, ctms, 0, 0); //Console.WriteLine("**********Image2patternSystem************"); //double ALXO = origion.P.X; //double ALYO = origion.P.Y; //double ARXO = origion.P.X + origion.Width; //double ARYO = origion.P.Y - origion.Height; //Console.WriteLine("ALXO={0},ALYO={1},ARXO={2},ARYO={3}", ALXO, ALYO, ARXO, ARYO); //double[][] points = new double[][] //{ // new double[] { ALXO, ALYO, 1 },//左上角的点 // new double[] { ARXO, ARYO, 1 },//右下角的点 //}; //double[][] results = new double[][] //{ // new double[] { 0,0, 1 },//左上角的点 // new double[] { 0, 0, 1 },//右下角的点 //}; //Matrix ctm = null; //double[][] _ctm = null; ////先执行一次转换操作 //ctm = ctms[0]; //_ctm = ctm.Points; //Console.WriteLine("origion matrix"); //Matrix.PrintMatrix(points); //Console.WriteLine(); //Console.WriteLine("CTM matrix"); //Matrix.PrintMatrix(_ctm); //Console.WriteLine(); //results = Matrix.MatrixMult(points, _ctm); //Console.WriteLine("result matrix"); //Matrix.PrintMatrix(results); //Console.WriteLine(); //Console.WriteLine("********************"); //points = results;//update the matrix #region clip operation /* * 中间截取一段执行Clip操作,截取可见区域 */ Recatangle source = new Recatangle(new Point(points[0][0], points[0][1]), points[1][0] - points[0][0], points[0][1] - points[1][1]); target = new Recatangle(new Point(0, 0), 404.5725, 213.4275); bool canClipse = commonUtils.canClip(source, target); //判断是否可以截取该区域 points[1][1] = -213.4275; //更新可见区域 canClipse = true; //放行 //Clip操作执行完成 #endregion if (canClipse) { points = commonTools(Matrix.ConvertArr2Rect(points), ctms, 1, ctms.Count - 1); //for (int i = 1; i < ctms.Count; i++) //{ // ctm = ctms[i]; // _ctm = ctm.Points; // Console.WriteLine("origion matrix"); // Matrix.PrintMatrix(points); // Console.WriteLine(); // Console.WriteLine("CTM matrix"); // Matrix.PrintMatrix(_ctm); // Console.WriteLine(); // results = Matrix.MatrixMult(points, _ctm); // Console.WriteLine("result matrix"); // Matrix.PrintMatrix(results); // Console.WriteLine(); // Console.WriteLine("********************"); // points = results;//update the matrix //} } //将计算出来的矩形区域的主对角线的两点分别给相应的(BLX,BLY)(BRX,BRY)赋值,并输出结果矩形 double BLX = points[0][0]; double BLY = points[0][1]; double BRX = points[1][0]; double BRY = points[1][1]; target.P.X = BLX; target.P.Y = BLY; target.Width = BRX - BLX; target.Height = BLY - BRY; return(target); }
/// <summary> /// return the two Recatangle's Sequare /// </summary> /// <param name="rectA"></param> /// <param name="rectB"></param> /// <returns></returns> public static double getSequare(Recatangle rectA, Recatangle rectB) { double result = 0.0; if (isOverlap(rectA, rectB) && isIdentical(rectA, rectB))//首先判断两个矩形是否完全重合 { result = rectA.Width * rectA.Height; Console.WriteLine("两个图形完全重合了!!!两个矩形相交的面积是{0}", result); return(result); } //第1个矩形左上角的坐标 double ALX = rectA.P.X; double ALY = rectA.P.Y; //第1个矩形右下角的坐标 double ARX = ALX + rectA.Width; double ARY = ALY - rectA.Height; //第2个矩形左上角的坐标 double BLX = rectB.P.X; double BLY = rectB.P.Y; //第2个矩形右下角的坐标 double BRX = BLX + rectB.Width; double BRY = BLY - rectB.Height; double min1 = Math.Max(Math.Min(ALX, ARX), Math.Min(BLX, BRX)); double min2 = Math.Max(Math.Min(ALY, ARY), Math.Min(BLY, BRY)); double max1 = Math.Min(Math.Max(ALX, ARX), Math.Max(BLX, BRX)); double max2 = Math.Min(Math.Max(ALY, ARY), Math.Max(BLY, BRY)); if (min1 < max1 && max2 > min2) { Console.WriteLine("ALX={0},ALY={1},ARX={2},ARY={3}", ALX, ALY, ARX, ARY); Console.WriteLine("BLX={0},BLY={1},BRX={2},BRY={3}", BLX, BLY, BRX, BRY); Console.WriteLine("min1={0},min2={1},max1={2},max2={3}", min1, min2, max1, max2); Console.WriteLine("max1 - min1={0},max2 - min2={1},(max1 - min1) * (max2 - min2)={2}", max1 - min1, max2 - min2, (max1 - min1) * (max2 - min2)); //结果保留2位小数 result = Convert.ToDouble(decimal.Round(decimal.Parse("" + (max1 - min1) * (max2 - min2)), 2)); } else { //在这个判断条件当中处理了有关两个矩形的包含的情况 //说明这两个矩形有相互包含的情况 if (rectA.Width * rectA.Height > rectB.Width * rectB.Height && (max1 - min1) * (max2 - min2) > 0) //说明recA包含recB { result = rectB.Height * rectB.Width; } else if (rectA.Width * rectA.Height < rectB.Width * rectB.Height && (max1 - min1) * (max2 - min2) > 0)//说明rectB包含rectA { result = rectA.Height * rectA.Width; } else { Console.WriteLine("两个矩形相离!!!"); Console.WriteLine("Sorry,Somthings Wrong!!,Sequares=0.00"); result = 0.00; } } return(result); }