MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
mandelbrot_potential.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file mandelbrot_potential.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief This program draws a Mandelbrot set using the "potential"@EOL
7 @std C++20
8 @see https://www.mitchr.me/SS/mandelbrot/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 This example program computes the potential function of the Mandelbrot set over various regions of the complex plane. For each region it produces three
33 images -- two POV-Ray height fields and a 16-bit greyscale images.
34
35 One can create a BOV-type data file like so:
36
37 dd if=mandelbrot_potential_2.mrw skip=100 iflag=skip_bytes of=mandelbrot_potential_2.mrd
38
39*/
40/*******************************************************************************************************************************************************.H.E.**/
41/** @cond exj */
42
43//--------------------------------------------------------------------------------------------------------------------------------------------------------------
44#include "ramCanvas.hpp"
45
46//--------------------------------------------------------------------------------------------------------------------------------------------------------------
47/** Reasons iteration may stop */
48enum class whyStopMPO { OUTSET, //!< Not in set (|z|>BALL)
49 MAXCOUNT, //!< Maximum iteration reached
50 INSET //!< In set (known region)
51 };
52
53/* This is out here so that it will get allocated on the heap. I'm too lazy to use malloc just this moment. */
54const int CSIZE = 4096;
55double theValues[CSIZE][CSIZE];
56
57double ranges[3][4] = { { -2.0, 1.0, -1.5, 1.5 },
58 { -0.12, -0.03, -0.92, -0.81 },
59 { 0.0353469, 0.5353469, 0.1153845, 0.6153845 } };
60
61//--------------------------------------------------------------------------------------------------------------------------------------------------------------
62int main(void) {
63 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
64 const int MAXITR = 6000;
65 const double BALL = 10000000.0;
66 double maxPot = 0;
67 double minPot = BALL;
68 mjr::ramCanvas3c8b::colorType aColor;
69 mjr::ramCanvas3c8b theRamCanvas(CSIZE, CSIZE);
70 whyStopMPO why;
71
72 for(int i=0; i<3; i++) {
73 //for(int i : { 0 } ) {
74 theRamCanvas.newRealCoords(ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
75 theRamCanvas.clrCanvasToBlack();
76 /* Compute the potential function on our grid. For off-set points we store the potential in an array,
77 for in-set points we store a -1. */
78 std::complex<double> z;
79 for(int y=0;y<theRamCanvas.getNumPixY();y++) {
80 if((y%(CSIZE/10))==0)
81 std::cout << " CASE: " << i << " LINE: " << y << "/" << CSIZE << std::endl;
82 for(int x=0;x<theRamCanvas.getNumPixX();x++) {
83 int count;
84 double cr = theRamCanvas.int2realX(x);
85 double ci = theRamCanvas.int2realY(y);
86 std::complex<double> c(cr, ci);
87 double p = std::abs(c-0.25);
88 if((cr >= p-2.0*p*p+0.25) && std::abs(c+1.0) >= 0.25) {
89 z=c;
90 for(count=0; ; count++) {
91 if(count>=MAXITR) {
92 why = whyStopMPO::MAXCOUNT;
93 break;
94 }
95 if(std::abs(z)>BALL) {
96 why = whyStopMPO::OUTSET;
97 break;
98 }
99 z = z * z + c;
100 }
101 } else {
102 why = whyStopMPO::INSET;
103 }
104 if(why == whyStopMPO::OUTSET) {
105 double pot = 0;
106 if(count<1000) {
107 double zn = std::norm(z);
108 if(zn > 1) {
109 pot = 0.5*log(zn)/pow(2.0, count);
110 }
111 }
112 theValues[x][y]=pot;
113 if(maxPot < pot) { maxPot = pot; }
114 if(minPot > pot) { minPot = pot; }
115 } else {
116 theValues[x][y] = -1;
117 }
118 }
119 }
120
121 std::cout << "MIN: " << minPot << " MAX:" << maxPot << std::endl;
122
123 /* Draw a POV-Ray height field from the potential data. This one will have the Mandelbrot set itself
124 set to zero height (black). This allows us to render canyon-like images. Rendering this data in 3D
125 will tend to emphasize the edge of the set (the walls of the canyon), so a high maximum iteration
126 count will yield better results. */
127 std::cout << "TGA_1" << std::endl;
128 for(int x=0;x<theRamCanvas.getNumPixX();x++) {
129 for(int y=0;y<theRamCanvas.getNumPixY();y++) {
130 double pot=theValues[x][y];
131 if(pot >= 0) {
132 pot = pot - minPot;
133 pot=(0xffff-1)-pot*(0xffff-2)/(maxPot-minPot);
134 aColor.setRGBcmpGreyTGA16bit(static_cast<uint16_t>(pot));
135 } else {
136 aColor.setToBlack();
137 }
138 theRamCanvas.drawPoint(x, y, aColor);
139 }
140 }
141 theRamCanvas.writeTGAfile("mandelbrot_potential_a_" + std::to_string(i) + ".tga");
142
143 /* Draw a POV-Ray height field from the potential data. This one will have the Mandelbrot set itself
144 set the maximum height. This allows us to render plateau-like images. */
145 std::cout << "TGA_2" << std::endl;
146 for(int x=0;x<theRamCanvas.getNumPixX();x++)
147 for(int y=0;y<theRamCanvas.getNumPixY();y++)
148 if(theValues[x][y] < 0)
149 theRamCanvas.drawPoint(x, y, aColor.setRGBcmpGreyTGA16bit(0xffff-1));
150 theRamCanvas.writeTGAfile("mandelbrot_potential_b_" + std::to_string(i) + ".tga");
151
152 /* We dump out 16-bit unsigned integers less than 2^15 so that they may be interpreted as signed
153 integers by tools like VisIT which might read the data via the BOV file. */
154 std::cout << "RAW_2" << std::endl;
155 mjr::ramCanvas1c16b theRamCanvasG(CSIZE, CSIZE);
156 for(int x=0;x<theRamCanvasG.getNumPixX();x++) {
157 for(int y=0;y<theRamCanvasG.getNumPixY();y++) {
158 double pot=theValues[x][y];
159 if(pot >= 0) {
160 pot = pot - minPot;
161 pot=(0x8000-1)-pot*(0x8000-2)/(maxPot-minPot);
162 theRamCanvasG.drawPoint(x, y, static_cast<mjr::ramCanvas1c16b::colorChanType>(pot));
163 } else {
164 theRamCanvasG.drawPoint(x, y, 0x8000-1);
165 }
166 }
167 }
168 theRamCanvasG.writeRAWfile("mandelbrot_potential_" + std::to_string(i) + ".mrw");
169 theRamCanvasG.autoHistStrech();
170 theRamCanvasG.writeTIFFfile("mandelbrot_potential_" + std::to_string(i) + ".tiff");
171 }
172 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
173 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
174}
175/** @endcond */
int main(int argc, char *argv[])