The ChainCon Software (Release 0.03)
cub2sim.cpp
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A program that saves a simplicial set obtained from
6 /// a cubical set by means of simplicial subdivision.
7 ///
8 /////////////////////////////////////////////////////////////////////////////
9 
10 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
11 //
12 // This file is part of my research software package. This is free software:
13 // you can redistribute it and/or modify it under the terms of the GNU
14 // General Public License as published by the Free Software Foundation,
15 // either version 3 of the License, or (at your option) any later version.
16 //
17 // This software is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this software; see the file "license.txt". If not,
24 // please, see <http://www.gnu.org/licenses/>.
25 
26 // Started on August 3, 2015. Last revision: August 3, 2015.
27 
28 
29 // include the string hashing definitions (before including "hashsets.h")
30 #include "chaincon/stringhash.h"
31 
32 // include selected header files from the CHomP library
33 #include "chomp/system/config.h"
34 #include "chomp/system/textfile.h"
35 #include "chomp/system/timeused.h"
36 #include "chomp/system/arg.h"
37 #include "chomp/struct/hashsets.h"
38 
39 // include relevant local header files
40 #include "chaincon/ringzp.h"
41 #include "chaincon/simplex.h"
42 #include "chaincon/cubcell.h"
43 #include "chaincon/emptycell.h"
44 #include "chaincon/wrapping.h"
45 #include "chaincon/prodcell.h"
46 #include "chaincon/cubproduct.h"
47 #define SPACE_WRAPPING
48 
49 
50 // --------------------------------------------------
51 // -------------------- OVERTURE --------------------
52 // --------------------------------------------------
53 
54 /// The title of the program and licensing information.
55 const char *title = "\
56 * * * UNDER CONSTRUCTION * * *\n\
57 Converts a cubical set into an equivalent simplicial complex.\n\
58 Version 0.00 (August 4, 2015). Copyright (C) 1997-2016 by Pawel Pilarczyk.\n\
59 This is free software. No warranty. Consult 'license.txt' for details.";
60 
61 /// Brief help information on the program's usage.
62 const char *helpinfo = "\
63 This program saves a simplicial set obtained from a cubical set by means of\n\
64 simplicial subdivision, using the representation of cubical cells by means of\n\
65 the Cartesian product of the 2D simplices that correspond to the edges.\n\
66 Please, beware of the fact that this is a very inefficient way of subdividing\n\
67 each hypercube into the union of simplices; a much smarter algorithm\n\
68 is possible, but I needed this specific one to check some features.\n\
69 Call with:\n\
70 input.cub - the name of a file that contains a list of cubical cells,\n\
71 output.sim - the name of a file that will contain a list of simplices,\n\
72 Switches and additional arguments:\n\
73 --log filename - save the output to a file (without progress indicators),\n\
74 --quiet - suppress data output to the screen (whcih can be still logged),\n\
75 --help - display this brief help information only and exit.\n\
76 For more information please consult the accompanying documentation\n\
77 or ask the program's author at http://www.PawelPilarczyk.com/.";
78 
79 
80 // --------------------------------------------------
81 // -------------------- routines --------------------
82 // --------------------------------------------------
83 
84 template <class CellT>
85 void flattenProduct (const tProdCell<CellT> &prod, std::vector<CellT> &cells)
86 {
87  if (prod. product ())
88  {
89  flattenProduct (prod. getLeft (), cells);
90  flattenProduct (prod. getRight (), cells);
91  }
92  else
93  {
94  cells. push_back (prod. getCell ());
95  }
96  return;
97 } /* flattenProduct */
98 
99 template <class ProdCellT, class SimCellT>
100 inline void prod2simplex (const ProdCellT &prod, SimCellT &simplex)
101 {
102  using chomp::homology::sbug;
103 
104  int cellDim (prod. dim ());
105  if (cellDim >= chomp::homology::MaxBasDim)
106  throw "Too high dimension of a product cell.";
107  std::vector<int_t> pointNumbers (cellDim + 1);
108  typedef typename ProdCellT::CellType CellT;
109  typedef typename CellT::VertexType CoordT;
110  std::vector<CellT> cells;
111  sbug << "DEBUG: prod = " << prod << "\n";
112  flattenProduct (prod, cells);
113  int spaceDim = cells. size ();
114  for (int i = 0; i <= cellDim; ++ i)
115  {
116  CoordT c [chomp::homology::MaxBasDim];
117  for (int j = 0; j < spaceDim; ++ j)
118  c [j] = cells [j] [i];
119  pointNumbers [i] = chomp::homology::tPointBase<CoordT>::number
120  (c, spaceDim) + 1;
121  }
122  std::sort (pointNumbers. begin (), pointNumbers. end ());
123  simplex = SimCellT (cellDim, pointNumbers);
124  sbug << "DEBUG: simplex = " << simplex << "\n\n";
125  return;
126 } /* prod2simplex */
127 
128 
129 // --------------------------------------------------
130 // ---------------------- main ----------------------
131 // --------------------------------------------------
132 
133 /// The main procedure of the program.
134 /// Returns: 0 = Ok, -1 = Error, 1 = Help displayed, 2 = Wrong arguments.
135 int main (int argc, char *argv [])
136 {
137  using namespace chomp::homology;
138 
139  // prepare the types for coordinates, cubes, and simplices
140  typedef short int CoordT;
142  CubCellT;
144 
145  // turn on a message that will appear if the program does not finish
146  program_time = "Aborted after";
147 
148  // prepare user-configurable data
149  char *inName = 0;
150  char *outName = 0;
151 #ifdef SPACE_WRAPPING
152  const int maxWrapping = 100;
153  char *wrapping [maxWrapping];
154  int nWrapping = 0;
155 #endif
156 
157  // analyze the command line
158  arguments a;
159  arg (a, NULL, inName);
160  arg (a, NULL, outName);
161 #ifdef SPACE_WRAPPING
162  arg (a, "w", wrapping, nWrapping, maxWrapping);
163 #endif
164  arghelp (a);
165 
166  argstreamprepare (a);
167  int argresult = a. analyze (argc, argv);
168  argstreamset ();
169 
170  // show the program's title
171  if (argresult >= 0)
172  sout << title << '\n';
173 
174  // if something was incorrect, show an additional message and exit
175  if (argresult < 0)
176  {
177  sout << "Call with '--help' for help.\n";
178  return 2;
179  }
180 
181  // if help requested or no filename present, show help information
182  if ((argresult > 0) || !outName)
183  {
184  sout << helpinfo << '\n';
185  return 1;
186  }
187 
188  // try running the main function and catch an error message if thrown
189  try
190  {
191 #ifdef SPACE_WRAPPING
192  // set wrapping as desired
193  for (int i = 0; i < nWrapping; ++ i)
194  {
195  setWrapping<typename CubCellT::WrapType>
196  (std::string (wrapping [i]));
197  }
198 #endif
199 
200  // say what you are doing
201  sout << "Transforming cubical cells into simplices:\n'" <<
202  inName << "' --> '" << outName << "'...\n";
203 
204  // open the file with the cubical cells
205  std::ifstream in (inName);
206  if (!in)
207  fileerror (inName);
208 
209  // create a file for the simplices
210  std::ofstream out (outName);
211  if (!out)
212  fileerror (outName, "create");
213 
214  // read the cubes one by one, subdivide them,
215  // and write the corresponding simplices
216  int_t cubCounter (0);
217  int_t simCounter (0);
218  ignorecomments (in);
219  while (!in. eof ())
220  {
221  // read a cubical cell
222  CubCellT q;
223  in >> q;
224  ++ cubCounter;
225  ignorecomments (in);
226 
227  // transform the cell into simplices and normalize
228  tSimplSet<ProdCellT> prodCells;
229  cube2product (q, prodCells);
230  prodCells. normalize ();
231 
232  // process all the simplices
233  int_t prodSize (prodCells. size ());
234  for (int_t n = 0; n < prodSize; ++ n)
235  {
236  const ProdCellT &prod (prodCells [n]);
237  if (prod. dim () != q. dim ())
238  continue;
239 
240  // convert the product into a simplex
242  prod2simplex (prod, simplex);
243 
244  // write the simplex to the output file
245  out << simplex << "\n";
246  ++ simCounter;
247  }
248  }
249  in. close ();
250  out. close ();
251  sout << cubCounter << " cubical cells read.\n";
252  sout << simCounter << " simplices written.\n";
253 
254  program_time = "Total time used:";
255  program_time = 1;
256  return 0;
257  }
258  catch (const char *msg)
259  {
260  sout << "ERROR: " << msg << '\n';
261  return -1;
262  }
263  catch (const std::exception &e)
264  {
265  sout << "ERROR: " << e. what () << '\n';
266  return -1;
267  }
268  catch (...)
269  {
270  sout << "ABORT: An unknown error occurred.\n";
271  return -1;
272  }
273 } /* main */
274 
Elements of the ring Z_p.
A function that converts a cubical cell into a simplicial set that corresponds to the cartesian produ...
void flattenProduct(const tProdCell< CellT > &prod, std::vector< CellT > &cells)
Definition: cub2sim.cpp:85
const char * helpinfo
Brief help information on the program&#39;s usage.
Definition: cub2sim.cpp:62
A simplex with vertices of arbitrary type.
Definition: simplex.h:59
A Cartesian product of simplicial cells of arbitrary type.
void cube2product(const CubCellT &q, SetT &cells)
Transforms an elementary cube into a simplicial set that represents the Cartesian product of the simp...
Definition: cubproduct.h:57
Tools for coordinate wrapping, a.k.a.
void prod2simplex(const ProdCellT &prod, SimCellT &simplex)
Definition: cub2sim.cpp:100
A cubical cell.
const char * title
The title of the program and licensing information.
Definition: cub2sim.cpp:55
Hashing keys for std::string.
A simplex class with arbitrary vertices.
The decision on whether the empty cell should be used as a valid cell of dimension -1...
int main(int argc, char *argv [])
The main procedure of the program.
Definition: cub2sim.cpp:135
A Cartesian product of simplicial cells as a simplicial cell.
Definition: prodcell.h:48
An empty cell existence decision class with settable global flag.
Definition: emptycell.h:70
A simplicial set.
Definition: simplset.h:51
An elementary cubical cell with vertex coordinates of integer type.
Definition: cubcell.h:63