File: PSurfaceFactory.cpp

package info (click to toggle)
psurface 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,016 kB
  • ctags: 1,077
  • sloc: cpp: 12,335; makefile: 111; awk: 38
file content (312 lines) | stat: -rw-r--r-- 11,308 bytes parent folder | download | duplicates (3)
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
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
134
135
136
137
138
139
140
141
142
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "config.h"

#include <vector>

#include "PSurface.h"
#include "PSurfaceFactory.h"

using namespace psurface;

template <int dim, class ctype>
void PSurfaceFactory<dim,ctype>::setTargetSurface(Surface* surface)
{
    psurface_->surface = surface;
}

template <int dim, class ctype>
void PSurfaceFactory<dim,ctype>::insertVertex(const StaticVector<ctype,dim+1>& position)
{
    psurface_->newVertex(position);
}

template <int dim, class ctype>
unsigned int  PSurfaceFactory<dim,ctype>::insertSimplex(const std::tr1::array<unsigned int, dim+1>& v)
{
    unsigned int idx = psurface_->createSpaceForTriangle(v[0], v[1], v[2]);
    psurface_->integrateTriangle(idx);
    return idx;
}

template <int dim, class ctype>
void PSurfaceFactory<dim,ctype>::insertTargetVertexMapping(unsigned int targetVertex,
                                                           unsigned int domainTriangle,
                                                           const StaticVector<ctype,dim>& domainLocalPosition,
                                                           NodeBundle& projectedTo,
                                                           int& domainVertex)
{
    const double eps = 0.0001;

    // determine which type of node to add
    typename Node<ctype>::NodeType newType = Node<ctype>::INTERIOR_NODE;
    int dir = -1;
    double mu;

    // if the normal projection hits a base grid vertex, this is the vertex
    domainVertex = -1;

    if (domainLocalPosition[0] < eps) {
        dir = 1;
        mu = 1-domainLocalPosition[1];
        if (domainLocalPosition[1] < eps) {
            newType = Node<ctype>::CORNER_NODE;
            domainVertex = psurface_->triangles(domainTriangle).vertices[2];
        } else if (domainLocalPosition[1] > 1-eps) {
            newType = Node<ctype>::CORNER_NODE;
            domainVertex = psurface_->triangles(domainTriangle).vertices[1];
        } else {
            newType = Node<ctype>::TOUCHING_NODE;
        }
    } else if (domainLocalPosition[1] < eps) {
        dir = 2;
        mu = domainLocalPosition[0];
        if (domainLocalPosition[0] < eps) {
            newType = Node<ctype>::CORNER_NODE;
            domainVertex = psurface_->triangles(domainTriangle).vertices[2];
        } else if (domainLocalPosition[0] > 1-eps) {
            newType = Node<ctype>::CORNER_NODE;
            domainVertex = psurface_->triangles(domainTriangle).vertices[0];
        } else {
            newType = Node<ctype>::TOUCHING_NODE;
        }
    } else if (1-domainLocalPosition[0]-domainLocalPosition[1] < eps) {
        dir = 0;
        mu = 1-domainLocalPosition[0];
        if (domainLocalPosition[1] < eps) {
            newType = Node<ctype>::CORNER_NODE;
            domainVertex = psurface_->triangles(domainTriangle).vertices[0];
        } else if (domainLocalPosition[1] > 1-eps) {
            newType = Node<ctype>::CORNER_NODE;
            domainVertex = psurface_->triangles(domainTriangle).vertices[1];
        } else {
            newType = Node<ctype>::TOUCHING_NODE;
        }
    }

    if (newType==Node<ctype>::TOUCHING_NODE) {

        // find the other triangle, if there is one
        int neighboringTri = psurface_->getNeighboringTriangle(domainTriangle, dir);

        if (neighboringTri == -1) {
            NodeIdx newNodeNumber = addTouchingNode(domainTriangle, domainLocalPosition, dir, targetVertex);
            projectedTo.resize(1);
            projectedTo[0].setValue(domainTriangle, newNodeNumber);
        } else {
            // find domain pos on other triangle
            int commonEdge = psurface_->triangles(domainTriangle).getCommonEdgeWith(psurface_->triangles(neighboringTri));
            int dir2 = psurface_->triangles(neighboringTri).getEdge(commonEdge);
            StaticVector<ctype,2> dP2((dir2==0)*(mu) + (dir2==2)*(1-mu), (dir2==0)*(1-mu) + (dir2==1)*(mu));

            // insert touching node pair
            projectedTo = addTouchingNodePair(domainTriangle, neighboringTri, domainLocalPosition, dP2,
                                                                   dir, dir2, targetVertex);
        }

    } else if (newType==Node<ctype>::CORNER_NODE) {

        addCornerNodeBundle(domainVertex, targetVertex);
        std::vector<int> neighboringTris = psurface_->getTrianglesPerVertex(domainVertex);
        projectedTo.resize(neighboringTris.size());
        for (size_t j=0; j<neighboringTris.size(); j++) {
            projectedTo[j].setValue(neighboringTris[j],
                                                  psurface_->triangles(neighboringTris[j]).nodes.size()-1);
        }

    } else {

        NodeIdx newNodeNumber = addInteriorNode(domainTriangle, domainLocalPosition, targetVertex);

        projectedTo.resize(1);
        projectedTo[0].setValue(domainTriangle, newNodeNumber);

    }

}

