MRaster examples 21.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
37//--------------------------------------------------------------------------------------------------------------------------------------------------------------
38typedef mjr::ramCanvas3c8b::coordFltType drT;
39
40//--------------------------------------------------------------------------------------------------------------------------------------------------------------
41drT f(drT x, drT y);
42void drawLevelCurves(int numBand, drT bandWidth, drT zMin, drT zMax, drT minRealX, drT maxRealX, drT minRealY, drT maxRealY, const char *file);
43
44//--------------------------------------------------------------------------------------------------------------------------------------------------------------
45int main(void) {
46 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
47 drawLevelCurves(10, 0.0, 0, 0, -20, 20, -20, 20, "level_curves_l.tiff");
48 drawLevelCurves( 0, 0.0, 0, 0, -20, 20, -20, 20, "level_curves_c.tiff");
49 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
50 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
51}
52
53//--------------------------------------------------------------------------------------------------------------------------------------------------------------
54drT f(drT x, drT y) {
55 return sin(x)+cos(y)+sqrt(x*x+y*y)*0.05;
56}
57
58//--------------------------------------------------------------------------------------------------------------------------------------------------------------
59void drawLevelCurves(int numBand, drT bandWidth, drT zMin, drT zMax, drT minRealX, drT maxRealX, drT minRealY, drT maxRealY, const char *file) {
60 int x, y, foundBand;
61 mjr::ramCanvas3c8b::csIntType clr;
62 drT fxy, bandGap, band, minDist, minBand;
63
64 mjr::ramCanvas3c8b theRamCanvas(1024, 1024, minRealX, maxRealX, minRealY, maxRealY);
65
66 // Compute zMin and zMax if we need to.
67 if( (zMax == 0) && (zMin == 0) ) {
68 fprintf(stderr, "Compute zMax and zMin\n");
69 zMax = zMin = f(theRamCanvas.int2realX(0), theRamCanvas.int2realY(0));
70 for(y=0;y<theRamCanvas.getNumPixY();y++) {
71 for(x=0;x<theRamCanvas.getNumPixX();x++) {
72 fxy = f(theRamCanvas.int2realX(x), theRamCanvas.int2realY(y));
73 if(fxy > zMax) zMax=fxy;
74 if(fxy < zMin) zMin=fxy;
75 }
76 }
77 }
78
79 bandGap = 1.0*(zMax-zMin)/numBand;
80 fprintf(stderr, "Compute curves\n");
81 for(y=0;y<theRamCanvas.getNumPixY();y++) {
82 for(x=0;x<theRamCanvas.getNumPixX();x++) {
83 fxy = f(theRamCanvas.int2realX(x), theRamCanvas.int2realY(y));
84 if(fxy < zMin) {
85 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(255, 0, 0));
86 } else {
87 if(fxy > zMax) {
88 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(0, 255, 0));
89 } else {
90 if(numBand == 0) {
91 clr = static_cast<mjr::ramCanvas3c8b::csIntType>(mjr::math::linm::gen_map(fxy, zMin, zMax, (drT)0, (drT)(255*4+1)));
92 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType::csCColdeIceToWaterToHot::c(clr));
93 } else {
94 if(bandWidth == 0) {
95 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(0, 0, 255));
96 foundBand=0;
97 minDist = (zMax-zMin+1);
98 minBand = 0.0;
99 for(band=zMax; band>zMin; band=band-bandGap) {
100 if(fabs(band-fxy) < minDist) {
101 minDist = fabs(band-fxy);
102 minBand = band;
103 foundBand = 1;
104 }
105 }
106 if(foundBand) {
107 clr = static_cast<mjr::ramCanvas3c8b::csIntType>(mjr::math::linm::gen_map(minBand, zMin, zMax, (drT)0, (drT)(255*4+1)));
108 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType::csCColdeIceToWaterToHot::c(clr));
109 }
110 } else {
111 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(0, 0, 255));
112 foundBand=0;
113 for(band=zMax; band>zMin; band=band-bandGap) {
114 if(fabs(band-fxy) < bandWidth) {
115 clr = (int)((zMax-fxy)/(zMax-zMin)*250);
116 theRamCanvas.setDfltColor(mjr::ramCanvas3c8b::colorType(255, 255, 255));
117 foundBand = 1;
118 break;
119 }
120 }
121 }
122 }
123 }
124 }
125 theRamCanvas.drawPoint(x, y);
126 }
127 }
128
129 theRamCanvas.writeTIFFfile(file);
130}
131/** @endcond */
int main(int argc, char *argv[])