-
Notifications
You must be signed in to change notification settings - Fork 40
/
Path3D.cpp
executable file
·277 lines (217 loc) · 7.02 KB
/
Path3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// Copyright (c) 2016, 2021 Daniel Moore, Madeline Gannon, and The Frank-Ratchye STUDIO for Creative Inquiry All rights reserved.
////
#include "Path3D.h"
using namespace ofxRobotArm;
void Path3D::setup(){
// set the Z axis as the forward axis by default
makeZForward = true;
ptIndex = 0;
centroid = ofPoint(.5,.25,.25); // all coordinates are in meters
reverse = false;
profile = buildProfile(0.025,4);
direction = 1;
feedRate = 0.01;
}
//void Path3D::setup(ofPolyline &polyline, vector<ofMatrix4x4> &m44){
//
//
//
// ptIndex = 0;
//
// // ignore the first and last points for the centroid
// for (int i=0; i<polyline.getVertices().size(); i++){
// centroid += polyline.getVertices()[i];
// }
// centroid /= polyline.getVertices().size();
//
// path = polyline;
// perpFrames.clear();
// perpFrames = m44;
//
//}
void Path3D::set(ofPolyline &polyline){
setup(); // incase path hasn't been set up yet
ptIndex = 0;
reverse = true;
direction = 1;
// ignore the first and last points for the centroid
for (int i=1; i<polyline.getVertices().size()-1; i++){
centroid += polyline.getVertices()[i];
}
centroid /= polyline.getVertices().size()-2;
path = polyline;
buildPerpFrames(path);
}
void Path3D::keyPressed(int key){
float step = .01; // 10 millimeters
if (key == OF_KEY_UP){
for (auto &p : path.getVertices())
p.y += step;
buildPerpFrames(path);
}else if(key == OF_KEY_DOWN){
for (auto &p : path.getVertices())
p.y -= step;
buildPerpFrames(path);
}else if(key == OF_KEY_RIGHT){
for (auto &p : path.getVertices())
p.x += step;
buildPerpFrames(path);
}else if(key == OF_KEY_LEFT){
for (auto &p : path.getVertices())
p.x -= step;
buildPerpFrames(path);
}else if (key == '!'){
makeZOut = false;
makeZForward = true;
}
else if (key == '@'){
makeZForward = false;
makeZOut = true;
}
else if (key == '#'){
makeZForward = false;
makeZOut = false;
}
}
ofMatrix4x4 Path3D::getNextPose(){
reverse = true;
if(ptf.framesSize()>0){
// go back-and-forth along a path
if (reverse && (ptIndex == 0 || ptIndex == ptf.framesSize()-2))
direction *= -1;
ptIndex = (ptIndex + direction) % ptf.framesSize();
orientation = ptf.frameAt(ptIndex);
}
return orientation;
}
ofMatrix4x4 Path3D::getPoseAt(int index){
return ptf.frameAt(index);
}
int Path3D::getPtIndex(){
return ptIndex;
};
void Path3D::setPtIndex(int index){
ptIndex = index;
};
void Path3D::draw(){
// show the current orientation plane
ofSetColor(ofColor::lightYellow);
ofSetLineWidth(3);
ofPushMatrix();
ofMultMatrix(orientation);
profile.draw();
ofDrawAxis(.010);
ofPopMatrix();
// show all the frames
ofSetColor(ofColor::aqua,80);
for (auto frame : ptf.getFrames()){
ofPushMatrix();
ofMultMatrix(frame);
profile.draw();
// ofDrawAxis(.010);
ofPopMatrix();
}
// show the target point
ofSetColor(ofColor::yellow);
if (path.size() > 0){
// ofDrawSphere(path.getVertices()[ptIndex], .003);
ofDrawSphere(getPoseAt(ptIndex).getTranslation(), .003);
}
// show the 3D path
ofSetLineWidth(3);
ofSetColor(ofColor::aqua);
//path.draw();
}
int Path3D::size(){
return ptf.framesSize();
}
//--------------------------------------------------------------
void Path3D::parsePts(string filename, ofPolyline &polyline){
ofFile file = ofFile(ofToDataPath(filename));
polyline.clear();
if(!file.exists()){
ofLogError("The file " + filename + " is missing");
}
ofBuffer buffer(file);
//Read file
for (ofBuffer::Line it = buffer.getLines().begin(); it != buffer.getLines().end(); it++) {
string line = *it;
float scalar = 10;
ofVec3f offset;
if (filename == "path_XZ.txt"){
offset = ofVec3f(0, 0, 0);
scalar = 3;
}
else if (filename == "path_YZ.txt"){
offset = ofVec3f(0, 0, 0);
scalar = 3;
}
else{
offset = ofVec3f(0, 0, .25);
scalar = 3;
}
ofStringReplace(line, "{", "");
ofStringReplace(line, "}", "");
vector<string> coords = ofSplitString(line, ", "); // get x y z coordinates
ofVec3f p = ofVec3f(ofToFloat(coords[0])*scalar,ofToFloat(coords[1])*scalar,ofToFloat(coords[2])*scalar);
p += offset;
polyline.addVertex(p);
}
}
//--------------------------------------------------------------
void Path3D::buildPerpFrames(ofPolyline polyline){
// reset the perp frames
ptf.clear();
for (auto &p : polyline){
ptf.addPoint(p);
}
}
//--------------------------------------------------------------
ofPolyline Path3D::buildProfile(float radius, int res){
ofPolyline temp;
// make a plane
if (res == 4){
temp.addVertex(ofVec3f(-radius/2, radius/2,0));
temp.addVertex(ofVec3f( radius/2, radius/2,0));
temp.addVertex(ofVec3f( radius/2,-radius/2,0));
temp.addVertex(ofVec3f(-radius/2,-radius/2,0));
}
// make a polygon
else{
float theta = 360/res;
for (int i=0; i<res; i++){
ofPoint p = ofPoint(0,0,radius);
temp.addVertex(p.rotate(theta*i, ofVec3f(1,0,0)));
}
}
temp.close();
return temp;
}
//--------------------------------------------------------------
ofMatrix4x4 Path3D::flip(ofMatrix4x4 originalMat){
ofVec3f pos = originalMat.getTranslation();
ofVec3f z = originalMat.getRowAsVec3f(2); // local y-axis
originalMat.setTranslation(0,0,0);
originalMat.rotate(180, z.x, z.y, z.z); // rotate about the y
originalMat.setTranslation(pos);
return originalMat;
}
//--------------------------------------------------------------
ofMatrix4x4 Path3D::zForward(ofMatrix4x4 originalMat){
ofVec3f pos = originalMat.getTranslation();
ofVec3f y = originalMat.getRowAsVec3f(1); // local y-axis
originalMat.setTranslation(0,0,0);
originalMat.rotate(90, y.x, y.y, y.z); // rotate about the y
originalMat.setTranslation(pos);
return originalMat;
}
//--------------------------------------------------------------
ofMatrix4x4 Path3D::zOut(ofMatrix4x4 originalMat){
ofVec3f pos = originalMat.getTranslation();
ofVec3f x = originalMat.getRowAsVec3f(0); // local x-axis
originalMat.setTranslation(0,0,0);
originalMat.rotate(-90, x.x, x.y, x.z); // rotate about the y
originalMat.setTranslation(pos);
return originalMat;
}