Skip to content

File shape.hpp

File List > data_format > shape.hpp

Go to the documentation of this file

#pragma once

#include "normalize.hpp"

#include 

namespace ILLIXR {
namespace data_format::shapes {
    struct [[maybe_unused]] rect {
        double x_center; 
        double y_center; 
        double width;    
        double height;   
        double rotation; 
        bool   valid;    
        bool   normalized;

        rect()
            : x_center{0.}
            , y_center{0.}
            , width{0.}
            , height{0.}
            , rotation{0.}
            , valid{false}
            , normalized{false} { }

        explicit rect(rect* other) {
            if (other != nullptr) {
                x_center   = other->x_center;
                y_center   = other->y_center;
                width      = other->width;
                height     = other->height;
                rotation   = other->rotation;
                valid      = other->valid;
                normalized = other->normalized;
            }
        }

        rect(const double xc, const double yc, const double w, const double h, const double r, bool normal = false)
            : x_center{xc}
            , y_center{yc}
            , width{w}
            , height{h}
            , rotation{r}
            , valid{true}
            , normalized{normal} { }

        void set(const double xc, const double yc, const double w, const double h, const double r, bool normal = false) {
            x_center   = xc;
            y_center   = yc;
            width      = w;
            height     = h;
            rotation   = r;
            valid      = true;
            normalized = normal;
        }

        void flip_y(const uint32_t im_height = 0) {
            if (normalized) {
                y_center = 1.0 - y_center;
                return;
            }
            if (im_height == 0)
                throw std::runtime_error("Cannot flip rect if no image height is given.");
            y_center = (float) im_height - y_center;
        }
    };
} // namespace data_format::shapes

template<>
inline void normalize<data_format::shapes::rect>(data_format::shapes::rect& obj, const float width, const float height,
                                                 const float depth) {
    (void) depth;
    if (obj.normalized) {
        return;
    }
    obj.x_center /= width;
    obj.y_center /= height;
    obj.width /= width;
    obj.height /= height;
    obj.normalized = true;
}

template<>
inline void denormalize<data_format::shapes::rect>(data_format::shapes::rect& obj, const float width, const float height,
                                                   const float depth) {
    (void) depth;
    if (!obj.valid)
        return;
    if (!obj.normalized) {
        return;
    }
    obj.x_center *= width;
    obj.y_center *= height;
    obj.width *= width;
    obj.height *= height;
    obj.normalized = false;
}

} // namespace ILLIXR