| 1 |
#include <array> |
| 2 |
#include <cstdio> |
| 3 |
#include <cstdlib> |
| 4 |
#include <iostream> |
| 5 |
#include <memory> |
| 6 |
|
| 7 |
#define FLOAT double |
| 8 |
|
| 9 |
// |
| 10 |
// Given a big volume with dimension `dims` and logical indices `ijk`, |
| 11 |
// return a location in the 1D array representing the volume. |
| 12 |
// |
| 13 |
size_t translate_idx(std::array<size_t, 3> dims, std::array<size_t, 3> ijk) |
| 14 |
{ |
| 15 |
const auto plane = ijk[2] * dims[0] * dims[1]; |
| 16 |
const auto col = ijk[1] * dims[0]; |
| 17 |
return (ijk[0] + col + plane); |
| 18 |
} |
| 19 |
|
| 20 |
int main(int argc, char* argv[]) |
| 21 |
{ |
| 22 |
if (argc != 12) { |
| 23 |
std::cerr << "Usage: InFileName, InFileNX, InFileNY, InFileNZ, " |
| 24 |
<< "OutFileName, OutStartX, OutFinishX, " |
| 25 |
<< "OutStartY, OutFinishY, OutStartZ, OutFinishZ. " << std::endl |
| 26 |
<< "For example, if you want to crop from index 64 " |
| 27 |
<< "to index 128 (exclusive) in X dimension, just type 64 and 128!" << std::endl; |
| 28 |
exit(1); |
| 29 |
} |
| 30 |
|
| 31 |
/* |
| 32 |
* It is the user's responsibility to make sure the range to crop |
| 33 |
* is within the range of input file. |
| 34 |
*/ |
| 35 |
auto inDims = std::array<size_t, 3>{static_cast<size_t>(std::atol(argv[2])), |
| 36 |
static_cast<size_t>(std::atol(argv[3])), |
| 37 |
static_cast<size_t>(std::atol(argv[4]))}; |
| 38 |
auto inValNum = inDims[0] * inDims[1] * inDims[2]; |
| 39 |
size_t outStartX = std::atol(argv[6]); |
| 40 |
size_t outFinishX = std::atol(argv[7]); |
| 41 |
size_t outStartY = std::atol(argv[8]); |
| 42 |
size_t outFinishY = std::atol(argv[9]); |
| 43 |
size_t outStartZ = std::atol(argv[10]); |
| 44 |
size_t outFinishZ = std::atol(argv[11]); |
| 45 |
auto outDims = |
| 46 |
std::array<size_t, 3>{outFinishX - outStartX, outFinishY - outStartY, outFinishZ - outStartZ}; |
| 47 |
auto outValNum = outDims[0] * outDims[1] * outDims[2]; |
| 48 |
|
| 49 |
std::FILE* infile = std::fopen(argv[1], "rb"); |
| 50 |
if (infile == nullptr) { |
| 51 |
std::cerr << "input file open error: " << argv[1] << std::endl; |
| 52 |
exit(1); |
| 53 |
} |
| 54 |
|
| 55 |
auto inbuf = std::make_unique<FLOAT[]>(inValNum); |
| 56 |
auto cnt = std::fread(inbuf.get(), sizeof(FLOAT), inValNum, infile); |
| 57 |
if (cnt != inValNum) { |
| 58 |
std::cerr << "input file read error: " << argv[1] << std::endl; |
| 59 |
exit(1); |
| 60 |
} |
| 61 |
|
| 62 |
auto outbuf = std::make_unique<FLOAT[]>(outValNum); |
| 63 |
size_t counter = 0; |
| 64 |
for (size_t z = outStartZ; z < outFinishZ; z++) { |
| 65 |
for (size_t y = outStartY; y < outFinishY; y++) { |
| 66 |
for (size_t x = outStartX; x < outFinishX; x++) { |
| 67 |
auto idx = translate_idx(inDims, {x, y, z}); |
| 68 |
outbuf[counter++] = inbuf[idx]; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
std::FILE* outfile = std::fopen(argv[5], "wb"); |
| 74 |
if (outfile == nullptr) { |
| 75 |
std::cerr << "output file open error: " << argv[5] << std::endl; |
| 76 |
exit(1); |
| 77 |
} |
| 78 |
|
| 79 |
cnt = std::fwrite(outbuf.get(), sizeof(FLOAT), outValNum, outfile); |
| 80 |
if (cnt != outValNum) { |
| 81 |
std::cerr << "output file write error: " << argv[5] << std::endl; |
| 82 |
exit(1); |
| 83 |
} |
| 84 |
|
| 85 |
std::fclose(infile); |
| 86 |
std::fclose(outfile); |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |