// Copyright 2024 SquareBlock Inc. All Rights Reserved.
// Author: tianlei.richard@qq.com (tianlei.richard)

#pragma once

#include "mesh.h"
#include "triangle.h"
#include <opencv2/core/core.hpp>
#include <vector>

typedef struct RasterizerResult {
  double depth{-std::numeric_limits<double>::infinity()};
  struct {
    int32_t mesh_index{-1};
    int64_t triangle_index{-1};
  } triangle;
} RasterizerResult;

class Rasterizer {
public:
  Rasterizer(const int width, const int height);

public:
  std::vector<std::vector<RasterizerResult>>
  rasterize(const std::vector<Mesh> &meshes);

  void reset();

private:
  static std::pair<bool, double> inside(const Point2d &p_screen,
                                        const Triangle &t);

private:
  void rasterize(const int mesh_idx, const std::vector<Triangle> &primitives,
                 const int begin, const int end);

private:
  int width_;
  int height_;
  std::vector<std::vector<RasterizerResult>> shading_points_;
};
