primitives3d.hpp 7 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

//TEMPORAIRE
template <typename PFP>
36
Dart Primitive3D<PFP>::createHexa()
Pierre Kraemer's avatar
Pierre Kraemer committed
37
{
Pierre Kraemer's avatar
Pierre Kraemer committed
38
	Dart base = m_map.newFace(4, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
39

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
	Dart side1 = m_map.newFace(4, false);
	m_map.sewFaces(base, side1, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
42

Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45
	Dart side2 = m_map.newFace(4, false);
	m_map.sewFaces(m_map.phi1(base), side2, false);
	m_map.sewFaces(m_map.phi_1(side1), m_map.phi1(side2), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
46

Pierre Kraemer's avatar
Pierre Kraemer committed
47 48 49
	Dart side3 = m_map.newFace(4, false);
	m_map.sewFaces(m_map.phi1(m_map.phi1(base)), side3, false);
	m_map.sewFaces(m_map.phi_1(side2), m_map.phi1(side3), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
50

Pierre Kraemer's avatar
Pierre Kraemer committed
51 52 53
	Dart side4 = m_map.newFace(4, false);
	m_map.sewFaces(m_map.phi_1(base), side4, false);
	m_map.sewFaces(m_map.phi_1(side3), m_map.phi1(side4), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
54

Pierre Kraemer's avatar
Pierre Kraemer committed
55
	m_map.sewFaces(m_map.phi_1(side4), m_map.phi1(side1), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
56

Pierre Kraemer's avatar
Pierre Kraemer committed
57 58 59 60 61
	Dart top = m_map.newFace(4, false);
	m_map.sewFaces(top, m_map.phi1(m_map.phi1(side1)), false);
	m_map.sewFaces(m_map.phi_1(top), m_map.phi1(m_map.phi1(side2)), false);
	m_map.sewFaces(m_map.phi1(m_map.phi1(top)), m_map.phi1(m_map.phi1(side3)), false);
	m_map.sewFaces(m_map.phi1(top), m_map.phi1(m_map.phi1(side4)), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63 64 65 66

	return base;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
67
Dart Primitive3D<PFP>::HexaGrid1Topo(unsigned int nx)
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69
{
	// first cube
70
	Dart d0 = createHexa();
Pierre Kraemer's avatar
Pierre Kraemer committed
71 72 73
	m_tableVertDarts.push_back(d0);
	Dart d1 = m_map.template phi<2112>(d0);

Pierre Kraemer's avatar
Pierre Kraemer committed
74
	for (unsigned int i = 1; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
75
	{
76
		Dart d2 = createHexa();
Pierre Kraemer's avatar
Pierre Kraemer committed
77
		m_tableVertDarts.push_back(d2);
78
		m_map.sewVolumes(d1, d2, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
79 80 81 82
		d1 = m_map.template phi<2112>(d2);
	}

	// add last vertex (x=nx)
untereiner's avatar
untereiner committed
83
	d1 = m_map.phi2(d1); //TODO can take phi1 instead > same vertex ??
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85 86 87 88 89
	m_tableVertDarts.push_back(d1);

	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
90
Dart Primitive3D<PFP>::HexaGrid2Topo(unsigned int nx, unsigned int ny)
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93 94 95
{
	// creation premiere ligne
	Dart d0 = HexaGrid1Topo(nx);
	Dart d1 = m_map.template phi<112>(d0);

Pierre Kraemer's avatar
Pierre Kraemer committed
96
	for (unsigned int i = 1; i < ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
97 98 99
	{
		// creation ligne suivante
		Dart d2 = HexaGrid1Topo(nx);
Pierre Kraemer's avatar
Pierre Kraemer committed
100
		Dart d3 = m_map.phi2(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
101 102

		// couture des deux lignes de cubes
Pierre Kraemer's avatar
Pierre Kraemer committed
103
		for (unsigned int i = 0; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
104
		{
105
			m_map.sewVolumes(d1, d3, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109 110 111 112 113 114 115
			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
116
	for (unsigned int i = 0; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
117 118
	{
		Dart dd = m_tableVertDarts[index++];
119
		dd = m_map.template phi<112>(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122 123
		m_tableVertDarts.push_back(dd);	
	}
	// warning last vertex of row has not same dart
	Dart dd = m_tableVertDarts[index++];
124
	dd = m_map.template phi<211>(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127 128 129 130
	m_tableVertDarts.push_back(dd);	

	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
131
Dart Primitive3D<PFP>::hexaGrid_topo(unsigned int nx, unsigned int ny, unsigned int nz)
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134 135 136 137 138 139
{
	m_kind = HEXAGRID;
	m_nx = nx;
	m_ny = ny;
	m_nz = nz;
	m_tableVertDarts.clear();
	m_tableVertDarts.reserve((nx+1)*(ny+1)*(nz+1));

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

Pierre Kraemer's avatar
Pierre Kraemer committed
143
	for (unsigned int i = 1; i < nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145
	{
		// creation grille suivante
146
		Dart d2 = HexaGrid2Topo(nx, ny);
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148 149
		Dart d3 = m_map.phi2(m_map.phi_1(d2));
		
		// couture des deux grilles 2D de cubes
Pierre Kraemer's avatar
Pierre Kraemer committed
150
		for (unsigned int j = 0; j < ny; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
151 152 153
		{
			Dart da = d1;
			Dart db = d3;
Pierre Kraemer's avatar
Pierre Kraemer committed
154
			for (unsigned int k = 0; k < nx; ++k)
Pierre Kraemer's avatar
Pierre Kraemer committed
155
			{
156
				m_map.sewVolumes(da, db, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159 160 161 162 163 164 165 166 167 168 169
				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
170 171 172
	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
173 174
	{
		Dart dd = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
175
		dd = m_map.phi2(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
176 177 178
		m_tableVertDarts.push_back(dd);
	}

179 180
	m_map.closeMap() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183 184
	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
185
void Primitive3D<PFP>::embedHexaGrid(float x, float y, float z)
Pierre Kraemer's avatar
Pierre Kraemer committed
186 187 188
{
	if (m_kind != HEXAGRID)
	{
189
		CGoGNerr << "Warning try to embedHexaGrid something that is not a grid of hexahedron"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194 195 196
		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
197
	unsigned int nbs = (m_nx+1)*(m_ny+1);
Pierre Kraemer's avatar
Pierre Kraemer committed
198

Pierre Kraemer's avatar
Pierre Kraemer committed
199
	for(unsigned int i = 0; i <= m_nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
200
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
201
		for(unsigned int j = 0; j <= m_ny; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
202
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
203
			for(unsigned int k = 0; k <= m_nx; ++k)
Pierre Kraemer's avatar
Pierre Kraemer committed
204 205 206 207
			{
				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 ];

Pierre Kraemer's avatar
Pierre Kraemer committed
208
				m_map.embedNewCell(VERTEX, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
209 210 211 212 213 214 215 216 217
				m_positions[d] = pos;
			}
		}
	}
}

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

}

Pierre Kraemer's avatar
Pierre Kraemer committed
226 227 228 229 230 231 232 233
//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
234

Pierre Kraemer's avatar
Pierre Kraemer committed
235 236 237 238 239
} // namespace Modelisation

} // namespace Algo

} // namespace CGoGN