File: aslBarycentric.cxx

package info (click to toggle)
asl 0.1.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,536 kB
  • sloc: cpp: 36,180; makefile: 26
file content (100 lines) | stat: -rw-r--r-- 2,646 bytes parent folder | download | duplicates (4)
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
28
29
30
31
32
33
34
35
36
37
38
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
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
/*
 * Advanced Simulation Library <http://asl.org.il>
 * 
 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
 *
 *
 * This file is part of Advanced Simulation Library (ASL).
 *
 * ASL is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3 of the License.
 *
 * ASL 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "aslBarycentric.h"
#include "aslMatrices.h"
#include "../aslUtilities.h"

using namespace std;
using asl::errorMessage;

namespace asl
{
	Barycentric::Barycentric(vector<AVec<>> & p)
	{
		init(p);
	}
	
	Barycentric::Barycentric():
		corners(0)
	{
	}

	
	void Barycentric::init(vector<AVec<>> & p)
	{

		if(p.size()-1 != p[0].getSize())
			errorMessage("asl::Barycentric::init: number of points does not corresponds to the dimensionality");
			
		corners=p;

		unsigned int n(p.size()-1);
		t.resize(n, n);
		tInv.resize(n, n);

		for(unsigned int i(1); i < p.size(); ++i)
		{
			t.setRow(i - 1, corners[i]-corners[0]);
		}
		
		tInv = inverseMatrix(t);
	}

	AVec<> Barycentric::getCordinates(const AVec<> & p)
	{
		if(t.getNRow() != p.getSize())
			errorMessage("asl::Barycentric::interpolate: point dimensionality does not corresponds to the triangle dimensionality");
		return tInv * (p - corners[0]);
	}

	double Barycentric::interpolate(const AVec<> & p, 
	                                          const AVec<> & f)
	{
		if(p.getSize()+1 != f.getSize())
			errorMessage("Barycentric::interpolate: number of function values does not corresponds to the dimensionality");

		AVec<> fm(subAVec(f, 1, p.getSize()) - AVec<>(p.getSize(), f[0]));
		return f[0] + fm * getCordinates(p); 
	}
		
	bool  Barycentric::in(const AVec<> & p)
	{
		auto x(getCordinates(p));
		auto res(0 <= x[0]);
		for(unsigned int i(1); i < x.getSize(); ++i)
			res = res && ( 0 <= x[i] );
		res = res && (sumOfElements(x) <= 1);
		return res;
	}

	AVec<> Barycentric::gradient(const AVec<> & f)
	{
		if(t.getNRow()+1 != f.getSize())
			errorMessage("Barycentric::gradient: number of function values does not corresponds to the dimensionality");
		unsigned int nd(f.getSize()-1);
		AVec<> fm(subAVec(f, 1, nd) - AVec<>(nd,f[0]));
		return fm*tInv; 
	}
		
} // namespace asl