Skip to content

Commit e2ccc3a

Browse files
committed
add volume measurement
1 parent 07391d6 commit e2ccc3a

File tree

8 files changed

+104
-1
lines changed

8 files changed

+104
-1
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
9+
## Unreleased
10+
11+
### Added
12+
13+
* Volume calculation of closed triangle mesh.
14+
15+
### Changed
16+
17+
### Removed

scripts/test_volume.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from compas.geometry import Box, Frame
2+
from compas.datastructures import Mesh
3+
4+
from compas_cgal.measure import volume
5+
6+
box = Box(Frame.worldXY(), 1, 1, 1)
7+
8+
mesh = Mesh.from_shape(box)
9+
mesh.quads_to_triangles()
10+
11+
V = volume(mesh.to_vertices_and_faces())
12+
13+
print(V)

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def get_library_dirs():
5959
'src/booleans.cpp',
6060
'src/slicer.cpp',
6161
'src/intersections.cpp',
62+
'src/measure.cpp',
6263
]),
6364
include_dirs=[
6465
'./include',
@@ -166,6 +167,7 @@ def build_extensions(self):
166167
"Programming Language :: Python :: 3",
167168
"Programming Language :: Python :: 3.6",
168169
"Programming Language :: Python :: 3.7",
170+
"Programming Language :: Python :: 3.8",
169171
"Programming Language :: Python :: Implementation :: CPython",
170172
],
171173
keywords=[],

src/compas_cgal.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ void init_meshing(py::module&);
77
void init_booleans(py::module&);
88
void init_slicer(py::module&);
99
void init_intersections(py::module&);
10+
void init_measure(py::module&);
1011

1112
PYBIND11_MODULE(_cgal, m) {
1213
m.doc() = "";
@@ -19,4 +20,5 @@ PYBIND11_MODULE(_cgal, m) {
1920
init_booleans(m);
2021
init_slicer(m);
2122
init_intersections(m);
23+
init_measure(m);
2224
}

src/compas_cgal/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
'compas_cgal.booleans',
6969
'compas_cgal.intersections',
7070
'compas_cgal.meshing',
71-
'compas_cgal.slicer'
71+
'compas_cgal.slicer',
7272
]
7373

7474
__all__ = ["HOME", "DATA", "DOCS", "TEMP"]

src/compas_cgal/measure.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
from compas_cgal._cgal import measure
3+
from compas.plugins import plugin
4+
5+
6+
__all__ = [
7+
'volume'
8+
]
9+
10+
11+
@plugin(category='trimesh', pluggable_name='trimesh_volume')
12+
def volume(mesh):
13+
"""Compute the volume of a closed triangle mesh.
14+
15+
Parameters
16+
----------
17+
mesh : tuple of vertices and faces
18+
The mesh.
19+
20+
Returns
21+
-------
22+
float
23+
The volume of the mesh.
24+
25+
"""
26+
V, F = mesh
27+
V = np.asarray(V, dtype=np.float64)
28+
F = np.asarray(F, dtype=np.int32)
29+
return measure.volume(V, F)

src/measure.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "measure.h"
2+
3+
#include <CGAL/Polygon_mesh_processing/measure.h>
4+
5+
namespace PMP = CGAL::Polygon_mesh_processing;
6+
namespace py = pybind11;
7+
8+
double
9+
pmp_volume(
10+
Eigen::Ref<const compas::RowMatrixXd> & V,
11+
Eigen::Ref<const compas::RowMatrixXi> & F)
12+
{
13+
Mesh mesh = compas::mesh_from_vertices_and_faces(V, F);
14+
15+
double volume = PMP::volume(mesh);
16+
17+
return volume;
18+
};
19+
20+
void init_measure(py::module & m) {
21+
py::module submodule = m.def_submodule("measure");
22+
23+
submodule.def(
24+
"volume",
25+
&pmp_volume,
26+
py::arg("V").noconvert(),
27+
py::arg("F").noconvert()
28+
);
29+
};

src/measure.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef COMPAS_MEASURE_H
2+
#define COMPAS_MEASURE_H
3+
4+
#include <compas.h>
5+
6+
double
7+
pmp_volume(
8+
Eigen::Ref<const compas::RowMatrixXd> & V,
9+
Eigen::Ref<const compas::RowMatrixXi> & F);
10+
11+
#endif /* COMPAS_MEASURE_H */

0 commit comments

Comments
 (0)