primitives3d.hpp 6.05 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 33 34 35 36
namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
37
Dart Primitive3D<PFP>::HexaGrid1Topo(unsigned int nx)
Pierre Kraemer's avatar
Pierre Kraemer committed
38 39
{
	// first cube
40
	Dart d0 = createHexahedron<PFP>(m_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
41
	m_tableVertDarts.push_back(d0);
untereiner's avatar
untereiner committed
42

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

Pierre Kraemer's avatar
Pierre Kraemer committed
45
	for (unsigned int i = 1; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
46
	{
47
		Dart d2 = createHexahedron<PFP>(m_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
48
		m_tableVertDarts.push_back(d2);
49
		m_map.sewVolumes(d1, d2, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
50 51 52 53
		d1 = m_map.template phi<2112>(d2);
	}

	// add last vertex (x=nx)
untereiner's avatar
untereiner committed
54
	d1 = m_map.phi2(d1); //TODO can take phi1 instead > same vertex ??
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56 57 58 59 60
	m_tableVertDarts.push_back(d1);

	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
61
Dart Primitive3D<PFP>::HexaGrid2Topo(unsigned int nx, unsigned int ny)
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63 64 65 66
{
	// creation premiere ligne
	Dart d0 = HexaGrid1Topo(nx);
	Dart d1 = m_map.template phi<112>(d0);

Pierre Kraemer's avatar
Pierre Kraemer committed
67
	for (unsigned int i = 1; i < ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69 70
	{
		// creation ligne suivante
		Dart d2 = HexaGrid1Topo(nx);
Pierre Kraemer's avatar
Pierre Kraemer committed
71
		Dart d3 = m_map.phi2(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
72 73

		// couture des deux lignes de cubes
Pierre Kraemer's avatar
Pierre Kraemer committed
74
		for (unsigned int i = 0; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
75
		{
76
			m_map.sewVolumes(d1, d3, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79 80 81 82 83 84 85 86
			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
87
	for (unsigned int i = 0; i < nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89
	{
		Dart dd = m_tableVertDarts[index++];
90
		dd = m_map.template phi<112>(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93 94
		m_tableVertDarts.push_back(dd);	
	}
	// warning last vertex of row has not same dart
	Dart dd = m_tableVertDarts[index++];
95
	dd = m_map.template phi<211>(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98 99 100 101
	m_tableVertDarts.push_back(dd);	

	return d0;
}

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
114
	for (unsigned int i = 1; i < nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116
	{
		// creation grille suivante
117
		Dart d2 = HexaGrid2Topo(nx, ny);
Pierre Kraemer's avatar
Pierre Kraemer committed
118 119 120
		Dart d3 = m_map.phi2(m_map.phi_1(d2));
		
		// couture des deux grilles 2D de cubes
Pierre Kraemer's avatar
Pierre Kraemer committed
121
		for (unsigned int j = 0; j < ny; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
122 123 124
		{
			Dart da = d1;
			Dart db = d3;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
			for (unsigned int k = 0; k < nx; ++k)
Pierre Kraemer's avatar
Pierre Kraemer committed
126
			{
127
				m_map.sewVolumes(da, db, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
128 129 130 131 132 133 134 135 136 137 138 139 140
				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
141 142 143
	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
144 145
	{
		Dart dd = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
146
		dd = m_map.phi2(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148 149
		m_tableVertDarts.push_back(dd);
	}

150 151
	m_map.closeMap() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
152 153 154 155
	return d0;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
156
void Primitive3D<PFP>::embedHexaGrid(float x, float y, float z)
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159
{
	if (m_kind != HEXAGRID)
	{
160
		CGoGNerr << "Warning try to embedHexaGrid something that is not a grid of hexahedron"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
161 162 163 164 165 166 167
		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
168
	unsigned int nbs = (m_nx+1)*(m_ny+1);
Pierre Kraemer's avatar
Pierre Kraemer committed
169

Pierre Kraemer's avatar
Pierre Kraemer committed
170
	for(unsigned int i = 0; i <= m_nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
171
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
172
		for(unsigned int j = 0; j <= m_ny; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
173
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
174
			for(unsigned int k = 0; k <= m_nx; ++k)
Pierre Kraemer's avatar
Pierre Kraemer committed
175 176 177 178
			{
				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 ];

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

template <typename PFP>
void Primitive3D<PFP>::transform(const Geom::Matrix44f& matrice)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
189
	for(typename std::vector<Dart>::iterator di = m_tableVertDarts.begin(); di != m_tableVertDarts.end(); ++di)
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194 195 196
	{
		typename PFP::VEC3& pos = m_positions[*di];
		pos = Geom::transform(pos, matrice);
	}

}

Pierre Kraemer's avatar
Pierre Kraemer committed
197 198 199 200 201 202 203 204
//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
205

Pierre Kraemer's avatar
Pierre Kraemer committed
206 207 208 209 210
} // namespace Modelisation

} // namespace Algo

} // namespace CGoGN