1 #ifndef CELL_MAPPING_CPP_SCM_H 2 #define CELL_MAPPING_CPP_SCM_H 8 #include <jpeg-9a/jpeglib.h> 14 template <
class CellType,
class IDType,
class StateVectorType>
19 IDType periodicGroups;
20 std::vector<std::vector<IDType>> periodicGroupIDs;
21 std::vector<IDType> periodicities;
23 SCM(StateVectorType center, StateVectorType width,
const std::vector<IDType>& cellCounts,
25 css(center, width, cellCounts), systemPointer(systemPointer) {
27 periodicities.resize(0);
28 periodicGroupIDs.resize(0);
30 void solve(IDType max_steps = 1) {
32 std::cout <<
"Initializing Cell state space with " << css.
getCellSum() <<
" cells\n";
33 #pragma omp parallel for 35 IDType steps = 0; IDType image = i;
36 StateVectorType imageState = css.
getCenter(i);
38 while (image == i && steps < max_steps) {
39 imageState = systemPointer->
step(imageState);
40 image = css.
getID(imageState);
46 periodicities.resize(0);
47 periodicGroupIDs.resize(0);
51 std::vector<IDType> sequence;
52 std::vector<IDType> newPG;
55 periodicGroupIDs.push_back(newPG);
56 for (IDType i = 0; i < css.
getCellSum(); i++) {
62 sequence.push_back(z);
66 switch (css.
getCell(z).getState()) {
70 sequence.push_back(z);
78 for (
size_t j=0; j<s; j++) {
79 if (sequence[s-1-j]==z) { p = j+1; }
82 periodicities.push_back(p);
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);
90 newPG.push_back(sequence[s-1-j]);
93 periodicGroupIDs.push_back(newPG);
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);
107 for(
size_t j=0; j<s; j++) {
109 css.
setStep(sequence[s-1-j], step+1+j);
120 std::cout <<
"Number of PGs: " << periodicGroups << std::endl;
124 IDType x0=0, IDType y0=0, IDType xw=0, IDType yw=0) {
127 if (coloringMethod ==
nullptr) {
128 coloringMethod = &defaultColoring;
130 std::cout <<
"Generating JPG: " << filepath << std::endl;
133 FILE* outfile = fopen(filepath.c_str(),
"wb");
134 if (outfile == NULL) {
135 std::cout <<
"Failed to open output file: " << filepath << std::endl;
137 struct jpeg_compress_struct cinfo;
138 struct jpeg_error_mgr jerr;
141 size_t components = 3;
143 std::vector<char> buffer(xw*yw*components);
145 std::vector<IDType> cellCoord(2);
147 for (
size_t i=0; i<yw; i++) {
148 for (
size_t j=0; j<xw; j++) {
150 cellCoord[1]=y0+yw-1-i;
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];
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;
169 jpeg_set_defaults(&cinfo);
170 jpeg_set_quality (&cinfo, 100,
true);
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);
177 jpeg_finish_compress(&cinfo);
180 return systemPointer;
189 return periodicGroups;
192 SCM::periodicGroups = periodicGroups;
196 template <
class StateVectorType>
199 template <
class StateVectorType>
205 #endif //CELL_MAPPING_CPP_SCM_H
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
Definition: coloring.h:21
void solve(IDType max_steps=1)
Definition: scm.h:30
SCMUniformCellStateSpace< CellType, IDType, StateVectorType > & getCss()
Definition: scm.h:185
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
virtual StateVectorType step(const StateVectorType &state) const =0
void setPeriodicGroups(IDType periodicGroups)
Definition: scm.h:191
Definition: coloring.h:13
const SCMUniformCellStateSpace< CellType, IDType, StateVectorType > & getCss() const
Definition: scm.h:182
IDType getPeriodicGroups() const
Definition: scm.h:188