| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <cmath> |
| 12 |
#include <iostream> |
| 13 |
#include <memory> |
| 14 |
#include <vector> |
| 15 |
#include "eckit/log/BigNum.h" |
| 16 |
#include "atlas/util/QhullSphericalTriangulation.h" |
| 17 |
#include "atlas/util/CGALSphericalTriangulation.h" |
| 18 |
#include "atlas/library/config.h" |
| 19 |
|
| 20 |
#include "atlas/array/ArrayView.h" |
| 21 |
#include "atlas/array/MakeView.h" |
| 22 |
#include "atlas/field/Field.h" |
| 23 |
#include "atlas/grid/Grid.h" |
| 24 |
#include "atlas/interpolation/method/PointSet.h" |
| 25 |
#include "atlas/mesh/ElementType.h" |
| 26 |
#include "atlas/mesh/HybridElements.h" |
| 27 |
#include "atlas/mesh/Mesh.h" |
| 28 |
#include "atlas/mesh/Nodes.h" |
| 29 |
#include "atlas/mesh/actions/BuildConvexHull3D.h" |
| 30 |
#include "atlas/runtime/Log.h" |
| 31 |
#include "atlas/runtime/Trace.h" |
| 32 |
#include "atlas/util/CoordinateEnums.h" |
| 33 |
|
| 34 |
using atlas::interpolation::method::PointSet; |
| 35 |
|
| 36 |
namespace atlas { |
| 37 |
namespace mesh { |
| 38 |
namespace actions { |
| 39 |
|
| 40 |
//---------------------------------------------------------------------------------------------------------------------- |
| 41 |
|
| 42 |
BuildConvexHull3D::BuildConvexHull3D(const eckit::Parametrisation& config) { |
| 43 |
config.get("remove_duplicate_points", remove_duplicate_points_ = true); |
| 44 |
} |
| 45 |
|
| 46 |
//---------------------------------------------------------------------------------------------------------------------- |
| 47 |
|
| 48 |
void BuildConvexHull3D::operator()(Mesh& mesh) const { |
| 49 |
// don't tesselate meshes already with triags or quads |
| 50 |
if (mesh.cells().size()) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
std::string default_backend = (ATLAS_HAVE_CGAL ? "cgal" : "qhull"); |
| 55 |
std::string backend = eckit::Resource<std::string>("$ATLAS_DELAUNAY_BACKEND",default_backend); |
| 56 |
|
| 57 |
ATLAS_TRACE("BuildConvexHull3D [" + backend + "]"); |
| 58 |
|
| 59 |
std::vector<size_t> local_index; |
| 60 |
if (remove_duplicate_points_) { |
| 61 |
PointSet points(mesh); |
| 62 |
points.list_unique_points(local_index); |
| 63 |
} |
| 64 |
|
| 65 |
auto add_triangles = [&]( const auto& triangles ) { |
| 66 |
const idx_t nb_triags = triangles.size(); |
| 67 |
mesh.cells().add(mesh::ElementType::create("Triangle"), nb_triags); |
| 68 |
mesh::HybridElements::Connectivity& triag_nodes = mesh.cells().node_connectivity(); |
| 69 |
auto triag_gidx = array::make_view<gidx_t, 1>(mesh.cells().global_index()); |
| 70 |
auto triag_part = array::make_view<int, 1>(mesh.cells().partition()); |
| 71 |
|
| 72 |
Log::debug() << "Inserting triags (" << eckit::BigNum(nb_triags) << ")" << std::endl; |
| 73 |
|
| 74 |
for (idx_t tidx = 0; tidx<nb_triags; ++tidx) { |
| 75 |
auto& t = triangles[tidx]; |
| 76 |
std::array<idx_t,3> idx{t[0],t[1],t[2]}; |
| 77 |
if( local_index.size() ) { |
| 78 |
idx[0] = local_index[idx[0]]; |
| 79 |
idx[1] = local_index[idx[1]]; |
| 80 |
idx[2] = local_index[idx[2]]; |
| 81 |
} |
| 82 |
triag_nodes.set(tidx, idx.data()); |
| 83 |
triag_gidx(tidx) = tidx + 1; |
| 84 |
triag_part(tidx) = 0; |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
if( local_index.size() == mesh.nodes().size() or local_index.empty() ) { |
| 90 |
local_index.clear(); |
| 91 |
auto lonlat = array::make_view<double,2>(mesh.nodes().lonlat()); |
| 92 |
if (backend == "qhull") { |
| 93 |
ATLAS_TRACE("qhull"); |
| 94 |
auto triangles = util::QhullSphericalTriangulation{static_cast<size_t>(lonlat.shape(0)),lonlat.data()}.triangles(); |
| 95 |
add_triangles(triangles); |
| 96 |
} |
| 97 |
else if (backend == "cgal") { |
| 98 |
ATLAS_TRACE("cgal"); |
| 99 |
auto triangles = util::CGALSphericalTriangulation{static_cast<size_t>(lonlat.shape(0)),lonlat.data()}.triangles(); |
| 100 |
add_triangles(triangles); |
| 101 |
} |
| 102 |
else { |
| 103 |
ATLAS_THROW_EXCEPTION("backend " << backend << " not supported"); |
| 104 |
} |
| 105 |
} |
| 106 |
else { |
| 107 |
auto lonlat_view = array::make_view<double,2>(mesh.nodes().lonlat()); |
| 108 |
|
| 109 |
std::vector<PointLonLat> lonlat(local_index.size()); |
| 110 |
size_t jnode = 0; |
| 111 |
for( auto& ip: local_index ) { |
| 112 |
lonlat[jnode] = {lonlat_view(ip,0),lonlat_view(ip,1)}; |
| 113 |
++jnode; |
| 114 |
} |
| 115 |
if (backend == "qhull") { |
| 116 |
ATLAS_TRACE("qhull"); |
| 117 |
auto triangles = util::QhullSphericalTriangulation{lonlat}.triangles(); |
| 118 |
add_triangles(triangles); |
| 119 |
} |
| 120 |
else if (backend == "cgal") { |
| 121 |
ATLAS_TRACE("cgal"); |
| 122 |
auto triangles = util::CGALSphericalTriangulation{lonlat}.triangles(); |
| 123 |
add_triangles(triangles); |
| 124 |
} |
| 125 |
else { |
| 126 |
ATLAS_THROW_EXCEPTION("backend " << backend << " not supported"); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
//---------------------------------------------------------------------------------------------------------------------- |
| 133 |
|
| 134 |
} // namespace actions |
| 135 |
} // namespace mesh |
| 136 |
} // namespace atlas |