Vector2: Make coordinate components public

The components were already unconditionally/without side-effects writable. Let's make them public so we don't need the setters.
This commit is contained in:
Peter Keresztes Schmidt 2021-05-08 12:58:32 +02:00
parent c8885fe2aa
commit e6c159fb70
8 changed files with 99 additions and 118 deletions

View File

@ -40,27 +40,27 @@ class Box {
inline Box(const Vector2 &p_lo, const Vector2 &p_hi) : lo(p_lo), hi(p_hi), size(Vector2::Range(hi, lo)) {}
inline const Vector2 &Lo() const { return lo; }
inline int LoX() const { return lo.X(); }
inline int LoX(int p_lo_x) { return lo.X(p_lo_x); }
inline int LoY() const { return lo.Y(); }
inline int LoY(int p_lo_y) { return lo.Y(p_lo_y); }
inline int LoX() const { return lo.x_; }
inline int LoX(int p_lo_x) { return lo.x_ = p_lo_x; }
inline int LoY() const { return lo.y_; }
inline int LoY(int p_lo_y) { return lo.y_ = p_lo_y; }
inline const Vector2 &Hi() const { return hi; }
inline int HiX() const { return hi.X(); }
inline int HiX(int p_hi_x) { return hi.X(p_hi_x); }
inline int HiY() const { return hi.Y(); }
inline int HiY(int p_hi_y) { return hi.Y(p_hi_y); }
inline int HiX() const { return hi.x_; }
inline int HiX(int p_hi_x) { return hi.x_ = p_hi_x; }
inline int HiY() const { return hi.y_; }
inline int HiY(int p_hi_y) { return hi.y_ = p_hi_y; }
inline const Vector2 &Size() const { return size; }
inline int Width() const { return size.X(); }
inline int Height() const { return size.Y(); }
inline int Area() const { return size.X() * size.Y(); }
inline int Width() const { return size.x_; }
inline int Height() const { return size.y_; }
inline int Area() const { return size.x_ * size.y_; }
inline const Vector2 Centre() const {
int mid_x = int(std::round(lo.X() + (size.X() / 2.0)));
int mid_y = int(std::round(lo.Y() + (size.Y() / 2.0)));
int mid_x = int(std::round(lo.x_ + (size.x_ / 2.0)));
int mid_y = int(std::round(lo.y_ + (size.y_ / 2.0)));
return Vector2(mid_x, mid_y);
}
inline bool Inside(const Vector2 &coord) const {
return (coord.X() >= lo.X() && coord.X() <= hi.X() && coord.Y() >= lo.Y() && coord.Y() <= hi.Y());
inline bool Inside(const Vector2 &coord) const {
return (coord.x_ >= lo.x_ && coord.x_ <= hi.x_ && coord.y_ >= lo.y_ && coord.y_ <= hi.y_);
}
};

View File

@ -820,10 +820,10 @@ Image *Image::HighlightEdges(
/* Set image to all black */
high_image->Clear();
unsigned int lo_x = limits ? limits->Lo().X() : 0;
unsigned int lo_y = limits ? limits->Lo().Y() : 0;
unsigned int hi_x = limits ? limits->Hi().X() : width-1;
unsigned int hi_y = limits ? limits->Hi().Y() : height-1;
unsigned int lo_x = limits ? limits->Lo().x_ : 0;
unsigned int lo_y = limits ? limits->Lo().y_ : 0;
unsigned int hi_x = limits ? limits->Hi().x_ : width - 1;
unsigned int hi_y = limits ? limits->Hi().y_ : height - 1;
if ( p_colours == ZM_COLOUR_GRAY8 ) {
for ( unsigned int y = lo_y; y <= hi_y; y++ ) {
@ -2031,8 +2031,8 @@ void Image::Annotate(
// Calculate initial coordinates of annotation so that everything is displayed even if the
// user set coordinates would prevent that.
uint32 x0 = ZM::clamp(static_cast<uint32>(coord.X()), 0u, x0_max);
uint32 y0 = ZM::clamp(static_cast<uint32>(coord.Y()), 0u, y0_max);
uint32 x0 = ZM::clamp(static_cast<uint32>(coord.x_), 0u, x0_max);
uint32 y0 = ZM::clamp(static_cast<uint32>(coord.y_), 0u, y0_max);
uint32 y = y0;
for (const std::string &line : lines) {
@ -2294,10 +2294,10 @@ void Image::Fill( Rgb colour, const Box *limits ) {
/* Convert the colour's RGBA subpixel order into the image's subpixel order */
colour = rgb_convert(colour,subpixelorder);
unsigned int lo_x = limits?limits->Lo().X():0;
unsigned int lo_y = limits?limits->Lo().Y():0;
unsigned int hi_x = limits?limits->Hi().X():width-1;
unsigned int hi_y = limits?limits->Hi().Y():height-1;
unsigned int lo_x = limits ? limits->Lo().x_ : 0;
unsigned int lo_y = limits ? limits->Lo().y_ : 0;
unsigned int hi_x = limits ? limits->Hi().x_ : width - 1;
unsigned int hi_y = limits ? limits->Hi().y_ : height - 1;
if ( colours == ZM_COLOUR_GRAY8 ) {
for ( unsigned int y = lo_y; y <= hi_y; y++ ) {
unsigned char *p = &buffer[(y*width)+lo_x];
@ -2339,10 +2339,10 @@ void Image::Fill( Rgb colour, int density, const Box *limits ) {
/* Convert the colour's RGBA subpixel order into the image's subpixel order */
colour = rgb_convert(colour, subpixelorder);
unsigned int lo_x = limits?limits->Lo().X():0;
unsigned int lo_y = limits?limits->Lo().Y():0;
unsigned int hi_x = limits?limits->Hi().X():width-1;
unsigned int hi_y = limits?limits->Hi().Y():height-1;
unsigned int lo_x = limits ? limits->Lo().x_ : 0;
unsigned int lo_y = limits ? limits->Lo().y_ : 0;
unsigned int hi_x = limits ? limits->Hi().x_ : width - 1;
unsigned int hi_y = limits ? limits->Hi().y_ : height - 1;
if ( colours == ZM_COLOUR_GRAY8 ) {
for ( unsigned int y = lo_y; y <= hi_y; y++ ) {
unsigned char *p = &buffer[(y*width)+lo_x];
@ -2389,10 +2389,10 @@ void Image::Outline( Rgb colour, const Polygon &polygon ) {
const Vector2 &p1 = polygon.getCoord(i);
const Vector2 &p2 = polygon.getCoord(j);
int x1 = p1.X();
int x2 = p2.X();
int y1 = p1.Y();
int y2 = p2.Y();
int x1 = p1.x_;
int x2 = p2.x_;
int y1 = p1.y_;
int y2 = p2.y_;
double dx = x2 - x1;
double dy = y2 - y1;
@ -2473,10 +2473,10 @@ void Image::Fill(Rgb colour, int density, const Polygon &polygon) {
const Vector2 &p1 = polygon.getCoord(i);
const Vector2 &p2 = polygon.getCoord(j);
int x1 = p1.X();
int x2 = p2.X();
int y1 = p1.Y();
int y2 = p2.Y();
int x1 = p1.x_;
int x2 = p2.x_;
int y1 = p1.y_;
int y2 = p2.y_;
//Debug( 9, "x1:%d,y1:%d x2:%d,y2:%d", x1, y1, x2, y2 );
if ( y1 == y2 )

View File

@ -1544,8 +1544,9 @@ bool Monitor::CheckSignal(const Image *image) {
if ( !config.timestamp_on_capture || !label_format[0] )
break;
// Avoid sampling the rows with timestamp in
if ( index < (label_coord.Y()*width) || index >= (label_coord.Y()+Image::LINE_HEIGHT)*width )
if (index < (label_coord.y_ * width) || index >= (label_coord.y_ + Image::LINE_HEIGHT) * width) {
break;
}
}
if ( colours == ZM_COLOUR_GRAY8 ) {
@ -2898,8 +2899,8 @@ unsigned int Monitor::DetectMotion(const Image &comp_image, Event::StringSet &zo
} // end if alarm
if (top_score > 0) {
shared_data->alarm_x = alarm_centre.X();
shared_data->alarm_y = alarm_centre.Y();
shared_data->alarm_x = alarm_centre.x_;
shared_data->alarm_y = alarm_centre.y_;
Info("Got alarm centre at %d,%d, at count %d",
shared_data->alarm_x, shared_data->alarm_y, analysis_image_count);
@ -2954,7 +2955,7 @@ bool Monitor::DumpSettings(char *output, bool verbose) {
sprintf(output+strlen(output), "Subpixel Order : %u\n", camera->SubpixelOrder() );
sprintf(output+strlen(output), "Event Prefix : %s\n", event_prefix.c_str() );
sprintf(output+strlen(output), "Label Format : %s\n", label_format.c_str() );
sprintf(output+strlen(output), "Label Coord : %d,%d\n", label_coord.X(), label_coord.Y() );
sprintf(output+strlen(output), "Label Coord : %d,%d\n", label_coord.x_, label_coord.y_ );
sprintf(output+strlen(output), "Label Size : %d\n", label_size );
sprintf(output+strlen(output), "Image Buffer Count : %d\n", image_buffer_count );
sprintf(output+strlen(output), "Warmup Count : %d\n", warmup_count );

View File

@ -24,7 +24,7 @@
void Polygon::calcArea() {
double float_area = 0.0L;
for (int i = 0, j = n_coords - 1; i < n_coords; j = i++) {
double trap_area = ((coords[i].X() - coords[j].X()) * ((coords[i].Y() + coords[j].Y()))) / 2.0L;
double trap_area = ((coords[i].x_ - coords[j].x_) * ((coords[i].y_ + coords[j].y_))) / 2.0L;
float_area += trap_area;
//printf( "%.2f (%.2f)\n", float_area, trap_area );
}
@ -36,10 +36,8 @@ void Polygon::calcCentre() {
calcArea();
double float_x = 0.0L, float_y = 0.0L;
for (int i = 0, j = n_coords - 1; i < n_coords; j = i++) {
float_x += ((coords[i].Y() - coords[j].Y())
* ((coords[i].X() * 2) + (coords[i].X() * coords[j].X()) + (coords[j].X() * 2)));
float_y += ((coords[j].X() - coords[i].X())
* ((coords[i].Y() * 2) + (coords[i].Y() * coords[j].Y()) + (coords[j].Y() * 2)));
float_x += ((coords[i].y_ - coords[j].y_) * ((coords[i].x_ * 2) + (coords[i].x_ * coords[j].x_) + (coords[j].x_ * 2)));
float_y += ((coords[j].x_ - coords[i].x_) * ((coords[i].y_ * 2) + (coords[i].y_ * coords[j].y_) + (coords[j].y_ * 2)));
}
float_x /= (6 * area);
float_y /= (6 * area);
@ -55,14 +53,14 @@ Polygon::Polygon(int p_n_coords, const Vector2 *p_coords) : n_coords(p_n_coords)
int max_y = -1;
for (int i = 0; i < n_coords; i++) {
coords[i] = p_coords[i];
if (min_x == -1 || coords[i].X() < min_x)
min_x = coords[i].X();
if (max_x == -1 || coords[i].X() > max_x)
max_x = coords[i].X();
if (min_y == -1 || coords[i].Y() < min_y)
min_y = coords[i].Y();
if (max_y == -1 || coords[i].Y() > max_y)
max_y = coords[i].Y();
if (min_x == -1 || coords[i].x_ < min_x)
min_x = coords[i].x_;
if (max_x == -1 || coords[i].x_ > max_x)
max_x = coords[i].x_;
if (min_y == -1 || coords[i].y_ < min_y)
min_y = coords[i].y_;
if (max_y == -1 || coords[i].y_ > max_y)
max_y = coords[i].y_;
}
extent = Box(min_x, min_y, max_x, max_y);
calcArea();
@ -98,11 +96,9 @@ Polygon &Polygon::operator=(const Polygon &p_polygon) {
bool Polygon::isInside(const Vector2 &coord) const {
bool inside = false;
for ( int i = 0, j = n_coords-1; i < n_coords; j = i++ ) {
if ( (((coords[i].Y() <= coord.Y()) && (coord.Y() < coords[j].Y()) )
|| ((coords[j].Y() <= coord.Y()) && (coord.Y() < coords[i].Y())))
&& (coord.X() < (coords[j].X() - coords[i].X()) * (coord.Y() - coords[i].Y()) / (coords[j].Y() - coords[i].Y()) + coords[i].X()))
{
for (int i = 0, j = n_coords - 1; i < n_coords; j = i++) {
if ((((coords[i].y_ <= coord.y_) && (coord.y_ < coords[j].y_)) || ((coords[j].y_ <= coord.y_) && (coord.y_ < coords[i].y_)))
&& (coord.x_ < (coords[j].x_ - coords[i].x_) * (coord.y_ - coords[i].y_) / (coords[j].y_ - coords[i].y_) + coords[i].x_)) {
inside = !inside;
}
}

View File

@ -27,60 +27,44 @@
//
class Vector2 {
public:
inline Vector2() : x(0), y(0) {}
inline Vector2(int p_x, int p_y) : x(p_x), y(p_y) {}
Vector2() : x_(0), y_(0) {}
Vector2(int32 x, int32 y) : x_(x), y_(y) {}
inline int &X(int p_x) {
x = p_x;
return x;
}
inline const int &X() const { return x; }
inline int &Y(int p_y) {
y = p_y;
return y;
}
inline const int &Y() const { return y; }
inline static Vector2 Range(const Vector2 &coord1, const Vector2 &coord2) {
Vector2 result((coord1.x - coord2.x) + 1, (coord1.y - coord2.y) + 1);
static Vector2 Range(const Vector2 &coord1, const Vector2 &coord2) {
Vector2 result((coord1.x_ - coord2.x_) + 1, (coord1.y_ - coord2.y_) + 1);
return result;
}
inline bool operator==(const Vector2 &coord) const { return (x == coord.x && y == coord.y); }
inline bool operator!=(const Vector2 &coord) const { return (x != coord.x || y != coord.y); }
bool operator==(const Vector2 &rhs) const { return (x_ == rhs.x_ && y_ == rhs.y_); }
bool operator!=(const Vector2 &rhs) const { return (x_ != rhs.x_ || y_ != rhs.y_); }
// These operators are not idiomatic. If lexicographic comparison is needed, it should be implemented separately.
inline bool operator>(const Vector2 &coord) const = delete;
inline bool operator>=(const Vector2 &coord) const = delete;
inline bool operator<(const Vector2 &coord) const = delete;
inline bool operator<=(const Vector2 &coord) const = delete;
bool operator>(const Vector2 &rhs) const = delete;
bool operator>=(const Vector2 &rhs) const = delete;
bool operator<(const Vector2 &rhs) const = delete;
bool operator<=(const Vector2 &rhs) const = delete;
inline Vector2 &operator+=(const Vector2 &coord) {
x += coord.x;
y += coord.y;
Vector2 operator+(const Vector2 &rhs) const {
return {x_ + rhs.x_, y_ + rhs.y_};
}
Vector2 operator-(const Vector2 &rhs) const {
return {x_ - rhs.x_, y_ - rhs.y_};
}
Vector2 &operator+=(const Vector2 &rhs) {
x_ += rhs.x_;
y_ += rhs.y_;
return *this;
}
inline Vector2 &operator-=(const Vector2 &coord) {
x -= coord.x;
y -= coord.y;
Vector2 &operator-=(const Vector2 &rhs) {
x_ -= rhs.x_;
y_ -= rhs.y_;
return *this;
}
inline friend Vector2 operator+(const Vector2 &coord1, const Vector2 &coord2) {
Vector2 result(coord1);
result += coord2;
return result;
}
inline friend Vector2 operator-(const Vector2 &coord1, const Vector2 &coord2) {
Vector2 result(coord1);
result -= coord2;
return result;
}
private:
int x;
int y;
public:
int32 x_;
int32 y_;
};
#endif // ZM_VECTOR2_H

View File

@ -267,8 +267,8 @@ bool Zone::CheckAlarms(const Image *delta_image) {
Debug(5, "Current score is %d", stats.score_);
if (check_method >= FILTERED_PIXELS) {
int bx = filter_box.X();
int by = filter_box.Y();
int bx = filter_box.x_;
int by = filter_box.y_;
int bx1 = bx-1;
int by1 = by-1;
@ -924,7 +924,7 @@ bool Zone::DumpSettings(char *output, bool /*verbose*/) const {
))))));
sprintf( output+strlen(output), " Shape : %d points\n", polygon.getNumCoords() );
for ( int i = 0; i < polygon.getNumCoords(); i++ ) {
sprintf( output+strlen(output), " %i: %d,%d\n", i, polygon.getCoord( i ).X(), polygon.getCoord( i ).Y() );
sprintf(output+strlen(output), " %i: %d,%d\n", i, polygon.getCoord( i ).x_, polygon.getCoord(i ).y_ );
}
sprintf( output+strlen(output), " Alarm RGB : %06x\n", alarm_rgb );
sprintf( output+strlen(output), " Check Method: %d - %s\n", check_method,
@ -936,7 +936,7 @@ bool Zone::DumpSettings(char *output, bool /*verbose*/) const {
sprintf( output+strlen(output), " Max Pixel Threshold : %d\n", max_pixel_threshold );
sprintf( output+strlen(output), " Min Alarm Pixels : %d\n", min_alarm_pixels );
sprintf( output+strlen(output), " Max Alarm Pixels : %d\n", max_alarm_pixels );
sprintf( output+strlen(output), " Filter Box : %d,%d\n", filter_box.X(), filter_box.Y() );
sprintf(output+strlen(output), " Filter Box : %d,%d\n", filter_box.x_, filter_box.y_ );
sprintf( output+strlen(output), " Min Filter Pixels : %d\n", min_filter_pixels );
sprintf( output+strlen(output), " Max Filter Pixels : %d\n", max_filter_pixels );
sprintf( output+strlen(output), " Min Blob Pixels : %d\n", min_blob_pixels );

View File

@ -71,8 +71,8 @@ class ZoneStats {
alarm_box_.LoY(),
alarm_box_.HiX(),
alarm_box_.HiY(),
alarm_centre_.X(),
alarm_centre_.Y(),
alarm_centre_.x_,
alarm_centre_.y_,
score_
);
}

View File

@ -20,40 +20,40 @@
#include "zm_vector2.h"
std::ostream &operator<<(std::ostream &os, Vector2 const &value) {
os << "{ X: " << value.X() << ", Y: " << value.Y() << " }";
os << "{ X: " << value.x_ << ", Y: " << value.y_ << " }";
return os;
}
TEST_CASE("Vector2: default constructor") {
Vector2 c;
REQUIRE(c.X() == 0);
REQUIRE(c.Y() == 0);
REQUIRE(c.x_ == 0);
REQUIRE(c.y_ == 0);
}
TEST_CASE("Vector2: x/y constructor") {
Vector2 c(1, 2);
REQUIRE(c.X() == 1);
REQUIRE(c.Y() == 2);
REQUIRE(c.x_ == 1);
REQUIRE(c.y_ == 2);
}
TEST_CASE("Vector2: assignment/copy") {
Vector2 c;
Vector2 c2(1, 2);
REQUIRE(c.X() == 0);
REQUIRE(c.Y() == 0);
REQUIRE(c.x_ == 0);
REQUIRE(c.y_ == 0);
SECTION("assignment operator") {
c = c2;
REQUIRE(c.X() == 1);
REQUIRE(c.Y() == 2);
REQUIRE(c.x_ == 1);
REQUIRE(c.y_ == 2);
}
SECTION("copy constructor") {
Vector2 c3(c2); // NOLINT(performance-unnecessary-copy-initialization)
REQUIRE(c3.X() == 1);
REQUIRE(c3.Y() == 2);
REQUIRE(c3.x_ == 1);
REQUIRE(c3.y_ == 2);
}
}