common.h 1.64 KB
/*
 * @Author: yangzilong
 * @Date: 2021-12-01 15:14:02
 * @Last Modified by: yangzilong
 * @Last Modified time: Do not edit
 * @Email: yangzilong@objecteye.com
 * @Description:
 */

#pragma once

#include <cmath>
#include <type_traits>

namespace ai_engine_module {
template <typename T> T clip(const T &val, const T &min, const T &max) {
  if (std::is_integral<T>::value)
    return std::min(std::max(val, min), max);
  return val;
}

using obj_id_t = long;
using task_id_t = std::string;

struct unique_obj_id_t {
  obj_id_t obj_id;
  task_id_t task_id;

  bool operator<(const unique_obj_id_t &obj) const {
    return obj_id < obj.obj_id || (task_id < obj.task_id);
  }
};

struct obj_key_t {
  obj_id_t obj_id;
  task_id_t task_id;
  algorithm_type_t algor_type;

  bool operator<(const obj_key_t &obj) const {
    if (obj_id < obj.obj_id)
      return true;

    else if (obj_id == obj.obj_id) {
      if (strcmp(task_id.c_str(), obj.task_id.c_str()) < 0)
        return true;

      else if (strcmp(task_id.c_str(), obj.task_id.c_str()) == 0) {
        if (static_cast<int>(algor_type) < static_cast<int>(obj.algor_type))
          return true;
        else
          return false;
      }
    }
    return false;
  }
};

struct point_t {
  int x, y;
};

struct box_t {
  long id; // -1: placeholder.
  float score;
  int top, left, right, bottom, cls;

  int width() const {
    return std::max(0, right - left);
  }

  int height() const {
    return std::max(0, bottom - top);
  }

  int cx() const {
    return std::max(0, int((left + right) * 0.5f));
  }

  int cy() const {
    return std::max(0, int((top + bottom) * 0.5f));
  }
};

} // namespace ai_engine_module