[vtkusers] RE: Problems with vtkProgrammableFilter

Andrew J. P. Maclean a.maclean at acfr.usyd.edu.au
Wed Feb 20 18:50:46 EST 2002


Vlad,
	Here is roughly what I do. Note the use of the static qualifier.
It is probably not the best solution and it is not fully tested, but if
there is any interest in the group I could parcel up the relevant
classes with some documentation at some stage.

I use a class as defined below and to read in data (I have only shown
part of the implementation) and the code to read is something like this
(there is also a parameter class which sets the parameters to be used
like the data order and the type of data).

// Reading in the data points.
	this->m_readerParams].Points(this->m_pts]);
			this->m_readerParams].Source(this->m_ptSrc]);
			// Read in the data
	
this->m_reader.LoadData(&m_readerParams]);
	
this->m_ptSrc->SetExecuteMethod(this->m_reader.LoadData,&this->m_readerP
arams);
					this->m_ptSrc->Update();
					TRACE("Ending read of surface:
%d\n",i);
					num_pts =
this->m_pts->GetNumberOfPoints();
					this->m_pts->GetBounds(bounds);

// This is the class

class CDataReader
{
public:
	CDataReader();
	virtual ~CDataReader();
	static void LoadData(void* pParameters);


private:
	static void LoadFromStream (void* pParameters);
	static void LoadFromVTKFile( void* pParameters );
	static void LoadFromUserFile( void * pParameters );
	static void LoadFromTextFile( void* pParameters );
	static void LoadIntFromBinaryFile( void* pParameters );
	static void LoadFloatFromBinaryFile( void* pParameters );
	static void LoadDoubleFromBinaryFile( void* pParameters );
	template <class T>
	static CCoordinate<T> Permute(CCoordinate<T> const &p, int const
&order);

};

// Here is the implementation of some of the functions.

/*!
 * Selects the correct data reader. Default is user.
 *
 * @param none
 *
 * @return void  : 
 */
void CDataReader::LoadData(void* pParameters)
{
	CDataReaderParams* pParams =
static_cast<CDataReaderParams*>(pParameters);

	switch (pParams->DataSourceType())
	{
	case pParams->data_source_type::VTK:
		LoadFromVTKFile(pParameters);
		break;
	case pParams->data_source_type::SERIAL:
		LoadFromStream(pParameters);
		break;
	case pParams->data_source_type::USER:
	default:
		LoadFromUserFile(pParameters);
		break;
	}
}

/*!
 * Load data that is in the native vtk data format
 *
 * @param none
 *
 * @return void  : 
 */
void CDataReader::LoadFromVTKFile(void* pParameters)
{
	CDataReaderParams* pParams =
static_cast<CDataReaderParams*>(pParameters);
	pParams->VTKReader()->SetFileName(pParams->FileName().c_str());
}


/*!
 * Load data from a user file that is in either text or
 * binary format. Default is text.
 *
 * @param none
 *
 * @return void  : 
 */
void CDataReader::LoadFromUserFile(void * pParameters)
{
	CDataReaderParams* pParams =
static_cast<CDataReaderParams*>(pParameters);

	switch(pParams->FileType())
	{
	case pParams->file_type::BINARY:
		switch(pParams->DataType())
		{
		case pParams->file_data_type::INTEGER:
			LoadIntFromBinaryFile(pParameters);
			break;
		case pParams->file_data_type::FLOAT:
			LoadFloatFromBinaryFile(pParameters);
			break;
		case pParams->file_data_type::DOUBLE:
		default:
			LoadDoubleFromBinaryFile(pParameters);
			break;
		}
		break;
	case pParams->file_type::TEXT:
	default:
		LoadFromTextFile(pParameters);
		break;
	}
}


/*!
 * Load points from a text file.
 * Each line of the file is expected to contain the
 * coordinates of one point, terminated by a LF (or CR/LF).
 * Once the three coordinates corresponding to a point
 * have been read, the remainder of the line is discarded.
 * Points are read in as double.
 * The expected format is that each coordinate of the point
 * is separated by whitespace. i.e x y z
 * Leading alpha or whitespace is stripped.
 *
 * @param fn : the file name 
 *
 * @param data_order : the way the coordinates of a point are ordered
 *
 * @param data_order : the vtk programmable data source
 *
 * @param data_order : the vtk point source
 *
 * @return void  : 
 */
void CDataReader::LoadFromTextFile( void * pParameters )
{
	CDataReaderParams* pParams =
static_cast<CDataReaderParams*>(pParameters);

	vtkPolyData *output;

	// This ties the PointsData to the Point Source
	output = pParams->Source()->GetPolyDataOutput();
	output->SetPoints( pParams->Points() );

	std::string fn = pParams->FileName();

	std::ifstream from(fn.c_str());
	if (!from)
	{
		TRACE("LoadFromTextFile(): %s does not
exist.",pParams->FileName());
		return;
	}

	while(from)
	{
		// Eat up whitespace and alpha characters at the start
of the line.
		char c;
		while(from.get(c))
		{
			if(!isspace(c)&&!isalpha(c))
			{
				from.putback(c);
				break;
			}
		}

		if(!from.eof())
		{
			CCoordinate<double> pt(3);
//			pt[0] = pt[1] = pt[2] = 0;
			from >> pt[0] >> pt[1] >> pt[2];
			// Insert into the point set in the right order.
			if (!from.eof())
			{
				if (pParams->DataOrder() !=
pParams->data_order::XYZ)
					pt =
Permute(pt,pParams->DataOrder());
	
pParams->Points()->InsertNextPoint(pt[0],pt[1],pt[2]);
			}
		}

		// Discard the rest of the line.
		std::string s;
		std::getline(from,s);
	}
//	TRACE("LoadFromTextFile() Points:
%d\n",pParams->Points()->GetNumberOfPoints());

	if (from)
		from.close();
}

___________________________________________
Andrew J. P. Maclean
Postal:
Australian Centre for Field Robotics
The Rose Street Building J04
The University of Sydney  2006  NSW
AUSTRALIA
Room:
106
Phone:
+61 2 9351 3283
Fax:
+61 2 9351 7474

http://www.acfr.usyd.edu.au/
___________________________________________


-----Original Message-----
From: Kaufman, Vlad [mailto:vkaufman at mc.com] 
Sent: Thursday, 21 February 2002 07:32
To: 'a.maclean at acfr.usyd.edu.au'
Cc: Kaufman, Vlad
Subject: RE: Problems with vtkProgrammableFilter

Dear Andrew,

I hope that about 6 months later, you already have a C++ example of a
function set via the SetExecuteMethod(void (*F)(void *)) for the
vtkProgrammableSource class.

I am just at the point when I need such an example.  Do you have one?
Can
you share it?

Thanks,

	- Vlad 


Thanks,

----
           - Vlad Kaufman
             Telephone: (978) 967-1921
             E-mail:       vkaufman at mc.com




More information about the vtkusers mailing list