collector.h 9.54 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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 31 32 33 34 35 36 37
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __COLLECTOR_H__
#define __COLLECTOR_H__

/*****************************************
 * Class hierarchy :
 * Collector (virtual)
 * - Collector_WithinSphere
 * - Collector_OneRing
 ****************************************/

namespace CGoGN
{

38 39 40
namespace Algo
{

Pierre Kraemer's avatar
Pierre Kraemer committed
41 42 43 44
namespace Selection
{

/*********************************************************
45
 * Generic Collector
Pierre Kraemer's avatar
Pierre Kraemer committed
46
 *********************************************************/
47

Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51 52 53 54 55
template <typename PFP>
class Collector
{
protected:
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	typename PFP::MAP& map;
Sylvain Thery's avatar
Sylvain Thery committed
56
	unsigned int m_thread;
Pierre Kraemer's avatar
Pierre Kraemer committed
57 58 59 60 61 62 63 64 65

	Dart centerDart;

	std::vector<Dart> insideVertices;
	std::vector<Dart> insideEdges;
	std::vector<Dart> insideFaces;
	std::vector<Dart> border;

public:
Sylvain Thery's avatar
Sylvain Thery committed
66
	Collector(typename PFP::MAP& m, unsigned int thread =0);
Pierre Kraemer's avatar
Pierre Kraemer committed
67

Pierre Kraemer's avatar
Pierre Kraemer committed
68 69 70 71 72 73 74 75
	inline void init(Dart d)
	{
		centerDart = d;
		insideVertices.clear();
		insideEdges.clear();
		insideFaces.clear();
		border.clear();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
76

Pierre Kraemer's avatar
Pierre Kraemer committed
77 78
	virtual void collectAll(Dart d) = 0;
	virtual void collectBorder(Dart d) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
79

Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82 83
	bool applyOnInsideVertices(FunctorType& f);
	bool applyOnInsideEdges(FunctorType& f);
	bool applyOnInsideFaces(FunctorType& f);
	bool applyOnBorder(FunctorType& f);
84

Pierre Kraemer's avatar
Pierre Kraemer committed
85
	inline void sort()
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87 88 89 90 91 92
	{
		std::sort(insideVertices.begin(), insideVertices.end());
		std::sort(insideEdges.begin(), insideEdges.end());
		std::sort(insideFaces.begin(), insideFaces.end());
		std::sort(border.begin(), border.end());
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
93
	inline typename PFP::MAP& getMap() { return map; }
Pierre Kraemer's avatar
Pierre Kraemer committed
94

Pierre Kraemer's avatar
Pierre Kraemer committed
95
	inline Dart getCenterDart() const { return centerDart; }
Pierre Kraemer's avatar
Pierre Kraemer committed
96

Pierre Kraemer's avatar
Pierre Kraemer committed
97 98 99 100
	inline const std::vector<Dart>& getInsideVertices() const { return insideVertices; }
	inline const std::vector<Dart>& getInsideEdges() const { return insideEdges; }
	inline const std::vector<Dart>& getInsideFaces() const { return insideFaces; }
	inline const std::vector<Dart>& getBorder() const { return border; }
Pierre Kraemer's avatar
Pierre Kraemer committed
101

Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104 105
	inline unsigned int getNbInsideVertices() const { return insideVertices.size(); }
	inline unsigned int getNbInsideEdges() const { return insideEdges.size(); }
	inline unsigned int getNbInsideFaces() const { return insideFaces.size(); }
	inline unsigned int getNbBorder() const { return border.size(); }
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109 110 111 112 113 114 115 116 117 118

	template <typename PPFP>
	friend std::ostream& operator<<(std::ostream &out, const Collector<PPFP>& c);
};

/*********************************************************
 * Collector One Ring
 *********************************************************/

/*
 * insideVertices = centerDart
 * insideEdges = star (edges incident to centerDart)
 * insideFaces = triangles incident to centerDart
119
 * border = vertices of 1-ring -> link (set of adjacent vertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122 123 124 125
 *        = edges of 1-ring
 */
template <typename PFP>
class Collector_OneRing : public Collector<PFP>
{
public:
Sylvain Thery's avatar
Sylvain Thery committed
126 127
	Collector_OneRing(typename PFP::MAP& m, unsigned int thread=0):
		Collector<PFP>(m, thread) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
128 129
	void collectAll(Dart d);
	void collectBorder(Dart d);
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
};

/*********************************************************
 * Collector Within Sphere
 *********************************************************/

/*
 * collect all primitives of the connected component containing "centerDart"
 * within the sphere of radius "radius" and center "position[centerDart]"
 * (hopefully) it defines a 2-manifold (if inserting border-vertices along the border-edges)
 */
template <typename PFP>
class Collector_WithinSphere : public Collector<PFP>
{
protected:
145
	const VertexAttribute<typename PFP::VEC3>& position;
Pierre Kraemer's avatar
Pierre Kraemer committed
146
	typename PFP::REAL radius;
Pierre Kraemer's avatar
Pierre Kraemer committed
147
	typename PFP::REAL area;
148

Pierre Kraemer's avatar
Pierre Kraemer committed
149
public:
150
	Collector_WithinSphere(typename PFP::MAP& m, const VertexAttribute<typename PFP::VEC3>& p, typename PFP::REAL r = 0, unsigned int thread=0) :
Sylvain Thery's avatar
Sylvain Thery committed
151
		Collector<PFP>(m, thread),
Pierre Kraemer's avatar
Pierre Kraemer committed
152 153 154
		position(p),
		radius(r),
		area(0)
Pierre Kraemer's avatar
Pierre Kraemer committed
155
	{}
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157
	inline void setRadius(typename PFP::REAL r) { radius = r; }
	inline typename PFP::REAL getRadius() const { return radius; }
158
	inline const VertexAttribute<typename PFP::VEC3>& getPosition() const { return position; }
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161 162

	void collectAll(Dart d);
	void collectBorder(Dart d);

Pierre Kraemer's avatar
Pierre Kraemer committed
163
	void computeArea();
Pierre Kraemer's avatar
Pierre Kraemer committed
164
	inline typename PFP::REAL getArea() const { return area; }
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166
};

167
/*********************************************************
168
 * Collector Normal Angle (Vertices)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
 *********************************************************/

/*
 * collect all primitives of the connected component containing "centerDart"
 * the angle between the included vertices normal vectors and the central normal vector
 * stays under a given threshold
 */
template <typename PFP>
class Collector_NormalAngle : public Collector<PFP>
{
protected:
	const VertexAttribute<typename PFP::VEC3>& normal ;
	typename PFP::REAL angleThreshold ;

public:
	Collector_NormalAngle(
		typename PFP::MAP& m,
		const VertexAttribute<typename PFP::VEC3>& n,
Sylvain Thery's avatar
Sylvain Thery committed
187 188
		typename PFP::REAL a,
		unsigned int thread=0
189
	) :	Collector<PFP>(m,thread), normal(n), angleThreshold(a)
190 191 192 193 194 195 196 197 198
	{}
	inline void setAngleThreshold(typename PFP::REAL a) { angleThreshold = a; }
	inline typename PFP::REAL getAngleThreshold() const { return angleThreshold; }
	inline const VertexAttribute<typename PFP::VEC3>& getNormal() const { return normal ; }

	void collectAll(Dart d) ;
	void collectBorder(Dart d) ;
};

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/*********************************************************
 * Collector Normal Angle (Triangles)
 *********************************************************/

/*
 * collect all primitives of the connected component containing "centerDart"
 * the angle between the included triangles normal vectors and the central normal vector
 * stays under a given threshold
 */
template <typename PFP>
class Collector_NormalAngle_Triangles : public Collector<PFP>
{
protected:
	const FaceAttribute<typename PFP::VEC3>& normal ;
	typename PFP::REAL angleThreshold ;

public:
	Collector_NormalAngle_Triangles(
		typename PFP::MAP& m,
		const FaceAttribute<typename PFP::VEC3>& n,
219 220 221
		typename PFP::REAL a,
		unsigned int thread=0
	) :	Collector<PFP>(m,thread), normal(n), angleThreshold(a)
222 223 224 225 226 227 228 229 230
	{}
	inline void setAngleThreshold(typename PFP::REAL a) { angleThreshold = a; }
	inline typename PFP::REAL getAngleThreshold() const { return angleThreshold; }
	inline const VertexAttribute<typename PFP::VEC3>& getNormal() const { return normal ; }

	void collectAll(Dart d) ;
	void collectBorder(Dart d) ;
};

Basile Sauvage's avatar
Basile Sauvage committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/*********************************************************
 * Collector Dijkstra
 *********************************************************/

/*
 * collect all primitives of the connected component containing "centerDart"
 * within a distance < maxDist (the shortest path follows edges)
 */
template <typename PFP>
class Collector_Dijkstra : public Collector<PFP>
{
protected:
	const VertexAttribute<typename PFP::VEC3>& position;
	typename PFP::REAL maxDist;

	typedef struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "DijkstraVertexInfo" ; }
	} DijkstraVertexInfo ;
	typedef NoMathIOAttribute<DijkstraVertexInfo> VertexInfo ;

