surface_import.cpp 4.3 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"
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());

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;
}

34
MapHandlerGen* Surface_Import_Plugin::importMeshFromFile(const QString& nameOfFile)
Pierre Kraemer's avatar
Pierre Kraemer committed
35
{
36 37
	QString fileName = SCHNApps::noBackSlash(nameOfFile);

Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40
	QFileInfo fi(fileName);
	if(fi.exists())
	{
41
		MapHandlerGen* mhg = m_schnapps->addMap(SCHNApps::forceASCII(fi.baseName()), 2);
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43 44 45 46 47
		if(mhg)
		{
			MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
			PFP2::MAP* map = mh->getMap();

			std::vector<std::string> attrNames;
48
			Algo::Surface::Import::importMesh<PFP2>(*map, SCHNApps::niceStdString(fileName), attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
49

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
		pythonRecording("importMeshFromFile", mhg->getName(), fileName);
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63 64 65 66 67
		return mhg;
	}
	else
		return NULL;
}

68
void Surface_Import_Plugin::importMeshFromFileDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
69
{
70
	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
71
	QStringList::Iterator it = fileNames.begin();
72 73 74 75 76 77 78
	while(it != fileNames.end())
	{
		importMeshFromFile(*it);
		++it;
	}
}

79
MapHandlerGen* Surface_Import_Plugin::importImageFromFile(const QString& nameOfFile)
80
{
81 82
	QString fileName = SCHNApps::noBackSlash(nameOfFile);

83 84 85
	QFileInfo fi(fileName);
	if(fi.exists())
	{
86
		MapHandlerGen* mhg = m_schnapps->addMap(SCHNApps::forceASCII(fi.baseName()), 2);
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
		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);

		}
123
		pythonRecording("importImageFromFile", mhg->getName(), fileName);
124 125 126 127 128 129 130 131 132 133 134 135 136
		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
137 138 139 140
		++it;
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
141 142 143 144 145
#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
146 147 148 149

} // namespace SCHNApps

} // namespace CGoGN