Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
using System.Diagnostics;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Aetheris.Kernel.Core.Math;
using Aetheris.Kernel.Firmament.Assembly;
using Aetheris.Semantics;
using Aetheris.Kernel.Firmament.Modules;
using Aetheris.Kernel.Firmament.FirmamentV2.Syntax;
namespace Aetheris.Kernel.Firmament.Scene;
public sealed record SceneBoundary(string Path, string Room, string Kind, double[] Frame, double WidthMm, double HeightMm,
IReadOnlyList<string> Openings);
public sealed record SceneNode(string Path, string Kind, string DefinitionIdentity, string PlacementAuthority,
SemanticSourceSpan Span, string? PatternKey = null);
public sealed record ScenePerformance(double BindMilliseconds, double DefinitionCompilationMilliseconds, double CompositionMilliseconds,
int UniqueEngineeringDefinitions, int ReusedEngineeringDefinitions, int RebuiltEngineeringDefinitions, double DisplayPreparationMilliseconds);
public sealed record CompiledScene(SceneSource Source, AssemblyDisplayMeshDocument Display, IReadOnlyList<SceneBoundary> Boundaries,
IReadOnlyList<SceneNode> Nodes, IReadOnlyDictionary<string, AssemblyAppearanceBinding> Appearances,
IReadOnlyList<AssemblyDefinitionReuse> Reuse, ScenePerformance Performance, double[] MinimumMm, double[] MaximumMm);
public sealed record SceneCompilationResult(CompiledScene? Scene, IReadOnlyList<AssemblyDiagnostic> Diagnostics)
{ public bool IsSuccess => Scene is not null && Diagnostics.All(d => d.Severity != AssemblyDiagnosticSeverity.Error); }
/// <summary>Retained scene composition reuses the existing immutable STEP definition cache.
/// Each independently engineered assembly keeps its own ordinary compiler/session.
/// Scene owns display/environment and occurrence state, never a BRep product.</summary>
public sealed class FirmamentSceneSession : IDisposable
{
private readonly object gate = new();
private readonly AssemblyDefinitionCache parts = new(256);
private readonly Dictionary<string,FirmamentCompilationSession> assemblies = new(StringComparer.OrdinalIgnoreCase);
// BUILD-PERF-M2: display definitions are pure functions of their inputs (a box of its key; a
// part of its content key and id; an assembly-derived definition of the assembly session's
// prepared definition and its scene id), so a rebuild reuses the same objects. Reference
// identity is what lets the web runtime send unchanged definitions as retained stubs.
private readonly DerivedValueCache<AssemblyDisplayMeshDefinition> sceneDefinitions = new();
private readonly DerivedValueCache<(AssemblyDisplayMeshDefinition Source,AssemblyDisplayMeshDefinition Derived)> assemblyDefinitions = new();
private readonly Dictionary<string,FirmamentSceneSession> nestedScenes = new(StringComparer.Ordinal);
private bool disposed;
public SceneCompilationResult CompileFile(string path) => Compile(File.ReadAllText(path),Path.GetFullPath(path));
public SceneCompilationResult CompileProject(FirmamentProjectSnapshot project) => project.TryResolve(project.RootDocument, out var source)
? Compile(source, project.RootDocument, project)
: new(null, [new("scene-project-root-missing", project.RootDocument)]);
public SceneCompilationResult Compile(string source, string sourceIdentity = "<memory>", FirmamentProjectSnapshot? project = null)
{
FirmamentModuleGraph? graph = null;
if (FirmamentModuleGraph.HasImports(source))
{
graph = FirmamentModuleGraph.Load(source, sourceIdentity, project);
if (graph.Diagnostics.Count > 0) return new(null,graph.Diagnostics);
}
return CompileBound(source,sourceIdentity,project,graph,graph?.Root);
}
private SceneCompilationResult CompileBound(string source,string sourceIdentity,FirmamentProjectSnapshot? project,
FirmamentModuleGraph? graph,FirmamentModuleGraph.Module? module,DeclarationSyntax? selected = null)
{
var projection = graph is null ? null : graph.ProjectSource(module!,selected);
source = projection?.Text ?? source;
lock (gate)
{
ObjectDisposedException.ThrowIf(disposed,this);
var watch = Stopwatch.StartNew(); var parsed = SceneAuthoring.Parse(source,sourceIdentity);
if (projection is not null) parsed=SceneAuthoring.ApplyModuleContext(parsed,graph!,projection);
if (!parsed.IsSuccess) return new(null,parsed.Diagnostics);
var bindMs=watch.Elapsed.TotalMilliseconds; watch.Restart(); parts.BeginBuild(); sceneDefinitions.BeginBuild(); assemblyDefinitions.BeginBuild();
var s=parsed.Source!; var diagnostics=parsed.Diagnostics.ToList();
var definitions=new Dictionary<string,AssemblyDisplayMeshDefinition>(StringComparer.Ordinal);
var occurrences=new List<AssemblyDisplayMeshOccurrence>(); var nodes=new List<SceneNode>();
var looks=new Dictionary<string,AssemblyAppearanceBinding>(StringComparer.Ordinal);
var compiledScenes=new Dictionary<string,CompiledScene>(StringComparer.Ordinal);
var compiledAssemblies=new Dictionary<string,AssemblyM1CompilationResult>(StringComparer.OrdinalIgnoreCase);
var preparedAssemblies=new Dictionary<string,AssemblyDisplayMeshDocument>(StringComparer.OrdinalIgnoreCase);
double displayMs=0;
var materializedParts=new Dictionary<string,MaterializedAssemblyDefinition>(StringComparer.Ordinal);
foreach (var item in s.Occurrences)
{
if (graph?.TryReference(item.Definition,out var imported) == true && item.Kind == "Scene")
{
if (!nestedScenes.TryGetValue(imported.Identity,out var session))
{
if (nestedScenes.Count >= 256) { Error("scene-definition-session-capacity-exceeded","At most 256 imported Scene definitions can be retained."); continue; }
nestedScenes[imported.Identity]=session=new();
}
var child=session.CompileBound(imported.Module!.Text,imported.Path,project,graph,imported.Module,imported.Export);
diagnostics.AddRange(child.Diagnostics);
if (child.Scene is not null) compiledScenes[imported.Identity]=child.Scene;
}
else if (graph?.TryReference(item.Definition,out imported) == true && item.Kind == "Assembly")
{
if (!assemblies.TryGetValue(imported.Identity,out var session))
{
if (assemblies.Count >= 256) { Error("scene-definition-session-capacity-exceeded","At most 256 imported Assembly definitions can be retained."); continue; }
assemblies[imported.Identity]=session=new();
}
if (!compiledAssemblies.ContainsKey(imported.Identity))
{
var child=session.CompileModule(graph,imported);
compiledAssemblies[imported.Identity]=child;
diagnostics.AddRange(child.Diagnostics);
}
}
else if (item.Kind == "Assembly")
{
var match=Regex.Match(item.Definition,"^AssemblyFile<\\s*\"(?<path>[^\"]+)\"\\s*>$");
if (!match.Success) { Error("scene-assembly-definition-invalid","X0 assembly occurrences require AssemblyFile<\"relative.firmament\">; their document compiles independently."); continue; }
var path=AssemblyPathFor(match.Groups["path"].Value);
if (path is null) continue;
if (project is null)
{
var allowedRoot=AssemblyM0Parser.FindAllowedSourceRoot(Path.GetFullPath(sourceIdentity));
var allowedPrefix=Path.TrimEndingDirectorySeparator(allowedRoot)+Path.DirectorySeparatorChar;
if (!path.StartsWith(allowedPrefix,StringComparison.OrdinalIgnoreCase))
{ Error("scene-assembly-file-outside-root","AssemblyFile must stay inside the existing allowed source root."); continue; }
}
if (!path.EndsWith(".firmament",StringComparison.OrdinalIgnoreCase)) { Error("scene-assembly-file-invalid","AssemblyFile requires a .firmament source document."); continue; }
if (!compiledAssemblies.ContainsKey(path))
{
if (project is null ? !File.Exists(path) : !project.TryResolve(path, out _)) { Error("scene-assembly-file-missing",path); continue; }
if (!assemblies.TryGetValue(path,out var session))
{
if (assemblies.Count >= 256) { Error("scene-definition-session-capacity-exceeded","The retained Scene session supports at most 256 independent Assembly sources; start a fresh session."); continue; }
assemblies[path]=session=new();
}
var assembly=project is null ? session.CompileFile(path) : session.CompileProject(new FirmamentProjectSnapshot(path, project.Documents)); compiledAssemblies.Add(path,assembly);
diagnostics.AddRange(assembly.Diagnostics);
}
}
else if (item.Kind == "Scene")
Error("scene-definition-unresolved", "A Scene occurrence requires a bound Inventory Scene entry.");
else if (!materializedParts.ContainsKey(item.Definition))
{
var part=graph?.TryReference(item.Definition,out var importedPart) == true
? AssemblyDefinitionMaterializer.MaterializeModule(graph,importedPart,item.Definition,diagnostics,parts)
: parts.Materialize(item.Definition,s.DefinitionSource,sourceIdentity,diagnostics,project,
() => AssemblyDefinitionMaterializer.TryMaterialize(item.Definition,s.DefinitionSource,sourceIdentity,diagnostics,project));
if (part is not null) materializedParts.Add(item.Definition,part);
}
}
var definitionMs=watch.Elapsed.TotalMilliseconds; watch.Restart();
if (diagnostics.Any(d => d.Severity == AssemblyDiagnosticSeverity.Error)) return new(null,diagnostics);
var boundaries=s.Rooms.SelectMany(RoomBoundaries).ToArray();
var layoutFrames=s.LayoutFrames.ToDictionary(f => f.Identity,StringComparer.Ordinal);
var resolvedFrames=new Dictionary<string,Transform3D>(StringComparer.Ordinal);
Transform3D? ResolveFrame(string path,HashSet<string> active)
{
if (path == "World") return Transform3D.Identity;
if (boundaries.SingleOrDefault(b => b.Path == path) is { } boundary) return Transform3D.FromRowMajor(boundary.Frame);
if (resolvedFrames.TryGetValue(path,out var cached)) return cached;
if (!layoutFrames.TryGetValue(path,out var layout))
{ Error("scene-frame-unresolved",$"Unknown Scene target frame '{path}'. Use World, a Room boundary or a Concept datum."); return null; }
if (!active.Add(path)) { Error("scene-frame-cycle",$"Scene Concept frame cycle at '{path}'."); return null; }
var basis=ResolveFrame(layout.Transform.From,active);
active.Remove(path);
if (basis is null) return null;
var frame=AssemblyFrameAuthoring.Compose(basis.Value,layout.Transform,diagnostics);
resolvedFrames[path]=frame;
return frame;
}
// Validate unused guides too, so invalid scaffolding cannot hide behind a
// placement edit. They never become display/product occurrences.
foreach (var layout in s.LayoutFrames) ResolveFrame(layout.Identity,[]);
AddNode(s.Name,"Scene",s.Name,null,Transform3D.Identity.ToRowMajor(),null,new(sourceIdentity,0,source.Length));
foreach (var room in s.Rooms)
{
var roomPath=s.Name+"."+room.Name; var transform=Transform3D.CreateTranslation(new(room.AtMm[0],room.AtMm[1],room.AtMm[2]));
AddNode(roomPath,"Room",room.Name,s.Name,transform.ToRowMajor(),null,room.Span);
foreach (var boundary in boundaries.Where(b => b.Room == room.Name))
{
var wallPath=s.Name+"."+boundary.Path;
AddNode(wallPath,"SceneBoundary",boundary.Path,roomPath,transform.ToRowMajor(),null,room.Span);
foreach (var panel in Panels(room,boundary.Kind,s.Openings.Where(o => o.Boundary == boundary.Path).ToArray()))
{
var key="scene-box:"+string.Join(":",panel.Size.Select(F)); var id=Id(key);
if (!definitions.ContainsKey(id)) definitions.Add(id,sceneDefinitions.GetOrAdd(id,() => WithShader(Box(id,key,panel.Size))));
var panelPath=wallPath+".panel"+panel.Index;
var world=Transform3D.CreateTranslation(new(panel.At[0],panel.At[1],panel.At[2]))*transform;
AddNode(panelPath,"EnvironmentPanel",key,wallPath,world.ToRowMajor(),id,room.Span);
ApplyLook(panelPath,boundary.Kind switch { "floor" => room.FloorAppearance ?? room.Appearance, "ceiling" => room.CeilingAppearance ?? room.Appearance, _ => room.Appearance });
}
foreach (var opening in s.Openings.Where(o => o.Boundary == boundary.Path))
{
var frame=Transform3D.FromRowMajor(boundary.Frame);
var aperture=Transform3D.CreateTranslation(new(opening.AlongMm,opening.SillMm,0))*frame;
var openingPath=wallPath+"."+opening.Name;
AddNode(openingPath,opening.Kind,opening.Name,wallPath,aperture.ToRowMajor(),null,opening.Span);
foreach (var piece in WindowPieces(opening,room.ThicknessMm,boundary.Kind))
{
var key="scene-box:"+string.Join(":",piece.Size.Select(F)); var id=Id(key);
if (!definitions.ContainsKey(id)) definitions.Add(id,sceneDefinitions.GetOrAdd(id,() => WithShader(Box(id,key,piece.Size))));
var path=openingPath+"."+piece.Name;
var world=Transform3D.CreateTranslation(new(piece.At[0],piece.At[1],piece.At[2]))*aperture;
AddNode(path,"WindowFinish",key,openingPath,world.ToRowMajor(),id,opening.Span);
ApplyLook(path,piece.Appearance);
}
}
}
}
foreach (var item in s.Occurrences)
{
// Existing frame composition owns rotation/basis/translation; no Mate is synthesized.
var target=ResolveFrame(item.Placement.From,[]);
if (target is null) continue;
var world=AssemblyFrameAuthoring.Compose(target.Value,item.Placement,diagnostics);
var segments=item.Path.Split('.');
for (var n=2;n<segments.Length;n++)
{
var group=string.Join('.',segments.Take(n));
if (!occurrences.Any(o => o.Path == group)) AddNode(group,"PatternGroup",group,string.Join('.',segments.Take(n-1)),Transform3D.Identity.ToRowMajor(),null,item.Span,item.PatternKey);
}
var parent=string.Join('.',segments.SkipLast(1));
if (item.Kind == "Part")
{
if (item.From != "Origin") { Error("scene-part-source-frame-unsupported","X0 Part Scene placement starts at Origin; assembly published source frames are supported."); continue; }
var part=materializedParts[item.Definition]; var id=Id("part:"+item.Definition);
if (!definitions.ContainsKey(id))
{
var preparation=Stopwatch.StartNew();
AssemblyDisplayMeshDefinition Prepare() => AssemblyDisplayMeshExporter.PrepareDefinition(id,item.Definition,part.Body, externalStep: part.Artifact.Provenance.Any(p => p.Stage == "imported-step-definition")) with { Cir = part.Cir is null ? null : part.Cir with { DefinitionId = id }, GeometryRevision = part.Artifact.StepSha256 };
definitions.Add(id,part.InputKey is { } contentKey ? sceneDefinitions.GetOrAdd("part|"+contentKey+"|"+id,() => WithShader(Prepare())) : Prepare());
displayMs+=preparation.Elapsed.TotalMilliseconds;
}
AddNode(item.Path,"Part",item.Definition,parent,world.ToRowMajor(),id,item.Span,item.PatternKey); ApplyLook(item.Path,item.Appearance);
}
else if (item.Kind == "Scene")
{
if (item.From != "Origin") { Error("scene-source-frame-unresolved","An imported Scene is placed from Origin."); continue; }
if (!compiledScenes.TryGetValue(item.Definition,out var child)) { Error("scene-definition-unresolved",item.Definition); continue; }
var prefix=Id(item.Definition);
foreach (var d in child.Display.Definitions)
{
var id=prefix+":"+d.Id;
definitions.TryAdd(id,Derive(d,id,item.Definition));
}
var childRoot=child.Source.Name;
foreach (var o in child.Display.Occurrences)
{
var scenePath=item.Path+o.Path[childRoot.Length..];
var parentPath=o.ParentId is null ? parent : item.Path+child.Display.Occurrences.Single(p => p.Id == o.ParentId).Path[childRoot.Length..];
var childNode=child.Nodes.Single(n => n.Path == o.Path);
AddNode(scenePath,childNode.Kind,childNode.DefinitionIdentity,parentPath,
(Transform3D.FromRowMajor(o.Transform)*world).ToRowMajor(),o.DefinitionId is null ? null : prefix+":"+o.DefinitionId,childNode.Span,item.PatternKey);
if (child.Appearances.TryGetValue(o.Id,out var look)) looks[Id(scenePath)]=look;
ApplyLook(scenePath,item.Appearance);
}
boundaries=boundaries.Concat(child.Boundaries.Select(b => b with { Path=item.Path+"."+b.Path,Room=item.Path+"."+b.Room,
Frame=(Transform3D.FromRowMajor(b.Frame)*world).ToRowMajor(),Openings=b.Openings.Select(o => item.Path+"."+o).ToArray() })).ToArray();
}
else
{
var match=Regex.Match(item.Definition,"\"(?<path>[^\"]+)\"");
var path=graph?.TryReference(item.Definition,out _) == true ? item.Definition : AssemblyPathFor(match.Groups["path"].Value)!;
var assembly=compiledAssemblies[path];
if (!preparedAssemblies.TryGetValue(path,out var mesh))
{ var preparation=Stopwatch.StartNew(); preparedAssemblies[path]=mesh=assemblies[path].ExportDisplayMesh(assembly); displayMs+=preparation.Elapsed.TotalMilliseconds; }
if (item.From != "Origin")
{
var root=assembly.Ir!.Instances.Single(i => i.ParentStableId is null);
var reference=AssemblyPath.Parse(root.Path+"."+item.From);
if (!AssemblyM0Compiler.TryResolve(reference,assembly.Ir.Instances,out var resolved) || !resolved!.Value.TryBinding<ExactDatumFrameBinding>(out var binding))
{ Error("scene-source-frame-unresolved",$"Assembly does not publish source frame '{item.From}'."); continue; }
world=AssemblyFrameAuthoring.Matrix(binding).Inverse()*world;
}
var prefix=Id(path); var rootPath=mesh.Occurrences.Single(o => o.ParentId is null).Path;
foreach (var d in mesh.Definitions)
{
var id=prefix+":"+d.Id;
if (definitions.ContainsKey(id)) continue;
var derived=assemblyDefinitions.GetOrAdd(id,() => (d,Derive(d,id,path)));
if (!ReferenceEquals(derived.Source,d)) { assemblyDefinitions.Forget(id); derived=assemblyDefinitions.GetOrAdd(id,() => (d,Derive(d,id,path))); }
definitions.Add(id,derived.Derived);
}
foreach (var o in mesh.Occurrences)
{
var scenePath=item.Path+o.Path[rootPath.Length..];
var parentPath=o.ParentId is null ? parent : item.Path+mesh.Occurrences.Single(p => p.Id == o.ParentId).Path[rootPath.Length..];
var instance=assembly.Ir!.Instances.Single(i => i.StableId == o.Id);
AddNode(scenePath,instance.Kind.ToString(),instance.DefinitionIdentity,parentPath,(Transform3D.FromRowMajor(o.Transform)*world).ToRowMajor(),o.DefinitionId is null ? null : prefix+":"+o.DefinitionId,item.Span,item.PatternKey);
if (instance.Appearance is { } look) looks[Id(scenePath)]=look;
ApplyLook(scenePath,item.Appearance);
}
}
}
if (diagnostics.Any(d => d.Severity == AssemblyDiagnosticSeverity.Error)) return new(null,diagnostics);
var display=new AssemblyDisplayMeshDocument("aetheris/scene-display-mesh/1",s.Name,"mm",definitions.Values.ToArray(),occurrences);
var bounds=WorldBounds(display);
var reuse=parts.Evidence.Concat(compiledAssemblies.Values.SelectMany(a => a.Reuse?.Definitions ?? [])).ToArray();
var perf=new ScenePerformance(bindMs,definitionMs,watch.Elapsed.TotalMilliseconds-displayMs,reuse.Length,reuse.Count(r => r.Reused),reuse.Count(r => !r.Reused),displayMs);
return new(new(s,display,boundaries,nodes,looks,reuse,perf,bounds.Min,bounds.Max),diagnostics);
IEnumerable<SceneBoundary> RoomBoundaries(SceneRoom room)
{
var w=room.SizeMm[0]; var d=room.SizeMm[1]; var h=room.SizeMm[2];
foreach (var (kind,frame,width,height) in new (string,double[],double,double)[] {
("floor",[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],w,d),
("ceiling",[1,0,0,0,0,1,0,0,0,0,1,0,0,0,h,1],w,d),
("southWall",[1,0,0,0,0,0,1,0,0,-1,0,0,0,0,0,1],w,h),
("northWall",[1,0,0,0,0,0,1,0,0,-1,0,0,0,d,0,1],w,h),
("westWall",[0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,1],d,h),
("eastWall",[0,1,0,0,0,0,1,0,1,0,0,0,w,0,0,1],d,h) })
{
var world=Transform3D.FromRowMajor(frame)*Transform3D.CreateTranslation(new(room.AtMm[0],room.AtMm[1],room.AtMm[2]));
var path=room.Name+"."+kind;
yield return new(path,room.Name,kind,world.ToRowMajor(),width,height,s.Openings.Where(o => o.Boundary == path).Select(o => o.Name).ToArray());
}
}
AssemblyDisplayMeshDefinition Derive(AssemblyDisplayMeshDefinition d,string id,string path) =>
d with { Id=id, Identity=path+"::"+d.Identity, Cir = d.Cir is null ? null : d.Cir with { DefinitionId = id } };
void Error(string code,string message) => diagnostics.Add(new(code,message));
string? AssemblyPathFor(string relative)
{
try
{
return project is null
? Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(sourceIdentity))!, relative))
: FirmamentProjectSnapshot.NormalizePath((Path.GetDirectoryName(sourceIdentity)?.Replace('\\','/') is { Length: > 0 } directory ? directory+"/" : "")+relative);
}
catch (ArgumentException exception) { Error("scene-assembly-file-outside-root", exception.Message); return null; }
}
void AddNode(string path,string kind,string definition,string? parent,double[] transform,string? def,SemanticSourceSpan span,string? key=null)
{ occurrences.Add(new(Id(path),path,parent is null ? null : Id(parent),def,transform,path)); nodes.Add(new(path,kind,definition,"SceneFrame",span,key)); }
void ApplyLook(string path,string? look)
{ if (look is not null) looks[Id(path)]=new("","",look,s.Looks.Appearances[look].Preview,"scene-appearance"); }
}
}
public void Dispose() { lock(gate) { foreach (var session in assemblies.Values) session.Dispose(); assemblies.Clear(); foreach(var child in nestedScenes.Values) child.Dispose(); nestedScenes.Clear(); parts.Clear(); sceneDefinitions.Clear(); assemblyDefinitions.Clear(); disposed=true; } }
// The shader DisplayProjection would attach; attached once here so the cached object is final.
private static AssemblyDisplayMeshDefinition WithShader(AssemblyDisplayMeshDefinition d) =>
d.Shader is null ? d with { Shader = CirShaderArtifactProvider.Resolve(d.Cir) } : d;
private static string F(double value) => value.ToString("R",CultureInfo.InvariantCulture);
private static string Id(string value) => "scene:"+Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(value)))[..24];
private sealed record Panel(int Index,double[] At,double[] Size);
private sealed record WindowPiece(string Name,double[] At,double[] Size,string Appearance);
private static IEnumerable<WindowPiece> WindowPieces(SceneOpening opening,double wallThickness,string wall)
{
if (opening.Finish is not { } finish) yield break;
var w=opening.WidthMm; var h=opening.HeightMm; var b=finish.FrameWidthMm;
// Boundary chart +Z is -Y for north/south and +X for east/west.
var center=wall is "southWall" or "eastWall" ? wallThickness/2 : -wallThickness/2;
if (finish.FrameAppearance is { } frame)
{
var z=center-wallThickness/2;
yield return new("leftJamb",[0,0,z],[b,h,wallThickness],frame);
yield return new("rightJamb",[w-b,0,z],[b,h,wallThickness],frame);
yield return new("sill",[b,0,z],[w-2*b,b,wallThickness],frame);
yield return new("head",[b,h-b,z],[w-2*b,b,wallThickness],frame);
}
if (finish.GlazingAppearance is { } glass)
yield return new("pane",[b,b,center-finish.GlassThicknessMm/2],[w-2*b,h-2*b,finish.GlassThicknessMm],glass);
}
private static IEnumerable<Panel> Panels(SceneRoom r,string wall,IReadOnlyList<SceneOpening> openings)
{
var w=r.SizeMm[0]; var d=r.SizeMm[1]; var h=r.SizeMm[2]; var t=r.ThicknessMm;
if (wall is "floor" or "ceiling") { yield return new(0,[0,0,wall == "floor" ? -t : h],[w,d,t]); yield break; }
var horizontal=wall is "southWall" or "northWall"; var width=horizontal ? w : d;
var xs=new[] {0d,width}.Concat(openings.SelectMany(o => new[] {o.AlongMm,o.AlongMm+o.WidthMm})).Distinct().Order().ToArray();
var zs=new[] {0d,h}.Concat(openings.SelectMany(o => new[] {o.SillMm,o.SillMm+o.HeightMm})).Distinct().Order().ToArray();
var index=0;
for(var x=0;x+1<xs.Length;x++) for(var z=0;z+1<zs.Length;z++)
{
if (openings.Any(o => (xs[x]+xs[x+1])/2 > o.AlongMm && (xs[x]+xs[x+1])/2 < o.AlongMm+o.WidthMm && (zs[z]+zs[z+1])/2 > o.SillMm && (zs[z]+zs[z+1])/2 < o.SillMm+o.HeightMm)) continue;
yield return horizontal ? new(index++,[xs[x],wall == "southWall" ? -t : d,zs[z]],[xs[x+1]-xs[x],t,zs[z+1]-zs[z]])
: new(index++,[wall == "westWall" ? -t : w,xs[x],zs[z]],[t,xs[x+1]-xs[x],zs[z+1]-zs[z]]);
}
}
private static AssemblyDisplayMeshDefinition Box(string id,string identity,double[] s)
{
double[][] p=[[0,0,0],[s[0],0,0],[s[0],s[1],0],[0,s[1],0],[0,0,s[2]],[s[0],0,s[2]],[s[0],s[1],s[2]],[0,s[1],s[2]]];
int[][] faces=[[0,3,2,1],[4,5,6,7],[0,1,5,4],[3,7,6,2],[0,4,7,3],[1,2,6,5]];
double[][] normals=[[0,0,-1],[0,0,1],[0,-1,0],[0,1,0],[-1,0,0],[1,0,0]];
var positions=new List<double>(); var ns=new List<double>(); var indices=new List<int>();
for(var f=0;f<6;f++) { var start=positions.Count/3; foreach(var v in faces[f]) { positions.AddRange(p[v]); ns.AddRange(normals[f]); } indices.AddRange([start,start+1,start+2,start,start+2,start+3]); }
// Room panels and window pieces own rectangular boundary geometry. Export its
// twelve real edges alongside the faces, never the triangulation diagonals.
var edgePairs = faces.SelectMany(face => Enumerable.Range(0, face.Length)
.Select(i => (A: System.Math.Min(face[i], face[(i + 1) % face.Length]),
B: System.Math.Max(face[i], face[(i + 1) % face.Length]))))
.Distinct().OrderBy(edge => edge.A).ThenBy(edge => edge.B).ToArray();
return new(id,identity,positions.ToArray(),ns.ToArray(),indices.ToArray(),"SceneRectangularBoundary") {
GeometryRevision = identity,
Edges = edgePairs.Select((edge, index) => new DisplayProjectionEdge($"boundary-edge:{index}", [p[edge.A],p[edge.B]], false)).ToArray()
};
}
private static (double[] Min,double[] Max) WorldBounds(AssemblyDisplayMeshDocument d)
{
var min=new[] {double.PositiveInfinity,double.PositiveInfinity,double.PositiveInfinity}; var max=new[] {double.NegativeInfinity,double.NegativeInfinity,double.NegativeInfinity};
var defs=d.Definitions.ToDictionary(v => v.Id);
foreach(var o in d.Occurrences.Where(o => o.DefinitionId is not null))
{
var p=defs[o.DefinitionId!].Positions; var t=Transform3D.FromRowMajor(o.Transform);
for(var i=0;i<p.Length;i+=3) { var point=t.Apply(new Point3D(p[i],p[i+1],p[i+2])); var xyz=new[] {point.X,point.Y,point.Z}; for(var a=0;a<3;a++) { min[a]=Math.Min(min[a],xyz[a]); max[a]=Math.Max(max[a],xyz[a]); } }
}
return double.IsFinite(min[0]) ? (min,max) : (new double[3],new double[3]);
}
}