template <int dim, class ctype>
void PSurfaceFactory<dim,ctype>::insertGhostNode(unsigned int domainVertex,
                                                 unsigned int targetTriangle,
                                                 const StaticVector<ctype,dim>& targetLocalPosition)
{
    std::vector<int> neighbors = psurface_->getTrianglesPerVertex(domainVertex);

    for (size_t i=0; i<neighbors.size(); i++) {

        int corner = psurface_->triangles(neighbors[i]).getCorner(domainVertex);
        addGhostNode(neighbors[i], corner, targetTriangle, targetLocalPosition);

    }
}

template <int dim, class ctype>
void PSurfaceFactory<dim,ctype>::insertEdge()
{
}

template <int dim, class ctype>
void PSurfaceFactory<dim,ctype>::addCornerNodeBundle(int domainVertex, int targetVertex)
{
    std::vector<int> neighbors = psurface_->getTrianglesPerVertex(domainVertex);

    for (size_t i=0; i<neighbors.size(); i++) {

        int corner = psurface_->triangles(neighbors[i]).getCorner(domainVertex);
        addCornerNode(neighbors[i], corner, targetVertex);

    }

}


template <int dim, class ctype>
NodeIdx PSurfaceFactory<dim,ctype>::addInteriorNode(int tri, const StaticVector<ctype,2>& dom, int nodeNumber)
{
    psurface_->triangles(tri).nodes.push_back(Node<ctype>(dom, nodeNumber, Node<ctype>::INTERIOR_NODE));
    return psurface_->triangles(tri).nodes.size()-1;
}

template <int dim, class ctype>
NodeIdx PSurfaceFactory<dim,ctype>::addGhostNode(int tri, int corner, int targetTri, const StaticVector<ctype,2>& localTargetCoords)
{
    psurface_->triangles(tri).nodes.push_back(Node<ctype>());
    psurface_->triangles(tri).nodes.back().makeGhostNode(corner, targetTri, localTargetCoords);
    return psurface_->triangles(tri).nodes.size()-1;
}

template <int dim, class ctype>
NodeIdx PSurfaceFactory<dim,ctype>::addCornerNode(int tri, int corner, int nodeNumber)
{
    DomainTriangle<ctype>& cT = psurface_->triangles(tri);

    cT.nodes.push_back(Node<ctype>());
    cT.nodes.back().makeCornerNode(corner, nodeNumber);
    return cT.nodes.size()-1;
}

// BUG: The node needs to be entered in the edgepoint arrays
template <int dim, class ctype>
NodeBundle PSurfaceFactory<dim,ctype>::addIntersectionNodePair(int tri1, int tri2,
                                                const StaticVector<ctype,2>& dP1, const StaticVector<ctype,2>& dP2,
                                                int edge1, int edge2, const StaticVector<ctype,3>& range)
{
    // We return the pair of new nodes
    NodeBundle result(2);
    result[0].tri = tri1;
    result[1].tri = tri2;

    DomainTriangle<ctype>& cT1 = psurface_->triangles(tri1);
    DomainTriangle<ctype>& cT2 = psurface_->triangles(tri2);

    psurface_->iPos.push_back(range);
    int nodeNumber = psurface_->iPos.size()-1;

    cT1.nodes.push_back(Node<ctype>());
    cT2.nodes.push_back(Node<ctype>());

    result[0].idx = cT1.nodes.size()-1;
    result[1].idx = cT2.nodes.size()-1;

    cT1.nodes.back().setValue(dP1, nodeNumber, Node<ctype>::INTERSECTION_NODE);
    cT2.nodes.back().setValue(dP2, nodeNumber, Node<ctype>::INTERSECTION_NODE);

    cT1.nodes.back().setDomainEdge(edge1);
    cT2.nodes.back().setDomainEdge(edge2);

    return result;
}

