polyhedron.h 9.64 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 25 26 27 28 29 30
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef POLYHEDRON_H
#define POLYHEDRON_H

#include <vector>
#include "Algo/Modelisation/subdivision.h"
#include "Geometry/transfo.h"
Sylvain Thery's avatar
Sylvain Thery committed
31 32
#include "Topology/generic/cellmarker.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35 36 37 38 39 40 41 42 43 44
#include "Utils/os_spec.h"


namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

45
enum { NONE, GRID, CUBE, CYLINDER, CONE, SPHERE, TORE, COMPOSED };
Pierre Kraemer's avatar
Pierre Kraemer committed
46

Sylvain Thery's avatar
Sylvain Thery committed
47 48 49 50 51 52
//template <typename PFP>
//void sewFaceEmb(typename PFP::MAP& map, Dart d, Dart e);
//
//template <typename PFP>
//Dart newFaceEmb(typename PFP::MAP& map, unsigned int n);

Pierre Kraemer's avatar
Pierre Kraemer committed
53
/**
Sylvain Thery's avatar
Sylvain Thery committed
54
* sudivide the all quads of a CC into 2 triangles
Pierre Kraemer's avatar
Pierre Kraemer committed
55
*/
Sylvain Thery's avatar
Sylvain Thery committed
56 57
 template <typename PFP>
 void quads2TrianglesCC(typename PFP::MAP& the_map, Dart primd);
Pierre Kraemer's avatar
Pierre Kraemer committed
58 59 60 61 62 63 64 65 66 67

/**
* Create a triangle fans (to close cylinders)
* simple topo creation  no modification of Polyhedron
* @param n nb of triangles in the fan in n
* @return the dart
*/
// template <typename PFP>
// Dart triangleFan_topo(typename PFP::MAP& the_map, int n);

untereiner's avatar
untereiner committed
68 69

/**
70
 * Unsex the Umbrella around a vertex, close the hole and then
untereiner's avatar
untereiner committed
71 72 73 74
 * create a symetric to construct a polyedron
 * @param d a dart from the vertex
 */
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
75
void explodPolyhedron(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3>& position);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116




/**
 * create a n-sided pyramid
 */
template <typename PFP>
Dart createPyramid(typename PFP::MAP& map, unsigned int nbSides);

/**
 * create a n-sided prism
 */
template <typename PFP>
Dart createPrism(typename PFP::MAP& map, unsigned int nbSides);

/**
 * create a n-sided diamond
 */
template <typename PFP>
Dart createDiamond(typename PFP::MAP& map, unsigned int nbSides);

/**
 * create a tetrahedron
 */
template <typename PFP>
Dart createTetrahedron(typename PFP::MAP& map);

/**
 * create a hexahedron
 */
template <typename PFP>
Dart createHexahedron(typename PFP::MAP& map);

/**
 * create a 3-sided prism
 */
template <typename PFP>
Dart createTriangularPrism(typename PFP::MAP& map);

/**
117
 * create a 4-sided pyramid
118 119 120 121 122 123 124 125 126 127 128 129
 */
template <typename PFP>
Dart createQuadrangularPyramid(typename PFP::MAP& map);

/**
 * create 4-sided diamond (i.e. an octahedron)
 */
template <typename PFP>
Dart createOctahedron(typename PFP::MAP& map);



Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/**
* class of geometric Polyhedron
* It alloaw the creation of:
* - grid 2D
* - subdivided cube
* - subdivided cone
* - subdivided cylinder
* - subdivided tore
* - subdivided sphere (with pole)
*
* Topological creation methods are separated from embedding to
* easily allow specific embedding.
*/
template <typename PFP>
class Polyhedron
{
	typedef typename PFP::MAP MAP;
147
	typedef typename PFP::VEC3 VEC3;
Pierre Kraemer's avatar
Pierre Kraemer committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

public:
	enum {NONE,GRID, CUBE, CYLINDER, CONE, SPHERE, TORE, COMPOSED};

protected:
	/**
	* Map in which we are working
	*/
	MAP& m_map;

