collector.hpp 7.29 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
39
40
41
42
43
template <typename PFP>
Collector<PFP>::Collector(typename PFP::MAP& mymap, const typename PFP::TVEC3& myposition):
	map(mymap),
	position(myposition)
{}

44
45
46
47
48
49
50
51
52
template <typename PFP>
bool Collector<PFP>::apply(FunctorType& f)
{
	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
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
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
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() << ") = {";
    for (unsigned int i=0; i< c.insideVertices.size(); ++i) out << c.insideVertices[i] << " ";
    out << "}" << std::endl ;
    out << "insideEdges (" << c.insideEdges.size() << ") = {";
    for (unsigned int i=0; i< c.insideEdges.size(); ++i) out << c.insideEdges[i] << " ";
    out << "}" << std::endl ;
    out << "insideFaces (" << c.insideFaces.size() << ") = {";
    for (unsigned int i=0; i< c.insideFaces.size(); ++i) out << c.insideFaces[i] << " ";
    out << "}" << std::endl ;
    out << "border (" << c.border.size() << ") = {";
    for (unsigned int i=0; i< c.border.size(); ++i) out << c.border[i] << " ";
    out << "}" << std::endl;
    return out;
}

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

template <typename PFP>
void Collector_OneRing<PFP>::init(Dart d, typename PFP::REAL r)
{
	this->centerDart = d;
	this->radius = r;
	this->insideVertices.clear();
	this->insideEdges.clear();
	this->insideFaces.clear();
	this->border.clear();
}

template <typename PFP>
void Collector_OneRing<PFP>::collect()
{
	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);
}

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

template <typename PFP>
void Collector_WithinSphere<PFP>::init(Dart d, typename PFP::REAL r)
{
	this->centerDart = d;
	this->radius = r;
	this->insideVertices.clear();
	this->insideEdges.clear();
	this->insideFaces.clear();
	this->border.clear();
	centerPosition = this->position[d];
	area = 0;
}

template <typename PFP>
void Collector_WithinSphere<PFP>::collect()
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	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

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

	unsigned int i = 0;
133
//	for(std::vector<Dart>::iterator iv = this->insideVertices.begin(); iv != this->insideVertices.end(); ++iv)
Pierre Kraemer's avatar
Pierre Kraemer committed
134
135
136
137
138
139
140
141
142
143
144
	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);

145
				if (! Geom::isPointInSphere(this->position[f], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
				{
					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);
					}
163
					if (! fm.isMarked(e) && Geom::isPointInSphere(this->position[g], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
					{
						this->insideFaces.push_back(e);
						fm.mark(e);
					}
				}
			}
			e = this->map.alpha1(e);
		} while (e != end);
		++i;
	}
}

template <typename PFP>
void Collector_WithinSphere<PFP>::computeArea()
{
	for (std::vector<Dart>::const_iterator it = this->insideFaces.begin(); it != this->insideFaces.end(); ++it)
	{
181
		area += Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
Pierre Kraemer's avatar
Pierre Kraemer committed
182
183
184
185
186
187
	}

	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);
188
		if (Geom::isPointInSphere(this->position[g], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
189
		{ // only f is outside
190
191
192
193
			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
194
195
196
		}
		else
		{ // f and g are outside
197
198
199
200
			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
201
202
203
204
205
206
		}
	}
}

} // namespace Selection

207
208
} // namespace Algo

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