9
9
#include " ofxPoly.h"
10
10
11
11
// --------------------------------------------------------------
12
- void ofxPolyGrow (const ofPolyline & polyIn, ofPolyline & polyOut , float amount) {
12
+ void ofxPolyGrow (ofPolyline & poly, const ofPolyline & polySource , float amount) {
13
13
14
- polyOut .clear ();
15
- polyOut .setClosed (polyIn .isClosed ());
14
+ poly .clear ();
15
+ poly .setClosed (polySource .isClosed ());
16
16
17
- if (polyIn .size () < 2 ) {
18
- polyOut = polyIn ;
17
+ if (polySource .size () < 2 ) {
18
+ poly = polySource ;
19
19
return ;
20
20
}
21
21
22
- const vector<ofVec3f> & points = polyIn .getVertices ();
22
+ const vector<ofVec3f> & points = polySource .getVertices ();
23
23
int numOfPoints = points.size ();
24
24
25
25
bool bClosed = true ;
26
- bClosed = bClosed && (polyIn .isClosed () == true );
26
+ bClosed = bClosed && (polySource .isClosed () == true );
27
27
bClosed = bClosed && (numOfPoints >= 3 );
28
28
29
29
for (int i=0 ; i<numOfPoints; i++) {
@@ -36,9 +36,9 @@ void ofxPolyGrow(const ofPolyline & polyIn, ofPolyline & polyOut, float amount)
36
36
if (bEndings == true ) {
37
37
38
38
const ofVec3f & p0 = points[i];
39
- ofVec3f n0 = polyIn .getNormalAtIndex (i);
39
+ ofVec3f n0 = polySource .getNormalAtIndex (i);
40
40
ofVec3f point = p0 + (n0 * amount);
41
- polyOut .addVertex (point);
41
+ poly .addVertex (point);
42
42
43
43
continue ;
44
44
}
@@ -51,13 +51,99 @@ void ofxPolyGrow(const ofPolyline & polyIn, ofPolyline & polyOut, float amount)
51
51
const ofVec3f & p0 = points[i0];
52
52
const ofVec3f & p1 = points[i];
53
53
ofVec3f n0 = ofVec2f (p0 - p1).getPerpendicular ();
54
- ofVec3f n1 = polyIn .getNormalAtIndex (i);
54
+ ofVec3f n1 = polySource .getNormalAtIndex (i);
55
55
56
56
float angle = ofVec2f (n0).angle (ofVec2f (n1));
57
57
float length = amount / cos (angle * DEG_TO_RAD);
58
58
59
59
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);
61
147
}
62
148
}
63
149
0 commit comments