surface_import.cpp 1.79 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "surface_import.h"

#include "schnapps.h"
#include "mapHandler.h"

#include "Algo/Import/import.h"

#include <QFileDialog>
#include <QFileInfo>

namespace CGoGN
{

namespace SCHNApps
{

17
bool Surface_Import_Plugin::enable()
Pierre Kraemer's avatar
Pierre Kraemer committed
18
{
19 20 21
//	magic line that init static variables of GenericMap in the plugins
	GenericMap::copyAllStatics(m_schnapps->getStaticPointers());

Pierre Kraemer's avatar
Pierre Kraemer committed
22 23 24 25 26 27
	importAction = new QAction("import", this);
	m_schnapps->addMenuAction(this, "Surface;Import", importAction);
	connect(importAction, SIGNAL(triggered()), this, SLOT(importFromFileDialog()));
	return true;
}

28
MapHandlerGen* Surface_Import_Plugin::importFromFile(const QString& fileName)
Pierre Kraemer's avatar
Pierre Kraemer committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42
{
	QFileInfo fi(fileName);
	if(fi.exists())
	{
		MapHandlerGen* mhg = m_schnapps->addMap(fi.baseName(), 2);
		if(mhg)
		{
			MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
			PFP2::MAP* map = mh->getMap();

			std::vector<std::string> attrNames;
			Algo::Surface::Import::importMesh<PFP2>(*map, fileName.toStdString(), attrNames);

			// get vertex position attribute
Sylvain Thery's avatar
Sylvain Thery committed
43
			VertexAttribute<PFP2::VEC3, PFP2::MAP> position = map->getAttribute<PFP2::VEC3, VERTEX, PFP2::MAP>(attrNames[0]);
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
			mh->registerAttribute(position);

			// update corresponding VBO & emit attribute update signal
			mh->notifyAttributeModification(position);

			// compute map bounding box
			mh->updateBB(position);
		}
		return mhg;
	}
	else
		return NULL;
}

58
void Surface_Import_Plugin::importFromFileDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62 63 64 65 66 67
{
	QStringList fileNames = QFileDialog::getOpenFileNames(m_schnapps, "Import surfaces", m_schnapps->getAppPath(), "Surface mesh Files (*.ply *.off *.trian)");
	QStringList::Iterator it = fileNames.begin();
	while(it != fileNames.end()) {
		importFromFile(*it);
		++it;
	}
}

68
Q_EXPORT_PLUGIN2(Surface_Import_Plugin, Surface_Import_Plugin)
Pierre Kraemer's avatar
Pierre Kraemer committed
69 70 71 72

} // namespace SCHNApps

} // namespace CGoGN