Exemplo n.º 1
0
        private static double errorPolygon(Sample s, PixelMap original, TrigradOptions options)
        {
            if(options.ResampleColors)
            foreach (var sample in s.Samples)
            {
                sample.Color = original[sample.Point];
            }

            double error = 0d;
            foreach (var t in s.Triangles)
            {
                t.CenterColor = original[t.CenterPoint];

                options.Renderer.Fill(t,tempMap);

                foreach (var drawPoint in t.Points)
                {
                    Pixel a = original[drawPoint.Point];
                    Pixel b = tempMap[drawPoint.Point];

                    Pixel diff = a - b;

                    error += diff.R;
                    error += diff.G;
                    error += diff.B;
                }
            }
            return error;
        }
Exemplo n.º 2
0
        /// <summary> Produces a color from the specified coordinates and colors. </summary>
        public Pixel Grade(Sample u, Sample v, Sample w, DrawPoint p)
        {
            int R = u.Color.R + v.Color.R + w.Color.R;
            int G = u.Color.G + v.Color.G + w.Color.G;
            int B = u.Color.B + v.Color.B + w.Color.B;

            return new Pixel((byte) (R/3), (byte) (G/3), (byte) (B/3));
        }
Exemplo n.º 3
0
 /// <summary> Produces a color from the specified coordinates and colors. </summary>
 public Pixel Grade(Sample u, Sample v, Sample w, DrawPoint p)
 {
     if (p.BarycentricCoordinates.U >= p.BarycentricCoordinates.V && p.BarycentricCoordinates.U >= p.BarycentricCoordinates.W)
         return u.Color;
     if (p.BarycentricCoordinates.V >= p.BarycentricCoordinates.W)
         return v.Color;
     return w.Color;
 }
Exemplo n.º 4
0
 /// <summary> Produces a color from the specified coordinates and colors. </summary>
 public Pixel Grade(Sample u, Sample v, Sample w, DrawPoint p)
 {
     int val = (p.Point.X + p.Point.Y) % 3;
     if (val == 0)
         return u.Color;
     if (val == 1)
         return v.Color;
     return w.Color;
 }
Exemplo n.º 5
0
        /// <summary> Produces a color from the specified coordinates and colors. </summary>
        public Pixel Grade(Sample u, Sample v, Sample w, DrawPoint p)
        {
            int uSum = u.Color.R + u.Color.G + u.Color.B;
            int vSum = v.Color.R + v.Color.G + v.Color.B;
            int wSum = w.Color.R + w.Color.G + w.Color.B;

            if (uSum > vSum && uSum > wSum)
            {
                return u.Color;
            }
            else if (vSum > wSum)
                return v.Color;
            else
                return w.Color;
        }
Exemplo n.º 6
0
        private static List<Sample> depthSelect(Sample sample, int depth, List<Sample> parents = null)
        {
            if (depth <= 0)
                return new List<Sample>();

            List<Sample> samples = new List<Sample>();
            foreach (var child in sample.Samples)
            {
                if (parents != null && parents.Contains(child))
                    continue;

                samples.Add(child);
                var result = depthSelect(child, depth - 1, samples);

                samples.AddRange(result);
            }
            return samples;
        }
Exemplo n.º 7
0
        /// <summary> Produces a color from the specified coordinates and colors. </summary>
        public Pixel Grade(Sample u, Sample v, Sample w, DrawPoint p)
        {
            if (p.BarycentricCoordinates.U >= 0.9)
                return u.Color;
            if (p.BarycentricCoordinates.V >= 0.9)
                return v.Color;
            if (p.BarycentricCoordinates.W >= 0.9)
                return w.Color;

            int valU = (int)(p.Point.X + p.Point.Y + p.BarycentricCoordinates.U + r.Next(0, 4)) % 4;
            int valV = (int)(p.Point.X + p.Point.Y + p.BarycentricCoordinates.V + r.Next(0, 4)) % 4;
            int valW = (int)(p.Point.X + p.Point.Y + p.BarycentricCoordinates.W + r.Next(0, 4)) % 4;

            if (p.BarycentricCoordinates.U >= p.BarycentricCoordinates.V && p.BarycentricCoordinates.U >= p.BarycentricCoordinates.W)
                return ditherFurther(u.Color, v.Color, w.Color, valU);
            if (p.BarycentricCoordinates.V >= p.BarycentricCoordinates.W)
                return ditherFurther(v.Color, w.Color, w.Color, valV);

            return ditherFurther(w.Color, v.Color, u.Color, valW);
        }
Exemplo n.º 8
0
        private static bool polygonConvex(Sample s)
        {
            List<Point> outerPolygonPoints = s.Samples.Except(new[]{s}).Select(sample=>sample.Point).ToList();

            bool got_negative = false;
            bool got_positive = false;
            int num_points = outerPolygonPoints.Count();
            int B, C;
            for (int A = 0; A < num_points; A++)
            {
                B = (A + 1) % num_points;
                C = (B + 1) % num_points;

                float cross_product =
                    crossProductMagnitude(
                        outerPolygonPoints[A].X, outerPolygonPoints[A].Y,
                        outerPolygonPoints[B].X, outerPolygonPoints[B].Y,
                        outerPolygonPoints[C].X, outerPolygonPoints[C].Y);
                if (cross_product < 0)
                {
                    got_negative = true;
                }
                else if (cross_product > 0)
                {
                    got_positive = true;
                }
                if (got_negative && got_positive) return false;
            }

            // If we got this far, the polygon is convex.
            return true;
        }
Exemplo n.º 9
0
        private static void minimiseSample(Sample s, int resamples, PixelMap original, TrigradOptions options)
        {
            if (s.Point.X == 0 || s.Point.Y == 0)
                return;

            if (s.Point.X == original.Width - 1 || s.Point.Y == original.Height - 1)
                return;

            var curPoints = s.Points;

            double minError = errorPolygon(s, original, options);
            Point bestPoint = s.Point;

            if (polygonConvex(s))
                return;

            int count = curPoints.Count;
            int skip = count / resamples;
            if (skip == 0)
                skip = 1;

            foreach (var drawPoint in curPoints.Where((x, i) => i % skip == 0))
            {
                s.Point = drawPoint.Point;

                TriangleRasterization.CalculateMesh(s.Triangles);

                double error = errorPolygon(s, original, options);
                if (error < minError)
                {
                    bestPoint = drawPoint.Point;
                    minError = error;
                }
            }

            s.Point = bestPoint;
            TriangleRasterization.CalculateMesh(s.Triangles);
        }
Exemplo n.º 10
0
 /// <summary> Produces a color from the specified coordinates and colors. </summary>
 public Pixel Grade(Sample u, Sample v, Sample w, DrawPoint p)
 {
     return u.Color;
 }