vtkTreeCompositer.cxx 7.49 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTreeCompositer.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
9
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
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
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

// This software and ancillary information known as vtk_ext (and
// herein called "SOFTWARE") is made available under the terms
// described below.  The SOFTWARE has been approved for release with
// associated LA_CC Number 99-44, granted by Los Alamos National
// Laboratory in July 1999.
//
// Unless otherwise indicated, this SOFTWARE has been authored by an
// employee or employees of the University of California, operator of
// the Los Alamos National Laboratory under Contract No. W-7405-ENG-36
// with the United States Department of Energy.
//
// The United States Government has rights to use, reproduce, and
// distribute this SOFTWARE.  The public may copy, distribute, prepare
// derivative works and publicly display this SOFTWARE without charge,
// provided that this Notice and any statement of authorship are
// reproduced on all copies.
//
// Neither the U. S. Government, the University of California, nor the
// Advanced Computing Laboratory makes any warranty, either express or
// implied, nor assumes any liability or responsibility for the use of
// this SOFTWARE.
//
// If SOFTWARE is modified to produce derivative works, such modified
// SOFTWARE should be clearly marked, so as not to confuse it with the
// version available from Los Alamos National Laboratory.

#include "vtkTreeCompositer.h"
#include "vtkObjectFactory.h"
#include "vtkToolkits.h"
#include "vtkFloatArray.h"
#include "vtkUnsignedCharArray.h"
47
#include "vtkMultiProcessController.h"
48
49
50
51
52
53
54
55

vtkStandardNewMacro(vtkTreeCompositer);

//-------------------------------------------------------------------------
vtkTreeCompositer::vtkTreeCompositer()
{
}

56

57
58
59
60
61
62
63
64
65
//-------------------------------------------------------------------------
vtkTreeCompositer::~vtkTreeCompositer()
{
}

//-------------------------------------------------------------------------
// Jim's composite stuff
//-------------------------------------------------------------------------
// Results are put in the local data.
66
67
68
69
void vtkCompositeImagePair(vtkFloatArray *localZ,
                           vtkDataArray *localP,
                           vtkFloatArray *remoteZ,
                           vtkDataArray *remoteP)
70
71
72
73
74
75
76
77
78
79
80
81
{
  int i,j;
  int pixel_data_size;
  float *pEnd;
  int numComp = localP->GetNumberOfComponents();
  float* remoteZdata = remoteZ->GetPointer(0);
  float* remotePdata = reinterpret_cast<float*>(remoteP->GetVoidPointer(0));
  float* localZdata = localZ->GetPointer(0);
  float* localPdata = reinterpret_cast<float*>(localP->GetVoidPointer(0));

  int total_pixels = localZ->GetNumberOfTuples();
  int useCharFlag = 0;
82

83
84
85
  if (localP->GetDataType() == VTK_UNSIGNED_CHAR)
    {
    useCharFlag = 1;
86
    }
87

88
  if (useCharFlag)
89
90
91
92
    {
    pEnd = remoteZdata + total_pixels;
    if (numComp == 4)
      {
93
      while(remoteZdata != pEnd)
94
        {
95
        if (*remoteZdata < *localZdata)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
          {
          *localZdata++ = *remoteZdata++;
          *localPdata++ = *remotePdata++;
          }
        else
          {
          ++localZdata;
          ++remoteZdata;
          ++localPdata;
          ++remotePdata;
          }
        }
      }
    else if (numComp == 3)
      {
      unsigned char* clocalPdata = reinterpret_cast<unsigned char*>(localPdata);
      unsigned char* cremotePdata = reinterpret_cast<unsigned char*>(remotePdata);
113
      while(remoteZdata != pEnd)
114
        {
115
        if (*remoteZdata < *localZdata)
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
          {
          *localZdata++ = *remoteZdata++;
          *clocalPdata++ = *cremotePdata++;
          *clocalPdata++ = *cremotePdata++;
          *clocalPdata++ = *cremotePdata++;
          }
        else
          {
          ++localZdata;
          ++remoteZdata;
          clocalPdata += 3;
          cremotePdata += 3;
          }
        }
      }
131
132
    }
  else
133
134
    {
    pixel_data_size = numComp;
135
    for (i = 0; i < total_pixels; i++)
136
      {
137
      if (remoteZdata[i] < localZdata[i])
138
139
        {
        localZdata[i] = remoteZdata[i];
140
        for (j = 0; j < pixel_data_size; j++)
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
          {
          localPdata[i*pixel_data_size+j] = remotePdata[i*pixel_data_size+j];
          }
        }
      }
    }
}


