Example #1
0
bool Foam::triSurface::readOFF(const fileName& OFFfileName)
{
    IFstream OFFfile(OFFfileName);

    if (!OFFfile.good())
    {
        FatalErrorIn("triSurface::readOFF(const fileName&)")
            << "Cannot read file " << OFFfileName
            << exit(FatalError);
    }

    // Read header
    string hdr = getLineNoComment(OFFfile);
    if (hdr != "OFF")
    {
        FatalErrorIn("triSurface::readOFF(const fileName&)")
            << "OFF file " << OFFfileName
            << " does not start with 'OFF'"
            << exit(FatalError);
    }


    label nPoints, nEdges, nElems;

    string line = getLineNoComment(OFFfile);
    {
        IStringStream lineStream(line);
        lineStream >> nPoints >> nElems >> nEdges;
    }

    // Read points
    pointField points(nPoints);

    forAll(points, pointi)
    {
        scalar x, y, z;
        line = getLineNoComment(OFFfile);
        {
            IStringStream lineStream(line);
            lineStream >> x >> y >> z;
        }
        points[pointi] = point(x, y, z);
    }
BOOL CSampleImportGeomviewPlugIn::ReadOffFile( const wchar_t* filename, CRhinoDoc& doc )
{
	std::string buf;
	ON_String str( filename );
	
	std::fstream OFFfile( str );
	OFFfile >> buf;

	if( buf != "OFF" )
		return FALSE;

	int i, j, numvert, numface, numedge;

	OFFfile >> numvert >> numface >> numedge;
	ON_Mesh mesh( numface, numvert, FALSE, FALSE );
	for( int i = 0; i < numvert; i++ )
  {
		ON_3dPoint p;
		OFFfile >> p.x >> p.y >> p.z;
		mesh.SetVertex( i, p );
	}

	int find = 0;
	int vind = numvert - 1;
	for( i = 0; i < numface; i++ )
  {
		int ngon;
		OFFfile >> ngon;
  	ON_SimpleArray<int> vert(32);
    vert.SetCount( ngon );
		for( j = 0; j < ngon; j++ )
			OFFfile >> vert[j];
		if( ngon == 3 )
    {
			mesh.SetTriangle( find++, vert[0], vert[1],vert[2] ); 
		}
		else if( ngon == 4 )
    {
			mesh.SetQuad( find++, vert[0], vert[1],vert[2], vert[3] ); 
		}
		else if( ngon > 4 )
    {
			ON_3fPoint center(0,0,0);
			for( j = 0; j < ngon; j++ )
				center += mesh.m_V[vert[j]];
			center *= (float)(1.0 / ngon);
			mesh.SetVertex( ++vind, center );
			
			for( j = 0; j < ngon; j++ )
      {
				mesh.SetTriangle( find++, vert[j], vert[(j+1)%ngon], vind ); 			
			}
		}
	}
	
  mesh.ComputeVertexNormals();
  mesh.Compact();

	doc.AddMeshObject( mesh );

  RhinoApp().RunScript( L"_Zoom _All _Extents", 0 );

  return TRUE;
}