MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
tinkerbell.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file tinkerbell.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw some Tinkerbell Attractors.@EOL
7 @std C++20
8 @see https://www.mitchr.me/SS/tinkerbell/index.html
9 @copyright
10 @parblock
11 Copyright (c) 2024, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19
20 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
21 without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 DAMAGE.
29 @endparblock
30 @filedetails
31 The Tinkerbell Attractor is a discrete-time dynamical system defined by the following relationship:
32 @f[ \begin{align*}
33 x_{n+1} & = x_n^2-y_n^2+ax_n+by_n \\
34 y_{n+1} & = 2x_ny_n+cx_n+dy_n
35 \end{align*} @f]
36 Most any non-zero initial conditions may be used for \‍( (x_1, y_1) \‍).
37*/
38/*******************************************************************************************************************************************************.H.E.**/
39/** @cond exj */
40
41//--------------------------------------------------------------------------------------------------------------------------------------------------------------
42#include "ramCanvas.hpp"
43
44//--------------------------------------------------------------------------------------------------------------------------------------------------------------
45std::vector<std::array<double, 11>> params {
46 /* a, b, c, d, x0, y0, N, x-min, x-max, y-min, y-max */
47 { 0.900000, -0.220000, 2.0000000, 0.500000, 0.1, 0.1, 1e7, -1.410000, 0.370000, -1.380000, -0.350000 }, // 0
48 { 0.200000, 0.150000, 2.0000000, 0.650000, 0.1, 0.1, 1e7, -1.460000, 0.950000, -1.240000, -0.150000 }, // 1
49 { 0.900000, -0.601300, 2.0000000, 0.500000, 0.1, 0.1, 1e7, -1.240000, 0.470000, -1.560000, 0.550000 }, // 2
50 { -0.783671, -0.450251, 0.0756921, 1.908150, 0.1, 0.1, 1e8, -0.784949, 0.190357, 0.100000, 1.012020 }, // 3
51 { -0.667316, 0.789788, 1.1057300, 0.268078, 0.1, 0.1, 1e8, -0.903469, 1.087990, -0.708937, 0.319083 }, // 4
52 { -1.016430, -1.458560, 1.3230100, 0.604218, 0.1, 0.1, 1e8, -0.399636, 0.403479, -0.562466, 0.225223 }, // 5
53 { -0.254258, 0.925243, 0.7704410, -0.416270, 0.1, 0.1, 1e8, -0.888853, 0.939351, -0.734047, 0.346012 }, // 6
54 { -1.548590, -0.249840, -0.2864630, 1.508330, 0.1, 0.1, 1e8, -0.849159, 1.357350, -0.054140, 1.033830 }, // 7
55 { -0.769882, -0.743097, -1.0527500, 0.216079, 0.1, 0.1, 1e8, -0.864322, 1.141670, -0.394488, 0.686611 }, // 8
56 { -0.305249, -0.507228, -1.5821000, 0.919462, 0.1, 0.1, 1e8, -1.234830, 0.745791, 0.300000, 0.881405 }, // 9
57 { -1.754900, -0.904038, 0.9181340, 1.379920, 0.1, 0.1, 1e8, -0.821663, 1.448790, -2.112250, -0.100000 }, // 10
58 { -1.659840, -1.286440, 0.7102750, 1.097500, 0.1, 0.1, 1e8, -0.412856, 0.803154, -1.709030, -0.800778 }, // 11
59 { -1.733520, -0.647135, 0.5964760, 0.988405, 0.1, 0.1, 1e8, -0.700000, 1.800000, -0.312000, -0.273000 }, // 12
60};
61
62//--------------------------------------------------------------------------------------------------------------------------------------------------------------
63int main(void) {
64 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
65 const int BSIZ = 480*8;
66 mjr::ramCanvas1c16b::colorType aColor;
67 aColor.setChans(1);
68
69 for(decltype(params.size()) j=0; j<params.size(); ++j) {
70 mjr::ramCanvas1c16b theRamCanvas(BSIZ, BSIZ, params[j][7], params[j][8], params[j][9], params[j][10]);
71
72 double a = params[j][0];
73 double b = params[j][1];
74 double c = params[j][2];
75 double d = params[j][3];
76
77 /* Draw the Attractor on a 16-bit, greyscale canvas such that the level is the hit count for that pixel.
78 Thus we are using an "image" as a way to store field data instead of color information. */
79 double x = params[j][4];
80 double y = params[j][5];
81 uint64_t maxII = 0;
82 uint64_t inCnt = 0;
83 uint64_t maxItr = static_cast<uint64_t>(params[j][6]);
84 uint64_t iPrt = maxItr / 5;
85 uint64_t itr;
86 for(itr=1;itr<maxItr;itr++) {
87 double xNew = x*x-y*y+a*x+b*y;
88 double yNew = 2*x*y+c*x+d*y;
89 if ( !theRamCanvas.isCliped(x, y)) {
90 inCnt++;
91 theRamCanvas.drawPoint(x, y, theRamCanvas.getPxColor(x, y).tfrmAdd(aColor));
92 if(theRamCanvas.getPxColor(x, y).getC0() > maxII) {
93 maxII = theRamCanvas.getPxColor(x, y).getC0();
94 if(maxII > 16384) { // 1/4 of max possible intensity
95 std::cout << "ITER(" << j << "): " << itr << " MAXS: " << maxII << " EXIT: Maximum intensity reached" << std::endl;
96 break;
97 }
98 }
99 }
100 if((itr % iPrt) == 0)
101 std::cout << "ITER(" << j << "): " << itr << " MAXS: " << maxII << " INC: " << inCnt << std::endl;
102 x=xNew;
103 y=yNew;
104 }
105 std::cout << "ITER(" << j << "): " << itr << " MAXS: " << maxII << " INC: " << inCnt << std::endl;
106
107 // Log image transform
108 theRamCanvas.applyHomoPixTfrm(&mjr::ramCanvas1c16b::colorType::tfrmLn1);
109 maxII = static_cast<uint64_t>(std::log(static_cast<double>(maxII)));
110
111 /* Create a new image based on csCCfractal0RYBCW -- this one is 24-bit RGB color. */
112 mjr::ramCanvas3c8b anotherRamCanvas(BSIZ, BSIZ);
113 typedef mjr::ramCanvas3c8b::colorType::csCCfractal0RYBCW cs_t;
114 for(int yi=0;yi<theRamCanvas.getNumPixY();yi++)
115 for(int xi=0;xi<theRamCanvas.getNumPixX();xi++)
116 anotherRamCanvas.drawPoint(xi, yi, cs_t::c(theRamCanvas.getPxColor(xi, yi).getC0() / static_cast<mjr::ramCanvas3c8b::csFltType>(maxII)));
117
118 anotherRamCanvas.writeTIFFfile("tinkerbell_" + mjr::math::str::fmt_int(j, 2, '0') + ".tiff");
119 }
120 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
121 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
122 return 0;
123}
124/** @endcond */
int main(int argc, char *argv[])