void QuadNode::addItem(QuadItem* item) {

    if(allowMoreItems()) {
        tree->item_count++;
        item->node_count++;
        items.push_back(item);
        return;
    }

    if(!children.empty()) {
        addToChild(item);
        return;
    }

    vec2 average = bounds.centre();

    vec2 middle = average - bounds.min;

    vec2 relmax = bounds.max-bounds.min;

    Bounds2D newbounds;

    children.reserve(4);

    //top left
    newbounds = Bounds2D( bounds.min + vec2(0.0, 0.0), bounds.min + middle );
    children.push_back(new QuadNode(tree, this, newbounds, depth));

    //top right
    newbounds = Bounds2D( bounds.min + vec2(middle.x, 0.0), bounds.min + vec2(relmax.x,middle.y) );
    children.push_back(new QuadNode(tree, this, newbounds, depth));

    //bottom left
    newbounds = Bounds2D( bounds.min + vec2(0.0, middle.y), bounds.min + vec2(middle.x,relmax.y) );
    children.push_back(new QuadNode(tree, this, newbounds, depth));

    //bottom right
    newbounds = Bounds2D( bounds.min + middle, bounds.max );
    children.push_back(new QuadNode(tree, this, newbounds, depth));

    for(std::list<QuadItem*>::iterator it = items.begin(); it != items.end(); it++) {
        QuadItem* oi = *it;
        tree->item_count--;
        oi->node_count--;
        addToChild(oi);
    }

    items.clear();

    addToChild(item);
}
示例#2
0
void Area::split() {
    this->children = new Area*[8];
    for (int i = 0; i < 8; ++i) {
        this->children[i] = new Area();
        this->children[i]->parent = this;
        this->children[i]->p_depth = this->p_depth + 1;

        this->children[i]->area_size = this->area_size / 2;
        this->children[i]->area_x = this->area_x;
        this->children[i]->area_y = this->area_y;
        this->children[i]->area_z = this->area_z;

        if (i % 2 == 1)
			this->children[i]->area_x += this->children[i]->area_size;
		if (i % 4 >= 2)
			this->children[i]->area_z += this->children[i]->area_size;
		if (i >= 4)
			this->children[i]->area_y += this->children[i]->area_size;
    }

    Object *tmp_obj = objects_head;
    while (objects_head != nullptr) {
        tmp_obj = objects_head;
        objects_head = tmp_obj->next();

        addToChild(tmp_obj);
    }

    objects_count = 0;
    objects_head = objects_tail = nullptr;
}
示例#3
0
void Area::addObject(Object *obj) {
    if (children != nullptr) {
        addToChild(obj);
        return;
    }

    if (objects_tail != nullptr) {
        objects_tail->setNext(obj);
    } else {
        objects_head = obj;
    }
    objects_tail = obj;

    obj->p_area = this;
    obj->setNext(nullptr);

    if (++objects_count > P_SPLIT_AT && p_depth < P_MAX_DEPTH) {
        split();
    }
}