Archived

This topic is now archived and is closed to further replies.

Grain

Reading vertices and triangles from a file.

Recommended Posts

I am really inexperienced when it comes to file IO How do I get a file like this in to my vertex and triangle structures? .
Named object: "Box01"
Tri-mesh, Vertices: 8 Faces: 12
Vertex List:
Vertex 0:  X:-20.500000    Y:16.500000    Z:16.250000
Vertex 1:  X:-20.500000    Y:-12.750000    Z:16.250000
Vertex 2:  X:12.000000    Y:16.500000    Z:16.250000
Vertex 3:  X:12.000000    Y:-12.750000    Z:16.250000
Vertex 4:  X:12.000000    Y:16.500000    Z:-16.250000
Vertex 5:  X:12.000000    Y:-12.750000    Z:-16.250000
Vertex 6:  X:-20.500000    Y:16.500000    Z:-16.250000
Vertex 7:  X:-20.500000    Y:-12.750000    Z:-16.250000
Face list:
Face 0: A:0 B:1 C:2 AB:1 BC:1 CA:1
Smoothing: 1
Face 1: A:1 B:3 C:2 AB:1 BC:1 CA:1
Smoothing: 1
Face 2: A:2 B:3 C:4 AB:1 BC:1 CA:1
Smoothing: 2
Face 3: A:3 B:5 C:4 AB:1 BC:1 CA:1
Smoothing: 2
Face 4: A:4 B:5 C:6 AB:1 BC:1 CA:1
Smoothing: 1
Face 5: A:5 B:7 C:6 AB:1 BC:1 CA:1
Smoothing: 1
Face 6: A:6 B:7 C:0 AB:1 BC:1 CA:1
Smoothing: 2
Face 7: A:7 B:1 C:0 AB:1 BC:1 CA:1
Smoothing: 2
Face 8: A:6 B:0 C:4 AB:1 BC:1 CA:1
Smoothing: 4
Face 9: A:0 B:2 C:4 AB:1 BC:1 CA:1
Smoothing: 4
Face 10: A:1 B:7 C:3 AB:1 BC:1 CA:1
Smoothing: 4
Face 11: A:7 B:5 C:3 AB:1 BC:1 CA:1
Smoothing: 4
Here are my vertex, triangle and mesh structures.
struct triangle
{
	int vert[3];
	int side[3];
	vector3d norm;
};
class Object3d
{

	vector3d *Verts;
        int num_verts;
	triangle *Tris;
        int num_tris;
	edge	 *Edges;
        int num_edegs;
        int color, backcolor;
public:
	vector3d pos, up, right, foward;	
	Object3d(){};
	Object3d(vector3d*,int, triangle*,int,int,int);
};

Share this post


Link to post
Share on other sites
Files containing data like this should be stored as binary.

Storing them as text is going to require doing a lot of extra parsing that''s ultimately just going to be a big pain in the ass.

Share this post


Link to post
Share on other sites
But files like that look all garbled, how are you supposed to know how it is structured so you can write a program to read it.

Share this post


Link to post
Share on other sites
Text files do take more time to parse and set up but IMHO the readability you gain (for debugging and quick changes) makes up for it especially if you are not too familiar with file I/O. It is easier to write/read a structure in binary ala fread and fwrite and eventually you may want to do this but as an exercise in file I/O this sort of thing is perfect to wet your feet with standard C file I/O. Try making a wrapper for text file I/O that incldues methods for reading in integers, floats, single words (i.e. "Vertex") and possibly strings. Once you do that you never have to write it again and you also know how things work. You can also add to it to make vertex reading functions etc that you plan on using a lot.

Share this post


Link to post
Share on other sites
quote:
Original post by evillive2
Text files do take more time to parse and set up but IMHO the readability you gain (for debugging and quick changes) makes up for it especially if you are not too familiar with file I/O. It is easier to write/read a structure in binary ala fread and fwrite and eventually you may want to do this but as an exercise in file I/O this sort of thing is perfect to wet your feet with standard C file I/O. Try making a wrapper for text file I/O that incldues methods for reading in integers, floats, single words (i.e. "Vertex") and possibly strings. Once you do that you never have to write it again and you also know how things work. You can also add to it to make vertex reading functions etc that you plan on using a lot.