	/**
	* Reference dart of Polyhedron
	*/
	Dart m_dart;

	/**
	* Kind of Polyhedron (grid, cylinder,
	*/
	int m_kind;

	/**
	* Table of vertex darts (one dart per vertex)
	* Order depend on Polyhedron kind
	*/
	std::vector<Dart> m_tableVertDarts;

	/**
	* numbers that defined the subdivision of  Polyhedron
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
177
	unsigned int m_nx;
Pierre Kraemer's avatar
Pierre Kraemer committed
178

Pierre Kraemer's avatar
Pierre Kraemer committed
179
	unsigned int m_ny;
Pierre Kraemer's avatar
Pierre Kraemer committed
180

Pierre Kraemer's avatar
Pierre Kraemer committed
181
	unsigned int m_nz;
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183 184 185 186

	bool m_top_closed;

	bool m_bottom_closed;

187
	VertexAttribute<VEC3>& m_positions;
Pierre Kraemer's avatar
Pierre Kraemer committed
188

189
	VEC3 m_center;
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

	/**
	* return the dart of next vertex when traversing the boundary of a quad or trifan grid
	*/
	Dart nextDV(Dart d) { return m_map.phi1(m_map.phi2(m_map.phi1(d))); }

	/**
	* return the dart of preceding vertex when traversing the boundary of a quad or trifan grid
	*/
	Dart precDV(Dart d) { return m_map.phi_1(m_map.phi2(m_map.phi_1(d))); }

	void computeCenter();


public:
	/**
	* Constructor
	* @param map the map in which we want to work
	* @param idPositions id of attribute position
	*/
210
	Polyhedron(MAP& map, VertexAttribute<VEC3>& position):
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213 214 215 216
		m_map(map),
		m_kind(NONE),
		m_nx(-1), m_ny(-1), m_nz(-1),
		m_top_closed(false), m_bottom_closed(false),
		m_positions(position)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
217
		computeCenter();
Pierre Kraemer's avatar
Pierre Kraemer committed
218
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221 222 223 224 225 226 227 228 229

	/**
	* Polyhedron fusion: give a COMPOSED type
	* @param p1 first Polyhedron
	* @param p1 second Polyhedron
	*/
	Polyhedron(const Polyhedron<PFP>& p1, const Polyhedron<PFP>& p2);

	/*
	* get the reference dart
	*/
230
	Dart getDart() { return m_dart; }
Pierre Kraemer's avatar
Pierre Kraemer committed
231 232 233 234

	/*
	* get the center of Polyhedron
	*/
235
	const typename PFP::VEC3&  getCenter() { return m_center; }
Pierre Kraemer's avatar
Pierre Kraemer committed
236 237 238 239

	/**
	* get the table of darts (one per vertex)
	*/
240
	std::vector<Dart>& getVertexDarts() { return m_tableVertDarts; }
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245 246 247 248 249

