|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Numerics; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +using Plotly; |
| 8 | +using Plotly.Types; |
| 9 | + |
| 10 | +using TRACES = Plotly.Box<Plotly.Types.ITracesProperty>; |
| 11 | + |
| 12 | +using VERTEX = SharpGLTF.Geometry.IVertexBuilder; |
| 13 | + |
| 14 | +namespace SharpGLTF |
| 15 | +{ |
| 16 | + public class PlotlyScene |
| 17 | + { |
| 18 | + #region data |
| 19 | + |
| 20 | + private readonly List<TRACES> _Traces = new List<TRACES>(); |
| 21 | + private readonly Dictionary<Object, int> mats = new Dictionary<Object, int>(); // materials to color mapping |
| 22 | + |
| 23 | + #endregion |
| 24 | + |
| 25 | + #region API |
| 26 | + |
| 27 | + public void AppendTriangles<TMaterial>(IEnumerable<(VERTEX A, VERTEX B, VERTEX C, TMaterial Material)> tris, Matrix4x4 xform, Func<TMaterial, int> materialColorFunc) |
| 28 | + { |
| 29 | + var trace = _CreateTrace(tris, xform, materialColorFunc); |
| 30 | + |
| 31 | + _Traces.Add(trace); |
| 32 | + } |
| 33 | + |
| 34 | + public Box<IPlotProperty> ToPlotProperties() { return Plot.traces(_Traces.ToArray()); } |
| 35 | + |
| 36 | + public Plot ToPlot() |
| 37 | + { |
| 38 | + var plot = ToPlotProperties(); |
| 39 | + var layout = _CreateLayoutProperties(); |
| 40 | + |
| 41 | + var document = new Plot(plot, layout); |
| 42 | + return document; |
| 43 | + } |
| 44 | + |
| 45 | + public string ToHtml() |
| 46 | + { |
| 47 | + Plot document = ToPlot(); |
| 48 | + return document.Render().ToString(); |
| 49 | + } |
| 50 | + |
| 51 | + #endregion |
| 52 | + |
| 53 | + #region core |
| 54 | + |
| 55 | + private static Box<IPlotProperty> _CreateLayoutProperties() |
| 56 | + { |
| 57 | + var xaxis = Scene.xaxis(Xaxis.color("red")); |
| 58 | + var yaxis = Scene.yaxis(Yaxis.color("green")); |
| 59 | + var zaxis = Scene.zaxis(Zaxis.color("blue")); |
| 60 | + var camera = Camera.up(Up.x(0), Up.y(1), Up.z(0)); |
| 61 | + var scene = Layout.scene(Scene.Aspectmode.data(), Scene.camera(camera), xaxis, yaxis, zaxis); |
| 62 | + |
| 63 | + return Plot.layout |
| 64 | + (Layout.autosize(true) |
| 65 | + // , Layout.width(0) |
| 66 | + , Layout.height(920) |
| 67 | + // , Layout.margin(Margin.autoexpand(true)) |
| 68 | + // , Layout.margin(Margin.pad(5)) |
| 69 | + // , Layout.margin(Margin.t(5), Margin.b(5)) |
| 70 | + , scene |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private TRACES _CreateTrace<TMaterial>(IEnumerable<(VERTEX A, VERTEX B, VERTEX C, TMaterial Material)> tris, Matrix4x4 xform, Func<TMaterial, int> materialColorFunc) |
| 75 | + { |
| 76 | + var vrts = new List<(Vector3 p, Vector3 n)>(); // vertex list |
| 77 | + var vrtm = new Dictionary<(Vector3 p, Vector3 n), int>(); // vertex sharing map |
| 78 | + |
| 79 | + var idxs = new List<(int, int, int)>(); // triangle indices |
| 80 | + var tric = new List<int>(); // face colors |
| 81 | + |
| 82 | + int _useSharedVertex(VERTEX v) |
| 83 | + { |
| 84 | + var g = v.GetGeometry(); |
| 85 | + |
| 86 | + g.TryGetNormal(out Vector3 n); |
| 87 | + var item = (g.GetPosition(), n); |
| 88 | + if (vrtm.TryGetValue(item, out int idx)) return idx; |
| 89 | + idx = vrts.Count; |
| 90 | + vrts.Add(item); |
| 91 | + vrtm.Add(item, idx); |
| 92 | + return idx; |
| 93 | + } |
| 94 | + |
| 95 | + foreach (var (A, B, C, Material) in tris) |
| 96 | + { |
| 97 | + if (!mats.TryGetValue(Material, out int color)) |
| 98 | + { |
| 99 | + mats[Material] = color = materialColorFunc(Material); |
| 100 | + } |
| 101 | + |
| 102 | + var ap = _useSharedVertex(A); |
| 103 | + var bp = _useSharedVertex(B); |
| 104 | + var cp = _useSharedVertex(C); |
| 105 | + |
| 106 | + idxs.Add((ap, bp, cp)); |
| 107 | + tric.Add(color); |
| 108 | + } |
| 109 | + |
| 110 | + // create a Plotly Mesh3D from the previously filled lists. |
| 111 | + |
| 112 | + var mx = Mesh3d.x(vrts.Select(item => item.p.X)); |
| 113 | + var my = Mesh3d.y(vrts.Select(item => item.p.Y)); |
| 114 | + var mz = Mesh3d.z(vrts.Select(item => item.p.Z)); |
| 115 | + |
| 116 | + var mi = Mesh3d.i(idxs.Select(item => item.Item1).ToArray()); |
| 117 | + var mj = Mesh3d.j(idxs.Select(item => item.Item2).ToArray()); |
| 118 | + var mk = Mesh3d.k(idxs.Select(item => item.Item3).ToArray()); |
| 119 | + |
| 120 | + var mo = Mesh3d.opacity(1); |
| 121 | + var mc = Mesh3d.facecolor(tric.ToArray()); |
| 122 | + |
| 123 | + return Traces.mesh3d(mx, my, mz, mi, mj, mk, mo, mc); |
| 124 | + } |
| 125 | + |
| 126 | + #endregion |
| 127 | + } |
| 128 | +} |
0 commit comments