1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright © 2026 The µcad authors <info@microcad.xyz>
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "rust/cxx.h"
#include <memory>
#include <manifold/manifold.h>
namespace manifold
{
using Mesh = MeshGLP<float, uint32_t>;
} // namespace manifold
namespace manifold_rs
{
/// @brief Wrapper around manifold::Polygons
/// @details This class will be exposed to Rust
class Polygons
{
public:
Polygons();
Polygons(::manifold::Polygons &&polygons);
~Polygons();
/// @brief Get the number of polygons
size_t size() const;
/// @brief Get a polygon by index as a slice of doubles
rust::Slice<const double> get_as_slice(size_t index) const;
std::unique_ptr<::manifold::Polygons> polygons;
};
/// @brief Wrapper around manifold::Manifold
/// @details This class will be exposed to Rust
class Manifold
{
public:
Manifold();
Manifold(::manifold::Manifold &&manifold);
~Manifold();
/// @brief Does Manifold have triangles?
bool is_empty() const;
/// @brief Slice the manifold at a given height
std::unique_ptr<Polygons> slice(double height) const;
/// @brief Project the manifold
std::unique_ptr<Polygons> project() const;
/// @brief Trim the manifold by a plane
/// @param x X coordinate of normal vector of the plane
/// @param y Y coordinate of normal vector of the plane
/// @param z Z coordinate of normal vector of the plane
/// @param offset Offset of the plane
std::unique_ptr<Manifold> trim_by_plane(double x, double y, double z, double offset) const;
/// @brief Calculate the convex hull
std::unique_ptr<Manifold> hull() const;
/// @brief Translate the manifold
std::unique_ptr<Manifold> translate(double x, double y, double z) const;
/// @brief Scale the manifold
std::unique_ptr<Manifold> scale(double x, double y, double z) const;
/// @brief Rotate the manifold
std::unique_ptr<Manifold> rotate(double x_degrees, double y_degrees = 0.0,
double z_degrees = 0.0) const;
/// @brief Refine manifold `n` times
std::unique_ptr<Manifold> refine(std::int32_t n) const;
/// Refine manifold to Length.
std::unique_ptr<Manifold> refine_to_length(double t) const;
/// Refine to tolerance.
std::unique_ptr<Manifold> refine_to_tolerance(double t) const;
/// Smooth by normals.
std::unique_ptr<Manifold> smooth_by_normals(std::int32_t n) const;
/// Smooth out.
std::unique_ptr<Manifold> smooth_out(double min_sharp_angle, double min_smoothness) const;
/// Calculate normals for the manifold and return a new one.
std::unique_ptr<Manifold> calculate_normals(std::int32_t normal_idx, double min_sharp_angle) const;
std::unique_ptr<::manifold::Manifold> manifold;
};
/// @brief Create a new empty manifold
std::unique_ptr<Manifold> empty();
/// @brief Create a tetrahedron.
std::unique_ptr<Manifold> tetrahedron();
/// @brief Create a cube.
/// @param x_size A size of the cube in x direction
/// @param y_size A size of the cube in y direction
/// @param z_size A size of the cube in z direction
/// @return A new cube as a Manifold
std::unique_ptr<Manifold> cube(double x_size, double y_size, double z_size);
/// @brief Create a sphere.
/// @param radius Radius of the sphere
/// @param circular_segments Number of circular segments
/// @return A new sphere as a Manifold
std::unique_ptr<Manifold> sphere(double radius, uint32_t circular_segments);
/// @brief Create a cylinder.
/// @param radius_low Lower radius of the cylinder
/// @param radius_high Higher radius of the cylinder
/// @param height Height of the cylinder
/// @param circular_segments Number of circular segments
/// @return A new cylinder as a Manifold
std::unique_ptr<Manifold> cylinder(double radius_low, double radius_high, double height, uint32_t circular_segments);
/// @brief Perform a union operation
/// @param a First manifold
/// @param b Second manifold
/// @return A new manifold as a result of the union operation
std::unique_ptr<Manifold> union_(const Manifold &a, const Manifold &b);
/// @brief Perform an intersection operation
/// @param a First manifold
/// @param b Second manifold
/// @return A new manifold as a result of the intersection operation
std::unique_ptr<Manifold> intersection(const Manifold &a, const Manifold &b);
/// @brief Perform a difference operation
/// @param a First manifold
/// @param b Second manifold
/// @return A new manifold as a result of the difference operation
std::unique_ptr<Manifold> difference(const Manifold &a, const Manifold &b);
/// @brief Extrude a multi-polygon to create a 3D shape
std::unique_ptr<Manifold> extrude(
rust::Slice<const rust::Slice<const double>> multi_polygon_data,
double height, uint32_t divisions, double twist_degrees, double scale_top_x, double scale_top_y);
/// @brief Revolve a multi-polygon to create a 3D shape
std::unique_ptr<Manifold> revolve(
rust::Slice<const rust::Slice<const double>> multi_polygon_data,
uint32_t circular_segments, double angle);
/// @brief A mesh, which is a collection of vertices and indices
/// @details This class will be exposed to Rust
class Mesh
{
public:
Mesh();
Mesh(::manifold::Mesh &&mesh);
~Mesh();
/// @brief Number of vertex properties of the mesh.
std::uint32_t num_props() const;
/// @brief Get the vertices of the mesh
/// @details The vertex coefficients are in the following order:
/// * position x, y, z
/// @return A vector of vertices
std::unique_ptr<std::vector<float>> vertices() const;
/// @brief Get the indices of the mesh
std::unique_ptr<std::vector<uint32_t>> indices() const;
std::unique_ptr<::manifold::Mesh> mesh;
};
/// @brief Create a mesh from a manifold
/// @param manifold A manifold
/// @return A new mesh
std::unique_ptr<Mesh> mesh_from_manifold(const Manifold &manifold);
/// @brief Create a manifold from a mesh
/// @param mesh A mesh
/// @return A new manifold
std::unique_ptr<Manifold> manifold_from_mesh(const Mesh &mesh);
/// @brief Create a mesh from vertices and indices
/// @param vertices Vertices, a slice of floats where each 3 elements represent a vertex position (x, y, z)
/// @param indices Indices
/// @return A new mesh
std::unique_ptr<Mesh> mesh_from_vertices(
rust::Slice<const float> vertices,
rust::Slice<const uint32_t> indices);
} // namespace manifold_rs