LOOS  v2.3.2
gridslice.cpp
1 /*
2  gridslice.cpp
3 
4  Takes a double grid and extracts a plane from it as a matrix...
5 */
6 
7 /*
8  This file is part of LOOS.
9 
10  LOOS (Lightweight Object-Oriented Structure library)
11  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
12  Department of Biochemistry and Biophysics
13  School of Medicine & Dentistry, University of Rochester
14 
15  This package (LOOS) is free software: you can redistribute it and/or modify
16  it under the terms of the GNU General Public License as published by
17  the Free Software Foundation under version 3 of the License.
18 
19  This package is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27 
28 
29 #include <loos.hpp>
30 #include <boost/format.hpp>
31 #include <boost/tuple/tuple.hpp>
32 #include <DensityGrid.hpp>
33 
34 using namespace std;
35 using namespace loos;
36 using namespace loos::DensityTools;
37 
39 
40 void invalidIndex(int i) {
41  cerr << "ERROR - invalid plane index " << i << endl;
42  exit(-1);
43 }
44 
45 
46 
47 int main(int argc, char *argv[]) {
48  if (argc != 3) {
49  cerr << "Usage- gridslice [i|j|k] index <grid >matrix\n";
50  cerr <<
51  "\n"
52  "Gridslice extracts a slice of the grid and writes it out\n"
53  "as a Matlab/Octave/Gnuplot compatible ASCII matrix.\n"
54  "The first option (i, j, or k) determines the orientation\n"
55  "of the slice. The index represents the coordinate in the\n"
56  "direction. For example, \"k 20\" means extract the plane\n"
57  "when k=20 (an i,j-plane). Using \"i 13\" means extract the\n"
58  "plane when i=13 (a j,k-plane).\n";
59  exit(-1);
60  }
61 
62  string hdr = invocationHeader(argc, argv);
63 
64  string plane(argv[1]);
65  int idx = atoi(argv[2]);
66 
68  cin >> grid;
69  DensityGridpoint dims = grid.gridDims();
70  cerr << boost::format("Grid dimensions are %d x %d x %d (i x j x k)\n") % dims[0] % dims[1] % dims[2];
71  if (plane == "k") {
72 
73  if (idx > dims[2])
74  invalidIndex(idx);
75  Matrix M(dims[1]+1, dims[0]+1);
76  for (int j=0; j<dims[1]; ++j)
77  for (int i=0; i<dims[0]; ++i)
78  M(j,i) = grid(idx,j,i);
79 
80  writeAsciiMatrix(cout, M, hdr);
81 
82  } else if (plane == "j") {
83 
84  if (idx > dims[1])
85  invalidIndex(idx);
86  Matrix M(dims[2]+1, dims[0]+1);
87  for (int k=0; k<dims[2]; ++k)
88  for (int i=0; i<dims[0]; ++i)
89  M(k,i) = grid(k,idx,i);
90 
91  writeAsciiMatrix(cout, M, hdr);
92 
93  } else if (plane == "i") {
94 
95  if (idx > dims[0])
96  invalidIndex(idx);
97  Matrix M(dims[2]+1, dims[1]+1);
98  for (int k=0; k<dims[2]; ++k)
99  for (int j=0; j<dims[1]; ++j)
100  M(k,j) = grid(k,j,idx);
101 
102  writeAsciiMatrix(cout, M, hdr);
103 
104  } else {
105  cerr << "ERROR - unknown plane '" << plane << "'\n";
106  exit(-1);
107  }
108 
109 }
Simple matrix template class using policy classes to determine behavior.
Definition: MatrixImpl.hpp:53
STL namespace.
std::string invocationHeader(int argc, char *argv[])
Create an invocation header.
Definition: utils.cpp:124
Namespace for Density package.
Definition: DensityGrid.hpp:48
std::ostream & writeAsciiMatrix(std::ostream &os, const Math::Matrix< T, P, S > &M, const std::string &meta, const Math::Range &start, const Math::Range &end, const bool trans=false, F fmt=F())
Write a submatrix to a stream.
Namespace for most things not already encapsulated within a class.