MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
attracting_torus_shadow.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file attracting_torus_shadow.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw the intersection of the Attracting Torus Attractor with coordinate plains.@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 I first came across this strange attractor in Sprott's book "Elegant Automation" where it is the topic of chapter 43 starting on page 311. The system is
32 given by the following system of equations:
33
34 @f[\begin{align*}
35 \frac{dx}{dt} & = a_1 y \\
36 \frac{dy}{dt} & = -a_2 x - a_3 y z \\
37 \frac{dz}{dt} & = a_4 y^2 - a_5 + a_6 z
38 \end{align*}@f]
39
40 Typical values for the constants are @f$ a_1=a_2=a_3=a_4=1 @f$, @f$ a_5=4 @f$, and @f$ a_6=\frac{1}{10} @f$.
41
42 On page 315 of Sprott's book is an image of the intersection of the attractor with the @f$ z=0 @f$ plane. This program reproduces that image along with the
43 intersections of the @f$ x=0 @f$ & @f$ y=0 @f$ plane. As a bonus we produce projections of the curve as well.
44
45 Reference:
46 Sprott, Julien C. Elegant Automation: Robotic Analysis of Chaotic Systems. New Jersey: World Scientific, 2023.
47*/
48/*******************************************************************************************************************************************************.H.E.**/
49/** @cond exj */
50
51//--------------------------------------------------------------------------------------------------------------------------------------------------------------
52#include "ramCanvas.hpp"
53
54//--------------------------------------------------------------------------------------------------------------------------------------------------------------
55int main(void) {
56 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
57 const int XSIZ = 7680/1;
58 const int YSIZ = 4320/1;
59 mjr::ramCanvas3c8b theRamCanvasX0(XSIZ, YSIZ, -10, 10, -10, 10);
60 mjr::ramCanvas3c8b theRamCanvasY0(XSIZ, YSIZ, -10, 10, -10, 10);
61 mjr::ramCanvas3c8b theRamCanvasZ0(XSIZ, YSIZ, -10, 10, -10, 10);
62 mjr::ramCanvas3c8b theRamCanvasXY(XSIZ, YSIZ, -10, 10, -10, 10);
63 mjr::ramCanvas3c8b theRamCanvasXZ(XSIZ, YSIZ, -10, 10, -10, 10);
64 mjr::ramCanvas3c8b theRamCanvasYZ(XSIZ, YSIZ, -10, 10, -10, 10);
65
66 double isectDistToGo = 1e8; /* How long should the curve be for Z0 ? */
67 double curveDistToGo = 1e4; /* How long should the curve be for XY, XZ, & YZ? */
68
69 double a = 4.0; /* Equation paramaters */
70 double b = 0.1; /* Equation paramaters */
71 double x = 0.0; /* Initial conditions */
72 double y = 2.0; /* Initial conditions */
73 double z = 1.0; /* Initial conditions */
74
75 double targetDist = 0.025; /* Size of each step on curve */
76 int maxNumUpBisect = 5; /* Max times we double tDelta to get > targetDist. */
77 int maxNumDownBisect = 10; /* Max times we half tDelta to get < targetDist. */
78
79 /* Solve the equations..... */
80 double tDelta = 1.0;
81 double dist = 0.0;
82 double xOld = x;
83 double yOld = y;
84 double zOld = z;
85 double Xdelta, Ydelta, Zdelta, movDist;
86 while (dist < isectDistToGo) {
87 /* Find tDelta that gets us bigger than targetDist */
88 int numUpBisect = 0;
89 do {
90 tDelta += (2 * tDelta);
91 Xdelta = (y) * tDelta;
92 Ydelta = (-x-z*y) * tDelta;
93 Zdelta = (y*y-a+b*z) * tDelta;
94 movDist = sqrt (fabs (Xdelta * Xdelta + Ydelta * Ydelta + Zdelta * Zdelta));
95 numUpBisect++;
96 } while ((numUpBisect < maxNumUpBisect) && (movDist < targetDist));
97
98 /* Find tDelta that gets us a jump of <targetDist. */
99 int numDownBisect = 0;
100 do {
101 tDelta = tDelta / 2;
102 Xdelta = (y) * tDelta;
103 Ydelta = (-x-z*y) * tDelta;
104 Zdelta = (y*y-a+b*z) * tDelta;
105 movDist = sqrt (fabs (Xdelta * Xdelta + Ydelta * Ydelta + Zdelta * Zdelta));
106 numDownBisect++;
107 } while ((numDownBisect < maxNumDownBisect) && (movDist > targetDist));
108
109 /* Update and draw */
110 dist += movDist;
111 x = x + Xdelta;
112 y = y + Ydelta;
113 z = z + Zdelta;
114 if (dist < curveDistToGo) {
115 theRamCanvasXY.drawPoint(x, y, mjr::ramCanvas3c8b::colorType::cornerColorEnum::WHITE);
116 theRamCanvasXZ.drawPoint(x, z, mjr::ramCanvas3c8b::colorType::cornerColorEnum::WHITE);
117 theRamCanvasYZ.drawPoint(y, z, mjr::ramCanvas3c8b::colorType::cornerColorEnum::WHITE);
118 }
119 if(z*zOld<=0.0) {
120 if (z < 0) {
121 theRamCanvasZ0.drawPoint((x+xOld)/2.0, (y+yOld)/2.0, theRamCanvasZ0.getPxColor((x+xOld)/2.0, (y+yOld)/2.0).setC0(255));
122 } else {
123 theRamCanvasZ0.drawPoint((x+xOld)/2.0, (y+yOld)/2.0, theRamCanvasZ0.getPxColor((x+xOld)/2.0, (y+yOld)/2.0).setC2(255));
124 }
125 theRamCanvasZ0.drawPoint((x+xOld)/2.0, (y+yOld)/2.0, theRamCanvasZ0.getPxColor((x+xOld)/2.0, (y+yOld)/2.0).setC1(255));
126 }
127 if(x*xOld<=0.0) {
128 if (x < 0) {
129 theRamCanvasX0.drawPoint((y+yOld)/2.0, (z+zOld)/2.0, theRamCanvasX0.getPxColor((y+yOld)/2.0, (z+zOld)/2.0).setC0(255));
130 } else {
131 theRamCanvasX0.drawPoint((x+xOld)/2.0, (y+yOld)/2.0, theRamCanvasX0.getPxColor((y+yOld)/2.0, (z+zOld)/2.0).setC2(255));
132 }
133 theRamCanvasX0.drawPoint((y+yOld)/2.0, (z+zOld)/2.0, theRamCanvasX0.getPxColor((y+yOld)/2.0, (z+zOld)/2.0).setC1(255));
134 }
135 if(y*yOld<=0.0) {
136 if (y < 0) {
137 theRamCanvasY0.drawPoint((x+xOld)/2.0, (z+zOld)/2.0, theRamCanvasY0.getPxColor((x+xOld)/2.0, (z+zOld)/2.0).setC0(255));
138 } else {
139 theRamCanvasY0.drawPoint((x+xOld)/2.0, (y+yOld)/2.0, theRamCanvasY0.getPxColor((x+xOld)/2.0, (z+zOld)/2.0).setC2(255));
140 }
141 theRamCanvasY0.drawPoint((x+xOld)/2.0, (z+zOld)/2.0, theRamCanvasY0.getPxColor((x+xOld)/2.0, (z+zOld)/2.0).setC1(255));
142 }
143 xOld = x;
144 yOld = y;
145 zOld = z;
146 }
147
148 theRamCanvasXY.writeTIFFfile("attracting_torus_shadowXY.tiff");
149 theRamCanvasXZ.writeTIFFfile("attracting_torus_shadowXZ.tiff");
150 theRamCanvasYZ.writeTIFFfile("attracting_torus_shadowYZ.tiff");
151 theRamCanvasZ0.writeTIFFfile("attracting_torus_shadowZ0.tiff");
152 theRamCanvasX0.writeTIFFfile("attracting_torus_shadowX0.tiff");
153 theRamCanvasY0.writeTIFFfile("attracting_torus_shadowY0.tiff");
154
155 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
156 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
157
158 return 0;
159}
160/** @endcond */
int main(int argc, char *argv[])