MRaster examples 22.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
mandelbrot_emboss.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_emboss.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 @copyright
9 @parblock
10 Copyright (c) 1988-2015, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
13
14 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
15
16 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 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
20 without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 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
27 DAMAGE.
28 @endparblock
29 @filedetails
30
31 This example program computes some "surfaces" related to the Mandelbrot set, and a pseudo-3D rendering of same.
32
33 * pot: Potential surface
34 * dist: Distance surface (Using the Milnor and Thurston distance estimator)
35
36*/
37/*******************************************************************************************************************************************************.H.E.**/
38/** @cond exj */
39
40//--------------------------------------------------------------------------------------------------------------------------------------------------------------
41#include "ramCanvas.hpp"
42#include "MRMathLINM.hpp"
43#include "MRMathIVL.hpp"
44
45//--------------------------------------------------------------------------------------------------------------------------------------------------------------
46const int CSIZE = 2048;
47const int MAXITR = 2048;
48const double BALL = 100;
49
50/* This is out here so that it will get allocated on the heap. I'm too lazy to use malloc just this moment. */
51double theValues[CSIZE][CSIZE];
52
53double ranges[5][4] = { { -2.0, 1.0, -1.5, 1.5 },
54 { -0.12, -0.03, -0.92, -0.81 },
55 { 0.0353469, 0.5353469, 0.1153845, 0.6153845 },
56 { -1.5, -1.0, -0.5, 0.0 },
57 { 0.0353469, 0.5353469, 0.1153845, 0.6153845 }
58 };
59
60/** Reasons iteration may stop */
61enum class whyStop { OUTSET, //!< Not in set (|z|>BALL)
62 MAXCOUNT, //!< Maximum iteration reached
63 INSET //!< In set (known region)
64 };
65
66//--------------------------------------------------------------------------------------------------------------------------------------------------------------
67int main(void) {
68 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
69 mjr::ramCanvas3c8b potRamCanvas(CSIZE, CSIZE), distRamCanvas(CSIZE, CSIZE);
70 mjr::ramCanvas3c8b::colorType theColor;
71 double lightHeight = 1.125;
72 double lightAngle = std::numbers::pi/4;
73 std::complex<double> lightDirection = exp(lightAngle*std::complex<double>(0,1));
74 whyStop why;
75
76 for(int i=0; i<3; i++) {
77 //for(int i : { 0 } ) {
78 potRamCanvas.newRealCoords(ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
79 potRamCanvas.clrCanvasToBlack();
80 /* Compute the potential function on our grid. For off-set points we store the potential in an array, for in-set points we store a -1. */
81 std::complex<double> z;
82 for(int y=0;y<potRamCanvas.getNumPixY();y++) {
83 if((y%(CSIZE/10))==0)
84 std::cout << " CASE: " << i << " LINE: " << y << "/" << CSIZE << std::endl;
85 for(int x=0;x<potRamCanvas.getNumPixX();x++) {
86 int count;
87 double cr = potRamCanvas.int2realX(x);
88 double ci = potRamCanvas.int2realY(y);
89 std::complex<double> c(cr, ci);
90 std::complex<double> dc = 1.0;
91 std::complex<double> pder = dc;
92 std::complex<double> dder1 = 1.0;
93 std::complex<double> dder2 = 0.0;
94 double p = std::abs(c-0.25);
95 if((cr >= p-2.0*p*p+0.25) && std::abs(c+1.0) >= 0.25) {
96 z=c;
97 for(count=0; ; count++) {
98 if(count>=MAXITR) {
99 why = whyStop::MAXCOUNT;
100 break;
101 }
102 if(std::abs(z)>BALL) {
103 why = whyStop::OUTSET;
104 break;
105 }
106 dder2 = 2.0 * (dder2 * z + dder1 * dder1);
107 dder1 = 2.0 * dder1 * z + 1.0;
108 pder = pder * 2.0 * z + dc;
109 z = z * z + c;
110 }
111 } else {
112 why = whyStop::INSET;
113 }
114
115 if(why == whyStop::OUTSET) {
116 if(std::abs(pder) < 0.00001) { // Should never happen...
117 potRamCanvas.drawPoint(x, y, "blue");
118 } else {
119 if(std::abs(z) < 0.00001) { // This can't happen...
120 potRamCanvas.drawPoint(x, y, "green");
121 } else {
122 std::complex<double> potNormal = z/pder;
123 potNormal = potNormal/std::abs(potNormal); // normalize potNormal
124 double potShade = std::real(potNormal * std::conj(lightDirection)) + lightHeight; // dot product with the incoming light
125 potShade = mjr::math::ivl::unit_clamp(potShade/(1+lightHeight)); // rescale so <=1, and then clamp to keep >=0
126 potRamCanvas.drawPoint(x, y, mjr::ramCanvas3c8b::colorType(static_cast<mjr::ramCanvas3c8b::colorChanType>(mjr::math::linm::scl_real_to_int(potShade, 255)),
127 static_cast<mjr::ramCanvas3c8b::colorChanType>(mjr::math::linm::scl_real_to_int(potShade, 255)),
128 static_cast<mjr::ramCanvas3c8b::colorChanType>(mjr::math::linm::scl_real_to_int(potShade, 255))));
129 }
130 }
131 } else {
132 potRamCanvas.drawPoint(x, y, "red");
133 }
134
135 if(why == whyStop::OUTSET) {
136 double zLogMod = 0.5*std::log(std::norm(z));
137 std::complex<double> distNormal = z*dder1*((1.0+zLogMod)*std::conj(dder1*dder1)-zLogMod*std::conj(z*dder2));
138 double distNormalAbs = std::abs(distNormal);
139 if(distNormalAbs < 0.00001) { // Should never happen...
140 distRamCanvas.drawPoint(x, y, "blue");
141 } else {
142 distNormal = distNormal/distNormalAbs; // normalize distNormal
143 double distShade = std::real(distNormal * std::conj(lightDirection)) + lightHeight; // dot product with the incoming light
144 distShade = mjr::math::ivl::unit_clamp(distShade/(1+lightHeight)); // rescale so <=1, and then clamp to keep >=0
145 distRamCanvas.drawPoint(x, y, mjr::color3c8b::csCCdiag01::c(static_cast<mjr::ramCanvas3c8b::csIntType>(mjr::math::linm::scl_real_to_int(distShade, 255))));
146 }
147 } else {
148 distRamCanvas.drawPoint(x, y, "red");
149 }
150 }
151 }
152
153 potRamCanvas.writeTIFFfile("mandelbrot_emboss_pot_" + std::to_string(i) + ".tiff");
154 distRamCanvas.writeTIFFfile("mandelbrot_emboss_dist_" + std::to_string(i) + ".tiff");
155 }
156 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
157 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
158}
159/** @endcond */
int main(int argc, char *argv[])