I have a string .txt file that has 6 columns of data, which corresponds to x,y,z,r,g,b
I want to load the string file and parse the data into an array.
I would like to have the data as an array [x][y][z].
and an array seperately for [r][g][b].
for example, one line of my .txt file looks like this:
12 18 -19 124 119 113
This is from a point cloud .txt file output.
float[][] allPoints;
float[] centroid = {0, 0, 0};
PShape pixelCloud;
void setup() {
String[] lines = loadStrings("grid.txt");
println("there are " + lines.length + " lines");
allPoints = new float[lines.length][3];
//Read through each line of the file
for (int i = 0; i < lines.length; i=i+1) {
This is what I have so far. I am a beginner, and I have searched through youtube and the reference libraries on the processing website, and I am failing to understand data types and how to accesses data within the array.
I have read your resources, and I was able to get some help, but I donât fully understand how everything is working.
Could someone explain how this method works for accessing each index in the array? Intuitively, I can understand that because the .txt file is being split by the number of spaces in between each column of the file String[] vals = lines[i].split(" "); that each column becomes the array index i.e, 6 lines of code = 0-5 array index.
So, all you need to do is declare the float and the array index and that will be successful in storing the value?
In addition, in the void setup() could someone explain how the for statement stores integer values?
int grid;
void importText(String fName) {
String lines = loadStrings(fName);
for (int i = 0; i < lines.length; i+=1) {//edit point density
String vals = lines[i].split(" ");
float x = float(vals[0]);
float y = float(vals[1]);
float z = float(vals[2]);
float r = float(vals[3]);
float g = float(vals[4]);
float b = float(vals[5]);
PVector temp = new PVector(x, y, z);
int col = color(r, g, b, 255);
if (x < dimx && x >= 0 && y < dimy && y >= 0 && z <dimz && z >=0) {
grid[int(x)][int(y)][int(z)] = col;
}
}
}
void setup() {
size(1920, 1080, P3D);
dataList = new ArrayList();
grid = new int[dimx][dimy][dimz];
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid.length; y++) {
for (int z = 0; z < grid[y].length; z++) {
grid[y][z] = 0;
}
}
}
importText(âpointCloud.txtâ);
}
Yeah, split() gives you an array per line (!) and the slots of the arrays are (depending on your csv) the xyz and rgb values.
In general youâre doing it right.
Instead of int for color use data type color. Same thing but more clear.
Here: int col = color(r, g, b, 255);
and here:
int[][][] grid;
The grid
You donât have to use the grid[][][] at all though.
Instead have to parallel arrays, one of type PVector, other of type color and store your points in there. Then for loop over those two and display the content.
No grid needed.
The grid is much too slow and too big since you would have to for loop over a lot of empty cells which is time consuming.
And since you use a float position as an index (int) you could also get rounding errors.
Remark
(when you are into OOP make a class point with color and use sphere to display it)
Remark
Itâs easier for us when you post the first 10 lines of your data file here formatted as code.
In this line first note the i: the line number of your text file. We loop over the text file with i and look at each line separately (the ith line in the file).
split() breaks up the line. So vals hold the data / values of this line. So one point in the cloud.