File: Orthogonal_groups_2.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (149 lines) | stat: -rw-r--r-- 4,702 bytes parent folder | download
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
146
147
148
149
// Copyright (c) 2020 GeometryFactory SARL (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Shape_regularization/include/CGAL/Shape_regularization/internal/Orthogonal_groups_2.h $
// $Id: include/CGAL/Shape_regularization/internal/Orthogonal_groups_2.h b26b07a1242 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Dmitry Anisimov, Gennadii Sytov
//

#ifndef CGAL_SHAPE_REGULARIZATION_ORTHOGONAL_GROUPS_2_H
#define CGAL_SHAPE_REGULARIZATION_ORTHOGONAL_GROUPS_2_H

#include <CGAL/license/Shape_regularization.h>

// Boost includes.
#include <CGAL/boost/graph/named_params_helper.h>
#include <CGAL/Named_function_parameters.h>

// Internal includes.
#include <CGAL/Shape_regularization/internal/utils.h>
#include <CGAL/Shape_regularization/internal/Parallel_groups_2.h>

namespace CGAL {
namespace Shape_regularization {
namespace internal {

  template<
  typename GeomTraits,
  typename InputRange,
  typename SegmentMap>
  class Orthogonal_groups_2 {

  public:
    using Traits = GeomTraits;
    using Input_range = InputRange;
    using Segment_map = SegmentMap;

    using FT = typename Traits::FT;
    using Segment_2 = typename Traits::Segment_2;
    using Indices = std::vector<std::size_t>;

    using PGroups_2 = Parallel_groups_2<Traits, Input_range, Segment_map>;

    template<typename NamedParameters>
    Orthogonal_groups_2(
      const InputRange& input_range,
      const NamedParameters& np,
      const SegmentMap segment_map,
      const GeomTraits&) :
    m_input_range(input_range),
    m_segment_map(segment_map),
    m_grouping(
      input_range, np, segment_map, GeomTraits()) {

      const FT max_angle = parameters::choose_parameter(
        parameters::get_parameter(np, internal_np::maximum_angle), FT(5));
      const bool preserve_order = parameters::choose_parameter(
        parameters::get_parameter(np, internal_np::preserve_order), false);
      CGAL_precondition(max_angle >= FT(0) && max_angle <= FT(90));
      m_max_angle = max_angle;
      make_orthogonal_groups(preserve_order);
    }

    template<typename OutputIterator>
    OutputIterator groups(OutputIterator groups) const {
      for (const auto& orthogonal_group : m_orthogonal_groups) {
        const auto& group = orthogonal_group;
        *(groups++) = group;
      }
      return groups;
    }

  private:
    const Input_range& m_input_range;
    const Segment_map m_segment_map;
    const PGroups_2 m_grouping;

    FT m_max_angle;
    std::vector<Indices> m_orthogonal_groups;

    void make_orthogonal_groups(const bool preserve_order) {

      std::vector<Indices> parallel_groups;
      m_grouping.groups(
        std::back_inserter(parallel_groups));

      Indices orthogonal_group;
      std::vector<bool> states(parallel_groups.size(), false);
      for (std::size_t i = 0; i < parallel_groups.size(); ++i) {
        if (states[i]) continue;

        CGAL_assertion(parallel_groups[i].size() > 0);
        const std::size_t si_index = parallel_groups[i][0];
        const auto& si = get(
          m_segment_map, *(m_input_range.begin() + si_index));

        states[i] = true;
        orthogonal_group.clear();
        for (const std::size_t seg_index : parallel_groups[i]) {
          orthogonal_group.push_back(seg_index);
        }

        traverse_group(
          preserve_order, i, si, parallel_groups,
          states, orthogonal_group);
        m_orthogonal_groups.push_back(orthogonal_group);
      }
      CGAL_assertion(
        m_orthogonal_groups.size() <= parallel_groups.size());
    }

    void traverse_group(
      const bool preserve_order,
      const std::size_t i,
      const Segment_2& si,
      const std::vector<Indices>& parallel_groups,
      std::vector<bool>& states,
      Indices& orthogonal_group) const {

      for (std::size_t j = i + 1; j < parallel_groups.size(); ++j) {
        if (states[j]) continue;

        CGAL_assertion(parallel_groups[j].size() > 0);
        const std::size_t sj_index = parallel_groups[j][0];
        const auto& sj = get(
          m_segment_map, *(m_input_range.begin() + sj_index));

        const FT angle_2 = internal::angle_2(si, sj);
        if (angle_2 <= m_max_angle ||
            angle_2 >= FT(90) - m_max_angle) {

          states[j] = true;
          for (const std::size_t seg_index : parallel_groups[j]) {
            orthogonal_group.push_back(seg_index);
          }
        } else if (preserve_order) return;
      }
    }
  };

} // namespace internal
} // namespace Shape_regularization
} // namespace CGAL

#endif // CGAL_SHAPE_REGULARIZATION_ORTHOGONAL_GROUPS_2_H