%D \module
%D   [       file=m-meshes,
%D        version=2025.07.01,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=meshes,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is a preliminary module.

\startmodule[meshes]

\startluacode

moduledata.meshes = moduledata.meshes or { }

local context = context

local NC = context.NC
local BC = context.BC
local NR = context.NR

function moduledata.meshes.show(name,method,average,addtitle)
    local n = vector.meshname(name,method,average)
    local t = table.load(n)
    if t then
        local title = name .. " : " .. t.method.name .. " @ " .. t.average.name
        local state = t.state or { }
        --
        if addtitle ~= false then
            context.starttitle { title = title }
        end
            --
            local function some(key,value)
                BC() context(key) NC() context(value) NC() NR()
            end
            context.startcolumns { n = 3 }
                context.starttabulate { "|l|r|" }
                    some("N"        , t.M or 0)
                    some("M"        , t.N or 0)
                    some("method"   , t.method.id)
                    some("average"  , t.average.id)
                    some("points"   , t.points and #t.points or 0)
                    some("triangles", t.triangles and #t.triangles or 0)
                    some("found"    , t.overlap and table.count(t.overlap) or 0)
                    some("areas"    , t.areas and table.count(t.areas) or 0)
                    some("unique"   , t.unique and table.count(t.unique) or 0)
                    some("epsilon"  , t.epsilon or vector.getepsilon(true))
                context.stoptabulate()
            context.stopcolumns()
            --
            local function some(key,value)
                BC() context(key) NC() context("%0.9f",value) NC() NR()
            end
            context.startcolumns { n = 3 }
                context.starttabulate { "|l|r|" }
                    some("min x", state.minx)
                    some("max x", state.maxx)
                    some("min y", state.miny)
                    some("max y", state.maxy)
                    some("min z", state.minz)
                    some("max z", state.maxz)
                context.stoptabulate()
            context.stopcolumns()
            --
            context.startcolumns { n = 3 }
                context.starttabulate { "|r|r|l|" }
                for k, v in table.sortedhash(t.areas) do
                    BC() context(k)
                    NC() context(t.unique[k])
                    NC() context("%0.9f",v)
                    NC() NR()
                end
                context.stoptabulate()
            context.stopcolumns()
            --
            context.startcolumns { n = 4 }
                context.starttabulate { "|r|r|l|" }
                for k, v in table.sortedhash(t.overlap) do
                    BC() context(k)
                    NC() context(v[1])
                    NC() context(v[2])
                    NC() NR()
                end
                context.stoptabulate()
            context.stopcolumns()
            --
        if addtitle ~= false then
            context.stoptitle()
        end
    end


end

\stopluacode

\stopmodule
