Example #1
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator LeftEyeRightCornerPoint = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.LeftEyeRightCorner);
            Cl3DModel.Cl3DModelPointIterator RightEyeLeftCornerPoint = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.RightEyeLeftCorner);

            CalculateDistancesFromPoint("UV_LeftEyeRightCornerDistance", LeftEyeRightCornerPoint.U, LeftEyeRightCornerPoint.V, p_Model);
            CalculateDistancesFromPoint("UV_RightEyeLeftCornerDistance", RightEyeLeftCornerPoint.U, RightEyeLeftCornerPoint.V, p_Model);
            CalculateDistancesFromPoint("UV_NoseTipDistance", 0.0f, 0.0f, p_Model);

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            do
            {
                double UV_LeftEyeRightCornerDistance = 0;
                iter.GetSpecificValue("UV_LeftEyeRightCornerDistance", out UV_LeftEyeRightCornerDistance);

                double UV_RightEyeLeftCornerDistance = 0;
                iter.GetSpecificValue("UV_RightEyeLeftCornerDistance", out UV_RightEyeLeftCornerDistance);

                double UV_NoseTipDistance = 0;
                iter.GetSpecificValue("UV_NoseTipDistance", out UV_NoseTipDistance);

                double alldistances = Math.Sqrt(Math.Pow(UV_LeftEyeRightCornerDistance, 2) + Math.Pow(UV_RightEyeLeftCornerDistance, 2) + Math.Pow(UV_NoseTipDistance, 2));
                iter.AddSpecificValue("AllDistancesUV", alldistances);
            } while (iter.MoveToNext());
        }
Example #2
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            if (iter.IsValid())
            {
                do
                {
                    double H;
                    double K;
                    double ShapeIndex;

                    if (!iter.GetSpecificValue("Gaussian_25", out K))
                    {
                        continue;
                    }
                    if (!iter.GetSpecificValue("Mean_25", out H))
                    {
                        continue;
                    }

                    if (H < 0 && K > m_ThresholdK) //  Nose
                    {
                        iter.Color = Color.Green;
                    }
                }while (iter.MoveToNext());
            }
        }
Example #3
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator nose = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip.ToString());

            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(nose, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip.ToString());

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            do
            {
                double distance = 0;
                if (iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip, out distance))
                {
                    if (distance > m_fDistence)
                    {
                        iter = p_Model.RemovePointFromModel(iter);
                    }
                    else
                    {
                        if (!iter.MoveToNext())
                        {
                            break;
                        }
                    }
                }
                else
                {
                    iter = p_Model.RemovePointFromModel(iter);
                }
            }while(iter.IsValid());
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            string name = p_Model.ModelFileFolder + p_Model.ModelFileName + ".curv";

            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 curvatures");
                tw.WriteLine("@  Model name: " + p_Model.ModelFileName);
                tw.WriteLine("@----------------------------------------");
                tw.WriteLine("@ (PointID) X Y Z");
                tw.WriteLine("@ \tCurvatureName: value");
                do
                {
                    List <String> SpecValList = iter.GetListOfSpecificValues();
                    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) + "\n";
                    foreach (String SPV in SpecValList)
                    {
                        double val;
                        iter.GetSpecificValue(SPV, out val);
                        line += "\t" + SPV + ": " + val.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
                    }
                    tw.WriteLine(line);
                } while (iter.MoveToNext());

                tw.Close();
            }
        }
Example #5
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            string k1 = "K1_" + NeighborhoodSize.ToString();
            string k2 = "K2_" + NeighborhoodSize.ToString();
            string CurvednessIndex = "CurvednessIndex_" + NeighborhoodSize.ToString();

            do
            {
                double vK1, vK2;
                if (!iter.GetSpecificValue(k1, out vK1) || !iter.GetSpecificValue(k2, out vK2))
                {
                    continue;
                }

                double CurvednessIndexValue = Math.Sqrt(Math.Pow(vK1, 2) + Math.Pow(vK2, 2)) / 2;
                iter.AddSpecificValue("CurvednessIndex_" + NeighborhoodSize.ToString(), CurvednessIndexValue);
            } while (iter.MoveToNext());
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator LeftEyeRightCornerPoint = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.LeftEyeRightCorner);
            Cl3DModel.Cl3DModelPointIterator RightEyeLeftCornerPoint = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.RightEyeLeftCorner);
            Cl3DModel.Cl3DModelPointIterator NoseTipPoint            = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip);

            if (m_bEuclidean)
            {
                CalculateEuclideanDistancesFromPoint("XYZ_LeftEyeRightCornerDistance", LeftEyeRightCornerPoint, p_Model);
                CalculateEuclideanDistancesFromPoint("XYZ_RightEyeLeftCornerDistance", RightEyeLeftCornerPoint, p_Model);
                CalculateEuclideanDistancesFromPoint("XYZ_NoseTipDistance", NoseTipPoint, p_Model);
            }
            else
            {
                Cl3DModel.Cl3DModelPointIterator iterRmoeve = p_Model.GetIterator();
                do
                {
                    iterRmoeve.RemoveSpecificValue("XYZ_LeftEyeRightCornerDistance");
                    iterRmoeve.RemoveSpecificValue("XYZ_RightEyeLeftCornerDistance");
                    iterRmoeve.RemoveSpecificValue("XYZ_NoseTipDistance");
                } while (iterRmoeve.MoveToNext());
                CalculateGeodesicDistancesFromPoint("XYZ_LeftEyeRightCornerDistance", LeftEyeRightCornerPoint, p_Model);
                CalculateGeodesicDistancesFromPoint("XYZ_RightEyeLeftCornerDistance", RightEyeLeftCornerPoint, p_Model);
                CalculateGeodesicDistancesFromPoint("XYZ_NoseTipDistance", NoseTipPoint, p_Model);
            }

            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            do
            {
                double XYZ_LeftEyeRightCornerDistance = 0;
                iter.GetSpecificValue("XYZ_LeftEyeRightCornerDistance", out XYZ_LeftEyeRightCornerDistance);

                double XYZ_RightEyeLeftCornerDistance = 0;
                iter.GetSpecificValue("XYZ_RightEyeLeftCornerDistance", out XYZ_RightEyeLeftCornerDistance);

                double XYZ_NoseTipDistance = 0;
                iter.GetSpecificValue("XYZ_NoseTipDistance", out XYZ_NoseTipDistance);

                double alldistances = Math.Sqrt(Math.Pow(XYZ_LeftEyeRightCornerDistance, 2) + Math.Pow(XYZ_RightEyeLeftCornerDistance, 2) + Math.Pow(XYZ_NoseTipDistance, 2));
                iter.AddSpecificValue("AllDistancesXYZ", alldistances);
            } while (iter.MoveToNext());
        }
