Beispiel #1
0
        public void FindNearest_NormalsCheck_Rednaxela(PointCloudVertices pointCloud, bool normalsCheck)
        {
            PointCloudVertices nearestNeighbours = new PointCloudVertices();

            //for (int i = pointCloud.Count - 1; i >= 0; i--)
            for (int i = 0; i < pointCloud.Count; i++)
            {
                Vertex vSource = pointCloud[i];

                // Perform a nearest neighbour search around that point.
                KDTreeRednaxela.NearestNeighbour <EllipseWrapper> nearestNeighbor = null;

                if (normalsCheck)
                {
                    nearestNeighbor = KdTree_Rednaxela.FindNearest_EuclidDistance(new float[] { Convert.ToSingle(vSource.Vector.X), Convert.ToSingle(vSource.Vector.Y), Convert.ToSingle(vSource.Vector.Z) }, NumberOfNeighboursToSearch + 1, -1);
                }
                else
                {
                    nearestNeighbor = KdTree_Rednaxela.FindNearest_EuclidDistance(new float[] { Convert.ToSingle(vSource.Vector.X), Convert.ToSingle(vSource.Vector.Y), Convert.ToSingle(vSource.Vector.Z) }, NumberOfNeighboursToSearch, -1);
                }

                while (nearestNeighbor.MoveNext())
                {
                    EllipseWrapper wr      = nearestNeighbor.CurrentPoint;
                    Vertex         vTarget = wr.Vertex;

                    if (vSource != vTarget)
                    {
                        if (!vSource.KDTreeSearch.Contains(vTarget.IndexInModel))
                        {
                            if (!vTarget.TakenInTree)
                            {
                                vTarget.TakenInTree = true;
                                KeyValuePair <int, float> el = new KeyValuePair <int, float>(vTarget.IndexInModel, nearestNeighbor.CurrentDistance);
                                vSource.KDTreeSearch.Add(el);
                                break;
                            }
                        }

                        if ((vSource.KDTreeSearch.Count) >= 1)
                        {
                            break;
                        }
                    }
                }
                //if (vSource.KDTreeSearch.Count == 0)
                //{
                //    System.Windows.Forms.MessageBox.Show("Error in finding neighbour for index " + i.ToString());
                //}
            }

            if (KdTree_Rednaxela.pLeft != null)
            {
                SetRecursive(KdTree_Rednaxela.pLeft);
            }
            if (KdTree_Rednaxela.pRight != null)
            {
                SetRecursive(KdTree_Rednaxela.pRight);
            }

            GlobalVariables.ShowLastTimeSpan("Find neighbours");

            //RemoveAllVerticesBasedOnRadius(pointCloud);
        }
 public static void ShowLastTimeSpan(string name)
 {
     System.Diagnostics.Debug.WriteLine("--Duration for " + name + " : " + GlobalVariables.TimeSpanString());
 }
Beispiel #3
0
        public PointCloudVertices AlignPointClouds_SVD_WithShuflleEffect(bool axesRotateEffect, PointCloudVertices pointCloudSource, PointCloudVertices pointCloudTarget)
        {
            try
            {
                if (pointCloudSource == null || pointCloudTarget == null || pointCloudSource.Count == 0 || pointCloudTarget.Count == 0)
                {
                    System.Diagnostics.Debug.WriteLine("PCA - please check point clouds ");
                    return(null);
                }
                this.Matrix = Matrix4d.Identity;
                pointCloudSourceCentered = CalculatePCA_Internal(pointCloudSource);
                PrepareTargetTree(pointCloudTarget);


                PointCloudVertices myPointCloudIteration = PointCloudVertices.CloneVertices(pointCloudSource);

                for (int i = 0; i < MaxmimumIterations; i++)
                {
                    double meanDistance = SVD_Iteration(myPointCloudIteration);
                    System.Diagnostics.Debug.WriteLine("-->>  Iteration " + i.ToString() + " : Mean Distance : " + meanDistance.ToString("G") + ": duration: " + GlobalVariables.TimeSpanString());

                    if (meanDistance < thresholdConvergence)
                    {
                        break;
                    }
                    myPointCloudIteration = pointCloudResultBest;
                }

                //final check:

                pointCloudResultCentered = CalculatePCA_Internal(pointCloudResult);


                //"Shuffle" effect - the target points are in other order after kdtree search:
                //The mean distance calculated again, as check (was calculated before in the kdTree routine)

                MeanDistance = PointCloudVertices.MeanDistance(pointCloudResult, pointCloudTargetKDTree);
                System.Diagnostics.Debug.WriteLine("-->>  TO CHECK: PCA (SVD) - Final Mean Distance : " + MeanDistance.ToString("G"));

                //MeanDistance = PointCloudVertices.MeanDistance(pointCloudResult, pointCloudTarget);
                //System.Diagnostics.Debug.WriteLine("-->>  PCA (SVD) - Final Mean Distance : " + MeanDistance.ToString("G"));

                this.Matrix              = AdjustSourceTargetByTranslation(Matrix, pointCloudSource, pointCloudTarget);
                pointCloudResult         = Matrix.TransformPoints(pointCloudSource);
                pointCloudResultCentered = CalculatePCA_Internal(pointCloudResult);

                //MeanDistance = PointCloudVertices.MeanDistance(pointCloudResult, pointCloudTarget);
                //System.Diagnostics.Debug.WriteLine("-->>  PCA (SVD) - Final Mean Distance : " + MeanDistance.ToString("G"));

                //for display later:
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Error aligning point cloud");
            }
            return(pointCloudResult);
        }
