From 99fd7ab61a3f5a9e5244683a2606a7a328e78c1c Mon Sep 17 00:00:00 2001 From: tyuXX Date: Thu, 29 Oct 2026 00:51:30 +0300 Subject: [PATCH] fix dot contamination --- src/GraphTUI/Rendering/BrailleCanvas.cs | 158 ++++++++++++++++++--- src/GraphTUI/Rendering/PlotRenderer.cs | 18 +-- tests/GraphTUI.Tests/BrailleCanvasTests.cs | 68 +++++++++ 3 files changed, 215 insertions(+), 29 deletions(-) diff --git a/src/GraphTUI/Rendering/BrailleCanvas.cs b/src/GraphTUI/Rendering/BrailleCanvas.cs index f1506e0..44629da 100644 --- a/src/GraphTUI/Rendering/BrailleCanvas.cs +++ b/src/GraphTUI/Rendering/BrailleCanvas.cs @@ -1,5 +1,17 @@ namespace GraphTUI.Rendering; +public enum CanvasLayer : byte +{ + None = 0, + Grid = 1, // Background grid lines (faint reference dots) + Axis = 2, // Axis lines (X and Y coordinate axes) + CrosshairGuide = 3, // Dashed reference lines of crosshair + Shading = 4, // Integral shading / area fill + Curve = 5, // Function curves (Cartesian, Parametric, Polar, Implicit) + Annotation = 6, // Tangent lines, derivative curves + Crosshair = 7 // Central cursor point +} + public sealed class BrailleCanvas { private static readonly byte[,] DotMap = new byte[4, 2] @@ -10,14 +22,27 @@ public sealed class BrailleCanvas { 0x40, 0x80 } // row 3: dot 7, dot 8 }; + private static readonly byte[] DotIndexMask = [ 0x01, 0x08, 0x02, 0x10, 0x04, 0x20, 0x40, 0x80 ]; + + private readonly struct DotPixel + { + public readonly ColorRgb Color; + public readonly CanvasLayer Layer; + + public DotPixel(ColorRgb color, CanvasLayer layer) + { + Color = color; + Layer = layer; + } + } + public int CellWidth { get; private set; } public int CellHeight { get; private set; } public int SubpixelWidth => CellWidth * 2; public int SubpixelHeight => CellHeight * 4; - private byte[] _dotMasks = []; - private ColorRgb[] _fgColors = []; + private DotPixel[] _pixels = []; private ColorRgb[] _bgColors = []; public BrailleCanvas(int cellWidth, int cellHeight) @@ -30,40 +55,79 @@ public sealed class BrailleCanvas cellWidth = System.Math.Max(1, cellWidth); cellHeight = System.Math.Max(1, cellHeight); - if (CellWidth == cellWidth && CellHeight == cellHeight && _dotMasks != null) + if (CellWidth == cellWidth && CellHeight == cellHeight && _pixels != null && _pixels.Length == cellWidth * cellHeight * 8) return; CellWidth = cellWidth; CellHeight = cellHeight; - int total = cellWidth * cellHeight; + int totalCells = cellWidth * cellHeight; - _dotMasks = new byte[total]; - _fgColors = new ColorRgb[total]; - _bgColors = new ColorRgb[total]; + _pixels = new DotPixel[totalCells * 8]; + _bgColors = new ColorRgb[totalCells]; Clear(ColorRgb.Background); } public void Clear(ColorRgb bgColor) { - Array.Clear(_dotMasks); - Array.Fill(_fgColors, ColorRgb.White); + Array.Clear(_pixels); Array.Fill(_bgColors, bgColor); } - public void SetPixel(int subX, int subY, ColorRgb color) + public void SetPixel(int subX, int subY, ColorRgb color, CanvasLayer layer = CanvasLayer.Curve) { if (subX < 0 || subX >= SubpixelWidth || subY < 0 || subY >= SubpixelHeight) return; + if (layer == CanvasLayer.Curve) + { + if (color == ColorRgb.GridLine) layer = CanvasLayer.Grid; + else if (color == ColorRgb.AxisLine) layer = CanvasLayer.Axis; + } + int cellCol = subX / 2; int cellRow = subY / 4; int dotCol = subX % 2; int dotRow = subY % 4; + int dotIndex = (dotRow << 1) | dotCol; - int idx = cellRow * CellWidth + cellCol; - _dotMasks[idx] |= DotMap[dotRow, dotCol]; - _fgColors[idx] = color; + int pixelIdx = ((cellRow * CellWidth + cellCol) << 3) | dotIndex; + + if (layer >= _pixels[pixelIdx].Layer) + { + _pixels[pixelIdx] = new DotPixel(color, layer); + } + } + + public bool IsPixelSet(int subX, int subY) + { + if (subX < 0 || subX >= SubpixelWidth || subY < 0 || subY >= SubpixelHeight) + return false; + + int cellCol = subX / 2; + int cellRow = subY / 4; + int dotCol = subX % 2; + int dotRow = subY % 4; + int dotIndex = (dotRow << 1) | dotCol; + int pixelIdx = ((cellRow * CellWidth + cellCol) << 3) | dotIndex; + + return _pixels[pixelIdx].Layer != CanvasLayer.None; + } + + public (bool IsSet, ColorRgb Color, CanvasLayer Layer) GetPixel(int subX, int subY) + { + if (subX < 0 || subX >= SubpixelWidth || subY < 0 || subY >= SubpixelHeight) + return (false, ColorRgb.Background, CanvasLayer.None); + + int cellCol = subX / 2; + int cellRow = subY / 4; + int dotCol = subX % 2; + int dotRow = subY % 4; + int dotIndex = (dotRow << 1) | dotCol; + int pixelIdx = ((cellRow * CellWidth + cellCol) << 3) | dotIndex; + + ref readonly var p = ref _pixels[pixelIdx]; + return (p.Layer != CanvasLayer.None, p.Color, p.Layer); } public void SetCellBackground(int cellCol, int cellRow, ColorRgb color) @@ -90,7 +154,7 @@ public sealed class BrailleCanvas return code; } - public void DrawLine(int x0Int, int y0Int, int x1Int, int y1Int, ColorRgb color) + public void DrawLine(int x0Int, int y0Int, int x1Int, int y1Int, ColorRgb color, CanvasLayer layer = CanvasLayer.Curve) { double xmin = 0; double ymin = 0; @@ -176,7 +240,7 @@ public sealed class BrailleCanvas while (true) { - SetPixel(cx0, cy0, color); + SetPixel(cx0, cy0, color, layer); if (cx0 == cx1 && cy0 == cy1) break; @@ -195,7 +259,7 @@ public sealed class BrailleCanvas } } - public void DrawVerticalFill(int subX, int subY0, int subY1, ColorRgb color) + public void DrawVerticalFill(int subX, int subY0, int subY1, ColorRgb color, CanvasLayer layer = CanvasLayer.Shading) { if (subX < 0 || subX >= SubpixelWidth) return; @@ -205,7 +269,7 @@ public sealed class BrailleCanvas // For shaded areas, sample every 2nd or all subpixels for (int y = startY; y <= endY; y += 2) { - SetPixel(subX, y, color); + SetPixel(subX, y, color, layer); } } @@ -214,10 +278,64 @@ public sealed class BrailleCanvas if (cellCol < 0 || cellCol >= CellWidth || cellRow < 0 || cellRow >= CellHeight) return (' ', ColorRgb.White, ColorRgb.Background); - int idx = cellRow * CellWidth + cellCol; - byte mask = _dotMasks[idx]; + int cellIdx = cellRow * CellWidth + cellCol; + int cellOffset = cellIdx << 3; + + // 1. Find the highest priority layer present in this cell + CanvasLayer maxLayer = CanvasLayer.None; + for (int d = 0; d < 8; d++) + { + var l = _pixels[cellOffset + d].Layer; + if (l > maxLayer) + { + maxLayer = l; + } + } + + if (maxLayer == CanvasLayer.None) + { + return (' ', ColorRgb.White, _bgColors[cellIdx]); + } + + // 2. Identify the dominant color among the dots of maxLayer + ColorRgb dominantColor = default; + int maxCount = 0; + + for (int d = 0; d < 8; d++) + { + if (_pixels[cellOffset + d].Layer != maxLayer) continue; + + ColorRgb c = _pixels[cellOffset + d].Color; + int count = 0; + for (int k = 0; k < 8; k++) + { + if (_pixels[cellOffset + k].Layer == maxLayer && _pixels[cellOffset + k].Color == c) + { + count++; + } + } + + if (count > maxCount) + { + maxCount = count; + dominantColor = c; + } + } + + // 3. Construct the Braille mask using ONLY the dots that match maxLayer and dominantColor. + // This eliminates dot contamination, where background/grid dots would otherwise take over + // the graph's color and appear as stray points away from the actual line. + byte mask = 0; + for (int d = 0; d < 8; d++) + { + ref readonly var p = ref _pixels[cellOffset + d]; + if (p.Layer == maxLayer && p.Color == dominantColor) + { + mask |= DotIndexMask[d]; + } + } char ch = mask == 0 ? ' ' : (char)(0x2800 | mask); - return (ch, _fgColors[idx], _bgColors[idx]); + return (ch, dominantColor, _bgColors[cellIdx]); } } diff --git a/src/GraphTUI/Rendering/PlotRenderer.cs b/src/GraphTUI/Rendering/PlotRenderer.cs index 0b16ee5..f251662 100644 --- a/src/GraphTUI/Rendering/PlotRenderer.cs +++ b/src/GraphTUI/Rendering/PlotRenderer.cs @@ -67,7 +67,7 @@ public sealed class PlotRenderer { for (int x = 0; x < subW; x++) { - canvas.SetPixel(x, axisY, ColorRgb.AxisLine); + canvas.SetPixel(x, axisY, ColorRgb.AxisLine, CanvasLayer.Axis); } } @@ -76,7 +76,7 @@ public sealed class PlotRenderer { for (int y = 0; y < subH; y++) { - canvas.SetPixel(axisX, y, ColorRgb.AxisLine); + canvas.SetPixel(axisX, y, ColorRgb.AxisLine, CanvasLayer.Axis); } } } @@ -95,7 +95,7 @@ public sealed class PlotRenderer { for (int y = 0; y < subH; y += 4) { - canvas.SetPixel(subX, y, ColorRgb.GridLine); + canvas.SetPixel(subX, y, ColorRgb.GridLine, CanvasLayer.Grid); } } } @@ -109,7 +109,7 @@ public sealed class PlotRenderer { for (int x = 0; x < subW; x += 4) { - canvas.SetPixel(x, subY, ColorRgb.GridLine); + canvas.SetPixel(x, subY, ColorRgb.GridLine, CanvasLayer.Grid); } } } @@ -304,7 +304,7 @@ public sealed class PlotRenderer { if (System.Math.Abs(sy - prevDerivSy.Value) < subH / 2) { - canvas.DrawLine(prevDerivSx.Value, prevDerivSy.Value, sx, sy, derivColor); + canvas.DrawLine(prevDerivSx.Value, prevDerivSy.Value, sx, sy, derivColor, CanvasLayer.Annotation); } } prevDerivSx = sx; @@ -327,7 +327,7 @@ public sealed class PlotRenderer viewport.MathToSubpixel(xLeft, yLeft, subW, subH, out int sx0, out int sy0); viewport.MathToSubpixel(xRight, yRight, subW, subH, out int sx1, out int sy1); - canvas.DrawLine(sx0, sy0, sx1, sy1, ColorRgb.Yellow); + canvas.DrawLine(sx0, sy0, sx1, sy1, ColorRgb.Yellow, CanvasLayer.Annotation); } } } @@ -663,17 +663,17 @@ public sealed class PlotRenderer // Dashed horizontal line for (int x = 0; x < subW; x += 3) { - canvas.SetPixel(x, chY, ColorRgb.Magenta); + canvas.SetPixel(x, chY, ColorRgb.Magenta, CanvasLayer.CrosshairGuide); } // Dashed vertical line for (int y = 0; y < subH; y += 3) { - canvas.SetPixel(chX, y, ColorRgb.Magenta); + canvas.SetPixel(chX, y, ColorRgb.Magenta, CanvasLayer.CrosshairGuide); } // Central crosshair point - canvas.SetPixel(chX, chY, ColorRgb.White); + canvas.SetPixel(chX, chY, ColorRgb.White, CanvasLayer.Crosshair); } } diff --git a/tests/GraphTUI.Tests/BrailleCanvasTests.cs b/tests/GraphTUI.Tests/BrailleCanvasTests.cs index 06c18da..07b6dab 100644 --- a/tests/GraphTUI.Tests/BrailleCanvasTests.cs +++ b/tests/GraphTUI.Tests/BrailleCanvasTests.cs @@ -90,5 +90,73 @@ public static class BrailleCanvasTests Assert.Equal(20, sx1); Assert.Equal(20, sy1); }); + + runner.Add("BrailleCanvas: Dot Contamination Prevention", () => + { + var canvas = new BrailleCanvas(1, 1); + canvas.Clear(ColorRgb.Background); + + // 1. Grid dot at (0, 0) + canvas.SetPixel(0, 0, ColorRgb.GridLine, CanvasLayer.Grid); + + // Verify grid alone produces dot 0 in GridLine color + var (chGrid, fgGrid, _) = canvas.GetCell(0, 0); + Assert.Equal(ColorRgb.GridLine, fgGrid); + Assert.Equal((char)(0x2800 | 0x01), chGrid); + + // 2. Add Curve dot at (1, 3) in Cyan + canvas.SetPixel(1, 3, ColorRgb.Cyan, CanvasLayer.Curve); + + // The cell must now prioritize the Curve: + // - Foreground must be Cyan + // - Mask must ONLY contain the curve dot (1, 3) which is 0x80 + // - The grid dot (0, 0) which is 0x01 must NOT be in the mask (not contaminated) + var (chCurve, fgCurve, _) = canvas.GetCell(0, 0); + Assert.Equal(ColorRgb.Cyan, fgCurve); + Assert.Equal((char)(0x2800 | 0x80), chCurve); + Assert.True((chCurve & 0x01) == 0, "Grid dot 0x01 must NOT be contaminated into the curve mask"); + + // 3. Add Crosshair Guide dot at (0, 2) in Magenta + // Crosshair guide (layer 3) is lower than curve (layer 5), so curve dominates + canvas.SetPixel(0, 2, ColorRgb.Magenta, CanvasLayer.CrosshairGuide); + var (chAfterGuide, fgAfterGuide, _) = canvas.GetCell(0, 0); + Assert.Equal(ColorRgb.Cyan, fgAfterGuide); + Assert.Equal((char)(0x2800 | 0x80), chAfterGuide); + + // 4. Center Crosshair (layer 7) at (0, 1) in White dominates + canvas.SetPixel(0, 1, ColorRgb.White, CanvasLayer.Crosshair); + var (chCenter, fgCenter, _) = canvas.GetCell(0, 0); + Assert.Equal(ColorRgb.White, fgCenter); + Assert.Equal((char)(0x2800 | 0x02), chCenter); + }); + + runner.Add("PlotRenderer: Render functions without stray grid contamination", () => + { + var canvas = new BrailleCanvas(20, 20); + var vp = new Viewport(); + var fns = new List + { + new(1, "y = x", ColorRgb.Cyan) + }; + + // Render with grid enabled + PlotRenderer.Render(canvas, vp, fns, _ => 0, showGrid: true); + + // Check all cells: any cell with Cyan foreground should only have dots corresponding + // to the line, and must NOT contaminate background grid dots. + int cyanCells = 0; + for (int r = 0; r < canvas.CellHeight; r++) + { + for (int c = 0; c < canvas.CellWidth; c++) + { + var (ch, fg, _) = canvas.GetCell(c, r); + if (fg == ColorRgb.Cyan && ch != ' ') + { + cyanCells++; + } + } + } + Assert.True(cyanCells > 0, "Expected Cyan cells for line y=x"); + }); } }