Reading vertices and triangles from a file.

Started by
11 comments, last by Grain 19 years, 11 months ago
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);
};
Advertisement
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.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

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.
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.
Evillive2
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
i have just used fprintf in the past, works for me

ace
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=12file >> 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]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
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]
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 =)
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
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 “:” .

This topic is closed to new replies.

Advertisement