importMesh.hpp 10.2 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/generic/attributeHandler.h"
#include "Topology/generic/autoAttributeHandler.h"
#include "Container/fakeAttribute.h"
28
#include "Algo/Modelisation/polyhedron.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
29 30 31 32 33 34 35 36 37 38 39 40 41

namespace CGoGN
{

namespace Algo
{

namespace Import
{

template <typename PFP>
bool importMesh(typename PFP::MAP& map, MeshTablesSurface<PFP>& mts)
{
42
	AutoAttributeHandler< NoMathIONameAttribute< std::vector<Dart> >, VERTEX > vecDartsPerVertex(map, "incidents");
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46 47

	unsigned nbf = mts.getNbFaces();
	int index = 0;
	// buffer for tempo faces (used to remove degenerated edges)
	std::vector<unsigned int> edgesBuffer;
48
	edgesBuffer.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
49 50 51 52 53 54 55 56 57 58 59 60 61

	DartMarkerNoUnmark m(map) ;

	// for each face of table
	for(unsigned int i = 0; i < nbf; ++i)
	{
		// store face in buffer, removing degenerated edges
		unsigned int nbe = mts.getNbEdgesFace(i);
		edgesBuffer.clear();
		unsigned int prec = EMBNULL;
		for (unsigned int j = 0; j < nbe; ++j)
		{
			unsigned int em = mts.getEmbIdx(index++);
Pierre Kraemer's avatar
Pierre Kraemer committed
62
			if (em != prec)
Pierre Kraemer's avatar
Pierre Kraemer committed
63 64 65 66 67 68 69 70 71 72 73 74 75
			{
				prec = em;
				edgesBuffer.push_back(em);
			}
		}
		// check first/last vertices
		if (edgesBuffer.front() == edgesBuffer.back())
			edgesBuffer.pop_back();

		// create only non degenerated faces
		nbe = edgesBuffer.size();
		if (nbe > 2)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
76
			Dart d = map.newFace(nbe, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79
			for (unsigned int j = 0; j < nbe; ++j)
			{
				unsigned int em = edgesBuffer[j];		// get embedding
80

81
				FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, em);
82
//				foreach_dart_of_orbit_in_parent<typename PFP::MAP>(&map, VERTEX, d, fsetemb) ;
83
				map.template foreach_dart_of_orbit<VERTEX + PFP::MAP::IN_PARENT>(d, fsetemb);
84

85
				m.mark(d) ;								// mark on the fly to unmark on second loop
Pierre Kraemer's avatar
Pierre Kraemer committed
86
				vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
Pierre Kraemer's avatar
Pierre Kraemer committed
87 88 89 90 91 92
				d = map.phi1(d);
			}
		}
	}

