DHART
Loading...
Searching...
No Matches
meshinfo.h
Go to the documentation of this file.
1#pragma once
8#ifndef HF_MESHINFO
9#define HF_MESHINFO
10
11#include <Dense>
12#include <array>
13
14//#define EIGEN_DONT_ALIGN_STATICALLY
15//#define EIGEN_DONT_VECTORIZE
16
17// [nanoRT]
18namespace HF::nanoGeom {
19 struct Mesh {
21 size_t num_faces;
22 double* vertices;
28 unsigned int* faces;
29 unsigned int* material_ids;
30 };
31}
32// end [nanoRT]
33
34namespace std {
37
51 template <typename SizeT>
52 inline void array_hash_combine_impl(SizeT& seed, SizeT value)
53 {
54 seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2);
55 }
56
58 template <typename T>
59 struct hash<std::array<T, 3>>
60 {
64
77 inline std::size_t operator()(const std::array<T, 3>& k) const
78 {
79 size_t seed = std::hash<T>()(k[0]);
80 array_hash_combine_impl(seed, std::hash<T>()(k[1]));
81 array_hash_combine_impl(seed, std::hash<T>()(k[2]));
82 return seed;
83 }
84 };
85}
86
87namespace HF::Geometry {
88
97 template <typename ptr_type>
99 int size;
100 ptr_type* data;
101
102 /* \brief Copy the contents of the array pointed to by this struct. */
103 inline std::vector<ptr_type> CopyArray() const {
104 return std::vector<ptr_type>(data, data + size);
105 }
106 };
107
123 template <typename numeric_type = float>
124 class MeshInfo {
125 public:
126 int meshid;
127 std::string name = "";
128 private:
129
130 using VertMatrix = Eigen::Matrix3X<numeric_type>;
132 Eigen::Matrix3X<int> indices;
133
134
140
145 void SetVert(int index, numeric_type x, numeric_type y, numeric_type z);
146
153
165 void VectorsToBuffers(const std::vector<std::array<numeric_type, 3>>& vertices);
166 public:
167
169
177 MeshInfo() { meshid = 0; verts.resize(3, 0); name = "INVALID"; };
178
187
218 const std::vector<std::array<numeric_type, 3>>& vertices,
219 int id,
220 std::string name = ""
221 );
222
232
266 const std::vector<numeric_type>& in_vertices,
267 const std::vector<int>& in_indexes,
268 int id,
269 std::string name = ""
270 );
271
312 void AddVerts(const std::vector<std::array<numeric_type, 3>>& verts);
313
316
347 int NumVerts() const;
348
351
383 int NumTris() const;
384
420
457
462 /*
463 \details
464 Uses Eigen's geometry module to create a rotation matrix, then applies it to
465 this mesh's vertex matrix.
466
467 \code
468 // be sure to #include "meshinfo.h", and #include <vector>
469
470 // Prepare the vertices
471 std::array<numeric_type, 3> vertex_0 = { 34.1, 63.9, 16.5 };
472 std::array<numeric_type, 3> vertex_1 = { 23.5, 85.7, 45.2 };
473 std::array<numeric_type, 3> vertex_2 = { 12.0, 24.6, 99.4 };
474
475 // Create an array of vertices
476 std::vector<std::array<numeric_type, 3>> vertices{ vertex_0, vertex_1, vertex_2 };
477
478 // Create the MeshInfo
479 HF::Geometry::MeshInfo mesh(vertices, 3451, "My Mesh");
480
481 // Perform rotation
482 numeric_type rot_x = 10.0;
483 numeric_type rot_y = 20.0;
484 numeric_type rot_z = 30.0;
485
486 mesh.PerformRotation(rot_x, rot_y, rot_z);
487
488 // Display the vertices for mesh
489 std::cout << "Vertices in mesh with ID " << mesh.GetMeshID() << ": " << std::endl;
490 for (auto vertex : mesh.GetVertsAsArrays()) {
491 std::cout << "(" << vertex[0] << ", " << vertex[1] << ", " << vertex[2] << ")" << std::endl;
492 }
493 \endcode
494 */
495 void PerformRotation(numeric_type rx, numeric_type ry, numeric_type rz);
496
499
521 int GetMeshID() const;
522
523
527
589 std::vector<numeric_type> GetIndexedVertices() const;
590
597
643 std::vector<int> getRawIndices() const;
644
646
688 std::vector<std::array<numeric_type, 3>> GetUnindexedVertices() const;
689
692
722 void SetMeshID(int new_id);
723
727
771 bool operator==(const MeshInfo& M2) const;
772
776
809 std::array<numeric_type, 3> operator[](int i) const;
810
817
824 };
825
826 template <typename T> MeshInfo()->MeshInfo<float>;
827}
828#endif
STL namespace.
void array_hash_combine_impl(SizeT &seed, SizeT value)
Combine the hash of value into seed.
Definition: meshinfo.h:52
Manipulate and load geometry from disk.
Definition: meshinfo.cpp:21
unsigned int * faces
[xyz] * 3(triangle) * num_faces
Definition: meshinfo.h:28
size_t num_faces
Definition: meshinfo.h:21
double * facevarying_vertex_colors
[xyz] * 3(triangle) * num_faces
Definition: meshinfo.h:27
size_t num_vertices
Definition: meshinfo.h:20
double * facevarying_binormals
[xyz] * 3(triangle) * num_faces
Definition: meshinfo.h:25
double * facevarying_tangents
[xyz] * 3(triangle) * num_faces
Definition: meshinfo.h:24
double * facevarying_uvs
[xyz] * 3(triangle) * num_faces
Definition: meshinfo.h:26
double * vertices
Definition: meshinfo.h:22
unsigned int * material_ids
triangle x num_faces
Definition: meshinfo.h:29
double * facevarying_normals
[xyz] * num_vertices
Definition: meshinfo.h:23
std::size_t operator()(const std::array< T, 3 > &k) const
Calculate a hash for k.
Definition: meshinfo.h:77
A simple type to hold the size and data pointer of an array.
Definition: meshinfo.h:98
int size
Number of elements in data
Definition: meshinfo.h:99
std::vector< ptr_type > CopyArray() const
Pointer to some array.
Definition: meshinfo.h:103
A collection of vertices and indices representing geometry.
Definition: meshinfo.h:124
void AddVerts(const std::vector< std::array< numeric_type, 3 > > &verts)
Add more vertices to this mesh.
Definition: meshinfo.cpp:162
MeshInfo(const std::vector< std::array< numeric_type, 3 > > &vertices, int id, std::string name="")
Construct a new MeshInfo object from an unindexed vector of vertices, an ID, and a name.
Eigen::Matrix3X< int > indices
3 by X matrix of indices for triangles.
Definition: meshinfo.h:132
int GetMeshID() const
Get the ID of this mesh.
Definition: meshinfo.cpp:241
bool operator==(const MeshInfo &M2) const
Compare the vertices of two MeshInfo objects.
Definition: meshinfo.cpp:310
void PerformRotation(numeric_type rx, numeric_type ry, numeric_type rz)
Rotate this mesh by x, y, z rotations in degrees (pitch, yaw, roll).
Definition: meshinfo.cpp:209
std::array< numeric_type, 3 > operator[](int i) const
Get vertex at a specific index in the mesh.
Definition: meshinfo.cpp:285
void SetMeshID(int new_id)
Change the ID of this mesh.
Definition: meshinfo.cpp:282
std::vector< std::array< numeric_type, 3 > > GetUnindexedVertices() const
Retrieve an unindexed array of this mesh's vertices.
Definition: meshinfo.cpp:264
MeshInfo(const std::vector< numeric_type > &in_vertices, const std::vector< int > &in_indexes, int id, std::string name="")
Construct a new MeshInfo object from an indexed vector of vertices.
void SetVert(int index, numeric_type x, numeric_type y, numeric_type z)
Change the position of the vertex at index.
Definition: meshinfo.cpp:40
int meshid
Identifier for this mesh.
Definition: meshinfo.h:126
void ConvertToOBJCoordinates()
Convert a mesh from Z-Up to Y-Up.
Definition: meshinfo.cpp:196
int NumTris() const
Calculate the total number of triangles in this mesh.
Definition: meshinfo.cpp:181
std::string name
A human-readable title.
Definition: meshinfo.h:127
int NumVerts() const
Determine how many vertices are in this mesh.
Definition: meshinfo.cpp:178
MeshInfo()
Construct an empty instance of MeshInfo.
Definition: meshinfo.h:177
void VectorsToBuffers(const std::vector< std::array< numeric_type, 3 > > &vertices)
Index vertices then insert them into verts and indices.
Definition: meshinfo.cpp:122
const array_and_size< int > GetIndexPointer() const
Get a pointer to the index array of this mesh.
Definition: meshinfo.cpp:326
const array_and_size< numeric_type > GetVertexPointer() const
Get a pointer to the vertex array of this mesh.
Definition: meshinfo.cpp:336
VertMatrix verts
3 by X matrix of vertices
Definition: meshinfo.h:131
Eigen::Matrix3X< numeric_type > VertMatrix
Definition: meshinfo.h:130
void ConvertToRhinoCoordinates()
Convert a mesh from Y-Up to Z-Up.
Definition: meshinfo.cpp:184
std::vector< numeric_type > GetIndexedVertices() const
A copy of every vertex in this array.
Definition: meshinfo.cpp:244
std::vector< int > getRawIndices() const
Retrieve a copy of this mesh's index buffer as a 1D array.
Definition: meshinfo.cpp:256