Beispiel #1
0
        public void UpdatePath()
        {
            var bitmap = new RawBitmap(1024, 1024);

            if (mesh != null)
            {
                if (mesh.uv != null && mesh.uv.Count == mesh.vertices.Count)
                {
                    Parallel.For(0, mesh.triangles.Count, i =>
                    {
                        Triangle t = mesh.triangles[i];

                        Vector2 uv0 = mesh.uv[t.u0];
                        Vector2 uv1 = mesh.uv[t.u1];
                        Vector2 uv2 = mesh.uv[t.u2];

                        Point p  = new Point(uv0.X * 1024, uv0.Y * 1024);
                        Point p2 = new Point(uv1.X * 1024, uv1.Y * 1024);
                        Point p3 = new Point(uv2.X * 1024, uv2.Y * 1024);

                        bitmap.DrawLine((int)p.X, (int)p.Y, (int)p2.X, (int)p2.Y, 0, 255, 255, 255);
                        bitmap.DrawLine((int)p2.X, (int)p2.Y, (int)p3.X, (int)p3.Y, 0, 255, 255, 255);
                        bitmap.DrawLine((int)p3.X, (int)p3.Y, (int)p.X, (int)p.Y, 0, 255, 255, 255);
                    });
                }
            }
            Preview.Source = bitmap.ToImageSource();
        }
Beispiel #2
0
        /// <summary>
        /// Renderes the path to the buffer
        /// and returns the normalized curve data
        /// the path should be a normalized set of points from 0 - 1
        /// </summary>
        /// <param name="path"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        protected List <MathHelpers.Point> RenderPath(List <CurvePoint> path, RawBitmap buffer, byte r = 175, byte g = 175, byte b = 175)
        {
            List <MathHelpers.Point> points     = new List <MathHelpers.Point>();
            List <MathHelpers.Point> curve      = new List <MathHelpers.Point>();
            List <MathHelpers.Point> normalized = new List <MathHelpers.Point>();
            double width  = CurveView.ActualWidth;
            double height = CurveView.ActualHeight - 1;

            foreach (CurvePoint p in path)
            {
                MathHelpers.Point n = p.Normalized;
                points.Add(new MathHelpers.Point(n.X * width, n.Y * height));
            }

            Sort(points);

            //make sure we have x points on edges
            if (points.Count >= 2)
            {
                MathHelpers.Point f = points[0];

                if (f.X > 0)
                {
                    points.Insert(0, new MathHelpers.Point(0, f.Y));
                }

                MathHelpers.Point l = points[points.Count - 1];

                if (l.X < width)
                {
                    points.Add(new MathHelpers.Point(width, l.Y));
                }
            }

            double[] sd = Curves.SecondDerivative(points.ToArray());

            for (int i = 0; i < points.Count - 1; ++i)
            {
                MathHelpers.Point cur  = points[i];
                MathHelpers.Point next = points[i + 1];

                for (double x = cur.X; x < next.X; ++x)
                {
                    double t = (double)(x - cur.X) / (next.X - cur.X);

                    double a  = 1 - t;
                    double bt = t;
                    double h  = next.X - cur.X;

                    double y = a * cur.Y + bt * next.Y + (h * h / 6) * ((a * a * a - a) * sd[i] + (bt * bt * bt - bt) * sd[i + 1]);


                    if (y < 0)
                    {
                        y = 0;
                    }
                    if (y > height - 1)
                    {
                        y = height - 1;
                    }

                    curve.Add(new MathHelpers.Point(x, y));
                    normalized.Add(new MathHelpers.Point(x / width, y / height));
                }
            }

            MathHelpers.Point lp = points[points.Count - 1];
            curve.Add(lp);
            normalized.Add(new MathHelpers.Point(lp.X / width, lp.Y / height));

            Parallel.For(0, curve.Count - 1, i =>
            {
                MathHelpers.Point p1 = curve[i];
                MathHelpers.Point p2 = curve[i + 1];
                buffer.DrawLine((int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, r, g, b, 255);
            });

            return(normalized);
        }