FuncViz examples
Generalized bitree/quadtree/octree library
Loading...
Searching...
No Matches
trefoil.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file trefoil.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @date 2024-07-14
7 @brief Trefoil parametric surface.@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 This example doesn't really demonstrate anything not found in the other examples. It's just a neat surface. ;)
33*/
34/*******************************************************************************************************************************************************.H.E.**/
35/** @cond exj */
36
37////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38#include <numbers> /* C++ math constants C++20 */
39
40////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
41#include "MR_rect_tree.hpp"
42#include "MR_cell_cplx.hpp"
43#include "MR_rt_to_cc.hpp"
44
45////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46typedef mjr::tree15b2d6rT tt_t;
47typedef mjr::MRccT5 cc_t;
48typedef mjr::MR_rt_to_cc<tt_t, cc_t> tc_t;
49
50////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51tt_t::rrpt_t trefoil(tt_t::drpt_t xvec) {
52 double u = xvec[0] * std::numbers::pi;
53 double v = xvec[1] * std::numbers::pi;
54 double r = 5;
55 double x = r * std::sin(3 * u) / (2 + std::cos(v));
56 double y = r * (std::sin(u) + 2 * std::sin(2 * u)) / (2 + std::cos(v + std::numbers::pi * 2 / 3));
57 double z = r / 2 * (std::cos(u) - 2 * std::cos(2 * u)) * (2 + std::cos(v)) * (2 + std::cos(v + std::numbers::pi * 2 / 3)) / 4;
58 double dxdu = (3*r*std::cos(3*u))/(std::cos(v)+2);
59 double dxdv = (r*std::sin(3*u)*std::sin(v))/(std::cos(v)+2)/(std::cos(v)+2);
60 double dydu = (r*(4*std::cos(2*u)+std::cos(u)))/(std::cos(v+(2*std::numbers::pi)/3)+2);
61 double dydv = (r*(2*std::sin(2*u)+std::sin(u))*std::sin(v+(2*std::numbers::pi)/3))/
62 ((std::cos(v+(2*std::numbers::pi)/3)+2)*(std::cos(v+(2*std::numbers::pi)/3)+2));
63 double dzdu = (r*(4*std::sin(2*u)-std::sin(u))*(std::cos(v)+2)*(std::cos(v+(2*std::numbers::pi)/3)+2))/8;
64 double dzdv = (-(r*(std::cos(u)-2*std::cos(2*u))*(std::cos(v)+2)*std::sin(v+(2*std::numbers::pi)/3))/8) -
65 (r*(std::cos(u)-2*std::cos(2*u))*std::sin(v)*(std::cos(v+(2*std::numbers::pi)/3)+2))/8;
66 double nx = dydu*dzdv-dydv*dzdu;
67 double ny = dxdv*dzdu-dxdu*dzdv;
68 double nz = dxdu*dydv-dxdv*dydu;
69 double nm = std::sqrt(nx*nx+ny*ny+nz*nz);
70 nm = (nm > 0 ? nm : 1);
71 nx = nx / nm;
72 ny = ny / nm;
73 nz = nz / nm;
74 return {x, y, z, nx, ny, nz};
75}
76
77////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78int main() {
79 tt_t tree;
80
81 cc_t ccplx;
82
83 tree.refine_grid(7, trefoil);
84
85 tree.dump_tree(20);
86
87 tc_t::construct_geometry_fans(ccplx,
88 tree,
89 2,
90 {{tc_t::val_src_spc_t::FRANGE, 0},
91 {tc_t::val_src_spc_t::FRANGE, 1},
92 {tc_t::val_src_spc_t::FRANGE, 2}});
93
94 ccplx.create_named_datasets({"u", "v", "x(u,v)", "y(u,v)", "z(u,v)", "nx", "ny", "nz"},
95 {{"NORMALS", {5, 6, 7}}});
96
97 ccplx.write_xml_vtk("trefoil.vtu", "trefoil");
98}
99/** @endcond */
int main()