matrixSetup.h 14.8 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>
Pierre Kraemer's avatar
Pierre Kraemer committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
class FunctorEquality_PerVertexWeight_Scalar : public FunctorType
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
	const AttributeHandler<unsigned int>& indexTable ;
	const AttributeHandler<ATTR_TYPE>& attrTable ;
	const AttributeHandler<typename PFP::REAL>& weightTable ;

public:
	FunctorEquality_PerVertexWeight_Scalar(
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const AttributeHandler<ATTR_TYPE>& attr,
		const AttributeHandler<typename PFP::REAL>& weight
	) :	solver(s), indexTable(index), attrTable(attr), weightTable(weight)
	{}

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

template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
class FunctorEquality_UniformWeight_Scalar : public FunctorType
Pierre Kraemer's avatar
Pierre Kraemer committed
69 70 71
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
72
	const AttributeHandler<unsigned int>& indexTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
73
	const AttributeHandler<ATTR_TYPE>& attrTable ;
74 75 76
	float weight ;

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
77
	FunctorEquality_UniformWeight_Scalar(
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		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>
Pierre Kraemer's avatar
Pierre Kraemer committed
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
class FunctorEquality_PerVertexWeight_Vector : public FunctorType
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
	const AttributeHandler<unsigned int>& indexTable ;
	const AttributeHandler<ATTR_TYPE>& attrTable ;
	const AttributeHandler<typename PFP::REAL>& weightTable ;
	unsigned int coord ;

public:
	FunctorEquality_PerVertexWeight_Vector(
		LinearSolver<SOLVER_TRAITS>* s,
		const AttributeHandler<unsigned int>& index,
		const AttributeHandler<ATTR_TYPE>& attr,
		const AttributeHandler<typename PFP::REAL>& weight,
		unsigned int c
	) :	solver(s), indexTable(index), attrTable(attr), weightTable(weight), 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(weightTable[d]) ;
		solver->end_row() ;
		return false ;
	}
} ;

template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
class FunctorEquality_UniformWeight_Vector : public FunctorType
133 134 135
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136
	const AttributeHandler<unsigned int>& indexTable ;
137
	const AttributeHandler<ATTR_TYPE>& attrTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138
	float weight ;
139 140 141
	unsigned int coord ;

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
142
	FunctorEquality_UniformWeight_Vector(
143 144 145 146 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
		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
172 173 174

public:
	typedef typename PFP::MAP MAP ;
175
	typedef typename PFP::REAL REAL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
176

177 178 179 180 181
	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
182
	{}
183

Pierre Kraemer's avatar
Pierre Kraemer committed
184 185
	bool operator()(Dart d)
	{
186 187 188 189 190 191 192 193
		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) ;
194 195 196 197 198 199
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
200 201 202 203 204
			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
205 206 207 208 209
		solver->end_row() ;
		return false ;
	}
} ;

210 211 212 213
/*******************************************************************************
 * LAPLACIAN TOPO MATRIX : right-hand-side SCALAR
 *******************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
214
template<typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
215
class FunctorLaplacianTopoRHS_Scalar : public FunctorMap<typename PFP::MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
216 217 218
{
protected:
	LinearSolver<SOLVER_TRAITS>* solver ;
219
	const AttributeHandler<unsigned int>& indexTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
220
	const AttributeHandler<ATTR_TYPE>& attrTable ;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

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) ;
244 245 246 247 248 249
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
			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
269
	const AttributeHandler<unsigned int>& indexTable ;
270
	const AttributeHandler<ATTR_TYPE>& attrTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
271 272 273 274
	unsigned int coord ;

public:
	typedef typename PFP::MAP MAP ;
275
	typedef typename PFP::REAL REAL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
276

277 278 279 280 281 282 283
	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
284
	{}
285

Pierre Kraemer's avatar
Pierre Kraemer committed
286 287
	bool operator()(Dart d)
	{
288 289 290 291 292 293 294 295
		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) ;
296 297 298 299 300 301
			Dart dboundary = this->m_map.phi_1(it) ;
			if(this->m_map.phi2(dboundary) == dboundary)
			{
				aii += aij ;
				solver->add_coefficient(indexTable[dboundary], aij) ;
			}
302 303 304 305
			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
306 307 308 309 310 311
		solver->set_right_hand_side((attrTable[d])[coord]) ;
		solver->end_row() ;
		return false ;
	}
} ;

312 313 314 315 316 317
/*******************************************************************************
 * 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
318
{
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
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
337

338 339 340 341 342 343 344 345 346 347 348
	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) ;
349 350 351 352 353 354 355
			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) ;
			}
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
			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
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 403 404
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) ;
405 406 407 408 409 410 411
			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) ;
			}
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			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) ;
463 464 465 466 467 468 469
			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) ;
			}
470 471 472 473 474 475 476 477 478
			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
479

480
} // namespace LinearSolving
Pierre Kraemer's avatar
Pierre Kraemer committed
481

482
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
483 484

#endif