MRaster examples 21.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
89//--------------------------------------------------------------------------------------------------------------------------------------------------------------
90std::vector<std::array<double, 14>> params {
91 /* a b c d, s, f, g, h, n, k, x-min, x-max, y-min, y-max */
92 { -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
93 { 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
94 { -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
95 { -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
96 { -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
97 { 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
98 { -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
99 { -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
100 { -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
101 { -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
102 { -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
103 { -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
104 { -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
105 { -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
106 { -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
107 { -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
108};
109
110//--------------------------------------------------------------------------------------------------------------------------------------------------------------
111int main(void) {
112 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
113 const int BSIZ = 480*8;
114 mjr::ramCanvas1c16b::colorType aColor;
115 aColor.setChans(1);
116
117 for(decltype(params.size()) j=0; j<params.size(); ++j) {
118 mjr::ramCanvas1c16b theRamCanvas(BSIZ, BSIZ, params[j][10], params[j][11], params[j][12], params[j][13]);
119
120 double a = params[j][0];
121 double b = params[j][1];
122 double c = params[j][2];
123 double d = params[j][3];
124 double s = params[j][4];
125 double k = params[j][9];
126
127 double f = params[j][5];
128 double g = params[j][6];
129 double h = params[j][7];
130
131 /* Draw the Attractor on a 16-bit, greyscale canvas such that the level is the hit count for that pixel.
132 Thus we are using an "image" as a way to store field data instead of color information. */
133 double x = 0.0;
134 double y = 0.0;
135 uint64_t maxII = 0;
136 uint64_t inCnt = 0;
137 uint64_t maxItr = static_cast<uint64_t>(params[j][8]);
138 uint64_t iPrt = maxItr / 5;
139 for(uint64_t i=1;i<maxItr;i++) {
140 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));
141 double yNew = a-x;
142 if ( !theRamCanvas.isCliped(x, y)) {
143 inCnt++;
144 theRamCanvas.drawPoint(x, y, theRamCanvas.getPxColor(x, y).tfrmAdd(aColor));
145 if(theRamCanvas.getPxColor(x, y).getC0() > maxII) {
146 maxII = theRamCanvas.getPxColor(x, y).getC0();
147 if(maxII > 16384) { // 1/4 of max possible intensity
148 std::cout << "ITER(" << j << "): " << i << " MAXS: " << maxII << " EXIT: Maximum image intensity reached" << std::endl;
149 break;
150 }
151 }
152 }
153 if((i % iPrt) == 0)
154 std::cout << "ITER(" << j << "): " << i << " MAXS: " << maxII << " INC: " << inCnt << std::endl;
155 x=xNew;
156 y=yNew;
157 }
158 std::cout << "ITER(" << j << "): " << "DONE" << " MAXS: " << maxII << " INC: " << inCnt << std::endl;
159
160 // Log image transform
161 theRamCanvas.applyHomoPixTfrm(&mjr::ramCanvas1c16b::colorType::tfrmLn1);
162 maxII = static_cast<uint64_t>(std::log(static_cast<double>(maxII)));
163
164 /* Create a new image based on csCCfractal0RYBCW -- this one is 24-bit RGB color. */
165 mjr::ramCanvas3c8b anotherRamCanvas(BSIZ, BSIZ);
166 typedef mjr::ramCanvas3c8b::colorType::csCCfractal0RYBCW cs_t;
167 for(int yi=0;yi<theRamCanvas.getNumPixY();yi++)
168 for(int xi=0;xi<theRamCanvas.getNumPixX();xi++)
169 anotherRamCanvas.drawPoint(xi, yi, cs_t::c(k * theRamCanvas.getPxColor(xi, yi).getC0() / static_cast<mjr::ramCanvas3c8b::csFltType>(maxII)));
170
171 anotherRamCanvas.writeTIFFfile("barrymartin_" + mjr::math::str::fmt_int(j, 2, '0') + ".tiff");
172 }
173 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
174 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
175 return 0;
176}
177/** @endcond */
int main(int argc, char *argv[])