// 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_OffsetSurfaceHasher_HeaderFile
#define _GeomHash_OffsetSurfaceHasher_HeaderFile

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

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

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

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

    const GeomHash_SurfaceHasher aSurfaceHasher{CompTolerance, HashTolerance};
    const std::size_t            aHashes[2] = {
      aSurfaceHasher(theSurface->BasisSurface()),
      opencascade::hash(static_cast<int64_t>(std::round(theSurface->Offset() * aFactor)))};
    return opencascade::hashBytes(aHashes, sizeof(aHashes));
  }

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

    return aSurfaceHasher(theSurface1->BasisSurface(), theSurface2->BasisSurface())
           && std::abs(theSurface1->Offset() - theSurface2->Offset()) <= CompTolerance;
  }
};

#endif // _GeomHash_OffsetSurfaceHasher_HeaderFile
