Skip to content

Commit 0df4316

Browse files
committed
Add dimension property to mesh types
1 parent 7355333 commit 0df4316

24 files changed

+131
-39
lines changed

addons/nd/doc_classes/ArrayCellMeshND.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<member name="cell_indices" type="PackedInt32Array" setter="set_cell_indices" getter="get_cell_indices" default="PackedInt32Array()">
3131
The indices of the cell cells. Every 4 integers defines a cell cell. Each integer is an index to the [member vertices] array. Integers in this array should not exceed the length of the vertices array, or else the mesh is invalid.
3232
</member>
33+
<member name="dimension" type="int" setter="set_dimension" getter="get_dimension" default="0">
34+
The dimension of the mesh. This is calculated as the length of the first vertex. Setting this will resize all vertices to the new dimension, either truncating them or padding them with zeros as necessary. The amount of indices making up a simplex cell is equal to the dimension.
35+
</member>
3336
<member name="vertices" type="PackedFloat64Array[]" setter="set_vertices" getter="get_vertices" default="[]">
3437
The vertices of the cell mesh. Each vertex is a VectorN ([PackedFloat64Array]) with a length equal to the number of dimensions. Indices of cells and edges refer to the items in this array. The vertices array must be kept consistent with the cell indices array, such that each cell index points to a valid vertex index, or else the mesh is invalid.
3538
</member>

addons/nd/doc_classes/ArrayWireMeshND.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
</method>
4545
</methods>
4646
<members>
47+
<member name="dimension" type="int" setter="set_dimension" getter="get_dimension" default="0">
48+
The dimension of the mesh. This is calculated as the length of the first vertex. Setting this will resize all vertices to the new dimension, either truncating them or padding them with zeros as necessary.
49+
</member>
4750
<member name="edge_indices" type="PackedInt32Array" setter="set_edge_indices" getter="get_edge_indices" default="PackedInt32Array()">
4851
The indices of the edges. Every 2 integers defines an edge. Each integer is an index to the [member vertices] array. Integers in this array should not exceed the length of the vertices array, or else the mesh is invalid.
4952
</member>

addons/nd/doc_classes/BoxCellMeshND.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
</method>
2525
</methods>
2626
<members>
27+
<member name="dimension" type="int" setter="set_dimension" getter="get_dimension" default="0">
28+
The dimension of the box. This is calculated as the length of [member size]. Setting this will resize [member size] to the new dimension.
29+
</member>
2730
<member name="half_extents" type="PackedFloat64Array" setter="set_half_extents" getter="get_half_extents">
2831
The half-extents of the box mesh in meters, also known as just "extents". This is the "radius" of the box. This is a wrapper around [member size] for situations where you want to use the extents instead of size. Since the box is centered at the origin, one vertex is located at the half-extents, and the rest have some of the components negated.
2932
</member>

addons/nd/doc_classes/BoxWireMeshND.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<tutorials>
1010
</tutorials>
1111
<members>
12+
<member name="dimension" type="int" setter="set_dimension" getter="get_dimension" default="0">
13+
The dimension of the box. This is calculated as the length of [member size]. Setting this will resize [member size] to the new dimension.
14+
</member>
1215
<member name="half_extents" type="PackedFloat64Array" setter="set_half_extents" getter="get_half_extents">
1316
The half-extents of the box mesh in meters, also known as just "extents". This is the "radius" of the box. This is a wrapper around [member size] for situations where you want to use the extents instead of size. Since the box is centered at the origin, one vertex is located at the half-extents, and the rest have some of the components negated.
1417
</member>