Beispiel #4
0
        private void ReadObjFile(string fileOBJ)
        {
            this.FileNameLong = fileOBJ;
            IOUtils.ExtractDirectoryAndNameFromFileName(this.FileNameLong, ref this.FileNameShort, ref this.Path);

            string line = string.Empty;

            Vector3 vector;
            Vector3 color;

            List <Vector3> vectors        = new List <Vector3>();
            List <Vector3> colors         = new List <Vector3>();
            List <Vector3> normals        = new List <Vector3>();
            List <uint>    indices        = new List <uint>();
            List <uint>    indicesNormals = new List <uint>();
            List <uint>    indicesTexture = new List <uint>();

            try
            {
                using (StreamReader streamReader = new StreamReader(fileOBJ))
                {
                    while (!streamReader.EndOfStream)
                    {
                        line = streamReader.ReadLine().Trim();

                        if (!line.StartsWith("#"))
                        {
                            while (line.EndsWith("\\"))
                            {
                                line = line.Substring(0, line.Length - 1) + streamReader.ReadLine().Trim();
                            }
                            string   str1         = GlobalVariables.TreatLanguageSpecifics(line);
                            string[] strArrayRead = str1.Split();
                            if (strArrayRead.Length >= 0)
                            {
                                switch (strArrayRead[0].ToLower())
                                {
                                case "mtllib":
                                    if (strArrayRead.Length < 2)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Error reading obj file (mtllib) in line : " + line);
                                    }

                                    this.Texture = IOUtils.ReadTexture(strArrayRead[1], fileOBJ);
                                    break;

                                case "v":    //Vertex
                                    IOUtils.HelperReadVector3dAndColor(strArrayRead, out vector, out color);
                                    vectors.Add(vector);
                                    colors.Add(color);


                                    break;

                                case "vt":    //Texture
                                    if (strArrayRead.Length < 3)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Error reading obj file (Texture) in line : " + line);
                                    }
                                    Vector3d vector1 = new Vector3d(0, 0, 0);
                                    double.TryParse(strArrayRead[1], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector1.X);
                                    double.TryParse(strArrayRead[2], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector1.Y);
                                    this.TextureCoords.Add(new double[2] {
                                        (double)vector1.X, (double)vector1.Y
                                    });
                                    break;

                                case "vn":    //Normals
                                    if (strArrayRead.Length < 4)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Error reading obj file (Normals) in line : " + line);
                                    }
                                    Vector3 vector2 = new Vector3(0, 0, 0);
                                    float.TryParse(strArrayRead[1], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector2.X);
                                    float.TryParse(strArrayRead[2], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector2.Y);
                                    float.TryParse(strArrayRead[3], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector2.Z);
                                    //vector2.NormalizeNew();
                                    normals.Add(vector2);
                                    break;

                                case "f":
                                    IOUtils.ReadIndicesLine(strArrayRead, indices, indicesNormals, indicesTexture);
                                    break;

                                case "g":
                                    //if (myNewModel.Triangles.Count > 0)
                                    //{
                                    //    if (myNewModel.TextureBitmap != null)
                                    //    {
                                    //        p.ColorOverall = System.Drawing.Color.FromArgb(1, 1, 1);
                                    //    }
                                    //    else
                                    //    {
                                    //        double r = Convert.ToSingle(0.3 * Math.Cos((double)(23 * myNewModel.Parts.Count)) + 0.5);
                                    //        double g = Convert.ToSingle(0.5f * Math.Cos((double)(17 * myNewModel.Parts.Count + 1)) + 0.5);
                                    //        double b = Convert.ToSingle(0.5f * Math.Cos((double)myNewModel.Parts.Count) + 0.5);


                                    //        p.ColorOverall = System.Drawing.Color.FromArgb(Convert.ToInt32(r * byte.MaxValue), Convert.ToInt32(g * byte.MaxValue), Convert.ToInt32(b * byte.MaxValue));
                                    //    }
                                    //    //p.ColorOverall = myNewModel.TextureBitmap != null ? new Vector3d(1, 1, 1) : new Vector3d(0.3 * Math.Cos((double)(23 * myNewModel.Parts.Count)) + 0.5, 0.5f * Math.Cos((double)(17 * myNewModel.Parts.Count + 1)) + 0.5, 0.5f * Math.Cos((double)myNewModel.Parts.Count) + 0.5);
                                    //    myNewModel.Parts.Add(new Part(p));
                                    //}
                                    //if (strArrayRead.Length > 1)
                                    //    p.Name = str1.Replace(strArrayRead[1], "");
                                    //myNewModel.Triangles.Clear();
                                    break;
                                }
                            }
                        }
                    }

                    streamReader.Close();
                }
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show("Error reading obj file (general): " + line + " ; " + err.Message);
            }
            if (indices.Count != vectors.Count)
            {
                for (uint i = Convert.ToUInt32(indices.Count); i < vectors.Count; i++)
                {
                    indices.Add(i);
                }
            }
            AssignData(vectors, colors, normals, indices, indicesNormals, indicesTexture);
        }