MRaster examples 22.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
demo_corners.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file demo_corners.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Demonstrate the ramCanvas corners API.@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 code demonstrates one way to use an SDF function to color pixels using the "corners" API in ramCanvas. Note that the methodology show is inefficient
32 because it evaluates most coordinates four times, but the point here is to demonstrate the API... It also demonstrates the mjr::math::esgn library which is
33 part of MRMathCPP (https://github.com/richmit/MRMathCPP/).
34
35*/
36/*******************************************************************************************************************************************************.H.E.**/
37/** @cond exj */
38
39//--------------------------------------------------------------------------------------------------------------------------------------------------------------
40#include "ramCanvas.hpp"
41#include "MRMathESGN.hpp"
42
43//--------------------------------------------------------------------------------------------------------------------------------------------------------------
44double f(double x, double y);
45
46//--------------------------------------------------------------------------------------------------------------------------------------------------------------
47int main(void) {
48 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
49
50 mjr::ramCanvas3c8b theRamCanvas(7680/2, 4320/2, -10.0, 10.0, -6.0, 6.0);
51 //mjr::ramCanvas3c8b theRamCanvas(7680/2, 4320/2, -5.0, 5.0, -3.0, 3.0);
52
53 // Run across all the pixels, and paint them according to the height of f.
54 for(int yi=0;yi<theRamCanvas.getNumPixY();yi++) {
55 for(int xi=0;xi<theRamCanvas.getNumPixX();xi++) {
56 double fxy = f(theRamCanvas.int2realX(xi), theRamCanvas.int2realY(yi));
57 theRamCanvas.drawPoint(xi, yi, mjr::ramCanvas3c8b::colorType::csPGrey3x::c(static_cast<mjr::ramCanvas3c8b::csIntType>((382*(1+fxy)))));
58 //theRamCanvas.drawPoint(xi, yi, mjr::ramCanvas3c8b::colorType::csPGrey3x::c(static_cast<mjr::ramCanvas3c8b::csIntType>((764*std::abs(fxy)))));
59 }
60 }
61
62 // Run across all the pixels, and draw the ones with a zero crossing red.
63 for(int yi=0;yi<theRamCanvas.getNumPixY();yi++) {
64 for(int xi=0;xi<theRamCanvas.getNumPixX();xi++) {
65 std::vector<mjr::math::esgn::signT> signs(4);
66 for(int c=0;c<4;c++) {
67 auto fpt = theRamCanvas.int2realCorner(xi, yi, c);
68 signs[c] = mjr::math::esgn::sgn(f(fpt.x, fpt.y));
69 }
70 if (mjr::math::esgn::zero_or_change(signs))
71 theRamCanvas.drawPoint(xi, yi, mjr::ramCanvas3c8b::colorType::cornerColorEnum::RED);
72 }
73 }
74
75 theRamCanvas.writeTIFFfile("demo_corners.tiff");
76 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
77 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
78}
79
80//--------------------------------------------------------------------------------------------------------------------------------------------------------------
81double f(double x, double y) {
82 double z = y+2*std::exp(-x*x/4);
83 double d = std::sqrt(x*x+y*y);
84 double a = std::atan2(std::abs(x), std::abs(z)+0.1);
85 return std::sin(d*d)*std::sin(9*a*d+0*int(d+1));
86}
87
88/** @endcond */
int main(int argc, char *argv[])