addons/nd/doc_classes/CellMeshND.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
Gets the cell indices of the cell mesh. Each set of [method get_indices_per_cell] integers represents the indices of the vertices that make up a cell, for a total of [method get_cell_count] cells. The vertices can be obtained using [method MeshND.get_vertices]. Integers in this array should not exceed the length of the vertices array.
4444
</description>
4545
</method>
46-
<method name="get_indices_per_cell" qualifiers="const">
46+
<method name="get_indices_per_cell">
4747
<return type="int" />
4848
<description>
4949
Gets the number of indices per cell. This is the number of vertices that make up a cell. For example, a triangle has 3 vertices, so the number of indices per cell is 3. The total number of indices in the [method get_cell_indices] array is equal to this value multiplied by [method get_cell_count].
@@ -56,9 +56,4 @@
5656
</description>
5757
</method>
5858
</methods>
59-
<members>
60-
<member name="dimension" type="int" setter="set_dimension" getter="get_dimension" default="0">
61-
The dimension of the mesh. The amount of indices making up a simplex cell is usually equal to the dimension.
62-
</member>
63-
</members>
6459
</class>

addons/nd/doc_classes/MeshND.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
[b]Note[/b]: This only looks at edge indices. It does not remove duplicate vertices, nor does it have access to the vertices array.
4343
</description>
4444
</method>
45+
<method name="get_dimension">
46+
<return type="int" />
47+
<description>
48+
Returns the dimension of the mesh. This is calculated as the length of the first vertex. Setting this will resize all vertices to the new dimension, either truncating them or padding them with zeros as necessary. For [CellMeshND], the amount of indices making up a simplex cell is usually equal to the dimension.
49+
</description>
50+
</method>
4551
<method name="get_edge_indices">
4652
<return type="PackedInt32Array" />
4753
<description>

addons/nd/doc_classes/OrthoplexCellMeshND.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
</method>
2929
</methods>
3030
<members>
31+
<member name="dimension" type="int" setter="set_dimension" getter="get_dimension" default="0">
32+
The dimension of the orthoplex. This is calculated as the length of [member size]. Setting this will resize [member size] to the new dimension.
33+
</member>
3134
<member name="half_extents" type="PackedFloat64Array" setter="set_half_extents" getter="get_half_extents">
3235
The half-extents of the orthoplex in meters, also known as just "extents". This is the "radius" of the orthoplex, the distance between each vertex and the center. This is a wrapper around [member size] for situations where you want to use the extents instead of size.
3336
</member>

addons/nd/doc_classes/OrthoplexWireMeshND.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<link title="Wikipedia 16-cell / 4D Orthoplex">https://en.wikipedia.org/wiki/16-cell</link>
1414
</tutorials>
1515
<members>
16+
<member name="dimension" type="int" setter="set_dimension" getter="get_dimension" default="0">
17+
The dimension of the orthoplex. This is calculated as the length of [member size]. Setting this will resize [member size] to the new dimension.
18+
</member>
1619
<member name="half_extents" type="PackedFloat64Array" setter="set_half_extents" getter="get_half_extents">
1720
The half-extents of the orthoplex in meters, also known as just "extents". This is the "radius" of the orthoplex, the distance between each vertex and the center. This is a wrapper around [member size] for situations where you want to use the extents instead of size.
1821
</member>

