Help in reading a .PLY file via C++

S

Sweety Pie

Hi,
I need to read the values from this file and save only the numerical
ones in a new file. How can I skip the header which is up to
end_header word? how can read each value and save it
Please help me
This is the format of the file:
ply
format ascii 1.0
obj_info is_cyberware_data 1
obj_info is_mesh 0
obj_info is_warped 0
obj_info is_interlaced 1
obj_info num_cols 512
obj_info num_rows 400
obj_info echo_rgb_offset_x 0.013000
obj_info echo_rgb_offset_y 0.153600
obj_info echo_rgb_offset_z 0.172000
obj_info echo_rgb_frontfocus 0.930000
obj_info echo_rgb_backfocus 0.012660
obj_info echo_rgb_pixelsize 0.000010
obj_info echo_rgb_centerpixel 232
obj_info echo_frames 512
obj_info echo_lgincr 0.000500
element vertex 40256
property float x
property float y
property float z
element range_grid 204800
property list uchar int vertex_indices
end_header
-0.06325 0.0359793 0.0420873
-0.06275 0.0360343 0.0425949
-0.0645 0.0365101 0.0404362
-0.064 0.0366195 0.0414512
-0.0635 0.0367289 0.0424662
-0.063 0.0367836 0.0429737
-0.0625 0.0368247 0.0433543
-0.062 0.0368657 0.0437349
-0.0615 0.0369067 0.0441155
-0.061 0.0369614 0.044623
-0.0605 0.0370162 0.0451305
-0.06 0.0370572 0.0455111
-0.0595 0.0370845 0.0457648
-0.059 0.0371256 0.0461455
-0.0585 0.0371529 0.0463992
-0.058 0.0371666 0.0465261
-0.0575 0.0371666 0.0465261
-0.057 0.0371666 0.0465261
-0.0565 0.0371803 0.046653
-0.056 0.0371803 0.046653
-0.0555 0.0371803 0.046653
-0.055 0.037194 0.0467798
-0.0545 0.0371666 0.0465261
-0.054 0.037194 0.0467798
-0.0535 0.0371803 0.046653
-0.053 0.037194 0.0467798
-0.0525 0.0371803 0.046653 .......................




I have written this code:
#include <iostream>
#include <fstream>

using namespace std;

int main () {
char myString;
float aNumber;

ifstream myfile ("test.txt");

if (! myfile.is_open())
{
cout << "Error opening file";
exit (1);
}

while (! myfile.eof() )
{
getline(myfile, myString);
if(myString !="end_header");
myFile >> aNumber;
}
return 0;
}
 
F

Fred Zwarts \(KVI\)

"Sweety Pie" wrote in message
Hi,
I need to read the values from this file and save only the numerical
ones in a new file. How can I skip the header which is up to
end_header word? how can read each value and save it
Please help me
This is the format of the file:
ply
format ascii 1.0
obj_info is_cyberware_data 1
obj_info is_mesh 0
obj_info is_warped 0
obj_info is_interlaced 1
obj_info num_cols 512
obj_info num_rows 400
obj_info echo_rgb_offset_x 0.013000
obj_info echo_rgb_offset_y 0.153600
obj_info echo_rgb_offset_z 0.172000
obj_info echo_rgb_frontfocus 0.930000
obj_info echo_rgb_backfocus 0.012660
obj_info echo_rgb_pixelsize 0.000010
obj_info echo_rgb_centerpixel 232
obj_info echo_frames 512
obj_info echo_lgincr 0.000500
element vertex 40256
property float x
property float y
property float z
element range_grid 204800
property list uchar int vertex_indices
end_header
-0.06325 0.0359793 0.0420873
-0.06275 0.0360343 0.0425949
-0.0645 0.0365101 0.0404362
-0.064 0.0366195 0.0414512
-0.0635 0.0367289 0.0424662
-0.063 0.0367836 0.0429737
-0.0625 0.0368247 0.0433543
-0.062 0.0368657 0.0437349
-0.0615 0.0369067 0.0441155
-0.061 0.0369614 0.044623
-0.0605 0.0370162 0.0451305
-0.06 0.0370572 0.0455111
-0.0595 0.0370845 0.0457648
-0.059 0.0371256 0.0461455
-0.0585 0.0371529 0.0463992
-0.058 0.0371666 0.0465261
-0.0575 0.0371666 0.0465261
-0.057 0.0371666 0.0465261
-0.0565 0.0371803 0.046653
-0.056 0.0371803 0.046653
-0.0555 0.0371803 0.046653
-0.055 0.037194 0.0467798
-0.0545 0.0371666 0.0465261
-0.054 0.037194 0.0467798
-0.0535 0.0371803 0.046653
-0.053 0.037194 0.0467798
-0.0525 0.0371803 0.046653 .......................




