// build tree of boxes as sequential array void build_sequential(DCurve3 curve) { int NV = curve.VertexCount; int N = (curve.Closed) ? NV : NV - 1; int boxCount = 0; layers = 0; layer_counts = new List <int>(); // count how many boxes in each layer, building up from initial segments int bi = 0; while (N > 1) { int layer_boxes = (N / 2) + (N % 2 == 0 ? 0 : 1); boxCount += layer_boxes; N = layer_boxes; layer_counts.Add(layer_boxes); bi += layer_boxes; layers++; } // [RMS] this case happens if N = 1, previous loop is skipped and we have to // hardcode initialization to this redundant box if (layers == 0) { layers = 1; boxCount = 1; layer_counts = new List <int>() { 1 }; } boxes = new Box3d[boxCount]; bi = 0; // make first layer int NStop = (curve.Closed) ? NV : NV - 1; for (int si = 0; si < NStop; si += 2) { Vector3d v1 = curve[(si + 1) % NV]; var seg1 = new Segment3d(curve[si], v1); var box = new Box3d(seg1); if (si < NV - 1) { var seg2 = new Segment3d(v1, curve[(si + 2) % NV]); var box2 = new Box3d(seg2); box = Box3d.Merge(ref box, ref box2); } boxes[bi++] = box; } // repeatedly build layers until we hit a single box N = bi; if (N == 1) { return; } int prev_layer_start = 0; bool done = false; while (done == false) { int layer_start = bi; for (int k = 0; k < N; k += 2) { var mbox = Box3d.Merge(ref boxes[prev_layer_start + k], ref boxes[prev_layer_start + k + 1]); boxes[bi++] = mbox; } N = (N / 2) + (N % 2 == 0 ? 0 : 1); prev_layer_start = layer_start; if (N == 1) { done = true; } } }
// build tree of boxes as sequential array void build_sequential(DCurve3 curve) { int NV = curve.VertexCount; int N = NV; int boxCount = 0; layers = 0; layer_counts = new List <int>(); // count how many boxes in each layer, building up from initial segments int bi = 0; while (N > 1) { int layer_boxes = (N / 2) + (N % 2 == 0 ? 0 : 1); boxCount += layer_boxes; N = layer_boxes; layer_counts.Add(layer_boxes); bi += layer_boxes; layers++; } boxes = new Box3d[boxCount]; bi = 0; // make first layer for (int si = 0; si < NV; si += 2) { Vector3d v1 = curve[(si + 1) % NV]; Segment3d seg1 = new Segment3d(curve[si], v1); Box3d box = new Box3d(seg1); if (si < NV - 1) { Segment3d seg2 = new Segment3d(v1, curve[(si + 2) % NV]); Box3d box2 = new Box3d(seg2); box = Box3d.Merge(ref box, ref box2); } boxes[bi++] = box; } // repeatedly build layers until we hit a single box N = bi; int prev_layer_start = 0; bool done = false; while (done == false) { int layer_start = bi; for (int k = 0; k < N; k += 2) { Box3d mbox = Box3d.Merge(ref boxes[prev_layer_start + k], ref boxes[prev_layer_start + k + 1]); boxes[bi++] = mbox; } N = (N / 2) + (N % 2 == 0 ? 0 : 1); prev_layer_start = layer_start; if (N == 1) { done = true; } } }