protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            while (iter.IsValid())
            {
                List <Cl3DModel.Cl3DModelPointIterator> neighboors = iter.GetListOfNeighbors();

                foreach (Cl3DModel.Cl3DModelPointIterator neighbor1Ring in neighboors)
                {
                    bool founded = false;
                    foreach (Cl3DModel.Cl3DModelPointIterator neighbor2Ring in neighbor1Ring.GetListOfNeighbors())
                    {
                        foreach (Cl3DModel.Cl3DModelPointIterator neighbor3Ring in neighbor2Ring.GetListOfNeighbors())
                        {
                            if (neighbor3Ring.PointID == iter.PointID)
                            {
                                founded = true;
                                break;
                            }
                        }
                    }
                    if (!founded)
                    {
                        iter.RemoveNeighbor(neighbor1Ring);
                    }
                }

                if (!iter.MoveToNext())
                {
                    break;
                }
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            List <Cl3DModel.Cl3DModelPointIterator>[,] Map = null;

            int   width        = 0;
            int   heinght      = 0;
            float MinusXoffset = 0.0f;
            float MinusYoffset = 0.0f;

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            do
            {
                foreach (Cl3DModel.Cl3DModelPointIterator neighbor in iter.GetListOfNeighbors())
                {
                    iter.RemoveNeighbor(neighbor);
                }
            } while (iter.MoveToNext());

            ClTools.CreateGridBasedOnRealXY(p_Model, out Map, out width, out heinght, out MinusXoffset, out MinusYoffset);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < heinght; y++)
                {
                    if (Map[x, y] != null)
                    {
                        float MeanValue = ClTools.CalculateMeanValue(Map[x, y]);

                        Cl3DModel.Cl3DModelPointIterator Point = Map[x, y][0];

                        foreach (Cl3DModel.Cl3DModelPointIterator pt in Map[x, y])
                        {
                            string label = "";
                            if (pt.IsLabeled(out label))
                            {
                                Point = pt;
                                break;
                            }
                        }
                        foreach (Cl3DModel.Cl3DModelPointIterator pt in Map[x, y])
                        {
                            if (pt.PointID != Point.PointID)
                            {
                                p_Model.RemovePointFromModel(pt);
                                //         Map[x, y].Remove(pt);
                            }
                        }

                        Point.RangeImageX = x;
                        Point.RangeImageY = y;
                        Point.Z           = MeanValue;
                        Point.X           = x + MinusXoffset;
                        Point.Y           = y + MinusYoffset;
                    }
                }
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            if (iter.IsValid())
            {
                do
                {
                    List <float> points = new List <float>();
                    List <Cl3DModel.Cl3DModelPointIterator> Neighbors = iter.GetListOfNeighbors();

                    foreach (Cl3DModel.Cl3DModelPointIterator neighbor in Neighbors)
                    {
                        points.Add(neighbor.Z);
                    }
                    points.Add(iter.Z);

                    points.Sort();

                    float result = 0;
                    if (points.Count % 2 == 0)// even
                    {
                        int first = (int)(points.Count / 2);
                        result = (points[first - 1] + points[first]) / 2;
                    }
                    else // odd
                    {
                        int element = (int)(points.Count / 2);
                        result = points[element];
                    }

                    if (m_bDecision)
                    {
                        float diff = Math.Abs(iter.Z - result);
                        if (diff >= m_SpikeThreshold)
                        {
                            iter = p_Model.RemovePointFromModel(iter);
                        }
                        else
                        if (!iter.MoveToNext())
                        {
                            break;
                        }
                    }
                    else
                    {
                        iter.Z = result;
                        if (!iter.MoveToNext())
                        {
                            break;
                        }
                    }
                } while (iter.IsValid());
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            string name = p_Model.ModelFileFolder + p_Model.ModelFileName + m_sFilePostFix;


            if (m_bBinaryMode)
            {
                p_Model.SaveModel(name);
            }
            else
            {
                name += ".model";
                Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
                if (!iter.IsValid())
                {
                    throw new Exception("Iterator in the model is not valid");
                }

                using (TextWriter tw = new StreamWriter(name, false))
                {
                    tw.WriteLine("@----------------------------------------");
                    tw.WriteLine("@     Przemyslaw Szeptycki LIRIS 2008");
                    tw.WriteLine("@                Face model");
                    tw.WriteLine("@  Model name: " + p_Model.ModelFileName);
                    tw.WriteLine("@----------------------------------------");
                    tw.WriteLine("@ PointID X Y Z (TextureX TextureY) (Neighbors PointID)");
                    do
                    {
                        string line = iter.PointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + iter.X.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + iter.Y.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + iter.Z.ToString(System.Globalization.CultureInfo.InvariantCulture) + " ( " + iter.RangeImageX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + iter.RangeImageY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " ) ( ";
                        List <Cl3DModel.Cl3DModelPointIterator> neighbors = iter.GetListOfNeighbors();
                        foreach (Cl3DModel.Cl3DModelPointIterator neighbor in neighbors)
                        {
                            line += neighbor.PointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " ";
                        }
                        line += ")";

                        tw.WriteLine(line);
                    } while (iter.MoveToNext());

                    int nop = p_Model.GetAllSpecificPoints().Count;
                    if (nop != 0)
                    {
                        tw.WriteLine("Landmark points (ptID): " + nop.ToString());
                        foreach (KeyValuePair <string, Cl3DModel.Cl3DModelPointIterator> specificPoint in p_Model.GetAllSpecificPoints())
                        {
                            tw.WriteLine(specificPoint.Key + " " + specificPoint.Value.PointID.ToString());
                        }
                    }

                    tw.Close();
                }
            }
        }
