Commit 39ab4a00 authored by Mike Kremer's avatar Mike Kremer

Cache number of entities for faster access.

git-svn-id: http://www.openvolumemesh.org/svnrepo/OpenVolumeMesh/trunk@180 66977474-1d4b-4f09-8fe9-267525286df2
parent b3669c1c
......@@ -89,13 +89,8 @@ public:
return vertices_[_vh];
}
/// Override of n_vertices()
virtual unsigned int n_vertices() const {
return vertices_.size();
}
virtual VertexIter delete_vertex(const VertexHandle& _h) {
assert(_h.idx() < (int)n_vertices());
assert(_h.idx() < (int)TopologyKernel::n_vertices());
VertexIter nV = TopologyKernelT::delete_vertex(_h);
......
......@@ -56,10 +56,13 @@ const HalfFaceHandle TopologyKernel::InvalidHalfFaceHandle = HalfFaceHandle(-
const CellHandle TopologyKernel::InvalidCellHandle = CellHandle(-1);
TopologyKernel::TopologyKernel() :
n_vertices_(0u),
n_edges_(0u),
n_faces_(0u),
n_cells_(0u),
v_bottom_up_(true),
e_bottom_up_(true),
f_bottom_up_(true),
n_vertices_(0u) {
f_bottom_up_(true) {
}
......@@ -96,7 +99,7 @@ EdgeHandle TopologyKernel::add_edge(const VertexHandle& _fromVertex,
}
// Test if edge does not exist, yet
for(unsigned int i = 0; i < edges_.size(); ++i) {
for(unsigned int i = 0; i < n_edges_; ++i) {
if(edge(EdgeHandle(i)).from_vertex() == _fromVertex && edge(EdgeHandle(i)).to_vertex() == _toVertex) {
return EdgeHandle(i);
} else if(edge(EdgeHandle(i)).from_vertex() == _toVertex && edge(EdgeHandle(i)).to_vertex() == _fromVertex) {
......@@ -109,11 +112,12 @@ EdgeHandle TopologyKernel::add_edge(const VertexHandle& _fromVertex,
// Store edge locally
edges_.push_back(e);
++n_edges_;
// Resize props
resize_eprops(n_edges());
EdgeHandle eh((int)edges_.size()-1);
EdgeHandle eh((int)n_edges_-1);
// Update vertex bottom-up adjacencies
if(v_bottom_up_) {
......@@ -140,7 +144,7 @@ FaceHandle TopologyKernel::add_face(const std::vector<HalfEdgeHandle>& _halfedge
// Test if all edges are valid
for(std::vector<HalfEdgeHandle>::const_iterator it = _halfedges.begin(),
end = _halfedges.end(); it != end; ++it) {
if((unsigned int)*it >= edges_.size() * 2u) {
if((unsigned int)*it >= n_edges_ * 2u) {
std::cerr << "Halfedge handle out of bounds!" << std::endl;
return InvalidFaceHandle;
}
......@@ -186,9 +190,10 @@ FaceHandle TopologyKernel::add_face(const std::vector<HalfEdgeHandle>& _halfedge
OpenVolumeMeshFace face(_halfedges);
faces_.push_back(face);
++n_faces_;
// Get added face's handle
FaceHandle fh(faces_.size() - 1);
FaceHandle fh(n_faces_ - 1);
// Resize props
resize_fprops(n_faces());
......@@ -347,7 +352,7 @@ CellHandle TopologyKernel::add_cell(const std::vector<HalfFaceHandle>& _halfface
// Test if halffaces have valid indices
for(std::vector<HalfFaceHandle>::const_iterator it = _halffaces.begin(),
end = _halffaces.end(); it != end; ++it) {
if((unsigned int)*it >= faces_.size() * 2u) {
if((unsigned int)*it >= n_faces_ * 2u) {
std::cerr << "HalfFace handle is out of bounds!" << std::endl;
return InvalidCellHandle;
}
......@@ -390,11 +395,12 @@ CellHandle TopologyKernel::add_cell(const std::vector<HalfFaceHandle>& _halfface
OpenVolumeMeshCell cell(_halffaces);
cells_.push_back(cell);
++n_cells_;
// Resize props
resize_cprops(n_cells());
CellHandle ch((int)cells_.size()-1);
CellHandle ch((int)n_cells_-1);
// Update face bottom-up adjacencies
if(f_bottom_up_) {
......@@ -555,7 +561,7 @@ VertexIter TopologyKernel::delete_vertex(const VertexHandle& _h) {
*/
EdgeIter TopologyKernel::delete_edge(const EdgeHandle& _h) {
assert(_h.idx() < (int)edges_.size());
assert(_h.idx() < (int)n_edges_);
// 1)
if(v_bottom_up_) {
......@@ -660,6 +666,7 @@ EdgeIter TopologyKernel::delete_edge(const EdgeHandle& _h) {
// 5)
edges_.erase(edges_.begin() + _h.idx());
--n_edges_;
// 6)
edge_deleted(_h);
......@@ -699,7 +706,7 @@ EdgeIter TopologyKernel::delete_edge(const EdgeHandle& _h) {
*/
FaceIter TopologyKernel::delete_face(const FaceHandle& _h) {
assert(_h.idx() < (int)faces_.size());
assert(_h.idx() < (int)n_faces_);
// 1)
if(e_bottom_up_) {
......@@ -799,6 +806,7 @@ FaceIter TopologyKernel::delete_face(const FaceHandle& _h) {
// 5)
faces_.erase(faces_.begin() + _h.idx());
--n_faces_;
// 6)
face_deleted(_h);
......@@ -832,7 +840,7 @@ FaceIter TopologyKernel::delete_face(const FaceHandle& _h) {
*/
CellIter TopologyKernel::delete_cell(const CellHandle& _h) {
assert(_h.idx() < (int)cells_.size());
assert(_h.idx() < (int)n_cells_);
// 1)
if(f_bottom_up_) {
......@@ -855,6 +863,7 @@ CellIter TopologyKernel::delete_cell(const CellHandle& _h) {
// 3)
cells_.erase(cells_.begin() + _h.idx());
--n_cells_;
// 4)
cell_deleted(_h);
......@@ -868,7 +877,7 @@ CellIter TopologyKernel::delete_cell(const CellHandle& _h) {
const OpenVolumeMeshEdge& TopologyKernel::edge(const EdgeHandle& _edgeHandle) const {
// Test if edge is valid
assert((unsigned int)_edgeHandle < edges_.size());
assert((unsigned int)_edgeHandle < n_edges_);
assert(_edgeHandle >= 0);
return edges_[_edgeHandle];
......@@ -880,7 +889,7 @@ const OpenVolumeMeshEdge& TopologyKernel::edge(const EdgeHandle& _edgeHandle) co
const OpenVolumeMeshFace& TopologyKernel::face(const FaceHandle& _faceHandle) const {
// Test if face is valid
assert((unsigned int)_faceHandle < faces_.size());
assert((unsigned int)_faceHandle < n_faces_);
assert(_faceHandle >= 0);
return faces_[_faceHandle];
......@@ -892,7 +901,7 @@ const OpenVolumeMeshFace& TopologyKernel::face(const FaceHandle& _faceHandle) co
const OpenVolumeMeshCell& TopologyKernel::cell(const CellHandle& _cellHandle) const {
// Test if cell is valid
assert((unsigned int)_cellHandle < cells_.size());
assert((unsigned int)_cellHandle < n_cells_);
assert(_cellHandle >= 0);
return cells_[_cellHandle];
......@@ -904,7 +913,7 @@ const OpenVolumeMeshCell& TopologyKernel::cell(const CellHandle& _cellHandle) co
OpenVolumeMeshEdge& TopologyKernel::edge(const EdgeHandle& _edgeHandle) {
// Test if edge is valid
assert((unsigned int)_edgeHandle < edges_.size());
assert((unsigned int)_edgeHandle < n_edges_);
assert(_edgeHandle >= 0);
return edges_[_edgeHandle];
......@@ -916,7 +925,7 @@ OpenVolumeMeshEdge& TopologyKernel::edge(const EdgeHandle& _edgeHandle) {
OpenVolumeMeshFace& TopologyKernel::face(const FaceHandle& _faceHandle) {
// Test if face is valid
assert((unsigned int)_faceHandle < faces_.size());
assert((unsigned int)_faceHandle < n_faces_);
assert(_faceHandle >= 0);
return faces_[_faceHandle];
......@@ -928,7 +937,7 @@ OpenVolumeMeshFace& TopologyKernel::face(const FaceHandle& _faceHandle) {
OpenVolumeMeshCell& TopologyKernel::cell(const CellHandle& _cellHandle) {
// Test if cell is valid
assert((unsigned int)_cellHandle < cells_.size());
assert((unsigned int)_cellHandle < n_cells_);
assert(_cellHandle >= 0);
return cells_[_cellHandle];
......@@ -940,7 +949,7 @@ OpenVolumeMeshCell& TopologyKernel::cell(const CellHandle& _cellHandle) {
const OpenVolumeMeshEdge TopologyKernel::halfedge(const HalfEdgeHandle& _halfEdgeHandle) const {
// Is handle in range?
assert((unsigned int)_halfEdgeHandle < (edges_.size() * 2));
assert((unsigned int)_halfEdgeHandle < (n_edges_ * 2));
assert(_halfEdgeHandle >= 0);
// In case the handle is even, just return the corresponding edge
......@@ -957,7 +966,7 @@ const OpenVolumeMeshEdge TopologyKernel::halfedge(const HalfEdgeHandle& _halfEdg
const OpenVolumeMeshFace TopologyKernel::halfface(const HalfFaceHandle& _halfFaceHandle) const {
// Is handle in range?
assert((unsigned int)_halfFaceHandle < (faces_.size() * 2));
assert((unsigned int)_halfFaceHandle < (n_faces_ * 2));
assert(_halfFaceHandle >= 0);
// In case the handle is not even, just return the corresponding face
......@@ -975,7 +984,7 @@ const OpenVolumeMeshEdge TopologyKernel::opposite_halfedge(const HalfEdgeHandle&
// Is handle in range?
assert(_halfEdgeHandle >= 0);
assert((unsigned int)_halfEdgeHandle < (edges_.size() * 2));
assert((unsigned int)_halfEdgeHandle < (n_edges_ * 2));
// In case the handle is not even, just return the corresponding edge
// Otherwise return the opposite halfedge via opposite()
......@@ -992,7 +1001,7 @@ const OpenVolumeMeshFace TopologyKernel::opposite_halfface(const HalfFaceHandle&
// Is handle in range?
assert(_halfFaceHandle >= 0);
assert((unsigned int)_halfFaceHandle < (faces_.size() * 2));
assert((unsigned int)_halfFaceHandle < (n_faces_ * 2));
// In case the handle is not even, just return the corresponding face
// Otherwise return the opposite via the first face's opposite() function
......@@ -1065,8 +1074,8 @@ const HalfFaceHandle TopologyKernel::halfface(const std::vector<HalfEdgeHandle>&
const HalfEdgeHandle TopologyKernel::next_halfedge_in_halfface(const HalfEdgeHandle& _heh, const HalfFaceHandle& _hfh) const {
assert((unsigned int)_hfh < faces_.size() * 2u);
assert((unsigned int)_heh < edges_.size() * 2u);
assert((unsigned int)_hfh < n_faces_ * 2u);
assert((unsigned int)_heh < n_edges_ * 2u);
std::vector<HalfEdgeHandle> hes = halfface(_hfh).halfedges();
......@@ -1085,8 +1094,8 @@ const HalfEdgeHandle TopologyKernel::next_halfedge_in_halfface(const HalfEdgeHan
const HalfEdgeHandle TopologyKernel::prev_halfedge_in_halfface(const HalfEdgeHandle& _heh, const HalfFaceHandle& _hfh) const {
assert((unsigned int)_hfh < faces_.size() * 2u);
assert((unsigned int)_heh < edges_.size() * 2u);
assert((unsigned int)_hfh < n_faces_ * 2u);
assert((unsigned int)_heh < n_edges_ * 2u);
std::vector<HalfEdgeHandle> hes = halfface(_hfh).halfedges();
......@@ -1169,7 +1178,7 @@ void TopologyKernel::compute_vertex_bottom_up_adjacencies() {
outgoing_hes_per_vertex_.resize(n_vertices());
// Store outgoing halfedges per vertex
unsigned int n_vertices = edges_.size();
unsigned int n_vertices = n_edges_;
for(unsigned int i = 0; i < n_vertices; ++i) {
VertexHandle from = edges_[i].from_vertex();
......@@ -1195,10 +1204,10 @@ void TopologyKernel::compute_edge_bottom_up_adjacencies() {
// Clear
incident_hfs_per_he_.clear();
incident_hfs_per_he_.resize(edges_.size() * 2u);
incident_hfs_per_he_.resize(n_edges_ * 2u);
// Store incident halffaces per halfedge
unsigned int n_faces = faces_.size();
unsigned int n_faces = n_faces_;
for(unsigned int i = 0; i < n_faces; ++i) {
std::vector<HalfEdgeHandle> halfedges = faces_[i].halfedges();
......@@ -1220,9 +1229,9 @@ void TopologyKernel::compute_face_bottom_up_adjacencies() {
// Clear
incident_cell_per_hf_.clear();
incident_cell_per_hf_.resize(faces_.size() * 2u, InvalidCellHandle);
incident_cell_per_hf_.resize(n_faces_ * 2u, InvalidCellHandle);
unsigned int n_cells = cells_.size();
unsigned int n_cells = n_cells_;
for(unsigned int i = 0; i < n_cells; ++i) {
std::vector<HalfFaceHandle> halffaces = cells_[i].halffaces();
......
......@@ -206,15 +206,28 @@ public:
/// Get number of vertices in mesh
virtual unsigned int n_vertices() const { return n_vertices_; }
/// Get number of edges in mesh
virtual unsigned int n_edges() const { return edges_.size(); }
virtual unsigned int n_edges() const { return n_edges_; }
/// Get number of halfedges in mesh
virtual unsigned int n_halfedges() const { return (2u * edges_.size()); }
virtual unsigned int n_halfedges() const { return n_edges_ * 2u; }
/// Get number of faces in mesh
virtual unsigned int n_faces() const { return faces_.size(); }
virtual unsigned int n_faces() const { return n_faces_; }
/// Get number of halffaces in mesh
virtual unsigned int n_halffaces() const { return (2u * faces_.size()); }
virtual unsigned int n_halffaces() const { return n_faces_ * 2u; }
/// Get number of cells in mesh
virtual unsigned int n_cells() const { return cells_.size(); }
virtual unsigned int n_cells() const { return n_cells_; }
private:
// Cache total entity numbers
unsigned int n_vertices_;
unsigned int n_edges_;
unsigned int n_faces_;
unsigned int n_cells_;
public:
/// Add abstract vertex
virtual VertexHandle add_vertex();
......@@ -621,9 +634,6 @@ public:
protected:
// Number of (abstract) vertices
unsigned int n_vertices_;
// List of edges
std::vector<Edge> edges_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment