functor.h 8.62 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __FUNCTOR_H__
#define __FUNCTOR_H__

28
#include "Topology/generic/dart.h"
29
//#include "Topology/generic/marker.h"
30

31 32
//#include "Container/attributeMultiVector.h"
//#include <vector>
Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35 36 37 38

namespace CGoGN
{

// Base Class for Functors: object function that is applied to darts
/********************************************************/
39

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41 42 43 44 45 46 47
class FunctorType
{
public:
	FunctorType() {}
	virtual ~FunctorType() {}
	virtual bool operator()(Dart d) = 0;
};

Sylvain Thery's avatar
Sylvain Thery committed
48

Sylvain Thery's avatar
Sylvain Thery committed
49 50 51 52 53 54 55 56 57 58 59 60 61
template <typename MAP>
class FunctorMap : public virtual FunctorType
{
protected:
	MAP& m_map ;
public:
	FunctorMap(MAP& m): m_map(m) {}
};





Pierre Kraemer's avatar
Pierre Kraemer committed
62 63
// Selector functors : return true to select or false to not select a dart
/********************************************************/
64

Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68 69 70
class FunctorSelect
{
public:
	FunctorSelect() {}
	virtual ~FunctorSelect() {}
	virtual bool operator()(Dart d) const = 0 ;
71
	virtual FunctorSelect* copy() const = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
72 73 74 75 76
};

class SelectorTrue : public FunctorSelect
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
77
	bool operator()(Dart) const { return true; }
78 79
	FunctorSelect* copy() const { return new SelectorTrue();}

Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82 83 84
};

class SelectorFalse : public FunctorSelect
{
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
85
	bool operator()(Dart) const { return false; }
86
	FunctorSelect* copy() const { return new SelectorFalse();}
Pierre Kraemer's avatar
Pierre Kraemer committed
87 88
};

Pierre Kraemer's avatar
Pierre Kraemer committed
89
const SelectorTrue allDarts = SelectorTrue() ;
Sylvain Thery's avatar
Sylvain Thery committed
90

91 92 93 94 95 96 97 98 99 100 101 102

class SelectorAnd : public FunctorSelect
{
protected:
	const FunctorSelect* m_sel1;
	const FunctorSelect* m_sel2;

public:
	SelectorAnd(const FunctorSelect& fs1, const FunctorSelect& fs2) { m_sel1 = fs1.copy(); m_sel2 = fs2.copy();}
	bool operator()(Dart d) const { return m_sel1->operator()(d) && m_sel2->operator()(d); }
	~SelectorAnd() { delete m_sel1; delete m_sel2;}
	FunctorSelect* copy() const  {  return new SelectorAnd(*m_sel1,*m_sel2);}
Pierre Kraemer's avatar
Pierre Kraemer committed
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
class SelectorOr : public FunctorSelect
{
protected:
	const FunctorSelect* m_sel1;
	const FunctorSelect* m_sel2;

public:
	SelectorOr(const FunctorSelect& fs1, const FunctorSelect& fs2) { m_sel1 = fs1.copy(); m_sel2 = fs2.copy();}
	bool operator()(Dart d) const { return m_sel1->operator()(d) || m_sel2->operator()(d); }
	~SelectorOr() { delete m_sel1; delete m_sel2;}
	FunctorSelect* copy() const  { return new SelectorOr(*m_sel1,*m_sel2);}
};

inline SelectorAnd operator&&(const FunctorSelect& fs1, const FunctorSelect& fs2)
{
	return SelectorAnd(fs1,fs2);
}

inline SelectorOr operator||(const FunctorSelect& fs1, const FunctorSelect& fs2)
{
	return SelectorOr(fs1,fs2);
}

128 129 130 131 132
template <typename MAP>
class SelectorVertexBoundary : public FunctorSelect
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
133
	const MAP& m_map;
134
public:
Sylvain Thery's avatar
Sylvain Thery committed
135
	SelectorVertexBoundary(const MAP& m): m_map(m) {}
136 137 138 139 140 141 142 143 144
	bool operator()(Dart d) const { return m_map.isBoundaryVertex(d); }
	FunctorSelect* copy() const { return new SelectorVertexBoundary(m_map);}
};

template <typename MAP>
class SelectorVertexNoBoundary : public FunctorSelect
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
145
	const MAP& m_map;
146
public:
Sylvain Thery's avatar
Sylvain Thery committed
147
	SelectorVertexNoBoundary(const MAP& m): m_map(m) {}
148 149 150
	bool operator()(Dart d) const { return !m_map.isBoundaryVertex(d); }
	FunctorSelect* copy() const { return new SelectorVertexNoBoundary(m_map);}
};
151

Sylvain Thery's avatar
Sylvain Thery committed
152
template <typename MAP>
153
class SelectorEdgeBoundary : public FunctorSelect
Sylvain Thery's avatar
Sylvain Thery committed
154 155 156
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
157
	const MAP& m_map;
Sylvain Thery's avatar
Sylvain Thery committed
158
public:
Sylvain Thery's avatar
Sylvain Thery committed
159
	SelectorEdgeBoundary(const MAP& m): m_map(m) {}
160
	bool operator()(Dart d) const { return m_map.isBoundaryEdge(d); }
161
	FunctorSelect* copy() const { return new SelectorEdgeBoundary(m_map);}
Sylvain Thery's avatar
Sylvain Thery committed
162 163
};

164 165 166 167 168 169

template <typename MAP>
class SelectorEdgeNoBoundary : public FunctorSelect
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
170
	const MAP& m_map;
