Visual Computing Library
Loading...
Searching...
No Matches
geometry.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_CORE_POLYGON_GEOMETRY_H
24#define VCL_ALGORITHMS_CORE_POLYGON_GEOMETRY_H
25
26#include <vclib/concepts/mesh/elements/face.h>
27#include <vclib/space/core/polygon.h>
28#include <vclib/views/mesh.h>
29
30namespace vcl {
31
44template<FaceConcept FaceType>
45typename FaceType::VertexType::CoordType faceNormal(const FaceType& f)
46{
47 using CoordType = FaceType::VertexType::CoordType;
48 if constexpr (TriangleFaceConcept<FaceType>) {
50 f.vertex(0)->coord(), f.vertex(1)->coord(), f.vertex(2)->coord());
51 }
52 else {
53 if (f.vertexNumber() == 3) {
55 f.vertex(0)->coord(),
56 f.vertex(1)->coord(),
57 f.vertex(2)->coord());
58 }
59 else {
60 return Polygon<CoordType>::normal(f.vertices() | views::coords);
61 }
62 }
63}
64
76template<FaceConcept FaceType>
77typename FaceType::VertexType::CoordType faceBarycenter(const FaceType& f)
78{
79 using CoordType = FaceType::VertexType::CoordType;
80 if constexpr (TriangleFaceConcept<FaceType>) {
82 f.vertex(0)->coord(), f.vertex(1)->coord(), f.vertex(2)->coord());
83 }
84 else {
85 return Polygon<CoordType>::barycenter(f.vertices() | views::coords);
86 }
87}
88
100template<FaceConcept FaceType>
101auto faceArea(const FaceType& f)
102{
103 using CoordType = FaceType::VertexType::CoordType;
104 if constexpr (TriangleFaceConcept<FaceType>) {
106 f.vertex(0)->coord(), f.vertex(1)->coord(), f.vertex(2)->coord());
107 }
108 else {
109 if (f.vertexNumber() == 3) {
111 f.vertex(0)->coord(),
112 f.vertex(1)->coord(),
113 f.vertex(2)->coord());
114 }
115 else {
116 return Polygon<CoordType>::area(f.vertices() | views::coords);
117 }
118 }
119}
120
132template<FaceConcept FaceType>
133auto facePerimeter(const FaceType& f)
134{
135 using CoordType = FaceType::VertexType::CoordType;
136 if constexpr (TriangleFaceConcept<FaceType>) {
138 f.vertex(0)->coord(), f.vertex(1)->coord(), f.vertex(2)->coord());
139 }
140 else {
141 if (f.vertexNumber() == 3) {
143 f.vertex(0)->coord(),
144 f.vertex(1)->coord(),
145 f.vertex(2)->coord());
146 }
147 else {
148 return Polygon<CoordType>::perimeter(f.vertices() | views::coords);
149 }
150 }
151}
152
166template<FaceConcept FaceType>
167auto faceAngleOnVertexRad(const FaceType& f, uint vi)
168{
169 const auto& p0 = f.vertex(vi)->coord();
170 const auto& p1 = f.vertexMod((int) vi + 1)->coord();
171 const auto& p2 = f.vertexMod((int) vi - 1)->coord();
172 return (p2 - p0).angle(p1 - p0);
173}
174
175} // namespace vcl
176
177#endif // VCL_ALGORITHMS_CORE_POLYGON_GEOMETRY_H
PointT normal() const
Computes the normal of the polygon.
Definition polygon.h:190
PointT barycenter() const
Computes the barycenter of the polygon.
Definition polygon.h:203
ScalarType area() const
Returns the area of the polygon.
Definition polygon.h:235
ScalarType perimeter() const
Returns the perimeter of the polygon.
Definition polygon.h:228
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
ScalarType area() const
Computes the area of the triangle.
Definition triangle.h:219
ScalarType perimeter() const
Computes the perimeter of the triangle.
Definition triangle.h:209
PointT barycenter() const
Computes the barycenter of the triangle.
Definition triangle.h:122
PointT normal() const
Returns the normal of the triangle.
Definition triangle.h:112
Definition face.h:133
auto faceAngleOnVertexRad(const FaceType &f, uint vi)
Returns the internal angle (in radians) of the vi-th vertex of the face.
Definition geometry.h:167
auto facePerimeter(const FaceType &f)
Computes the perimeter of a face. Works both for triangle and polygonal faces, and it is optimized in...
Definition geometry.h:133
FaceType::VertexType::CoordType faceNormal(const FaceType &f)
Computes the normal of a face, without modifying the face. Works both for triangle and polygonal face...
Definition geometry.h:45
FaceType::VertexType::CoordType faceBarycenter(const FaceType &f)
Computes the barycenter of a face. Works both for triangle and polygonal faces, and it is optimized i...
Definition geometry.h:77
auto faceArea(const FaceType &f)
Computes the area of a face. Works both for triangle and polygonal faces, and it is optimized in case...
Definition geometry.h:101