	/**
	* Create a 2D grid
	* quads are oriented counter-clockwise and the returned dart
	* is the lower left dart (upper right it is symetric)
	* @param x nb of quads in x
	* @param y nb of quads in y
	* @return the dart
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
250
	Dart grid_topo(unsigned int x, unsigned int y);
Pierre Kraemer's avatar
Pierre Kraemer committed
251 252 253 254 255 256 257 258 259

	/**
	* Create a subdivided (surface) cylinder
	* @param n nb of quads around circunference
	* @param z nb of quads in height
	* @param top_closed close the top with triangles fan
	* @param bottom_closed close the bottom with triangles fan
	* @return the dart
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
260
	Dart cylinder_topo(unsigned int n, unsigned int z, bool top_closed, bool bottom_closed);
Pierre Kraemer's avatar
Pierre Kraemer committed
261 262 263 264 265 266 267 268

	/**
	* Create a subdivided (surface) cone (with param 1,3,true create tetrahedron)
	* @param n nb of quads around circunference (must be >=3)
	* @param z nb of quads in height (must be >=1)
	* @param bottom_closed close the bottom with triangles fan
	* @return the dart
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
269
	Dart cone_topo(unsigned int n, unsigned int z, bool bottom_closed);
Pierre Kraemer's avatar
Pierre Kraemer committed
270 271 272 273 274 275 276 277 278

	/**
	* Create a subdived (surface) cube
	* quads are oriented counter-clockwise
	* @param x nb of quads in x
	* @param y nb of quads in y
	* @param z nb of quads in z
	* @return the dart
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
279
	Dart cube_topo(unsigned int x, unsigned int y, unsigned int z);
Pierre Kraemer's avatar
Pierre Kraemer committed
280 281 282 283

	/**
	* Create a subdivided (surface) cylinder
	* @param m nb of quads around big circunference
284
	* @param n nb of quads around small circunference
Pierre Kraemer's avatar
Pierre Kraemer committed
285 286 287 288
	* @param top_closed close the top with triangles fan
	* @param bottom_closed close the bottom with triangles fan
	* @return the dart
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
289
	Dart tore_topo(unsigned int m, unsigned int n);
Pierre Kraemer's avatar
Pierre Kraemer committed
290 291 292 293 294 295 296

	/**
	* embed the topo grid Polyhedron
	* Grid has size x,y centered on 0
	* @param x
	* @param y
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
297
	void embedGrid(float x, float y, float z = 0.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

	/**
	* embed the topo cylinder Polyhedron
	* @param bottom_radius
	* @parma top_radius
	* @param height
	*/
	void embedCylinder(float bottom_radius, float top_radius, float height);

	/**
	* embed the topo cylinder Polyhedron
	* @param radius
	* @param height
	*/
	void embedCone(float radius, float height);

	/**
	* embed the topo sphere Polyhedron
	* @param radius
	* @param height
	*/
	void embedSphere(float radius);

	/**
	* embed the topo tore Polyhedron
	* @param big_radius
	* @param small_radius
	*/
	void embedTore(float big_radius, float small_radius);

	/**
	* embed the topo cube Polyhedron
	* @param sx size of cube in X
	* @param sy size of cube in Y
	* @param sz size of cube in Z
	*/
	void embedCube(float sx, float sy, float sz);

	/**
	* embed the a grid into a twister open ribbon
	* with turns=PI it is a Moebius strip, needs only to be closed (if model allow it)
	* @param radius_min
	* @param radius_max
	* @param turns number of turn multiplied by 2*PI
	*/
	void embedTwistedStrip(float radius_min, float radius_max, float turns);

	/**
	* embed a grid into a helicoid
	* @param radius_min
	* @param radius_max
	* @param maxHeight height to reach
	* @param turns number of turn
	*/
352
	void embedHelicoid(float radius_min,  float radius_max, float maxHeight, float nbTurn, int orient = 1);
Pierre Kraemer's avatar
Pierre Kraemer committed
353 354 355 356 357 358 359 360 361

	/**
	* transform the Polyhedron with transformation matrice
	* @param matrice
	*/
	void transform(const Geom::Matrix44f& matrice);

	/**
	* mark all darts of the Polyhedron
Sylvain Thery's avatar
Sylvain Thery committed
362
	* @param m the CellMarker(VERTEX) to use
Pierre Kraemer's avatar
Pierre Kraemer committed
363
	*/
364
	void mark(CellMarker<VERTEX>& m);
Pierre Kraemer's avatar
Pierre Kraemer committed
365 366 367 368 369

	/**
	* mark all embedded vertices of the Polyhedron
	* @param m the marker to use
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
370
//	void markEmbVertices(Mark m);
Pierre Kraemer's avatar
Pierre Kraemer committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

	/**
	* test if a vertex is in the Polyhedron
	* @param d a dart of the vertex to test
	*/
	bool containVertex(Dart d);
};

} // namespace Modelisation

} // namespace Algo

} // namespace CGoGN

#include "Algo/Modelisation/polyhedron.hpp"

#endif