I have written this code:
#include <iostream>
#include <fstream>

using namespace std;

namespaces are invented to prevent name collisions. Try to understand why
using a namespace is against this idea.
int main () {
char myString;
float aNumber;

ifstream myfile ("test.txt");

if (! myfile.is_open())
{
cout << "Error opening file";
exit (1);
}

while (! myfile.eof() )
{
getline(myfile, myString);
if(myString !="end_header");
myFile >> aNumber;
}
return 0;
}

Not clear to me what you mean here. Why don't you check the return value of
getline?
Sooner or later getline will run against an EOF, but you ignore it and
continue to use myString.
and under the != operator

There is an if, but nothing is executed if the condition matches. Usually,
an if is followed with something to be executed if the condition is
fulfilled. Now the result is the same as when there was no if at all. Why
did you place the if here?

For debugging purposes, you can temporarily insert a few lines with "cout <<
__LINE__<< " " << myString << endl;" at some places in your code, so that
you can follow the flow of your program, or use a debugger.
 
Q

Quint Rankid

Hi,
I need to read the values from this file and save only the numerical
ones in a new file.
How can I skip the header which is up to
end_header word?

But evidently you don't want to save all of the numerical values.
What do you mean by "skip" the header?


how can read each value and save it
Please help me
This is the format of the file:
ply
format ascii 1.0
obj_info is_cyberware_data 1

Do some lines have integral values and some real values?
obj_info is_mesh 0

element vertex 40256
property float x
property float y
property float z

Some lines have no numeric values?
element range_grid 204800
property list uchar int vertex_indices
end_header
-0.06325 0.0359793 0.0420873
-0.06275 0.0360343 0.0425949

Do you want to read the lines that have three values each?
I have written this code:
#include <iostream>
#include <fstream>
and
#include <sstream>
#include <string>
and maybe you'll use this too at some point...
#include said:
using namespace std;

Please don't use namespaces. I know that there is some controversy
about when it is proper or reasonable to do this. I have a simpler
rule. Don't do it. It'll save you from a debugging session that lasts
into the wee hours. I really hate those. So I don't do it. Of course
YMMV and you may not mind. I prefer the easy way out.

int main () {
  ..char myString;
char?

char stores one single character. Did you want to do that in a
variable called myString? I'm not clear on your intent here.
  //float aNumber;



std::ifstream myfile("test.txt");
  if (! myfile.is_open())
  {
     cout << "Error opening file";
     exit (1);
  }





double aNumber;
std::string s1,s2; // just temps

std::string line;

// we have a variable number of "symbols" per line,
// so read a line at a time.
while(std::getline(myfile,line)) {
std::istringstream ix(line);
if(ix >> s1 >> s2 >> aNumber) {
// write aNumber
}
}

}

If you want the three values per line that follow end_header, or you
want to discriminate between real and integral values, you have other
problems. This will just get you the lines that have the form:
string string <number>

If you want to check for "end_header" I suggest reading each of s1, s2
and aNumber from ix individually and checking s1 after you read it.
if(ix >> s1) {
// check here for "end_header"
}
if(ix >> s2) {
// whatever you want to do
}
if(ix >> aNumber) {
// write aNumber
}

