Debian Codegraph @

src/atlas/interpolation/method/PointSet.h

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 #pragma once
12
13 #include <stdint.h>
14 #include <cassert>
15 #include <fstream>
16 #include <limits>
17 #include <map>
18 #include <memory>
19 #include <vector>
20
21 #include "eckit/config/Resource.h"
22 #include "eckit/types/FloatCompare.h"
23
24 #include "atlas/interpolation/method/PointIndex3.h"
25 #include "atlas/mesh/Mesh.h"
26 #include "atlas/runtime/Exception.h"
27 #include "atlas/runtime/Trace.h"
28
29 namespace atlas {
30 namespace interpolation {
31 namespace method {
32
33 //----------------------------------------------------------------------------------------------------------------------
34
35 class PointSet {
36 public: // types
37 typedef PointIndex3::Point Point;
38 typedef PointIndex3::iterator iterator;
39 typedef std::map<size_t, size_t> DupStore_t;
40
41 public: // methods
42 PointSet(const std::vector<Point>& ipts);
43
44 PointSet(atlas::Mesh& mesh);
45
46 ~PointSet() { delete tree_; }
47
48 DupStore_t& duplicates() { return duplicates_; }
49
50 template <typename POINT_T>
51 void list_unique_points(std::vector<POINT_T>& opts) {
52 ATLAS_TRACE("Finding unique points");
53
54 ATLAS_ASSERT(opts.empty());
55
56 opts.reserve(npts_);
57
58 for (PointIndex3::iterator i = tree_->begin(); i != tree_->end(); ++i) {
59 Point p(i->point());
60 size_t ip = i->payload();
61 // std::cout << "point " << ip << " " << p << std::endl;
62 size_t uidx = unique(p, ip);
63 if (ip == uidx) {
64 opts.push_back(POINT_T(p.data()));
65 // std::cout << "----> UNIQ " << ip << std::endl;
66 }
67 else {
68 // std::cout << "----> DUP " << ip << " -> " << uidx <<
69 // std::endl;
70 }
71 // ++show_progress;
72 }
73 }
74
75 void list_unique_points(std::vector<size_t>& opts) {
76 ATLAS_TRACE("Finding unique points");
77
78 ATLAS_ASSERT(opts.empty());
79
80 opts.reserve(npts_);
81
82 for (PointIndex3::iterator i = tree_->begin(); i != tree_->end(); ++i) {
83 Point p(i->point());
84 size_t ip = i->payload();
85 // std::cout << "point " << ip << " " << p << std::endl;
86 size_t uidx = unique(p, ip);
87 if (ip == uidx) {
88 opts.push_back(ip);
89 // std::cout << "----> UNIQ " << ip << std::endl;
90 }
91 else {
92 // std::cout << "----> DUP " << ip << " -> " << uidx <<
93 // std::endl;
94 }
95 // ++show_progress;
96 }
97 }
98
99
100 size_t unique(const Point& p, size_t idx = std::numeric_limits<size_t>::max()) {
101 DupStore_t::iterator dit = duplicates_.find(idx);
102 if (dit != duplicates_.end()) {
103 // std::cout << " !! DUPLICATE !!" << std::endl;
104 return dit->second;
105 }
106 else
107 return this->search_unique(p, idx, 0);
108 }
109
110 iterator begin() { return tree_->begin(); }
111 iterator end() { return tree_->end(); }
112
113 size_t size() const { return npts_; }
114
115 protected: // methods
116 template <typename V>
117 void build(const V& ipts) {
118 static bool fastBuildKDTrees = eckit::Resource<bool>("$ATLAS_FAST_BUILD_KDTREES", true);
119 tree_ = new PointIndex3();
120
121 if (fastBuildKDTrees) {
122 std::vector<PointIndex3::Value> pidx;
123 pidx.reserve(npts_);
124
125 for (size_t ip = 0; ip < npts_; ++ip) {
126 pidx.push_back(PointIndex3::Value(PointIndex3::Point(ipts[ip]), ip));
127 }
128
129 tree_->build(pidx.begin(), pidx.end());
130 }
131 else {
132 for (size_t ip = 0; ip < npts_; ++ip) {
133 tree_->insert(PointIndex3::Value(PointIndex3::Point(ipts[ip]), ip));
134 }
135 }
136 }
137
138 size_t search_unique(const Point& p, size_t idx, uint32_t n);
139
140 protected:
141 size_t Kn(uint32_t n) {
142 if (!n)
143 return 2;
144 else
145 return 2 + std::pow(2, n) * 180;
146 }
147
148 bool duplicate(size_t idx) { return duplicates_.find(idx) != duplicates_.end(); }
149
150 private:
151 size_t npts_;
152 PointIndex3* tree_;
153 DupStore_t duplicates_; ///< map from duplicate idx to idx representing group
154 /// of points
155 };
156
157 //----------------------------------------------------------------------------------------------------------------------
158
159 } // namespace method
160 } // namespace interpolation
161 } // namespace atlas