Example #7
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();
            do
            {
                double value = 0;
                if (!iter.GetSpecificValue(m_SpecificValue, out value))
                {
                    iter.MoveToNext();
                    continue;
                }

                if (value > m_ThresholdValue)
                {
                    iter = p_Model.RemovePointFromModel(iter);
                }
                else
                {
                    iter.MoveToNext();
                }
            } while (iter.IsValid());
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            if (iter.IsValid())
            {
                do
                {
                    double H;
                    double K;

                    if (!iter.GetSpecificValue("Gaussian_" + NeighborhoodSize, out K))
                    {
                        continue;
                    }
                    if (!iter.GetSpecificValue("Mean_" + NeighborhoodSize, out H))
                    {
                        continue;
                    }

                    if (H < 0 && K < 0)                         // Saddle ridge
                    {
                        iter.Color = Color.FromArgb(0, 0, 255); //Blue;
                    }
                    else if (H == 0 && K < 0)                   // Minimal Surface
                    {
                        iter.Color = Color.Orange;
                    }
                    else if (H > 0 && K < 0)                      // Saddle Valley
                    {
                        iter.Color = Color.FromArgb(255, 255, 0); //Yellow;
                    }
                    else if (H < 0 && K == 0)                     // Ridge, Cylindrical convex (wypukłość)
                    {
                        iter.Color = Color.FromArgb(0, 255, 255); //SkyBlue;
                    }
                    else if (H == 0 && K == 0)                    // Plane
                    {
                        iter.Color = Color.FromArgb(255, 255, 255);
                    }
                    else if (H > 0 && K == 0)                     // Valley, Cylindrical concave (wklęsłość)
                    {
                        iter.Color = Color.FromArgb(255, 0, 255); //Pink;
                    }
                    else if (H < 0 && K > 0)                      // Peak, Elliptical convex (wypukłość)
                    {
                        iter.Color = Color.FromArgb(0, 255, 0);   //Green;
                    }
                    else if (H == 0 && K > 0)                     // !!! Impossible
                    {
                        iter.Color = Color.Black;
                    }
                    else if (H > 0 && K > 0)                    // Pit, Elliptical concave (wklęsłość)
                    {
                        iter.Color = Color.FromArgb(255, 0, 0); //Red;
                    }
                    else
                    {
                        iter.Color = Color.White;
                    }
                }while (iter.MoveToNext());
            }
        }
        private void MarkHighCurvEdges(ref Cl3DModel Model)
        {
            Cl3DModel.Cl3DModelPointIterator NoseTip  = Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip);
            Cl3DModel.Cl3DModelPointIterator LeftEye  = Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.LeftEyeRightCorner);
            Cl3DModel.Cl3DModelPointIterator RightEye = Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.RightEyeLeftCorner);

            float diistance = (float)Math.Sqrt(Math.Pow(NoseTip - LeftEye, 2) + Math.Pow(NoseTip - RightEye, 2) + Math.Pow(LeftEye - RightEye, 2)) * 1.15f;

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

            Cl3DModel.Cl3DModelPointIterator iter = Model.GetIterator();
            do
            {
                float dist = (float)Math.Sqrt(Math.Pow((float)(iter - NoseTip), 2) + Math.Pow((float)(iter - LeftEye), 2) + Math.Pow((float)(iter - RightEye), 2));
                if (dist < diistance)
                {
                    pointsIn.Add(iter.CopyIterator());
                }
            } while (iter.MoveToNext());

            double MaxK1_10 = double.MinValue;
            double MinK1_10 = double.MaxValue;

            double MaxK1_15 = double.MinValue;
            double MinK1_15 = double.MaxValue;

            double MaxK1_20 = double.MinValue;
            double MinK1_20 = double.MaxValue;

            double MaxK1_25 = double.MinValue;
            double MinK1_25 = double.MaxValue;

            double MaxK1_55 = double.MinValue;
            double MinK1_40 = double.MaxValue;

            foreach (Cl3DModel.Cl3DModelPointIterator PointIn in pointsIn)
            {
                double valK1_10;
                if (!PointIn.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_10, out valK1_10))
                {
                    ClInformationSender.SendInformation("K1 10 unavailable for the point " + PointIn.PointID.ToString(), ClInformationSender.eInformationType.eDebugText);
                    continue;
                }

                double valK1_15;
                if (!PointIn.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_15, out valK1_15))
                {
                    ClInformationSender.SendInformation("K1 15 unavailable for the point " + PointIn.PointID.ToString(), ClInformationSender.eInformationType.eDebugText);
                    continue;
                }

                double valK1_20;
                if (!PointIn.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_20, out valK1_20))
                {
                    ClInformationSender.SendInformation("K1 20 unavailable for the point " + PointIn.PointID.ToString(), ClInformationSender.eInformationType.eDebugText);
                    continue;
                }

                double valK1_25;
                if (!PointIn.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_25, out valK1_25))
                {
                    ClInformationSender.SendInformation("K1 25 unavailable for the point " + PointIn.PointID.ToString(), ClInformationSender.eInformationType.eDebugText);
                    continue;
                }

                double valK1_40;
                if (!PointIn.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_40, out valK1_40))
                {
                    ClInformationSender.SendInformation("K1 40 unavailable for the point " + PointIn.PointID.ToString(), ClInformationSender.eInformationType.eDebugText);
                    continue;
                }

                if (MinK1_10 > valK1_10)
                {
                    MinK1_10 = valK1_10;
                }
                if (MaxK1_10 < valK1_10)
                {
                    MaxK1_10 = valK1_10;
                }

                if (MinK1_15 > valK1_15)
                {
                    MinK1_15 = valK1_15;
                }
                if (MaxK1_15 < valK1_15)
                {
                    MaxK1_15 = valK1_15;
                }

                if (MinK1_20 > valK1_20)
                {
                    MinK1_20 = valK1_20;
                }
                if (MaxK1_20 < valK1_20)
                {
                    MaxK1_20 = valK1_20;
                }

                if (MinK1_25 > valK1_25)
                {
                    MinK1_25 = valK1_25;
                }
                if (MaxK1_25 < valK1_25)
                {
                    MaxK1_25 = valK1_25;
                }

                if (MinK1_40 > valK1_40)
                {
                    MinK1_40 = valK1_40;
                }
                if (MaxK1_55 < valK1_40)
                {
                    MaxK1_55 = valK1_40;
                }
            }

            iter = Model.GetIterator();
            do
            {
                double valK1_10;
                if (!iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_10, out valK1_10))
                {
                    continue;
                }

                double valK1_15;
                if (!iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_15, out valK1_15))
                {
                    continue;
                }

                double valK1_20;
                if (!iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_20, out valK1_20))
                {
                    continue;
                }

                double valK1_25;
                if (!iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_25, out valK1_25))
                {
                    continue;
                }

                double valK1_40;
                if (!iter.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_40, out valK1_40))
                {
                    continue;
                }

                if (valK1_10 < MinK1_10 || valK1_10 > MaxK1_10 ||
                    valK1_15 < MinK1_15 || valK1_15 > MaxK1_15 ||
                    valK1_20 < MinK1_20 || valK1_20 > MaxK1_20 ||
                    valK1_25 < MinK1_25 || valK1_25 > MaxK1_25 ||
                    valK1_40 < MinK1_40 || valK1_40 > MaxK1_55
                    )
                {
                    // iter.Color = Color.LightBlue;
                    iter.AddSpecificValue("ToRemove_HighCurvature", 1.0f);
                }
            } while (iter.MoveToNext());
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            MarkHighCurvEdges(ref p_Model);

            p_Model.ResetVisitedPoints();
            //-------------------------------- Cropping
            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> pointsToRemove = 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 - 43)
                {
                    // iter.Color = Color.LightBlue;
                    if (iter.IsSpecificValueCalculated("ToRemove_HighCurvature"))
                    {
                        pointsToRemove.Add(iter.PointID, iter.CopyIterator());
                    }
                }
            } while (iter.MoveToNext());

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

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

                bool toRemove = false;
                List <Cl3DModel.Cl3DModelPointIterator> CurrentList = new List <Cl3DModel.Cl3DModelPointIterator>();
                ListOfRegions.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());

                        foreach (Cl3DModel.Cl3DModelPointIterator ptNb in pt.GetListOfNeighbors())
                        {
                            if (ptNb.IsSpecificValueCalculated("ToRemove_HighCurvature") && ptNb.AlreadyVisited == false)
                            {
                                ptNb.AlreadyVisited = true;
                                NewToCheck.Add(ptNb.CopyIterator());
                                pointsToRemove.Remove(ptNb.PointID);
                                List <Cl3DModel.Cl3DModelPointIterator> Neighbors = ptNb.GetListOfNeighbors();

                                //   if (Neighbors.Count < 8) // that means the points from marked region went to the end of the model very rare happens that the open mouth region is the externatl region
                                //       toRemove = true;
                            }
                        }
                    }
                    ToCheck    = NewToCheck;
                    NewToCheck = new List <Cl3DModel.Cl3DModelPointIterator>();
                } while (ToCheck.Count != 0);
                if (toRemove)
                {
                    ListOfRegions.Remove(CurrentList);
                }
            }

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

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

            //------------------- Creating image of localized mouth part

            /*   int NoseX = NoseTip.RangeImageX;
             * int NoseY = NoseTip.RangeImageY;
             * //BiggestRegion
             * int[,] FMap = null;
             * if (!FaceMaps.TryGetValue(p_Model.m_sExpression, out FMap))
             * {
             *     FMap = new int[200, 200];
             *     FaceMaps.Add(p_Model.m_sExpression, FMap);
             * }
             * Cl3DModel.Cl3DModelPointIterator its = p_Model.GetIterator();
             * do
             * {
             *     int currX = its.RangeImageX;
             *     int currY = its.RangeImageY;
             *
             *
             *     int x = currX - NoseX + 100;
             *     int y = currY - NoseY + 100;
             *
             *     FMap[x, y]++;
             * } while (its.MoveToNext());
             *
             * int MaxF = int.MinValue;
             * for (int i = 0; i < 200; i++)
             *     for(int j=0; j< 200; j++)
             *         if (FMap[i, j] > MaxF)
             *             MaxF = FMap[i, j];
             *
             *
             * //-
             * int[,] FRem = null;
             * if (!RemovedMaps.TryGetValue(p_Model.m_sExpression, out FRem))
             * {
             *     FRem = new int[200, 200];
             *     RemovedMaps.Add(p_Model.m_sExpression, FRem);
             * }
             *
             * if (BiggestRegion != null)
             * {
             *     foreach (Cl3DModel.Cl3DModelPointIterator pp in BiggestRegion)
             *     {
             *         int currX = pp.RangeImageX;
             *         int currY = pp.RangeImageY;
             *
             *
             *         int x = currX - NoseX + 100;
             *         int y = currY - NoseY + 100;
             *
             *         FRem[x, y]++;
             *     }
             * }
             *
             * FRem[NoseTip.RangeImageX - NoseX + 100, NoseTip.RangeImageY - NoseY + 100]++;
             * //  FRem[LeftEye.RangeImageX - NoseX + 100, LeftEye.RangeImageY - NoseY + 100]++;
             * //  FRem[RightEye.RangeImageX - NoseX + 100, RightEye.RangeImageY - NoseY + 100]++;
             *
             *
             * int MaxR = int.MinValue;
             * for (int i = 0; i < 200; i++)
             *     for (int j = 0; j < 200; j++)
             *         if (FRem[i, j] > MaxR)
             *             MaxR = FRem[i, j];
             *
             *
             *
             *
             *
             *
             *
             *
             *
             * Bitmap map = new Bitmap(200, 200);
             * for (int i = 0; i < 200; i++)
             * {
             *     for (int j = 0; j < 200; j++)
             *     {
             *         map.SetPixel(i, j, ClTools.GetColorGray(((float)FMap[i, j]) / MaxF, 1.0f));
             *         if(FRem[i, j] != 0)
             *             map.SetPixel(i, j, ClTools.GetColorGray( 1 - ((float)FRem[i, j]) / MaxR, 1.0f));
             *     }
             * }
             * map.Save("d:\\" + p_Model.m_sExpression + ".bmp");
             * return;
             */
            //------------------------------------------------------------

            /*  TextWriter tw = new StreamWriter("d:\\MouthSizeBosphorus2.txt", false);
             *
             *
             * int ssize = 0;
             * if (BiggestRegion != null)
             *    ssize = BiggestRegion.Count;
             *
             * int[] OldSize;
             * if(!mouthSize.TryGetValue(p_Model.m_sExpression, out OldSize))
             * {
             *    OldSize = new int[2];
             *    mouthSize.Add(p_Model.m_sExpression, OldSize);
             * }
             * OldSize[0] += ssize;
             * OldSize[1]++;
             *
             * foreach(KeyValuePair<string, int[]> val in mouthSize)
             *    tw.WriteLine(val.Key+" "+ val.Value[0].ToString()+ " " + val.Value[1].ToString());
             *
             * tw.Close();
             *
             * return;
             */
            //----------------------------------------------------------------

            //---------------------- NO mouth REGION LOCALIZED
            if (BiggestRegion == null)
            {
                ClInformationSender.SendInformation("No mouth localized, normal Geodesic distance calculation", ClInformationSender.eInformationType.eDebugText);
                if (m_bRemoveRegions) // still there can be vertexes with high curvature (edges between hair and the face)
                {
                    Cl3DModel.Cl3DModelPointIterator remover = p_Model.GetIterator();
                    while (remover.IsValid())
                    {
                        if (remover.IsSpecificValueCalculated("ToRemove_HighCurvature"))
                        {
                            remover = p_Model.RemovePointFromModel(remover);
                        }
                        else
                        {
                            remover.MoveToNext();
                        }
                    }
                }

                ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(NoseTip, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip.ToString());

                if (!m_bCrop)
                {
                    return;
                }

                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;
            }

            //------------------------- Mouth REGION LOCALiZED
            p_Model.ResetVisitedPoints();
            List <Cl3DModel.Cl3DModelPointIterator> BorderOfTheBigestRegion = new List <Cl3DModel.Cl3DModelPointIterator>();

            foreach (Cl3DModel.Cl3DModelPointIterator pts in BiggestRegion)
            {
                if (m_bMarkRegion)
                {
                    pts.Color = Color.Red;
                }

                List <Cl3DModel.Cl3DModelPointIterator> neighb = pts.GetListOfNeighbors();
                foreach (Cl3DModel.Cl3DModelPointIterator ppt in neighb)
                {
                    if (!ppt.AlreadyVisited && !ppt.IsSpecificValueCalculated("ToRemove_HighCurvature"))
                    {
                        BorderOfTheBigestRegion.Add(ppt);
                        ppt.AlreadyVisited = true;
                    }
                }
            }

            if (m_bRemoveRegions) // After we have border of the mouth region we can remove them
            {
                Cl3DModel.Cl3DModelPointIterator remover = p_Model.GetIterator();
                while (remover.IsValid())
                {
                    if (remover.IsSpecificValueCalculated("ToRemove_HighCurvature"))
                    {
                        remover = p_Model.RemovePointFromModel(remover);
                    }
                    else
                    {
                        remover.MoveToNext();
                    }
                }
            }

            Cl3DModel.Cl3DModelPointIterator MaxLeft  = BorderOfTheBigestRegion[0];
            Cl3DModel.Cl3DModelPointIterator MaxRight = BorderOfTheBigestRegion[0];
            foreach (Cl3DModel.Cl3DModelPointIterator pt in BorderOfTheBigestRegion)
            {
                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 BorderOfTheBigestRegion)
            {
                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);
                    }
                }
            }

            //-------------------------------- Calculation of the geodesic using movement points
            p_Model.ResetVisitedPoints();
            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPointsWithMovement(NoseTip, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip.ToString(), movingPoints);

            if (!m_bCrop)
            {
                return;
            }

            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();
                    }
                }
            }
        }
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            if (p_Model.ModelFileName.Equals("Neutral"))
            {
                NeutralModel = p_Model;
            }
            else
            {
                ExpressionModel = p_Model;
            }

            if (NeutralModel == null || ExpressionModel == null)
            {
                return;
            }

            Cl3DModel.Cl3DModelPointIterator NeutralSavedNoseTip = null;
            Cl3DModel.Cl3DModelPointIterator NeutralLeftEye      = null;
            Cl3DModel.Cl3DModelPointIterator NeutralRightEye     = null;

            if (!NeutralModel.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip, ref NeutralSavedNoseTip) || !NeutralModel.GetSpecificPoint(Cl3DModel.eSpecificPoints.LeftEyeRightCorner, ref NeutralLeftEye) || !NeutralModel.GetSpecificPoint(Cl3DModel.eSpecificPoints.RightEyeLeftCorner, ref NeutralRightEye))
            {
                throw new Exception("Cannot get all specific points (Neutral model)");
            }

            Cl3DModel.Cl3DModelPointIterator ExpressionSavedNoseTip = null;
            Cl3DModel.Cl3DModelPointIterator ExpressionLeftEye      = null;
            Cl3DModel.Cl3DModelPointIterator ExpressionRightEye     = null;

            if (!ExpressionModel.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip, ref ExpressionSavedNoseTip) || !ExpressionModel.GetSpecificPoint(Cl3DModel.eSpecificPoints.LeftEyeRightCorner, ref ExpressionLeftEye) || !ExpressionModel.GetSpecificPoint(Cl3DModel.eSpecificPoints.RightEyeLeftCorner, ref ExpressionRightEye))
            {
                throw new Exception("Cannot get all specific points (Exprssion model)");
            }

            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(NeutralSavedNoseTip, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip.ToString());
            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(NeutralLeftEye, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToLeftEye.ToString());
            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(NeutralRightEye, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToRightEye.ToString());

            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(ExpressionSavedNoseTip, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip.ToString());
            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(ExpressionLeftEye, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToLeftEye.ToString());
            ClTools.CalculateGeodesicDistanceFromSourcePointToAllPoints(ExpressionRightEye, Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToRightEye.ToString());

            Cl3DModel.Cl3DModelPointIterator iterNeutral = NeutralModel.GetIterator();
            do
            {
                // search for the closest point in distance geodesic distance domain
                Cl3DModel.Cl3DModelPointIterator iterExpression = ExpressionModel.GetIterator();
                Cl3DModel.Cl3DModelPointIterator ClossestPoint  = null;
                double ClosestPointDistance = 0;
                bool   first = true;
                double NoseDistanceNeutral     = 0;
                double LeftEyeDistanceNeutral  = 0;
                double RightEyeDistanceNeutral = 0;

                if (!iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip, out NoseDistanceNeutral) || !iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToLeftEye, out LeftEyeDistanceNeutral) || !iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToRightEye, out RightEyeDistanceNeutral))
                {
                    throw new Exception("Cannot get specific value");
                }

                double NoseDistanceExpression;
                double LeftEyeDistanceExpression;
                double RightEyeDistanceExpression;

                do
                {
                    if (!iterExpression.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToNoseTip, out NoseDistanceExpression) || !iterExpression.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToLeftEye, out LeftEyeDistanceExpression) || !iterExpression.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.GeodesicDistanceToRightEye, out RightEyeDistanceExpression))
                    {
                        throw new Exception("Cannot get specific value");
                    }

                    double newClosestPointDistance = Math.Sqrt(Math.Pow(NoseDistanceNeutral - NoseDistanceExpression, 2) + Math.Pow(LeftEyeDistanceNeutral - LeftEyeDistanceExpression, 2) + Math.Pow(RightEyeDistanceNeutral - RightEyeDistanceExpression, 2));
                    if (first)
                    {
                        ClossestPoint        = iterExpression.CopyIterator();
                        ClosestPointDistance = newClosestPointDistance;
                        first = false;
                        continue;
                    }

                    if (ClosestPointDistance > newClosestPointDistance)
                    {
                        ClossestPoint        = iterExpression.CopyIterator();
                        ClosestPointDistance = newClosestPointDistance;
                    }
                } while (iterExpression.MoveToNext());


                double ValNatural;
                double ValExpression;

                iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.Mean_25, out ValNatural);
                ClossestPoint.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.Mean_25, out ValExpression);
                iterNeutral.AddSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.DifferenceBetween_HCurvatures_25, ValNatural - ValExpression);

                iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.Gaussian_25, out ValNatural);
                ClossestPoint.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.Gaussian_25, out ValExpression);
                iterNeutral.AddSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.DifferenceBetween_KCurvatures_25, ValNatural - ValExpression);

                iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_25, out ValNatural);
                ClossestPoint.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K1_25, out ValExpression);
                iterNeutral.AddSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.DifferenceBetween_K1Curvatures_25, ValNatural - ValExpression);

                iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K2_25, out ValNatural);
                ClossestPoint.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.K2_25, out ValExpression);
                iterNeutral.AddSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.DifferenceBetween_K2Curvatures_25, ValNatural - ValExpression);

                iterNeutral.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.ShapeIndex_25, out ValNatural);
                ClossestPoint.GetSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.ShapeIndex_25, out ValExpression);
                iterNeutral.AddSpecificValue(Cl3DModel.Cl3DModelPointIterator.eSpecificValues.DifferenceBetween_ShapeIndexCurvatures_25, ValNatural - ValExpression);
            } while (iterNeutral.MoveToNext());
        }