171
public:
Sylvain Thery's avatar
Sylvain Thery committed
172
	SelectorEdgeNoBoundary(const MAP& m): m_map(m) {}
173
	bool operator()(Dart d) const { return !m_map.isBoundaryEdge(d); }
174
	FunctorSelect* copy() const { return new SelectorEdgeNoBoundary(m_map);}
175 176
};

Thery Sylvain's avatar
Thery Sylvain committed
177 178 179
/**
 * Selector for darts of boundary (of current dimension)
 */
180 181 182 183 184
template <typename MAP>
class SelectorDartBoundary : public FunctorSelect
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
185
	const MAP& m_map;
186
public:
Sylvain Thery's avatar
Sylvain Thery committed
187
	SelectorDartBoundary(const MAP& m): m_map(m) {}
188
	bool operator()(Dart d) const { return m_map.template isBoundaryMarked<MAP::DIMENSION>(d); }
189 190 191
	FunctorSelect* copy() const { return new SelectorDartBoundary(m_map);}
};

Thery Sylvain's avatar
Thery Sylvain committed
192 193 194
/**
 * Selector for darts not of boundary (of current dimension)
 */
195 196 197 198 199 200

template <typename MAP>
class SelectorDartNoBoundary : public FunctorSelect
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
201
	const MAP& m_map;
202
public:
Sylvain Thery's avatar
Sylvain Thery committed
203
	SelectorDartNoBoundary(const MAP& m): m_map(m) {}
204
	bool operator()(Dart d) const { return !m_map.template isBoundaryMarked<MAP::DIMENSION>(d); }
205 206 207
	FunctorSelect* copy() const { return new SelectorDartNoBoundary(m_map);}
};

208 209 210 211 212
template <typename MAP>
class SelectorLevel : public FunctorSelect
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
213
	const MAP& m_map;
214 215
	unsigned int m_level;
public:
Sylvain Thery's avatar
Sylvain Thery committed
216
	SelectorLevel(const MAP& m, unsigned int l): m_map(m), m_level(l) {}
217 218 219 220 221 222 223 224 225
	bool operator()(Dart d) const { return m_map.getDartLevel(d) == m_level; }
	FunctorSelect* copy() const { return new SelectorLevel(m_map, m_level);}
};

template <typename MAP>
class SelectorEdgeLevel : public FunctorSelect
{
public:
protected:
Sylvain Thery's avatar
Sylvain Thery committed
226
	const MAP& m_map;
227 228
	unsigned int m_level;
public:
Sylvain Thery's avatar
Sylvain Thery committed
229
	SelectorEdgeLevel(const MAP& m, unsigned int l): m_map(m), m_level(l) {}
230 231 232 233 234
	bool operator()(Dart d) const { return (m_map.getDartLevel(d) == m_level) && (m_map.getDartLevel(m_map.phi2(d)) == m_level); }
	FunctorSelect* copy() const { return new SelectorEdgeLevel(m_map, m_level);}
};


235

Pierre Kraemer's avatar
Pierre Kraemer committed
236
// Multiple Functor: to apply several Functors in turn to a dart
Pierre Kraemer's avatar
Pierre Kraemer committed
237 238
/********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
239
class FunctorDoubleFunctor : public FunctorType
Pierre Kraemer's avatar
Pierre Kraemer committed
240 241
{
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
242 243
	FunctorType& m_fonct1;
	FunctorType& m_fonct2;
Pierre Kraemer's avatar
Pierre Kraemer committed
244
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
245 246 247 248 249
	FunctorDoubleFunctor(FunctorType& f1, FunctorType& f2) : m_fonct1(f1), m_fonct2(f2) {}
	bool operator()(Dart d)
	{
		if (m_fonct1(d)) return true;
		return m_fonct2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
250 251 252
	}
};

253

254 255 256 257 258 259 260

//
// FOR PARALLEL TRAVERSALS
//

/**
 * Functor class for parallel::foreach_orbit/cell/dart
Pierre Kraemer's avatar
Pierre Kraemer committed
261
 * Overload run
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
 * Overload duplicate if necessary (no sharing of functors)
 */
template<typename MAP>
class FunctorMapThreaded
{
protected:
	MAP& m_map ;

public:
	FunctorMapThreaded(MAP& m): m_map(m) {}

	virtual ~FunctorMapThreaded() {}

	/**
	 * @return a pointer on a copy of the object.
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
278
	virtual FunctorMapThreaded<MAP>* duplicate() const { return NULL; }
279 280 281 282 283 284

	/**
	 * insert your code here:
	 * @param d the dart on which apply functor
	 * @param threadID the id of thread currently running your code
	 */
285
	virtual void run(Dart d, unsigned int threadID) = 0;
286 287 288 289
};

/**
 * Functor class for parallel::foreach_attrib
290
 * Overload run
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
 * Overload duplicate if necessary (no sharing of functors)
 */
class FunctorAttribThreaded
{
public:
	virtual ~FunctorAttribThreaded() {}

	/**
	 * @return a pointer on a copy of the object.
	 */
	virtual FunctorAttribThreaded* duplicate() const { return NULL;}

	/**
	 * insert your code here:
	 * @param d the dart on which apply functor
	 * @param threadID the id of thread currently running your code
	 */
308
	virtual void run(unsigned int i, unsigned int threadID) = 0;
309 310
};

Pierre Kraemer's avatar
Pierre Kraemer committed
311 312 313
} //namespace CGoGN

#endif