Stil stuck on drawing SVGs from a folder

trying to work the ofxsvg and directory list examples into one project that reads from a folder of of svgs and then outlines them on the screen.

It runs, but it just keeps drawing the same thing, the very first image in the folder.

q: why am I unable to iterate through the files in my folder?

I think I’m not getting something pretty fundamental about how reading and drawing works…

details

I have a series of svgs in a folder in bin/data.

variables

float step;
vector<ofPolyline> outlines;
vector<ofxSVG> images;
ofDirectory dir;
int currentImage;

my setup looks like this

void ofApp::setup(){
    
    ofSetVerticalSync(true);
    ofBackground(0);
    ofSetColor(255);
    ofSetFrameRate(60);

    


    dir.listDir("svgs");
    if ( dir.size()){
        images.assign(dir.size(), ofxSVG());
    }
    
    for(int i = 0; i < (int)dir.size(); i++){
        images[i].load(dir.getPath(i));
    }
    
    currentImage = 0;
    
    for (int i = 0; i < images[currentImage].getNumPath(); i++){
        
        ofPath p = images[currentImage].getPathAt(i);
        p.setPolyWindingMode(OF_POLY_WINDING_ODD);
        vector<ofPolyline> & lines = const_cast<vector<ofPolyline>&>(p.getOutline());
        
        for(int j=0;j<(int)lines.size();j++){
            outlines.push_back(lines[j].getResampledBySpacing(1));
        }
    }
    
}

then in update I say

if (step > 1) {
//from example
        currentImage++;
        currentImage %= dir.size();
        step -= 1;
    }

the draw function is exactly like the svg example, stripped down to the part I need…

void ofApp::draw(){
    
    ofPushMatrix();
    
    ofNoFill();
    
    for (int i = 0; i < (int)outlines.size(); i++){
        ofPolyline & line = outlines[i];
        
        int num = step * line.size();
        
        ofBeginShape();
        for (int j = 0; j < num; j++){
            ofVertex(line[j]);
        }
        ofEndShape();
    }

    ofPopMatrix();

any insight would be much appreciated :slight_smile:

Hi there,

If, in svgExample, vector <ofPolyline> outlines; creates one group of polylines to draw one object, then you need something like vector <vector<ofPolyline>> outlines; so that you can have groups of groups of polylines.

After you load you SVG, you only loop through the first one: currentImage is always 0. You need to nest that loop inside another one and do the proper changes to you code.

hey, thanks for the hints. So now I’m working towards setting up everything, so that draw just cycles through a vector where all my vectors of the paths are stored. I like that, I get that.

But I’ve tried a lot of different ways to do it and end up at the same problem every time: I get a runtime error that says bad access and outputs (lldb). For that, I find explanations about memory use (or solutions that point to something with xcode, but I doubt thats the problem…)

to get my head around it

   /*the group vector will have as many elemnts as there are pictures in the folder.
each picture is stored as a collection of paths. 
each path is simplified to a polyline (the outline of the path)*/
            //for every picture in the folder
                //for every path in the picture
                    //add all the polylines

maybe I’m not understanding how polylines are const and thats tripping me up?

void ofApp::setup(){

    ofSetVerticalSync(true);
    
    ofBackground(0);
    ofSetColor(255);
    
    dir.listDir("svgs");
    if ( dir.size()){
        images.assign(dir.size(), ofxSVG());
    }
    for(int d = 0; d < (int)dir.size(); d++){
        images[d].load(dir.getPath(d));
    }
    
    for (int c =0; c<images.size(); c++){
        
        for (int i = 0; i < images[c].getNumPath(); i++){
        
        ofPath p = images[c].getPathAt(i);
        // svg defaults to non zero winding which doesn't look so good as contours
        p.setPolyWindingMode(OF_POLY_WINDING_ODD);
        //lines is a refrence, remove the const from getOutline results (const bc of oF core stuff?)
        vector<ofPolyline>& lines = const_cast< vector<ofPolyline>& > (p.getOutline());
        
            for(int j=0;j<(int)lines.size();j++){
                outlines.push_back(lines[j].getResampledBySpacing(1));
            }

        }
        cout<<outlines.size() <<endl;
//and then the simplest imaginable solution...fails
        outlinesGroup[c]=outlines;
    }
}

o_O

Hi,

Im still stuck on this problem. I have a vector of vectors of polylines called outlines group, and I’m trying to add a vector of polylines to it like so:

outlinesGroup[c]=outlines;

which trips up the program. Details in my post above. Any hints would be great.

came across this project again and thought I’d update this in case anyone finds themselves here:

I just added

outlinesGroup.assign(images.size(),vector<ofPolyline>());

before the loop populating the outlinesGroup vector and it solved the runtime error…

cheers