FuncViz examples
Generalized bitree/quadtree/octree library
Loading...
Searching...
No Matches
surface_plot_corner.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file surface_plot_corner.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @brief Surface with a sharp edge.@EOL
7 @date 2024-07-16
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 The function illustrated here is continuous on the entire plane, but has no derivative on the unit circle. While no derivative exists on the unit circle,
33 directional derivatives pointing from the origin approach infinity as we get close to the unit circle. The derivative at the origin is zero. Thus the
34 surface is not only zero on the unit circle, but it drops to zero very quickly from it's local extrema at the origin.
35
36 If we sample on a uniform grid, some of the resulting polygons will have vertexes both inside and outside the unit circle. These polygons will never touch
37 the x-y plane, and thus the surface will not appear to have a uniform zero set on the unit circle. At low resolution the results are so bad they are
38 difficult to interpret. At higher resolutions we see what appears to be a jagged edge over the unit circle. Meaning the results are visually quite wrong,
39 but an astute viewer might well guess the true behavior of the function from the resulting image. In order to correct this graph we need sample points in
40 the triangulation that are on, or very near, the unit circle. We can do that by folding and resampling the cell complex on the unit circle.
41
42 - How to drive up the sample rate near a particular SDF -- so that we get higher resolution where the surface meets the plane.
43 - How to "fold" the resulting triangles to achieve higher accuracy on the non-differentiable edge.
44 - How to use tsampf_to_cdatf() & tsdf_to_csdf() to adapt functions designed for a MR_rect_tree for use with a MR_cell_cplx.
45*/
46/*******************************************************************************************************************************************************.H.E.**/
47/** @cond exj */
48
49////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50#include "MR_rect_tree.hpp"
51#include "MR_cell_cplx.hpp"
52#include "MR_rt_to_cc.hpp"
53
54////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55typedef mjr::tree15b2d1rT tt_t;
56typedef mjr::MRccT5 cc_t;
57typedef mjr::MR_rt_to_cc<tt_t, cc_t> tc_t;
58
59////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60
61tt_t::rrpt_t half_sphere_hat(tt_t::drpt_t xvec) {
62 double m = xvec[0] * xvec[0] + xvec[1] * xvec[1];
63 return (std::sqrt(std::abs(1-m)));
64}
65
66////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67tt_t::src_t unit_circle_sdf(tt_t::drpt_t xvec) {
68 double m = xvec[0] * xvec[0] + xvec[1] * xvec[1];
69 return (1-m);
70}
71
72////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73int main() {
74 tt_t tree({-1.1, -1.1},
75 { 1.1, 1.1});
76 cc_t ccplx;
77
78 /* Here is another way to get fine samples on the circle, but with a SDF this time. */
79 tree.refine_grid(5, half_sphere_hat);
80
81 /* Increase sample resolution on the unit circle. Here we do that with an SDF. */
82 tree.refine_leaves_recursive_cell_pred(7, half_sphere_hat, [&tree](int i) { return (tree.cell_cross_sdf(i, unit_circle_sdf)); });
83
84 /* Balance the three to the traditional level of 1 (no cell borders a cell more than half it's size) */
85 tree.balance_tree(1, half_sphere_hat);
86
87 /* Take a peek at the raw tree data */
88 tree.dump_tree(10);
89
90 /* Generate a cell complex from the tree samples */
91 tc_t::construct_geometry_fans(ccplx,
92 tree,
93 2,
94 {{tc_t::val_src_spc_t::FDOMAIN, 0},
95 {tc_t::val_src_spc_t::FDOMAIN, 1},
96 {tc_t::val_src_spc_t::FRANGE, 0}});
97
98 /* The single argument form of create_named_datasets() allows us to easily name data points. */
99 ccplx.create_named_datasets({"x", "y", "f(x,y)"});
100
101 /* Take a look at the generated cell complex */
102 ccplx.dump_cplx(10);
103
104 /* Fold all triangles that cross the unit circle! */
105 ccplx.triangle_folder([](cc_t::node_data_t x){return tc_t::tsampf_to_cdatf(half_sphere_hat, x); },
106 [](cc_t::node_data_t x){return tc_t::tsdf_to_csdf(unit_circle_sdf, x); });
107
108 /* Notice how it changed after the fold */
109 ccplx.dump_cplx(10);
110
111 ccplx.write_xml_vtk("surface_plot_corner.vtu", "surface_plot_corner");
112}
113/** @endcond */
int main()