forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPixelExtent.cxx
346 lines (309 loc) · 7.94 KB
/
vtkPixelExtent.cxx
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPixelExtenth.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPixelExtent.h"
using std::deque;
using std::ostream;
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::Grow(
const vtkPixelExtent &inputExt,
const vtkPixelExtent &problemDomain,
int n)
{
vtkPixelExtent outputExt = vtkPixelExtent::Grow(inputExt, n);
outputExt &= problemDomain;
return outputExt;
}
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::Grow(
const vtkPixelExtent &inputExt,
int n)
{
vtkPixelExtent outputExt(inputExt);
outputExt.Grow(0, n);
outputExt.Grow(1, n);
return outputExt;
}
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::GrowLow(
const vtkPixelExtent &inputExt,
int q,
int n)
{
vtkPixelExtent outputExt(inputExt);
outputExt[2*q] -= n;
return outputExt;
}
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::GrowHigh(
const vtkPixelExtent &inputExt,
int q,
int n)
{
vtkPixelExtent outputExt(inputExt);
outputExt[2*q+1] += n;
return outputExt;
}
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::Shrink(
const vtkPixelExtent &inputExt,
int n)
{
return vtkPixelExtent::Grow(inputExt, -n);
}
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::Shrink(
const vtkPixelExtent &inputExt,
const vtkPixelExtent &problemDomain,
int n)
{
vtkPixelExtent outputExt(inputExt);
outputExt.Grow(0, -n);
outputExt.Grow(1, -n);
// don't shrink at the problem domain
// because you don't grow outside the problem domain.
for (int i=0; i<4; ++i)
{
if (inputExt[i] == problemDomain[i])
{
outputExt[i] = problemDomain[i];
}
}
return outputExt;
}
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::CellToNode(
const vtkPixelExtent &inputExt)
{
vtkPixelExtent outputExt(inputExt);
++outputExt[1];
++outputExt[3];
return outputExt;
}
//-----------------------------------------------------------------------------
vtkPixelExtent vtkPixelExtent::NodeToCell(const vtkPixelExtent &inputExt)
{
vtkPixelExtent outputExt(inputExt);
--outputExt[1];
--outputExt[3];
return outputExt;
}
//-----------------------------------------------------------------------------
void vtkPixelExtent::Shift(
int *ij,
int n)
{
ij[0] += n;
ij[1] += n;
}
//-----------------------------------------------------------------------------
void vtkPixelExtent::Shift(
int *ij,
int *n)
{
ij[0] += n[0];
ij[1] += n[1];
}
//-----------------------------------------------------------------------------
void vtkPixelExtent::Split(
int i1,
int j1,
const vtkPixelExtent &ext,
deque<vtkPixelExtent> &newExts)
{
// cell is inside, split results in as many as
// 4 new extents. check for each one.
int i0 = i1 - 1;
int j0 = j1 - 1;
int outside = 1;
// lower left
if (ext.Contains(i0, j0))
{
newExts.push_back(vtkPixelExtent(ext[0], i0, ext[2], j0));
outside = 0;
}
// lower right
if (ext.Contains(i1, j0))
{
newExts.push_back(vtkPixelExtent(i1, ext[1], ext[2], j0));
outside = 0;
}
// upper left
if (ext.Contains(i0, j1))
{
newExts.push_back(vtkPixelExtent(ext[0], i0, j1, ext[3]));
outside = 0;
}
// upper right
if (ext.Contains(i1, j1))
{
newExts.push_back(vtkPixelExtent(i1, ext[1], j1, ext[3]));
outside = 0;
}
// split cell is outside, pass through
if (outside)
{
newExts.push_back(ext);
return;
}
}
//-----------------------------------------------------------------------------
void vtkPixelExtent::Subtract(
const vtkPixelExtent &A,
const vtkPixelExtent& B,
deque<vtkPixelExtent> &C)
{
// split method requires split point inside the extent
vtkPixelExtent I(A);
I &= B;
if (I.Empty())
{
// do nothing if disjoint
C.push_back(A);
return;
}
if (B.Contains(A))
{
// if A is covered by B then remove A
return;
}
// split left and below this cells
I.CellToNode();
deque<vtkPixelExtent> tmpA0;
tmpA0.push_back(A);
for (int q=0; q<4; ++q)
{
const int ids[8] = {0,2, 1,2, 1,3, 0,3};
int qq = 2*q;
int i = I[ids[qq]];
int j = I[ids[qq+1]];
deque<vtkPixelExtent> tmpA1;
while ( !tmpA0.empty() )
{
vtkPixelExtent ext = tmpA0.back();
tmpA0.pop_back();
vtkPixelExtent::Split(i,j, ext, tmpA1);
}
tmpA0 = tmpA1;
}
// remove anything covered by B
size_t n = tmpA0.size();
for (size_t q=0; q<n; ++q)
{
const vtkPixelExtent &ext = tmpA0[q];
if (!B.Contains(ext))
{
C.push_back(ext);
}
}
}
// ----------------------------------------------------------------------------
void vtkPixelExtent::Merge(deque<vtkPixelExtent> &exts)
{
size_t ne = exts.size();
// working in point space simplifies things because
// points overlap in adjacent extents while cells do not
deque<vtkPixelExtent> tmpExts(ne);
for (size_t t=0; t<ne; ++t)
{
vtkPixelExtent ext(exts[t]);
ext.CellToNode();
tmpExts[t] = ext;
}
// one pass for each direction
for (int q=0; q<2; ++q)
{
int qq = 2*q;
// consider each extent as a target to be merged
for (size_t t=0; t<ne; ++t)
{
// if a merger occurs the merged extent is added
// as a new target with the constituents marked empty
// and the current pass is terminated early
bool nextPass = false;
// current target
vtkPixelExtent &ext0 = tmpExts[t];
if (ext0.Empty())
{
// was merged in preceding pass
continue;
}
for (size_t c=0; c<ne; ++c)
{
if (c == t)
{
// don't attempt merge with self
continue;
}
// candidate
vtkPixelExtent &ext1 = tmpExts[c];
if (ext1.Empty())
{
// was merged in preceding pass
continue;
}
// must be same size and coordinate in merge dir
if ( (ext0[qq] == ext1[qq]) && (ext0[qq+1] == ext1[qq+1]) )
{
// must overlap overlap
vtkPixelExtent ext2(ext0);
ext2 &= ext1;
if (!ext2.Empty())
{
// merge and add as new target
// in a later pass
vtkPixelExtent ext3(ext0);
ext3 |= ext1;
tmpExts.push_back(ext3);
++ne;
// mark the merged extents empty
ext0.Clear();
ext1.Clear();
// move to next target
nextPass = true;
}
}
if (nextPass)
{
break;
}
}
}
}
// discard merged targets copy to output
exts.clear();
for (size_t t=0; t<ne; ++t)
{
vtkPixelExtent &ext = tmpExts[t];
if (!ext.Empty())
{
ext.NodeToCell();
exts.push_back(ext);
}
}
}
//*****************************************************************************
ostream &operator<<(ostream &os, const vtkPixelExtent &ext)
{
if (ext.Empty())
{
os << "(empty)";
}
else
{
os
<< "("
<< ext[0] << ", "
<< ext[1] << ", "
<< ext[2] << ", "
<< ext[3] << ")";
}
return os;
}