dune-grid-glue  2.9
projectionwriter_impl.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-dune-grid-glue-exception
3 #include <fstream>
4 
5 namespace Dune {
6 namespace GridGlue {
7 
8 namespace ProjectionWriterImplementation {
9 
10 template<unsigned side, typename Coordinate, typename Corners>
11 void write_points(const Projection<Coordinate>& projection, const Corners& corners, std::ostream& out)
12 {
13  using namespace ProjectionImplementation;
14  using std::get;
15  const unsigned other_side = 1 - side;
16 
17  for (const auto& c : get<side>(corners))
18  out << c << "\n";
19 
20  for (const auto& i : get<side>(projection.images())) {
21  const auto global = interpolate(i, get<other_side>(corners));
22  out << global << "\n";
23  }
24 }
25 
26 template<unsigned side, typename Coordinate, typename Normals>
27 void write_normals(const Projection<Coordinate>& projection, const Normals& normals, std::ostream& out)
28 {
29  using namespace ProjectionImplementation;
30  using std::get;
31  const unsigned other_side = 1 - side;
32 
33  for (const auto& n : get<side>(normals))
34  out << n << "\n";
35 
36  for (const auto& x : get<side>(projection.images())) {
37  const auto n = interpolate_unit_normals(x, get<other_side>(normals));
38  out << n << "\n";
39  }
40 }
41 
42 template<typename Coordinate, typename Corners>
43 void write_edge_intersection_points(const Projection<Coordinate>& projection, const Corners& corners, std::ostream& out)
44 {
45  using namespace ProjectionImplementation;
46  using std::get;
47 
48  for (std::size_t i = 0; i < projection.numberOfEdgeIntersections(); ++i) {
49  const auto& local = projection.edgeIntersections()[i].local;
50  out << interpolate(local[0], get<0>(corners)) << "\n"
51  << interpolate(local[1], get<1>(corners)) << "\n";
52  }
53 }
54 
55 template<typename Coordinate, typename Normals>
56 void write_edge_intersection_normals(const Projection<Coordinate>& projection, const Normals& normals, std::ostream& out)
57 {
58  using namespace ProjectionImplementation;
59  using std::get;
60 
61  for (std::size_t i = 0; i < projection.numberOfEdgeIntersections(); ++i) {
62  const auto& local = projection.edgeIntersections()[i].local;
63  const auto n0 = interpolate_unit_normals(local[0], get<0>(normals));
64  const auto n1 = interpolate_unit_normals(local[1], get<1>(normals));
65 
66  out << n0 << "\n"
67  << n1 << "\n";
68  }
69 }
70 
71 template<unsigned side, typename Coordinate>
72 void write_success(const Projection<Coordinate>& projection, std::ostream& out)
73 {
74  using std::get;
75 
76  out << side << "\n";
77 
78  const auto& success = get<side>(projection.success());
79  for (std::size_t i = 0; i < success.size(); ++i)
80  out << (success[i] ? "1\n" : "0\n");
81 }
82 
83 } /* namespace ProjectionWriterImplementation */
84 
85 template<typename Coordinate, typename Corners, typename Normals>
86 void write(const Projection<Coordinate>& projection,
87  const Corners& corners,
88  const Normals& normals,
89  std::ostream& out)
90 {
91  using namespace ProjectionWriterImplementation;
92 
93  const auto numberOfEdgeIntersections = projection.numberOfEdgeIntersections();
94  const auto nPoints = 12 + 2 * numberOfEdgeIntersections;
95 
96  out << "# vtk DataFile Version2.0\n"
97  << "Filename: projection\n"
98  << "ASCII\n"
99  << "DATASET UNSTRUCTURED_GRID\n"
100  << "POINTS " << nPoints << " double\n";
101  write_points<0>(projection, corners, out);
102  write_points<1>(projection, corners, out);
103  write_edge_intersection_points(projection, corners, out);
104  out << "CELLS " << (8 + numberOfEdgeIntersections) << " " << (26 + 3 * numberOfEdgeIntersections) << "\n";
105  out << "3 0 1 2\n" "2 0 3\n" "2 1 4\n" "2 2 5\n"
106  << "3 6 7 8\n" "2 6 9\n" "2 7 10\n" "2 8 11\n";
107  for (std::size_t i = 0; i < numberOfEdgeIntersections; ++i)
108  out << "2 " << (12 + 2*i) << " " << (12 + 2*i + 1) << "\n";
109  out << "CELL_TYPES " << (8 + numberOfEdgeIntersections) << "\n" "5\n3\n3\n3\n" "5\n3\n3\n3\n";
110  for (std::size_t i = 0; i < numberOfEdgeIntersections; ++i)
111  out << "3\n";
112  out << "CELL_DATA " << (8 + numberOfEdgeIntersections) << "\n";
113  out << "SCALARS success int 1\n"
114  << "LOOKUP_TABLE success\n";
115  write_success<0>(projection, out);
116  write_success<1>(projection, out);
117  for (std::size_t i = 0; i < numberOfEdgeIntersections; ++i)
118  out << "2\n";
119  out << "LOOKUP_TABLE success 2\n"
120  << "1.0 0.0 0.0 1.0\n"
121  << "0.0 1.0 0.0 1.0\n";
122  out << "POINT_DATA " << nPoints << "\n"
123  << "NORMALS normals double\n";
124  write_normals<0>(projection, normals, out);
125  write_normals<1>(projection, normals, out);
126  write_edge_intersection_normals(projection, normals, out);
127 }
128 
129 template<typename Coordinate, typename Corners, typename Normals>
130 void write(const Projection<Coordinate>& projection,
131  const Corners& corners,
132  const Normals& normals,
133  const std::string& filename)
134 {
135  std::ofstream out(filename.c_str());
136  write(projection, corners, normals, out);
137 }
138 
139 template<typename Coordinate, typename Corners, typename Normals>
140 void print(const Projection<Coordinate>& projection,
141  const Corners& corners,
142  const Normals& normals)
143 {
144  using namespace ProjectionWriterImplementation;
145 
146  std::cout << "Side 0 corners and images:\n";
147  write_points<0>(projection, corners, std::cout);
148  std::cout << "Side 0 success:\n";
149  write_success<0>(projection, std::cout);
150  std::cout << "Side 1 corners and images:\n";
151  write_points<1>(projection, corners, std::cout);
152  std::cout << "Side 1 success:\n";
153  write_success<1>(projection, std::cout);
154  std::cout << "Side 0 normals and projected normals:\n";
155  write_normals<0>(projection, normals, std::cout);
156  std::cout << "Side 1 normals and projected normals:\n";
157  write_normals<1>(projection, normals, std::cout);
158  std::cout << projection.numberOfEdgeIntersections() << " edge intersections:\n";
159  write_edge_intersection_points(projection, corners, std::cout);
160 }
161 
162 } /* namespace GridGlue */
163 } /* namespace Dune */
Definition: gridglue.hh:37
void write(const Projection< Coordinate > &projection, const Corners &corners, const Normals &normals, std::ostream &out)
write projection in VTK format
Definition: projectionwriter_impl.hh:86
void print(const Projection< Coordinate > &projection, const Corners &corners, const Normals &normals)
Print information about the projection to std::cout stream.
Definition: projectionwriter_impl.hh:140
Corners::value_type interpolate(const Coordinate &x, const Corners &corners)
Definition: projection_impl.hh:70
Normals::value_type interpolate_unit_normals(const Coordinate &x, const Normals &normals)
Definition: projection_impl.hh:91
void write_normals(const Projection< Coordinate > &projection, const Normals &normals, std::ostream &out)
Definition: projectionwriter_impl.hh:27
void write_points(const Projection< Coordinate > &projection, const Corners &corners, std::ostream &out)
Definition: projectionwriter_impl.hh:11
void write_success(const Projection< Coordinate > &projection, std::ostream &out)
Definition: projectionwriter_impl.hh:72
void write_edge_intersection_points(const Projection< Coordinate > &projection, const Corners &corners, std::ostream &out)
Definition: projectionwriter_impl.hh:43
void write_edge_intersection_normals(const Projection< Coordinate > &projection, const Normals &normals, std::ostream &out)
Definition: projectionwriter_impl.hh:56
Projection of a line (triangle) on another line (triangle).
Definition: projection.hh:21
const std::tuple< std::bitset< dim >, std::bitset< dim > > & success() const
Indicate whether projection (inverse projection) is valid for each corner or not.
Definition: projection.hh:252
const std::tuple< Images, Preimages > & images() const
Images and preimages of corners.
Definition: projection.hh:235
unsigned numberOfEdgeIntersections() const
Number of edge intersections.
Definition: projection.hh:262
const std::array< EdgeIntersection, maxEdgeIntersections > & edgeIntersections() const
Edge-edge intersections.
Definition: projection.hh:273