MRaster examples 22.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
level_curves.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file level_curves.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw level curves for a real valued function on two real variables.@EOL
7 @keywords
8 @std C++20
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*/
31/*******************************************************************************************************************************************************.H.E.**/
32/** @cond exj */
33
34//--------------------------------------------------------------------------------------------------------------------------------------------------------------
35#include "ramCanvas.hpp"
36#include "MRMathLINM.hpp"
37
38//--------------------------------------------------------------------------------------------------------------------------------------------------------------
39typedef mjr::ramCanvas3c8b::coordFltType drT;
40
41//--------------------------------------------------------------------------------------------------------------------------------------------------------------
42drT f(drT x, drT y);
43void drawLevelCurves(int numBand, drT bandWidth, drT zMin, drT zMax, drT minRealX, drT maxRealX, drT minRealY, drT maxRealY, const char *file);
44
45//--------------------------------------------------------------------------------------------------------------------------------------------------------------
46int main(void) {
47 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
48 drawLevelCurves(10, 0.0, 0, 0, -20, 20, -20, 20, "level_curves_l.tiff");
49 drawLevelCurves( 0, 0.0, 0, 0, -20, 20, -20, 20, "level_curves_c.tiff");
50 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
51 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
52}
53
54//--------------------------------------------------------------------------------------------------------------------------------------------------------------
55drT f(drT x, drT y) {
56 return sin(x)+cos(y)+sqrt(x*x+y*y)*0.05;
57}
58
59//--------------------------------------------------------------------------------------------------------------------------------------------------------------
60void drawLevelCurves(int numBand, drT bandWidth, drT zMin, drT zMax, drT minRealX, drT maxRealX, drT minRealY, drT maxRealY, const char *file) {
61 int x, y, foundBand;
62 mjr::ramCanvas3c8b::csIntType clr;
63 drT fxy, bandGap, band, minDist, minBand;
64
65 mjr::ramCanvas3c8b theRamCanvas(1024, 1024, minRealX, maxRealX, minRealY, maxRealY);
66
67 // Compute zMin and zMax if we need to.
68 if( (zMax == 0) && (zMin == 0) ) {
69 fprintf(stderr, "Compute zMax and zMin\n");
70 zMax = zMin = f(theRamCanvas.int2realX(0), theRamCanvas.int2realY(0));
71 for(y=0;y<theRamCanvas.getNumPixY();y++) {
72 for(x=0;x<theRamCanvas.getNumPixX();x++) {
73 fxy = f(theRamCanvas.int2realX(x), theRamCanvas.int2realY(y));
74 if(fxy > zMax) zMax=fxy;
75 if(fxy < zMin) zMin=fxy;
76 }
77 }
78 }
79
80 bandGap = 1.0*(zMax-zMin)/numBand;
81 fprintf(stderr, "Compute curves\n");
82 for(y=0;y<theRamCanvas.getNumPixY();y++) {
83 for(x=0;x<theRamCanvas.getNumPixX();x++) {
84 fxy = f(theRamCanvas.int2realX(x), theRamCanvas.int2realY(y));
85 if(fxy < zMin) {
86 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(255, 0, 0));
87 } else {
88 if(fxy > zMax) {
89 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(0, 255, 0));
90 } else {
91 if(numBand == 0) {
92 clr = static_cast<mjr::ramCanvas3c8b::csIntType>(mjr::math::linm::gen_map(fxy, zMin, zMax, (drT)0, (drT)(255*4+1)));
93 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType::csCColdeIceToWaterToHot::c(clr));
94 } else {
95 if(bandWidth == 0) {
96 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(0, 0, 255));
97 foundBand=0;
98 minDist = (zMax-zMin+1);
99 minBand = 0.0;
100 for(band=zMax; band>zMin; band=band-bandGap) {
101 if(fabs(band-fxy) < minDist) {
102 minDist = fabs(band-fxy);
103 minBand = band;
104 foundBand = 1;
105 }
106 }
107 if(foundBand) {
108 clr = static_cast<mjr::ramCanvas3c8b::csIntType>(mjr::math::linm::gen_map(minBand, zMin, zMax, (drT)0, (drT)(255*4+1)));
109 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType::csCColdeIceToWaterToHot::c(clr));
110 }
111 } else {
112 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(0, 0, 255));
113 foundBand=0;
114 for(band=zMax; band>zMin; band=band-bandGap) {
115 if(fabs(band-fxy) < bandWidth) {
116 clr = (int)((zMax-fxy)/(zMax-zMin)*250);
117 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(255, 255, 255));
118 foundBand = 1;
119 break;
120 }
121 }
122 }
123 }
124 }
125 }
126 theRamCanvas.drawPoint(x, y);
127 }
128 }
129
130 theRamCanvas.writeTIFFfile(file);
131}
132/** @endcond */
int main(int argc, char *argv[])