public void Insert (RecastMeshObj mesh) {
var box = new RecastBBTreeBox(mesh);
if (root == null) {
root = box;
return;
}
RecastBBTreeBox c = root;
while (true) {
c.rect = ExpandToContain(c.rect, box.rect);
if (c.mesh != null) {
//Is Leaf
c.c1 = box;
var box2 = new RecastBBTreeBox(c.mesh);
c.c2 = box2;
c.mesh = null;
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;
}
}
}
}