1 module creator.medit.mesh;
2 import inochi2d;
3 import inochi2d.core.dbg;
4 import bindbc.opengl;
5     
6 struct MeshVertex {
7     vec2 position;
8     MeshVertex*[] connections;
9 
10     bool isConnectedTo(MeshVertex* other) {
11         if (other == null) return false;
12 
13         foreach(conn; other.connections) {
14             if (*conn == this) return true;
15         }
16         return false;
17     }
18 
19     void connect(MeshVertex* other) {
20         this.connections ~= other;
21         other.connections ~= &this;
22     }
23 }
24 
25 class IncMesh {
26 private:
27     MeshData* data;
28     MeshVertex*[] vertices;
29     bool changed;
30 
31     vec3[] pData;
32     vec3[] pDataSel; // TODO: selection
33     void pRegen() {
34         pData = new vec3[vertices.length];
35         foreach(i, point; vertices) {
36             pData[i] = vec3(point.position, 0);
37         }
38         inDbgSetBuffer(pData);
39     }
40 
41     vec3[] lData;
42     ushort[] lIndices;
43     void lRegen() {
44 
45     }
46 
47     void mImport(ref MeshData data) {
48         // Reset vertex length
49         vertices.length = 0;
50 
51         // Iterate over flat mesh and extract it in to
52         // vertices and "connections"
53         MeshVertex*[ushort] iVertices;
54         foreach(i; 0..data.indices.length/3) {
55             auto index = data.indices[i*3];
56             auto nindex = data.indices[(i*3)+1];
57             auto nnindex = data.indices[(i*3)+2];
58             if (nnindex !in iVertices) iVertices[nnindex] = new MeshVertex(data.vertices[nnindex], []);
59             if (nindex !in iVertices) iVertices[nindex] = new MeshVertex(data.vertices[nindex], []);
60             if (index !in iVertices) iVertices[index] = new MeshVertex(data.vertices[index], []);
61 
62             if (!iVertices[index].isConnectedTo(iVertices[nindex])) iVertices[index].connect(iVertices[nindex]);
63             if (!iVertices[nindex].isConnectedTo(iVertices[nnindex])) iVertices[nindex].connect(iVertices[nnindex]);
64             if (!iVertices[index].isConnectedTo(iVertices[nnindex])) iVertices[index].connect(iVertices[nnindex]);
65         }
66 
67         foreach(vertex; iVertices) {
68             vertices ~= vertex;
69         }
70 
71         pRegen();
72         lRegen();
73     }
74 
75     MeshData mExport() {
76 
77         return *data;
78     }
79 
80 public:
81 
82     /**
83         Constructs a new IncMesh
84     */
85     this(ref MeshData mesh) {
86         data = &mesh;
87         mImport(mesh);
88     }
89 
90     /**
91         Exports the working mesh to a MeshData object.
92     */
93     MeshData export_() {
94         return mExport();
95     }
96 
97     /**
98         Resets mesh to prior state
99     */
100     void reset() {
101         mImport(*data);
102     }
103 
104     /**
105         Removes all vertices from the mesh
106     */
107     void clear() {
108         vertices.length = 0;
109     }
110 }