model/cell/array_cell_mesh_nd.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ void ArrayCellMeshND::_clear_cache() {
99

1010
bool ArrayCellMeshND::validate_mesh_data() {
1111
const int64_t cell_indices_count = _cell_indices.size();
12-
if (cell_indices_count % _dimension != 0) {
12+
const int dimension = get_dimension();
13+
if (cell_indices_count % dimension != 0) {
1314
return false; // Must be a multiple of _dimension.
1415
}
1516
const int64_t cell_normals_count = _cell_normals.size();
16-
if (cell_normals_count > 0 && cell_normals_count * _dimension != cell_indices_count) {
17-
return false; // Must be have one normal per cell (_dimension indices).
17+
if (cell_normals_count > 0 && cell_normals_count * dimension != cell_indices_count) {
18+
return false; // Must be have one normal per cell (dimension indices).
1819
}
1920
const int64_t vertex_count = _vertices.size();
2021
for (int32_t cell_index : _cell_indices) {
@@ -57,6 +58,7 @@ void ArrayCellMeshND::merge_with(const Ref<ArrayCellMeshND> &p_other, const Ref<
5758
const int64_t other_vertex_count = p_other->_vertices.size();
5859
const int64_t end_cell_index_count = start_cell_index_count + other_cell_index_count;
5960
const int64_t end_vertex_count = start_vertex_count + other_vertex_count;
61+
const int dimension = get_dimension();
6062
_cell_indices.resize(end_cell_index_count);
6163
_vertices.resize(end_vertex_count);
6264
// Copy in the cell indices and vertices from the other mesh.
@@ -68,17 +70,17 @@ void ArrayCellMeshND::merge_with(const Ref<ArrayCellMeshND> &p_other, const Ref<
6870
}
6971
// Can't simply add these together in case the first mesh has no normals.
7072
if (start_cell_normal_count > 0 || other_cell_normal_count > 0) {
71-
const int64_t end_cell_normal_count = end_cell_index_count / _dimension;
73+
const int64_t end_cell_normal_count = end_cell_index_count / dimension;
7274
_cell_normals.resize(end_cell_normal_count);
73-
const int64_t start_normal_count = start_cell_index_count / _dimension;
75+
const int64_t start_normal_count = start_cell_index_count / dimension;
7476
// Initialize the mesh's normals to zero if it has none.
7577
if (start_cell_normal_count == 0) {
7678
for (int64_t i = 0; i < start_normal_count; i++) {
7779
_cell_normals.set(i, VectorN());
7880
}
7981
}
8082
if (other_cell_normal_count == 0) {
81-
for (int64_t i = 0; i < other_cell_index_count / _dimension; i++) {
83+
for (int64_t i = 0; i < other_cell_index_count / dimension; i++) {
8284
_cell_normals.set(start_normal_count + i, VectorN());
8385
}
8486
}
@@ -131,6 +133,16 @@ void ArrayCellMeshND::set_vertices_bind(const TypedArray<VectorN> &p_vertices) {
131133
reset_mesh_data_validation();
132134
}
133135

136+
void ArrayCellMeshND::set_dimension(int p_dimension) {
137+
ERR_FAIL_COND_MSG(p_dimension < 0, "ArrayCellMeshND: Dimension must not be negative.");
138+
ERR_FAIL_COND_MSG(p_dimension > 1000, "ArrayCellMeshND: Too many dimensions for cell mesh.");
139+
for (int i = 0; i < _vertices.size(); i++) {
140+
_vertices.set(i, VectorND::with_dimension(_vertices[i], p_dimension));
141+
}
142+
_clear_cache();
143+
reset_mesh_data_validation();
144+
}
145+
134146
void ArrayCellMeshND::_bind_methods() {
135147
ClassDB::bind_method(D_METHOD("append_vertex", "vertex", "deduplicate_vertices"), &ArrayCellMeshND::append_vertex, DEFVAL(true));
136148

@@ -139,6 +151,8 @@ void ArrayCellMeshND::_bind_methods() {
139151
// Only bind the setters here because the getters are already bound in CellMeshND.
140152
ClassDB::bind_method(D_METHOD("set_cell_indices", "cell_indices"), &ArrayCellMeshND::set_cell_indices);
141153
ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &ArrayCellMeshND::set_vertices_bind);
154+
ClassDB::bind_method(D_METHOD("set_dimension", "dimension"), &ArrayCellMeshND::set_dimension);
142155
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "cell_indices"), "set_cell_indices", "get_cell_indices");
143156
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "vertices", PROPERTY_HINT_ARRAY_TYPE, "PackedFloat64Array"), "set_vertices", "get_vertices");
157+
ADD_PROPERTY(PropertyInfo(Variant::INT, "dimension", PROPERTY_HINT_RANGE, "0,1000,1", PROPERTY_USAGE_EDITOR), "set_dimension", "get_dimension");
144158
}

model/cell/array_cell_mesh_nd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ class ArrayCellMeshND : public CellMeshND {
3131
virtual Vector<VectorN> get_vertices() override;
3232
void set_vertices(const Vector<VectorN> &p_vertices);
3333
void set_vertices_bind(const TypedArray<VectorN> &p_vertices);
34+
void set_dimension(int p_dimension);
3435
};

0 commit comments

Comments
 (0)