[go: up one dir, main page]

Menu

[0a098c]: / data / PadMap.cc  Maximize  Restore  History

Download this file

197 lines (167 with data), 3.6 kB

  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
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include "PadMap.h"
PadMap::PadMap(const char *fname):
_nx(0), _ny(0), _ntot(0),
_pad2chan(0), _chan2pad(0), _connected(0)
{
read_map(fname);
}
void PadMap::read_map(const char *fname)
{
clear();
std::ifstream ifile(fname);
if (!ifile)
{
std::cout << "**** PadMap: cannot open file " << fname << std::endl;
exit(1);
}
ifile >> _nx >> std::ws >> _ny >> std::ws;
_ntot = _nx * _ny;
int i, k, kmx = -1;
std::vector<int> iv;
std::vector<int>::iterator ip;
while (1)
{
ifile >> k;
if (!ifile || ifile.eof())
break;
if (kmx < k)
kmx = k;
if (k>=0)
iv.push_back(k);
}
kmx++;
if (kmx > _ntot)
_ntot = kmx;
init();
for (i = 0, ip = iv.begin();ip != iv.end();++ip, i++)
{
k = *ip;
if (k<0)
continue;
_pad2chan[i] = k;
_chan2pad[ k ] = i;
_connected[k] = 1;
}
}
PadMap::PadMap(int n, int *m, int nx, int ny):
_nx(nx), _ny(ny), _ntot(nx*ny),
_pad2chan(0), _chan2pad(0), _connected(0)
{
if (n > _ntot)
_ntot = n;
int i;
init();
for (i = 0;i < n;i++)
{
_pad2chan[i] = m[i];
_chan2pad[ m[i] ] = i;
_connected[ m[i] ] = 1;
}
}
PadMap::PadMap(int nx, int ny, int ntot):
_nx(nx), _ny(ny), _ntot(ntot),
_pad2chan(0), _chan2pad(0), _connected(0)
{
if (_ntot < 0)
_ntot = _nx * _ny;
init();
}
PadMap::PadMap(const PadMap &m) :
_pad2chan(0), _chan2pad(0), _connected(0)
{
cpy(m);
}
PadMap &PadMap::operator=(const PadMap &m)
{
if (&m!=this)
cpy(m);
return *this;
}
void PadMap::cpy(const PadMap &m)
{
int i;
clear();
_nx = m._nx;
_ny = m._ny;
_ntot = m._ntot;
_pad2chan = new int[_ntot];
_chan2pad = new int[_ntot];
_connected = new char[_ntot];
for (i = 0;i < _ntot;i++)
{
_pad2chan[i] = m._pad2chan[i];
_chan2pad[i] = m._chan2pad[i];
_connected[i] = m._connected[i];
}
}
void PadMap::clear()
{
if (_pad2chan)
delete [] _pad2chan;
if (_chan2pad )
delete [] _chan2pad;
if (_connected)
delete [] _connected;
}
void PadMap::init()
{
int i;
_pad2chan = new int[_ntot];
_chan2pad = new int[_ntot];
_connected = new char[_ntot];
for (i = 0;i < _ntot;i++)
{
_pad2chan[i] = -1;
_chan2pad[i] = -1;
_connected[i] = 0;
}
}
PadMap::~PadMap()
{
clear();
}
void PadMap::print(std::ostream &os) const
{
int ix, iy;
for (iy = 0;iy < ny();iy++)
{
for (ix = 0;ix < nx();ix++)
{
os << std::setw(5) << channel(ix, iy);
}
os << std::endl;
}
}
void PadMap::print_unconnected() const
{
int i, j;
std::cout << "Unconnected channels" << std::endl;
for (i = j = 0;i < _ntot;i++)
{
if (! _connected[i] )
{
if (!(j % 8))
std::cout << std::endl;
std::cout << std::setw(8) << i;
j++;
}
}
std::cout << std::endl;
}
void PadMap::set(int channel, int x, int y)
{
if (channel<0 || channel >_ntot)
return;
int ipad = x + _nx * y;
if (ipad>=0 && ipad<_ntot)
{
_pad2chan[ ipad ] = channel;
_chan2pad[ channel ] = ipad;
_connected[ channel ] = 1;
}
}