primitives3d.hpp 6.92 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25
26
#include "Algo/Modelisation/polyhedron.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
27
28
29
30
31
32
namespace CGoGN
{

namespace Algo
{

33
34
35
namespace Volume
{

Pierre Kraemer's avatar
Pierre Kraemer committed
36
37
38
39
namespace Modelisation
{

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
40
Dart Primitive3D<PFP>::HexaGrid1Topo(unsigned int nx)
Pierre Kraemer's avatar
Pierre Kraemer committed
41
42
{
	// first cube
untereiner's avatar
untereiner committed
43

44
	Dart d0 = Surface::Modelisation::createHexahedron<PFP>(m_map,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
45
	m_tableVertDarts.push_back(d0);
untereiner's avatar
untereiner committed
46

Pierre Kraemer's avatar
Pierre Kraemer committed
47
48
	Dart d1 = m_map.template phi<2112>(d0);

Pierre Kraemer's avatar
Pierre Kraemer committed
49
	for (unsigned int i = 1; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	{
51
		Dart d2 = Surface::Modelisation::createHexahedron<PFP>(m_map,false);
untereiner's avatar
untereiner committed
52

Pierre Kraemer's avatar
Pierre Kraemer committed
53
		m_tableVertDarts.push_back(d2);
54
		m_map.sewVolumes(d1, d2, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
55
56
57
58
		d1 = m_map.template phi<2112>(d2);
	}

	// add last vertex (x=nx)
untereiner's avatar
untereiner committed
59
	d1 = m_map.phi2(d1); //TODO can take phi1 instead > same vertex ??
Pierre Kraemer's avatar
Pierre Kraemer committed
60
61
62
63
64
65
	m_tableVertDarts.push_back(d1);

	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
66
Dart Primitive3D<PFP>::HexaGrid2Topo(unsigned int nx, unsigned int ny)
Pierre Kraemer's avatar
Pierre Kraemer committed
67
68
69
70
71
{
	// creation premiere ligne
	Dart d0 = HexaGrid1Topo(nx);
	Dart d1 = m_map.template phi<112>(d0);

Pierre Kraemer's avatar
Pierre Kraemer committed
72
	for (unsigned int i = 1; i < ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
73
74
75
	{
		// creation ligne suivante
		Dart d2 = HexaGrid1Topo(nx);
Pierre Kraemer's avatar
Pierre Kraemer committed
76
		Dart d3 = m_map.phi2(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
77
78

		// couture des deux lignes de cubes
Pierre Kraemer's avatar
Pierre Kraemer committed
79
		for (unsigned int i = 0; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
80
		{
81
			m_map.sewVolumes(d1, d3, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
82
83
84
85
86
87
88
89
90
91
			d1 = m_map.template phi<11232>(d1);
			d3 = m_map.template phi<11232>(d3);
		}
		// passage a lignes suivante
		d1 = m_map.template phi<112>(d2);
	}

	// add last row of vertices (y = ny)

	int index = m_tableVertDarts.size()-(nx+1); // pos of last inserted row of dart
Pierre Kraemer's avatar
Pierre Kraemer committed
92
	for (unsigned int i = 0; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
93
94
	{
		Dart dd = m_tableVertDarts[index++];
95
		dd = m_map.template phi<112>(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
96
97
98
99
		m_tableVertDarts.push_back(dd);	
	}
	// warning last vertex of row has not same dart
	Dart dd = m_tableVertDarts[index++];
100
	dd = m_map.template phi<211>(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
101
102
103
104
105
106
	m_tableVertDarts.push_back(dd);	

	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
107
Dart Primitive3D<PFP>::hexaGrid_topo(unsigned int nx, unsigned int ny, unsigned int nz)
Pierre Kraemer's avatar
Pierre Kraemer committed
108
109
110
111
112
113
114
115
{
	m_kind = HEXAGRID;
	m_nx = nx;
	m_ny = ny;
	m_nz = nz;
	m_tableVertDarts.clear();
	m_tableVertDarts.reserve((nx+1)*(ny+1)*(nz+1));

116
	Dart d0 = HexaGrid2Topo(nx, ny);
Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
	Dart d1 = m_map.template phi<12>(d0);

Pierre Kraemer's avatar
Pierre Kraemer committed
119
	for (unsigned int i = 1; i < nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
120
121
	{
		// creation grille suivante
122
		Dart d2 = HexaGrid2Topo(nx, ny);
Pierre Kraemer's avatar
Pierre Kraemer committed
123
124
125
		Dart d3 = m_map.phi2(m_map.phi_1(d2));
		
		// couture des deux grilles 2D de cubes
Pierre Kraemer's avatar
Pierre Kraemer committed
126
		for (unsigned int j = 0; j < ny; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
127
128
129
		{
			Dart da = d1;
			Dart db = d3;
Pierre Kraemer's avatar
Pierre Kraemer committed
130
			for (unsigned int k = 0; k < nx; ++k)
Pierre Kraemer's avatar
Pierre Kraemer committed
131
			{
132
				m_map.sewVolumes(da, db, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
133
134
135
136
137
138
139
140
141
142
143
144
145
				da = m_map.template phi<11232>(da);
				db = m_map.template phi<11232>(db);
			}
			d1 = m_map.phi_1(d1);
			d1 = m_map.template phi<232>(d1);
			d1 = m_map.phi_1(d1);
			d3 = m_map.template phi<12321>(d3);
		}
		// passage a lignes suivante
		d1 = m_map.template phi<12>(d2);
	}

	// add last slice of vertices to the table
Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
148
	unsigned int nb = (nx+1)*(ny+1);	// nb of vertices in one slice XY
	unsigned int index = nb*(nz-1);	// last slice
	for (unsigned int i = 0; i < nb; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
149
150
	{
		Dart dd = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
151
		dd = m_map.phi2(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
152
153
154
		m_tableVertDarts.push_back(dd);
	}

155
	std::cout << m_map.closeMap() << std::endl;
156

Pierre Kraemer's avatar
Pierre Kraemer committed
157
158
159
160
	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
161
void Primitive3D<PFP>::embedHexaGrid(float x, float y, float z)
Pierre Kraemer's avatar
Pierre Kraemer committed
162
163
164
{
	if (m_kind != HEXAGRID)
	{
165
		CGoGNerr << "Warning try to embedHexaGrid something that is not a grid of hexahedron"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
166
167
168
169
170
171
172
		return;
	}

	float dx = x/float(m_nx);
	float dy = y/float(m_ny);
	float dz = z/float(m_nz);

Pierre Kraemer's avatar
Pierre Kraemer committed
173
	unsigned int nbs = (m_nx+1)*(m_ny+1);
Pierre Kraemer's avatar
Pierre Kraemer committed
174

Pierre Kraemer's avatar
Pierre Kraemer committed
175
	for(unsigned int i = 0; i <= m_nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
176
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
177
		for(unsigned int j = 0; j <= m_ny; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
178
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
179
			for(unsigned int k = 0; k <= m_nx; ++k)
Pierre Kraemer's avatar
Pierre Kraemer committed
180
181
182
183
			{
				typename PFP::VEC3 pos(-x/2.0f + dx*float(k), -y/2.0f + dy*float(j), -z/2.0f + dz*float(i));
				Dart d = m_tableVertDarts[ i*nbs+j*(m_nx+1)+k ];

184
				m_map.template setOrbitEmbeddingOnNewCell<VERTEX>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
185
186
187
188
189
190
				m_positions[d] = pos;
			}
		}
	}
}

untereiner's avatar
untereiner committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
template <typename PFP>
void Primitive3D<PFP>::embedHexaGrid(typename PFP::VEC3 origin, float x, float y, float z)
{
	if (m_kind != HEXAGRID)
	{
		CGoGNerr << "Warning try to embedHexaGrid something that is not a grid of hexahedron"<<CGoGNendl;
		return;
	}

	float dx = x/float(m_nx);
	float dy = y/float(m_ny);
	float dz = z/float(m_nz);

	unsigned int nbs = (m_nx+1)*(m_ny+1);

	for(unsigned int i = 0; i <= m_nz; ++i)
	{
		for(unsigned int j = 0; j <= m_ny; ++j)
		{
			for(unsigned int k = 0; k <= m_nx; ++k)
			{
				typename PFP::VEC3 pos(-x/2.0f + dx*float(k), -y/2.0f + dy*float(j), -z/2.0f + dz*float(i));
				Dart d = m_tableVertDarts[ i*nbs+j*(m_nx+1)+k ];

				m_map.template setOrbitEmbeddingOnNewCell<VERTEX>(d);
				m_positions[d] = origin + pos;
			}
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
222
223
224
template <typename PFP>
void Primitive3D<PFP>::transform(const Geom::Matrix44f& matrice)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
225
	for(typename std::vector<Dart>::iterator di = m_tableVertDarts.begin(); di != m_tableVertDarts.end(); ++di)
Pierre Kraemer's avatar
Pierre Kraemer committed
226
227
228
229
230
231
232
	{
		typename PFP::VEC3& pos = m_positions[*di];
		pos = Geom::transform(pos, matrice);
	}

}

Pierre Kraemer's avatar
Pierre Kraemer committed
233
234
235
236
237
238
239
240
//template <typename PFP>
//void Primitive3D<PFP>::mark(Mark m)
//{
//	for(typename std::vector<Dart>::iterator di = m_tableVertDarts.begin(); di != m_tableVertDarts.end(); ++di)
//	{
//		m_map.markOrbit(0, *di, m);
//	}
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
241

Pierre Kraemer's avatar
Pierre Kraemer committed
242
243
} // namespace Modelisation

244
245
}

Pierre Kraemer's avatar
Pierre Kraemer committed
246
247
248
} // namespace Algo

} // namespace CGoGN