polyhedron.h 9.13 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
* 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
{

Pierre Kraemer's avatar
Pierre Kraemer committed
45
enum { NONE,GRID, CUBE, CYLINDER, CONE, SPHERE, TORE, COMPOSED };
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

/**
* sudivide the all quads of primtive into 2 triangles
* the vertex darts table remain valid (adding only darts, no remove)
*/
// template <typename PFP>
// void onlyTriangles(typename PFP::MAP& the_map, Dart primd);

/**
* 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
63 64 65 66 67 68 69 70 71 72


/**
 * Unsex the Umbrella aroud a vertex, close the hole and then
 * create a symetric to construct a polyedron
 * @param d a dart from the vertex
 */
template <typename PFP>
void explodPolyhedron(typename PFP::MAP& map, Dart d, typename PFP::TVEC3 position);

Pierre Kraemer's avatar
Pierre Kraemer committed
73 74 75 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/**
* 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;

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
	*/
	int m_nx;

	int m_ny;

	int m_nz;

	bool m_top_closed;

	bool m_bottom_closed;

	typename PFP::TVEC3& m_positions;

	typename PFP::VEC3 m_center;

	/**
	* 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
	*/
	Polyhedron(MAP& map, typename PFP::TVEC3& position):
	 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)
	 {
		computeCenter();
	 }

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

	/**
171
	 * create simple simple polyhedron (not handled by Polyhedron object)
Pierre Kraemer's avatar
Pierre Kraemer committed
172
	 */
173
	static Dart createPolyhedron(typename PFP::MAP& the_map, int nbFaces);
Pierre Kraemer's avatar
Pierre Kraemer committed
174

175 176 177
	/**
	 * create simple simple tetrahedron (not handled by Polyhedron object)
	 */
178
	static Dart createTetra(typename PFP::MAP& the_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
179

180 181 182
	/**
	 * create simple simple pyramid (not handled by Polyhedron object)
	 */
183
	static Dart createPyra(typename PFP::MAP& the_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
184

185 186 187
	/**
	 * create simple simple hexaedron (not handled by Polyhedron object)
	 */
188
	static Dart createHexa(typename PFP::MAP& the_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
189

190 191 192
	/**
	 * create simple simple prism (not handled by Polyhedron object)
	 */
193
	static Dart createPrism(typename PFP::MAP& the_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

	/*
	* get the reference dart
	*/
	Dart getDart() { return m_dart;}

	/*
	* get the center of Polyhedron
	*/
	const typename PFP::VEC3&  getCenter() { return m_center;}

	/**
	* get the table of darts (one per vertex)
	*/
	std::vector<Dart>& getVertexDarts() { return m_tableVertDarts;}

	/**
	* 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
218
	Dart grid_topo(unsigned int x, unsigned int y);
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

	/**
	* 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
	*/
	Dart cylinder_topo(int n, int z, bool top_closed, bool bottom_closed);

	/**
	* 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
	*/
	Dart cone_topo(int n, int z, bool bottom_closed);

	/**
	* 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
	*/
	Dart cube_topo(int x, int y, int z);

	/**
	* Create a subdivided (surface) cylinder
	* @param m nb of quads around big circunference
252
	* @param n nb of quads around small circunference
Pierre Kraemer's avatar
Pierre Kraemer committed
253 254 255 256
	* @param top_closed close the top with triangles fan
	* @param bottom_closed close the bottom with triangles fan
	* @return the dart
	*/
257
	Dart tore_topo(int m, int n);
Pierre Kraemer's avatar
Pierre Kraemer committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

	/**
	* embed the topo grid Polyhedron
	* Grid has size x,y centered on 0
	* @param x
	* @param y
	*/
	void embedGrid(float x, float y, float z=0.0f);

	/**
	* 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
	*/
Thomas's avatar
Thomas committed
320
	void embedHelicoid( float radius_min,  float radius_max, float maxHeight, float nbTurn, int orient=1);
Pierre Kraemer's avatar
Pierre Kraemer committed
321 322 323 324 325 326 327 328 329

	/**
	* 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
330
	* @param m the CellMarker(VERTEX) to use
Pierre Kraemer's avatar
Pierre Kraemer committed
331
	*/
Sylvain Thery's avatar
Sylvain Thery committed
332
	void mark(CellMarker& m);
Pierre Kraemer's avatar
Pierre Kraemer committed
333 334 335 336 337

	/**
	* mark all embedded vertices of the Polyhedron
	* @param m the marker to use
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
338
//	void markEmbVertices(Mark m);
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

	/**
	* 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