Example #12
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();
            }
        }
Example #13
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            if (!iter.IsValid())
            {
                throw new Exception("Iter is not valid");
            }

            if (m_ResetColor)
            {
                do
                {
                    iter.Color = Color.White;
                } while (iter.MoveToNext());
                return;
            }

            if (!m_bMinMaxPreset)
            {
                bool firstTime = true;
                do
                {
                    double Value;

                    if (m_RangeColor)
                    {
                        Value = iter.Z;
                    }
                    else if (!iter.GetSpecificValue(m_SpecificValue, out Value))
                    {
                        continue;
                    }

                    if (firstTime)
                    {
                        m_dMaxValue = Value;
                        m_dMinValue = Value;
                        firstTime   = false;
                        continue;
                    }

                    if (Value > m_dMaxValue)
                    {
                        m_dMaxValue = Value;
                    }
                    if (Value < m_dMinValue)
                    {
                        m_dMinValue = Value;
                    }
                } while (iter.MoveToNext());
            }


            if (m_SpecificValue.Contains("ShapeIndex") && !m_RangeColor)
            {
                m_dMinValue = 0;
                m_dMaxValue = 1;
            }


            if (m_bMaxMinFromNoseAndEyes)
            {
                Cl3DModel.Cl3DModelPointIterator        NoseTip          = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.NoseTip);
                List <Cl3DModel.Cl3DModelPointIterator> NeighborhoodNose = null;
                ClTools.GetNeighborhoodWithGeodesicDistance(out NeighborhoodNose, NoseTip, 10f);

                if (m_sMinValuePointName.Equals("Nose"))
                {
                    m_dMinValue = NoseTip.GetSpecificValue(m_SpecificValue);
                    foreach (Cl3DModel.Cl3DModelPointIterator nb in NeighborhoodNose)
                    {
                        double currVal = nb.GetSpecificValue(m_SpecificValue);
                        if (currVal < m_dMinValue)
                        {
                            m_dMinValue = currVal;
                        }
                    }
                }
                else if (m_sMaxValuePointName.Equals("Nose"))
                {
                    m_dMaxValue = NoseTip.GetSpecificValue(m_SpecificValue);
                    foreach (Cl3DModel.Cl3DModelPointIterator nb in NeighborhoodNose)
                    {
                        double currVal = nb.GetSpecificValue(m_SpecificValue);
                        if (currVal > m_dMaxValue)
                        {
                            m_dMaxValue = currVal;
                        }
                    }
                }
                NeighborhoodNose = null;

                Cl3DModel.Cl3DModelPointIterator        Eye1             = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.LeftEyeRightCorner);
                Cl3DModel.Cl3DModelPointIterator        Eye2             = p_Model.GetSpecificPoint(Cl3DModel.eSpecificPoints.RightEyeLeftCorner);
                List <Cl3DModel.Cl3DModelPointIterator> NeighborhoodEye1 = null;
                List <Cl3DModel.Cl3DModelPointIterator> NeighborhoodEye2 = null;
                ClTools.GetNeighborhoodWithGeodesicDistance(out NeighborhoodEye1, Eye1, 10f);
                ClTools.GetNeighborhoodWithGeodesicDistance(out NeighborhoodEye2, Eye2, 10f);

                NeighborhoodEye1.Add(Eye2);
                foreach (Cl3DModel.Cl3DModelPointIterator ne in NeighborhoodEye2)
                {
                    NeighborhoodEye1.Add(ne);
                }


                if (m_sMinValuePointName.Equals("Eye"))
                {
                    m_dMinValue = Eye1.GetSpecificValue(m_SpecificValue);
                    foreach (Cl3DModel.Cl3DModelPointIterator nb in NeighborhoodEye1)
                    {
                        double currVal = nb.GetSpecificValue(m_SpecificValue);
                        if (currVal < m_dMinValue)
                        {
                            m_dMinValue = currVal;
                        }
                    }
                }
                else if (m_sMaxValuePointName.Equals("Eye"))
                {
                    m_dMaxValue = Eye1.GetSpecificValue(m_SpecificValue);
                    foreach (Cl3DModel.Cl3DModelPointIterator nb in NeighborhoodEye1)
                    {
                        double currVal = nb.GetSpecificValue(m_SpecificValue);
                        if (currVal > m_dMaxValue)
                        {
                            m_dMaxValue = currVal;
                        }
                    }
                }
            }

            iter = p_Model.GetIterator();
            do
            {
                double Value;
                if (m_RangeColor)
                {
                    Value = iter.Z;
                }
                else if (!iter.GetSpecificValue(m_SpecificValue, out Value))
                {
                    continue;
                }


                double divide = 1;
                if (m_dMaxValue != m_dMinValue)
                {
                    divide = (m_dMaxValue - m_dMinValue);
                }

                if (m_Color == ColorType.RGB)
                {
                    iter.Color = ClTools.GetColorRGB((float)((Value - m_dMinValue) / divide), 1.0f);
                }
                else if (m_Color == ColorType.Gray)
                {
                    iter.Color = ClTools.GetColorGray((float)((Value - m_dMinValue) / divide), 1.0f);
                }
                else if (m_Color == ColorType.R)
                {
                    Color PartialColor = ClTools.GetColorRGB((float)((Value - m_dMinValue) / divide), 1.0f);

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

                    Color newColor = Color.FromArgb((R + G + B) / 3, iter.Color.G, iter.Color.B);

                    iter.Color = newColor;
                }
                else if (m_Color == ColorType.G)
                {
                    Color PartialColor = ClTools.GetColorRGB((float)((Value - m_dMinValue) / divide), 1.0f);

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

                    Color newColor = Color.FromArgb(iter.Color.R, (R + G + B) / 3, iter.Color.B);
                    iter.Color = newColor;
                }
                else if (m_Color == ColorType.B)
                {
                    Color PartialColor = ClTools.GetColorRGB((float)((Value - m_dMinValue) / divide), 1.0f);

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

                    Color newColor = Color.FromArgb(iter.Color.R, iter.Color.G, (R + G + B) / 3);
                    iter.Color = newColor;
                }
            } while (iter.MoveToNext());
            ClInformationSender.SendInformation(m_SpecificValue.ToString() + " Max: " + m_dMaxValue.ToString() + " Min: " + m_dMinValue.ToString(), ClInformationSender.eInformationType.eColorMapChanged);
        }
