public static bool sameCylinder(Surface firstSurf, Surface secondSurf) { double[] firstParameters = firstSurf.CylinderParams; double[] firstOrigin = new double[3]; double[] firstAxes = new double[3]; Array.Copy(firstParameters, 0, firstOrigin, 0, 3); Array.Copy(firstParameters, 3, firstAxes, 0, 3); var firstRay = (double)firstParameters.GetValue(6); double[] secondParameters = secondSurf.CylinderParams; double[] secondOrigin = new double[3]; double[] secondAxes = new double[3]; Array.Copy(secondParameters, 0, secondOrigin, 0, 3); Array.Copy(secondParameters, 3, secondAxes, 0, 3); var secondRay = (double)secondParameters.GetValue(6); double[,] matrix = { { (double)firstAxes.GetValue(0) + (double)firstOrigin.GetValue(0), (double)firstAxes.GetValue(1) + (double)firstOrigin.GetValue(1), (double)firstAxes.GetValue(2) + (double)firstOrigin.GetValue(2) }, { (double)firstOrigin.GetValue(0), (double)firstOrigin.GetValue(1), (double)firstOrigin.GetValue(2) }, { (double)secondAxes.GetValue(0) + (double)secondOrigin.GetValue(0), (double)secondAxes.GetValue(1) + (double)secondOrigin.GetValue(1), (double)secondAxes.GetValue(2) + (double)secondOrigin.GetValue(2) }, { (double)secondOrigin.GetValue(0), (double)secondOrigin.GetValue(1), (double)secondOrigin.GetValue(2) }, }; return(Matrix.Rank(matrix) == 1 && Math.Abs(firstRay - secondRay) < 0.001); }
/// <summary> /// Computes the new image size. /// </summary> protected override Size CalculateNewImageSize(UnmanagedImage sourceData) { // Calculate source size float w = sourceData.Width; float h = sourceData.Height; // Get the four corners and the center of the image PointF[] corners = { new PointF(0, 0), new PointF(w, 0), new PointF(0, h), new PointF(w, h), new PointF(w / 2f, h / 2f) }; // Project those points corners = homography.Inverse().TransformPoints(corners); // Recalculate image size float[] px = { corners[0].X, corners[1].X, corners[2].X, corners[3].X }; float[] py = { corners[0].Y, corners[1].Y, corners[2].Y, corners[3].Y }; float maxX = Matrix.Max(px); float minX = Matrix.Min(px); float newWidth = Math.Max(maxX, overlayImage.Width) - Math.Min(0, minX); float maxY = Accord.Math.Matrix.Max(py); float minY = Accord.Math.Matrix.Min(py); float newHeight = Math.Max(maxY, overlayImage.Height) - Math.Min(0, minY); // Store overlay image size this.imageSize = new Size((int)Math.Round(maxX - minX), (int)Math.Round(maxY - minY)); // Store image center this.center = Point.Round(corners[4]); // Calculate and store image offset int offsetX = 0, offsetY = 0; if (minX < 0) { offsetX = (int)Math.Round(minX); } if (minY < 0) { offsetY = (int)Math.Round(minY); } this.offset = new Point(offsetX, offsetY); // Return the final image size return(new Size((int)Math.Ceiling(newWidth), (int)Math.Ceiling(newHeight))); }
private void FindTranslation() { var pam = Matrix.Create <double>(3, 1); pam[0, 0] = Centroid.LocalCoordinateSystem.X; pam[1, 0] = Centroid.LocalCoordinateSystem.Y; pam[2, 0] = Centroid.LocalCoordinateSystem.Z; var pbm = Matrix.Create <double>(3, 1); pbm[0, 0] = Centroid.ParentCoordinateSystem.X; pbm[1, 0] = Centroid.ParentCoordinateSystem.Y; pbm[2, 0] = Centroid.ParentCoordinateSystem.Z; var a = Matrix.Add(Matrix.Multiply(-1, Matrix.Multiply(_matrix, pam)), pbm); Translation = new Vector3((float)a[0, 0], (float)a[1, 0], (float)a[2, 0]); }
//public void SaveTransform(string fileUri) //{ // const string separator = ","; // using (StreamWriter writer = new StreamWriter(fileUri)) // { // writer.WriteLine( // RotationMatrix.M11 + separator + // RotationMatrix.M12 + separator + // RotationMatrix.M13 + separator + // RotationMatrix.M21 + separator + // RotationMatrix.M22 + separator + // RotationMatrix.M23 + separator + // RotationMatrix.M31 + separator + // RotationMatrix.M32 + separator + // RotationMatrix.M33 + separator + // Translation.X + separator + // Translation.Y + separator + // Translation.Z + separator + // Scaling // ); // } //} //public void LoadTransform(string fileUri) //{ // using (TextFieldParser parser = new TextFieldParser(fileUri)) // { // parser.TextFieldType = FieldType.Delimited; // parser.SetDelimiters(","); // while (!parser.EndOfData) // { // //Processing row // string[] fields = parser.ReadFields(); // if (fields.Count() != 13) // return; // _matrix[0, 0] = float.Parse(fields[0]); // _matrix[0, 1] = float.Parse(fields[1]); // _matrix[0, 2] = float.Parse(fields[2]); // _matrix[1, 0] = float.Parse(fields[3]); // _matrix[1, 1] = float.Parse(fields[4]); // _matrix[1, 2] = float.Parse(fields[5]); // _matrix[2, 0] = float.Parse(fields[6]); // _matrix[2, 1] = float.Parse(fields[7]); // _matrix[2, 2] = float.Parse(fields[8]); // Translation = new Vector3( // float.Parse(fields[9]), // float.Parse(fields[10]), // float.Parse(fields[11]) // ); // Scaling = float.Parse(fields[12]); // } // } //} public void AddTestelements() { Correspondences = new List <Correspondence>(); const int n = 10; var t = Matrix.Random(3, 1); var svd = new SingularValueDecomposition(Matrix.Random(3, 3)); var r = Matrix.Multiply(svd.LeftSingularVectors, svd.RightSingularVectors); if (Matrix.Determinant(r) < 0) { var v = svd.RightSingularVectors; v[0, 2] *= -1; v[1, 2] *= -1; v[2, 2] *= -1; r = Matrix.Multiply(v, Matrix.Transpose(svd.LeftSingularVectors)); } var a = Matrix.Random(3, n); var tExpanded = t; for (int i = 0; i < n - 1; i++) { tExpanded = Matrix.InsertColumn(tExpanded, Matrix.GetColumn(t, 0)); } var b = Matrix.Add(Matrix.Multiply(r, a), tExpanded); for (int i = 0; i < n; i++) { Correspondences.Add(new Correspondence { LocalCoordinateSystem = new Vector3(Matrix.GetColumn(a, i).Select(j => (float)j).ToArray()), ParentCoordinateSystem = new Vector3(Matrix.GetColumn(b, i).Select(j => (float)j).ToArray()) }); } }
private void FindOptimalRotation() { var h = new double[3, 3]; foreach (var correspondence in Correspondences) { var pa = correspondence.LocalCoordinateSystem - Centroid.LocalCoordinateSystem; var pb = correspondence.ParentCoordinateSystem - Centroid.ParentCoordinateSystem; var pam = Matrix.Create <double>(3, 1); pam[0, 0] = pa.X; pam[1, 0] = pa.Y; pam[2, 0] = pa.Z; var pbm = Matrix.Create <double>(1, 3); pbm[0, 0] = pb.X; pbm[0, 1] = pb.Y; pbm[0, 2] = pb.Z; var hl = Matrix.Add(h, Matrix.Multiply(pam, pbm)); h = hl; } var svd = new SingularValueDecomposition(h); var r = Matrix.Multiply(svd.RightSingularVectors, Matrix.Transpose(svd.LeftSingularVectors)); if (Matrix.Determinant(r) < 0) { var v = svd.RightSingularVectors; v[0, 2] *= -1; v[1, 2] *= -1; v[2, 2] *= -1; r = Matrix.Multiply(v, Matrix.Transpose(svd.LeftSingularVectors)); } _matrix = r; }
public static bool samePlane(Face2 firstFace, Face2 secondFace, SldWorks swApp) { var firstSurf = (Surface)firstFace.GetSurface(); var secondSurf = (Surface)secondFace.GetSurface(); var firstParameters = (Array)firstSurf.PlaneParams; var secondParameters = (Array)secondSurf.PlaneParams; var firstNormal = new double[3]; var firstPoint = new double[3]; var secondNormal = new double[3]; var secondPoint = new double[3]; Array.Copy(firstParameters, 0, firstNormal, 0, 3); Array.Copy(firstParameters, 3, firstPoint, 0, 3); Array.Copy(secondParameters, 0, secondNormal, 0, 3); Array.Copy(secondParameters, 3, secondPoint, 0, 3); if (!firstFace.FaceInSurfaceSense()) { firstNormal.SetValue(-(double)firstNormal.GetValue(0), 0); firstNormal.SetValue(-(double)firstNormal.GetValue(1), 1); firstNormal.SetValue(-(double)firstNormal.GetValue(2), 2); } if (!secondFace.FaceInSurfaceSense()) { secondNormal.SetValue(-(double)secondNormal.GetValue(0), 0); secondNormal.SetValue(-(double)secondNormal.GetValue(1), 1); secondNormal.SetValue(-(double)secondNormal.GetValue(2), 2); } var results = Math.Abs(Matrix.InnerProduct(firstNormal, secondNormal) - 1); var normalPrint = String.Format("{0} {1} {2} --- {3} {4} {5} = {6}", firstNormal[0], firstNormal[1], firstNormal[2], secondNormal[0], secondNormal[1], secondNormal[2], results); var firstEquation = new double[4] { (double)firstNormal.GetValue(0), (double)firstNormal.GetValue(1), (double)firstNormal.GetValue(2), -(double)firstNormal.GetValue(0) * (double)firstPoint.GetValue(0) - (double)firstNormal.GetValue(1) * (double)firstPoint.GetValue(1) - (double)firstNormal.GetValue(2) * (double)firstPoint.GetValue(2), }; var secondEquation = new double[4] { (double)secondNormal.GetValue(0), (double)secondNormal.GetValue(1), (double)secondNormal.GetValue(2), -(double)secondNormal.GetValue(0) * (double)secondPoint.GetValue(0) - (double)secondNormal.GetValue(1) * (double)secondPoint.GetValue(1) - (double)secondNormal.GetValue(2) * (double)secondPoint.GetValue(2), }; var equationPrint = String.Format("Eq: {0}x {1}y {2}z = {3}", firstEquation[0], firstEquation[1], firstEquation[2], firstEquation[3]); /* * if (Math.Abs(Accord.Math.Matrix.InnerProduct(firstNormal, secondNormal) - 1) < 0.001) * { * swApp.SendMsgToUser("Normale uguale"); * return false; * } */ //return firstEquation.Equals(secondFace); if (Math.Abs(firstEquation[0] - secondEquation[0]) < 0.01 && Math.Abs(firstEquation[1] - secondEquation[1]) < 0.01 && Math.Abs(firstEquation[2] - secondEquation[2]) < 0.01 && Math.Abs(firstEquation[3] - secondEquation[3]) < 0.01) { swApp.SendMsgToUser("Equazione uguale"); return(true); } return(false); }
/// <summary> /// Constructs a new Blend filter. /// </summary> /// /// <param name="overlayImage">The overlay image (also called the anchor).</param> /// public Blend(Bitmap overlayImage) : this(Matrix.Identity(3), overlayImage) { }