// Moshade - Compute average cross section for 3D shapes
// Copyright (C) 2018-2026 Eric Bechet - bechet@cadxfem.org
//
// See the LICENSE file for license information and contributions.
// Please report all bugs and problems to <bechet@cadxfem.org>.

#ifndef MOSHADE_GEOM_OP_H
#define MOSHADE_GEOM_OP_H
#include "nutil.h"
#define Pi (3.14159265358979323846264338327950288419716939937511)
class tri_bb : public triangle
{
  virtual bool in_bb(const npoint3 &pt) const;
  virtual bool in_bb(const tri_bb &tri) const;

  npoint3 max,min; // bounding box
public :
  tri_bb(const triangle &t);
  tri_bb() {}
  virtual ~tri_bb() {}
  virtual bool in(const npoint3 &pt,bool boundary_out) const;
  virtual double area2(void) const; // in xy plane, sign depends on the ordering of nodes
  virtual double area(void) const; // 3D, >=0
  virtual double circ(void) const; // total lengh of sides, >0
  virtual npoint3 G(void) const; // location of the center of gravity
  virtual npoint3 normal(void) const;// computed with the actual order of nodes
  virtual void cut(const line& l, std::vector< tri_bb >& ret) const;
  virtual bool cut(const tri_bb& tri, std::vector< tri_bb >& outside,bool disp=false) const;
};

/// apply a transformation to a point in homog. coordinates
void npt_transfo(const double mat[4][4],const npoint &src, npoint &dest);
/// creates (fills) a rotation matrix around given axis (0->x, 1->y, 2->z)
void fill_rot_mat(int axis,double angle,double mat[4][4]);
/// creates (fills) a translation matrix along a given axis (0->x, 1->y, 2->z)
void fill_tr_mat(int axis,double value,double mat[4][4]);
/// 4x4 matrix product : mo[i][j] = m1[i][k]*m2[k][j]
void mat_prod(const double m1[4][4],const double m2[4][4],double mo[4][4]);
/// apply a transformation to a triangle in 3D
void tri_transfo(const triangle& tri, const double mat[4][4], triangle& tro);
/// project onto one of the xyz plane (discard axis coordinate)
void tri_project(const triangle& tri, int axis, triangle& tro);
/// sort vector of tris w/r of the area (decreasing)
void sort_area(std::vector<triangle> &source);
/// compute intersection between two line segments
bool line_int(line l1, line l2,npoint3 &pt,npoint3 &uv);
/// in tri
bool in_tri(const npoint3& pt, const tri_bb& tri, bool boundary_out);
/// cut triangle trc by line
void tri_cut_line(const line& l1, const tri_bb& trc, std::vector< tri_bb >& ret);
/// cut triangle trc by triangle tri, and keep only outside parts in out vector: returns true if no cut is found and outside
bool tri_cut(const tri_bb& tri, const tri_bb& trc, std::vector< tri_bb >& outside,bool disp=false);
/// compute area of a triangle
double tri_area(const triangle& tri);

#endif // MOSHADE_GEOM_OP_H