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
|
#ifndef CGAL_SHAPE_DETECTION_EXAMPLES_UTILS_H
#define CGAL_SHAPE_DETECTION_EXAMPLES_UTILS_H
// STL includes.
#include <string>
#include <vector>
#include <utility>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <cassert>
// CGAL includes.
#include <CGAL/array.h>
#include <CGAL/memory.h>
#include <CGAL/IO/PLY.h>
#include <CGAL/IO/OFF.h>
#include <CGAL/Real_timer.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/write_ply_points.h>
#include <CGAL/IO/Color.h>
#include <CGAL/Random.h>
#include <CGAL/Shape_detection/Region_growing/internal/utils.h>
// Define how a color should be stored.
namespace CGAL {
template<class F>
struct Output_rep< ::std::array<unsigned char, 3>, F > {
const ::std::array<unsigned char, 3>& c;
static const bool is_specialized = true;
Output_rep(const ::std::array<unsigned char, 3>& c) : c(c) { }
std::ostream& operator()(std::ostream& out) const {
if (IO::is_ascii(out)) { out << int(c[0]) << " " << int(c[1]) << " " << int(c[2]);
} else { out.write(reinterpret_cast<const char*>(&c), sizeof(c)); }
return out;
}
};
} // namespace CGAL
namespace utils {
template<typename Kernel, typename Region, typename Point_map>
void save_point_regions_2(
const Region& regions,
const std::string fullpath,
const Point_map point_map = Point_map()) {
using Point_3 = typename Kernel::Point_3;
using Color = std::array<unsigned char, 3>;
using Point_with_color = std::pair<Point_3, Color>;
using PLY_Point_map = CGAL::First_of_pair_property_map<Point_with_color>;
using PLY_Color_map = CGAL::Second_of_pair_property_map<Point_with_color>;
std::vector<Point_with_color> pwc;
srand(static_cast<unsigned int>(time(NULL)));
// Iterate through all regions.
for (const auto& region : regions) {
// Generate a random color.
const Color color =
CGAL::make_array(
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256));
// Iterate through all region items.
for (const auto item : region.second) {
const auto& point = get(point_map, item);
pwc.push_back(std::make_pair(Point_3(point.x(), point.y(), 0), color));
}
}
std::ofstream out(fullpath);
CGAL::IO::set_ascii_mode(out);
CGAL::IO::write_PLY_with_properties(
out, pwc,
CGAL::make_ply_point_writer(PLY_Point_map()),
std::make_tuple(
PLY_Color_map(),
CGAL::PLY_property<unsigned char>("red"),
CGAL::PLY_property<unsigned char>("green"),
CGAL::PLY_property<unsigned char>("blue")));
out.close();
}
template<typename Kernel, typename Region, typename Point_map>
void save_point_regions_3(
const Region& regions,
const std::string fullpath,
const Point_map point_map = Point_map()) {
using Point_3 = typename Kernel::Point_3;
using Color = std::array<unsigned char, 3>;
using Point_with_color = std::pair<Point_3, Color>;
using PLY_Point_map = CGAL::First_of_pair_property_map<Point_with_color>;
using PLY_Color_map = CGAL::Second_of_pair_property_map<Point_with_color>;
std::vector<Point_with_color> pwc;
srand(static_cast<unsigned int>(time(NULL)));
// Iterate through all regions.
for (const auto& region : regions) {
// Generate a random color.
const Color color =
CGAL::make_array(
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256));
// Iterate through all region items.
for (const auto item : region.second) {
const auto& point = get(point_map, *item);
pwc.push_back(std::make_pair(point, color));
}
}
std::ofstream out(fullpath);
CGAL::IO::set_ascii_mode(out);
CGAL::IO::write_PLY_with_properties(
out, pwc,
CGAL::make_ply_point_writer(PLY_Point_map()),
std::make_tuple(
PLY_Color_map(),
CGAL::PLY_property<unsigned char>("red"),
CGAL::PLY_property<unsigned char>("green"),
CGAL::PLY_property<unsigned char>("blue")));
out.close();
}
template<typename Kernel, typename Region, typename Segment_map>
void save_segment_regions_2(
const Region& regions,
const std::string fullpath,
const Segment_map segment_map = Segment_map()) {
using Point_3 = typename Kernel::Point_3;
using Color = std::array<unsigned char, 3>;
using Point_with_color = std::pair<Point_3, Color>;
using PLY_Point_map = CGAL::First_of_pair_property_map<Point_with_color>;
using PLY_Color_map = CGAL::Second_of_pair_property_map<Point_with_color>;
std::vector<Point_with_color> pwc;
srand(static_cast<unsigned int>(time(NULL)));
// Iterate through all regions.
for (const auto& region : regions) {
// Generate a random color.
const Color color =
CGAL::make_array(
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256));
// Iterate through all region items.
for (const auto item : region.second) {
const auto& segment = get(segment_map, item);
const auto& s = segment.source();
const auto& t = segment.target();
pwc.push_back(std::make_pair(Point_3(s.x(), s.y(), 0), color));
pwc.push_back(std::make_pair(Point_3(t.x(), t.y(), 0), color));
}
}
std::ofstream out(fullpath);
CGAL::IO::set_ascii_mode(out);
CGAL::IO::write_PLY_with_properties(
out, pwc,
CGAL::make_ply_point_writer(PLY_Point_map()),
std::make_tuple(
PLY_Color_map(),
CGAL::PLY_property<unsigned char>("red"),
CGAL::PLY_property<unsigned char>("green"),
CGAL::PLY_property<unsigned char>("blue")));
out.close();
}
template<typename Kernel, typename Regions, typename Segment_map>
void save_segment_regions_3(
const Regions& regions,
const std::string fullpath,
const Segment_map segment_map = Segment_map()) {
using Point_3 = typename Kernel::Point_3;
using Color = std::array<unsigned char, 3>;
using Point_with_color = std::pair<Point_3, Color>;
using PLY_Point_map = CGAL::First_of_pair_property_map<Point_with_color>;
using PLY_Color_map = CGAL::Second_of_pair_property_map<Point_with_color>;
std::vector<Point_with_color> pwc;
srand(static_cast<unsigned int>(time(NULL)));
// Iterate through all regions.
for (const auto& region : regions) {
// Generate a random color.
const Color color =
CGAL::make_array(
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256));
// Iterate through all region items.
for (const auto &item : region.second) {
const auto& segment = get(segment_map, item);
pwc.push_back(std::make_pair(segment.source(), color));
pwc.push_back(std::make_pair(segment.target(), color));
}
}
std::ofstream out(fullpath);
CGAL::IO::set_ascii_mode(out);
CGAL::IO::write_PLY_with_properties(
out, pwc,
CGAL::make_ply_point_writer(PLY_Point_map()),
std::make_tuple(
PLY_Color_map(),
CGAL::PLY_property<unsigned char>("red"),
CGAL::PLY_property<unsigned char>("green"),
CGAL::PLY_property<unsigned char>("blue")));
out.close();
}
// Define an insert iterator.
template<
typename Item,
typename Output_range,
typename Point_map,
typename Primitive>
struct Insert_point_colored_by_region_index {
using argument_type = std::pair< Primitive, std::vector<Item> >;
using result_type = void;
using Color_map =
typename Output_range:: template Property_map<unsigned char>;
const Point_map m_point_map;
Output_range& m_output_range;
std::size_t& m_number_of_regions;
Color_map m_red, m_green, m_blue;
Insert_point_colored_by_region_index(
const Point_map point_map,
Output_range& output_range,
std::size_t& number_of_regions) :
m_point_map(point_map),
m_output_range(output_range),
m_number_of_regions(number_of_regions) {
m_red = m_output_range.template add_property_map<unsigned char>("red", 0).first;
m_green = m_output_range.template add_property_map<unsigned char>("green", 0).first;
m_blue = m_output_range.template add_property_map<unsigned char>("blue", 0).first;
}
result_type operator()(const argument_type& region) {
CGAL::Random rand(static_cast<unsigned int>(m_number_of_regions));
const unsigned char r = static_cast<unsigned char>(64 + rand.get_int(0, 192));
const unsigned char g = static_cast<unsigned char>(64 + rand.get_int(0, 192));
const unsigned char b = static_cast<unsigned char>(64 + rand.get_int(0, 192));
for (Item index : region.second) {
const auto& point = get(m_point_map, index);
const auto it = m_output_range.insert(point);
m_red[*it] = r;
m_green[*it] = g;
m_blue[*it] = b;
}
++m_number_of_regions;
}
};
template<typename Polygon_mesh, typename Region>
void save_polygon_mesh_regions(
Polygon_mesh& polygon_mesh,
const Region& regions,
const std::string fullpath) {
using Color = CGAL::IO::Color;
srand(static_cast<unsigned int>(time(NULL)));
using Face_property_color = CGAL::dynamic_face_property_t<Color>;
using Face_color_map = typename boost::property_map<Polygon_mesh, Face_property_color>::type;
Face_color_map face_color = get(Face_property_color(), polygon_mesh);
// Iterate through all regions.
std::ofstream out(fullpath);
for (const auto& region : regions) {
// Generate a random color.
const Color color(
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256),
static_cast<unsigned char>(rand() % 256));
// Iterate through all region items.
for (const auto &item : region.second) {
put(face_color, item, color);
}
}
CGAL::IO::write_PLY(out, polygon_mesh, CGAL::parameters::face_color_map(face_color));
}
} // namespace utils
#endif // CGAL_SHAPE_DETECTION_EXAMPLES_UTILS_H
|