HexahedralMeshTopologyKernel.cc 10.7 KB
Newer Older
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
/*===========================================================================*\
 *                                                                           *
 *                            OpenVolumeMesh                                 *
 *        Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen         *
 *                        www.openvolumemesh.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenVolumeMesh.                                     *
 *                                                                           *
 *  OpenVolumeMesh 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 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenVolumeMesh 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 LesserGeneral Public          *
 *  License along with OpenVolumeMesh.  If not,                              *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Date$                    *
 *   $LastChangedBy$                                                *
 *                                                                           *
\*===========================================================================*/

#include "HexahedralMeshTopologyKernel.hh"

namespace OpenVolumeMesh {

47
48

HexahedralMeshTopologyKernel::HexahedralMeshTopologyKernel() {
49
50
51
52
53

}

//========================================================================================

54
55

HexahedralMeshTopologyKernel::~HexahedralMeshTopologyKernel() {
56
57
58
59
60

}

//========================================================================================

61
62

FaceHandle HexahedralMeshTopologyKernel::add_face(const std::vector<HalfEdgeHandle>& _halfedges, bool _topologyCheck) {
63
64
65

    if(_halfedges.size() != 4) {
        std::cerr << "Face valence is not four! Aborting." << std::endl;
66
        return TopologyKernel::InvalidFaceHandle;
67
68
    }

69
    return TopologyKernel::add_face(_halfedges, _topologyCheck);
70
71
72
73
}

//========================================================================================

74

75
FaceHandle
76
HexahedralMeshTopologyKernel::add_face(const std::vector<VertexHandle>& _vertices) {
77
78
79

    if(_vertices.size() != 4) {
        std::cerr << "Face valence is not four! Aborting." << std::endl;
80
        return TopologyKernel::InvalidFaceHandle;
81
82
    }

83
    return TopologyKernel::add_face(_vertices);
84
85
86
87
}

//========================================================================================

88

89
CellHandle
90
HexahedralMeshTopologyKernel::add_cell(const std::vector<HalfFaceHandle>& _halffaces, bool _topologyCheck, bool _reorderFaces) {
91
92
93

    if(_halffaces.size() != 6) {
        std::cerr << "Cell valence is not six! Aborting." << std::endl;
94
        return TopologyKernel::InvalidCellHandle;
95
96
97
    }
    for(typename std::vector<HalfFaceHandle>::const_iterator it = _halffaces.begin();
            it != _halffaces.end(); ++it) {
98
        if(TopologyKernel::halfface(*it).halfedges().size() != 4) {
99
            std::cerr << "Incident face does not have valence four! Aborting." << std::endl;
100
            return TopologyKernel::InvalidCellHandle;
101
102
103
104
105
106
107
108
109
110
111
112
113
        }
    }

    // Ordering array (see below for details)
    const int orderTop[] = {2, 4, 3, 5};
    const int orderBot[] = {3, 4, 2, 5};

    // Create new halffaces vector
    std::vector<HalfFaceHandle> ordered_halffaces;

    // The user wants the faces to be reordered
    if(_reorderFaces) {

114
        ordered_halffaces.resize(6, TopologyKernel::InvalidHalfFaceHandle);
115
116
117
118
119

        // Create top side
        ordered_halffaces[0] = _halffaces[0];

        // Go over all incident halfedges
120
        std::vector<HalfEdgeHandle> hes = TopologyKernel::halfface(ordered_halffaces[0]).halfedges();
121
122
123
124
125
        unsigned int idx = 0;
        for(typename std::vector<HalfEdgeHandle>::const_iterator he_it = hes.begin();
                he_it != hes.end(); ++he_it) {

            HalfFaceHandle ahfh = get_adjacent_halfface(ordered_halffaces[0], *he_it, _halffaces);
126
            if(ahfh == TopologyKernel::InvalidHalfFaceHandle) {
127
128
129
130
131
132
133
134
135
                std::cerr << "The current halfface is invalid!" << std::endl;
                continue;
            }
            ordered_halffaces[orderTop[idx]] = ahfh;
            ++idx;
        }

        // Now set bottom-halfface
        HalfFaceHandle cur_hf = ordered_halffaces[0];
136
        HalfEdgeHandle cur_he = *(TopologyKernel::halfface(cur_hf).halfedges().begin());
137
        cur_hf = get_adjacent_halfface(cur_hf, cur_he, _halffaces);
138
139
140
        cur_he = TopologyKernel::opposite_halfedge_handle(cur_he);
        cur_he = TopologyKernel::next_halfedge_in_halfface(cur_he, cur_hf);
        cur_he = TopologyKernel::next_halfedge_in_halfface(cur_he, cur_hf);
141
142
        cur_hf = get_adjacent_halfface(cur_hf, cur_he, _halffaces);

143
        if(cur_hf != TopologyKernel::InvalidHalfFaceHandle) {
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
            ordered_halffaces[1] = cur_hf;
        } else {
            std::cerr << "The current halfface is invalid!" << std::endl;
        }

    } else {
        // Just copy the original ones
        ordered_halffaces = _halffaces;
    }

    // Now check if faces are in right order
    /*
     * The test works as follows: Test for both the first and second face in the list,
     * whether the following order holds (clockwise):
     *
     * View from above, outside.
     *           ____
     *          | 4  |
     *      ____|____|____
     *     | 5  | 1  | 6  |
     *     |____|____|____|
     *          | 3  |
     *          |____|
     *
     * View from below, outside.
     *           ____
     *          | 3  |
     *      ____|____|____
     *     | 5  | 2  | 6  |
     *     |____|____|____|
     *          | 4  |
     *          |____|
     */

    HalfFaceHandle hfhTop = ordered_halffaces[0];
    HalfFaceHandle hfhBot = ordered_halffaces[1];

181
182
    std::vector<HalfEdgeHandle> halfedgesTop = TopologyKernel::halfface(ordered_halffaces[0]).halfedges();
    std::vector<HalfEdgeHandle> halfedgesBot = TopologyKernel::halfface(ordered_halffaces[1]).halfedges();
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

    int offsetTop = -1;
    int offsetBot = -1;

    // Traverse halfedges top
    for(typename std::vector<HalfEdgeHandle>::const_iterator it = halfedgesTop.begin();
            it != halfedgesTop.end(); ++it) {

        HalfFaceHandle ahfh = get_adjacent_halfface(hfhTop, *it, ordered_halffaces);

        if(offsetTop == -1) {
            if(ahfh == ordered_halffaces[2])       offsetTop = 0;
            else if(ahfh == ordered_halffaces[4])  offsetTop = 1;
            else if(ahfh == ordered_halffaces[3])  offsetTop = 2;
            else if(ahfh == ordered_halffaces[5])  offsetTop = 3;
        } else {
            offsetTop = (offsetTop + 1) % 4;
            if(ahfh != ordered_halffaces[orderTop[offsetTop]]) {
                std::cerr << "Faces not in right order!" << std::endl;
202
                return TopologyKernel::InvalidCellHandle;
203
204
205
206
207
208
            }
        }
    }

    if(offsetTop == -1) {
        std::cerr << "Faces not in right order!" << std::endl;
209
        return TopologyKernel::InvalidCellHandle;
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    }

    // Traverse halfedges bottom
    for(typename std::vector<HalfEdgeHandle>::const_iterator it = halfedgesBot.begin();
            it != halfedgesBot.end(); ++it) {

        HalfFaceHandle ahfh = get_adjacent_halfface(hfhBot, *it, ordered_halffaces);

        if(offsetBot == -1) {
            if(ahfh == ordered_halffaces[3])       offsetBot = 0;
            else if(ahfh == ordered_halffaces[4])  offsetBot = 1;
            else if(ahfh == ordered_halffaces[2])  offsetBot = 2;
            else if(ahfh == ordered_halffaces[5])  offsetBot = 3;
        } else {
            offsetBot = (offsetBot + 1) % 4;
            if(ahfh != ordered_halffaces[orderBot[offsetBot]]) {
                std::cerr << "Faces not in right order!" << std::endl;
227
                return TopologyKernel::InvalidCellHandle;
228
229
230
231
232
233
            }
        }
    }

    if(offsetBot == -1) {
        std::cerr << "Faces not in right order!" << std::endl;
234
        return TopologyKernel::InvalidCellHandle;
235
236
    }

237
    return TopologyKernel::add_cell(ordered_halffaces, _topologyCheck);
238
239
240
241
}

//========================================================================================

242

243
const HalfFaceHandle&
244
HexahedralMeshTopologyKernel::get_adjacent_halfface(const HalfFaceHandle& _hfh, const HalfEdgeHandle& _heh,
245
246
247
248
        const std::vector<HalfFaceHandle>& _halffaces) const {

    // Search for halfface that is incident to the opposite
    // halfedge of _heh
249
    HalfEdgeHandle o_he = TopologyKernel::opposite_halfedge_handle(_heh);
250
251
252
253

    for(typename std::vector<HalfFaceHandle>::const_iterator it = _halffaces.begin();
            it != _halffaces.end(); ++it) {
        if(*it == _hfh) continue;
254
        std::vector<HalfEdgeHandle> halfedges = TopologyKernel::halfface(*it).halfedges();
255
256
257
258
259
260
        for(typename std::vector<HalfEdgeHandle>::const_iterator h_it = halfedges.begin();
                h_it != halfedges.end(); ++h_it) {
            if(*h_it == o_he) return *it;
        }
    }

261
    return TopologyKernel::InvalidHalfFaceHandle;
262
263
264
}

} // Namespace OpenVolumeMesh