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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
// 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.1/Shape_regularization/include/CGAL/Shape_regularization/internal/Open_contour_2.h $
// $Id: include/CGAL/Shape_regularization/internal/Open_contour_2.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Dmitry Anisimov, Simon Giraudot
//
#ifndef CGAL_SHAPE_REGULARIZATION_OPEN_CONTOUR_2_H
#define CGAL_SHAPE_REGULARIZATION_OPEN_CONTOUR_2_H
#include <CGAL/license/Shape_regularization.h>
// Internal includes.
#include <CGAL/Shape_regularization/internal/Contour_base_2.h>
namespace CGAL {
namespace Shape_regularization {
namespace internal {
template<
typename ContourDirections,
typename GeomTraits>
class Open_contour_2 {
public:
using Contour_directions = ContourDirections;
using Traits = GeomTraits;
using FT = typename Traits::FT;
using Segment_2 = typename Traits::Segment_2;
using Base = internal::Contour_base_2<Traits>;
using Segment_wrapper_2 = typename Base::Segment_wrapper_2;
Open_contour_2(
const Contour_directions& estimator,
const FT max_offset_2) :
m_estimator(estimator),
m_max_offset_2(max_offset_2)
{ }
template<
typename Input_range,
typename Point_map>
void initialize(
const Input_range& input_range, const Point_map point_map) {
m_base.initialize_open(
input_range, point_map, m_wraps);
CGAL_assertion(m_wraps.size() == input_range.size() - 1);
}
template<typename OutputIterator>
OutputIterator regularize(OutputIterator contour) {
if (m_wraps.size() < 2) return contour;
CGAL_assertion(m_wraps.size() >= 2);
rotate_contour(m_wraps);
if (verbose()) {
m_base.export_polylines(m_wraps, "rotated");
}
bool success = optimize_contour(m_wraps);
if (!success) return contour;
if (verbose()) {
m_base.export_polylines(m_wraps, "optimized");
}
success = connect_contour(m_wraps);
if (!success) return contour;
if (verbose()) {
m_base.export_polylines(m_wraps, "connected");
}
return update_input(m_wraps, contour);
}
private:
const Contour_directions& m_estimator;
const FT m_max_offset_2;
const Base m_base;
std::vector<Segment_wrapper_2> m_wraps;
bool verbose() const {
return m_base.verbose();
}
void rotate_contour(
std::vector<Segment_wrapper_2>& wraps) const {
for (std::size_t i = 0; i < wraps.size(); ++i) {
auto& wrap = wraps[i];
m_estimator.orient(i, wrap.segment);
}
}
bool optimize_contour(
std::vector<Segment_wrapper_2>& wraps) const {
// Clean.
m_base.remove_zero_length_segments(wraps);
if (wraps.size() < 1) return false; // should be at least a segment
CGAL_assertion(wraps.size() >= 1);
// Merge parallel/collinear segments.
std::vector<Segment_2> segments;
m_base.create_unique_segments(m_max_offset_2, wraps, segments);
if (verbose()) {
std::cout <<
"* number of segments (merging) = " << segments.size() << std::endl;
}
if (wraps.size() < 1) return false;
// Add orthogonal segments.
create_orthogonal_segments(segments, wraps);
if (verbose()) {
std::cout <<
"* number of segments (orthogonal) = " << wraps.size() << std::endl;
}
if (wraps.size() < 1) return false;
return true;
}
void create_orthogonal_segments(
const std::vector<Segment_2>& segments,
std::vector<Segment_wrapper_2>& wraps) const {
Segment_wrapper_2 wrap;
const std::size_t n = segments.size();
wraps.clear();
wraps.reserve(n);
std::size_t count = 0;
for (std::size_t i = 0; i < n; ++i) {
const auto& segmenti = segments[i];
wrap.index = count; ++count;
wrap.segment = segmenti;
wraps.push_back(wrap);
const std::size_t j = (i + 1) % n;
if (j == 0) break;
const auto& segmentj = segments[j];
if (m_base.is_parallel_segment(segmenti, segmentj)) {
wrap.index = count; ++count;
m_base.create_average_orth(
segmenti, segmentj, wrap.segment);
wraps.push_back(wrap);
}
}
CGAL_assertion(wraps.size() >= segments.size());
}
bool connect_contour(
std::vector<Segment_wrapper_2>& wraps) const {
CGAL_assertion(wraps.size() >= 1);
if (wraps.size() < 1) return false;
intersect_segments(wraps);
if (wraps.size() < 1) return false;
// Experimental code.
// make_segments_collinear(wraps);
// intersect_segments(wraps);
// if (wraps.size() < 1) return false;
return true;
}
void intersect_segments(
std::vector<Segment_wrapper_2>& wraps) const {
const std::size_t n = wraps.size();
for (std::size_t i = 0; i < n; ++i) {
std::size_t im = std::size_t(-1);
if (i > 0) im = i - 1;
std::size_t ip = std::size_t(-1);
if (i < n - 1) ip = i + 1;
auto& si = wraps[i].segment;
if (im == std::size_t(-1) && ip == std::size_t(-1)) {
break;
}
if (im == std::size_t(-1) && ip != std::size_t(-1)) {
const auto& sp = wraps[ip].segment;
m_base.intersect_segment(si, sp);
continue;
}
if (im != std::size_t(-1) && ip == std::size_t(-1)) {
const auto& sm = wraps[im].segment;
m_base.intersect_segment(sm, si);
continue;
}
const auto& sm = wraps[im].segment;
const auto& sp = wraps[ip].segment;
m_base.intersect_segment(sm, si, sp);
}
}
// Experimental function.
void make_segments_collinear(
std::vector<Segment_wrapper_2>& wraps) const {
Segment_wrapper_2 wrap;
std::vector<Segment_wrapper_2> clean, group;
const std::size_t n = wraps.size();
const FT sq_max_length = m_max_offset_2 * m_max_offset_2;
for (std::size_t i = 0; i < n; ++i) {
group.push_back(wraps[i]);
const std::size_t j = (i + 1) % n;
if (j == 0) break;
const FT sq_length = wraps[j].segment.squared_length();
if (sq_length < sq_max_length) {
++i; continue;
} else {
m_base.parallel_segments_to_segment(group, wrap.segment);
clean.push_back(wrap); group.clear();
}
}
const std::size_t before = wraps.size();
wraps = clean;
const std::size_t after = wraps.size();
CGAL_assertion(after <= before);
if (verbose()) {
std::cout <<
"* segments before/after = " << before << "/" << after << std::endl;
}
}
template<typename OutputIterator>
OutputIterator update_input(
const std::vector<Segment_wrapper_2>& wraps,
OutputIterator contour) const {
const std::size_t n = wraps.size();
for (std::size_t i = 0; i < n; ++i) {
const auto& wrap = wraps[i];
*(++contour) = wrap.segment.source();
}
*(++contour) = wraps[n - 1].segment.target();
return contour;
}
};
} // internal
} // namespace Shape_regularization
} // namespace CGAL
#endif // CGAL_SHAPE_REGULARIZATION_OPEN_CONTOUR_2_H
|