// Copyright (c) 2025 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#ifndef _GeomHash_RectangularTrimmedSurfaceHasher_HeaderFile
#define _GeomHash_RectangularTrimmedSurfaceHasher_HeaderFile

#include <Standard_HashUtils.hxx>
#include <Geom_RectangularTrimmedSurface.hxx>
#include <GeomHash_SurfaceHasher.hxx>
#include <cmath>
#include <Precision.hxx>

//! OCCT-style hasher for Geom_RectangularTrimmedSurface.
//! Used for geometry deduplication.
struct GeomHash_RectangularTrimmedSurfaceHasher
{
  double CompTolerance;
  double HashTolerance;

  GeomHash_RectangularTrimmedSurfaceHasher(double theCompTolerance = Precision::Angular(),
                                           double theHashTolerance = Precision::Confusion())
      : CompTolerance(theCompTolerance),
        HashTolerance(theHashTolerance)
  {
  }

  // Hashes the trimmed surface by its trim bounds and basis surface.
  std::size_t operator()(
    const occ::handle<Geom_RectangularTrimmedSurface>& theSurface) const noexcept
  {
    const double aFactor = 1.0 / HashTolerance;

    const GeomHash_SurfaceHasher aSurfaceHasher{CompTolerance, HashTolerance};

    double aU1, aU2, aV1, aV2;
    theSurface->Bounds(aU1, aU2, aV1, aV2);

    const std::size_t aHashes[5] = {
      aSurfaceHasher(theSurface->BasisSurface()),
      opencascade::hash(static_cast<int64_t>(std::round(aU1 * aFactor))),
      opencascade::hash(static_cast<int64_t>(std::round(aU2 * aFactor))),
      opencascade::hash(static_cast<int64_t>(std::round(aV1 * aFactor))),
      opencascade::hash(static_cast<int64_t>(std::round(aV2 * aFactor)))};
    return opencascade::hashBytes(aHashes, sizeof(aHashes));
  }

  // Compares two trimmed surfaces by their trim bounds and basis surfaces.
  bool operator()(const occ::handle<Geom_RectangularTrimmedSurface>& theSurface1,
                  const occ::handle<Geom_RectangularTrimmedSurface>& theSurface2) const noexcept
  {
    const GeomHash_SurfaceHasher aSurfaceHasher{CompTolerance, HashTolerance};

    // Compare basis surfaces
    if (!aSurfaceHasher(theSurface1->BasisSurface(), theSurface2->BasisSurface()))
    {
      return false;
    }

    // Compare trim bounds
    double aU1_1, aU2_1, aV1_1, aV2_1;
    double aU1_2, aU2_2, aV1_2, aV2_2;
    theSurface1->Bounds(aU1_1, aU2_1, aV1_1, aV2_1);
    theSurface2->Bounds(aU1_2, aU2_2, aV1_2, aV2_2);

    return std::abs(aU1_1 - aU1_2) <= CompTolerance && std::abs(aU2_1 - aU2_2) <= CompTolerance
           && std::abs(aV1_1 - aV1_2) <= CompTolerance && std::abs(aV2_1 - aV2_2) <= CompTolerance;
  }
};

#endif // _GeomHash_RectangularTrimmedSurfaceHasher_HeaderFile
