NDEVR
API Documentation
GriddedMesh.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Design
28File: GriddedMesh
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Model.h>
34#include <NDEVR/Geometry.h>
35namespace NDEVR
36{
40 template<uint01 t_dims>
42 {
43 public:
50 {
51 lib_assert(location < m_size, "Out of bounds grid conversion lookup");
52 switch (t_dims)
53 {
54 case 1: return location[X];
55 case 2: return m_size[X] * location[Y] + location[X];
56 case 3: return m_size[X] * m_size[Y] * location[Z] + m_size[X] * location[Y] + location[X];
57 }
58 return Constant<uint04>::Invalid;
59 }
60
67 {
68 Vector<t_dims, uint04> vec_location;
69 switch (t_dims)
70 {
71 case 1:
72 vec_location[X] = location;
73 break;
74 case 2:
75 vec_location[Y] = location / m_size[X];
76 vec_location[X] = location % m_size[X];
77 break;
78 case 3:
79 vec_location[Z] = location / (m_size[X] * m_size[Y]);
80 vec_location[Y] = (location / m_size[X]) % m_size[Y];
81 vec_location[X] = location % m_size[X];
82 break;
83 }
84 lib_assert(vec_location < m_size, "Out of bounds grid conversion lookup");
85 return vec_location;
86 }
87
95 static constexpr uint01 getNumberOfCorners()
96 {
97 switch (t_dims)
98 {
99 case 1: return 2;
100 case 2: return 4;
101 case 3: return 8;
102 }
103 return Constant<uint01>::Invalid;
104 }
105
111 {
112 m_size = index_size;
113 }
114 protected:
116 };
117
122 template<uint01 t_dims>
123 class GridMesh : public GridIndexing<t_dims>
124 {
125 public:
130 {}
131
136 explicit GridMesh(const Geometry& model)
137 : m_geo(model)
138 {}
139
144 bool isValid() const
145 {
146 return m_geo.isValid();
147 }
148
164 , Geometry::VertexMode position
171 {
173 //m_geo.setGeometryType(t_dims == 2 ? GeometryType::e_grids : GeometryType::e_block_models);
174 m_geo.setGeometryType(GeometryType::e_points);
175 uint04 count = index_size.product();
176 m_geo.setupVertexTable(count, position, normal, color, texture, tangent, bitangent, bones);
177 BitFlag flag(0);
180 //flag(VertexBitFlags::e_, true);
182 for (uint04 i = 0; i < count; i++)
183 {
184 iterator.setFlag(i, flag);
186 }
187 m_geo.updateVertexColumns();
188 //m_geo.addPrimitive(PrimitiveProperty::Solid, index_size);
189 }
190
196 {
198 }
199
206 template<class t_type>
207 void setVertex(VertexProperty property, Vector<t_dims, uint04> index, const t_type& vector)
208 {
209 m_geo.setVertex(property, GridIndexing<t_dims>::convertToIndex(index), vector);
210 }
211
218 template<class t_type>
219 t_type vertex(VertexProperty property, Vector<t_dims, uint04> index) const
220 {
221 return m_geo.vertex<t_type>(property, GridIndexing<t_dims>::convertToIndex(index));
222 }
223
230 template<class t_type>
231 void setVertexProperty(uint04 property_index, Vector<t_dims, uint04> index, const t_type& value)
232 {
233 m_geo.set(property_index, GridIndexing<t_dims>::convertToIndex(index), value);
234 }
235
245 template<class t_type>
246 t_type getVertexProperty(uint04 property_index, Vector<t_dims, uint04> index) const
247 {
248 return m_geo.get(property_index, GridIndexing<t_dims>::convertToIndex(index));
249 }
250
262 template<class t_type>
263 t_type interpolateVertex(VertexProperty property, const Vector<t_dims, fltp08>& index, InterpolationValues interpolation = InterpolationValues::e_linear) const
264 {
266 for (uint01 dim = 0; dim < t_dims; dim++)
267 {
268 if (index[dim] < 0 || index[dim] > size[dim])
269 return Constant<t_type>::Invalid;
270 }
271 Vector<t_dims, uint04> discrete = index.template as<t_dims, uint04>();
272 Vector<t_dims, fltp08> dt = index - discrete.template as<t_dims, fltp08>();
273
274 if (dt == Vector<t_dims, fltp08>(0.0) || interpolation == InterpolationValues::e_nearest_neighbor)
275 return vertex<t_type>(property, discrete);
276
278 for (uint01 i = 0; i < GridIndexing<t_dims>::getNumberOfCorners(); i++)
279 {
280 Vector<t_dims, uint04> location_sum(0);
281 if constexpr (t_dims >= 3)
282 location_sum[Z] = (i / 4) % 2;
283 if constexpr (t_dims >= 2)
284 location_sum[Y] = (i / 2) % 2;
285 if constexpr (t_dims >= 1)
286 location_sum[X] = (i / 1) % 2;
287 if (discrete[X] == size[X] - 1)
288 location_sum[X] = 0;
289 if (discrete[Y] == size[Y] - 1)
290 location_sum[Y] = 0;
291 center[i] = vertex<t_type>(property, discrete + location_sum);
292 }
293
294 if (IsInvalid(center))
295 {
296 return Constant<t_type>::Invalid;
297 }
298 else if (interpolation == InterpolationValues::e_linear)
299 {
300 t_type value = 0;
301 for (uint01 i = 0; i < GridIndexing<t_dims>::getNumberOfCorners(); i++)
302 {
303 Vector<t_dims, uint04> location_sum(0);
304 if constexpr (t_dims >= 3)
305 location_sum[Z] = (i / 4) % 2;
306 if constexpr (t_dims >= 2)
307 location_sum[Y] = (i / 2) % 2;
308 if constexpr (t_dims >= 1)
309 location_sum[X] = (i / 1) % 2;
310 Vector<t_dims, fltp08> location_s = (discrete + location_sum).template as<t_dims, fltp08>();
311 Vector<t_dims, fltp08> difference = (1.0 - abs(location_s - index));
312 value += center[i] * difference.product();
313 }
314 return value;
315 }
316 else//bicubic
317 {
318 /*const uint04 d10 = getZ(discrete_x - 1, discrete_y + 0, d11);
319 const uint04 d13 = getZ(discrete_x + 2, discrete_y + 0, d12);
320 const uint04 d20 = getZ(discrete_x - 1, discrete_y + 1, d21);
321 const uint04 d23 = getZ(discrete_x + 2, discrete_y + 1, d22);
322
323 const uint04 d00 = getZ(discrete_x - 1, discrete_y - 1, d10);
324 const uint04 d01 = getZ(discrete_x - 0, discrete_y - 1, d11);
325 const uint04 d02 = getZ(discrete_x + 1, discrete_y - 1, d12);
326 const uint04 d03 = getZ(discrete_x + 2, discrete_y - 1, d02);
327
328 const uint04 d30 = getZ(discrete_x - 1, discrete_y + 2, d20);
329 const uint04 d31 = getZ(discrete_x - 0, discrete_y + 2, d21);
330 const uint04 d32 = getZ(discrete_x + 1, discrete_y + 2, d22);
331 const uint04 d33 = getZ(discrete_x + 2, discrete_y + 2, d23);
332
333 const fltp08 p0 = (d01 + 0.5 * dx
334 * (d02 - d00 + dx * (2.0 * d00 - 5.0 * d01 + 4.0 * d02 - d03 + dx * (3.0 * (d01 - d02) + d03 - d00))));
335 const fltp08 p1 = (d11 + 0.5 * dx
336 * (d12 - d10 + dx * (2.0 * d10 - 5.0 * d11 + 4.0 * d12 - d13 + dx * (3.0 * (d11 - d12) + d13 - d10))));
337 const fltp08 p2 = (d21 + 0.5 * dx
338 * (d22 - d20 + dx * (2.0 * d20 - 5.0 * d21 + 4.0 * d22 - d23 + dx * (3.0 * (d21 - d22) + d23 - d20))));
339 const fltp08 p3 = (d31 + 0.5 * dx
340 * (d32 - d30 + dx * (2.0 * d30 - 5.0 * d31 + 4.0 * d32 - d33 + dx * (3.0 * (d31 - d32) + d33 - d30))));
341
342 return (p1 + 0.5f * dy * (p2 - p0 + dy * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + dy * (3.0 * (p1 - p2) + p3 - p0))));*/
343 }
344 return Constant<t_type>::Invalid;
345 }
346
351 Geometry geometry() const { return m_geo; };
352 protected:
354 };
355};
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:55
A core class within the model hierarchy containing vertex-based data (Usually 3D data) within a set c...
Definition Geometry.h:143
VertexMode
Describes the storage format and coordinate system of vertex data.
Definition Geometry.h:150
@ e_no_vertex
No vertex data present.
Definition Geometry.h:151
Converts 1 dimensional index into N dimensional index based on size of each dimension.
Definition GriddedMesh.h:42
static constexpr uint01 getNumberOfCorners()
Returns the number of corners for a grid cell of the given dimensionality.
Definition GriddedMesh.h:95
void setSize(Vector< t_dims, uint04 > index_size)
Sets the size of the grid in each dimension.
Vector< t_dims, uint04 > convertFromIndex(const uint04 &location) const
Converts a flat 1D index back into an N-dimensional grid coordinate.
Definition GriddedMesh.h:66
Vector< t_dims, uint04 > m_size
The number of elements along each dimension of the grid.
uint04 convertToIndex(const Vector< t_dims, uint04 > &location) const
Converts an N-dimensional grid coordinate into a flat 1D index using row-major ordering.
Definition GriddedMesh.h:49
t_type interpolateVertex(VertexProperty property, const Vector< t_dims, fltp08 > &index, InterpolationValues interpolation=InterpolationValues::e_linear) const
Interpolates a vertex property value at a fractional N-dimensional grid coordinate.
bool isValid() const
Checks whether this GridMesh has a valid underlying Geometry.
Geometry geometry() const
Returns the underlying Geometry object.
GridMesh()
Default constructor.
void setVertex(VertexProperty property, Vector< t_dims, uint04 > index, const t_type &vector)
Sets a vertex property value at the given N-dimensional grid index.
GridMesh(const Geometry &model)
Constructs a GridMesh wrapping the given Geometry object.
Geometry m_geo
The underlying Geometry object that stores the grid vertex data.
void setupVertexTable(Vector< t_dims, uint04 > index_size, Geometry::VertexMode position, Geometry::VertexMode normal=Geometry::VertexMode::e_no_vertex, Geometry::VertexMode color=Geometry::VertexMode::e_no_vertex, Geometry::VertexMode texture=Geometry::VertexMode::e_no_vertex, Geometry::VertexMode tangent=Geometry::VertexMode::e_no_vertex, Geometry::VertexMode bitangent=Geometry::VertexMode::e_no_vertex, Geometry::VertexMode bones=Geometry::VertexMode::e_no_vertex)
Initializes the vertex table for the grid with the specified dimensions and vertex modes.
t_type vertex(VertexProperty property, Vector< t_dims, uint04 > index) const
Retrieves a vertex property value at the given N-dimensional grid index.
t_type getVertexProperty(uint04 property_index, Vector< t_dims, uint04 > index) const
Retrieves a custom vertex property by column index at the given N-dimensional grid index.
const Vector< t_dims, uint04 > & getIndexSize() const
Returns the size of the grid in each dimension.
void setVertexProperty(uint04 property_index, Vector< t_dims, uint04 > index, const t_type &value)
Sets a custom vertex property by column index at the given N-dimensional grid index.
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
constexpr t_type product() const
Returns the product, or value of each dimension multiplied together.
Definition Vector.hpp:510
Typed vertex iterator providing array-style access to geometry vertex data.
Definition Geometry.h:2566
void setFlag(uint04 index, const BitFlag &value)
Sets the flag value for a vertex, creating the flag column if necessary.
Definition Geometry.h:2535
void setVertex(uint04 index, const t_type &value)
Sets a vertex value at the given index.
Definition Geometry.h:2527
The primary namespace for the NDEVR SDK.
VertexProperty
Per-vertex data channels that can be stored in the vertex table to be used by Geometry.
@ Position
XYZ position of the vertex.
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Changes an input with a negative sign, to a positive sign.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
@ e_points
Point cloud or discrete points.
double fltp08
Defines an alias representing an 8 byte floating-point number.
InterpolationValues
Values that represent interpolation functions.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
static constexpr Angle< t_type > difference(const Angle< t_type > &angle_a, const Angle< t_type > &angle_b)
Calculates minimal absolute signed angle between two angles.
@ e_is_filtered
Vertex is excluded by a filter.
@ e_is_hidden
Vertex is hidden from display.
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408