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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
#ifndef CGAL_SHAPE_REGULARIZATION_EXAMPLES_SAVER_H
#define CGAL_SHAPE_REGULARIZATION_EXAMPLES_SAVER_H
// STL includes.
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
// CGAL includes.
#include <CGAL/IO/io.h>
#include <CGAL/property_map.h>
#include <CGAL/squared_distance_2.h>
template<typename GeomTraits>
struct Saver {
public:
using Traits = GeomTraits;
using FT = typename Traits::FT;
using Point_2 = typename Traits::Point_2;
using Point_3 = typename Traits::Point_3;
using Segment_2 = typename Traits::Segment_2;
using Polyline = std::vector<Point_3>;
Saver() {
out.precision(20);
}
inline std::string data() const {
return out.str();
}
void export_segments(
const std::vector<Segment_2>& segments,
const std::string path,
const FT) {
std::vector<Polyline> polylines(segments.size());
for (std::size_t i = 0; i < segments.size(); ++i) {
const auto& s = segments[i].source();
const auto& t = segments[i].target();
polylines[i].push_back(Point_3(s.x(), s.y(), FT(0)));
polylines[i].push_back(Point_3(t.x(), t.y(), FT(0)));
}
export_polylines(polylines, path);
}
void export_group(
const std::vector<Segment_2>& segments,
const std::vector<std::size_t>& group,
const std::string path,
const FT) {
const FT stub = FT(0);
std::vector<Segment_2> edges;
for (const std::size_t seg_index : group) {
edges.push_back(segments[seg_index]);
}
export_segments(edges, path, stub);
}
void export_closed_contour(
const std::vector<Point_2>& contour,
const std::string path,
const FT) {
if (contour.size() == 0) {
return;
}
const FT stub = FT(0);
std::vector<Segment_2> segments;
const std::size_t n = contour.size();
segments.reserve(n);
for (std::size_t i = 0; i < n; ++i) {
const std::size_t ip = (i + 1) % n;
const auto& p = contour[i];
const auto& q = contour[ip];
segments.push_back(Segment_2(p, q));
}
export_segments(segments, path, stub);
}
void export_open_contour(
const std::vector<Point_2>& contour,
const std::string path,
const FT) {
if (contour.size() == 0) {
return;
}
const FT stub = FT(0);
std::vector<Segment_2> segments;
const std::size_t n = contour.size();
segments.reserve(n - 1);
for (std::size_t i = 0; i < n - 1; ++i) {
const std::size_t ip = i + 1;
const auto& p = contour[i];
const auto& q = contour[ip];
segments.push_back(Segment_2(p, q));
}
export_segments(segments, path, stub);
}
void export_eps_segments(
const std::vector<Segment_2>& input,
const std::string path,
FT scale) {
if (input.size() == 0) return;
clear();
// Compute barycenter.
Point_2 b;
compute_barycenter(input, b);
// Translate segments.
std::vector<Segment_2> segments;
translate_segments(input, b, segments);
// Compute bounding box.
Point_2 minb, maxb;
compute_bounding_box(segments, minb, maxb);
// Estimate eps parameters.
const FT length = static_cast<FT>(
CGAL::sqrt(CGAL::to_double(CGAL::squared_distance(minb, maxb))));
if (length < FT(10) && scale == FT(1)) scale *= FT(1000);
// const FT radius = FT(1);
const FT line_width = FT(1);
const bool dashed = false;
// Set eps header.
set_eps_header(
minb.x() * scale, minb.y() * scale,
maxb.x() * scale, maxb.y() * scale,
"segments");
// Start private namespace.
out << "0 dict begin gsave" << std::endl << std::endl;
// Draw segments.
for (const auto& segment : segments) {
add_eps_segment(segment, scale, line_width, dashed);
// add_eps_disc(segment.source(), radius, scale);
// add_eps_disc(segment.target(), radius, scale);
}
// Finish private namespace.
out << "grestore end" << std::endl << std::endl;
out << "%%EOF" << std::endl;
save(path + ".eps");
}
void export_eps_group(
const std::vector<Segment_2>& segments,
const std::vector<std::size_t>& group,
const std::string path) {
const FT stub = FT(0);
std::vector<Segment_2> edges;
for (const std::size_t seg_index : group) {
edges.push_back(segments[seg_index]);
}
export_eps_segments(edges, path, stub);
}
void export_eps_closed_contour(
const std::vector<Point_2>& contour,
const std::string path,
FT scale) {
if (contour.size() == 0) {
return;
}
std::vector<Segment_2> segments;
const std::size_t n = contour.size();
segments.reserve(n);
for (std::size_t i = 0; i < n; ++i) {
const std::size_t ip = (i + 1) % n;
const auto& p = contour[i];
const auto& q = contour[ip];
segments.push_back(Segment_2(p, q));
}
export_eps_segments(segments, path, scale);
}
void export_eps_open_contour(
const std::vector<Point_2>& contour,
const std::string path,
FT scale) {
if (contour.size() == 0) {
return;
}
std::vector<Segment_2> segments;
const std::size_t n = contour.size();
segments.reserve(n - 1);
for (std::size_t i = 0; i < n - 1; ++i) {
const std::size_t ip = i + 1;
const auto& p = contour[i];
const auto& q = contour[ip];
segments.push_back(Segment_2(p, q));
}
export_eps_segments(segments, path, scale);
}
private:
std::stringstream out;
void clear() {
out.str(std::string());
}
void save(
const std::string path) const {
std::ofstream file(path.c_str(), std::ios_base::out);
CGAL::IO::set_ascii_mode(file);
if (!file) {
std::cerr <<
"Error: cannot save the file: " << path << std::endl; return;
}
file << data() << std::endl; file.close();
std::cout <<
"* data are saved in " << path << std::endl;
}
void export_polylines(
const std::vector<Polyline>& polylines,
const std::string path) {
if (polylines.size() == 0) {
return;
}
clear();
for (std::size_t i = 0; i < polylines.size(); ++i) {
const auto& polyline = polylines[i];
out << polyline.size() << " ";
for (std::size_t j = 0; j < polyline.size(); ++j)
out << polyline[j] << " ";
out << std::endl;
}
save(path + ".polylines");
}
void compute_barycenter(
const std::vector<Segment_2>& segments,
Point_2& b) const {
FT bx = FT(0), by = FT(0);
for (const auto& segment : segments) {
const auto& source = segment.source();
const auto& target = segment.target();
bx += source.x(); by += source.y();
bx += target.x(); by += target.y();
}
bx /= static_cast<FT>(segments.size() * 2);
by /= static_cast<FT>(segments.size() * 2);
b = Point_2(bx, by);
}
void translate_segments(
const std::vector<Segment_2>& input,
const Point_2& b,
std::vector<Segment_2>& segments) const {
segments.clear();
segments.reserve(input.size());
for (const auto& segment : input) {
const auto& source = segment.source();
const auto& target = segment.target();
segments.push_back(Segment_2(
Point_2(source.x() - b.x(), source.y() - b.y()),
Point_2(target.x() - b.x(), target.y() - b.y())));
}
}
void compute_bounding_box(
const std::vector<Segment_2>& segments,
Point_2& minb, Point_2& maxb) const {
FT minx = FT(1000000000000), maxx = -FT(1000000000000);
FT miny = FT(1000000000000), maxy = -FT(1000000000000);
for (const auto& segment : segments) {
const auto& source = segment.source();
const auto& target = segment.target();
minx = (CGAL::min)(minx, source.x());
maxx = (CGAL::max)(maxx, source.x());
miny = (CGAL::min)(miny, source.y());
maxy = (CGAL::max)(maxy, source.y());
minx = (CGAL::min)(minx, target.x());
maxx = (CGAL::max)(maxx, target.x());
miny = (CGAL::min)(miny, target.y());
maxy = (CGAL::max)(maxy, target.y());
}
const FT d = CGAL::abs(maxx - minx) / FT(10);
minb = Point_2(minx - d, miny - d);
maxb = Point_2(maxx + d, maxy + d);
}
void set_eps_header(
const FT llx,
const FT lly,
const FT urx,
const FT ury,
const std::string title) {
out << "%!PS-Adobe-3.0 EPSF-3.0" << std::endl;
out << "%%BoundingBox: " << llx << " " << lly << " " << urx << " " << ury << std::endl;
out << "%%Pages: 1" << std::endl;
out << "%%Creator: Dmitry Anisimov, rudanston@gmail.com" << std::endl;
out << "%%Title: " << title.c_str() << std::endl;
out << "%%EndComments" << std::endl;
out << "%%EndProlog" << std::endl << std::endl;
out << "%%Page: 1 1" << std::endl << std::endl;
}
void add_eps_segment(
const Segment_2& segment,
const FT scale,
const FT line_width,
const bool dashed) {
const auto& source = segment.source();
const auto& target = segment.target();
out << source.x() * scale << " " << source.y() * scale << " moveto" << std::endl;
out << target.x() * scale << " " << target.y() * scale << " lineto" << std::endl;
out << 0 << " setgray" << std::endl;
if (dashed) out << "[4 1] 0 setdash" << std::endl;
else out << "[] 0 setdash" << std::endl;
out << line_width << " setlinewidth" << std::endl;
out << "stroke" << std::endl << std::endl;
}
void add_eps_disc(
const Point_2& center,
const FT radius,
const FT scale) {
out << 0 << " setgray" << std::endl;
out << "0 setlinewidth" << std::endl << std::endl;
out <<
center.x() * scale << " " <<
center.y() * scale << " " <<
radius << " 0 360 arc closepath" << std::endl << std::endl;
out << "gsave" << std::endl;
out << 0 << " setgray fill" << std::endl;
out << "grestore" << std::endl;
out << "stroke" << std::endl << std::endl;
}
};
#endif // CGAL_SHAPE_REGULARIZATION_EXAMPLES_SAVER_H
|