Lagrange
doublearea.h
1// Source: https://github.com/libigl/libigl/blob/main/include/igl/doublearea.cpp
2// SPDX-License-Identifier: MPL-2.0
3//
4// This file is part of libigl, a simple c++ geometry processing library.
5//
6// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
7//
8// This Source Code Form is subject to the terms of the Mozilla Public License
9// v. 2.0. If a copy of the MPL was not distributed with this file, You can
10// obtain one at http://mozilla.org/MPL/2.0/.
11//
12// This file has been modified by Adobe.
13//
14// All modifications are Copyright 2022 Adobe.
15#pragma once
16
17#include <lagrange/utils/assert.h>
18
19#include <Eigen/Dense>
20
21namespace lagrange::internal {
22
23template <typename DerivedV, typename DerivedF, typename DeriveddblA>
24void doublearea(
25 const Eigen::MatrixBase<DerivedV>& V,
26 const Eigen::MatrixBase<DerivedF>& F,
27 Eigen::PlainObjectBase<DeriveddblA>& dblA)
28{
29 const auto dim = V.cols();
30 const auto m = F.rows();
31
32 // Only support triangles
33 la_runtime_assert(F.cols() == 3);
34
35 // Only support dim 2 or 3
36 la_runtime_assert(dim == 2 || dim == 3);
37
38 // Compute edge lengths
39 Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> l;
40
41 // Projected area helper
42 const auto& proj_doublearea = [&V, &F](Eigen::Index x, Eigen::Index y, Eigen::Index f) ->
43 typename DerivedV::Scalar {
44 auto rx = V(F(f, 0), x) - V(F(f, 2), x);
45 auto sx = V(F(f, 1), x) - V(F(f, 2), x);
46 auto ry = V(F(f, 0), y) - V(F(f, 2), y);
47 auto sy = V(F(f, 1), y) - V(F(f, 2), y);
48 return rx * sy - ry * sx;
49 };
50
51 switch (dim) {
52 case 3: {
53 dblA = DeriveddblA::Zero(m, 1);
54 for (Eigen::Index f = 0; f < m; f++) {
55 for (int d = 0; d < 3; d++) {
56 const auto dblAd = proj_doublearea(d, (d + 1) % 3, f);
57 dblA(f) += dblAd * dblAd;
58 }
59 }
60 dblA = dblA.array().sqrt().eval();
61 break;
62 }
63 case 2: {
64 dblA.resize(m, 1);
65 for (Eigen::Index f = 0; f < m; f++) {
66 dblA(f) = proj_doublearea(0, 1, f);
67 }
68 break;
69 }
70 default: break;
71 }
72}
73
74} // namespace lagrange::internal
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
nullptr_t, size_t, ptrdiff_t basic_ostream bad_weak_ptr extent, remove_extent, is_array,...
Definition: attribute_string_utils.h:21