HexahedralMeshIterators.cc 11.1 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 47 48 49 50 51 52 53
/*===========================================================================*\
 *                                                                           *
 *                            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 <set>

#include "HexahedralMeshIterators.hh"
#include "HexahedralMeshTopologyKernel.hh"

namespace OpenVolumeMesh {

//================================================================================================
// CellSheetCellIter
//================================================================================================

54 55

CellSheetCellIter::CellSheetCellIter(const CellHandle& _ref_h,
Max Lyon's avatar
Max Lyon committed
56 57
        const unsigned char _orthDir, const HexahedralMeshTopologyKernel* _mesh, int _max_laps) :
BaseIter(_mesh, _ref_h, _max_laps) {
58

59
    if(!_mesh->has_face_bottom_up_incidences()) {
60
#ifndef NDEBUG
61
        std::cerr << "This iterator needs bottom-up incidences!" << std::endl;
62
#endif
63 64 65 66 67 68
        BaseIter::valid(false);
        return;
    }

	// First off, get all surrounding cells
	std::vector<HalfFaceHandle> halffaces = _mesh->cell(_ref_h).halffaces();
69
	for(std::vector<HalfFaceHandle>::const_iterator hf_it = halffaces.begin();
70 71 72 73 74
			hf_it != halffaces.end(); ++hf_it) {
		// Add those, that are perpendicular to the specified _orthDir
		if(_mesh->orientation(*hf_it, _ref_h) != _orthDir &&
				_mesh->orientation(*hf_it, _ref_h) != _mesh->opposite_orientation(_orthDir)) {
			CellHandle ch = _mesh->incident_cell(_mesh->opposite_halfface_handle(*hf_it));
75
			if(ch != TopologyKernel::InvalidCellHandle) {
Max Lyon's avatar
Max Lyon committed
76
                neighb_sheet_cell_hs_.push_back(ch);
77 78 79 80
			}
		}
	}

Max Lyon's avatar
Max Lyon committed
81 82 83 84 85 86
    // Remove all duplicate entries
    std::sort(neighb_sheet_cell_hs_.begin(), neighb_sheet_cell_hs_.end());
    neighb_sheet_cell_hs_.resize(std::unique(neighb_sheet_cell_hs_.begin(), neighb_sheet_cell_hs_.end()) - neighb_sheet_cell_hs_.begin());

    cur_index_ = 0;
    BaseIter::valid(neighb_sheet_cell_hs_.size() > 0);
87
	if(BaseIter::valid()) {
Max Lyon's avatar
Max Lyon committed
88
        BaseIter::cur_handle(neighb_sheet_cell_hs_[cur_index_]);
89 90 91
	}
}

92 93

CellSheetCellIter& CellSheetCellIter::operator--() {
94

Max Lyon's avatar
Max Lyon committed
95 96 97 98 99
    if (cur_index_ == 0) {
        cur_index_ = neighb_sheet_cell_hs_.size() - 1;
        --lap_;
        if (lap_ < 0)
            BaseIter::valid(false);
100
    } else {
Max Lyon's avatar
Max Lyon committed
101
        --cur_index_;
102
    }
Max Lyon's avatar
Max Lyon committed
103 104 105

    BaseIter::cur_handle(neighb_sheet_cell_hs_[cur_index_]);

106 107 108
    return *this;
}

109 110

CellSheetCellIter& CellSheetCellIter::operator++() {
111

Max Lyon's avatar
Max Lyon committed
112 113 114 115 116 117
    ++cur_index_;
    if(cur_index_ == neighb_sheet_cell_hs_.size()) {
        cur_index_ = 0;
        ++lap_;
        if (lap_ >= max_laps_)
            BaseIter::valid(false);
118
	}
Max Lyon's avatar
Max Lyon committed
119 120 121 122

    BaseIter::cur_handle(neighb_sheet_cell_hs_[cur_index_]);

    return *this;
123 124 125 126 127 128
}

//================================================================================================
// HalfFaceSheetHalfFaceIter
//================================================================================================

129 130

HalfFaceSheetHalfFaceIter::HalfFaceSheetHalfFaceIter(const HalfFaceHandle& _ref_h,
Max Lyon's avatar
Max Lyon committed
131 132
        const HexahedralMeshTopologyKernel* _mesh, int _max_laps) :
BaseIter(_mesh, _ref_h, _max_laps) {
133

134
	if(!_mesh->has_face_bottom_up_incidences()) {
135
#ifndef NDEBUG
136
        std::cerr << "This iterator needs bottom-up incidences!" << std::endl;
137
#endif
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        BaseIter::valid(false);
        return;
    }

	/*
	 * Each halfface uniquely belongs to either a cell
	 * or the boundary. If the halfface belongs
	 * to a cell, it suffices to determine the local axis
	 * the halfface represents w.r.t. the cell and to
	 * iterate over all neighboring cells orthogonal to
	 * this direction. We have to find those halffaces
	 * of the neighboring cells that contain exactly one
	 * of the initial halfface's opposite halfedges.
	 */

	if(_mesh->is_boundary(_ref_h)) {
154
#ifndef NDEBUG
155
		std::cerr << "HalfFaceSheetHalfFaceIter: HalfFace is boundary!" << std::endl;
156
#endif
157 158 159 160 161 162 163 164 165 166
		BaseIter::valid(false);
        return;
	}

	CellHandle ch = _mesh->incident_cell(_ref_h);
	unsigned char orientation = _mesh->orientation(_ref_h, ch);
	std::vector<HalfEdgeHandle> hes_v = _mesh->opposite_halfface(_mesh->halfface(_ref_h)).halfedges();
	std::set<HalfEdgeHandle> hes;
	hes.insert(hes_v.begin(), hes_v.end());