Example #14
0
        protected override void Algorithm(ref Cl3DModel p_Model)
        {
            Cl3DModel.Cl3DModelPointIterator iter = p_Model.GetIterator();

            if (!iter.IsValid())
            {
                return;
            }

            List <Cl3DModel.Cl3DModelPointIterator> ListOfNosePoints = new List <Cl3DModel.Cl3DModelPointIterator>();
            List <Cl3DModel.Cl3DModelPointIterator> ListOfEyesPoints = new List <Cl3DModel.Cl3DModelPointIterator>();
            string gausString = "Gaussian_" + m_iNeighborhoodSize.ToString();
            string meanString = "Mean_" + m_iNeighborhoodSize.ToString();

            do
            {
                double H = 0;
                double K = 0;

                if (!iter.GetSpecificValue(gausString, out K))
                {
                    continue;
                }
                if (!iter.GetSpecificValue(meanString, out H))
                {
                    continue;
                }

                if (H < 0 && K > m_NoseThresholdK) //  Nose
                {
                    ListOfNosePoints.Add(iter.CopyIterator());
                }
                else if (H > 0 && K > m_EyesThresholdK) //  Eyes
                {
                    ListOfEyesPoints.Add(iter.CopyIterator());
                }
            }while (iter.MoveToNext());

            List <List <Cl3DModel.Cl3DModelPointIterator> > NoseRegions = null;
            List <List <Cl3DModel.Cl3DModelPointIterator> > EyesRegions = null;

            // NOSE
            DivideToTheRegions(ListOfNosePoints, out NoseRegions);

            // EYES
            DivideToTheRegions(ListOfEyesPoints, out EyesRegions);

            // COLOR IT

            /*for (int i = 0; i < NoseRegions.Count; i++)
             * {
             *  foreach (Cl3DModel.Cl3DModelPointIterator point in NoseRegions[i])
             *      point.Color = ClTools.GetColorRGB((float)i / (NoseRegions.Count + EyesRegions.Count), 1);
             * }
             * for (int i = 0; i < EyesRegions.Count; i++)
             * {
             *  foreach (Cl3DModel.Cl3DModelPointIterator point in EyesRegions[i])
             *      point.Color = ClTools.GetColorRGB((float)(i + NoseRegions.Count) / (EyesRegions.Count + NoseRegions.Count), 1);
             * }
             */

            // -----------------  NOSE
            // remove smallest regions leave only 3 biggest one
            List <Cl3DModel.Cl3DModelPointIterator> NoseTipsPoints = new List <Cl3DModel.Cl3DModelPointIterator>();

            if (NoseRegions.Count > m_iMaxCountOfNoseRegions)
            {
                NoseRegions.Sort(new ClCompareCount());
                for (int i = NoseRegions.Count - 1; i >= m_iMaxCountOfNoseRegions; i--)
                {
                    NoseRegions.RemoveAt(i);
                }
            }
            //----------- NOSE searching for one point in each region
            for (int i = 0; i < NoseRegions.Count; i++)
            {
                double MaxKVal = 0;
                Cl3DModel.Cl3DModelPointIterator MaxPoint = null;
                foreach (Cl3DModel.Cl3DModelPointIterator point in NoseRegions[i])
                {
                    double K;
                    if (!point.GetSpecificValue(gausString, out K))
                    {
                        continue;
                    }

                    if (MaxPoint == null)
                    {
                        MaxPoint = point;
                        MaxKVal  = K;
                        continue;
                    }

                    if (MaxKVal < K)
                    {
                        MaxKVal  = K;
                        MaxPoint = point;
                    }
                }
                NoseTipsPoints.Add(MaxPoint);
            }

            // ---------------  EYES
            // remove smallest regions leave only 5 biggest one
            List <Cl3DModel.Cl3DModelPointIterator> EyesPoints = new List <Cl3DModel.Cl3DModelPointIterator>();

            if (EyesRegions.Count > m_iMaxCountOfEyeRegions)
            {
                EyesRegions.Sort(new ClCompareCount());
                for (int i = EyesRegions.Count - 1; i >= m_iMaxCountOfEyeRegions; i--)
                {
                    EyesRegions.RemoveAt(i);
                }
            }
            //--------------------------- EYES searching for one point in each region
            for (int i = 0; i < EyesRegions.Count; i++)
            {
                double MaxKval = 0;
                Cl3DModel.Cl3DModelPointIterator MinPoint = null;

                foreach (Cl3DModel.Cl3DModelPointIterator point in EyesRegions[i])
                {
                    double val;
                    if (!point.GetSpecificValue(gausString, out val))
                    {
                        continue;
                    }

                    if (MinPoint == null)
                    {
                        MinPoint = point;
                        MaxKval  = val;
                        continue;
                    }

                    if (MaxKval < val)
                    {
                        MaxKval  = val;
                        MinPoint = point;
                    }
                }
                EyesPoints.Add(MinPoint);
            }
            //------------- Looking for three face points ------------------------------
            Iridium.Numerics.LinearAlgebra.Matrix Model    = new Iridium.Numerics.LinearAlgebra.Matrix(3, 3);
            Iridium.Numerics.LinearAlgebra.Matrix ModelGen = new Iridium.Numerics.LinearAlgebra.Matrix(3, 3);

            // Generic model
            Vector GenericNoseTip  = new Vector(new double[] { 0, 0, 0 });
            Vector GenericLeftEye  = new Vector(new double[] { -21f, 40.5f, -41.5f });
            Vector GenericRightEye = new Vector(new double[] { 21f, 40.5f, -41.5f });

            Vector GenLeftCornerOfLeftEye   = new Vector(new double[] { -44f, 40f, -44f });
            Vector GenRightCornerOfRightEye = new Vector(new double[] { 44f, 40f, -44f });

            Vector GenLeftCornerOfNose  = new Vector(new double[] { -20f, -2f, -31f });
            Vector GenRightCornerOfNose = new Vector(new double[] { 20f, -2f, -31f });

            Vector GenLeftCornerOfLips  = new Vector(new double[] { -28f, -32f, -34f });
            Vector GenRightCornerOfLips = new Vector(new double[] { 28f, -32f, -34f });


            Vector MeanGenericModel = new Vector(new double[] { (GenericNoseTip[0] + GenericLeftEye[0] + GenericRightEye[0]) / 3,
                                                                (GenericNoseTip[1] + GenericLeftEye[1] + GenericRightEye[1]) / 3,
                                                                (GenericNoseTip[2] + GenericLeftEye[2] + GenericRightEye[2]) / 3 });

            // Centered generic model (generic model - mean)
            ModelGen[0, 0] = (GenericNoseTip[0] - MeanGenericModel[0]);
            ModelGen[1, 0] = (GenericNoseTip[1] - MeanGenericModel[1]);
            ModelGen[2, 0] = (GenericNoseTip[2] - MeanGenericModel[2]);

            ModelGen[0, 1] = (GenericLeftEye[0] - MeanGenericModel[0]);
            ModelGen[1, 1] = (GenericLeftEye[1] - MeanGenericModel[1]);
            ModelGen[2, 1] = (GenericLeftEye[2] - MeanGenericModel[2]);

            ModelGen[0, 2] = (GenericRightEye[0] - MeanGenericModel[0]);
            ModelGen[1, 2] = (GenericRightEye[1] - MeanGenericModel[1]);
            ModelGen[2, 2] = (GenericRightEye[2] - MeanGenericModel[2]);
            ModelGen.Transpose();

            List <KeyValuePair <float, List <Cl3DModel.Cl3DModelPointIterator> > > deltaOfDistanceBetweenModelAndPoints = new List <KeyValuePair <float, List <Cl3DModel.Cl3DModelPointIterator> > >();

            for (int iEye = 0; iEye < EyesPoints.Count - 1; iEye++)
            {
                for (int jEye = iEye + 1; jEye < EyesPoints.Count; jEye++)
                {
                    for (int iNose = 0; iNose < NoseTipsPoints.Count; iNose++)
                    {
                        Cl3DModel.Cl3DModelPointIterator eyeLeft  = EyesPoints[iEye];
                        Cl3DModel.Cl3DModelPointIterator eyeRight = EyesPoints[jEye];
                        Cl3DModel.Cl3DModelPointIterator noseTip  = NoseTipsPoints[iNose];

                        if (eyeLeft.Y < noseTip.Y || eyeRight.Y < noseTip.Y)
                        {
                            continue;
                        }

                        for (int c = 0; c < 2; c++)
                        {
                            if (c == 1)
                            {
                                eyeLeft  = EyesPoints[jEye]; // zmiana oczu nie wiemy ktore punkty to oczy prawdziwe sa
                                eyeRight = EyesPoints[iEye];
                            }

                            float MeanValX = (noseTip.X + eyeLeft.X + eyeRight.X) / 3.0f;
                            float MeanValY = (noseTip.Y + eyeLeft.Y + eyeRight.Y) / 3.0f;
                            float MeanValZ = (noseTip.Z + eyeLeft.Z + eyeRight.Z) / 3.0f;

                            Model[0, 0] = (noseTip.X - MeanValX);
                            Model[1, 0] = (noseTip.Y - MeanValY);
                            Model[2, 0] = (noseTip.Z - MeanValZ);

                            Model[0, 1] = (eyeLeft.X - MeanValX);
                            Model[1, 1] = (eyeLeft.Y - MeanValY);
                            Model[2, 1] = (eyeLeft.Z - MeanValZ);

                            Model[0, 2] = (eyeRight.X - MeanValX);
                            Model[1, 2] = (eyeRight.Y - MeanValY);
                            Model[2, 2] = (eyeRight.Z - MeanValZ);


                            //----------------- Finding best rotation and translation SVD algorithm

                            Iridium.Numerics.LinearAlgebra.Matrix Covariance = Model * ModelGen;

                            Iridium.Numerics.LinearAlgebra.SingularValueDecomposition SVD = new Iridium.Numerics.LinearAlgebra.SingularValueDecomposition(Covariance);
                            Iridium.Numerics.LinearAlgebra.Matrix U = SVD.LeftSingularVectors;
                            Iridium.Numerics.LinearAlgebra.Matrix V = SVD.RightSingularVectors;

                            Iridium.Numerics.LinearAlgebra.Matrix s = new Iridium.Numerics.LinearAlgebra.Matrix(3, 1.0);

                            if (Covariance.Rank() < 2)
                            {
                                throw new Exception("Cannot allign generic model (cov rank is less than 2)");
                            }

                            if (Covariance.Rank() == 2) // m-1 where m is dimension space (3D)
                            {
                                double detU = Math.Round(U.Determinant());
                                double detV = Math.Round(V.Determinant());
                                double detC = Covariance.Determinant();
                                if ((int)detU * (int)detV == 1)
                                {
                                    s[2, 2] = 1;
                                }
                                else if ((int)detU * (int)detV == -1)
                                {
                                    s[2, 2] = -1;
                                }
                                else
                                {
                                    throw new Exception("Determinant of U and V are not in conditions");
                                }
                            }
                            else
                            {
                                if (Covariance.Determinant() < 0)
                                {
                                    s[2, 2] = -1;
                                }
                            }

                            V.Transpose();
                            Iridium.Numerics.LinearAlgebra.Matrix Roatation = U * s * V;

                            Iridium.Numerics.LinearAlgebra.Matrix MeanValOfGenModelMatrix = new Iridium.Numerics.LinearAlgebra.Matrix(3, 1);
                            MeanValOfGenModelMatrix[0, 0] = MeanGenericModel[0];
                            MeanValOfGenModelMatrix[1, 0] = MeanGenericModel[1];
                            MeanValOfGenModelMatrix[2, 0] = MeanGenericModel[2];

                            Iridium.Numerics.LinearAlgebra.Matrix MeanValOfModelMatrix = new Iridium.Numerics.LinearAlgebra.Matrix(3, 1);
                            MeanValOfModelMatrix[0, 0] = MeanValX;
                            MeanValOfModelMatrix[1, 0] = MeanValY;
                            MeanValOfModelMatrix[2, 0] = MeanValZ;

                            Iridium.Numerics.LinearAlgebra.Matrix Translation = MeanValOfModelMatrix - Roatation * MeanValOfGenModelMatrix;

                            //--------------------------- End now we have translation and rotation

                            float GenericModelDistance = 0;;

                            Iridium.Numerics.LinearAlgebra.Matrix q = new Iridium.Numerics.LinearAlgebra.Matrix(3, 1);
                            q[0, 0] = GenericNoseTip[0];
                            q[1, 0] = GenericNoseTip[1];
                            q[2, 0] = GenericNoseTip[2];
                            Iridium.Numerics.LinearAlgebra.Matrix NewQ = Roatation * q + Translation;
                            GenericModelDistance += (float)Math.Sqrt(Math.Pow(noseTip.X - NewQ[0, 0], 2) + Math.Pow(noseTip.Y - NewQ[1, 0], 2) + Math.Pow(noseTip.Z - NewQ[2, 0], 2));
                            //---------
                            //    Cl3DModel.Cl3DModelPointIterator it1 = p_Model.AddPointToModel((float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0]);
                            //it1.AddNeighbor(noseTip);
                            //    it1.Color = Color.Green;
                            //---------
                            q[0, 0] = GenericLeftEye[0];
                            q[1, 0] = GenericLeftEye[1];
                            q[2, 0] = GenericLeftEye[2];
                            NewQ    = Roatation * q + Translation;
                            GenericModelDistance += (float)Math.Sqrt(Math.Pow(eyeLeft.X - NewQ[0, 0], 2) + Math.Pow(eyeLeft.Y - NewQ[1, 0], 2) + Math.Pow(eyeLeft.Z - NewQ[2, 0], 2));
                            //---------
                            //    Cl3DModel.Cl3DModelPointIterator it2 = p_Model.AddPointToModel((float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0]);
                            //it2.AddNeighbor(eyeLeft);
                            //    it2.Color = Color.White;
                            //---------

                            q[0, 0] = GenericRightEye[0];
                            q[1, 0] = GenericRightEye[1];
                            q[2, 0] = GenericRightEye[2];
                            NewQ    = Roatation * q + Translation;
                            GenericModelDistance += (float)Math.Sqrt(Math.Pow(eyeRight.X - NewQ[0, 0], 2) + Math.Pow(eyeRight.Y - NewQ[1, 0], 2) + Math.Pow(eyeRight.Z - NewQ[2, 0], 2));
                            //---------
                            //    Cl3DModel.Cl3DModelPointIterator it3 = p_Model.AddPointToModel((float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0]);
                            //it3.AddNeighbor(eyeRight);
                            //    it3.Color = Color.Orange;

                            //    it1.AddNeighbor(it2);
                            //    it1.AddNeighbor(it3);
                            //    it2.AddNeighbor(it3);
                            //---------

                            // ---------- Rest of generic model to find closest points and count distance;
                            q[0, 0] = GenLeftCornerOfLeftEye[0];
                            q[1, 0] = GenLeftCornerOfLeftEye[1];
                            q[2, 0] = GenLeftCornerOfLeftEye[2];
                            NewQ    = Roatation * q + Translation;
                            Cl3DModel.Cl3DModelPointIterator closestPoint = null;
                            float MinDistance;
                            ClTools.FindClosestPointInModel(p_Model, (float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0], out closestPoint, out MinDistance);
                            GenericModelDistance += MinDistance;

                            q[0, 0] = GenRightCornerOfRightEye[0];
                            q[1, 0] = GenRightCornerOfRightEye[1];
                            q[2, 0] = GenRightCornerOfRightEye[2];
                            NewQ    = Roatation * q + Translation;
                            ClTools.FindClosestPointInModel(p_Model, (float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0], out closestPoint, out MinDistance);
                            GenericModelDistance += MinDistance;

                            q[0, 0] = GenLeftCornerOfNose[0];
                            q[1, 0] = GenLeftCornerOfNose[1];
                            q[2, 0] = GenLeftCornerOfNose[2];
                            NewQ    = Roatation * q + Translation;
                            ClTools.FindClosestPointInModel(p_Model, (float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0], out closestPoint, out MinDistance);
                            GenericModelDistance += MinDistance;

                            q[0, 0] = GenRightCornerOfNose[0];
                            q[1, 0] = GenRightCornerOfNose[1];
                            q[2, 0] = GenRightCornerOfNose[2];
                            NewQ    = Roatation * q + Translation;
                            ClTools.FindClosestPointInModel(p_Model, (float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0], out closestPoint, out MinDistance);
                            GenericModelDistance += MinDistance;

                            q[0, 0] = GenLeftCornerOfLips[0];
                            q[1, 0] = GenLeftCornerOfLips[1];
                            q[2, 0] = GenLeftCornerOfLips[2];
                            NewQ    = Roatation * q + Translation;
                            ClTools.FindClosestPointInModel(p_Model, (float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0], out closestPoint, out MinDistance);
                            GenericModelDistance += MinDistance;

                            q[0, 0] = GenRightCornerOfLips[0];
                            q[1, 0] = GenRightCornerOfLips[1];
                            q[2, 0] = GenRightCornerOfLips[2];
                            NewQ    = Roatation * q + Translation;
                            ClTools.FindClosestPointInModel(p_Model, (float)NewQ[0, 0], (float)NewQ[1, 0], (float)NewQ[2, 0], out closestPoint, out MinDistance);
                            GenericModelDistance += MinDistance;
                            // ----------


                            ClInformationSender.SendInformation("One of distances between generic model and face before thresholding: " + GenericModelDistance.ToString(System.Globalization.CultureInfo.InvariantCulture), ClInformationSender.eInformationType.eDebugText);

                            if (GenericModelDistance > m_fDistanceMinValueFromGenModel)
                            {
                                continue;
                            }

                            // Check if between eyes and nose there is other region (it should be not)

                            /*bool isBetweenNoseRegion = false;
                             * foreach (Cl3DModel.Cl3DModelPointIterator noseTmp in NoseTipsPoints)
                             * {
                             *  if (noseTmp.PointID == noseTip.PointID)
                             *      continue;
                             *
                             *  if (noseTmp.X > eyeLeft.X && noseTmp.X < eyeRight.X && noseTmp.Y > noseTip.Y && (noseTip.Y < eyeLeft.Y || noseTip.Y < eyeRight.Y))
                             *      isBetweenNoseRegion = true;
                             * }
                             * if (isBetweenNoseRegion)
                             *  continue;
                             */
                            List <Cl3DModel.Cl3DModelPointIterator> list = new List <Cl3DModel.Cl3DModelPointIterator>();
                            list.Add(noseTip);
                            list.Add(eyeLeft);
                            list.Add(eyeRight);

                            KeyValuePair <float, List <Cl3DModel.Cl3DModelPointIterator> > KeyVal = new KeyValuePair <float, List <Cl3DModel.Cl3DModelPointIterator> >(GenericModelDistance, list);

                            deltaOfDistanceBetweenModelAndPoints.Add(KeyVal);
                        }
                    }
                }
            }

            if (deltaOfDistanceBetweenModelAndPoints.Count == 0)
            {
                throw new Exception("Cannot find nose and eyes points");
            }

            float min = deltaOfDistanceBetweenModelAndPoints[0].Key;
            int   no  = 0;

            for (int i = 1; i < deltaOfDistanceBetweenModelAndPoints.Count; i++)
            {
                if (deltaOfDistanceBetweenModelAndPoints[i].Key < min)
                {
                    min = deltaOfDistanceBetweenModelAndPoints[i].Key;
                    no  = i;
                }
            }

            ClInformationSender.SendInformation("Correct MIN distance between generic model and face: " + min.ToString(System.Globalization.CultureInfo.InvariantCulture), ClInformationSender.eInformationType.eDebugText);

            p_Model.AddSpecificPoint(NameOfNoseTip, deltaOfDistanceBetweenModelAndPoints[no].Value[0]);
            p_Model.AddSpecificPoint(NameLeftEyeRightCorner, deltaOfDistanceBetweenModelAndPoints[no].Value[1]);
            p_Model.AddSpecificPoint(NameRightEyeLeftCorner, deltaOfDistanceBetweenModelAndPoints[no].Value[2]);
        }
        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);
             */
            //---------------------------------------------------------------------------
        }