ofPolyline getSmoothed

I think I’m stumped as to what getSmoothed is supposed to do. I figured the following code would show how it work, but doesn’t make sense to me. Can anyone shed some light on what int smoothingSize and float smoothingShape are supposed to do?

  
  
  
void app::setup() {  
	for (float i = 0.0; i < TWO_PI; i+=0.5) {  
		line.lineTo(ofPoint(cos(i) * 100, sin(i) * 100));  
	}  
	line.close();  
}  
  
void app::draw() {  
	line.draw();  
	  
	ofTranslate(400, 0);  
	float smoothShape = (ofGetHeight()/2) - ((mouseY < 1.0) ? 1.0 : mouseY);  
	int smoothSize = (mouseX < 1.0) ? 0.0 : mouseX/5;  
	ofPolyline p = line.getSmoothed(smoothSize, smoothShape);  
	p.draw();  
}  
  

Thanks!

here’s how i would demonstrate it:

  
  
ofPolyline line;  
  
void ofApp::setup() {    
	int n = 64;  
	float radius = 64;  
	float offset = 16;  
	for (float i = 0; i < n; i++) {    
		float theta = TWO_PI * i / n;  
		ofVec2f cur = ofVec2f(cos(theta), sin(theta)) * radius;  
		cur += ofVec2f(ofRandomf(), ofRandomf()) * offset;  
		line.lineTo(cur);    
	}    
	line.close();    
}    
  
void ofApp::update() {  
}  
  
void ofApp::draw() {    
	ofTranslate(100, 400);  
	  
	line.draw();    
	  
	ofTranslate(400, 0);    
	float smoothShape = ofMap(mouseY, 0, ofGetHeight(), 0, 1, true);  
	int smoothSize = ofMap(mouseX, 0, ofGetWidth(), 0, 32, true);  
	ofPolyline p = line.getSmoothed(smoothSize, smoothShape);    
	p.draw();    
}    
  

i think the thing you’re missing is just the argument ranges, and also: smoothing a perfect circle doesn’t really do anything except give you a new circle.

when smoothShape is 0, the shape of the smoothing function uses a weighted average where the current value has a weight of 1, and it drops off linearly to almost 0 at smoothSize distance.

when smoothShape is 1, there is no dropoff in the weighted average function, so it acts like a moving average/rectangular window.

you’ll get weird looking results if you use a shape > 1.

1 Like

That’s great, I couldn’t figure out the argument ranges. That makes more sense, I was assuming given a very rough circle it would smooth it into a more circular circle, but I’ll put together something a little more “smooth-able” for the docs. Thanks!

do you guys think there is a way to use getSmoothed without having to close the line?
i guess similar to what ofxMSASpline did or still does do.

oh i get it, i have to use myPolyline.curveTo(newPoint, stepsBetweenOldAndNewPoint)