#define vtkTCPow2(j) (1 << (j))

static inline int vtkTCLog2(int j, int& exact)
{
  int counter=0;
  exact = 1;
  while(j)
    {
    if ( ( j & 1 ) && (j >> 1) )
      {
      exact = 0;
      }
    j = j >> 1;
    counter++;
    }
  return counter-1;
}

168
void vtkTreeCompositer::CompositeBuffer(vtkDataArray *pBuf,
Berk Geveci's avatar
Berk Geveci committed
169
                                        vtkFloatArray *zBuf,
170
                                        vtkDataArray *pTmp,
Berk Geveci's avatar
Berk Geveci committed
171
                                        vtkFloatArray *zTmp)
172
173
{
  int myId = this->Controller->GetLocalProcessId();
174
  int numProcs = this->NumberOfProcesses;
175
176
177
178
179
180
181
182
  int totalPixels;
  int pSize, zSize;
  int i, id;
  int numComp = pBuf->GetNumberOfComponents();
  int exactLog;
  int logProcs = vtkTCLog2(numProcs,exactLog);

  // not a power of 2 -- need an additional level
183
  if ( !exactLog )
184
185
186
187
188
189
190
191
192
193
194
    {
    logProcs++;
    }

  totalPixels = zBuf->GetNumberOfTuples();
  zSize = totalPixels;
  pSize = numComp*totalPixels;

#ifdef MPIPROALLOC
  vtkCommunicator::SetUseCopy(0);
#endif
195
  for (i = 0; i < logProcs; i++)
196
    {
197
    if ((myId % (int)vtkTCPow2(i)) == 0)
198
      { // Find participants
199
      if ((myId % (int)vtkTCPow2(i+1)) < vtkTCPow2(i))
200
201
202
        {
        // receivers
        id = myId+vtkTCPow2(i);
203

204
205
        // only send or receive if sender or receiver id is valid
        // (handles non-power of 2 cases)
206
        if (id < numProcs)
207
208
209
210
211
          {
          this->Controller->Receive(zTmp->GetPointer(0), zSize, id, 99);
          if (pTmp->GetDataType() == VTK_UNSIGNED_CHAR)
            {
            this->Controller->Receive(reinterpret_cast<unsigned char*>
212
                                      (pTmp->GetVoidPointer(0)),
213
214
215
216
217
                                      pSize, id, 99);
            }
          else
            {
            this->Controller->Receive(reinterpret_cast<float*>
218
                                      (pTmp->GetVoidPointer(0)),
219
220
                                      pSize, id, 99);
            }
221

222
223
224
225
          // notice the result is stored as the local data
          vtkCompositeImagePair(zBuf, pBuf, zTmp, pTmp);
          }
        }
226
      else
227
228
        {
        id = myId-vtkTCPow2(i);
229
        if (id < numProcs)
230
231
232
233
234
          {
          this->Controller->Send(zBuf->GetPointer(0), zSize, id, 99);
          if (pBuf->GetDataType() == VTK_UNSIGNED_CHAR)
            {
            this->Controller->Send(reinterpret_cast<unsigned char*>
235
                                   (pBuf->GetVoidPointer(0)),
236
237
238
239
240
                                   pSize, id, 99);
            }
          else
            {
            this->Controller->Send(reinterpret_cast<float*>
241
                                   (pBuf->GetVoidPointer(0)),
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
                                   pSize, id, 99);
            }
          }
        }
      }
    }

#ifdef MPIPROALLOC
  vtkCommunicator::SetUseCopy(1);
#endif

}

void vtkTreeCompositer::PrintSelf(ostream& os, vtkIndent indent)
{
257
  this->Superclass::PrintSelf(os, indent);
258
259
260
261
}