	// reconstruct neighbourhood
93
	unsigned int nbBoundaryEdges = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
94 95 96 97 98 99 100
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (m.isMarked(d))
		{
			// darts incident to end vertex of edge
			std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

101
			unsigned int embd = map.template getEmbedding<VERTEX>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
102
			Dart good_dart = NIL;
103
			for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
104
			{
105
				if (map.template getEmbedding<VERTEX>(map.phi1(*it)) == embd)
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108
					good_dart = *it;
			}

109
			if (good_dart != NIL)
Pierre Kraemer's avatar
Pierre Kraemer committed
110
			{
111
				map.sewFaces(d, good_dart, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
112
				m.unmarkOrbit<EDGE>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114 115
			}
			else
			{
116
				m.unmark(d);
117
				++nbBoundaryEdges;
Pierre Kraemer's avatar
Pierre Kraemer committed
118 119 120 121
			}
		}
	}

122
	if (nbBoundaryEdges > 0)
Sylvain Thery's avatar
Sylvain Thery committed
123
	{
124 125
		unsigned int nbH = map.closeMap();
		CGoGNout << "Map closed (" << nbBoundaryEdges << " boundary edges / " << nbH << " holes)" << CGoGNendl;
126
		// ensure bijection between topo and embedding
127
		map.template bijectiveOrbitEmbedding<VERTEX>();
Sylvain Thery's avatar
Sylvain Thery committed
128
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
129 130 131 132

	return true ;
}

133 134 135
template <typename PFP>
bool importMeshSToV(typename PFP::MAP& map, MeshTablesSurface<PFP>& mts, float dist)
{
136
	AutoAttributeHandler< NoMathIONameAttribute< std::vector<Dart> >, VERTEX > vecDartsPerVertex(map, "incidents");
137 138 139 140 141 142 143 144
	unsigned nbf = mts.getNbFaces();
	int index = 0;
	// buffer for tempo faces (used to remove degenerated edges)
	std::vector<unsigned int> edgesBuffer;
	edgesBuffer.reserve(16);

	DartMarkerNoUnmark m(map) ;

145
	AttributeHandler<typename PFP::VEC3, VERTEX> position = map.template getAttribute<typename PFP::VEC3, VERTEX>("position");
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	std::vector<unsigned int > backEdgesBuffer(mts.getNbVertices(), EMBNULL);

	// for each face of table -> create a prism
	for(unsigned int i = 0; i < nbf; ++i)
	{
		// store face in buffer, removing degenerated edges
		unsigned int nbe = mts.getNbEdgesFace(i);
		edgesBuffer.clear();
		unsigned int prec = EMBNULL;
		for (unsigned int j = 0; j < nbe; ++j)
		{
			unsigned int em = mts.getEmbIdx(index++);
			if (em != prec)
			{
				prec = em;
				edgesBuffer.push_back(em);
			}
		}
		// check first/last vertices
		if (edgesBuffer.front() == edgesBuffer.back())
			edgesBuffer.pop_back();

		// create only non degenerated faces
		nbe = edgesBuffer.size();
		if (nbe > 2)
		{
			Dart d = Algo::Modelisation::createPrism<PFP>(map, nbe);

			//Embed the base faces
			for (unsigned int j = 0; j < nbe; ++j)
			{
				unsigned int em = edgesBuffer[j];		// get embedding

				if(backEdgesBuffer[em] == EMBNULL)
				{
181
					unsigned int emn = map.template newCell<VERTEX>();
Pierre Kraemer's avatar
Pierre Kraemer committed
182
					map.copyCell<VERTEX>(emn, em);
183 184 185 186
					backEdgesBuffer[em] = emn;
					position[emn] += typename PFP::VEC3(0,0,dist);
				}

187
				FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, em);
untereiner's avatar
untereiner committed
188
				//foreach_dart_of_orbit_in_parent<typename PFP::MAP>(&map, VERTEX, d, fsetemb) ;
189
				map.foreach_dart_of_orbit<VERTEX + PFP::MAP::IN_PARENT>(d, fsetemb);
190 191 192 193

				//Embed the other base face
				Dart d2 = map.phi1(map.phi1(map.phi2(d)));
				unsigned int em2 = backEdgesBuffer[em];
194
				FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb2(map, em2);
untereiner's avatar
untereiner committed
195
				//foreach_dart_of_orbit_in_parent<typename PFP::MAP>(&map, VERTEX, d2, fsetemb2) ;
196
				map.foreach_dart_of_orbit<VERTEX + PFP::MAP::IN_PARENT>(d2, fsetemb2);
197 198 199

				m.mark(d) ;								// mark on the fly to unmark on second loop
				vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
200
				d = map.phi_1(d);
201 202 203 204 205 206 207 208 209 210 211 212 213 214
			}

		}
	}

	// reconstruct neighbourhood
	unsigned int nbBoundaryEdges = 0;
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (m.isMarked(d))
		{
			// darts incident to end vertex of edge
			std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

215
			unsigned int embd = map.getEmbedding<VERTEX>(d);
216 217 218
			Dart good_dart = NIL;
			for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
			{
219
				if (map.getEmbedding<VERTEX>(map.phi1(*it)) == embd)
220 221 222 223 224 225
					good_dart = *it;
			}

			if (good_dart != NIL)
			{
				map.sewVolumes(map.phi2(d), map.phi2(good_dart), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
226
				m.unmarkOrbit<EDGE>(d);
227 228 229 230 231 232 233 234 235 236 237 238
			}
			else
			{
				m.unmark(d);
				++nbBoundaryEdges;
			}
		}
	}

