matrixSetup.h 12.9 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25 26
#ifndef __LINEAR_SOLVING_MATRIX_SETUP__
#define __LINEAR_SOLVING_MATRIX_SETUP__
Pierre Kraemer's avatar
Pierre Kraemer committed
27 28 29 30 31 32 33

namespace CGoGN
{

namespace LinearSolving
{

34 35 36 37
/*******************************************************************************
 * EQUALITY MATRIX : right-hand-side SCALAR
 *******************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
38
template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
39
class FunctorEquality_Scalar : public FunctorType
Pierre Kraemer's avatar
Pierre Kraemer committed
40 41 42
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
43
	const AttributeHandler<unsigned int>& indexTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
44
	const AttributeHandler<ATTR_TYPE>& attrTable ;
45 46 47 48 49 50 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
	float weight ;

public:
	FunctorEquality_Scalar(
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const AttributeHandler<ATTR_TYPE>& attr,
		float w
	) :	solver(s), indexTable(index), attrTable(attr), weight(w)
	{}

	bool operator()(Dart d)
	{
		solver->begin_row() ;
		solver->add_coefficient(indexTable[d], 1) ;
		solver->set_right_hand_side(attrTable[d]) ;
		solver->normalize_row(weight) ;
		solver->end_row() ;
		return false ;
	}
} ;

/*******************************************************************************
 * EQUALITY MATRIX : right-hand-side VECTOR + coordinate
 *******************************************************************************/

template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
class FunctorEquality_Vector : public FunctorType
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
Pierre Kraemer's avatar
Pierre Kraemer committed
76
	const AttributeHandler<unsigned int>& indexTable ;
77
	const AttributeHandler<ATTR_TYPE>& attrTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78
	float weight ;
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
	unsigned int coord ;

public:
	FunctorEquality_Vector(
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const AttributeHandler<ATTR_TYPE>& attr,
		float w,
		unsigned int c
	) :	solver(s), indexTable(index), attrTable(attr), weight(w), coord(c)
	{}

