collector.hpp 9.07 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 20 21 22 23 24 25 26 27
/*******************************************************************************
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

28 29 30
namespace Algo
{

Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33
namespace Selection
{

34 35 36 37
/*********************************************************
 * Generic Collector
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
38
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
39
Collector<PFP>::Collector(typename PFP::MAP& m) : map(m)
Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
{}

42
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
43
inline bool Collector<PFP>::applyOnInsideVertices(FunctorType& f)
44 45 46 47 48 49 50
{
	for(std::vector<Dart>::iterator iv = insideVertices.begin(); iv != insideVertices.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
template <typename PFP>
inline bool Collector<PFP>::applyOnInsideEdges(FunctorType& f)
{
	for(std::vector<Dart>::iterator iv = insideEdges.begin(); iv != insideEdges.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

template <typename PFP>
inline bool Collector<PFP>::applyOnInsideFaces(FunctorType& f)
{
	for(std::vector<Dart>::iterator iv = insideFaces.begin(); iv != insideFaces.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

template <typename PFP>
inline bool Collector<PFP>::applyOnBorder(FunctorType& f)
{
	for(std::vector<Dart>::iterator iv = border.begin(); iv != border.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
78 79 80 81 82
template <typename PPFP>
std::ostream& operator<<(std::ostream &out, const Collector<PPFP>& c)
{
    out << "Collector around " << c.centerDart << std::endl;
    out << "insideVertices (" << c.insideVertices.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
83
    for (unsigned int i = 0; i< c.insideVertices.size(); ++i) out << c.insideVertices[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85
    out << "}" << std::endl ;
    out << "insideEdges (" << c.insideEdges.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
86
    for (unsigned int i = 0; i< c.insideEdges.size(); ++i) out << c.insideEdges[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
87 88
    out << "}" << std::endl ;
    out << "insideFaces (" << c.insideFaces.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
89
    for (unsigned int i = 0; i< c.insideFaces.size(); ++i) out << c.insideFaces[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91
    out << "}" << std::endl ;
    out << "border (" << c.border.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
92
    for (unsigned int i = 0; i< c.border.size(); ++i) out << c.border[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96 97 98 99 100 101
    out << "}" << std::endl;
    return out;
}

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

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
102
void Collector_OneRing<PFP>::collectAll(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
103
{
Pierre Kraemer's avatar
Pierre Kraemer committed
104 105 106 107
	this->init(d);
	this->insideEdges.reserve(12);
	this->insideFaces.reserve(12);
	this->border.reserve(12);
Pierre Kraemer's avatar
Pierre Kraemer committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121

	const Dart end = this->centerDart;
	this->insideVertices.push_back(end);
	Dart dd = end;
	do
	{
		this->insideEdges.push_back(dd);
		this->insideFaces.push_back(dd);
		this->border.push_back(this->map.phi1(dd));
		dd = this->map.alpha1(dd);
	} while(dd != end);
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
122
void Collector_OneRing<PFP>::collectBorder(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
123
{
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127 128 129 130 131 132 133
	this->init(d);
	this->border.reserve(12);

	const Dart end = this->centerDart;
	Dart dd = end;
	do
	{
		this->border.push_back(this->map.phi1(dd));
		dd = this->map.alpha1(dd);
	} while(dd != end);
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135
}

Pierre Kraemer's avatar
Pierre Kraemer committed
136 137 138 139
/*********************************************************
 * Collector Within Sphere
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
140
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
141
void Collector_WithinSphere<PFP>::collectAll(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
142 143 144 145
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148 149 150 151 152 153
	this->init(d);
	this->insideEdges.reserve(24);
	this->insideFaces.reserve(24);
	this->border.reserve(24);

	CellMarkerStore vm(this->map, VERTEX);	// mark the collected inside-vertices
	CellMarkerStore em(this->map, EDGE);	// mark the collected inside-edges + border-edges
	CellMarkerStore fm(this->map, FACE);	// mark the collected inside-faces + border-faces
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155 156 157

	this->insideVertices.push_back(this->centerDart);
	vm.mark(this->centerDart);

Pierre Kraemer's avatar
Pierre Kraemer committed
158
	VEC3 centerPosition = this->position[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
159
	unsigned int i = 0;
160
//	for(std::vector<Dart>::iterator iv = this->insideVertices.begin(); iv != this->insideVertices.end(); ++iv)
Pierre Kraemer's avatar
Pierre Kraemer committed
161 162 163 164 165 166 167 168 169 170 171
	while (i < this->insideVertices.size())
	{
		Dart end = this->insideVertices[i];
		Dart e = end;
		do
		{
			if (! em.isMarked(e) || ! fm.isMarked(e)) // are both tests useful ?
			{
				const Dart f = this->map.phi1(e);
				const Dart g = this->map.phi1(f);

172
				if (! Geom::isPointInSphere(this->position[f], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
				{
					this->border.push_back(e); // add to border
					em.mark(e);
					fm.mark(e); // is it useful ?
				}
				else
				{
					if (! vm.isMarked(f))
					{
						this->insideVertices.push_back(f);
						vm.mark(f);
					}
					if (! em.isMarked(e))
					{
						this->insideEdges.push_back(e);
						em.mark(e);
					}
190
					if (! fm.isMarked(e) && Geom::isPointInSphere(this->position[g], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
191 192 193 194 195 196 197 198 199 200 201 202
					{
						this->insideFaces.push_back(e);
						fm.mark(e);
					}
				}
			}
			e = this->map.alpha1(e);
		} while (e != end);
		++i;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205 206 207 208 209 210
template <typename PFP>
void Collector_WithinSphere<PFP>::collectBorder(Dart d)
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	this->init(d);
	this->border.reserve(100);
211
	this->insideVertices.reserve(100);
Pierre Kraemer's avatar
Pierre Kraemer committed
212

213
	CellMarkerStore vm(this->map, VERTEX);	// mark the collected inside-vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
214
	CellMarkerStore em(this->map, EDGE);	// mark the collected inside-edges + border-edges
215 216 217

	this->insideVertices.push_back(this->centerDart);
	vm.mark(this->centerDart);
Pierre Kraemer's avatar
Pierre Kraemer committed
218 219 220 221 222 223 224 225 226

	VEC3 centerPosition = this->position[d];
	unsigned int i = 0;
	while (i < this->insideVertices.size())
	{
		Dart end = this->insideVertices[i];
		Dart e = end;
		do
		{
227
			if ( ! em.isMarked(e) )
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229 230 231 232 233 234
			{
				const Dart f = this->map.phi1(e);

				if (! Geom::isPointInSphere(this->position[f], centerPosition, this->radius))
				{
					this->border.push_back(e); // add to border
				}
235 236 237 238 239 240 241 242 243
				else
				{
					if (! vm.isMarked(f))
					{
						this->insideVertices.push_back(f);
						vm.mark(f);
					}
				}
				em.mark(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
244 245 246 247 248
			}
			e = this->map.alpha1(e);
		} while (e != end);
		++i;
	}
249
	this->insideVertices.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
250 251
}

Pierre Kraemer's avatar
Pierre Kraemer committed
252 253 254
template <typename PFP>
void Collector_WithinSphere<PFP>::computeArea()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
255 256 257
	area = 0;
	typename PFP::VEC3 centerPosition = this->position[this->centerDart];

Pierre Kraemer's avatar
Pierre Kraemer committed
258
	for (std::vector<Dart>::const_iterator it = this->insideFaces.begin(); it != this->insideFaces.end(); ++it)
259
		area += Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
Pierre Kraemer's avatar
Pierre Kraemer committed
260 261 262 263 264

	for (std::vector<Dart>::const_iterator it = this->border.begin(); it != this->border.end(); ++it)
	{
		const Dart f = this->map.phi1(*it); // we know that f is outside
		const Dart g = this->map.phi1(f);
265
		if (Geom::isPointInSphere(this->position[g], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
266
		{ // only f is outside
267 268 269 270
			typename PFP::REAL alpha, beta;
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, *it, this->position, alpha);
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, this->map.phi2(f), this->position, beta);
			area += (alpha+beta - alpha*beta) * Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
Pierre Kraemer's avatar
Pierre Kraemer committed
271 272 273
		}
		else
		{ // f and g are outside
274 275 276 277
			typename PFP::REAL alpha, beta;
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, *it, this->position, alpha);
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, this->map.phi2(g), this->position, beta);
			area += alpha * beta * Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
Pierre Kraemer's avatar
Pierre Kraemer committed
278 279 280 281 282 283
		}
	}
}

} // namespace Selection

284 285
} // namespace Algo

Pierre Kraemer's avatar
Pierre Kraemer committed
286
} // namespace CGoGN