90 lines
1.8 KiB
Odin

package halfedge
import "core:container/small_array"
Vec3 :: [3]f32
Vertex :: struct {
pos: Vec3,
edge: i16,
}
Face :: struct {
edge: i16,
}
Vertex_Index :: distinct u16
Face_Index :: distinct i16
Edge_Index :: distinct i16
Half_Edge :: struct {
origin: Vertex_Index,
twin: Edge_Index,
face: Face_Index,
next: Edge_Index,
prev: Edge_Index,
}
Mesh :: struct {
vertices: []Vertex,
faces: []Face,
edges: []Half_Edge,
}
mesh_from_vertex_index_list :: proc(vertices: []Vec3, indices: []u16, allocator := context.allocator) {
vertices: [dynamic]Vertex
faces: [dynamic]Face
edges: [dynamic]Half_Edge
temp_edges: map[[2]u16]small_array.Small_Array(2, Edge_Index)
triangle_num := len(indices) / 3
for i in 0..<triangle_num {
i1, i2, i3 := indices[i * 3 + 0], indices[i * 3 + 1], indices[i * 3 + 2]
v1, v2, v3 := vertices[i1], vertices[i2], vertices[i3]
e1 := len(edges)
e2, e3 := e1 + 1, e1 + 2
new_edges := [3]Half_Edge{
{
origin = Vertex_Index(i1),
twin = -1,
face = Face_Index(i),
next = Edge_Index(e2),
prev = Edge_Index(e3),
},
{
origin = Vertex_Index(i2),
twin = -1,
face = Face_Index(i),
next = Edge_Index(e3),
prev = Edge_Index(e1),
},
{
origin = Vertex_Index(i3),
twin = -1,
face = Face_Index(i),
next = Edge_Index(e1),
prev = Edge_Index(e2),
},
}
append(&edges, ..new_edges[:])
temp_indices: [][2]u16 = {
{min(i1, i2), max(i1, i2)},
{min(i2, i3), max(i2, i3)},
{min(i3, i1), max(i3, i1)},
}
for tmp_idx, j in temp_indices {
assert(temp_edges[tmp_idx].len < 2)
if temp_edges[tmp_idx].len > 0 {
// small_array.push_back(Edge_Index(e1 + j))
}
}
}
}