Don''t mean to be rude but it looks like he''s using C++ if thats the case then he should be using iostream libaries

Share this post


Link to post
Share on other sites
looks like an .obj file? well either way... If you're not aiming for speed this should be kinda clare. I use a dummy string to extract dummy text. =)


std::ifstream file("filenamehere");
if( !file.is_open() )
;// Scream for mercy, and then change "filenamehere"


std::string dummy, objName;
file >> dummy; // "Named"

file >> dummy; // "object:"

file >> objName; // "Box01"


int vertices=0, faces=0;
file >> dummy >> dummy >> vertices >> dummy >> faces; // "Tri-mesh,", "Vertices:", vertices=8, "Faces:", faces=12


file >> dummy >> dummy; // "Vertex", "List:"

for( int vertex=0; vertex < vertices; vertex++ )
{
float x=0, y=0, z=0;
file >> dummy >> dummy; // "Vertex", index

file >> dummy >> x; // "X:"

file >> dummy >> y; // "Y:"

file >> dummy >> z; // "Z:"


// do something with your x,y,z =)

}

file >> dummy >> dummy; // "Face", "list:"

for( int face=0; face < faces; face++ )
{
int a=0, b=0, c=0, ab=0, bc=0, ca=0, smoothing;
file >> dummy >> dummy: // "Face", index

file >> dummy >> a; // "A:"

file >> dummy >> b; // "B:"

file >> dummy >> c; // "C:"

file >> dummy >> ab; // "AB:"

file >> dummy >> bc; // "BC:"

file >> dummy >> ca; // "CA:"


file >> dummy >> smoothing; // "Smoothing:"


// do something with your face info =)

}


That's a C++ way to do it, not saying it's THE way to do it... If you use C you'll get some better performance, and maybe more readable code? =P But I'm to tired to write an example, somebody else might want to give it a try? =)

[edited by - seriema on May 17, 2004 8:36:29 PM]

Share this post


Link to post
Share on other sites
Seriema Thanks allot. It looks like when you use the >> operator it grabs a string of characters up a space, correct? And it jumps over multiple spaces?

As far as speed is concerned its not really in issue with this because it only needs to read the file one time and then it has it in memory.


[edited by - Grain on May 17, 2004 9:04:12 PM]

Share this post


Link to post
Share on other sites
No problem =)

The >> extracts data out of a stream and skips whitespaces (tabs, newlines, spaces). if you put a std::string after it ( >> string) it extracts a string, if you put a int after it ( >> int) it extracts an int =)

Read more about the standard iostream library here.

And about speed, fast loading times are always nicer than slow But this should work until you get to big loading times, then you might want to switch to reading binary and/or using fscanf()

Oh yeah, I forgot in my last post. You can set a flag in the constructor for a ifstream, if it should be binary or not.
ifstream file("filename", ios_base::binary);
More info can be found here.
The same goes for writing to a file, using ofstream that is =)

Share this post


Link to post
Share on other sites
I seem to be having some trouble getting it to work. When it gets to the part of the file with:
Vertex 0: X:-20.500000 Y:16.500000 Z:16.250000

It seams to be reading X: and -20.500000 as one string since there is no space in between. And this throws the whole thing out of alignment. Is there a way to tell it to only read a certain amount of characters? In this instance 2 “X” and “:” .

Share this post


Link to post
Share on other sites
well since I''m the master of ugly hacks...
make a char dummy =)

char dum;
file >> dum >> dum >> x; // ''X'', '':'', -20.500000

Of course, if this was fscanf you''d write fscanf("X:%f", &x); =)
and boost has a boost::format to make it possible to write like you do in scanf/printf but using the C++ iostream library. Or maybe the boost::tokenizer is more your taste?

Good luck!

}-- Programmer/Gamer/Dreamer --{
I didn''t choose to code, coding choose me.

Share this post


Link to post
Share on other sites
I solved it on my own.

do{ch=file.get();}while(ch!=':');
file >> x;
do{ch=file.get();}while(ch!=':');
file >> y;
do{ch=file.get();}while(ch!=':');
file >> z;

My hack is even uglier I think.


[edited by - Grain on May 19, 2004 1:20:55 AM]

Share this post


Link to post
Share on other sites
Is there a reason you can''t just put a space between the colon and the number in your file? o_O

Share this post


Link to post
Share on other sites