	bool operator()(Dart d)
	{
		solver->begin_row() ;
		solver->add_coefficient(indexTable[d], 1) ;
		solver->set_right_hand_side((attrTable[d])[coord]) ;
		solver->normalize_row(weight) ;
		solver->end_row() ;
		return false ;
	}
} ;

/*******************************************************************************
 * LAPLACIAN TOPO MATRIX : right-hand-side 0
 *******************************************************************************/

template<typename PFP, class SOLVER_TRAITS>
class FunctorLaplacianTopo : public FunctorMap<typename PFP::MAP>
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
	const AttributeHandler<unsigned int>& indexTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114

public:
	typedef typename PFP::MAP MAP ;
115
	typedef typename PFP::REAL REAL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
116

117 118 119 120 121
	FunctorLaplacianTopo(
		MAP& m,
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index
	) :	FunctorMap<MAP>(m), solver(s), indexTable(index)
Pierre Kraemer's avatar
Pierre Kraemer committed
122
	{}
123

Pierre Kraemer's avatar
Pierre Kraemer committed
124 125
	bool operator()(Dart d)
	{
126 127 128 129 130 131 132 133
		solver->begin_row() ;
		Dart it = d ;
		REAL aii = 0 ;
		do
		{
			REAL aij = 1 ;
			aii += aij ;
			solver->add_coefficient(indexTable[this->m_map.phi1(it)], aij) ;
134 135 136 137 138 139
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
140 141 142 143 144
			it = this->m_map.alpha1(it) ;
		} while(it != d) ;
		solver->add_coefficient(indexTable[d], -aii) ;
		solver->normalize_row() ;
		solver->set_right_hand_side(0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
145 146 147 148 149
		solver->end_row() ;
		return false ;
	}
} ;

150 151 152 153
/*******************************************************************************
 * LAPLACIAN TOPO MATRIX : right-hand-side SCALAR
 *******************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
154
template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
155
class FunctorLaplacianTopoRHS_Scalar : public FunctorMap<typename PFP::MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
159
	const AttributeHandler<unsigned int>& indexTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
160
	const AttributeHandler<ATTR_TYPE>& attrTable ;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::REAL REAL ;

	FunctorLaplacianTopoRHS_Scalar(
		MAP& m,
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const AttributeHandler<ATTR_TYPE>& attr
	) :	FunctorMap<MAP>(m), solver(s), indexTable(index), attrTable(attr)
	{}

	bool operator()(Dart d)
	{
		solver->begin_row() ;
		Dart it = d ;
		REAL aii = 0 ;
		do
		{
			REAL aij = 1 ;
			aii += aij ;
			solver->add_coefficient(indexTable[this->m_map.phi1(it)], aij) ;
184 185 186 187 188 189
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
			it = this->m_map.alpha1(it) ;
		} while(it != d) ;
		solver->add_coefficient(indexTable[d], -aii) ;
		solver->normalize_row() ;
		solver->set_right_hand_side(attrTable[d]) ;
		solver->end_row() ;
		return false ;
	}
} ;

/*******************************************************************************
 * LAPLACIAN TOPO MATRIX : right-hand-side VECTOR + coordinate
 *******************************************************************************/

template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
class FunctorLaplacianTopoRHS_Vector : public FunctorMap<typename PFP::MAP>
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
Pierre Kraemer's avatar
Pierre Kraemer committed
209
	const AttributeHandler<unsigned int>& indexTable ;
210
	const AttributeHandler<ATTR_TYPE>& attrTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213 214
	unsigned int coord ;

public:
	typedef typename PFP::MAP MAP ;
215
	typedef typename PFP::REAL REAL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
216

217 218 219 220 221 222 223
	FunctorLaplacianTopoRHS_Vector(
		MAP& m,
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const AttributeHandler<ATTR_TYPE>& attr,
		unsigned int c
	) :	FunctorMap<MAP>(m), solver(s), indexTable(index), attrTable(attr), coord(c)
Pierre Kraemer's avatar
Pierre Kraemer committed
224
	{}
225

Pierre Kraemer's avatar
Pierre Kraemer committed
226 227
	bool operator()(Dart d)
	{
228 229 230 231 232 233 234 235
		solver->begin_row() ;
		Dart it = d ;
		REAL aii = 0 ;
		do
		{
			REAL aij = 1 ;
			aii += aij ;
			solver->add_coefficient(indexTable[this->m_map.phi1(it)], aij) ;
236 237 238 239 240 241
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
242 243 244 245
			it = this->m_map.alpha1(it) ;
		} while(it != d) ;
		solver->add_coefficient(indexTable[d], -aii) ;
		solver->normalize_row() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248 249 250 251
		solver->set_right_hand_side((attrTable[d])[coord]) ;
		solver->end_row() ;
		return false ;
	}
} ;

252 253 254 255 256 257
/*******************************************************************************
 * LAPLACIAN COTAN MATRIX : right-hand-side 0
 *******************************************************************************/

template<typename PFP, class SOLVER_TRAITS>
class FunctorLaplacianCotan : public FunctorMap<typename PFP::MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
258
{
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
	const AttributeHandler<unsigned int>& indexTable ;
	const typename PFP::TREAL& edgeWeight ;
	const typename PFP::TREAL& vertexArea ;

public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::REAL REAL ;

	FunctorLaplacianCotan(
		MAP& m,
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const typename PFP::TREAL& eWeight,
		const typename PFP::TREAL& vArea
	) :	FunctorMap<MAP>(m), solver(s), indexTable(index), edgeWeight(eWeight), vertexArea(vArea)
	{}
Pierre Kraemer's avatar
Pierre Kraemer committed
277

278 279 280 281 282 283 284 285 286 287 288
	bool operator()(Dart d)
	{
		solver->begin_row() ;
		Dart it = d ;
		REAL vArea = vertexArea[d] ;
		REAL aii = 0 ;
		do
		{
			REAL aij = edgeWeight[it] / vArea ;
			aii += aij ;
			solver->add_coefficient(indexTable[this->m_map.phi1(it)], aij) ;
289 290 291 292 293 294 295
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aij = edgeWeight[dboundary] / vArea ;
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
			it = this->m_map.alpha1(it) ;
		} while(it != d) ;
		solver->add_coefficient(indexTable[d], -aii) ;
		solver->normalize_row() ;
		solver->set_right_hand_side(0) ;
		solver->end_row() ;
		return false ;
	}
} ;

