MRaster examples 22.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
barrymartin.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file barrymartin.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw some Barry Martin Attractors.@EOL
7 @std C++20
8 @see https://www.mitchr.me/SS/barrymartin/index.html
9 @copyright
10 @parblock
11 Copyright (c) 1988-2015, 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
32 Barry Martin's "Hopalong" orbit fractals are a family of discrete-time dynamical systems:
33 - Classic Barry Martin fractal:
34 @f[ \begin{align*}
35 x_{n+1} & = y_n - \mathrm{sgn}(x_n) \cdot \sqrt{\vert b x_n-c\vert} \\
36 y_{n+1} & = a-x_n
37 \end{align*} @f]
38 - Positive Barry Martin fractal:
39 @f[ \begin{align*}
40 x_{n+1} & = y_n + \mathrm{sgn}(x_n) \cdot \sqrt{\vert b x_n-c\vert} \\
41 y_{n+1} & = a-x_n
42 \end{align*} @f]
43 - Additive Barry Martin fractal:
44 @f[ \begin{align*}
45 x_{n+1} & = y_n + \sqrt{\vert b x_n-c\vert} \\
46 y_{n+1} & = a-x_n
47 \end{align*} @f]
48 - Sinusoidal Barry Martin fractal:
49 @f[ \begin{align*}
50 x_{n+1} & = y_n + \sin{(b x_n-c)} \\
51 y_{n+1} & = a-x_n
52 \end{align*} @f]
53
54 All of these maps, and others, may be constructed from the following system as special cases:
55 @f[ \begin{align*}
56 x_{n+1} & = y_n+d \cdot \mathrm{ssgn}(x_n) \cdot \left(f \cdot \sqrt{\vert b x_n-c\vert} + g \cdot \sin{(b x_n-c)} + h \cdot \vert b x_n-c\vert \right) \\
57 y_{n+1} & = a-x_n
58 \end{align*} @f]
59 Where
60 @f[ \mathrm{ssgn}(v) = \cases{ s & $v\lt 0$ \cr
61 1 & $v\ge 0$ } @f]
62 @f[ \mathrm{sgn}(v) = \cases{ -1 & $v\lt 0$ \cr
63 +1 & $v\ge 0$ } @f]
64
65 Note the simplified form of the @f$\mathrm{sgn}(v)@f$ function used -- the standard definition may be used for nearly identical results.
66
67 Some special cases:
68 - When @f$ d=-1, s=-1, f=1, g=0, h=0 @f$, the map becomes the "Classic Barry Martin fractal"
69 - When @f$ d=1, s=-1, f=1, g=0, h=0 @f$, the map becomes the "Positive Barry Martin fractal"
70 - When @f$ d=1, s=1, f=1, g=0, h=0 @f$, the map becomes the "Additive Barry Martin fractal"
71 - When @f$ d=1, s=1, f=0, g=1, h=0, c=0 @f$, the map becomes the "Sinusoidal Barry Martin fractal"
72 - When @f$ d=1, s=1, f=0, g=0, h=1, c=0 @f$, the map becomes the "The Gingerbread Man":
73 @f[ \begin{align*}
74 x_{n+1} & = y_n + \sin{(b x_n)} \\
75 y_{n+1} & = a-x_n
76 \end{align*} @f]
77 - When @f$ d=1, s=1, f=0, g=0, h=1, c\ne0 @f$, the map becomes the "The Shifted Gingerbread Man":
78 @f[ \begin{align*}
79 x_{n+1} & = y_n + \sin{(b x_n-c)} \\
80 y_{n+1} & = a-x_n
81 \end{align*} @f]
82*/
83/*******************************************************************************************************************************************************.H.E.**/
84/** @cond exj */
85
86//--------------------------------------------------------------------------------------------------------------------------------------------------------------
87#include "ramCanvas.hpp"
88#include "MRMathSTR.hpp"
89
90//--------------------------------------------------------------------------------------------------------------------------------------------------------------
91std::vector<std::array<double, 14>> params {
92 /* a b c d, s, f, g, h, n, k, x-min, x-max, y-min, y-max */
93 { -2.00000, -0.33000, 0.01000, -1.0, -1.0, 1.0, 0.0, 0.0, 4.0e7, 1.0, -18.0, 17.0, -18.0, 17.0}, // 0
94 { 0.40000, 1.10000, 0.00000, -1.0, -1.0, 1.0, 0.0, 0.0, 2.5e7, 1.0, -7.0, 7.0, -7.0, 7.0}, // 1
95 { -3.14000, 0.20000, 0.30000, -1.0, -1.0, 1.0, 0.0, 0.0, 3.0e7, 1.0, -40.0, 40.0, -40.0, 40.0}, // 2
96 { -3.14000, 0.19000, 0.32000, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0e7, 1.0, -38.0, 35.0, -38.0, 35.0}, // 3
97 { -2.14000, -0.20000, 0.30000, 1.0, 1.0, 1.0, 0.0, 0.0, 3.0e7, 0.5, -9.0, 7.0, -9.0, 7.0}, // 4
98 { 0.40000, 1.00000, 0.00000, -1.0, -1.0, 0.0, 1.0, 0.0, 2.0e6, 1.0, -70.0, 70.0, -70.0, 70.0}, // 5
99 { -3.14000, 0.20000, 0.30000, -1.0, -1.0, 0.0, 1.0, 0.0, 6.0e7, 1.0, -45.0, 45.0, -45.0, 45.0}, // 6
100 { -3.14000, 0.20000, 0.30000, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0e7, 1.0, -35.0, 35.0, -35.0, 35.0}, // 7
101 { -2.00000, -0.33000, 0.01000, -1.0, -1.0, 1.0, 0.0, 1.0, 4.0e7, 1.0, -25.0, 25.0, -25.0, 25.0}, // 8
102 { -3.14000, 0.20000, 0.30000, -1.0, -1.0, 1.0, 0.0, 1.0, 4.0e7, 1.0, -60.0, 60.0, -60.0, 60.0}, // 9
103 { -3.14000, 0.20000, 0.30000, 1.0, -1.0, 1.0, 0.0, 1.0, 6.0e7, 1.0, -90.0, 90.0, -90.0, 90.0}, // 10
104 { -3.14000, 0.20000, 0.30000, -1.0, -1.0, 0.0, 0.0, 1.0, 2.0e7, 1.0, -17.0, 15.0, -17.0, 15.0}, // 11
105 { -3.14000, 0.20000, 0.30000, 1.0, -1.0, 0.0, 0.0, 1.0, 3.0e7, 1.0, -17.0, 14.0, -17.0, 14.0}, // 12
106 { -3.14000, 0.20000, 0.30000, 1.0, -1.0, 0.0, 0.0, 1.0, 1.0e7, 1.0, -17.0, 14.0, -17.0, 14.0}, // 13
107 { -1.14000, 0.01000, 0.05000, 1.0, -1.0, 0.0, 0.0, 1.0, 5.0e6, 1.0, -3.5, 2.5, -3.5, 2.5}, // 14
108 { -1.14000, 0.10000, 0.50000, 1.0, -1.0, 0.0, 0.0, 1.0, 6.0e7, 1.0, -17.0, 14.0, -17.0, 14.0}, // 15
109};
110
111//--------------------------------------------------------------------------------------------------------------------------------------------------------------
112int main(void) {
113 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
114 const int BSIZ = 480*8;
115 mjr::ramCanvas1c16b::colorType aColor;
116 aColor.setChans(1);
117
118 for(decltype(params.size()) j=0; j<params.size(); ++j) {
119 mjr::ramCanvas1c16b theRamCanvas(BSIZ, BSIZ, params[j][10], params[j][11], params[j][12], params[j][13]);
120
121 double a = params[j][0];
122 double b = params[j][1];
123 double c = params[j][2];
124 double d = params[j][3];
125 double s = params[j][4];
126 double k = params[j][9];
127
128 double f = params[j][5];
129 double g = params[j][6];
130 double h = params[j][7];
131
132 /* Draw the Attractor on a 16-bit, greyscale canvas such that the level is the hit count for that pixel.
133 Thus we are using an "image" as a way to store field data instead of color information. */
134 double x = 0.0;
135 double y = 0.0;
136 uint64_t maxII = 0;
137 uint64_t inCnt = 0;
138 uint64_t maxItr = static_cast<uint64_t>(params[j][8]);
139 uint64_t iPrt = maxItr / 5;
140 for(uint64_t i=1;i<maxItr;i++) {
141 double xNew = y+d*(x<0.0?s:1.0)* (f*std::sqrt(std::abs(b*x-c)) + g*std::sin(b*x-c) + h*std::abs(b*x-c));
142 double yNew = a-x;
143 if ( !theRamCanvas.isCliped(x, y)) {
144 inCnt++;
145 theRamCanvas.drawPoint(x, y, theRamCanvas.getPxColor(x, y).tfrmAdd(aColor));
146 if(theRamCanvas.getPxColor(x, y).getC0() > maxII) {
147 maxII = theRamCanvas.getPxColor(x, y).getC0();
148 if(maxII > 16384) { // 1/4 of max possible intensity
149 std::cout << "ITER(" << j << "): " << i << " MAXS: " << maxII << " EXIT: Maximum image intensity reached" << std::endl;
150 break;
151 }
152 }
153 }
154 if((i % iPrt) == 0)
155 std::cout << "ITER(" << j << "): " << i << " MAXS: " << maxII << " INC: " << inCnt << std::endl;
156 x=xNew;
157 y=yNew;
158 }
159 std::cout << "ITER(" << j << "): " << "DONE" << " MAXS: " << maxII << " INC: " << inCnt << std::endl;
160
161 // Log image transform
162 theRamCanvas.applyHomoPixTfrm(&mjr::ramCanvas1c16b::colorType::tfrmLn1);
163 maxII = static_cast<uint64_t>(std::log(static_cast<double>(maxII)));
164
165 /* Create a new image based on csCCfractal0RYBCW -- this one is 24-bit RGB color. */
166 mjr::ramCanvas3c8b anotherRamCanvas(BSIZ, BSIZ);
167 typedef mjr::ramCanvas3c8b::colorType::csCCfractal0RYBCW cs_t;
168 for(int yi=0;yi<theRamCanvas.getNumPixY();yi++)
169 for(int xi=0;xi<theRamCanvas.getNumPixX();xi++)
170 anotherRamCanvas.drawPoint(xi, yi, cs_t::c(k * theRamCanvas.getPxColor(xi, yi).getC0() / static_cast<mjr::ramCanvas3c8b::csFltType>(maxII)));
171
172 anotherRamCanvas.writeTIFFfile("barrymartin_" + mjr::math::str::fmt_int(j, 2, '0') + ".tiff");
173 }
174 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
175 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
176 return 0;
177}
178/** @endcond */
int main(int argc, char *argv[])