FuncViz examples
Generalized bitree/quadtree/octree library
Loading...
Searching...
No Matches
curve_plot.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file curve_plot.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @date 2024-07-18
7 @brief A simple curve plot.@EOL
8 @std C++23
9 @copyright
10 @parblock
11 Copyright (c) 2024, Mitchell Jay Richling <http://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 @filedetails
31
32 Univariate function plots are the bread-and-butter of the plotting world. Normally a simple, uniformly spaced, sequence is enough to get the job
33 done quite nicely. Still, a few things can come up:
34
35 - Jump discontinuities & Vertical asymptotes: Resolved with higher sampling near the discontinuities and a cutting edge (TBD)
36 - Isolated, non-differentiable points: Resolved with higher sampling near the points and a folding edge (TBD)
37 - Undefined intervals: Resolved with higher sampling near the edges and NaN edge repair
38 - Regions of high oscillation: Resolved with higher sampling on the regions
39 - Extrema: Resolved with higher sampling near the extrema
40
41 Note that most of the items above are listed TBD. A few features need to be added to MR_rt_to_cc. ;) Note the TODO comments in the body of main().
42*/
43/*******************************************************************************************************************************************************.H.E.**/
44/** @cond exj */
45
46////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47#include "MR_rect_tree.hpp"
48#include "MR_cell_cplx.hpp"
49#include "MR_rt_to_cc.hpp"
50
51////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52typedef mjr::tree15b1d1rT tt_t;
53typedef mjr::MRccT5 cc_t;
54typedef mjr::MR_rt_to_cc<tt_t, cc_t> tc_t;
55
56////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57tt_t::rrpt_t f(tt_t::drpt_t x) {
58 double ret = (x<0?-1:1)*std::pow(std::abs(x), 1/3.0) * std::sqrt((x+1.5)*(x+1.5)-1) * (x-2);
59 if (x>2)
60 ret = 2+std::sin(20*x);
61 if (ret < -3)
62 ret = -3;
63 if (ret > 3.2)
64 ret = 3.2;
65 return ret;
66}
67
68////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69int main() {
70 tt_t tree(-3, 3);
71 cc_t ccplx;
72
73 // Sample a uniform grid across the domain
74 tree.refine_grid(5, f);
75
76 // Refine near NaN
77 tree.refine_recursive_if_cell_vertex_is_nan(10, f);
78 // TODO: Add NaN edge repair when implemented in MR_rt_to_cc
79
80 // Refine near vertical tangent line
81 tree.refine_leaves_recursive_cell_pred(10, f, [&tree](tt_t::diti_t i) { return (tree.cell_near_domain_point(0.0, 1.0e-2, i)); });
82 // TODO: Use derivative test for this
83
84 // Step discontinuities at 2.
85 tree.refine_leaves_recursive_cell_pred(10, f, [&tree](tt_t::diti_t i) { return (tree.cell_near_domain_point(2.0, 1.0e-2, i)); });
86 // TODO: Add cell cut when implemented in MR_rt_to_cc
87
88 // Non differentiable point near x=-2.619185320
89 tree.refine_leaves_recursive_cell_pred(11, f, [&tree](tt_t::diti_t i) { return (tree.cell_near_domain_point(-2.619185320, 1.0e-2, i)); });
90 // TODO: Add folding edge when implemented in MR_rt_to_cc
91
92 // High oscillation from [2,3]
93 tree.refine_leaves_recursive_cell_pred(10, f, [&tree](tt_t::diti_t i) { return (tree.diti_to_drpt(i) >= 2.0); });
94
95 // Extrema near -0.2171001290
96 tree.refine_leaves_recursive_cell_pred(10, f, [&tree](tt_t::diti_t i) { return (tree.cell_near_domain_point(-0.2171001290, 1.0e-2, i)); });
97 // TODO: Use derivative test for this
98
99 // Extrema near 0.8775087009
100 tree.refine_leaves_recursive_cell_pred(8, f, [&tree](tt_t::diti_t i) { return (tree.cell_near_domain_point(0.8775087009, 1.0e-2, i)); });
101 // TODO: Use derivative test for this
102
103 tree.dump_tree(10);
104
105 tc_t::construct_geometry_fans(ccplx,
106 tree,
107 1,
108 {{tc_t::val_src_spc_t::FDOMAIN, 0 },
109 {tc_t::val_src_spc_t::FRANGE, 0 },
110 {tc_t::val_src_spc_t::CONSTANT, 0.0}},
111 f
112 );
113
114 // Note the first argument need not name *every* data element, just the first ones.
115 ccplx.create_named_datasets({"x", "f(x)"});
116
117 ccplx.dump_cplx(10);
118
119 ccplx.write_xml_vtk("curve_plot.vtu", "curve_plot");
120}
121/** @endcond */
int main()
double f(double x, double y)