Пример #5
0
 private Cl3DModel.Cl3DModelPointIterator GetRandomUnseenPointOfHole(ref Cl3DModel p_Model)
 {
     Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
     if (!iter.IsValid())
     {
         return(null);
     }
     do
     {
         if (iter.GetListOfNeighbors().Count < 8 && !iter.AlreadyVisited)
         {
             return(iter);
         }
     } while (iter.MoveToNext());
     return(null);
 }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            if (NormalsAdded.Count != 0)
            {
                foreach (Cl3DModel.Cl3DModelPointIterator pt in NormalsAdded)
                {
                    p_Model.RemovePointFromModel(pt);
                }

                NormalsAdded = new List <Cl3DModel.Cl3DModelPointIterator>();
            }

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            do
            {
                Vector NormalVector = null;
                if (UsePCA)
                {
                    ClTools.CalculateNormalVectorInPointUsingPCA(iter.X, iter.Y, iter.Z, iter.GetListOfNeighbors(), out NormalVector);
                }
                else
                {
                    ClTools.CalculateNormalVectorInPoint(iter.X, iter.Y, iter.Z, iter.GetListOfNeighbors(), out NormalVector);
                }

                double norm = Math.Sqrt(Math.Pow(NormalVector[0], 2) + Math.Pow(NormalVector[1], 2) + Math.Pow(NormalVector[2], 2));
                if (norm != 0)
                {
                    NormalVector[0] /= (float)norm;
                    NormalVector[1] /= (float)norm;
                    NormalVector[2] /= (float)norm;
                }
                iter.NormalVector = NormalVector;

                if (ShowNormals)
                {
                    Cl3DModel.Cl3DModelPointIterator point = p_Model.AddPointToModel(iter.X - (float)NormalVector[0] * 3, iter.Y - (float)NormalVector[1] * 3, iter.Z - (float)NormalVector[2] * 3);
                    iter.AddNeighbor(point);
                    NormalsAdded.Add(point);
                }
            } while (iter.MoveToNext());
        }
