Skip to content

File normal.h

File List > algorithm > normal.h

Go to the documentation of this file

// Copyright (c) 2012-2013, IGN France.
// Copyright (c) 2012-2022, Oslandia.
// SPDX-License-Identifier: LGPL-2.0-or-later

#ifndef _SFCGAL_ALGORITHM_NORMAL_H_
#define _SFCGAL_ALGORITHM_NORMAL_H_

#include "SFCGAL/config.h"

#include "SFCGAL/Polygon.h"

namespace SFCGAL {
namespace algorithm {

template <typename Kernel>
CGAL::Vector_3<Kernel>
normal3D(const CGAL::Point_3<Kernel> &a, const CGAL::Point_3<Kernel> &b,
         const CGAL::Point_3<Kernel> &c)
{
  // bc ^ ba
  return CGAL::cross_product(c - b, a - b);
}

template <typename Kernel>
CGAL::Vector_3<Kernel>
normal3D(const LineString &ls, bool exact = true)
{
  // Newell's formula
  typename Kernel::FT nx, ny, nz;
  nx = ny = nz = 0.0;

  for (size_t i = 0; i < ls.numPoints(); ++i) {
    const Point        &pi = ls.pointN(i);
    const Point        &pj = ls.pointN((i + 1) % ls.numPoints());
    typename Kernel::FT zi = pi.z();
    typename Kernel::FT zj = pj.z();
    nx += (pi.y() - pj.y()) * (zi + zj);
    ny += (zi - zj) * (pi.x() + pj.x());
    nz += (pi.x() - pj.x()) * (pi.y() + pj.y());
  }

  if (exact) {
    return CGAL::Vector_3<Kernel>(nx, ny, nz);
  } else {
    return CGAL::Vector_3<Kernel>(CGAL::to_double(nx), CGAL::to_double(ny),
                                  CGAL::to_double(nz));
  }
}

template <typename Kernel>
CGAL::Vector_3<Kernel>
normal3D(const Polygon &polygon, bool exact = true)
{
  return normal3D<Kernel>(polygon.exteriorRing(), exact);
}

} // namespace algorithm
} // namespace SFCGAL

#endif