File: contourLine.h

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-11.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 239,928 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; javascript: 164; makefile: 88
file content (327 lines) | stat: -rw-r--r-- 8,396 bytes parent folder | download | duplicates (8)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#ifndef BALL_DATATYPE_CONTOURLINE_H
#define BALL_DATATYPE_CONTOURLINE_H

#ifndef BALL_COMMON_H
# include <BALL/common.h>
#endif

#ifndef BALL_DATATYPE_REGULARDATA2D_H
# include <BALL/DATATYPE/regularData2D.h>
#endif

#include <vector>

namespace BALL
{
  // First I define some macros needed for the marching cube-algorithm. 
	// The names come from the number associated with the different corners of the square.
  #define INTERPOL12 { \
						vec = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x, act_cell_y)));\
						d1  = from[act_cell_x + act_cell_y*(number_of_cells_x+1)];\
						d2  = from[act_cell_x + 1 + act_cell_y*(number_of_cells_x+1)];\
						vec2 = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x + 1, act_cell_y + 1)));\
						slope = (d2 - d1) / (vec2.x - vec.x);\
						vec.x += (threshold - d1)/slope;\
						data_.push_back(vec);\
  } 

  #define INTERPOL18 { \
						vec = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x, act_cell_y)));\
						d1  = from[act_cell_x + act_cell_y*(number_of_cells_x+1)];\
						d2  = from[act_cell_x + (act_cell_y+1)*(number_of_cells_x+1)];\
						vec2 = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x, act_cell_y+1)));\
						slope = (d2 - d1) / (vec2.y - vec.y);\
						vec.y += (threshold - d1)/slope;\
						data_.push_back(vec);\
  }

  #define INTERPOL24 {  \
            vec = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x+1, act_cell_y)));\
            d1  = from[act_cell_x+1 + act_cell_y*(number_of_cells_x+1)];\
            d2  = from[act_cell_x+1 + (act_cell_y+1)*(number_of_cells_x+1)];\
            vec2 = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x+1, act_cell_y+1)));\
            slope = (d2 - d1) / (vec2.y - vec.y);\
            vec.y += (threshold - d1)/slope;\
            data_.push_back(vec);\
  }

	// is it vec.x += or vec.y += ...?
  #define INTERPOL48 {  \
				    vec = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x+1, act_cell_y+1)));\
            d1  = from[act_cell_x+1 + (act_cell_y+2)*(number_of_cells_x+1)];\
            d2  = from[act_cell_x   + (act_cell_y+1)*(number_of_cells_x+1)];\
            vec2 = from.getCoordinates(from.getClosestIndex(Vector2(act_cell_x, act_cell_y+1)));\
            slope = (d2 - d1) / (vec2.x - vec.x);\
            vec.x += (threshold - d1)/slope;\
						data_.push_back(vec);\
  }


  /** This class is intended to store a single contour line generated from a RegularData2D - class.
      \ingroup DatatypeMiscellaneous   
  */
  template <typename T>  
  class TContourLine
  {
    public:

      /** @name Type definitions
       */
      //@{
      
      /** The point type.
	        This type is used to store points in the 2-d regularData.
      */
      typedef Vector2 PointType;
      
      /** The vector type.
	        This type is used to store the endpoints of the contour-line.
      */
      typedef std::vector<PointType> VectorType;
      //@}

      /** @name Constructors and Destructors.
       */
      //@{
      
      /// Default constructor
      TContourLine(T height = 0);

      /// Copy constructor
      TContourLine(const TContourLine& copyTContourLine);

      /// Destructor
      virtual ~TContourLine();
      //@}

      /// Creates a contour line from a given data set.
      void createContourLine(TRegularData2D<T>& from);

      /// Internal functions used for the marching cube-algorithm.
      void interpol12();
      void interpol18();
      void interpol24();
      void interpol48();

      /** @name Assignment
       */
      //@{
      
      /// Assignment operator
      const TContourLine& operator = (const TContourLine& assigTContourLine);

      /// Clear method
      virtual void clear();
      //@}

      /** @name Predicates
       */
      //@{

      /// Equality operator
      bool operator == (const TContourLine& compTContourLine) const;

      //@}

      /** @name Accessors
       */
      //@{

      /** Return the next endpoint.
       */
      bool getNextPoint(PointType &p);

      /** Reset the counter.
       */
      void resetCounter();

      //@}

      //  private:
      T height_;
			VectorType data_;
			typename VectorType::iterator it_;
			Position index_;
    };

    /** Default type
     */
    typedef TContourLine<float> ContourLine;

    template <typename T>
      TContourLine<T>::TContourLine(T height)
			: height_(height), 
				index_(0)
    {
    }

    template <typename T>
    TContourLine<T>::~TContourLine()
    {
    }

    template <typename T>
    TContourLine<T>::TContourLine(const TContourLine<T>& from)
      : height_(from.height_),
        data_(from.data_),
				it_(from.it_),
				index_(from.index_)
    {
    }

    template <typename T>
    void TContourLine<T>::clear()
    {
      data_.clear();
      it_=data_.begin();
      index_ = 0;
    }

    template <typename T>
    const TContourLine<T>& TContourLine<T>::operator = (const TContourLine<T>& data)
    {
      data_ = data.data_;
      height_ = data.height_;
      it_ = data.it_;
      index_ = data.index_;

			return *this;
    }

    template <typename T>
    bool TContourLine<T>:: operator == (const TContourLine<T>& data) const
    {
      return ((height_    == data.height_)
               && (data_  == data.data_)
							 && (it_    == data.it_)
							 && (index_ == data.index_));
    }

    template <typename T>
    void TContourLine<T>::createContourLine(TRegularData2D<T>& from)
    {
      // This function uses a "marching cubes"-style algorithm to determine the contour-lines.
      //Size number_of_cells;
      Size number_of_cells_x;
      Size number_of_cells_y;
      Position act_cell_x;
      Position act_cell_y;
      PointType vec, vec2;
      double d1, d2, slope;
      double threshold = height_;

      number_of_cells_x = (Size) from.getSize().x - 1;
      number_of_cells_y = (Size) from.getSize().y - 1;
      
      for (act_cell_y = 0; act_cell_y < number_of_cells_y; act_cell_y++)
			{
				for (act_cell_x = 0; act_cell_x < number_of_cells_x; act_cell_x++)
				{
					// First we have to find out the topology of the actual square.
					int topology = 0;
					
					if (from[act_cell_x + act_cell_y * (number_of_cells_x+1)] > threshold)
					{
						topology |= 1;
					}
					if (from[act_cell_x + 1 + act_cell_y * (number_of_cells_x+1)] > threshold)
					{
						topology |= 2;
					}
					if (from[act_cell_x + 1 + (act_cell_y + 1)*(number_of_cells_x + 1)] > threshold)
					{
						topology |= 4;
					}
					if (from[act_cell_x + (act_cell_y + 1) * (number_of_cells_x + 1)] > threshold)
					{
						topology |= 8;
					}
					// now we can use this information to compute the contour-line.
					switch (topology)
					{
						// no cut of contour-line here
						case 0  :
						case 15 : break;
							
						// Line from upper left to lower right
						case 1  : 
						case 14 : INTERPOL18
											INTERPOL12
											break;
						
						case 4  :
						case 11 : INTERPOL48
											INTERPOL24
											break;
						
						// Line from upper right to lower left
						case 2  :
						case 13 : INTERPOL12
											INTERPOL24
											break;

						case 8  :
						case 7  : INTERPOL18
											INTERPOL48
											break;
						
						// Line through the middle (upwards)
						case 9  :
						case 6  : INTERPOL12
											INTERPOL48
											break;

						// Line through the middle (left to right)
						case 3  :
						case 12 : INTERPOL18
											INTERPOL24
											break;

						// Two lines from upper right to lower left
						case 10 : INTERPOL18
											INTERPOL12
											INTERPOL48
											INTERPOL24
											break;
					
						// Two lines from upper left to lower right
						case 5  : INTERPOL12
											INTERPOL24
											INTERPOL18
											INTERPOL48
											break;
					};
				}
			}
      index_ = 0;
      it_ = data_.begin();
    }

    template <typename T>
    bool TContourLine<T>::getNextPoint(typename TContourLine<T>::PointType &p)
    {
      if (index_ < data_.size()) 
			{
				p = *it_;
				index_++;
				it_++;
				return true;
			} 
			else 
			{
				return false;
			}
    }

    template <typename T>
    void TContourLine<T>::resetCounter()
    {
      it_ = data_.begin();
      index_ = 0;
    }
}
#endif