167
	for(CellSheetCellIter csc_it = _mesh->csc_iter(ch, orientation);
168 169 170
			csc_it.valid(); ++csc_it) {

		std::vector<HalfFaceHandle> hfs = _mesh->cell(*csc_it).halffaces();
171
		for(std::vector<HalfFaceHandle>::const_iterator hf_it = hfs.begin();
172 173 174
				hf_it != hfs.end(); ++hf_it) {

			std::vector<HalfEdgeHandle> hf_hes = _mesh->halfface(*hf_it).halfedges();
175
			for(std::vector<HalfEdgeHandle>::const_iterator he_it = hf_hes.begin();
176 177 178 179 180 181 182 183 184 185 186 187
					he_it != hf_hes.end(); ++he_it) {

				if(hes.count(*he_it) > 0) {
					// Found halfface that lies on the same sheet
					adjacent_halffaces_.push_back(*hf_it);
					common_edges_.push_back(_mesh->edge_handle(*he_it));
					break;
				}
			}
		}
	}

Max Lyon's avatar
Max Lyon committed
188 189
    cur_index_ = 0;
    BaseIter::valid(adjacent_halffaces_.size() > 0);
190
    if(BaseIter::valid()) {
Max Lyon's avatar
Max Lyon committed
191
        BaseIter::cur_handle(adjacent_halffaces_[cur_index_]);
192 193 194
    }
}

195 196

HalfFaceSheetHalfFaceIter& HalfFaceSheetHalfFaceIter::operator--() {
197

Max Lyon's avatar
Max Lyon committed
198 199 200 201 202 203 204 205 206 207 208
    if (cur_index_ == 0) {
        cur_index_ = adjacent_halffaces_.size() - 1;
        --lap_;
        if (lap_ < 0)
            BaseIter::valid(false);
    } else {
        --cur_index_;
    }

    BaseIter::cur_handle(adjacent_halffaces_[cur_index_]);

209 210 211
    return *this;
}

212 213

HalfFaceSheetHalfFaceIter& HalfFaceSheetHalfFaceIter::operator++() {
214

Max Lyon's avatar
Max Lyon committed
215 216 217 218 219 220 221 222 223 224 225
    ++cur_index_;
    if(cur_index_ == adjacent_halffaces_.size()) {
        cur_index_ = 0;
        ++lap_;
        if (lap_ >= max_laps_)
            BaseIter::valid(false);
    }

    BaseIter::cur_handle(adjacent_halffaces_[cur_index_]);

    return *this;
226 227
}

