0

I am trying to read the data from some .OFF files(Object File Format)and store them in certain data struture. They contain the description of the composing polygons of a geometric object,(From wiki) and look like this:

    OFF
    2903 5804 120
    3.71636 2.34339 0
    4.12656 0.642027 0
    ...
    3   1955 2193 2185
    3   2193 1965 2192
  1. My understanding about the .OFF file structure is: Some header data on the very beginning. Data like ' 3.71636 2.34339 0' should be the coordinates of vertices. Data like '3 1955 2193 2185' should be 'the number of vertices of one face and the indexes of the vertices'. Is it correct?

  2. I found some methods to read data with C++. But I didn't find a way to read different types of data in one file. Is there a good way to read different data from one file?

  3. Is there a way to read the data row by row?

  4. How can I calculate the normals based on the data in such .OFF file?

Spektre
  • 49,595
  • 11
  • 110
  • 380
HF M
  • 21

1 Answers1

1

First see OFF fileformat descrition

  1. Yes starting 3 numbers are header

    its the number of points, faces, edges (the last is not very useful) So you know how big tables you have to allocate ahead before reading.

    beware the first OFF line is optional...

  2. Yes you can read more than one types from file

    just use fstream/cin or fscanf or whatever you got at your disposal I usually use direct binary file access instead of text file functions (as I have my own) for more info see

    however file access functions depend also on used OS and programing environment so yours might be called differently.

  3. Yes there is a way to read text file row by row (line by line)

    You have to parse the text line by line I read the whole file into memory and scan byte by byte for line separators 13,10

    Then parse line word by word by scanning for space,tab 32,9 or separators like ,;+- if I know ahead I am reading number then I consider any ASCII code not present in numbers as separator too.

    Then convert string to number (atof()) and append it to target table. Beware the national setting of decimal point separator in OS might affect the conversion as the file itself might use different one so you should handle that either by converting the string or by changng the separator for the conversion.

    Here an example of using std::ifstream to read line by line and parse wavefront file (similar to OFF but slightly more complex) The other answer is mine using my own functions for parsing instead...

  4. compute face normal using cross product on 2 of its edges

    This is very common way so if you have triangle:

    3   1955 2193 2185 
    

    then:

    normal = cross( pnt[1955]-pnt[2193] , pnt[2193]-pnt[2185] );
    

    If you compute the normal consistently from the same edges across all faces and your mesh has strict winding rule then all normals would point outside or inside of your mesh too...

Spektre
  • 49,595
  • 11
  • 110
  • 380