Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

ofNode EXC_BAD_ACCESS

$
0
0

@edapx wrote:

Hi all, I'm implementing the space colonization algorithm in OF.

I'm wanted to use ofNode to keep track of the branches and parents, but I receive this error

What I've done until now, is to implement a branch class like this:

#include "ofMain.h"

class Branch{
public:
    Branch(ofVec3f _position, ofVec3f _direction);
    void setPosition(ofVec3f _pos);

    ofVec3f direction;
    ofNode node;
};

//in .cpp
#include "branch.h"

Branch::Branch(const ofVec3f _position, const ofVec3f _direction){
    this->setPosition(_position);
    direction = _direction;
}

void Branch::setPosition(ofVec3f pos){
    this->node.move(pos);
}

And a tree class like this one

#include "ofMain.h"
#include "leaf.h"
#include "branch.h"

class Tree {

public:
    Tree();
    void draw();

private:
    vector<Leaf> leaves;
    std::vector<shared_ptr<Branch> > branches;
    int n_leaves = 5;
    int max_dist = 20;
    int min_dist = 13;
};

//in cpp
#include "tree.h"

Tree::Tree() {
    auto rootPos = ofVec3f(ofGetWidth()/2, ofGetHeight(), 0);
    auto rootDir = ofVec3f(0, -10, 0);
    shared_ptr<Branch> root(new Branch(rootPos, rootDir));
    branches.push_back(root);

    for (int i =0; i<n_leaves; i++) {
        leaves.push_back(Leaf());
    }

    bool found = false;
    while(!found){
        auto current = *branches.back();
        ofVec3f currentPosition = current.node.getPosition();
        for(auto l:leaves){
            l.draw();
            float distance = currentPosition.distance(l.getPosition());
            cout << distance << endl;
            if(distance < max_dist){
                found = true;
            }
        }

        if (!found){
            auto newPos = current.node.getGlobalPosition() + current.direction;
            shared_ptr<Branch> nextBranch(new Branch(newPos, current.direction));
            auto parent = *branches.back();
            nextBranch->node.setParent(parent.node);
            branches.push_back(nextBranch);
        }
    }
}

As you see I'm using a vector of shared pointer, and I'm assuming that the current branch is always the last one inserted in the branches container. Each time I add a new branch, I set as parent the previous one.
Someone has an idea about what I'm doing wrong?
The whole code is on github

Posts: 2

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles