SmartTaggerT.cc 3.86 KB
Newer Older
Matthias Möller's avatar
Matthias Möller committed
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
//=============================================================================
//
//  CLASS SmartTaggerT - IMPLEMENTATION
//
//=============================================================================

#define ACG_SMARTTAGGERT_C

//== INCLUDES =================================================================

#include "SmartTaggerT.hh"

#include <iostream>
#include <limits>

//== NAMESPACES ===============================================================

namespace ACG {

//== IMPLEMENTATION ==========================================================

template <class Mesh, class EHandle, class EPHandle>
SmartTaggerT<Mesh, EHandle, EPHandle>::
SmartTaggerT(Mesh& _mesh, unsigned int _tag_range)
  : mesh_(_mesh),
    current_base_(0),
    tag_range_(_tag_range)
{
  // add new property
  mesh_.add_property(ep_tag_);
  
  // reset all tags once
  all_tags_to_zero();
}


//-----------------------------------------------------------------------------

 
template <class Mesh, class EHandle, class EPHandle>
SmartTaggerT<Mesh, EHandle, EPHandle>::
~SmartTaggerT()
{
  mesh_.remove_property(ep_tag_);
}


//-----------------------------------------------------------------------------


template <class Mesh, class EHandle, class EPHandle>
void
SmartTaggerT<Mesh, EHandle, EPHandle>::
untag_all()
{
  unsigned int max_uint = std::numeric_limits<unsigned int>::max();

  if( current_base_ < max_uint - 2*tag_range_)
    current_base_ += tag_range_;
  else
  {
    //overflow -> reset all tags
#ifdef STV_DEBUG_CHECKS
    std::cerr << "Tagging Overflow occured...\n";
#endif
    current_base_ = 0;
    all_tags_to_zero();
  }
}


//-----------------------------------------------------------------------------


template <class Mesh, class EHandle, class EPHandle>
void
SmartTaggerT<Mesh, EHandle, EPHandle>::
untag_all( const unsigned int _new_tag_range)
{
  set_tag_range(_new_tag_range);
}


//-----------------------------------------------------------------------------
  
template <class Mesh, class EHandle, class EPHandle>
void
SmartTaggerT<Mesh, EHandle, EPHandle>::
set_tag  ( const EHandle _eh, unsigned int _tag)
{
#ifdef STV_DEBUG_CHECKS
  if( _tag > tag_range_)
    std::cerr << "ERROR in set_tag tag range!!!\n";
#endif

  mesh_.property(ep_tag_, _eh) = current_base_ + _tag;
}


//-----------------------------------------------------------------------------


template <class Mesh, class EHandle, class EPHandle>
unsigned int
SmartTaggerT<Mesh, EHandle, EPHandle>::
get_tag  ( const EHandle _eh) const
{
  unsigned int t = mesh_.property(ep_tag_, _eh);

#ifdef STV_DEBUG_CHECKS
  if( t > current_base_ + tag_range_)
    std::cerr << "ERROR in get_tag tag range!!!\n";
#endif

  if( t<= current_base_) return 0;
  else                   return t-current_base_;
}


//-----------------------------------------------------------------------------


template <class Mesh, class EHandle, class EPHandle>
bool
SmartTaggerT<Mesh, EHandle, EPHandle>::
is_tagged( const EHandle _eh) const
{
  return bool(get_tag(_eh));
}


//-----------------------------------------------------------------------------


template <class Mesh, class EHandle, class EPHandle>
void
SmartTaggerT<Mesh, EHandle, EPHandle>::
set_tag_range( const unsigned int _tag_range)
{
  if( _tag_range <= tag_range_)
  {
    untag_all();
    tag_range_ = _tag_range;
  }
  else
  {
    tag_range_ = _tag_range;
    untag_all();
  }
}

  
//-----------------------------------------------------------------------------


template <class Mesh, class EHandle, class EPHandle>
void
SmartTaggerT<Mesh, EHandle, EPHandle>::
all_tags_to_zero()
{
  // iterate over property vector
  for(unsigned int i=0; i<mesh_.property(ep_tag_).n_elements(); ++i)
  {
    mesh_.property(ep_tag_)[i] = 0;
  }
}

//=============================================================================
} // namespace ACG
//=============================================================================