Splitting a String into an Array

Hi Processing friends,

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.

Thank you for your time!

Best regards,

Processing.org/reference/loadTable_.html

Or use command split to get an array

Then you don’t have to use arrays

Use PVector and color data type instead

See reference for all 3 commands

1 Like

This is not complete.

We all have to start somewhere.
Keep working at it!
There are tutorials as well.

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.

This is a very short list:

Resources < Click here to expand !

I encourage you to review the resources available here:

You will have to work through arrays first:

This was an excellent resource:
https://processing.org/tutorials/data/

:)

I pieced together 5 lines from the data tutorial, created a simple file.txt and was able to achieve this:

I am not sharing code.

:)

1 Like

Hello Processing Friends!

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”);
}

Thank you so much for your time and help. :slight_smile:

Explanation

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.

Chrisir

1 Like

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.

1 Like

it’s a 3fold nested for loop which means the outer one calls the inner one and the inner one the most inner one grid.length times.

So the most inner line grid[y][z] = 0; is executed grid.length times * grid.length times * grid.length times.

For the data structure int grid, read the tutorials on arrays (a list) and on 2D array (a grid like a chessboard). Tutorials / Processing.org

This is a 3D array (like a pile of chessboards).

So the indexes say: take the xth row, yth column, zth level / etage in the pile of chess boards.

The order of the x,y,z can be discussed; for example, in 3D Sketches I see

  • x as left / right
  • y as the height above ground
  • and z as back / forth

see https://www.youtube.com/watch?v=37W7tJ6x2-w

Chrisir

1 Like

image

:)

1 Like