Debian Codegraph @

src/math/aslDistanceFunction.cxx

raw
1 /*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23
24 #include "aslDistanceFunction.h"
25 #include "acl/aclMath/aclVectorOfElements.h"
26 #include <acl/aclGenerators.h>
27 #include <acl/aclHardware.h>
28 #include <acl/acl.h>
29 #include <data/aslBlocks.h>
30 #include <aslGenerators.h>
31 #include <math/aslIndex2Position.h>
32 #include <acl/Kernels/aclKernelConfigurationTemplates.h>
33 #include <acl/Kernels/aclKernel.h>
34 #include <data/aslDataWithGhostNodes.h>
35 #include <math/aslTemplateVE.h>
36
37
38 namespace asl
39 {
40 DistanceFunction::DistanceFunction()
41 {}
42
43 DistanceFunction::~DistanceFunction()
44 {}
45
46 DistFBinaryOperation::
47 DistFBinaryOperation(SPDistanceFunction a, SPDistanceFunction b):
48 e1(a), e2(b)
49 {
50 }
51
52 DistFUnaryOperation::
53 DistFUnaryOperation(SPDistanceFunction a):
54 e1(a)
55 {
56 }
57
58 DistFOperationAnd::
59 DistFOperationAnd(SPDistanceFunction a, SPDistanceFunction b):
60 DistFBinaryOperation(a,b)
61 {
62 }
63
64 acl::VectorOfElements DistFOperationAnd::getDistance(const acl::VectorOfElements & pos)
65 {
66 return max(e1->getDistance(pos), e2->getDistance(pos));
67 }
68
69 DistFOperationOr::
70 DistFOperationOr(SPDistanceFunction a, SPDistanceFunction b):
71 DistFBinaryOperation(a,b)
72 {
73 }
74
75 acl::VectorOfElements DistFOperationOr::getDistance(const acl::VectorOfElements & pos)
76 {
77 return min(e1->getDistance(pos), e2->getDistance(pos));
78 }
79
80 DistFOperationInversion::
81 DistFOperationInversion(SPDistanceFunction a):
82 DistFUnaryOperation(a)
83 {
84 }
85
86 acl::VectorOfElements DistFOperationInversion::getDistance(const acl::VectorOfElements & pos)
87 {
88 return -(e1->getDistance(pos));
89 }
90
91
92 DistFSphere::
93 DistFSphere(double r, const AVec<double> & c):
94 radius(acl::generateVEConstant(r)), center(acl::generateVEConstant(c))
95 {
96 }
97
98 acl::VectorOfElements DistFSphere::getDistance(const acl::VectorOfElements & pos)
99 {
100 return sqrt(l2(pos-center))-radius;
101 }
102
103 DistFCylinder::
104 DistFCylinder(double r, const AVec<double> & l, const AVec<double> & c):
105 radius(acl::generateVEConstant(r)),
106 orientation(acl::generateVEConstant(normalize(l))),
107 center(acl::generateVEConstant(c))
108 {
109 }
110
111 acl::VectorOfElements DistFCylinder::getDistance(const acl::VectorOfElements & pos)
112 {
113 return sqrt(l2((pos-center) - ((pos-center)*orientation)*orientation))-radius;
114 }
115
116 DistFCone::
117 DistFCone(double th, const AVec<double> & l, const AVec<double> & a):
118 tanTheta(acl::generateVEConstant(tan(th))),
119 orientation(acl::generateVEConstant(normalize(l))),
120 apex(acl::generateVEConstant(a))
121 {
122 }
123
124 acl::VectorOfElements DistFCone::getDistance(const acl::VectorOfElements & pos)
125 {
126 return sqrt(l2((pos-apex) - ((pos-apex)*orientation)*orientation)) -
127 tanTheta*((pos-apex)*orientation);
128 }
129
130 acl::VectorOfElements DistFPlane::getDistance(const acl::VectorOfElements & pos)
131 {
132 return pos*normal-b;
133 }
134
135 DistFPlane::
136 DistFPlane(AVec<double> n, AVec<double> p0):
137 normal(acl::generateVEConstant(normalize(n))),
138 b(acl::generateVEConstant(p0*n/sqrt(l2(n))))
139 {
140 }
141
142 const double DistFNormalization::scaleFactor(1.8);
143
144 DistFNormalization::
145 DistFNormalization(SPDistanceFunction a, double dx):
146 DistFUnaryOperation(a),
147 factor(acl::generateVEConstant(1./dx/scaleFactor))
148 {
149 }
150
151 acl::VectorOfElements DistFNormalization::getDistance(const acl::VectorOfElements & pos)
152 {
153 auto type(getElementType(pos));
154 acl::VectorOfElements dist(1);
155 copy(factor*e1->getDistance(pos),dist);
156
157 acl::VectorOfElements topCut(1);
158 copy(acl::select(dist, acl::generateVEConstant(1.), dist>1., type),
159 topCut);
160 acl::VectorOfElements bottomCut(1);
161 copy(acl::select(topCut, acl::generateVEConstant(-1.), dist<-1., type),
162 bottomCut);
163 return bottomCut;
164 }
165
166 acl::VectorOfElements DataInterpolation::getDistance(const acl::VectorOfElements & pos)
167 {
168 acl::TypeID type(getElementType(data->getEContainer()));
169 auto & bl(data->getBlock());
170 auto e((pos - bl.position)/bl.dx);
171 auto ind(convert(acl::TYPE_SELECT[type],floor(e) * bl.c2iTransformVector,false));
172 copy(e - acl::floor(e),e);
173
174 TemplateVE dTVE;
175 auto d(generateDataContainer_SP(data->getBlock(), data->getEContainer(),0));
176 unsigned int nC(data->getEContainer().size());
177 unsigned int nd(nD(data->getBlock()));
178 acl::VectorOfElements res(nC);
179 for(unsigned int i(0); i < nC; ++i)
180 {
181 dTVE.init(*d, *elementaryCellVT(nd), i, false);
182 copy(excerpt(dTVE.values,ind),dTVE.values);
183 res[i] = (interpolate(dTVE, e))[0];
184 }
185 return res;
186 }
187
188 DataInterpolation::
189 DataInterpolation(SPAbstractDataWithGhostNodes d):
190 data(d)
191 {
192 }
193
194
195 } // asl