Skip to content

Instantly share code, notes, and snippets.

@golanlevin
Created February 13, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save golanlevin/4b3c624ba0047828e45c to your computer and use it in GitHub Desktop.
Save golanlevin/4b3c624ba0047828e45c to your computer and use it in GitHub Desktop.
Processing 3.0 utility to load a folder of data files, containing colored 3D points; outputs a folder of images of the colored points.
// Processing 3.0 program to load a folder of data files, containing colored 3D points.
// The program outputs a folder of images (in "output/" folder) of the colored points.
// Golan Levin (@golan), February 2016, written for Claire Hentschker
//
// The lines of the data files are in the format:
// X Y Z R G B A B C
// The data values are space-separated; see example file below.
/* Sample data file: data/arcade_003251.txt:
-33.92082214 -19.60678101 33.81335068 106 142 217 -0.517697 -0.204671 -0.830723
-32.12897873 -19.78975105 33.80073166 160 188 239 0.156332 -0.661404 -0.733557
-32.05957413 -19.79606056 33.81965637 140 176 238 -0.010007 -0.310202 -0.950618
-33.07537460 -18.56574440 33.80073166 28 69 151 0.514635 0.263594 0.815885
-31.93969917 -17.69506073 33.80073166 27 62 141 -0.316080 0.948239 0.030588
-31.96493530 -17.25971794 33.80704117 27 59 134 -0.480115 0.765588 -0.428210
-28.57683563 -19.92855644 33.80704117 29 50 133 -0.433498 -0.459771 -0.775042
-30.03428650 -18.54050827 33.80073166 38 95 178 -0.342376 -0.494543 -0.798878
-29.42228317 -17.50578117 33.81335068 27 65 145 -0.544637 0.730611 -0.411799
-30.12892532 -16.25022697 33.81335068 23 52 122 0.676998 0.464606 -0.570802
*/
//-----------------------------------------
// ******* USER-MODIFIABLE CONTROLS *******
//
color outputBackgroundColor = color(255,255,255);
float outputPointSize = 5; // pixel size of the points
float outputScaleX = -16.0;
float outputScaleY = -16.0;
float outputShiftX = 100;
float outputShiftY = 100;
//-----------------------------------------
boolean bProcessingFiles = false;
ArrayList<File> dataFilesList;
String currentFilename;
int currentFileId = 0;
// Data structures to store the XYZ and RGB data.
ArrayList<PVector> myPoints;
ArrayList<PVector> myColors;
//--------------------------------------------
void setup() {
size(1024, 512);
noSmooth();
myPoints = new ArrayList<PVector>();
myColors = new ArrayList<PVector>();
dataFilesList = new ArrayList<File>();
currentFileId = 0;
bProcessingFiles = false;
selectFolder("Select a folder to process:", "doThisWhenFolderSelected");
}
//--------------------------------------------
void draw() {
if (bProcessingFiles) {
processFileList(currentFileId);
renderCurrentFile();
// Advance to next file, or quit if we're done.
currentFileId = currentFileId+1;
if (currentFileId >= dataFilesList.size()) {
println ("Completed processing the files.");
exit();
}
}
}
//--------------------------------------------
void processFileList(int which) {
int nFiles = dataFilesList.size();
if ((which >= 0) && (which < nFiles)) {
myPoints.clear();
myColors.clear();
currentFilename = (dataFilesList.get(which)).toString();
// println("Loading file: " + currentFilename);
String myFileRows[] = loadStrings(currentFilename);
int nRows = myFileRows.length;
println("Number of rows in file " + currentFilename + " = " + nRows);
final int N_EXPECTED_PIECES = 9;
for (int i=0; i<nRows; i++) {
String aRowString = myFileRows[i];
String aRowPieces[] = split(aRowString, " ");
int nPiecesInaRow = aRowPieces.length;
if (nPiecesInaRow == N_EXPECTED_PIECES) {
float x = Float.parseFloat(aRowPieces[0]);
float y = Float.parseFloat(aRowPieces[1]);
// ignore z
float r = Float.parseFloat(aRowPieces[3]);
float g = Float.parseFloat(aRowPieces[4]);
float b = Float.parseFloat(aRowPieces[5]);
myPoints.add (new PVector(x, y));
myColors.add (new PVector(r, g, b));
}
}
}
}
//--------------------------------------------
void renderCurrentFile() {
background(outputBackgroundColor);
noStroke();
int nPoints = myPoints.size();
for (int i=0; i<nPoints; i++) {
PVector aPoint = myPoints.get(i);
float x = aPoint.x;
float y = aPoint.y;
x = outputShiftX + x*outputScaleX;
y = outputShiftY + y*outputScaleY;
PVector aColor = myColors.get(i);
fill(aColor.x, aColor.y, aColor.z);
ellipse(x, y, outputPointSize, outputPointSize);
}
int indexOfLastSlash = currentFilename.lastIndexOf("/");
int indexOfLastDot = currentFilename.lastIndexOf(".");
String namePart = currentFilename.substring(indexOfLastSlash+1, indexOfLastDot);
String outputFilename = "output/" + nf(currentFileId, 5) + "_" + namePart + ".png";
saveFrame(outputFilename);
}
//--------------------------------------------
void doThisWhenFolderSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
}
String folderPath = selection.getAbsolutePath();
if (folderPath != null) {
File file = new File(folderPath);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
dataFilesList.add(files[i]);
}
println("Number of files to process = " + dataFilesList.size());
}
bProcessingFiles = true;
}
//--------------------------------------------
// EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment