public bool ConstructNodes(ref int v_index, List <VertexPositionColorNormal> vertices, int grid_size) { if (size == 1) { return(ConstructLeaf(ref v_index, vertices, grid_size)); } int child_size = size / 2; bool has_children = false; for (int i = 0; i < 4; i++) { Vector2 offset = new Vector2(i / 2, i % 2); QuadtreeNode child = new QuadtreeNode(); child.size = child_size; child.position = position + offset * (float)child_size; child.type = QuadtreeNodeType.Internal; if (child.ConstructNodes(ref v_index, vertices, grid_size)) { has_children = true; } children[i] = child; } if (!has_children) { type = QuadtreeNodeType.Leaf; return(false); } type = QuadtreeNodeType.Internal; return(true); }
/* The code for generating the contour * It has connectivity issues due to table errors * TODO: Fix */ public void ProcessFace(List <int> indexes) { if (type == QuadtreeNodeType.Internal) { for (int i = 0; i < 4; i++) { if (children[i] != null) { children[i].ProcessFace(indexes); } } for (int i = 0; i < 4; i++) { QuadtreeNode c1 = children[edges[i, 0]]; QuadtreeNode c2 = children[edges[i, 1]]; if (c1 == null || c2 == null) { continue; } ProcessEdge(c1, c2, i / 2, indexes); } } }
public ADC(GraphicsDevice device, int resolution, int size) : base(device, resolution, size, false) { map = new float[resolution, resolution]; vertex_indexes = new int[resolution, resolution]; tree = new QuadtreeNode(); }
public ADC(GraphicsDevice device, int resolution, int size) : base(device, resolution, size, false) { map = new float[resolution, resolution]; vertex_indexes = new int[resolution, resolution]; tree = new QuadtreeNode(); }
public QuadtreeNode() { type = QuadtreeNodeType.None; position = Vector2.Zero; size = 0; children = new QuadtreeNode[4]; draw_info = new QuadtreeDrawInfo(); }
public void ConstructTreeGrid(QuadtreeNode node) { if (node == null) { return; } VertexPositionColor[] vs = new VertexPositionColor[16]; int x = (int)node.position.X * this.Size; int y = (int)node.position.Y * this.Size; Color c = Color.LightSteelBlue; Color v = Color.Red; float size = node.size * this.Size; vs[0] = new VertexPositionColor(new Vector3(x + 0 * size, y + 0 * size, 0), c); vs[1] = new VertexPositionColor(new Vector3(x + 1 * size, y + 0 * size, 0), c); vs[2] = new VertexPositionColor(new Vector3(x + 1 * size, y + 0 * size, 0), c); vs[3] = new VertexPositionColor(new Vector3(x + 1 * size, y + 1 * size, 0), c); vs[4] = new VertexPositionColor(new Vector3(x + 1 * size, y + 1 * size, 0), c); vs[5] = new VertexPositionColor(new Vector3(x + 0 * size, y + 1 * size, 0), c); vs[6] = new VertexPositionColor(new Vector3(x + 0 * size, y + 1 * size, 0), c); vs[7] = new VertexPositionColor(new Vector3(x + 0 * size, y + 0 * size, 0), c); if (node.type != QuadtreeNodeType.Leaf || node.draw_info.index == -1) { OutlineBuffer.SetData <VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 8, VertexPositionColor.VertexDeclaration.VertexStride); OutlineLocation += 8; } else { x += (int)(node.draw_info.position.X * (float)this.Size); y += (int)(node.draw_info.position.Y * (float)this.Size); float r = 2; vs[8] = new VertexPositionColor(new Vector3(x - r, y - r, 0), v); vs[9] = new VertexPositionColor(new Vector3(x + r, y - r, 0), v); vs[10] = new VertexPositionColor(new Vector3(x + r, y - r, 0), v); vs[11] = new VertexPositionColor(new Vector3(x + r, y + r, 0), v); vs[12] = new VertexPositionColor(new Vector3(x + r, y + r, 0), v); vs[13] = new VertexPositionColor(new Vector3(x - r, y + r, 0), v); vs[14] = new VertexPositionColor(new Vector3(x - r, y + r, 0), v); vs[15] = new VertexPositionColor(new Vector3(x - r, y - r, 0), v); OutlineBuffer.SetData <VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 16, VertexPositionColor.VertexDeclaration.VertexStride); OutlineLocation += 16; } if (node.type != QuadtreeNodeType.Leaf) { for (int i = 0; i < 4; i++) { ConstructTreeGrid(node.children[i]); } } }
public void ConstructTreeGrid(QuadtreeNode node) { if (node == null) return; VertexPositionColor[] vs = new VertexPositionColor[16]; int x = (int)node.position.X * this.Size; int y = (int)node.position.Y * this.Size; Color c = Color.LightSteelBlue; Color v = Color.Red; float size = node.size * this.Size; vs[0] = new VertexPositionColor(new Vector3(x + 0 * size, y + 0 * size, 0), c); vs[1] = new VertexPositionColor(new Vector3(x + 1 * size, y + 0 * size, 0), c); vs[2] = new VertexPositionColor(new Vector3(x + 1 * size, y + 0 * size, 0), c); vs[3] = new VertexPositionColor(new Vector3(x + 1 * size, y + 1 * size, 0), c); vs[4] = new VertexPositionColor(new Vector3(x + 1 * size, y + 1 * size, 0), c); vs[5] = new VertexPositionColor(new Vector3(x + 0 * size, y + 1 * size, 0), c); vs[6] = new VertexPositionColor(new Vector3(x + 0 * size, y + 1 * size, 0), c); vs[7] = new VertexPositionColor(new Vector3(x + 0 * size, y + 0 * size, 0), c); if (node.type != QuadtreeNodeType.Leaf || node.draw_info.index == -1) { OutlineBuffer.SetData<VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 8, VertexPositionColor.VertexDeclaration.VertexStride); OutlineLocation += 8; } else { x += (int)(node.draw_info.position.X * (float)this.Size); y += (int)(node.draw_info.position.Y * (float)this.Size); float r = 2; vs[8] = new VertexPositionColor(new Vector3(x - r, y - r, 0), v); vs[9] = new VertexPositionColor(new Vector3(x + r, y - r, 0), v); vs[10] = new VertexPositionColor(new Vector3(x + r, y - r, 0), v); vs[11] = new VertexPositionColor(new Vector3(x + r, y + r, 0), v); vs[12] = new VertexPositionColor(new Vector3(x + r, y + r, 0), v); vs[13] = new VertexPositionColor(new Vector3(x - r, y + r, 0), v); vs[14] = new VertexPositionColor(new Vector3(x - r, y + r, 0), v); vs[15] = new VertexPositionColor(new Vector3(x - r, y - r, 0), v); OutlineBuffer.SetData<VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 16, VertexPositionColor.VertexDeclaration.VertexStride); OutlineLocation += 16; } if (node.type != QuadtreeNodeType.Leaf) { for (int i = 0; i < 4; i++) { ConstructTreeGrid(node.children[i]); } } }
public static void ProcessEdge(QuadtreeNode node1, QuadtreeNode node2, int direction, List<int> indexes) { if (node1 == null || node2 == null) return; if (node1.size != 1 || node2.size != 1 || node1.draw_info.index == -1 || node2.draw_info.index == -1) { QuadtreeNode leaf1; QuadtreeNode leaf2; for (int i = 0; i < 2; i++) { if (node1.size == 1 && node1.draw_info.index != -1) leaf1 = node1; else { int c = edge_mask[direction, i, 0]; leaf1 = node1.children[c]; } if (node2.size == 1 && node2.draw_info.index != -1) leaf2 = node2; else { int c = edge_mask[direction, i, 1]; leaf2 = node2.children[c]; } ProcessEdge(leaf1, leaf2, direction, indexes); } } else { if (node1.draw_info.index == -1 || node2.draw_info.index == -1) { return; } int min_size = 100000; bool sign_change = false; QuadtreeNode[] nodes = new QuadtreeNode[] { node1, node2 }; for (int i = 0; i < 2; i++) { int edge = process_edge_mask[direction, i]; int c1 = edges[edge, 0]; int c2 = edges[edge, 1]; int m1 = (nodes[i].draw_info.corners >> c1) & 1; int m2 = (nodes[i].draw_info.corners >> c2) & 1; /* We have connectivity issues, so sign change is improperly set to favor connectivity * As a result, cells that shouldn't connect do * TODO: Fix */ //if (nodes[i].size <= min_size) { min_size = nodes[i].size; if (!sign_change) sign_change = m1 != m2; } } if (sign_change) { indexes.Add(node1.draw_info.index); indexes.Add(node2.draw_info.index); } } }
public bool ConstructNodes(ref int v_index, List<VertexPositionColorNormal> vertices, int grid_size) { if (size == 1) { return ConstructLeaf(ref v_index, vertices, grid_size); } int child_size = size / 2; bool has_children = false; for (int i = 0; i < 4; i++) { Vector2 offset = new Vector2(i / 2, i % 2); QuadtreeNode child = new QuadtreeNode(); child.size = child_size; child.position = position + offset * (float)child_size; child.type = QuadtreeNodeType.Internal; if (child.ConstructNodes(ref v_index, vertices, grid_size)) has_children = true; children[i] = child; } if (!has_children) { type = QuadtreeNodeType.Leaf; return false; } type = QuadtreeNodeType.Internal; return true; }
public static void ProcessEdge(QuadtreeNode node1, QuadtreeNode node2, int direction, List <int> indexes) { if (node1 == null || node2 == null) { return; } if (node1.size != 1 || node2.size != 1 || node1.draw_info.index == -1 || node2.draw_info.index == -1) { QuadtreeNode leaf1; QuadtreeNode leaf2; for (int i = 0; i < 2; i++) { if (node1.size == 1 && node1.draw_info.index != -1) { leaf1 = node1; } else { int c = edge_mask[direction, i, 0]; leaf1 = node1.children[c]; } if (node2.size == 1 && node2.draw_info.index != -1) { leaf2 = node2; } else { int c = edge_mask[direction, i, 1]; leaf2 = node2.children[c]; } ProcessEdge(leaf1, leaf2, direction, indexes); } } else { if (node1.draw_info.index == -1 || node2.draw_info.index == -1) { return; } int min_size = 100000; bool sign_changed = false; QuadtreeNode[] nodes = new QuadtreeNode[] { node1, node2 }; for (int i = 0; i < 2; i++) { int edge = process_edge_mask[direction, i]; int c1 = edges[edge, 0]; int c2 = edges[edge, 1]; int m1 = (nodes[i].draw_info.corners >> c1) & 1; int m2 = (nodes[i].draw_info.corners >> c2) & 1; /* We have connectivity issues, so sign change is improperly set to favor connectivity * As a result, cells that shouldn't connect do * TODO: Fix */ if (nodes[i].size < min_size) { min_size = nodes[i].size; sign_changed = ((m1 == 0 && m2 != 0) || (m1 != 0 && m2 == 0)); } } if (sign_changed) { indexes.Add(node1.draw_info.index); indexes.Add(node2.draw_info.index); } } }