Debian Codegraph @

src/atlas/interpolation/method/PointSet.cc

raw
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 "eckit/config/Resource.h"
12
13 #include "atlas/array/ArrayView.h"
14 #include "atlas/array/MakeView.h"
15 #include "atlas/field/Field.h"
16 #include "atlas/interpolation/method/PointSet.h"
17 #include "atlas/mesh/Mesh.h"
18 #include "atlas/mesh/Nodes.h"
19 #include "atlas/runtime/Exception.h"
20
21 using namespace eckit;
22
23 namespace atlas {
24 namespace interpolation {
25 namespace method {
26
27 //----------------------------------------------------------------------------------------------------------------------
28
29 PointSet::PointSet(const std::vector<Point>& ipts): npts_(ipts.size()) {
30 build(ipts);
31 }
32
33 PointSet::PointSet(Mesh& mesh) {
34 mesh::Nodes& nodes = mesh.nodes();
35
36 npts_ = nodes.size();
37
38 ATLAS_ASSERT(npts_ > 0);
39
40 ATLAS_ASSERT(nodes.has_field("xyz"));
41
42 array::ArrayView<double, 2> coords = array::make_view<double, 2>(nodes.field("xyz"));
43 static bool fastBuildKDTrees = eckit::Resource<bool>("$ATLAS_FAST_BUILD_KDTREES", true);
44
45 tree_ = new PointIndex3();
46
47 if (fastBuildKDTrees) {
48 std::vector<PointIndex3::Value> pidx;
49 pidx.reserve(npts_);
50
51 for (size_t ip = 0; ip < npts_; ++ip) {
52 pidx.emplace_back(PointIndex3::Point(coords(ip, (size_t)0), coords(ip, (size_t)1), coords(ip, (size_t)2)),
53 ip);
54 }
55 tree_->build(pidx.begin(), pidx.end());
56 }
57 else {
58 for (size_t ip = 0; ip < npts_; ++ip) {
59 tree_->insert(PointIndex3::Value(
60 PointIndex3::Point(coords(ip, (size_t)0), coords(ip, (size_t)1), coords(ip, (size_t)2)), ip));
61 }
62 }
63 }
64
65 size_t PointSet::search_unique(const Point& p, size_t idx, uint32_t n) {
66 PointIndex3::NodeList nearest = tree_->kNearestNeighbours(p, Kn(n));
67
68 std::vector<size_t> equals;
69 equals.reserve(nearest.size());
70
71 for (size_t i = 0; i < nearest.size(); ++i) {
72 Point np = nearest[i].value().point();
73 size_t nidx = nearest[i].value().payload();
74
75 // std::cout << " - " << nidx << " " << np << std::endl;
76
77 if (eckit::geometry::points_equal(p, np)) {
78 // std::cout << " EQUAL !!" << std::endl;
79 equals.push_back(nidx);
80 }
81 else {
82 break;
83 }
84 }
85
86 if (equals.size() == nearest.size()) /* need to increase the search to find
87 all duplicates of this point */
88 {
89 return this->search_unique(p, idx, ++n);
90 }
91 else /* stop recursion */
92 {
93 size_t ret = idx; /* if nothing found return same idx */
94
95 if (equals.size() >= 1) { /* if an equal point was found return the first one */
96 ret = equals[0];
97 }
98
99 for (size_t n = 1; n < equals.size(); ++n) {
100 duplicates_[equals[n]] = ret;
101 }
102
103 return ret;
104 }
105 }
106
107 //----------------------------------------------------------------------------------------------------------------------
108
109 } // namespace method
110 } // namespace interpolation
111 } // namespace atlas