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 int Build(Vector2 min, int size, float threshold, List <VertexPositionColorNormal> vertices, int grid_size) { this.position = min; this.size = size; this.type = QuadtreeNodeType.Internal; int v_index = 0; ConstructNodes(ref v_index, vertices, grid_size); return(v_index); }
public QuadtreeNode() { type = QuadtreeNodeType.None; position = Vector2.Zero; size = 0; children = new QuadtreeNode[4]; draw_info = new QuadtreeDrawInfo(); }
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 bool ConstructLeaf(ref int v_index, List<VertexPositionColorNormal> vertices, int grid_size) { int corners = 0; float[,] samples = new float[2, 2]; for (int i = 0; i < 4; i++) { if ((samples[i / 2, i % 2] = Sampler.Sample(position + new Vector2(i / 2, i % 2))) < 0) corners |= 1 << i; } if (corners == 0 || corners == 15) return false; QEF qef = new QEF(); Vector3 average_normal = new Vector3(); for (int i = 0; i < 4; i++) { int c1 = Sampler.Edges[i, 0]; int c2 = Sampler.Edges[i, 1]; int m1 = (corners >> c1) & 1; int m2 = (corners >> c2) & 1; if (m1 == m2) continue; float d1 = samples[c1 / 2, c1 % 2]; float d2 = samples[c2 / 2, c2 % 2]; Vector2 p1 = new Vector2((float)((c1 / 2)), (float)((c1 % 2))); Vector2 p2 = new Vector2((float)((c2 / 2)), (float)((c2 % 2))); Vector2 intersection = Sampler.GetIntersection(p1, p2, d1, d2); Vector2 normal = Sampler.GetNormal(intersection + position);//GetNormal(x, y); average_normal += new Vector3(normal, 0); qef.Add(intersection, normal); } average_normal /= (float)qef.Intersections.Count; average_normal.Normalize(); draw_info = new QuadtreeDrawInfo(); draw_info.position = qef.Solve2(0, 0, 0); draw_info.averageNormal = average_normal; draw_info.corners = corners; draw_info.index = v_index++; Color n_c = new Color(average_normal * 0.5f + Vector3.One * 0.5f); vertices.Add(new VertexPositionColorNormal(new Vector3(position * grid_size + draw_info.position * size * grid_size, 0), n_c, average_normal)); type = QuadtreeNodeType.Leaf; return true; }
public int Build(Vector2 min, int size, float threshold, List<VertexPositionColorNormal> vertices, int grid_size) { this.position = min; this.size = size; this.type = QuadtreeNodeType.Internal; int v_index = 0; ConstructNodes(ref v_index, vertices, grid_size); return v_index; }
public bool ConstructLeaf(ref int v_index, List <VertexPositionColorNormal> vertices, int grid_size) { int corners = 0; float[,] samples = new float[2, 2]; for (int i = 0; i < 4; i++) { if ((samples[i / 2, i % 2] = Sampler.Sample(position + new Vector2(i / 2, i % 2))) < 0) { corners |= 1 << i; } } if (corners == 0 || corners == 15) { return(false); } QEF qef = new QEF(); Vector3 average_normal = new Vector3(); for (int i = 0; i < 4; i++) { int c1 = Sampler.Edges[i, 0]; int c2 = Sampler.Edges[i, 1]; int m1 = (corners >> c1) & 1; int m2 = (corners >> c2) & 1; if (m1 == m2) { continue; } float d1 = samples[c1 / 2, c1 % 2]; float d2 = samples[c2 / 2, c2 % 2]; Vector2 p1 = new Vector2((float)((c1 / 2)), (float)((c1 % 2))); Vector2 p2 = new Vector2((float)((c2 / 2)), (float)((c2 % 2))); Vector2 intersection = Sampler.GetIntersection(p1, p2, d1, d2); Vector2 normal = Sampler.GetNormal(intersection + position); //GetNormal(x, y); average_normal += new Vector3(normal, 0); qef.Add(intersection, normal); } average_normal /= (float)qef.Intersections.Count; average_normal.Normalize(); draw_info = new QuadtreeDrawInfo(); draw_info.position = qef.Solve2(0, 0, 0); draw_info.averageNormal = average_normal; draw_info.corners = corners; draw_info.index = v_index++; Color n_c = new Color(average_normal * 0.5f + Vector3.One * 0.5f); vertices.Add(new VertexPositionColorNormal(new Vector3(position * grid_size + draw_info.position * size * grid_size, 0), n_c, average_normal)); type = QuadtreeNodeType.Leaf; return(true); }