Exemplo n.º 1
0
        private void LoadMesh()
        {
            using (TextReader tr = File.OpenText(System.AppDomain.CurrentDomain.BaseDirectory + "//meshmapping.txt"))
            {
                string s     = tr.ReadToEnd();
                var    lines = s.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                _Mappings = new List <FacePointMapping>();
                var n = lines.Length;
                for (int i = 0; i < n; i++)
                {
                    var v = lines[i].Split(':');
                    FacePointMapping fpm = new FacePointMapping();
                    fpm.side = v[0];
                    var v1 = v[1].Split('=');
                    fpm.index       = int.Parse(v1[0]);
                    fpm.mappedindex = int.Parse(v1[1]);
                    if (fpm.index == fpm.mappedindex)
                    {
                        fpm.side = "C";
                    }
                    _Mappings.Add(fpm);
                }
            }


            using (TextReader tr = File.OpenText(System.AppDomain.CurrentDomain.BaseDirectory + "//tri_index.txt"))
            {
                string s     = tr.ReadToEnd();
                var    lines = s.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                _TriangleIndices = new Int32Collection();
                var n = lines.Length;
                for (int i = 0; i < n; i++)
                {
                    _TriangleIndices.Add(int.Parse(lines[i]));
                }
            }


            using (TextReader tr = File.OpenText(BaseXFile))
            {
                string s = tr.ReadToEnd();
                _Positions          = new Point3DCollection();
                _TextureCoordinates = new PointCollection();
                _orgmeshpos         = new Point3DCollection();
                var lines = s.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);


                int n = lines.Length;

                if (lines.Length > 1347)
                {
                    n = lines.Length / 2;
                }

                for (int i = 0; i < n; i++)
                {
                    var c = lines[i].Split(':');

                    var vertice = new Point3D(double.Parse(c[0], CultureInfo.InvariantCulture),
                                              double.Parse(c[1], CultureInfo.InvariantCulture),
                                              double.Parse(c[2], CultureInfo.InvariantCulture));

                    _Positions.Add(vertice);
                    _orgmeshpos.Add(vertice);
                }

                //one time test code
                //create meshmapping
                // CreateMeshMapping();

                NormalizePos();

                GenerateTextureCordinatesFromPos();
            }
        }
Exemplo n.º 2
0
        private void RenderMesh()
        {
            if (_TextureCoordinates == null)
            {
                return;
            }

            double minx = 1.0, maxx = 0, miny = 1, maxy = 0;

            for (int i = 0; i < _TextureCoordinates.Count; i++)
            {
                if (_TextureCoordinates[i].X < minx)
                {
                    minx = _TextureCoordinates[i].X;
                }
                if (_TextureCoordinates[i].Y < miny)
                {
                    miny = _TextureCoordinates[i].Y;
                }
                if (_TextureCoordinates[i].X > maxx)
                {
                    maxx = _TextureCoordinates[i].X;
                }
                if (_TextureCoordinates[i].Y > maxy)
                {
                    maxy = _TextureCoordinates[i].Y;
                }
            }

            //2. normalize
            int width  = (int)((maxx - minx) * 1920) + 1;
            int height = (int)((maxy - miny) * 1080) + 1;
            List <System.Drawing.PointF> meshpoints = new List <System.Drawing.PointF>();

            for (int i = 0; i < _TextureCoordinates.Count; i++)
            {
                meshpoints.Add(new System.Drawing.PointF((float)(_TextureCoordinates[i].X - minx) * 1920,
                                                         (float)(_TextureCoordinates[i].Y - miny) * 1080)
                               );
            }

            ////The bitmap must have ARGB Pixel format to support transparency
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            ///////////////////////////////////////////////////////

            System.Drawing.Graphics gbm = System.Drawing.Graphics.FromImage(bm);
            gbm.Clear(System.Drawing.Color.Transparent);


            //Draw the points
            System.Drawing.Brush brush;
            for (int i = 0; i < _TextureCoordinates.Count; i++)
            {
                FacePointMapping mapping = _Mappings.Find(p => p.index == i);
                brush = (mapping.side == "R") ? System.Drawing.Brushes.Red : System.Drawing.Brushes.Blue;
                if (mapping.side == "C")
                {
                    brush = System.Drawing.Brushes.Cyan;
                }

                gbm.FillRectangle(brush, new System.Drawing.RectangleF(
                                      meshpoints[i].X,
                                      meshpoints[i].Y,
                                      3, 3));
            }



            if ((bool)chkMesh.IsChecked)
            {
                for (int i = 0; i < _TriangleIndices.Count; i = i + 3)
                {
                    System.Drawing.Pen pen  = new System.Drawing.Pen(_MeshColor);
                    System.Drawing.Pen pen2 = new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, System.Drawing.Color.YellowGreen));

                    //Use different pen for these triangles connected to these points
                    //10 (center of base of upper lip)
                    if (_TriangleIndices[i] == 10 || _TriangleIndices[i + 1] == 10 || _TriangleIndices[i + 2] == 10)
                    {
                        pen = pen2;
                    }

                    //14 (nose tip)
                    if (_TriangleIndices[i] == 14 || _TriangleIndices[i + 1] == 14 || _TriangleIndices[i + 2] == 14)
                    {
                        pen = pen2;
                    }
                    //0 (chin)
                    if (_TriangleIndices[i] == 0 || _TriangleIndices[i + 1] == 0 || _TriangleIndices[i + 2] == 0)
                    {
                        pen = pen2;
                    }
                    //328-1105 (right eye is between this 2 points)
                    if (_TriangleIndices[i] == 1105 || _TriangleIndices[i + 1] == 1105 || _TriangleIndices[i + 2] == 1105)
                    {
                        pen = pen2;
                    }
                    //883-1092 (left eye is between these 2 points)
                    if (_TriangleIndices[i] == 1092 || _TriangleIndices[i + 1] == 1092 || _TriangleIndices[i + 2] == 1092)
                    {
                        pen = pen2;
                    }

                    if (CCommon.ArePointsClockwise(
                            meshpoints[_TriangleIndices[i]],
                            meshpoints[_TriangleIndices[i + 1]],
                            meshpoints[_TriangleIndices[i + 2]])
                        )
                    {
                        gbm.DrawPolygon(pen, new System.Drawing.PointF[]
                        {
                            meshpoints[_TriangleIndices[i]],
                            meshpoints[_TriangleIndices[i + 1]],
                            meshpoints[_TriangleIndices[i + 2]]
                        }
                                        );
                    }
                }
            }
            gbm.Dispose();

            Image1.Source = CCommon.Bitmap2BitmapImage(bm);
            bm.Dispose();
            bm = null;
        }