/*******************************************************************************
 * LAPLACIAN COTAN MATRIX : right-hand-side SCALAR
 *******************************************************************************/

template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
class FunctorLaplacianCotanRHS_Scalar : public FunctorMap<typename PFP::MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
312
{
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
	const AttributeHandler<unsigned int>& indexTable ;
	const typename PFP::TREAL& edgeWeight ;
	const typename PFP::TREAL& vertexArea ;
	const AttributeHandler<ATTR_TYPE>& attrTable ;

public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::REAL REAL ;

	FunctorLaplacianCotanRHS_Scalar(
		MAP& m,
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const typename PFP::TREAL& eWeight,
		const typename PFP::TREAL& vArea,
		const AttributeHandler<ATTR_TYPE>& attr
	) :	FunctorMap<MAP>(m), solver(s), indexTable(index), edgeWeight(eWeight), vertexArea(vArea), attrTable(attr)
	{}

	bool operator()(Dart d)
	{
		solver->begin_row() ;
		Dart it = d ;
		REAL vArea = vertexArea[d] ;
		REAL aii = 0 ;
		do
		{
			REAL aij = edgeWeight[it] / vArea ;
			aii += aij ;
			solver->add_coefficient(indexTable[this->m_map.phi1(it)], aij) ;
345 346 347 348 349 350 351
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aij = edgeWeight[dboundary] / vArea ;
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
			it = this->m_map.alpha1(it) ;
		} while(it != d) ;
		solver->add_coefficient(indexTable[d], -aii) ;
		solver->normalize_row() ;
		solver->set_right_hand_side(attrTable[d]) ;
		solver->end_row() ;
		return false ;
	}
} ;

/*******************************************************************************
 * LAPLACIAN COTAN MATRIX : right-hand-side VECTOR + coordinate
 *******************************************************************************/

template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
class FunctorLaplacianCotanRHS_Vector : public FunctorMap<typename PFP::MAP>
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
	const AttributeHandler<unsigned int>& indexTable ;
	const typename PFP::TREAL& edgeWeight ;
	const typename PFP::TREAL& vertexArea ;
	const AttributeHandler<ATTR_TYPE>& attrTable ;
	unsigned int coord ;

public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::REAL REAL ;

	FunctorLaplacianCotanRHS_Vector(
		MAP& m,
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const typename PFP::TREAL& eWeight,
		const typename PFP::TREAL& vArea,
		const AttributeHandler<ATTR_TYPE>& attr,
		unsigned int c
	) :	FunctorMap<MAP>(m), solver(s), indexTable(index), edgeWeight(eWeight), vertexArea(vArea), attrTable(attr), coord(c)
	{}

	bool operator()(Dart d)
	{
		solver->begin_row() ;
		Dart it = d ;
		REAL vArea = vertexArea[d] ;
		REAL aii = 0 ;
		do
		{
			REAL aij = edgeWeight[it] / vArea ;
			aii += aij ;
			solver->add_coefficient(indexTable[this->m_map.phi1(it)], aij) ;
403 404 405 406 407 408 409
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aij = edgeWeight[dboundary] / vArea ;
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
410 411 412 413 414 415 416 417 418
			it = this->m_map.alpha1(it) ;
		} while(it != d) ;
		solver->add_coefficient(indexTable[d], -aii) ;
		solver->normalize_row() ;
		solver->set_right_hand_side((attrTable[d])[coord]) ;
		solver->end_row() ;
		return false ;
	}
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
419

420
} // namespace LinearSolving
Pierre Kraemer's avatar
Pierre Kraemer committed
421

422
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
423 424

#endif