	VertexAttribute<VertexInfo> vertexInfo ;

	std::multimap<float,Dart> front ;

public:
259 260
	Collector_Dijkstra(typename PFP::MAP& m, const VertexAttribute<typename PFP::VEC3>& p, typename PFP::REAL d = 0, unsigned int thread=0) :
		Collector<PFP>(m,thread),
Basile Sauvage's avatar
Basile Sauvage committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
		position(p),
		maxDist(d)
	{
		vertexInfo = m.template addAttribute<VertexInfo, VERTEX>("vertexInfo");
	}
	~Collector_Dijkstra(){
		this->map.removeAttribute(vertexInfo);
	}
	inline void init (Dart d) {Collector<PFP>::init(d); front.clear();}
	inline void setMaxDistance(typename PFP::REAL d) { maxDist = d; }
	inline typename PFP::REAL getMaxDist() const { return maxDist; }
	inline const VertexAttribute<typename PFP::VEC3>& getPosition() const { return position; }

	void collectAll(Dart d);
	void collectBorder(Dart d);
private :
	inline float edgeLength (Dart d);
278
//	inline Dart oppositeVertex (Dart d);
Basile Sauvage's avatar
Basile Sauvage committed
279
};
280 281


Pierre Kraemer's avatar
Pierre Kraemer committed
282 283
} // namespace Selection

284 285
} // namespace Algo

Pierre Kraemer's avatar
Pierre Kraemer committed
286 287 288 289 290
} // namespace CGoGN

#include "Algo/Selection/collector.hpp"

#endif