Skip to content

Commit a958236

Browse files
julapyLukasz Karluk
authored andcommitted
added ofxPolyGrowAlongNormals() + ofxPolyToMesh() methods.
1 parent dbfafd3 commit a958236

File tree

2 files changed

+104
-12
lines changed

2 files changed

+104
-12
lines changed

src/ofxPoly.cpp

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
#include "ofxPoly.h"
1010

1111
//--------------------------------------------------------------
12-
void ofxPolyGrow(const ofPolyline & polyIn, ofPolyline & polyOut, float amount) {
12+
void ofxPolyGrow(ofPolyline & poly, const ofPolyline & polySource, float amount) {
1313

14-
polyOut.clear();
15-
polyOut.setClosed(polyIn.isClosed());
14+
poly.clear();
15+
poly.setClosed(polySource.isClosed());
1616

17-
if(polyIn.size() < 2) {
18-
polyOut = polyIn;
17+
if(polySource.size() < 2) {
18+
poly = polySource;
1919
return;
2020
}
2121

22-
const vector<ofVec3f> & points = polyIn.getVertices();
22+
const vector<ofVec3f> & points = polySource.getVertices();
2323
int numOfPoints = points.size();
2424

2525
bool bClosed = true;
26-
bClosed = bClosed && (polyIn.isClosed() == true);
26+
bClosed = bClosed && (polySource.isClosed() == true);
2727
bClosed = bClosed && (numOfPoints >= 3);
2828

2929
for(int i=0; i<numOfPoints; i++) {
@@ -36,9 +36,9 @@ void ofxPolyGrow(const ofPolyline & polyIn, ofPolyline & polyOut, float amount)
3636
if(bEndings == true) {
3737

3838
const ofVec3f & p0 = points[i];
39-
ofVec3f n0 = polyIn.getNormalAtIndex(i);
39+
ofVec3f n0 = polySource.getNormalAtIndex(i);
4040
ofVec3f point = p0 + (n0 * amount);
41-
polyOut.addVertex(point);
41+
poly.addVertex(point);
4242

4343
continue;
4444
}
@@ -51,13 +51,99 @@ void ofxPolyGrow(const ofPolyline & polyIn, ofPolyline & polyOut, float amount)
5151
const ofVec3f & p0 = points[i0];
5252
const ofVec3f & p1 = points[i];
5353
ofVec3f n0 = ofVec2f(p0 - p1).getPerpendicular();
54-
ofVec3f n1 = polyIn.getNormalAtIndex(i);
54+
ofVec3f n1 = polySource.getNormalAtIndex(i);
5555

5656
float angle = ofVec2f(n0).angle(ofVec2f(n1));
5757
float length = amount / cos(angle * DEG_TO_RAD);
5858

5959
ofVec3f point = p1 + (n1 * length);
60-
polyOut.addVertex(point);
60+
poly.addVertex(point);
61+
}
62+
}
63+
64+
//--------------------------------------------------------------
65+
void ofxPolyGrowAlongNormals(ofPolyline & poly, const ofPolyline & polySource, float normalLength) {
66+
vector<float> thicknesses;
67+
thicknesses.insert(thicknesses.begin(), polySource.size(), normalLength);
68+
ofxPolyGrowAlongNormals(poly, polySource, thicknesses);
69+
}
70+
71+
void ofxPolyGrowAlongNormals(ofPolyline & poly, const ofPolyline & polySource, const vector<float> & normalLengths) {
72+
73+
poly = polySource;
74+
75+
if(poly.size() < 2) {
76+
return;
77+
}
78+
79+
vector<ofVec3f> & points = poly.getVertices();
80+
int numOfPoints = points.size();
81+
82+
for(int i=0; i<numOfPoints; i++) {
83+
84+
float normalLength = 0.0;
85+
if(i < normalLengths.size()) {
86+
normalLength = normalLengths[i];
87+
}
88+
89+
ofVec3f & point = points[i];
90+
ofVec3f normal = poly.getNormalAtIndex(i);
91+
point += (normal * normalLength);
92+
}
93+
}
94+
95+
//--------------------------------------------------------------
96+
void ofxPolyToMesh(ofMesh & mesh, const ofPolyline & polySource, float normalLength) {
97+
98+
float normalLength0 = -normalLength;
99+
float normalLength1 = normalLength;
100+
101+
ofPolyline poly0, poly1;
102+
ofxPolyGrowAlongNormals(poly0, polySource, normalLength0);
103+
ofxPolyGrowAlongNormals(poly1, polySource, normalLength1);
104+
105+
ofxPolyToMesh(mesh, poly0, poly1);
106+
}
107+
108+
void ofxPolyToMesh(ofMesh & mesh, const ofPolyline & polySource, const vector<float> & normalLengths) {
109+
110+
vector<float> normalLengths0;
111+
vector<float> normalLengths1;
112+
113+
for(int i=0; i<normalLengths.size(); i++) {
114+
float normalLength = normalLengths[i];
115+
float normalLength0 = -normalLength;
116+
float normalLength1 = normalLength;
117+
118+
normalLengths0.push_back(normalLength0);
119+
normalLengths1.push_back(normalLength1);
120+
}
121+
122+
ofPolyline poly0, poly1;
123+
ofxPolyGrowAlongNormals(poly0, polySource, normalLengths0);
124+
ofxPolyGrowAlongNormals(poly1, polySource, normalLengths1);
125+
126+
ofxPolyToMesh(mesh, poly0, poly1);
127+
}
128+
129+
void ofxPolyToMesh(ofMesh & mesh, const ofPolyline & poly0, const ofPolyline & poly1) {
130+
131+
mesh.clear();
132+
mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
133+
134+
int numOfPoints = MIN(poly0.size(), poly1.size());
135+
int numOfCycles = numOfPoints;
136+
if(poly0.isClosed() == true) {
137+
numOfCycles += 1;
138+
}
139+
140+
for(int i=0; i<numOfCycles; i++) {
141+
int j = i % numOfPoints;
142+
const ofVec3f & p0 = poly0.getVertices()[j];
143+
const ofVec3f & p1 = poly1.getVertices()[j];
144+
145+
mesh.addVertex(p0);
146+
mesh.addVertex(p1);
61147
}
62148
}
63149

src/ofxPoly.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88

99
#include "ofMain.h"
1010

11-
void ofxPolyGrow(const ofPolyline & polyIn, ofPolyline & polyOut, float amount);
11+
void ofxPolyGrow(ofPolyline & poly, const ofPolyline & polySource, float amount);
12+
void ofxPolyGrowAlongNormals(ofPolyline & poly, const ofPolyline & polySource, float normalLength);
13+
void ofxPolyGrowAlongNormals(ofPolyline & poly, const ofPolyline & polySource, const vector<float> & normalLengths);
14+
15+
void ofxPolyToMesh(ofMesh & mesh, const ofPolyline & polySource, float normalLength);
16+
void ofxPolyToMesh(ofMesh & mesh, const ofPolyline & polySource, const vector<float> & normalLengths);
17+
void ofxPolyToMesh(ofMesh & mesh, const ofPolyline & poly0, const ofPolyline & poly1);
1218

1319
void ofxPolyDrawNormals(const ofPolyline & poly, float normalLength);
1420

0 commit comments

Comments
 (0)