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

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

177
178
179
180
181
182
183
184
185
186
187
//template <typename PFP>
//typename PFP::REAL Collector_WithinSphere<PFP>::intersect_SphereEdge(const Dart din, const Dart dout)
//{
//	typedef typename PFP::VEC3 VEC3;
//	typedef typename PFP::REAL REAL;
//	VEC3 p = this->position[din] - this->centerPosition;
//	VEC3 qminusp = this->position[dout] - this->centerPosition - p;
//	REAL s = p * qminusp;
//	REAL n2 = qminusp.norm2();
//	return (- s + sqrt(s * s + n2 * (this->radius_2 - p.norm2()))) / n2;
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

template <typename PFP>
void Collector_WithinSphere<PFP>::computeArea()
{
	for (std::vector<Dart>::const_iterator it = this->insideFaces.begin(); it != this->insideFaces.end(); ++it)
	{
		this->area += Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
	}

	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);
		if (this->isInside(g))
		{ // only f is outside
203
204
			typename PFP::REAL alpha = this->intersect_SphereEdge(*it, f);
			typename PFP::REAL beta = this->intersect_SphereEdge(g, f);
Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
207
208
			this->area += (alpha+beta - alpha*beta) * Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
		}
		else
		{ // f and g are outside
209
210
			typename PFP::REAL alpha = this->intersect_SphereEdge(*it, f);
			typename PFP::REAL beta = this->intersect_SphereEdge(*it, g);
Pierre Kraemer's avatar
Pierre Kraemer committed
211
212
213
214
215
216
217
			this->area += alpha * beta * Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
		}
	}
}

} // namespace Selection

218
219
} // namespace Algo

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