Visual Computing Library  devel
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_MESH_ELEM_ALGORITHMS_POLYGON_GEOMETRY_H
24#define VCL_MESH_ELEM_ALGORITHMS_POLYGON_GEOMETRY_H
25
26#include <vclib/mesh/elements.h>
27#include <vclib/mesh/views/components.h>
28
29#include <vclib/space/core.h>
30
31namespace vcl {
32
45template<FaceConcept FaceType>
46typename FaceType::VertexType::PositionType faceNormal(const FaceType& f)
47{
48 using PositionType = FaceType::VertexType::PositionType;
49 if constexpr (TriangleFaceConcept<FaceType>) {
51 f.vertex(0)->position(),
52 f.vertex(1)->position(),
53 f.vertex(2)->position());
54 }
55 else {
56 if (f.vertexNumber() == 3) {
58 f.vertex(0)->position(),
59 f.vertex(1)->position(),
60 f.vertex(2)->position());
61 }
62 else {
64 f.vertices() | views::positions);
65 }
66 }
67}
68
80template<FaceConcept FaceType>
81typename FaceType::VertexType::PositionType faceBarycenter(const FaceType& f)
82{
83 using PositionType = FaceType::VertexType::PositionType;
84 if constexpr (TriangleFaceConcept<FaceType>) {
86 f.vertex(0)->position(),
87 f.vertex(1)->position(),
88 f.vertex(2)->position());
89 }
90 else {
92 f.vertices() | views::positions);
93 }
94}
95
107template<FaceConcept FaceType>
108auto faceArea(const FaceType& f)
109{
110 using PositionType = FaceType::VertexType::PositionType;
111 if constexpr (TriangleFaceConcept<FaceType>) {
113 f.vertex(0)->position(),
114 f.vertex(1)->position(),
115 f.vertex(2)->position());
116 }
117 else {
118 if (f.vertexNumber() == 3) {
120 f.vertex(0)->position(),
121 f.vertex(1)->position(),
122 f.vertex(2)->position());
123 }
124 else {
125 return Polygon<PositionType>::area(f.vertices() | views::positions);
126 }
127 }
128}
129
141template<FaceConcept FaceType>
142auto facePerimeter(const FaceType& f)
143{
144 using PositionType = FaceType::VertexType::PositionType;
145 if constexpr (TriangleFaceConcept<FaceType>) {
147 f.vertex(0)->position(),
148 f.vertex(1)->position(),
149 f.vertex(2)->position());
150 }
151 else {
152 if (f.vertexNumber() == 3) {
154 f.vertex(0)->position(),
155 f.vertex(1)->position(),
156 f.vertex(2)->position());
157 }
158 else {
160 f.vertices() | views::positions);
161 }
162 }
163}
164
178template<FaceConcept FaceType>
179auto faceAngleOnVertexRad(const FaceType& f, uint vi)
180{
181 const auto& p0 = f.vertex(vi)->position();
182 const auto& p1 = f.vertexMod((int) vi + 1)->position();
183 const auto& p2 = f.vertexMod((int) vi - 1)->position();
184 return (p2 - p0).angle(p1 - p0);
185}
186
187} // namespace vcl
188
189#endif // VCL_MESH_ELEM_ALGORITHMS_POLYGON_GEOMETRY_H
A class representing a box in N-dimensional space.
Definition box.h:46
PointT normal() const
Computes the normal of the polygon.
Definition polygon.h:187
PointT barycenter() const
Computes the barycenter of the polygon.
Definition polygon.h:200
ScalarType area() const
Returns the area of the polygon.
Definition polygon.h:232
ScalarType perimeter() const
Returns the perimeter of the polygon.
Definition polygon.h:225
ScalarType area() const
Computes the area of the triangle.
Definition triangle.h:217
ScalarType perimeter() const
Computes the perimeter of the triangle.
Definition triangle.h:207
PointT barycenter() const
Computes the barycenter of the triangle.
Definition triangle.h:120
PointT normal() const
Returns the normal of the triangle.
Definition triangle.h:110
Definition face.h:305
auto faceAngleOnVertexRad(const FaceType &f, uint vi)
Returns the internal angle (in radians) of the vi-th vertex of the face.
Definition geometry.h:179
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:142
FaceType::VertexType::PositionType faceNormal(const FaceType &f)
Computes the normal of a face, without modifying the face. Works both for triangle and polygonal face...
Definition geometry.h:46
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:108
FaceType::VertexType::PositionType 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:81