I haven't tested any of the above or tried to compile it, so there are
likely to be unanticipated problems, syntax errors or logic errors.


I have a feeling this may be no good to you unless you have a
reasonable c++ text to read through and learn about this.


Quint
 
G

Guest

I need to read the values from this file and save only the numerical
ones in a new file. How can I skip the header which is up to
end_header word? how can read each value and save it
Please help me
This is the format of the file:
ply
format ascii 1.0
obj_info is_cyberware_data 1
obj_info is_mesh 0
obj_info is_warped 0

end_header
-0.06325 0.0359793 0.0420873
-0.06275 0.0360343 0.0425949
-0.0645 0.0365101 0.0404362

I have written this code:
#include <iostream>
#include <fstream>

using namespace std;

int main () {
char myString;
float aNumber;

ifstream myfile ("test.txt");

if (! myfile.is_open())
{
cout << "Error opening file";
exit (1);
}

while (! myfile.eof() )
{
getline(myfile, myString);
if(myString !="end_header");
myFile >> aNumber;
}

reread your spec. You want to
- skip lines until end_header
- read three numbers per line

that requires two loops, one for the skipping and one for the line reading.

what is the type of myString? What is the type of "end header"? are they the same? What should a C++ programmer normally use to store a string?

Where are you going to store the data?
return 0;
}

meet us half way. Tell us what the errors say.
 
S

Sweety

reread your spec. You want to
   - skip lines until end_header
   - read three numbers per line

that requires two loops, one for the skipping and one for the line reading.

what is the type of myString? What is the type of "end header"? are they the same? What should a C++ programmer normally use to store a string?

Where are you going to store the data?



meet us half way. Tell us what the errors say.

Thank you very much for all the comments and help. I fixed the
errors .
Here is my code below and I want to read the file until it reaches the
word end header and start saving the floats after it in a new file.
Can some one plz check the code and help me . I didn't manage to write
the loops properly.
Thanks in advance.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string myString;
float aNumber;

ifstream myfile ("test.txt");
ofstream output;
if (! myfile.is_open())
{
cout << "Error opening file";
exit (1);
}
while (! myfile.eof() )
{
do{
getline(myfile, myString);
} while(myString !="end_header")

getline(myfile, myString);
myfile >> aNumber;
output << aNumber;
}
return 0;
}
 
A

Asger Joergensen

Hi Sweety
Here is my code below and I want to read the file until it reaches the
word end header and start saving the floats after it in a new file.
Can some one plz check the code and help me . I didn't manage to write
the loops properly.
Thanks in advance.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string myString;
float aNumber;

ifstream myfile ("test.txt");
if (! myfile.is_open())
{
cout << "Error opening file";
exit (1);
}
do{
getline(myfile, myString);
} while( myString !="end_header" && ! myfile.eof() )
ofstream output; // You need a filename here
check if open
while (! myfile.eof() )
{

getline(myfile, myString);
myfile >> aNumber;
output << aNumber;
}
return 0;
}

You need to tell how You want the output to look
one on each line all in one line space or tab between etc.?
Can there be anything but numbers ? Do You need to convert to
float before You write or can You just write the string You read ?

p.s. please delete unneeded quotes.

Best regards
Asger-P
 
S

Sweety

Hi Sweety













check if open



You need to tell how You want the output to look
one on each line all in one line space or tab between etc.?
Can there be anything but numbers ? Do You need to convert to
float before You write or can You just write the string You read ?

p.s. please delete unneeded quotes.

Best regards
Asger-P

Well the output file should have only the numbers and it doesn't
matter if they are all on one line. It should real the whole stream of
numbers and save them with spaces between each number and its
neighbour. There is no need for any conversion.
 
A

Asger Joergensen

