import { Vector3 } from "three/src/math/Vector3";
import { Triangle } from "three/src/math/Triangle";
import { CoreGeometry } from "@polygonjs/polygonjs/dist/src/core/geometry/Geometry";
import { CoreMath } from "@polygonjs/polygonjs/dist/src/core/math/_Module";
import { ObjectType } from "@polygonjs/polygonjs/dist/src/core/geometry/Constant";
export class MapboxPlaneFrustumController {
  constructor(node) {
    this.node = node;
    this._triangle_a = new Triangle();
    this._triangle_b = new Triangle();
    this._point_pos = new Vector3();
  }
  deleteOutOfView(geometry, core_geo, camera_node, transformer, plane_dimensions, segments_counts) {
    const near_lng_lat_pts = camera_node.verticalNearLngLatPoints();
    const far_lng_lat_pts = camera_node.verticalFarLngLatPoints();
    if (!near_lng_lat_pts || !far_lng_lat_pts) {
      return;
    }
    let delete_out_of_view_bound_pts = near_lng_lat_pts.concat(far_lng_lat_pts);
    const delete_out_of_view_margin = Math.max(plane_dimensions.x / segments_counts.x, plane_dimensions.y / segments_counts.y);
    return this._deleteOutOfView(core_geo, delete_out_of_view_bound_pts, delete_out_of_view_margin * 2);
  }
  _deleteOutOfView(core_geo, bound_pts, margin) {
    const points = core_geo.points();
    const positions = bound_pts.map((bound_pt) => new Vector3(bound_pt.lng, 0, bound_pt.lat));
    this._triangle_a.a.copy(positions[0]);
    this._triangle_a.b.copy(positions[1]);
    this._triangle_a.c.copy(positions[2]);
    this._triangle_b.a.copy(positions[2]);
    this._triangle_b.b.copy(positions[3]);
    this._triangle_b.c.copy(positions[1]);
    CoreMath.expand_triangle(this._triangle_a, margin);
    CoreMath.expand_triangle(this._triangle_b, margin);
    const kept_points = [];
    for (let point of points) {
      point.getPosition(this._point_pos);
      if (this._triangle_b.containsPoint(this._point_pos)) {
        kept_points.push(point);
      }
    }
    return CoreGeometry.geometryFromPoints(kept_points, ObjectType.MESH);
  }
}
