C
carl
I need to create either a 2D or 3D array based on the dimension specified by
the user. So far I just split it in two cases depending on the specified
parameter:
// 2D grid
if(NDimensions==2) {
double offset = imageSize[0] / (nodes-1) * 1.0;
// generalize to ND ?.
int id = 0;
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
ControlPointType cp;
VectorType location, deformation;
location[0] = i*offset;
location[1] = j*offset;
deformation.Fill(0);
cp.setDeformation(deformation);
cp.setLocation(location);
cp.setId(id);
m_ControlPoints.push_back(cp);
id++;
}
}
}
// 2D cube
if(NDimensions == 3) {
double x_offset = imageSize[0] / (nodes-1) * 1.0;
double y_offset = imageSize[1] / (nodes-1) * 1.0;
double z_offset = imageSize[2] / (nodes-1) * 1.0;
int id = 0;
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
for (int k = 0; k < nodes; k++) {
ControlPointType cp;
VectorType location, deformation;
location[0] = i*x_offset;
location[1] = j*y_offset;
location[2] = k*z_offset;
deformation.Fill(0);
cp.setDeformation(deformation);
cp.setLocation(location);
cp.setId(id);
m_ControlPoints.push_back(cp);
id++;
}
}
}
}
But is there some more generic way to do this or is it only possible when
splitting the code up like above?
the user. So far I just split it in two cases depending on the specified
parameter:
// 2D grid
if(NDimensions==2) {
double offset = imageSize[0] / (nodes-1) * 1.0;
// generalize to ND ?.
int id = 0;
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
ControlPointType cp;
VectorType location, deformation;
location[0] = i*offset;
location[1] = j*offset;
deformation.Fill(0);
cp.setDeformation(deformation);
cp.setLocation(location);
cp.setId(id);
m_ControlPoints.push_back(cp);
id++;
}
}
}
// 2D cube
if(NDimensions == 3) {
double x_offset = imageSize[0] / (nodes-1) * 1.0;
double y_offset = imageSize[1] / (nodes-1) * 1.0;
double z_offset = imageSize[2] / (nodes-1) * 1.0;
int id = 0;
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
for (int k = 0; k < nodes; k++) {
ControlPointType cp;
VectorType location, deformation;
location[0] = i*x_offset;
location[1] = j*y_offset;
location[2] = k*z_offset;
deformation.Fill(0);
cp.setDeformation(deformation);
cp.setLocation(location);
cp.setId(id);
m_ControlPoints.push_back(cp);
id++;
}
}
}
}
But is there some more generic way to do this or is it only possible when
splitting the code up like above?