Hi Sweety
Well the output file should have only the numbers and it doesn't
matter if they are all on one line. It should real the whole stream of
numbers and save them with spaces between each number and its
neighbour. There is no need for any conversion.

then try something like this:

while(! myfile.eof() )
{
getline(myfile, myString);
  output << myString << " ";
}

And please do delete unneeded quotes, it is annoying to have to scroll
through so much meaningless quote.

Best regards
Asger-P
 
F

Fred Zwarts \(KVI\)

"Asger Joergensen" wrote in message
then try something like this:

while(! myfile.eof() )
{
getline(myfile, myString);
output << myString << " ";
}

Why not:

while (!getline(myfile, myString).eof()) output << myString << ' ';

Otherwise, output is still used when getline hits EOF.
 
A

Alf P. Steinbach

in message



Why not:

while (!getline(myfile, myString).eof()) output << myString << ' ';

Otherwise, output is still used when getline hits EOF.

Asger's loop can incorrectly try to output `myString` when EOF is
encountered.

Fred's loop fixes that, but can incorrectly try to output `myString`
when some failure condition other than EOF, is encountered.

The common idiom is therefore to test the `fail()` member instead of
`eof()`, i.e.

while( !getline( f, s ).fail() ) { output << s << ' '; }

and usually that's expressed via the implicit conversion to `bool` in a
control statement condition, which invokes the `fail()` function,

while( getline( f, s ) ) { output << s << ' '; }

Note the lack of negation; even thought the implicit conversion invokes
`fail()`, it negates the result.

Also note the braces. ;-)


Cheers & hth.,

- Alf
 
J

Jorgen Grahn

Am 06.03.12 10:05, schrieb Sweety Pie:

There are better tools than C++ to perform such simple text processing
tasks.

There may be reason he's doing it in C++, but yes, it's worth pointing
out.
If you have access to a unix shell, try the command

awk '/end_header/ { skip = 1; next } skip' <input.ply >output

It should strip anything up to and including the first line which
contains end_header.

Or with Perl:

% perl -ne 'print unless 1 .. /end_header/' input.ply >output

/Jorgen
 
S

Sweety Pie

Well I have to use c++ cauz I will have Hardware parts later which
cannot get rather than c++
Thanks for the help
 
S

Sweety Pie

Thanks all for the replies. They were so useful to me. but I got
confused a bit about the loops.
I need a loop that will read the input from the file until the word
"end_header" and ignore all of that input. Then it should start
reading after that word and save the values which are numbers in a new
file separated via space.
can someone just guide me through this plz .Thanks
I have this code and I know it is wrong so please help me.
Thanks again

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string myString;
float aNumber;

ifstream myfile ("test.txt");
ofstream output;
if (! myfile.is_open())
{
cout << "Error opening file";
exit (1);
}

{
do{
getline(myfile, myString);
} while(myString !="end_header")

while (!getline(myfile, myString).eof()) output << myString << ' ';


return 0;
}
 
Q

Quint Rankid

Thanks all for the replies. They were so useful to me. but I got
confused a bit about the loops.
I need a loop that will read the input from the file  until the word
"end_header" and ignore all of that input. Then it should start
reading after that word and save the values which are numbers in a new
file separated via space.
can someone just guide me through this plz .Thanks
I have this code and I know it is wrong

What was wrong with it exactly?

so please help me.

Please tell us what problems you encountered when you say the code was
wrong.

Have you tried putting some trace in your code?

Did you edit your test input file to make it a little shorter to see
what happened? What did happen?
 
M

Miles Bader

Incidentally, if you want to be able to read somewhat arbitrary PLY
files (there's also a binary PLY form, for instance), I recommend
Diego Nehab's "RPLY", which is fairly simple but flexible PLY library.

[It's FOSS, using the MIT license (which, for instance allows it to be
used in propietary software without problems), and easy to embed in
your app just by copying the source files (just two files, rply.c and
rply.h).]

http://w3.impa.br/~diego/software/rply/

-miles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top