importMRDAT.h 5.21 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
28
29
30
31
32
33
34
35
36
37
38
/*******************************************************************************
* 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__

namespace CGoGN
{

namespace Algo
{

namespace Import
{

class QuadTreeNode
{
Pierre Kraemer's avatar
Pierre Kraemer committed
39
40
41
42
43
44
public:	
	unsigned int indices[3] ;
	QuadTreeNode* children[4] ;
	QuadTreeNode* parent ;
	unsigned int level ;
	
Pierre Kraemer's avatar
Pierre Kraemer committed
45
46
47
48
49
50
	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
51
		parent = NULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
52
		level = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
53
54
55
56
57
58
59
60
61
62
63
	}

	~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
64
		assert(!isSubdivided()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
65
		for(unsigned int i = 0; i < 4; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
66
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
67
			children[i] = new QuadTreeNode() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
68
			children[i]->parent = this ;
Pierre Kraemer's avatar
Pierre Kraemer committed
69
			children[i]->level = level + 1 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
70
71
72
73
74
75
76
77
		}
	}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
78
	template <typename PFP>
79
	void embed(typename PFP::MAP& map, Dart d, std::vector<unsigned int>& vID)
Pierre Kraemer's avatar
Pierre Kraemer committed
80
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
81
		assert(map.getCurrentLevel() == level) ;
82

Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
		if(isSubdivided())
		{
85
86
			unsigned int v0 = vID[indices[0]] ;
			unsigned int v1 = vID[indices[1]] ;
87
//			unsigned int v2 = vID[indices[2]] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
88

89
90
			Dart it = d ;
			do
91
			{
92
93
94
95
96
97
98
99
				Dart next = map.phi1(it) ;
				unsigned int emb = map.getEmbedding(VERTEX, it) ;
				unsigned int idx = emb == v0 ? 0 : emb == v1 ? 1 : 2 ;
				map.incCurrentLevel() ;
				Dart dd = map.phi1(next) ;
				unsigned int oldEmb = map.getEmbedding(VERTEX, dd) ;
				unsigned int newEmb = vID[children[0]->indices[idx]] ;
				if(oldEmb == EMBNULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
100
				{
101
102
103
104
105
106
107
108
					map.embedOrbit(VERTEX, dd, newEmb) ;
					map.pushLevel() ;
					for(unsigned int i = map.getCurrentLevel() + 1; i <= map.getMaxLevel(); ++i)
					{
						map.setCurrentLevel(i) ;
						map.embedOrbit(VERTEX, dd, newEmb) ;
					}
					map.popLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
109
				}
110
111
112
113
114
				else
					assert(oldEmb == newEmb) ;
				map.decCurrentLevel() ;
				it = next ;
			} while(it != d) ;
115

Pierre Kraemer's avatar
Pierre Kraemer committed
116
			map.incCurrentLevel() ;
117
118
			Dart d0 = map.phi2(map.phi1(d)) ;
			children[0]->embed<PFP>(map, d0, vID) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
119
120
			map.decCurrentLevel() ;

121
122
123
124
125
126
127
128
129
			do
			{
				unsigned int emb = map.getEmbedding(VERTEX, it) ;
				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
130
		}
131
132
133
		else
		{
			if(map.getCurrentLevel() < map.getMaxLevel())
Pierre Kraemer's avatar
Pierre Kraemer committed
134
				std::cout << "adaptive subdivision not managed yet" << std::endl ;
135
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
136
137
138
139
140
141
142
143
144
145
	}

	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
146
147
148
149
150
151
	}
} ;

class QuadTree
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
152
	std::vector<QuadTreeNode*> roots ;
Pierre Kraemer's avatar
Pierre Kraemer committed
153
	std::vector<Dart> darts ;
154
	std::vector<unsigned int> verticesID ;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
156
157
158
159
160

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

Pierre Kraemer's avatar
Pierre Kraemer committed
162
	template <typename PFP>
163
	void embed(typename PFP::MAP& map)
Pierre Kraemer's avatar
Pierre Kraemer committed
164
165
	{
		for(unsigned int i = 0; i < roots.size(); ++i)
166
			roots[i]->embed<PFP>(map, darts[i], verticesID) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
171
172
173
174
175
176
177
	}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
180
181
182
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
183
184
185
186
187
188
189
190
191
} // namespace Import

} // namespace Algo

} // namespace CGoGN

#include "Algo/Import/importMRDAT.hpp"

#endif