Points from ofPath

Hello,

I having been having trouble extracting the points from an ofPath object.

I created an ofPath from a set of points by using ‘lineto’.
Then, I carried out a series of actions such as translate, rotate and scale.

Now I need to extract the new points from the ofPath object.

Can anyone suggest a way of doing this?

hrm…if you’re using ofRotate/translate/scale then you’re not actually changing the point values but how they are displayed with an opengl translation, so I don’t know if its going to be super easy to get those

If you’re using the ofPath rotate/translate/scale you might need to add something in there that returns the vector of points that it is manipulating

The only ‘get’ thing im finding in ofPath.h is this:

vector & getSubPaths();
const vector & getSubPaths() const;

what i would do is use ofMatrix4x4 to create rotation and translation matrices.
then you multiply your points by these matrices and get returned your rotated+translated point value.

you can also use the ofmatrix4x4 in combination with ofQuaternion to then apply it to a push pop situation.

something like this:

  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	ofVec3f myPoint = ofVec3f(200,200,-50);  
	  
	ofVec3f fromVec = ofVec3f(0,0,1);  
	float angle = ofMap(mouseX,0,ofGetWidth(),-180,180);  
	ofMatrix4x4 mR1;  
	mR1.makeRotationMatrix(angle,fromVec);  
	  
	  
	ofVec3f translateVec = ofVec3f(100,100,-100);  
	ofMatrix4x4 mT;  
	mT.makeTranslationMatrix(translateVec);  
  
	  
	ofMatrix4x4 comboMatrix = mR1 * mT;  
	  
	ofFill();  
	ofSetColor(255, 0, 0);  
	ofCircle(myPoint, 15);  
	  
	ofSetColor(0, 0, 255);  
	ofCircle(myPoint*comboMatrix, 15);  
	  
	  
	ofPushMatrix();  
	  
	float x0,y0,z0;  
	float angle0;  
	ofVec3f temp_translateVec;  
	  
	ofQuaternion mP = comboMatrix.getRotate();  
	mP.getRotate(angle0,x0,y0,z0);  
	  
	temp_translateVec = comboMatrix.getTranslation();  
	  
ofTranslate(temp_translateVec);  
	ofRotate(angle0,x0,y0,z0);  
	  
	ofTranslate(myPoint);  
  
		  
	ofSetColor(0);  
	ofNoFill();  
	ofCircle(0,0,0,20);  
	  
	ofPopMatrix();  
}  
  
  
  
  

Wanted to do something similar so I dug around the internals and scrapped together this

  
  
#pragma once  
  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
	public:  
		void setup();  
		void update();  
		void draw();  
		  
		void keyPressed(int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y);  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void dragEvent(ofDragInfo dragInfo);  
		void gotMessage(ofMessage msg);  
		ofPolyline circlePolyline;  
	void drawCircle(float x, float y, float z,  float radius);  
	vector<ofPoint> circlePoints;  
	ofPath path;  
	//vector<ofPolyline> paths;  
	vector<ofSubPath::Command> commands;  
	int commandIndex;  
	ofPolyline polyline;  
};  
  
  

testApp.cpp

  
  
#include "testApp.h"  
  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
	int circleResolution = 6;  
	circlePolyline.arc(0,0,0,1,1,0,360,circleResolution);  
	circlePoints.resize(circlePolyline.size());  
	path.setFilled(false);  
  
	for (int i=0; i< 20; i++)   
	{  
		for (int j=0; j< 20; j++)   
		{  
			drawCircle(i*40, j*40, 0, 20);  
		}  
  
	}  
	  
	vector<ofSubPath>& subPaths = path.getSubPaths();  
	for (int i=0; i<subPaths.size(); i++)   
	{  
		vector<ofSubPath::Command>& pathCommands = subPaths[i].getCommands();  
		for (int j=0; j<pathCommands.size(); j++)  
		{  
			commands.push_back(pathCommands[j]);  
		}  
		  
	}  
	  
	commandIndex =0;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	ofSetWindowTitle(ofToString(ofGetFrameRate()));  
	if (ofGetFrameNum()%60 == 0)   
	{  
		if (commandIndex+1<commands.size())   
		{  
			commandIndex++;  
		}else {  
			commandIndex = 0;  
		}  
	}  
	polyline.clear();  
	int curveResolution = ofGetStyle().curveResolution;  
	//maybe a better way?  
	ofPath tempPath;  
	int arcResolution = tempPath.getArcResolution();  
	for(int i=0; i<commandIndex; i++)  
	{  
		switch(commands[i].type)  
		{  
			case ofSubPath::Command::lineTo:  
				polyline.addVertex(commands[i].to);  
				break;  
			case ofSubPath::Command::curveTo:  
				polyline.curveTo(commands[i].to, curveResolution);  
				break;  
			case ofSubPath::Command::bezierTo:  
				polyline.bezierTo(commands[i].cp1,commands[i].cp2,commands[i].to, curveResolution);  
				break;  
			case ofSubPath::Command::quadBezierTo:  
				polyline.quadBezierTo(commands[i].cp1,commands[i].cp2,commands[i].to, curveResolution);  
				break;  
			case ofSubPath::Command::arc:  
				polyline.arc(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd, arcResolution);  
				break;  
			case ofSubPath::Command::arcNegative:  
				polyline.arcNegative(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd, arcResolution);  
				break;  
		}  
		  
	}  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	ofPushMatrix();  
		ofSetColor(ofColor::yellow);  
		ofTranslate(20, 20, 0);  
			polyline.draw();  
	ofPopMatrix();  
  
}  
void testApp::drawCircle(float x, float y, float z,  float radius){  
	vector<ofPoint> & circleCache = circlePolyline.getVertices();  
	for(int i=0;i<(int)circleCache.size();i++){  
		circlePoints[i].set(radius*circleCache[i].x+x,radius*circleCache[i].y+y,z);  
		if(i==0)  
		{  
			path.moveTo(circlePoints[i]);  
		    
		}else  
		{  
		    path.lineTo(circlePoints[i]);  
		}  
	}  
}  
  