// BUG: The node needs to be entered in the edgepoint arrays
template <int dim, class ctype>
NodeBundle PSurfaceFactory<dim,ctype>::addBoundaryNode(int tri, const StaticVector<ctype,2>& dP,
                                                int edge, const StaticVector<ctype,3>& range, int targetVert)
{
    // We return the pair of new nodes
    NodeBundle result(1);
    result[0].tri = tri;

    DomainTriangle<ctype>& cT = psurface_->triangles(tri);

    psurface_->iPos.push_back(range);
    int nodeNumber = psurface_->iPos.size()-1;

    cT.nodes.push_back(Node<ctype>());

    result[0].idx = cT.nodes.size()-1;
    cT.nodes.back().setValue(dP, nodeNumber, Node<ctype>::INTERSECTION_NODE, targetVert);
    cT.nodes.back().setDomainEdge(edge);

    return result;
}

// BUG: The node needs to be entered in the edgepoint arrays
template <int dim, class ctype>
NodeIdx PSurfaceFactory<dim,ctype>::addTouchingNode(int tri, const StaticVector<ctype,2>& dP, int edge, int nodeNumber)
{
    DomainTriangle<ctype>& cT = psurface_->triangles(tri);

    cT.nodes.push_back(Node<ctype>());

    cT.nodes.back().setValue(dP, nodeNumber, Node<ctype>::TOUCHING_NODE);
    cT.nodes.back().setDomainEdge(edge);
    return cT.nodes.size()-1;
}

// BUG: The node needs to be entered in the edgepoint arrays
template <int dim, class ctype>
NodeBundle PSurfaceFactory<dim,ctype>::addTouchingNodePair(int tri1, int tri2,
                                            const StaticVector<ctype,2>& dP1, const StaticVector<ctype,2>& dP2,
                                            int edge1, int edge2, int nodeNumber)
{
    // We return the pair of new nodes
    NodeBundle result(2);
    result[0].tri = tri1;
    result[1].tri = tri2;

    DomainTriangle<ctype>& cT1 = psurface_->triangles(tri1);
    DomainTriangle<ctype>& cT2 = psurface_->triangles(tri2);

    cT1.nodes.push_back(Node<ctype>());
    cT2.nodes.push_back(Node<ctype>());

    cT1.nodes.back().setValue(dP1, nodeNumber, Node<ctype>::TOUCHING_NODE);
    cT2.nodes.back().setValue(dP2, nodeNumber, Node<ctype>::TOUCHING_NODE);

    cT1.nodes.back().setDomainEdge(edge1);
    cT2.nodes.back().setDomainEdge(edge2);

    result[0].idx = cT1.nodes.size()-1;
    result[1].idx = cT2.nodes.size()-1;

    return result;
}

template <int dim, class ctype>
void PSurfaceFactory<dim,ctype>::addParTriangle(int tri, const std::tr1::array<int,3>& p)
{
    DomainTriangle<ctype>& cT = psurface_->triangles(tri);

    assert(p[0]>=0 && p[0]<cT.nodes.size());
    assert(p[1]>=0 && p[1]<cT.nodes.size());
    assert(p[2]>=0 && p[2]<cT.nodes.size());

    if (!cT.nodes[p[0]].isConnectedTo(p[1]))
        cT.addEdge(p[0], p[1]);
    if (!cT.nodes[p[1]].isConnectedTo(p[2]))
        cT.addEdge(p[1], p[2]);
    if (!cT.nodes[p[2]].isConnectedTo(p[0]))
        cT.addEdge(p[2], p[0]);

}


// ///////////////////////////////////////////////////////////////////////
//   Explicitly instantiate 'float' and 'double' versions of this code
// ///////////////////////////////////////////////////////////////////////

namespace psurface {
//template class PSurfaceFactory<1,float>;
//template class PSurfaceFactory<1,double>;

  template class PSurfaceFactory<2,float>;
  template class PSurfaceFactory<2,double>;
}