public void Insert (MeshNode node) {
BBTreeBox box = new BBTreeBox (this,node);
if (root == null) {
root = box;
return;
}
BBTreeBox c = root;
while (true) {
c.rect = ExpandToContain (c.rect,box.rect);
if (c.node != null) {
//Is Leaf
c.c1 = box;
BBTreeBox box2 = new BBTreeBox (this,c.node);
//Console.WriteLine ("Inserted "+box.node+", rect "+box.rect.ToString ());
c.c2 = box2;
c.node = null;
//c.rect = c.rect.
return;
} else {
float e1 = ExpansionRequired (c.c1.rect,box.rect);
float e2 = ExpansionRequired (c.c2.rect,box.rect);
//Choose the rect requiring the least expansion to contain box.rect
if (e1 < e2) {
c = c.c1;
} else if (e2 < e1) {
c = c.c2;
} else {
//Equal, Choose the one with the smallest area
c = RectArea (c.c1.rect) < RectArea (c.c2.rect) ? c.c1 : c.c2;
}
}
}
}