Hi @jvcleave I know this is sort of old topic now but when I copy and paste your last example I am getting errors each time ofSubPath is used. I cant seem to find much information about it online and am wondering if maybe it has been removed from the source? Do you know why this isn’t compiling anymore with ofSubPath or how to fix it? Thanks.

ofSubPath has been removed

try this (tested at oF9.8 Mac )
hope it helps.
notice commands[i].to store the ofPoint in ofPath;

testApp.cpp

//--------------------------------------------------------------
include "testApp.h"    
void testApp::setup(){
        
        int circleResolution = 6;
        circlePolyline.arc(0,0,0,1,1,0,360,circleResolution);
        circlePoints.resize(circlePolyline.size());
        path.setFilled(false);
    
    for (int i=0; i< 20; i++)
    {
        for (int j=0; j< 20; j++)
        {
            drawCircle(i*40, j*40, 0, 20);
        }
        
    }
    
//    vector<ofPath>& subPaths = path.getSubPaths();
    for (int i=0; i<path.getCommands().size(); i++)
    {
        vector<ofPath::Command>& pathCommands = path.getCommands();
        for (int j=0; j<pathCommands.size(); j++)
        {
            commands.push_back(pathCommands[j]);
        }
        
    }
    
    commandIndex =0;

}

//--------------------------------------------------------------
void testApp::update(){
        ofSetWindowTitle(ofToString(ofGetFrameRate()));
        if (ofGetFrameNum()%10 == 0)
        {
            if (commandIndex+1<commands.size())
            {
                commandIndex++;
            }else {
                commandIndex = 0;
            }
        }
    polyline.clear();
    int curveResolution = ofGetStyle().curveResolution;
    //maybe a better way?
    ofPath tempPath;
    int arcResolution = tempPath.getCircleResolution();
    
    
    for(int i=0; i<commandIndex; i++)
    {
        switch(commands[i].type)
        {
            case ofPath::Command::moveTo:
                polyline.moveTo(commands[i].to);
                break;
            case ofPath::Command::lineTo:
                polyline.lineTo(commands[i].to);
                break;
            case ofPath::Command::curveTo:
                polyline.curveTo(commands[i].to);
                //                polyline.curveTo(commands[i].to, curveResolution);
                break;
            case ofPath::Command::bezierTo:
                polyline.bezierTo(commands[i].cp1,commands[i].cp2,commands[i].to);
                //                polyline.bezierTo(commands[i].cp1,commands[i].cp2,commands[i].to, curveResolution);
                break;
            case ofPath::Command::quadBezierTo:
                //                polyline.quadBezierTo(commands[i].cp1,commands[i].cp2,commands[i].to, curveResolution);
                polyline.quadBezierTo(commands[i].cp1,commands[i].cp2,commands[i].to);
                break;
            case ofPath::Command::arc:
                //                polyline.arc(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd, arcResolution);
                polyline.arc(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd);
                
                break;
            case ofPath::Command::arcNegative:
                //                polyline.arcNegative(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd, arcResolution);
                polyline.arcNegative(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd);
                break;
            case ofPath::Command::close:
                polyline.close();
                //                polylines.push_back(polyline);
                //                polyline.clear();
                break;
        }
        
    }

}

//--------------------------------------------------------------
void testApp::draw(){
    ofPushMatrix();
    ofSetColor(ofColor::yellow);
    ofTranslate(20, 20, 0);
//    for(int i=0;i<polylines.size();i++){
//        polylines[i].draw();
//    }
    
            polyline.draw();
    ofPopMatrix();
    
}  
void testApp::drawCircle(float x, float y, float z,  float radius){  
    vector<ofPoint> & circleCache = circlePolyline.getVertices();  
    for(int i=0;i<(int)circleCache.size();i++){  
        circlePoints[i].set(radius*circleCache[i].x+x,radius*circleCache[i].y+y,z);  
        if(i==0)  
        {  
            path.moveTo(circlePoints[i]);  
            
        }else  
        {  
            path.lineTo(circlePoints[i]);  
        }  
    }  
}  

testApp.h

#pragma once

#include "ofMain.h"

class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();

//    void keyPressed(int key);
//    void keyReleased(int key);
//    void mouseMoved(int x, int y);
//    void mouseDragged(int x, int y, int button);
//    void mousePressed(int x, int y, int button);
//    void mouseReleased(int x, int y, int button);
//    void windowResized(int w, int h);
//    void dragEvent(ofDragInfo dragInfo);
//    void gotMessage(ofMessage msg);
ofPolyline circlePolyline;
void drawCircle(float x, float y, float z,  float radius);
vector<ofPoint> circlePoints;
ofPath path;
//vector<ofPolyline> paths;
vector<ofPath::Command> commands;
int commandIndex;
vector<ofPolyline> polylines;
//    ofPolyline polyline;
ofPath polyline;
int linecount=0;
};

Cheers @ryanatltair that works great!