Parsing X,Y,Z,R,G,B from Text file

Hello,

I am working on using the particle system to visualize my Point Cloud. I have CloudCompare and am using a PC visualizer that consumes .ply files. It works great but the performance is an issue. So, partially to compare performance and partially to learn something new, I am trying this out.

I have the code to visualize the PC using Particles. I have saved the PC file to a txt file that contains xyz and rgb values for each point. The issue is getting the xyz and rgb from the .txt file and into my List. I think my code is close but not quite there. I am getting this error: “Input string was not in correct format. \r\n.…” It mentions an issue at line 72 which is where the the XYZ values should be input into the List. The txt file is formatted like so:

-1.27959657 -0.13457896 -4.51264572 224 208 193
-1.27935028 -0.13580444 -4.51264286 225 209 194
-1.27885675 -0.13697970 -4.51239252 226 210 195

Each line is a point and its xyz rgb values. I am trying to go line by line and separate by spaces.

Here is an image of the Debug console:

Here is my code:

public void NewParse()
    {
        string[] temp;
        float x, y, z, r, g, b;
        try
        {
            using(StreamReader reader = new StreamReader(filePath))
            {
                while(reader.Peek() >= 0)
                {
                    temp = extractedString.Split(' ');
                    x = float.Parse(temp[0]); y = float.Parse(temp[1]); z = float.Parse(temp[2]);
                    r = float.Parse(temp[3]); g = float.Parse(temp[4]); b = float.Parse(temp[5]);
                    Vector3 finalValuePos = new Vector3(x, y, z);
                    Vector3 finalValueCol = new Vector3(r, g, b);
                    pointPosValues.Add(finalValuePos);
                    pointColValues.Add(finalValueCol);
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.Log("Something was invalid");
        }
    }

What i can tell from testing is that the first 5 values are being read properly, but the “b” variable shows up as 0 instead of the actual first value (should be 193). I am obviously not great at Parsing and reading text files so any help is appreciated.

Where did extractedString come from?
What happened to all this stuff?

            using(StreamReader reader = new StreamReader(filePath))
            {
                while(reader.Peek() >= 0)

It seems ignored?

It seems like you probably just forgot to split the file by newlines first. So you’ve got a bunch of valid numbers, then a number followed by a newline after splitting on spaces, and it can’t parse the newline. It’d help of you just looked at temp in the debugger.

Or you’re on Windows and your newlines are \r\n and you’re only splitting on \n which leaves the \r in your last number string.

So i have another function that grabs the file for the StreamReader and then another that just converts it to a string (probably unnecessary).

public void ExtractStringFromTxt()
    {
        extractedString = File.ReadAllText(Application.dataPath + "\\" + file);
        filePath = Application.dataPath + "\\" + file;
        Debug.Log(filePath);
    }

Can you explain the \r\n part? I was led to believe that the “while(reader.Peek() >= 0)” portion would detect the next line. Obviously, im not understanding how to get to the next line.

None of that reader stuff matters whatsoever because you’re not using it.

You’re getting all the string data from here: extractedString = File.ReadAllText(Application.dataPath + "\\" + file);
So you have one big string there with the contents of the entire file in it. Then you’re doing this:
temp = extractedString.Split(' '); So you’re splitting the contents of the entire file by spaces. Well your string looks something like this:
1 2 3 4 5 6\n7 8 9 10 11 12\n
new lines in the file are represented by \n
So when you split on spaces you end up with:
1
2
3
4
5
6\n << and this is the one that fails to parse.
7

Different operating systems represent new lines with different characters (this is what I mean by the \r\n thing):
https://en.wikipedia.org/wiki/Newline#Representation

What you really want to do is first split the file up by lines so you get:
1 2 3 4 5 6
7 8 9 10 11 12
Then you split each line by spaces so for a single line you get:
1
2
3
4
5
6