	return true ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
239 240 241 242 243
template <typename PFP>
bool importMesh(typename PFP::MAP& map, MeshTablesVolume<PFP>& mtv)
{
	typedef typename PFP::VEC3 VEC3 ;

244
	AutoAttributeHandler< NoMathIONameAttribute< std::vector<Dart> >, VERTEX > vecDartsPerVertex(map, "incidents");
Pierre Kraemer's avatar
Pierre Kraemer committed
245

246
	return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
250
bool importMesh(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, bool mergeCloseVertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
251
{
Pierre Kraemer's avatar
Pierre Kraemer committed
252
	MeshTablesSurface<PFP> mts(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
253

Pierre Kraemer's avatar
Pierre Kraemer committed
254
	if(!mts.importMesh(filename, attrNames))
Pierre Kraemer's avatar
Pierre Kraemer committed
255
		return false;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
256

257 258
	if (mergeCloseVertices)
		mts.mergeCloseVertices();
Pierre Kraemer's avatar
Pierre Kraemer committed
259 260 261 262

	return importMesh<PFP>(map, mts);
}

263
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
264
bool importMeshV(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, bool mergeCloseVertices)
265
{
Pierre Kraemer's avatar
Pierre Kraemer committed
266 267 268 269 270
	ImportVolumique::ImportType kind = ImportVolumique::UNKNOWNVOLUME;

	if ((filename.rfind(".tet") != std::string::npos) || (filename.rfind(".TET") != std::string::npos))
		kind = ImportVolumique::TET;

271 272
	if ((filename.rfind(".off") != std::string::npos) || (filename.rfind(".OFF") != std::string::npos))
		kind = ImportVolumique::OFF;
Pierre Kraemer's avatar
Pierre Kraemer committed
273

untereiner's avatar
untereiner committed
274 275
	if ((filename.rfind(".node") != std::string::npos) || (filename.rfind(".NODE") != std::string::npos))
		kind = ImportVolumique::NODE;
Pierre Kraemer's avatar
Pierre Kraemer committed
276 277 278 279

	if ((filename.rfind(".ts") != std::string::npos) || (filename.rfind(".TS") != std::string::npos))
		kind = ImportVolumique::TS;

280 281 282 283 284
	switch (kind)
	{
		case ImportVolumique::TET:
			return Algo::Import::importTet<PFP>(map, filename, attrNames, 1.0f);
			break;
285
		case ImportVolumique::OFF:
286 287
		{
			size_t pos = filename.rfind(".");
288 289 290 291 292 293 294 295 296 297 298 299 300
			std::string fileEle = filename;
			fileEle.erase(pos);
			fileEle.append(".ele");
			return Algo::Import::importOFFWithELERegions<PFP>(map, filename, fileEle, attrNames);
			break;
		}
		case ImportVolumique::NODE:
		{
			size_t pos = filename.rfind(".");
			std::string fileEle = filename;
			fileEle.erase(pos);
			fileEle.append(".ele");
			return Algo::Import::importNodeWithELERegions<PFP>(map, filename, fileEle, attrNames);
301 302 303 304 305 306 307 308 309 310 311 312
			break;
		}
		case ImportVolumique::TS:
			Algo::Import::importTs<PFP>(map, filename, attrNames, 1.0f);
			break;
		default:
			CGoGNerr << "Not yet supported" << CGoGNendl;
			break;
	}
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
313
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
314
bool importMeshToExtrude(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
315
{
316
	float dist = 5.0f;
317
	MeshTablesSurface<PFP> mts(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
318

Pierre Kraemer's avatar
Pierre Kraemer committed
319
	if(!mts.importMesh(filename, attrNames))
320
		return false;
321 322

	return importMeshSToV<PFP>(map, mts, dist);
323 324
}

Pierre Kraemer's avatar
Pierre Kraemer committed
325 326 327 328 329
} // namespace Import

} // namespace Algo

} // namespace CGoGN