228 229 230 231 232 233
//================================================================================================
// HexVertexIter
//================================================================================================


HexVertexIter::HexVertexIter(const CellHandle& _ref_h,
Max Lyon's avatar
Max Lyon committed
234 235
        const HexahedralMeshTopologyKernel* _mesh, int _max_laps) :
BaseIter(_mesh, _ref_h, _max_laps) {
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

    assert(_ref_h.is_valid());
    assert(_mesh->cell(_ref_h).halffaces().size() == 6);

    // Get first half-face
    HalfFaceHandle curHF = *_mesh->cell(_ref_h).halffaces().begin();
    assert(curHF.is_valid());

    // Get first half-edge
    assert(_mesh->halfface(curHF).halfedges().size() == 4);
    HalfEdgeHandle curHE = *_mesh->halfface(curHF).halfedges().begin();
    assert(curHE.is_valid());

    vertices_.push_back(_mesh->halfedge(curHE).from_vertex());

    curHE = _mesh->prev_halfedge_in_halfface(curHE, curHF);

    vertices_.push_back(_mesh->halfedge(curHE).from_vertex());

    curHE = _mesh->prev_halfedge_in_halfface(curHE, curHF);

    vertices_.push_back(_mesh->halfedge(curHE).from_vertex());

    curHE = _mesh->prev_halfedge_in_halfface(curHE, curHF);

    vertices_.push_back(_mesh->halfedge(curHE).from_vertex());

    curHE = _mesh->prev_halfedge_in_halfface(curHE, curHF);
    curHF = _mesh->adjacent_halfface_in_cell(curHF, curHE);
    curHE = _mesh->opposite_halfedge_handle(curHE);
    curHE = _mesh->next_halfedge_in_halfface(curHE, curHF);
    curHE = _mesh->next_halfedge_in_halfface(curHE, curHF);
    curHF = _mesh->adjacent_halfface_in_cell(curHF, curHE);
    curHE = _mesh->opposite_halfedge_handle(curHE);

    vertices_.push_back(_mesh->halfedge(curHE).to_vertex());

    curHE = _mesh->prev_halfedge_in_halfface(curHE, curHF);

    vertices_.push_back(_mesh->halfedge(curHE).to_vertex());

    curHE = _mesh->prev_halfedge_in_halfface(curHE, curHF);

    vertices_.push_back(_mesh->halfedge(curHE).to_vertex());

    vertices_.push_back(_mesh->halfedge(curHE).from_vertex());

Max Lyon's avatar
Max Lyon committed
283 284
    cur_index_ = 0;
    BaseIter::valid(vertices_.size() > 0);
285
    if(BaseIter::valid()) {
Max Lyon's avatar
Max Lyon committed
286
        BaseIter::cur_handle(vertices_[cur_index_]);
287 288 289 290 291 292
    }
}


HexVertexIter& HexVertexIter::operator--() {

Max Lyon's avatar
Max Lyon committed
293 294 295 296 297
    if (cur_index_ == 0) {
        cur_index_ = vertices_.size() - 1;
        --lap_;
        if (lap_ < 0)
            BaseIter::valid(false);
298
    } else {
Max Lyon's avatar
Max Lyon committed
299
        --cur_index_;
300
    }
Max Lyon's avatar
Max Lyon committed
301 302 303

    BaseIter::cur_handle(vertices_[cur_index_]);

304 305 306 307 308 309
    return *this;
}


HexVertexIter& HexVertexIter::operator++() {

Max Lyon's avatar
Max Lyon committed
310 311 312 313 314 315
    ++cur_index_;
    if(cur_index_ == vertices_.size()) {
        cur_index_ = 0;
        ++lap_;
        if (lap_ >= max_laps_)
            BaseIter::valid(false);
316
    }
Max Lyon's avatar
Max Lyon committed
317 318 319

    BaseIter::cur_handle(vertices_[cur_index_]);

320 321 322
    return *this;
}

323
} // Namespace OpenVolumeMesh