importMRDAT.h 5.84 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
17
18
19
20
21
22
23
24
25
26
27
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __IMPORT_MR_DAT__
#define __IMPORT_MR_DAT__

untereiner's avatar
untereiner committed
28
29
#include "Topology/generic/autoAttributeHandler.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
30
31
32
33
34
35
namespace CGoGN
{

namespace Algo
{

36
37
38
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
39
40
41
42
43
namespace Import
{

class QuadTreeNode
{
Pierre Kraemer's avatar
Pierre Kraemer committed
44
45
46
47
48
49
public:	
	unsigned int indices[3] ;
	QuadTreeNode* children[4] ;
	QuadTreeNode* parent ;
	unsigned int level ;
	
Pierre Kraemer's avatar
Pierre Kraemer committed
50
51
52
53
54
55
	QuadTreeNode()
	{
		for(unsigned int i = 0; i < 3; ++i)
			indices[i] = -1 ;
		for(unsigned int i = 0; i < 4; ++i)
			children[i] = NULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
56
		parent = NULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
57
		level = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58
59
60
61
62
63
64
65
66
67
68
	}

	~QuadTreeNode()
	{
		for(unsigned int i = 0; i < 4; ++i)
			if(children[i] != NULL)
				delete children[i] ;
	}

	void subdivide()
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
69
		assert(!isSubdivided()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
70
		for(unsigned int i = 0; i < 4; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
71
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
72
			children[i] = new QuadTreeNode() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
73
			children[i]->parent = this ;
Pierre Kraemer's avatar
Pierre Kraemer committed
74
			children[i]->level = level + 1 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
75
76
77
78
79
80
81
82
		}
	}

	bool isSubdivided()
	{
		return children[0] != NULL ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
83
	template <typename PFP>
84
	void embed(typename PFP::MAP& map, Dart d, std::vector<unsigned int>& vID)
Pierre Kraemer's avatar
Pierre Kraemer committed
85
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
86
		assert(map.getCurrentLevel() == level) ;
87

Pierre Kraemer's avatar
Pierre Kraemer committed
88
89
		if(isSubdivided())
		{
90
91
			unsigned int v0 = vID[indices[0]] ;
			unsigned int v1 = vID[indices[1]] ;
92
//			unsigned int v2 = vID[indices[2]] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93

94
95
			Dart it = d ;
			do
96
			{
97
				Dart next = map.phi1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
98
				unsigned int emb = map.template getEmbedding<VERTEX>(it) ;
99
100
101
				unsigned int idx = emb == v0 ? 0 : emb == v1 ? 1 : 2 ;
				map.incCurrentLevel() ;
				Dart dd = map.phi1(next) ;
untereiner's avatar
untereiner committed
102
//				unsigned int oldEmb = map.template getEmbedding<VERTEX>(dd) ;
103
				unsigned int newEmb = vID[children[0]->indices[idx]] ;
untereiner's avatar
untereiner committed
104
105
//				if(oldEmb == EMBNULL)
//				{
106
					//std::cout << "oldEmb == NULL"<< std::endl;
107
					Algo::Topo::setOrbitEmbedding<VERTEX>(map,dd,newEmb);
untereiner's avatar
untereiner committed
108
109
110
111
112
113

					//needed because the darts are duplicated at each level
					//and the vertex orbits are initialized at the creation of each level with wrong embedding indices

					//map.pushLevel() ;
					unsigned int cur = map.getCurrentLevel(); //necessary because IHM2 does not allow a Stack
114
115
116
					for(unsigned int i = map.getCurrentLevel() + 1; i <= map.getMaxLevel(); ++i)
					{
						map.setCurrentLevel(i) ;
117
118
						Algo::Topo::setOrbitEmbedding<VERTEX>(map,dd,newEmb);

119
					}
untereiner's avatar
untereiner committed
120
121
122
123
124
125
126
127
					//map.popLevel() ;
					map.setCurrentLevel(cur);
//				}
//				else
//				{
//					//std::cout << "oldEmb != NULL"<< std::endl;
//					assert(oldEmb == newEmb) ;
//				}
128

129
130
131
				map.decCurrentLevel() ;
				it = next ;
			} while(it != d) ;
132

133
			//embed the 4 children (begin with the center child)
Pierre Kraemer's avatar
Pierre Kraemer committed
134
			map.incCurrentLevel() ;
135
136
			Dart d0 = map.phi2(map.phi1(d)) ;
			children[0]->embed<PFP>(map, d0, vID) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
137
138
			map.decCurrentLevel() ;

139
140
			do
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
141
				unsigned int emb = map.template getEmbedding<VERTEX>(it) ;
142
143
144
145
146
147
				unsigned int idx = emb == v0 ? 0 : emb == v1 ? 1 : 2 ;
				map.incCurrentLevel() ;
				children[idx+1]->embed<PFP>(map, it, vID) ;
				map.decCurrentLevel() ;
				it = map.phi1(it) ;
			} while(it != d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
148
		}
149
150
151
		else
		{
			if(map.getCurrentLevel() < map.getMaxLevel())
Pierre Kraemer's avatar
Pierre Kraemer committed
152
				std::cout << "adaptive subdivision not managed yet" << std::endl ;
153
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
154
155
156
157
158
159
160
161
162
163
	}

	void print()
	{
		std::cout << indices[0] << " " << indices[1] << " " << indices[2] << std::endl ;
		if(isSubdivided())
		{
			for(unsigned int i = 0; i < 4; ++i)
				children[i]->print() ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
164
165
166
167
168
169
	}
} ;

class QuadTree
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
170
	std::vector<QuadTreeNode*> roots ;
Pierre Kraemer's avatar
Pierre Kraemer committed
171
	std::vector<Dart> darts ;
172
	std::vector<unsigned int> verticesID ;
173
	unsigned int depth;
Pierre Kraemer's avatar
Pierre Kraemer committed
174
175
176
177
178
179

	~QuadTree()
	{
		for(unsigned int i = 0; i < roots.size(); ++i)
			delete roots[i] ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
180

Pierre Kraemer's avatar
Pierre Kraemer committed
181
	template <typename PFP>
182
	void embed(typename PFP::MAP& map)
Pierre Kraemer's avatar
Pierre Kraemer committed
183
184
	{
		for(unsigned int i = 0; i < roots.size(); ++i)
185
			roots[i]->embed<PFP>(map, darts[i], verticesID) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
186
187
188
189
190
191
192
193
194
195
196
	}

	void print()
	{
		std::cout << "printing quadtree (" << roots.size() << " roots)" << std::endl ;
		for(unsigned int i = 0; i < roots.size(); ++i)
		{
			std::cout << "root " << i << std::endl ;
			roots[i]->print() ;
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
197
198
} ;

Pierre Kraemer's avatar
Pierre Kraemer committed
199
200
201
template <typename PFP>
bool importMRDAT(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, QuadTree& qt) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
202
203
} // namespace Import

204
205
}

Pierre Kraemer's avatar
Pierre Kraemer committed
206
207
208
209
210
211
212
} // namespace Algo

} // namespace CGoGN

#include "Algo/Import/importMRDAT.hpp"

#endif