File: FuzzyPointLocator.h

package info (click to toggle)
mshr 2018.1.0%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,136 kB
  • sloc: cpp: 9,808; python: 680; makefile: 242; sh: 62; ansic: 11
file content (145 lines) | stat: -rw-r--r-- 4,868 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2016 Benjamin Kehlet
//
// This file is part of mshr.
//
// mshr is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mshr is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with mshr.  If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Anders Logg 2016

#ifndef FUZZY_POINT_SET_H
#define FUZZY_POINT_SET_H

#include <utility> // defines less operator for std::pair
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <vector>
#include <array>

// TODO: Template over dimension.
//template <std::size_t dim>
class FuzzyPointMap
{
 public:
  FuzzyPointMap(double tolerance)
    : tolerance(tolerance)
  {}
  //-----------------------------------------------------------------------------
  template <typename Point>
  std::size_t insert_point(const Point& p)
  //-----------------------------------------------------------------------------
  {
    const std::size_t index = get_index(p);
    if (index == points.size())
    {
      // insert the points
      points.push_back(std::array<double, 3>{{p[0], p[1], p[2]}});
      maps[0].insert(std::make_pair(p[0], index));
      maps[1].insert(std::make_pair(p[1], index));
      maps[2].insert(std::make_pair(p[2], index));
    }

    return index;
  }
  //-----------------------------------------------------------------------------
  template <typename Point>
  std::size_t forced_insert_point(const Point& p)
  //-----------------------------------------------------------------------------
  {
    const std::size_t index = points.size();

    points.push_back(std::array<double, 3>{{p[0], p[1], p[2]}});
    maps[0].insert(std::make_pair(p[0], index));
    maps[1].insert(std::make_pair(p[1], index));
    maps[2].insert(std::make_pair(p[2], index));

    return index;
  }
  //-----------------------------------------------------------------------------
  template<typename Point>
  bool contains(const Point& p)
  //-----------------------------------------------------------------------------
  {
    const std::size_t index = get_index(p);
    return index < points.size();
  }
  //-----------------------------------------------------------------------------
  const std::array<double, 3>& operator[](std::size_t i) const
  //-----------------------------------------------------------------------------
  {
    return points[i];
  }
  //-----------------------------------------------------------------------------
  const std::vector<std::array<double, 3>>& get_points() const
  //-----------------------------------------------------------------------------
  {
    return points;
  }
  //-----------------------------------------------------------------------------
  std::size_t size() const
  //-----------------------------------------------------------------------------
  {
    return points.size();
  }
  //-----------------------------------------------------------------------------
  template<typename Point>
  std::size_t get_index(const Point& p)
  //-----------------------------------------------------------------------------
  {
    typedef std::multimap<double, std::size_t>::iterator iterator;

    // Check if a nearby point exists
    std::array<std::set<std::size_t>, 3> matches;
    for (int i = 0; i < 3; i++)
    {
      iterator lb = maps[i].lower_bound(p[i]-tolerance);
      iterator ub = maps[i].upper_bound(p[i]+tolerance);

      if (lb != maps[i].end())
      {
        iterator it = lb;
        while (it != ub)
        {
          matches[i].insert(it->second);
          it++;
        }
      }
    }

    std::set<std::size_t> x_y_intersections;
    std::set_intersection(matches[0].begin(), matches[0].end(),
                          matches[1].begin(), matches[1].end(),
                          std::inserter(x_y_intersections, x_y_intersections.begin()));

    std::set<std::size_t> x_y_z_intersections;
    std::set_intersection(matches[2].begin(), matches[2].end(),
                          x_y_intersections.begin(), x_y_intersections.end(),
                          std::inserter(x_y_z_intersections, x_y_z_intersections.begin()));

    if (x_y_z_intersections.size() > 0)
      return *x_y_z_intersections.begin();
    else
      return points.size();
  }

 private:
  const double tolerance;

  // Map from component of point to index
  std::array<std::multimap<double, std::size_t>, 3> maps;
  std::vector<std::array<double, 3> > points;
};

#endif