MRaster examples 21.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
43//--------------------------------------------------------------------------------------------------------------------------------------------------------------
44const int CSIZE = 2048;
45const int MAXITR = 2048;
46const double BALL = 100;
47
48/* This is out here so that it will get allocated on the heap. I'm too lazy to use malloc just this moment. */
49double theValues[CSIZE][CSIZE];
50
51double ranges[5][4] = { { -2.0, 1.0, -1.5, 1.5 },
52 { -0.12, -0.03, -0.92, -0.81 },
53 { 0.0353469, 0.5353469, 0.1153845, 0.6153845 },
54 { -1.5, -1.0, -0.5, 0.0 },
55 { 0.0353469, 0.5353469, 0.1153845, 0.6153845 }
56 };
57
58/** Reasons iteration may stop */
59enum class whyStop { OUTSET, //!< Not in set (|z|>BALL)
60 MAXCOUNT, //!< Maximum iteration reached
61 INSET //!< In set (known region)
62 };
63
64//--------------------------------------------------------------------------------------------------------------------------------------------------------------
65int main(void) {
66 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
67 mjr::ramCanvas3c8b potRamCanvas(CSIZE, CSIZE), distRamCanvas(CSIZE, CSIZE);
68 mjr::ramCanvas3c8b::colorType theColor;
69 double lightHeight = 1.125;
70 double lightAngle = std::numbers::pi/4;
71 std::complex<double> lightDirection = exp(lightAngle*std::complex<double>(0,1));
72 whyStop why;
73
74 for(int i=0; i<3; i++) {
75 //for(int i : { 0 } ) {
76 potRamCanvas.newRealCoords(ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
77 potRamCanvas.clrCanvasToBlack();
78 /* 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. */
79 std::complex<double> z;
80 for(int y=0;y<potRamCanvas.getNumPixY();y++) {
81 if((y%(CSIZE/10))==0)
82 std::cout << " CASE: " << i << " LINE: " << y << "/" << CSIZE << std::endl;
83 for(int x=0;x<potRamCanvas.getNumPixX();x++) {
84 int count;
85 double cr = potRamCanvas.int2realX(x);
86 double ci = potRamCanvas.int2realY(y);
87 std::complex<double> c(cr, ci);
88 std::complex<double> dc = 1.0;
89 std::complex<double> pder = dc;
90 std::complex<double> dder1 = 1.0;
91 std::complex<double> dder2 = 0.0;
92 double p = std::abs(c-0.25);
93 if((cr >= p-2.0*p*p+0.25) && std::abs(c+1.0) >= 0.25) {
94 z=c;
95 for(count=0; ; count++) {
96 if(count>=MAXITR) {
97 why = whyStop::MAXCOUNT;
98 break;
99 }
100 if(std::abs(z)>BALL) {
101 why = whyStop::OUTSET;
102 break;
103 }
104 dder2 = 2.0 * (dder2 * z + dder1 * dder1);
105 dder1 = 2.0 * dder1 * z + 1.0;
106 pder = pder * 2.0 * z + dc;
107 z = z * z + c;
108 }
109 } else {
110 why = whyStop::INSET;
111 }
112
113 if(why == whyStop::OUTSET) {
114 if(std::abs(pder) < 0.00001) { // Should never happen...
115 potRamCanvas.drawPoint(x, y, "blue");
116 } else {
117 if(std::abs(z) < 0.00001) { // This can't happen...
118 potRamCanvas.drawPoint(x, y, "green");
119 } else {
120 std::complex<double> potNormal = z/pder;
121 potNormal = potNormal/std::abs(potNormal); // normalize potNormal
122 double potShade = std::real(potNormal * std::conj(lightDirection)) + lightHeight; // dot product with the incoming light
123 potShade = mjr::math::ivl::unit_clamp(potShade/(1+lightHeight)); // rescale so <=1, and then clamp to keep >=0
124 potRamCanvas.drawPoint(x, y, mjr::ramCanvas3c8b::colorType(static_cast<mjr::ramCanvas3c8b::colorChanType>(mjr::math::linm::scl_real_to_int(potShade, 255)),
125 static_cast<mjr::ramCanvas3c8b::colorChanType>(mjr::math::linm::scl_real_to_int(potShade, 255)),
126 static_cast<mjr::ramCanvas3c8b::colorChanType>(mjr::math::linm::scl_real_to_int(potShade, 255))));
127 }
128 }
129 } else {
130 potRamCanvas.drawPoint(x, y, "red");
131 }
132
133 if(why == whyStop::OUTSET) {
134 double zLogMod = 0.5*std::log(std::norm(z));
135 std::complex<double> distNormal = z*dder1*((1.0+zLogMod)*std::conj(dder1*dder1)-zLogMod*std::conj(z*dder2));
136 double distNormalAbs = std::abs(distNormal);
137 if(distNormalAbs < 0.00001) { // Should never happen...
138 distRamCanvas.drawPoint(x, y, "blue");
139 } else {
140 distNormal = distNormal/distNormalAbs; // normalize distNormal
141 double distShade = std::real(distNormal * std::conj(lightDirection)) + lightHeight; // dot product with the incoming light
142 distShade = mjr::math::ivl::unit_clamp(distShade/(1+lightHeight)); // rescale so <=1, and then clamp to keep >=0
143 distRamCanvas.drawPoint(x, y, mjr::color3c8b::csCCdiag01::c(static_cast<mjr::ramCanvas3c8b::csIntType>(mjr::math::linm::scl_real_to_int(distShade, 255))));
144 }
145 } else {
146 distRamCanvas.drawPoint(x, y, "red");
147 }
148 }
149 }
150
151 potRamCanvas.writeTIFFfile("mandelbrot_emboss_pot_" + std::to_string(i) + ".tiff");
152 distRamCanvas.writeTIFFfile("mandelbrot_emboss_dist_" + std::to_string(i) + ".tiff");
153 }
154 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
155 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
156}
157/** @endcond */
int main(int argc, char *argv[])