CMlib
Cell mapping algorithms in C++
scm.h
Go to the documentation of this file.
1 #ifndef CELL_MAPPING_CPP_SCM_H
2 #define CELL_MAPPING_CPP_SCM_H
3 
4 #include "css.h"
5 #include "system.h"
6 #include "coloring.h"
7 #include <stdio.h>
8 #include <jpeg-9a/jpeglib.h>
9 
10 #include <iostream>
11 
12 namespace cm {
13 
14  template <class CellType, class IDType, class StateVectorType>
15  class SCM {
16  private:
19  IDType periodicGroups;
20  std::vector<std::vector<IDType>> periodicGroupIDs;
21  std::vector<IDType> periodicities;
22  public:
23  SCM(StateVectorType center, StateVectorType width, const std::vector<IDType>& cellCounts,
24  DynamicalSystemBase<StateVectorType> *systemPointer) :
25  css(center, width, cellCounts), systemPointer(systemPointer) {
26  periodicGroups = 0;
27  periodicities.resize(0);
28  periodicGroupIDs.resize(0);
29  }
30  void solve(IDType max_steps = 1) {
31  // Calculate images
32  std::cout << "Initializing Cell state space with " << css.getCellSum() << " cells\n";
33 #pragma omp parallel for
34  for (IDType i=1; i<css.getCellSum(); i++) {
35  IDType steps = 0; IDType image = i;
36  StateVectorType imageState = css.getCenter(i);
37  css.getCell(i).setState(CellState::Untouched);
38  while (image == i && steps < max_steps) {
39  imageState = systemPointer->step(imageState);
40  image = css.getID(imageState);
41  steps++;
42  }
43  css.setImage(i, image);
44  }
45  periodicGroups = 0;
46  periodicities.resize(0);
47  periodicGroupIDs.resize(0);
48  // Determine cell evolutions for cells
49  IDType z,p,s;
50  bool processing;
51  std::vector<IDType> sequence;
52  std::vector<IDType> newPG;
53  // Store the first PG (sink cell)
54  newPG.push_back(0);
55  periodicGroupIDs.push_back(newPG);
56  for (IDType i = 0; i < css.getCellSum(); i++) {
57  z = i;
58  if (css.getCell(z).getState() == CellState::Untouched) {
59  css.getCell(z).setState(CellState::UnderProcessing);
60  processing = true;
61  sequence.resize(0);
62  sequence.push_back(z);
63  // Start processing sequence for i
64  while (processing) {
65  z = css.getImage(z);
66  switch (css.getCell(z).getState()) {
68  // Mark cell as under processing, store in the sequence then continue
69  css.getCell(z).setState(CellState::UnderProcessing);
70  sequence.push_back(z);
71  break;
73  // New periodic group and possibily some transients
74  processing = false;
75  // First find the periodic group by scanning sequence backwards
76  s = sequence.size();
77  p = 0;
78  for (size_t j=0; j<s; j++) {
79  if (sequence[s-1-j]==z) { p = j+1; }
80  }
81  // Create new PG
82  periodicities.push_back(p);
83  periodicGroups++; // Increase group counter
84  newPG.resize(0);
85  // Set properties for periodic cells,
86  for (size_t j=0; j<p; j++) {
87  css.setGroup(sequence[s-1-j], periodicGroups-1);
88  css.setStep(sequence[s-1-j], 0);
89  css.getCell(sequence[s-1-j]).setState(CellState::Processed);
90  newPG.push_back(sequence[s-1-j]);
91  }
92  // Add current PG to the container
93  periodicGroupIDs.push_back(newPG);
94  // Set properties for transient cells
95  for (size_t j=p; j<s; j++) {
96  css.setGroup(sequence[s-1-j], periodicGroups-1);
97  css.setStep(sequence[s-1-j], j-p+1);
98  css.getCell(sequence[s-1-j]).setState(CellState::Processed);
99  }
100  break;
102  // A set of transient cells leading to an already processed cell
103  processing = false;
104  s = sequence.size();
105  p = css.getGroup(z);
106  IDType step = css.getStep(z);
107  for(size_t j=0; j<s; j++) {
108  css.setGroup(sequence[s-1-j], p);
109  css.setStep(sequence[s-1-j], step+1+j);
110  css.getCell(sequence[s-1-j]).setState(CellState::Processed);
111  }
112  break;
113  }
114  }
115  }
116  else if (css.getCell(z).getState() == CellState::Processed) {
117  // Skip the cell (already processed)
118  }
119  } // end for
120  std::cout << "Number of PGs: " << periodicGroups << std::endl;
121  }
122  void printSummary();
123  void generateImage(std::string filepath, SCMColoringMethod<CellType, IDType>* coloringMethod=nullptr,
124  IDType x0=0, IDType y0=0, IDType xw=0, IDType yw=0) {
125  // Instantiate coloring method if not provided
126  SCMDefaultColoring<CellType, IDType> defaultColoring;
127  if (coloringMethod == nullptr) {
128  coloringMethod = &defaultColoring;
129  }
130  std::cout << "Generating JPG: " << filepath << std::endl;
131  // TODO: Coordinate values for other dimensions!
132  // TODO: Interface for accessing a plane from CellStateSpace
133  FILE* outfile = fopen(filepath.c_str(), "wb");
134  if (outfile == NULL) {
135  std::cout << "Failed to open output file: " << filepath << std::endl;
136  }
137  struct jpeg_compress_struct cinfo;
138  struct jpeg_error_mgr jerr;
139  if (xw == 0) xw = css.getCellCounts()[0];
140  if (yw == 0) yw = css.getCellCounts()[1];
141  size_t components = 3; // RGB
142  // TODO: Use only line buffers (generate and save on-the-fly)
143  std::vector<char> buffer(xw*yw*components);
144  size_t buff_p;
145  std::vector<IDType> cellCoord(2);
146  IDType id;
147  for (size_t i=0; i<yw; i++) {
148  for (size_t j=0; j<xw; j++) {
149  cellCoord[0]=x0+j;
150  cellCoord[1]=y0+yw-1-i;
151  id = css.getIDFromCellCoord(cellCoord);
152  CellType cell = css.getCell(id);
153  std::vector<char> rgb = coloringMethod->createColor(cell, periodicGroups);
154  buff_p = (i*xw+j)*components;
155  buffer[buff_p] = rgb[0];
156  buffer[buff_p+1]= rgb[1];
157  buffer[buff_p+2]= rgb[2];
158  }
159  }
160 
161  cinfo.err = jpeg_std_error(&jerr);
162  jpeg_create_compress(&cinfo);
163  jpeg_stdio_dest(&cinfo, outfile);
164  cinfo.image_width = (JDIMENSION) xw;
165  cinfo.image_height = (JDIMENSION) yw;
166  cinfo.input_components = (int) components;
167  cinfo.in_color_space = JCS_RGB;
168 
169  jpeg_set_defaults(&cinfo);
170  jpeg_set_quality (&cinfo, 100, true); // Set the quality [0..100]
171  jpeg_start_compress(&cinfo, true);
172  JSAMPROW row_pointer;
173  while (cinfo.next_scanline < cinfo.image_height) {
174  row_pointer = (JSAMPROW) &(buffer.data()[cinfo.next_scanline*components*xw]);
175  jpeg_write_scanlines(&cinfo, &row_pointer, 1);
176  }
177  jpeg_finish_compress(&cinfo);
178  }
180  return systemPointer;
181  }
183  return css;
184  }
186  return css;
187  }
188  IDType getPeriodicGroups() const {
189  return periodicGroups;
190  }
191  void setPeriodicGroups(IDType periodicGroups) {
192  SCM::periodicGroups = periodicGroups;
193  }
194  };
195 
196  template <class StateVectorType>
197  using SCM32 = SCM<SCMCell<uint32_t>, uint32_t, StateVectorType>;
198 
199  template <class StateVectorType>
200  using SCM64 = SCM<SCMCell<uint64_t>, uint64_t, StateVectorType>;
201 
202 }
203 
204 
205 #endif //CELL_MAPPING_CPP_SCM_H
void setImage(const IDType ID, const IDType image)
Definition: css.h:166
void generateImage(std::string filepath, SCMColoringMethod< CellType, IDType > *coloringMethod=nullptr, IDType x0=0, IDType y0=0, IDType xw=0, IDType yw=0)
Definition: scm.h:123
IDType getID(const StateVectorType &state) const
Definition: css.h:87
Definition: coloring.h:21
void solve(IDType max_steps=1)
Definition: scm.h:30
SCMUniformCellStateSpace< CellType, IDType, StateVectorType > & getCss()
Definition: scm.h:185
const IDType getGroup(const IDType ID) const
Definition: css.h:160
DynamicalSystemBase< StateVectorType > * getSystemPointer() const
Definition: scm.h:179
SCM(StateVectorType center, StateVectorType width, const std::vector< IDType > &cellCounts, DynamicalSystemBase< StateVectorType > *systemPointer)
Definition: scm.h:23
void setGroup(const IDType ID, const IDType group)
Definition: css.h:169
Definition: css.h:152
virtual StateVectorType step(const StateVectorType &state) const =0
void setPeriodicGroups(IDType periodicGroups)
Definition: scm.h:191
Definition: system.h:7
Definition: scm.h:15
Definition: coloring.h:13
const StateVectorType & getCenter() const
Definition: css.h:77
const IDType getStep(const IDType ID) const
Definition: css.h:163
IDType getIDFromCellCoord(std::vector< IDType > cellCoord)
Definition: css.h:80
const std::vector< IDType > & getCellCounts() const
Definition: css.h:140
Definition: cell.h:6
CellType & getCell(const IDType ID)
Returns the cell with the given ID.
Definition: css.h:111
void printSummary()
const SCMUniformCellStateSpace< CellType, IDType, StateVectorType > & getCss() const
Definition: scm.h:182
void setStep(const IDType ID, const IDType step)
Definition: css.h:172
IDType getPeriodicGroups() const
Definition: scm.h:188
const IDType getImage(const IDType ID) const
Definition: css.h:157
IDType getCellSum() const
Definition: css.h:137