Пример #7
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            // if (p_Model.ModelType != "abs")
            //   throw new Exception("Saveing to .obj files works only for ABS files");

            string name = p_Model.ModelFileFolder + p_Model.ModelFileName + ".obj";

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            if (!iter.IsValid())
            {
                throw new Exception("Iterator in the model is not valid");
            }

            using (TextWriter tw = new StreamWriter(name, false))
            {
                tw.WriteLine("#----------------------------------------");
                tw.WriteLine("#     Przemyslaw Szeptycki LIRIS 2009");
                tw.WriteLine("#            Object Face model");
                tw.WriteLine("#  Model name: " + p_Model.ModelFileName);
                tw.WriteLine("#         [email protected]");
                tw.WriteLine("#----------------------------------------");

                uint vertexNO = 1;
                Dictionary <uint, uint> MapVertexNo = new Dictionary <uint, uint>();
                do
                {
                    string line = "v " + iter.X.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + iter.Y.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + iter.Z.ToString(System.Globalization.CultureInfo.InvariantCulture);

                    MapVertexNo.Add(iter.PointID, vertexNO++);

                    tw.WriteLine(line);
                } while (iter.MoveToNext());

                // --- create faces
                iter = p_Model.GetIterator();
                do
                {
                    iter.AlreadyVisited = true;

                    List <Cl3DModel.Cl3DModelPointIterator> neighbors = iter.GetListOfNeighbors();

                    uint MainPointID;
                    if (!MapVertexNo.TryGetValue(iter.PointID, out MainPointID))
                    {
                        throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                    }

                    uint point1 = 0;
                    uint point2 = 0;
                    uint point3 = 0;

                    foreach (Cl3DModel.Cl3DModelPointIterator point in neighbors)
                    {
                        //if (point.AlreadyVisited)
                        //    continue;

                        if (point.RangeImageY == iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // first point
                        {
                            if (!MapVertexNo.TryGetValue(point.PointID, out point1))
                            {
                                throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                            }
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // second point
                        {
                            if (!MapVertexNo.TryGetValue(point.PointID, out point2))
                            {
                                throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                            }
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX == iter.RangeImageX) // third point
                        {
                            if (!MapVertexNo.TryGetValue(point.PointID, out point3))
                            {
                                throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                            }
                        }
                    }

                    string line = "";
                    if (point1 != 0 && point2 != 0)
                    {
                        line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        if (point3 != 0)
                        {
                            line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        }
                    }
                    else if (point1 != 0 && point3 != 0)
                    {
                        line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                    }
                    else if (point2 != 0 && point3 != 0)
                    {
                        line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                    }

                    tw.Write(line);
                } while (iter.MoveToNext());
                tw.Close();
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            uint   current          = 0;
            string NeighborhoodSize = m_fNeighborhoodSize.ToString();

            if (m_bFirstNeighbor)
            {
                NeighborhoodSize = "1Neighb";
            }
            string ShapeIndexString = "ShapeIndex_" + NeighborhoodSize;

            if (iter.IsValid())
            {
                do
                {
                    double A = 0;
                    double B = 0;
                    double C = 0;
                    double D = 0;
                    double E = 0;
                    double F = 0;

                    double H               = 0;
                    double K               = 0;
                    double k1              = 0;
                    double k2              = 0;
                    double ShapeIndex      = 0;
                    double CurvednessIndex = 0;
                    List <Cl3DModel.Cl3DModelPointIterator> ListOfneighborhood;
                    double dx;
                    double dy;
                    double dxy;
                    double dxx;
                    double dyy;

                    //------------------------- Neighborhood Custom --------------------------------------------------
                    if (!iter.IsSpecificValueCalculated(ShapeIndexString) || m_bRecalculateCurvature)
                    {
                        if (!m_bFirstNeighbor)
                        {
                            ClTools.GetNeighborhoodWithGeodesicDistance(out ListOfneighborhood, iter, m_fNeighborhoodSize);
                        }
                        else
                        {
                            ListOfneighborhood = iter.GetListOfNeighbors();
                        }

                        if (m_bNeighborhoodRotation)
                        {
                            Matrix Rotation = ClTools.CalculateRotationMatrix(iter.NormalVector, new Vector(new double[] { iter.NormalVector[0], iter.NormalVector[1], 1 }));
                            List <ClTools.MainPoint3D> Neighbors = new List <ClTools.MainPoint3D>();
                            foreach (Cl3DModel.Cl3DModelPointIterator Neighb in ListOfneighborhood)
                            {
                                Matrix after = Rotation * Neighb;
                                Neighbors.Add(new ClTools.MainPoint3D((float)after[0, 0], (float)after[1, 0], (float)after[2, 0], ""));
                            }

                            if (!ClTools.CountSurfaceCoefficients(Neighbors, ref A, ref B, ref C, ref D, ref E, ref F))
                            {
                                iter.RemoveSpecificValue("Gaussian_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("Mean_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("K1_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("K2_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("ShapeIndex_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("CurvednessIndex_" + NeighborhoodSize);
                                continue;
                            }
                        }
                        else
                        {
                            if (!ClTools.CountSurfaceCoefficients(ListOfneighborhood, ref A, ref B, ref C, ref D, ref E, ref F))
                            {
                                iter.RemoveSpecificValue("Gaussian_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("Mean_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("K1_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("K2_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("ShapeIndex_" + NeighborhoodSize);
                                iter.RemoveSpecificValue("CurvednessIndex_" + NeighborhoodSize);
                                continue;
                            }
                        }


                        dx  = B + 2 * D * iter.X + E * iter.Y;
                        dy  = C + E * iter.X + 2 * F * iter.Y;
                        dxy = E;
                        dxx = 2 * D;
                        dyy = 2 * F;
                        //Mean
                        H = ((((1 + Math.Pow(dy, 2)) * dxx) - (2 * dx * dy * dxy) + ((1 + Math.Pow(dx, 2)) * dyy)) / (2 * Math.Pow((1 + Math.Pow(dx, 2) + Math.Pow(dy, 2)), 3.0d / 2.0d)));
                        //Gaussian
                        K               = (((dxx * dyy) - Math.Pow(dxy, 2)) / Math.Pow((1 + Math.Pow(dx, 2) + Math.Pow(dy, 2)), 2));
                        k1              = H + Math.Sqrt(Math.Pow(H, 2) - K);
                        k2              = H - Math.Sqrt(Math.Pow(H, 2) - K);
                        ShapeIndex      = 0.5d - (1 / Math.PI) * Math.Atan((k1 + k2) / (k1 - k2));
                        CurvednessIndex = Math.Sqrt(Math.Pow(k1, 2) + Math.Pow(k2, 2)) / 2;

                        iter.AddSpecificValue("Gaussian_" + NeighborhoodSize, K);
                        iter.AddSpecificValue("Mean_" + NeighborhoodSize, H);
                        iter.AddSpecificValue("K1_" + NeighborhoodSize, k1);
                        iter.AddSpecificValue("K2_" + NeighborhoodSize, k2);
                        iter.AddSpecificValue("ShapeIndex_" + NeighborhoodSize, ShapeIndex);
                        iter.AddSpecificValue("CurvednessIndex_" + NeighborhoodSize, CurvednessIndex);
                    }

                    ClInformationSender.SendInformation((current * 100 / p_Model.ModelPointsCount).ToString(System.Globalization.CultureInfo.InvariantCulture), ClInformationSender.eInformationType.eProgress);
                    current++;
                } while (iter.MoveToNext());
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator Center = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip);

            Cl3DModel.Cl3DModelPointIterator        iter             = p_Model.GetIterator();
            List <Cl3DModel.Cl3DModelPointIterator> BoundaryVertexes = new List <Cl3DModel.Cl3DModelPointIterator>();

            Cl3DModel.Cl3DModelPointIterator BeginningPoint = null;
            do
            {
                if (iter.GetListOfNeighbors().Count <= 7)
                {
                    iter.AlreadyVisited = true;
                    // iter.Color = Color.Red;

                    if (BeginningPoint == null)
                    {
                        BeginningPoint = iter.CopyIterator();
                    }

                    BoundaryVertexes.Add(iter.CopyIterator());
                }
            } while (iter.MoveToNext());

            Cl3DModel.Cl3DModelPointIterator CurrentPoint = BeginningPoint.CopyIterator();
            Vector NormalVector   = new Vector(3);
            bool   IsNextInTheQue = false;
            float  CurrentNo      = 0;

            do
            {
                CurrentPoint.AlreadyVisited = false;
                Cl3DModel.Cl3DModelPointIterator NextPoint = null;
                IsNextInTheQue = false;
                foreach (Cl3DModel.Cl3DModelPointIterator neighbor in CurrentPoint.GetListOfNeighbors())
                {
                    if (neighbor.AlreadyVisited) // means next in the que
                    {
                        NextPoint      = neighbor.CopyIterator();
                        IsNextInTheQue = true;
                        break;
                    }
                }
                if (NextPoint == null)
                {
                    break;
                }

                Vector tmpVectorCurr = new Vector(new double[] { (CurrentPoint.X - Center.X), (CurrentPoint.Y - Center.Y), (CurrentPoint.Z - Center.Z) });
                Vector tmpVectorNext = new Vector(new double[] { (NextPoint.X - Center.X), (NextPoint.Y - Center.Y), (NextPoint.Z - Center.Z) });

                NormalVector[0] += (tmpVectorNext[1] * tmpVectorCurr[2] - tmpVectorNext[2] * tmpVectorCurr[1]);
                NormalVector[1] += (tmpVectorNext[2] * tmpVectorCurr[0] - tmpVectorNext[0] * tmpVectorCurr[2]);
                NormalVector[2] += (tmpVectorNext[0] * tmpVectorCurr[1] - tmpVectorNext[1] * tmpVectorCurr[0]);

                NormalVector      /= 2;
                CurrentPoint.Color = ClTools.GetColorRGB(CurrentNo / BoundaryVertexes.Count, 1f);
                CurrentNo++;

                CurrentPoint = NextPoint.CopyIterator();
            } while (IsNextInTheQue);

            //NormalVector = NormalVector.Normalize();
            Center.AddNeighbor(p_Model.AddPointToModel((float)NormalVector[0] + Center.X, (float)NormalVector[1] + Center.Y, (float)NormalVector[2] + Center.Z));


            /*   Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
             * while(iter.IsValid())
             * {
             *     float X = iter.X;
             *     float Y = iter.Y;
             *     float Z = iter.Z;
             *     ClTools.RotateXDirection(X, Y, Z, agnleX, out X, out Y, out Z);
             *     ClTools.RotateYDirection(X, Y, Z, angleY, out X, out Y, out Z);
             *     ClTools.RotateZDirection(X, Y, Z, agnleZ, out X, out Y, out Z);
             *
             *     iter.X = X;
             *     iter.Y = Y;
             *     iter.Z = Z;
             *
             *     if (!iter.MoveToNext())
             *         break;
             * }
             */
        }
Пример #10
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            if (m_sFileName.Length == 0)
            {
                m_sFileName = p_Model.ModelFileFolder + p_Model.ModelFileName + ".m2";
            }

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            if (!iter.IsValid())
            {
                throw new Exception("Iterator in the model is not valid");
            }

            using (TextWriter tw = new StreamWriter(m_sFileName, false))
            {
                Cl3DModel.Cl3DModelPointIterator NoseTip = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip);
                tw.WriteLine("CenterVertex " + (NoseTip.PointID + 1).ToString());
                do
                {
                    uint ID = iter.PointID + 1;

                    double geodesicDistance = 0;
                    if (!iter.GetSpecificValue("GeodesicDistanceToNoseTip", out geodesicDistance))
                    {
                        geodesicDistance = 100;
                    }

                    int R = 0;
                    R = (R << 8) + (int)iter.Color.R;
                    int G = 0;
                    G = (G << 8) + (int)iter.Color.G;
                    int B = 0;
                    B = (B << 8) + (int)iter.Color.B;

                    string line = "Vertex " + ID.ToString(System.Globalization.CultureInfo.InvariantCulture) + "  "
                                  + iter.X.ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + iter.Y.ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + iter.Z.ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + geodesicDistance.ToString(System.Globalization.CultureInfo.InvariantCulture) + " {rgb=("
                                  + ((float)R / 255.0f).ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + ((float)G / 255.0f).ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + ((float)B / 255.0f).ToString(System.Globalization.CultureInfo.InvariantCulture) + ")}";

                    tw.WriteLine(line);
                } while (iter.MoveToNext());

                // --- create faces
                iter = p_Model.GetIterator();
                uint FaceNO = 1;
                do
                {
                    iter.AlreadyVisited = true;

                    List <Cl3DModel.Cl3DModelPointIterator> neighbors = iter.GetListOfNeighbors();

                    uint MainPointID = iter.PointID + 1;

                    uint point1           = 0;
                    bool point1Founded    = false;
                    uint point2           = 0;
                    bool point2Founded    = false;
                    uint point3           = 0;
                    bool point3Founded    = false;
                    uint point4Exc        = 0;
                    bool point4ExcFounded = false;
                    uint point5Exc        = 0;
                    bool point5ExcFounded = false;
                    uint point6Exc        = 0;
                    bool point6ExcFounded = false;

                    foreach (Cl3DModel.Cl3DModelPointIterator point in neighbors)
                    {
                        if (point.RangeImageY == iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // first point
                        {
                            point1        = point.PointID + 1;
                            point1Founded = true;
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // second point
                        {
                            point2        = point.PointID + 1;
                            point2Founded = true;
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX == iter.RangeImageX) // third point
                        {
                            point3        = point.PointID + 1;
                            point3Founded = true;
                        }
                        if (point.RangeImageY < iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // second point
                        {
                            point4Exc        = point.PointID + 1;
                            point4ExcFounded = true;
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX < iter.RangeImageX) // second point
                        {
                            point5Exc        = point.PointID + 1;
                            point5ExcFounded = true;
                        }
                        if (point.RangeImageY < iter.RangeImageY && point.RangeImageX == iter.RangeImageX) // second point
                        {
                            point6Exc        = point.PointID + 1;
                            point6ExcFounded = true;
                        }
                    }

                    string line = "";
                    if (point1Founded && point2Founded)
                    {
                        line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        FaceNO++;
                        if (point3Founded)
                        {
                            line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                            FaceNO++;
                        }
                        if (!point6ExcFounded && point4ExcFounded)
                        {
                            line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point4Exc.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                            FaceNO++;
                        }
                    }
                    else if (point1Founded && point3Founded)
                    {
                        line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        FaceNO++;
                    }
                    else if (point2Founded && point3Founded)
                    {
                        line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        FaceNO++;
                    }

                    /*else if (point1Founded && !point2Founded && !point3Founded && point4ExcFounded)
                     * {
                     *  line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point4Exc.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                     *  FaceNO++;
                     *
                     * }
                     */
                    /*else if (!point1Founded && !point2Founded && point3Founded && point5ExcFounded)
                     * {
                     *  line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point5Exc.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                     *  FaceNO++;
                     *
                     * }
                     */
                    tw.Write(line);
                } while (iter.MoveToNext());
                tw.Close();
            }
        }
Пример #11
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator tmpIter = p_Model.GetIterator();

            while (tmpIter.IsValid())
            {
                if (tmpIter.GetListOfNeighbors().Count <= 1)
                {
                    tmpIter = p_Model.RemovePointFromModel(tmpIter);
                }
                else
                {
                    if (!tmpIter.MoveToNext())
                    {
                        break;
                    }
                }
            }

            List <List <Cl3DModel.Cl3DModelPointIterator> > listOfRegions = new List <List <Cl3DModel.Cl3DModelPointIterator> >();

            List <Cl3DModel.Cl3DModelPointIterator> ListToCheck    = new List <Cl3DModel.Cl3DModelPointIterator>();
            List <Cl3DModel.Cl3DModelPointIterator> newListToCheck = new List <Cl3DModel.Cl3DModelPointIterator>();

            Cl3DModel.Cl3DModelPointIterator iter = null;
            int listNo = 0;

            while ((iter = findNextUnseenElement(ref p_Model)) != null)
            {
                ListToCheck.Add(iter);
                listOfRegions.Add(new List <Cl3DModel.Cl3DModelPointIterator>());
                do
                {
                    foreach (Cl3DModel.Cl3DModelPointIterator ElementFromListToCheck in ListToCheck)
                    {
                        if (!ElementFromListToCheck.AlreadyVisited)
                        {
                            listOfRegions[listNo].Add(ElementFromListToCheck);
                            ElementFromListToCheck.AlreadyVisited = true;
                            foreach (Cl3DModel.Cl3DModelPointIterator it in ElementFromListToCheck.GetListOfNeighbors())
                            {
                                if (!it.AlreadyVisited)
                                {
                                    newListToCheck.Add(it);
                                }
                            }
                        }
                    }

                    ListToCheck.Clear();
                    foreach (Cl3DModel.Cl3DModelPointIterator it in newListToCheck)
                    {
                        ListToCheck.Add(it);
                    }
                    newListToCheck.Clear();
                } while (ListToCheck.Count != 0);
                ListToCheck.Clear();
                listNo++;
            }

            if (listOfRegions.Count <= 1)
            {
                return;
            }

            int maxCount = listOfRegions[0].Count;;
            int maxI     = 0;

            for (int i = 1; i < listOfRegions.Count; i++)
            {
                if (listOfRegions[i].Count > maxCount)
                {
                    maxI     = i;
                    maxCount = listOfRegions[i].Count;
                }
            }

            if (m_bColorIt)
            {
                for (int i = 0; i < listOfRegions.Count; i++)
                {
                    if (i == maxI)
                    {
                        continue;
                    }

                    for (int j = 0; j < listOfRegions[i].Count; j++)
                    {
                        listOfRegions[i][j].Color = ClTools.GetColorRGB(((float)i) / listOfRegions.Count, 1);
                    }
                }
            }
            else
            {
                for (int i = 0; i < listOfRegions.Count; i++)
                {
                    if (i == maxI)
                    {
                        continue;
                    }

                    for (int j = 0; j < listOfRegions[i].Count; j++)
                    {
                        p_Model.RemovePointFromModel(listOfRegions[i][j]);
                    }
                }
            }
        }
Пример #12
0
        public override void Render(Device p_dDevice, Control p_cRenderWindow)
        {
            if (m_Base3DModel.IsModelChanged)
            {
                //ClInformationSender.SendInformation("Creating render object ("+m_Base3DModel.ModelPointsCount+" points)...", ClInformationSender.eInformationType.eTextExternal);
                m_lRenderModelVertex.Clear();
                m_lRenderLines.Clear();
                m_Base3DModel.ResetVisitedPoints();
                Cl3DModel.Cl3DModelPointIterator iterator = m_Base3DModel.GetIterator();
                List <KeyValuePair <string, Cl3DModel.Cl3DModelPointIterator> > specificPoints = m_Base3DModel.GetAllSpecificPoints();
                float meanX = 0;
                float meanY = 0;
                float meanZ = 0;
                if (iterator.IsValid())
                {
                    do
                    {
                        meanX += -iterator.X;
                        meanY += iterator.Y;
                        meanZ += iterator.Z;
                        Color pointColor      = new Color();
                        bool  isSpecificPoint = false;
                        foreach (KeyValuePair <string, Cl3DModel.Cl3DModelPointIterator> specificPoint in specificPoints)
                        {
                            if (iterator.PointID == specificPoint.Value.PointID)
                            {
                                isSpecificPoint = true;
                                break;
                            }
                        }
                        if (!isSpecificPoint)
                        {
                            pointColor = iterator.Color;
                        }
                        else
                        {
                            pointColor = Color.Red;
                        }

                        m_lRenderModelVertex.Add(new CustomVertex.PositionColored(-iterator.X, iterator.Y, iterator.Z, pointColor.ToArgb()));

                        List <Cl3DModel.Cl3DModelPointIterator> neighbors = iterator.GetListOfNeighbors();
                        foreach (Cl3DModel.Cl3DModelPointIterator neighbor in neighbors)
                        {
                            if (!neighbor.AlreadyVisited)
                            {
                                m_lRenderLines.Add(new CustomVertex.PositionColored(-iterator.X, iterator.Y, iterator.Z, pointColor.ToArgb()));
                                m_lRenderLines.Add(new CustomVertex.PositionColored(-neighbor.X, neighbor.Y, neighbor.Z, neighbor.Color.ToArgb()));
                            }
                        }
                        iterator.AlreadyVisited = true;
                    }while (iterator.MoveToNext());
                }

                m_Base3DModel.IsModelChanged = false;
                m_Base3DModel.ResetVisitedPoints();
                #if RENDER_1
                ClCamera camera = ClRender.getInstance().getCamera();
                if (camera != null)
                {
                    meanX /= m_lRenderModelVertex.Count;
                    meanY /= m_lRenderModelVertex.Count;
                    meanZ /= m_lRenderModelVertex.Count;
                    camera.MoveCameraLookAt(meanX, meanY, meanZ);
                    //    ClRender.getInstance().AddRenderObj(new ClCoordinateSystem(meanX,meanY,meanZ));
                }
                #endif
            }

            if (m_lRenderModelVertex.Count != 0)
            {
                p_dDevice.VertexFormat = CustomVertex.PositionColored.Format;
                if (m_lRenderLines.Count != 0)
                {
                    p_dDevice.DrawUserPrimitives(PrimitiveType.LineList, m_lRenderLines.Count / 2, m_lRenderLines.ToArray());
                }

                p_dDevice.DrawUserPrimitives(PrimitiveType.PointList, m_lRenderModelVertex.Count, m_lRenderModelVertex.ToArray());
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            // Looking for a hole and connecting points ---- Later should be removed
            Cl3DModel.Cl3DModelPointIterator NoseTip  = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip);
            Cl3DModel.Cl3DModelPointIterator LeftEye  = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.LeftEyeRightCorner);
            Cl3DModel.Cl3DModelPointIterator RightEye = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.RightEyeLeftCorner);

            float distanceBetweenEyes = LeftEye - RightEye;
            float ossfest             = distanceBetweenEyes * 0.4f; //20% of distance between eyes will be taken as a stripe down to search for the mouth hole

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            Dictionary <uint, Cl3DModel.Cl3DModelPointIterator> pointsLowerBoundary = new Dictionary <uint, Cl3DModel.Cl3DModelPointIterator>();

            do
            {
                if (iter.X > LeftEye.X + ossfest && iter.X < RightEye.X - ossfest && iter.Y < NoseTip.Y - 20 && iter.Y > NoseTip.Y - 40)
                {
                    // iter.Color = Color.LightGreen;
                    if (iter.GetListOfNeighbors().Count != 8)
                    {
                        // iter.Color = Color.LightBlue;
                        pointsLowerBoundary.Add(iter.PointID, iter.CopyIterator());
                    }
                }
            } while (iter.MoveToNext());

            List <List <Cl3DModel.Cl3DModelPointIterator> > ListOfBoundarie = new List <List <Cl3DModel.Cl3DModelPointIterator> >();

            while (pointsLowerBoundary.Count != 0)
            {
                Cl3DModel.Cl3DModelPointIterator test = new List <Cl3DModel.Cl3DModelPointIterator>(pointsLowerBoundary.Values)[0];
                pointsLowerBoundary.Remove(test.PointID);

                bool toRemove = false;
                List <Cl3DModel.Cl3DModelPointIterator> CurrentList = new List <Cl3DModel.Cl3DModelPointIterator>();
                ListOfBoundarie.Add(CurrentList);

                List <Cl3DModel.Cl3DModelPointIterator> ToCheck = new List <Cl3DModel.Cl3DModelPointIterator>();
                ToCheck.Add(test);
                test.AlreadyVisited = true;
                do
                {
                    List <Cl3DModel.Cl3DModelPointIterator> NewToCheck = new List <Cl3DModel.Cl3DModelPointIterator>();
                    foreach (Cl3DModel.Cl3DModelPointIterator pt in ToCheck)
                    {
                        CurrentList.Add(pt.CopyIterator());
                        if (pt.Y > NoseTip.Y)
                        {
                            toRemove = true;
                        }

                        foreach (Cl3DModel.Cl3DModelPointIterator ptNb in pt.GetListOfNeighbors())
                        {
                            if (ptNb.GetListOfNeighbors().Count != 8 && ptNb.AlreadyVisited == false)
                            {
                                ptNb.AlreadyVisited = true;
                                NewToCheck.Add(ptNb.CopyIterator());
                                pointsLowerBoundary.Remove(ptNb.PointID);
                            }
                        }
                    }
                    ToCheck    = NewToCheck;
                    NewToCheck = new List <Cl3DModel.Cl3DModelPointIterator>();
                } while (ToCheck.Count != 0);
                if (toRemove)
                {
                    ListOfBoundarie.Remove(CurrentList);
                }
            }

            List <Cl3DModel.Cl3DModelPointIterator> BiggestBoundary = null;
            int SizeOfTheBiggest = int.MinValue;

            for (int i = 0; i < ListOfBoundarie.Count; i++)
            {
                if (ListOfBoundarie[i].Count > SizeOfTheBiggest)
                {
                    SizeOfTheBiggest = ListOfBoundarie[i].Count;
                    BiggestBoundary  = ListOfBoundarie[i];
                }
            }

            if (BiggestBoundary == null)
            {
                ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(NoseTip, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip.ToString());
                iter = p_Model.GetIterator();
                while (iter.IsValid())
                {
                    if (!iter.IsSpecificValueCalculated(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip))
                    {
                        iter = p_Model.RemovePointFromModel(iter);
                    }
                    else
                    {
                        double distance = iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip);
                        if (distance > m_fDistence)
                        {
                            iter = p_Model.RemovePointFromModel(iter);
                        }
                        else
                        {
                            iter.MoveToNext();
                        }
                    }
                }
                return;
            }

            Cl3DModel.Cl3DModelPointIterator MaxLeft  = BiggestBoundary[0];
            Cl3DModel.Cl3DModelPointIterator MaxRight = BiggestBoundary[0];
            foreach (Cl3DModel.Cl3DModelPointIterator pt in BiggestBoundary)
            {
                if (pt.X < MaxLeft.X)
                {
                    MaxLeft = pt;
                }
                if (pt.X > MaxRight.X)
                {
                    MaxRight = pt;
                }
            }

            int size = (int)Math.Abs(MaxLeft.X - MaxRight.X) + 1;

            List <Cl3DModel.Cl3DModelPointIterator>[] histogram = new List <Cl3DModel.Cl3DModelPointIterator> [size];

            foreach (Cl3DModel.Cl3DModelPointIterator pt in BiggestBoundary)
            {
                int pos = (int)(pt.X - MaxLeft.X);
                if (histogram[pos] == null)
                {
                    histogram[pos] = new List <Cl3DModel.Cl3DModelPointIterator>();
                }

                histogram[pos].Add(pt);
            }

            Dictionary <uint, uint> movingPoints = new Dictionary <uint, uint>();

            for (int i = 0; i < size; i++)
            {
                if (histogram[i] != null && histogram[i].Count != 0)
                {
                    //Color cl = ClTools.GetColorRGB(((float)i) / size, 1.0f);
                    Cl3DModel.Cl3DModelPointIterator UpperPoint = histogram[i][0];
                    Cl3DModel.Cl3DModelPointIterator LowerPoint = histogram[i][0];
                    foreach (Cl3DModel.Cl3DModelPointIterator pts in histogram[i])
                    {
                        //  pts.Color = cl;
                        if (UpperPoint.Y < pts.Y)
                        {
                            UpperPoint = pts;
                        }
                        if (LowerPoint.Y > pts.Y)
                        {
                            LowerPoint = pts;
                        }
                    }
                    //UpperPoint from this one
                    if (UpperPoint.PointID != LowerPoint.PointID)
                    {
                        float distance = Math.Min(LowerPoint - MaxLeft, LowerPoint - MaxRight);
                        List <Cl3DModel.Cl3DModelPointIterator> neighborhood = null;
                        ClTools.GetNeighborhoodWithGeodesicDistance(out neighborhood, LowerPoint, distance);
                        Cl3DModel.Cl3DModelPointIterator ClosestPoint = LowerPoint;
                        float MinDistance = LowerPoint - UpperPoint;
                        foreach (Cl3DModel.Cl3DModelPointIterator ptNeighb in neighborhood)
                        {
                            // ptNeighb.Color = Color.Pink;
                            float newDistance = ptNeighb - UpperPoint;
                            if (newDistance < MinDistance)
                            {
                                MinDistance  = newDistance;
                                ClosestPoint = ptNeighb;
                            }
                        }
                        Color cl = ClTools.GetColorRGB(((float)i) / size, 1.0f);
                        ClosestPoint.Color = cl;
                        UpperPoint.Color   = cl;
                        movingPoints.Add(UpperPoint.PointID, ClosestPoint.PointID);
                    }
                }
            }
            p_Model.ResetVisitedPoints();
            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPointsWithMovement(NoseTip, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip.ToString(), movingPoints);

            iter = p_Model.GetIterator();
            while (iter.IsValid())
            {
                if (!iter.IsSpecificValueCalculated(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip))
                {
                    iter = p_Model.RemovePointFromModel(iter);
                }
                else
                {
                    double distance = iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip);
                    if (distance > m_fDistence)
                    {
                        iter = p_Model.RemovePointFromModel(iter);
                    }
                    else
                    {
                        iter.MoveToNext();
                    }
                }
            }
            return;

            //------------------------ PCA ----------------------

            /*
             * float MeanX = 0;
             * float MeanY = 0;
             * float MeanZ = 0;
             * Matrix xy = new Matrix(3, BiggestBoundary.Count);
             * for (int i = 0; i < BiggestBoundary.Count; i++)
             * {
             *  xy[0,i] = BiggestBoundary[i].X;
             *  xy[1,i] = BiggestBoundary[i].Y
             *  xy[2, i] = BiggestBoundary[i].Z;
             *
             *  MeanX += BiggestBoundary[i].X;
             *  MeanY += BiggestBoundary[i].Y;
             *  MeanZ += BiggestBoundary[i].Z;
             *
             *  BiggestBoundary[i].Color = Color.Red;
             * }
             * MeanX /= BiggestBoundary.Count;
             * MeanY /= BiggestBoundary.Count;
             * MeanZ /= BiggestBoundary.Count;
             *
             * for (int i = 0; i < BiggestBoundary.Count; i++)
             * {
             *  xy[0, i] -= MeanX;
             *  xy[1, i] -= MeanY;
             *  xy[2, i] -= MeanZ;
             * }
             *
             *
             * Matrix Transpose = xy.Clone();
             * Transpose.Transpose();
             * Matrix Corelation = xy * Transpose;
             *
             * SingularValueDecomposition SVD = new SingularValueDecomposition(Corelation);
             *
             * Cl3DModel.Cl3DModelPointIterator point = p_Model.AddPointToModel(MeanX, MeanY, MeanZ);
             * Cl3DModel.Cl3DModelPointIterator point2 = p_Model.AddPointToModel(MeanX + (float)SVD.LeftSingularVectors[0, 0] * 10, MeanY + (float)SVD.LeftSingularVectors[1, 0] * 10, MeanZ + (float)SVD.LeftSingularVectors[2, 0] * 10);
             * Cl3DModel.Cl3DModelPointIterator point3 = p_Model.AddPointToModel(MeanX + (float)SVD.LeftSingularVectors[0, 1] * 10, MeanY + (float)SVD.LeftSingularVectors[1, 1] * 10, MeanZ + (float)SVD.LeftSingularVectors[2, 1] * 10);
             * Cl3DModel.Cl3DModelPointIterator point4 = p_Model.AddPointToModel(MeanX + (float)SVD.LeftSingularVectors[0, 2] * 10, MeanY + (float)SVD.LeftSingularVectors[1, 2] * 10, MeanZ + (float)SVD.LeftSingularVectors[2, 2] * 10);
             * point2.Color = Color.Red;
             * point3.Color = Color.Green;
             * point4.Color = Color.Blue;
             * point.AddNeighbor(point2);
             * point.AddNeighbor(point3);
             * point.AddNeighbor(point4);
             */
            //---------------------------------------------------------------------------
        }
Пример #14
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            // if (p_Model.ModelType != "abs")
            //   throw new Exception("Saveing to .obj files works only for ABS files");

            string name       = p_Model.ModelFileFolder + p_Model.ModelFileName + ".m";
            string namePoints = p_Model.ModelFileFolder + p_Model.ModelFileName + ".m";

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            if (!iter.IsValid())
            {
                throw new Exception("Iterator in the model is not valid");
            }

            using (TextWriter tw = new StreamWriter(name, false))
            {
                do
                {
                    uint ID = iter.PointID + 1;

                    int R = 0;
                    R = (R << 8) + (int)iter.Color.R;
                    int G = 0;
                    G = (G << 8) + (int)iter.Color.G;
                    int B = 0;
                    B = (B << 8) + (int)iter.Color.B;

                    string line = "Vertex " + ID.ToString(System.Globalization.CultureInfo.InvariantCulture) + "  "
                                  + iter.X.ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + iter.Y.ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + iter.Z.ToString(System.Globalization.CultureInfo.InvariantCulture) + " {rgb=("
                                  + ((float)R / 255.0f).ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + ((float)G / 255.0f).ToString(System.Globalization.CultureInfo.InvariantCulture) + " "
                                  + ((float)B / 255.0f).ToString(System.Globalization.CultureInfo.InvariantCulture) + ")}";

                    tw.WriteLine(line);
                } while (iter.MoveToNext());

                // --- create faces
                iter = p_Model.GetIterator();
                uint FaceNO = 1;
                do
                {
                    iter.AlreadyVisited = true;

                    List <Cl3DModel.Cl3DModelPointIterator> neighbors = iter.GetListOfNeighbors();

                    uint MainPointID = iter.PointID + 1;

                    uint point1           = 0;
                    bool point1Founded    = false;
                    uint point2           = 0;
                    bool point2Founded    = false;
                    uint point3           = 0;
                    bool point3Founded    = false;
                    uint point4Exc        = 0;
                    bool point4ExcFounded = false;
                    uint point5Exc        = 0;
                    bool point5ExcFounded = false;
                    uint point6Exc        = 0;
                    bool point6ExcFounded = false;

                    foreach (Cl3DModel.Cl3DModelPointIterator point in neighbors)
                    {
                        if (point.RangeImageY == iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // first point
                        {
                            point1        = point.PointID + 1;
                            point1Founded = true;
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // second point
                        {
                            point2        = point.PointID + 1;
                            point2Founded = true;
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX == iter.RangeImageX) // third point
                        {
                            point3        = point.PointID + 1;
                            point3Founded = true;
                        }
                        if (point.RangeImageY < iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // second point
                        {
                            point4Exc        = point.PointID + 1;
                            point4ExcFounded = true;
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX < iter.RangeImageX) // second point
                        {
                            point5Exc        = point.PointID + 1;
                            point5ExcFounded = true;
                        }
                        if (point.RangeImageY < iter.RangeImageY && point.RangeImageX == iter.RangeImageX) // second point
                        {
                            point6Exc        = point.PointID + 1;
                            point6ExcFounded = true;
                        }
                    }

                    string line = "";
                    if (point1Founded && point2Founded)
                    {
                        line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        FaceNO++;
                        if (point3Founded)
                        {
                            line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                            FaceNO++;
                        }
                        if (!point6ExcFounded && point4ExcFounded)
                        {
                            line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point4Exc.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                            FaceNO++;
                        }
                    }
                    else if (point1Founded && point3Founded)
                    {
                        line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        FaceNO++;
                    }
                    else if (point2Founded && point3Founded)
                    {
                        line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        FaceNO++;
                    }

                    /*else if (point1Founded && !point2Founded && !point3Founded && point4ExcFounded)
                     * {
                     *  line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point4Exc.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                     *  FaceNO++;
                     *
                     * }
                     */
                    /*else if (!point1Founded && !point2Founded && point3Founded && point5ExcFounded)
                     * {
                     *  line += "Face " + FaceNO.ToString() + "  " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                     *                  point5Exc.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                     *  FaceNO++;
                     *
                     * }
                     */
                    tw.Write(line);
                } while (iter.MoveToNext());
                tw.Close();
            }
        }
        void SaveModel(Cl3DModel p_Model)
        {
            string name = p_Model.ModelFileFolder + paramFileName + ".tmp";

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            using (TextWriter tw = new StreamWriter(name, false))
            {
                uint vertexNO = 1;
                Dictionary <uint, uint> MapVertexNo = new Dictionary <uint, uint>();
                do
                {
                    string line = "v " + iter.X.ToString(System.Globalization.CultureInfo.InvariantCulture) +
                                  " " + iter.Y.ToString(System.Globalization.CultureInfo.InvariantCulture) +
                                  " " + iter.Z.ToString(System.Globalization.CultureInfo.InvariantCulture) +
                                  " " + iter.PointID.ToString(System.Globalization.CultureInfo.InvariantCulture);

                    tw.WriteLine(line);

                    MapVertexNo.Add(iter.PointID, vertexNO++);
                } while (iter.MoveToNext());

                // --- create faces
                iter = p_Model.GetIterator();
                do
                {
                    iter.AlreadyVisited = true;

                    List <Cl3DModel.Cl3DModelPointIterator> neighbors = iter.GetListOfNeighbors();

                    uint MainPointID;
                    if (!MapVertexNo.TryGetValue(iter.PointID, out MainPointID))
                    {
                        throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                    }

                    uint point1 = 0;
                    uint point2 = 0;
                    uint point3 = 0;

                    foreach (Cl3DModel.Cl3DModelPointIterator point in neighbors)
                    {
                        //if (point.AlreadyVisited)
                        //    continue;

                        if (point.RangeImageY == iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // first point
                        {
                            if (!MapVertexNo.TryGetValue(point.PointID, out point1))
                            {
                                throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                            }
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX > iter.RangeImageX) // second point
                        {
                            if (!MapVertexNo.TryGetValue(point.PointID, out point2))
                            {
                                throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                            }
                        }
                        if (point.RangeImageY > iter.RangeImageY && point.RangeImageX == iter.RangeImageX) // third point
                        {
                            if (!MapVertexNo.TryGetValue(point.PointID, out point3))
                            {
                                throw new Exception("Cannot find point ID in the ID dictionary: " + iter.PointID);
                            }
                        }
                    }

                    string line = "";
                    if (point1 != 0 && point2 != 0)
                    {
                        line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        if (point3 != 0)
                        {
                            line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                    point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                        }
                    }
                    else if (point1 != 0 && point3 != 0)
                    {
                        line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                    }
                    else if (point2 != 0 && point3 != 0)
                    {
                        line += "f " + MainPointID.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point3.ToString(System.Globalization.CultureInfo.InvariantCulture) + " " +
                                point2.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                    }

                    tw.Write(line);
                } while (iter.MoveToNext());
                tw.Close();
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            // if (!(p_Model.ModelType == "abs" || p_Model.ModelType == "binaryModel" || p_Model.ModelType == "model"))
            //     throw new Exception("Remove Holes based on Range Image works only for abs, binaryModel and model files");

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            if (!iter.IsValid())
            {
                throw new Exception("Model iterator not Valid");
            }

            int MinX;
            int MaxX;
            int MinY;
            int MaxY;

            MinX = iter.RangeImageX;
            MaxX = iter.RangeImageX;
            MinY = iter.RangeImageY;
            MaxY = iter.RangeImageY;

            do
            {
                if (MinX > iter.RangeImageX)
                {
                    MinX = iter.RangeImageX;
                }
                if (MaxX < iter.RangeImageX)
                {
                    MaxX = iter.RangeImageX;
                }

                if (MinY > iter.RangeImageY)
                {
                    MinY = iter.RangeImageY;
                }
                if (MaxY < iter.RangeImageY)
                {
                    MaxY = iter.RangeImageY;
                }
            } while (iter.MoveToNext());

            int Width  = (MaxX - MinX) + 1;
            int Height = (MaxY - MinY) + 1;

            Cl3DModel.Cl3DModelPointIterator[,] map = new Cl3DModel.Cl3DModelPointIterator[Width, Height];

            iter = p_Model.GetIterator();
            do
            {
                map[iter.RangeImageX - MinX, iter.RangeImageY - MinY] = iter.CopyIterator();
                foreach (Cl3DModel.Cl3DModelPointIterator pts in iter.GetListOfNeighbors())
                {
                    iter.RemoveNeighbor(pts);
                }
            } while (iter.MoveToNext());

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    if (map[x, y] == null)
                    {
                        continue;
                    }

                    if (x - 1 >= 0 && y + 1 < Height && map[x - 1, y + 1] != null)
                    {
                        map[x, y].AddNeighbor(map[x - 1, y + 1]);
                    }

                    if (x - 1 >= 0 && map[x - 1, y] != null)
                    {
                        map[x, y].AddNeighbor(map[x - 1, y]);
                    }

                    if (x - 1 >= 0 && y - 1 >= 0 && map[x - 1, y - 1] != null)
                    {
                        map[x, y].AddNeighbor(map[x - 1, y - 1]);
                    }

                    if (y - 1 >= 0 && map[x, y - 1] != null)
                    {
                        map[x, y].AddNeighbor(map[x, y - 1]);
                    }

                    if (x + 1 < Width && y - 1 >= 0 && map[x + 1, y - 1] != null)
                    {
                        map[x, y].AddNeighbor(map[x + 1, y - 1]);
                    }

                    if (x + 1 < Width && map[x + 1, y] != null)
                    {
                        map[x, y].AddNeighbor(map[x + 1, y]);
                    }

                    if (x + 1 < Width && y + 1 < Height && map[x + 1, y + 1] != null)
                    {
                        map[x, y].AddNeighbor(map[x + 1, y + 1]);
                    }

                    if (y + 1 < Height && map[x, y + 1] != null)
                    {
                        map[x, y].AddNeighbor(map[x, y + 1]);
                    }
                }
            }
        }