Image: Move Edge class to its own namespace

The Edge class is an implementation detail of the scan-line polygon filling algorithm.
Tuck it away in the newly created PolygonFill namespace.
This commit is contained in:
Peter Keresztes Schmidt 2021-05-16 23:24:47 +02:00
parent 6642ca4515
commit c0017a5263
3 changed files with 30 additions and 31 deletions

View File

@ -2469,7 +2469,7 @@ void Image::Fill(Rgb colour, int density, const Polygon &polygon) {
size_t n_coords = polygon.GetVertices().size(); size_t n_coords = polygon.GetVertices().size();
std::vector<Edge> global_edges; std::vector<PolygonFill::Edge> global_edges;
global_edges.reserve(n_coords); global_edges.reserve(n_coords);
for (size_t j = 0, i = n_coords - 1; j < n_coords; i = j++) { for (size_t j = 0, i = n_coords - 1; j < n_coords; i = j++) {
const Vector2 &p1 = polygon.GetVertices()[i]; const Vector2 &p1 = polygon.GetVertices()[i];
@ -2487,9 +2487,9 @@ void Image::Fill(Rgb colour, int density, const Polygon &polygon) {
d.x_ / static_cast<double>(d.y_)); d.x_ / static_cast<double>(d.y_));
} }
std::sort(global_edges.begin(), global_edges.end(), Edge::CompareYX); std::sort(global_edges.begin(), global_edges.end(), PolygonFill::Edge::CompareYX);
std::vector<Edge> active_edges; std::vector<PolygonFill::Edge> active_edges;
active_edges.reserve(global_edges.size()); active_edges.reserve(global_edges.size());
int32 scan_line = global_edges[0].min_y; int32 scan_line = global_edges[0].min_y;
@ -2513,7 +2513,7 @@ void Image::Fill(Rgb colour, int density, const Polygon &polygon) {
++it; ++it;
} }
} }
std::sort(active_edges.begin(), active_edges.end(), Edge::CompareX); std::sort(active_edges.begin(), active_edges.end(), PolygonFill::Edge::CompareX);
if (!(scan_line % density)) { if (!(scan_line % density)) {
for (auto it = active_edges.begin(); it != active_edges.end(); ++it) { for (auto it = active_edges.begin(); it != active_edges.end(); ++it) {
@ -2555,10 +2555,6 @@ void Image::Fill(Rgb colour, int density, const Polygon &polygon) {
} }
} }
void Image::Fill(Rgb colour, const Polygon &polygon) {
Fill(colour, 1, polygon);
}
void Image::Rotate(int angle) { void Image::Rotate(int angle) {
angle %= 360; angle %= 360;

View File

@ -278,7 +278,7 @@ class Image {
void Fill( Rgb colour, const Box *limits=0 ); void Fill( Rgb colour, const Box *limits=0 );
void Fill( Rgb colour, int density, const Box *limits=0 ); void Fill( Rgb colour, int density, const Box *limits=0 );
void Outline( Rgb colour, const Polygon &polygon ); void Outline( Rgb colour, const Polygon &polygon );
void Fill(Rgb colour, const Polygon &polygon); void Fill(Rgb colour, const Polygon &polygon) { Fill(colour, 1, polygon); };
void Fill(Rgb colour, int density, const Polygon &polygon); void Fill(Rgb colour, int density, const Polygon &polygon);
void Rotate( int angle ); void Rotate( int angle );
@ -292,6 +292,31 @@ class Image {
void Deinterlace_4Field(const Image* next_image, unsigned int threshold); void Deinterlace_4Field(const Image* next_image, unsigned int threshold);
}; };
// Scan-line polygon fill algorithm
namespace PolygonFill {
class Edge {
public:
Edge() = default;
Edge(int32 min_y, int32 max_y, double min_x, double _1_m) : min_y(min_y), max_y(max_y), min_x(min_x), _1_m(_1_m) {}
static bool CompareYX(const Edge &e1, const Edge &e2) {
if (e1.min_y == e2.min_y)
return e1.min_x < e2.min_x;
return e1.min_y < e2.min_y;
}
static bool CompareX(const Edge &e1, const Edge &e2) {
return e1.min_x < e2.min_x;
}
public:
int32 min_y;
int32 max_y;
double min_x;
double _1_m;
};
}
#endif // ZM_IMAGE_H #endif // ZM_IMAGE_H
/* Blend functions */ /* Blend functions */

View File

@ -23,28 +23,6 @@
#include "zm_box.h" #include "zm_box.h"
#include <vector> #include <vector>
class Edge {
public:
Edge() = default;
Edge(int32 min_y, int32 max_y, double min_x, double _1_m) : min_y(min_y), max_y(max_y), min_x(min_x), _1_m(_1_m) {}
static bool CompareYX(const Edge &e1, const Edge &e2) {
if (e1.min_y == e2.min_y)
return e1.min_x < e2.min_x;
return e1.min_y < e2.min_y;
}
static bool CompareX(const Edge &e1, const Edge &e2) {
return e1.min_x < e2.min_x;
}
public:
int32 min_y;
int32 max_y;
double min_x;
double _1_m;
};
// This class represents convex or concave non-self-intersecting polygons. // This class represents convex or concave non-self-intersecting polygons.
class Polygon { class Polygon {
public: public: