surface_import.cpp 4.06 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6
#include "surface_import.h"

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

#include "Algo/Import/import.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
7
#include "Algo/Tiling/Surface/square.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
8 9 10 11 12 13 14 15 16 17

#include <QFileDialog>
#include <QFileInfo>

namespace CGoGN
{

namespace SCHNApps
{

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

Pierre Kraemer's avatar
Pierre Kraemer committed
23 24 25 26 27 28 29 30
	importMeshAction = new QAction("import mesh", this);
	m_schnapps->addMenuAction(this, "Surface;Import Mesh", importMeshAction);
	connect(importMeshAction, SIGNAL(triggered()), this, SLOT(importMeshFromFileDialog()));

	importImageAction = new QAction("import image", this);
	m_schnapps->addMenuAction(this, "Surface;Import Image", importImageAction);
	connect(importImageAction, SIGNAL(triggered()), this, SLOT(importImageFromFileDialog()));

Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
34
MapHandlerGen* Surface_Import_Plugin::importMeshFromFile(const QString& fileName)
Pierre Kraemer's avatar
Pierre Kraemer committed
35 36 37 38
{
	QFileInfo fi(fileName);
	if(fi.exists())
	{
Sylvain Thery's avatar
Sylvain Thery committed
39 40
		pythonRecording("importFromFile", fi.baseName(), fileName);

Pierre Kraemer's avatar
Pierre Kraemer committed
41 42 43 44 45 46 47 48 49
		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);

50 51 52 53 54 55 56 57 58 59
			for (unsigned int orbit = VERTEX; orbit <= VOLUME; orbit++)
			{
				AttributeContainer& cont = map->getAttributeContainer(orbit);
				std::vector<std::string> names;
				std::vector<std::string> types;
				cont.getAttributesNames(names);
				cont.getAttributesTypes(types);
				for(unsigned int i = 0; i < names.size(); ++i)
					mhg->registerAttribute(orbit, QString::fromStdString(names[i]), QString::fromStdString(types[i]));
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
60 61 62 63 64 65 66
		}
		return mhg;
	}
	else
		return NULL;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
67
void Surface_Import_Plugin::importMeshFromFileDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
68
{
Pierre Kraemer's avatar
Pierre Kraemer committed
69
	QStringList fileNames = QFileDialog::getOpenFileNames(m_schnapps, "Import surface meshes", m_schnapps->getAppPath(), "Surface mesh Files (*.ply *.off *.trian)");
Pierre Kraemer's avatar
Pierre Kraemer committed
70
	QStringList::Iterator it = fileNames.begin();
Pierre Kraemer's avatar
Pierre Kraemer committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	while(it != fileNames.end())
	{
		importMeshFromFile(*it);
		++it;
	}
}

MapHandlerGen* Surface_Import_Plugin::importImageFromFile(const QString& fileName)
{
	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();

			VertexAttribute<PFP2::VEC3, PFP2::MAP> position = map->addAttribute<PFP2::VEC3, VERTEX, PFP2::MAP>("position");
			VertexAttribute<PFP2::VEC3, PFP2::MAP> color = map->addAttribute<PFP2::VEC3, VERTEX, PFP2::MAP>("color");

			QImage image;
			QString extension = fi.suffix();
			extension.toUpper();
			if(!image.load(fileName, extension.toUtf8().constData()))
			{
				CGoGNout << "Image [" << fileName.toStdString() << "] has not been loaded correctly" << CGoGNendl;
				return NULL;
			}

			int imageX = image.width();
			int imageY = image.height();
			Algo::Surface::Tilings::Square::Grid<PFP2> grid(*map, imageX - 1, imageY - 1);
			grid.embedIntoGrid(position, imageX - 1, imageY - 1);
			const std::vector<Dart>& vDarts = grid.getVertexDarts();
			QRgb pixel;
			for(int i = 0; i < imageX; ++i)
			{
				for(int j = 0; j < imageY; ++j)
				{
					pixel = image.pixel(i, (imageY-j) - 1);
					color[vDarts[j*imageX + i]] = PFP2::VEC3(qRed(pixel)/255.f, qGreen(pixel)/255.f, qBlue(pixel)/255.f);
				}
			}

			mh->registerAttribute(position);
			mh->registerAttribute(color);

		}
		return mhg;
	}
	else
		return NULL;
}

void Surface_Import_Plugin::importImageFromFileDialog()
{
	QStringList fileNames = QFileDialog::getOpenFileNames(m_schnapps, "Import images", m_schnapps->getAppPath(), "Surface mesh Files (*.png *.jpg *.jpeg)");
	QStringList::Iterator it = fileNames.begin();
	while(it != fileNames.end())
	{
		importImageFromFile(*it);
Pierre Kraemer's avatar
Pierre Kraemer committed
133 134 135 136
		++it;
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
137 138 139 140 141
#if CGOGN_QT_DESIRED_VERSION == 5
	Q_PLUGIN_METADATA(IID "CGoGN.SCHNapps.Plugin")
#else
	Q_EXPORT_PLUGIN2(Surface_Import_Plugin, Surface_Import_Plugin)
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
142 143 144 145

} // namespace SCHNApps

} // namespace CGoGN