Added color generator; improved internal API

This commit is contained in:
Efi Fogel 2023-02-09 01:03:33 +02:00
parent 03285a42cf
commit 6705b15fdb
1 changed files with 93 additions and 45 deletions

View File

@ -46,19 +46,33 @@
namespace CGAL { namespace CGAL {
struct Default_color_generator {
/*! Obtain color
*/
template <typename HalfedgeHandle>
CGAL::IO::Color operator()(HalfedgeHandle /* h */) {
static std::random_device rd;
static std::mt19937 rng(rd());
static std::uniform_int_distribution<int> uni(0, 255);
return CGAL::IO::Color(uni(rng), uni(rng), uni(rng));
}
};
// Viewer class for`< Polygon_2 // Viewer class for`< Polygon_2
template <typename GeometryTraits_2, typename Dcel> template <typename Arrangement_2_,
typename ColorGenerator = Default_color_generator>
class Arr_2_basic_viewer_qt : public Basic_viewer_qt { class Arr_2_basic_viewer_qt : public Basic_viewer_qt {
typedef GeometryTraits_2 Gt; using Arr = Arrangement_2_;
typedef CGAL::Arrangement_2<Gt, Dcel> Arr; using Color_generator = ColorGenerator;
typedef Basic_viewer_qt Base; using Base = Basic_viewer_qt;
typedef typename Arr::Point_2 Point; using Gt = typename Arr::Geometry_traits_2;
typedef typename Arr::X_monotone_curve_2 X_monotone_curve; using Point = typename Arr::Point_2;
typedef typename Arr::Vertex_const_handle Vertex_const_handle; using X_monotone_curve = typename Arr::X_monotone_curve_2;
typedef typename Arr::Halfedge_const_handle Halfedge_const_handle; using Vertex_const_handle = typename Arr::Vertex_const_handle;
typedef typename Arr::Face_const_handle Face_const_handle; using Halfedge_const_handle = typename Arr::Halfedge_const_handle;
typedef typename Arr::Ccb_halfedge_const_circulator using Face_const_handle = typename Arr::Face_const_handle;
Ccb_halfedge_const_circulator; using Ccb_halfedge_const_circulator =
typename Arr::Ccb_halfedge_const_circulator;
template <typename T> template <typename T>
using approximate_2_object_t = using approximate_2_object_t =
@ -69,11 +83,13 @@ public:
/// @param arr the arrangement to view /// @param arr the arrangement to view
/// @param title the title of the window /// @param title the title of the window
Arr_2_basic_viewer_qt(QWidget* parent, const Arr& arr, Arr_2_basic_viewer_qt(QWidget* parent, const Arr& arr,
const char* title = "2D Arrangement Basic Viewer") : Color_generator color_generator,
const char* title = "2D Arrangement Basic Viewer",
bool draw_vertices = false) :
// First draw: vertices; edges, faces; multi-color; no inverse normal // First draw: vertices; edges, faces; multi-color; no inverse normal
Base(parent, title, true, true, true, false, false), Base(parent, title, draw_vertices, true, true, false, false),
m_arr(arr), m_arr(arr),
m_uni(0, 255) m_color_generator(color_generator)
{ {
// mimic the computation of Camera::pixelGLRatio() // mimic the computation of Camera::pixelGLRatio()
auto bbox = bounding_box(); auto bbox = bounding_box();
@ -157,9 +173,6 @@ public:
clear(); clear();
m_visited.clear(); m_visited.clear();
std::random_device rd;
m_rng.seed(rd());
if (m_arr.is_empty()) return; if (m_arr.is_empty()) return;
for (auto it = m_arr.unbounded_faces_begin(); for (auto it = m_arr.unbounded_faces_begin();
it != m_arr.unbounded_faces_end(); ++it) it != m_arr.unbounded_faces_end(); ++it)
@ -289,7 +302,7 @@ protected:
auto has_approximate_2_object = auto has_approximate_2_object =
bh::is_valid([](auto&& x) -> decltype(x.approximate_2_object()){}); bh::is_valid([](auto&& x) -> decltype(x.approximate_2_object()){});
CGAL::IO::Color color(m_uni(m_rng), m_uni(m_rng), m_uni(m_rng)); auto color = m_color_generator(circ->face());
this->face_begin(color); this->face_begin(color);
const auto* traits = this->m_arr.geometry_traits(); const auto* traits = this->m_arr.geometry_traits();
@ -464,32 +477,35 @@ protected:
//! The arrangement to draw. //! The arrangement to draw.
const Arr& m_arr; const Arr& m_arr;
std::unordered_map<Face_const_handle, bool> m_visited; //! The color generator.
Color_generator& m_color_generator;
std::mt19937 m_rng; std::unordered_map<Face_const_handle, bool> m_visited;
std::uniform_int_distribution<int> m_uni; // guaranteed unbiased
}; };
//! Basic viewer of a 2D arrangement. //! Basic viewer of a 2D arrangement.
template <typename GeometryTraits_2, typename Dcel> template <typename Arrangement_2_,
class Arr_2_viewer_qt : public Arr_2_basic_viewer_qt<GeometryTraits_2, Dcel> { typename ColorGenerator = Default_color_generator>
class Arr_2_viewer_qt : public Arr_2_basic_viewer_qt<Arrangement_2_,
ColorGenerator> {
public: public:
typedef GeometryTraits_2 Gt; using Arr = Arrangement_2_;
typedef CGAL::Arrangement_2<Gt, Dcel> Arr; using Color_generator = ColorGenerator;
typedef Arr_2_basic_viewer_qt<Gt, Dcel> Base; using Base = Arr_2_basic_viewer_qt<Arr, Color_generator>;
typedef typename Arr::Point_2 Point; using Point = typename Arr::Point_2;
typedef typename Arr::X_monotone_curve_2 X_monotone_curve; using X_monotone_curve = typename Arr::X_monotone_curve_2;
typedef typename Arr::Halfedge_const_handle Halfedge_const_handle; using Halfedge_const_handle = typename Arr::Halfedge_const_handle;
typedef typename Arr::Face_const_handle Face_const_handle; using Face_const_handle = typename Arr::Face_const_handle;
typedef typename Arr::Ccb_halfedge_const_circulator using Ccb_halfedge_const_circulator =
Ccb_halfedge_const_circulator; typename Arr::Ccb_halfedge_const_circulator;
/// Construct the viewer. /// Construct the viewer.
/// @param arr the arrangement to view /// @param arr the arrangement to view
/// @param title the title of the window /// @param title the title of the window
Arr_2_viewer_qt(QWidget* parent, const Arr& arr, Arr_2_viewer_qt(QWidget* parent, const Arr& arr,
Color_generator color_generator,
const char* title = "2D Arrangement Basic Viewer") : const char* title = "2D Arrangement Basic Viewer") :
Base(parent, arr, title) Base(parent, arr, color_generator, title)
{} {}
}; };
@ -500,22 +516,54 @@ void draw(const Arrangement_2<GeometryTraits_2, Dcel>& arr,
#if defined(CGAL_TEST_SUITE) #if defined(CGAL_TEST_SUITE)
bool cgal_test_suite=true; bool cgal_test_suite=true;
#else #else
bool cgal_test_suite=qEnvironmentVariableIsSet("CGAL_TEST_SUITE"); bool cgal_test_suite = qEnvironmentVariableIsSet("CGAL_TEST_SUITE");
#endif #endif
if (!cgal_test_suite) { if (cgal_test_suite) return;
typedef GeometryTraits_2 Gt; using Gt = GeometryTraits_2;
using Arr = CGAL::Arrangement_2<Gt, Dcel>;
using Viewer = Arr_2_viewer_qt<Arr, Default_color_generator>;
CGAL::Qt::init_ogl_context(4,3); CGAL::Qt::init_ogl_context(4,3);
int argc = 1; int argc = 1;
const char* argv[2] = {"t2_viewer", nullptr}; const char* argv[2] = {"t2_viewer", nullptr};
QApplication app(argc, const_cast<char**>(argv)); QApplication app(argc, const_cast<char**>(argv));
Arr_2_viewer_qt<Gt, Dcel> mainwindow(app.activeWindow(), arr, title); Default_color_generator color_generator;
mainwindow.add_elements(); Viewer mainwindow(app.activeWindow(), arr, color_generator, title);
mainwindow.show(); mainwindow.add_elements();
app.exec(); mainwindow.show();
} app.exec();
}
//!
template <typename GeometryTraits_2, typename Dcel,
typename ColorGenerator>
void draw(const Arrangement_2<GeometryTraits_2, Dcel>& arr,
ColorGenerator color_generator,
const char* title = "2D Arrangement Basic Viewer") {
#if defined(CGAL_TEST_SUITE)
bool cgal_test_suite=true;
#else
bool cgal_test_suite = qEnvironmentVariableIsSet("CGAL_TEST_SUITE");
#endif
if (cgal_test_suite) return;
using Color_generator = ColorGenerator;
using Gt = GeometryTraits_2;
using Arr = CGAL::Arrangement_2<Gt, Dcel>;
using Viewer = Arr_2_viewer_qt<Arr, Color_generator>;
CGAL::Qt::init_ogl_context(4,3);
int argc = 1;
const char* argv[2] = {"t2_viewer", nullptr};
QApplication app(argc, const_cast<char**>(argv));
Viewer mainwindow(app.activeWindow(), arr, color_generator, title);
mainwindow.add_elements();
mainwindow.show();
app.exec();
} }
} }