DHART
Loading...
Searching...
No Matches
HF::Geometry Namespace Reference

Manipulate and load geometry from disk. More...

Classes

struct  array_and_size
 A simple type to hold the size and data pointer of an array. More...
 
class  MeshInfo
 A collection of vertices and indices representing geometry. More...
 
struct  tinyobj_attr
 
struct  tinyobj_geometry
 
struct  tinyobj_material
 
struct  tinyobj_shape
 

Enumerations

enum  GROUP_METHOD { ONLY_FILE = 0 , BY_GROUP = 1 , BY_MATERIAL = 2 , MATERIAL_AND_FILE = 3 }
 Method of grouping submeshes in OBJ files. More...
 

Functions

template<typename T >
void IndexRawVertices (const vector< array< T, 3 > > &vertices, vector< int > &mapped_indexes, vector< T > &mapped_vertices)
 Index an array of vertices. More...
 
template<typename T >
arrayDist (const array< T, 3 > from, const array< T, 3 > &to)
 Calculate the distance between from and to. More...
 
template<typename T >
 MeshInfo () -> MeshInfo< float >
 
tinyobj::ObjReader CreateReader (const std::string &path)
 
template<typename T >
tinyobj_shape< T > MakeShape (const tinyobj::shape_t &shape)
 
template<typename T >
vector< tinyobj_shape< T > > MakeShapes (const vector< tinyobj::shape_t > &shapes)
 
vector< tinyobj_materialMakeMaterials (const vector< tinyobj::material_t > &materials)
 
tinyobj_geometry< doubleLoadMeshesFromTinyOBJ (std::string path)
 
vector< MeshInfo< float > > LoadMeshObjects (std::string path, GROUP_METHOD gm=ONLY_FILE, bool change_coords=false, int scale=1)
 Create MeshInfo instances from the OBJ at path. More...
 
vector< array< float, 3 > > LoadRawVertices (std::string path)
 Load a list of vertices directly from an OBJ file. More...
 
std::string GetTestOBJPath (std::string key)
 Get the path to the OBJ with the given key. More...
 
vector< MeshInfo< float > > LoadMeshObjects (std::vector< std::string > &path, GROUP_METHOD gm=ONLY_FILE, bool change_coords=false, int scale=1)
 Create MeshInfo instances from the OBJ files in path. More...
 
template<typename T >
HF::Geometry::MeshInfo< T > LoadTMPMeshObjects (const std::string &path)
 

Variables

static robin_hood::unordered_map< string, string > test_model_paths
 

Detailed Description

Manipulate and load geometry from disk.

HF::Geometry contains all functionality in DHARTAPI for dealing with mesh Geometry. The fundamental datatype of HF::Geometry is MeshInfo, which stores the vertices and indices of meshes as matricies in Eigen.

Remarks
Right now only OBJ files are supported for loading geometry from disk, however MeshInfo offers multiple constructors that accept arrays of vertices and indices so support for more formats can be developed.
See also
LoadMeshObjects to load OBJ files from disk.
MeshInfo for details on how DHARTAPI represents Mesh geometry.

Class Documentation

◆ HF::Geometry::tinyobj_attr

struct HF::Geometry::tinyobj_attr
template<typename T>
struct HF::Geometry::tinyobj_attr< T >

Definition at line 72 of file objloader.h.

+ Collaboration diagram for HF::Geometry::tinyobj_attr< T >:
Class Members
vector< T > vertices

◆ HF::Geometry::tinyobj_geometry

struct HF::Geometry::tinyobj_geometry
template<typename T>
struct HF::Geometry::tinyobj_geometry< T >

Definition at line 89 of file objloader.h.

+ Collaboration diagram for HF::Geometry::tinyobj_geometry< T >:
Class Members
tinyobj_attr< T > attributes
vector< tinyobj_material > materials
vector< tinyobj_shape< T > > shapes

◆ HF::Geometry::tinyobj_material

struct HF::Geometry::tinyobj_material

Definition at line 84 of file objloader.h.

+ Collaboration diagram for HF::Geometry::tinyobj_material:
Class Members
string name

◆ HF::Geometry::tinyobj_shape

struct HF::Geometry::tinyobj_shape
template<typename T>
struct HF::Geometry::tinyobj_shape< T >

Definition at line 78 of file objloader.h.

+ Collaboration diagram for HF::Geometry::tinyobj_shape< T >:
Class Members
vector< int > indices
vector< int > mat_ids
string name

Enumeration Type Documentation

◆ GROUP_METHOD

Method of grouping submeshes in OBJ files.

See also
LoadMeshObjects to use this enum.
Enumerator
ONLY_FILE 

Treat all the geometry in the file as a single mesh.

BY_GROUP 

Create a new MeshInfo instance for every OBJ group in the file.

BY_MATERIAL 

Create a new MeshInfo instance for every different material in the file.

MATERIAL_AND_FILE 

UNIMPLEMENTED.

Definition at line 64 of file objloader.h.

Function Documentation

◆ arrayDist()

template<typename T >
T HF::Geometry::arrayDist ( const array< T, 3 >  from,
const array< T, 3 > &  to 
)

Calculate the distance between from and to.

Parameters
fromx,y,z position to calculate distance from.
tox,y,z position to calculate distance to.
Returns
The distance between from and to.

Definition at line 306 of file meshinfo.cpp.

Referenced by HF::Geometry::MeshInfo< numeric_type >::operator==().

+ Here is the caller graph for this function:

◆ CreateReader()

tinyobj::ObjReader HF::Geometry::CreateReader ( const std::string &  path)

Definition at line 120 of file objloader.cpp.

Referenced by LoadMeshesFromTinyOBJ().

+ Here is the caller graph for this function:

◆ GetTestOBJPath()

std::string HF::Geometry::GetTestOBJPath ( std::string  key)

Get the path to the OBJ with the given key.

Parameters
keyThe name of the obj to get the path of
Returns
The path to the obj in key.
Exceptions
std::out_of_rangeif the given key couldn't be found

Definition at line 407 of file objloader.cpp.

References test_model_paths.

◆ IndexRawVertices()

template<typename T >
void HF::Geometry::IndexRawVertices ( const vector< array< T, 3 > > &  vertices,
vector< int > &  mapped_indexes,
vector< T > &  mapped_vertices 
)

Index an array of vertices.

Parameters
verticesAn array of vertices for a mesh organized so every 3 vertices represents a triangle on the mesh.
mapped_indexesOutput parameter for array of indicies
mapped_verticesOutput parameter for vertex array.

Iterate through every array in vertices. If the x,y,z location in the array hasn't been seen yet, add its coordinates to mapped_vertices, assign it a unique ID in a an index hashmap, then add its id to mapped_indexes. IF it has been seen add the existing ID in the index hashmap to mapped_indexes.

Precondition
mapped_indexes and mapped_vertices are empty vectors of integers and Ts respectively.
Postcondition
1) mapped_indexes contains an array of indexes for mapped_vertices where every 3 indices represents a different triangle on the mesh.
2) mapped_vertices contains an array of Ts with each 3 vertices represent the x,y, and z coordinates for a different vertex in the mesh with no repeats.

Definition at line 73 of file meshinfo.cpp.

Referenced by HF::Geometry::MeshInfo< numeric_type >::VectorsToBuffers().

+ Here is the caller graph for this function:

◆ LoadMeshesFromTinyOBJ()

tinyobj_geometry< double > HF::Geometry::LoadMeshesFromTinyOBJ ( std::string  path)

Definition at line 177 of file objloader.cpp.

References HF::Geometry::tinyobj_geometry< T >::attributes, CreateReader(), MakeMaterials(), HF::Geometry::tinyobj_geometry< T >::materials, and HF::Geometry::tinyobj_geometry< T >::shapes.

Referenced by LoadTMPMeshObjects().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ LoadMeshObjects() [1/2]

std::vector< MeshInfo< float > > HF::Geometry::LoadMeshObjects ( std::string  path,
GROUP_METHOD  gm = ONLY_FILE,
bool  change_coords = false,
int  scale = 1 
)

Create MeshInfo instances from the OBJ at path.

Parameters
pathPath to the OBJ to load.
gmMethod for dividing the mesh into subobjects.
change_coordsRotate the mesh from Y-up to Z-up.
Returns
A vector of meshinfo from the file at path.
Parameters
scaleScaling factor to use for the imported mesh
Exceptions
HF::Exceptions::InvalidOBJThe file at path was not a valid OBJ file.
HF::Exceptions::FileNotFoundNo file could be found at path.
std::exceptiongm did not match any valid GROUP_METHOD.
Todo:

the name of the mesh should probably just be the filename if the group type is ONLY_FILE.

Definitely some areas for minor speed gains by using indexing instead of .push_bcaks

Change std::exception to std::out_of_range.

Refactor this to break out whatever code can be repeated or reused. This has a McCabe complexity of 25!!!!

See also
GROUP_METHOD for more information about every different group method.
// be sure to #include "objloader.h", and #include <vector>
// relative path begins where EXE file is located if file_path is not a path to a
// valid OBJ file, HF::Exceptions::FileNotFound is thrown
std::string file_path = "big_teapot.obj";
// meshvec is a vector of meshinfo from file_path
std::vector<HF::Geometry::MeshInfo> meshvec = HF::Geometry::LoadMeshObjects(file_path,
// retrieve the MeshInfo
HF::Geometry::MeshInfo info = meshvec[0];
vector< MeshInfo< float > > LoadMeshObjects(std::string path, GROUP_METHOD gm, bool change_coords, int scale)
Create MeshInfo instances from the OBJ at path.
Definition: objloader.cpp:195
@ ONLY_FILE
Treat all the geometry in the file as a single mesh.
Definition: objloader.h:65
A collection of vertices and indices representing geometry.
Definition: meshinfo.h:124

Definition at line 195 of file objloader.cpp.

References BY_GROUP, BY_MATERIAL, LoadMeshObjects(), and ONLY_FILE.

Referenced by LoadMeshObjects(), and LoadOBJ().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ LoadMeshObjects() [2/2]

std::vector< MeshInfo< float > > HF::Geometry::LoadMeshObjects ( std::vector< std::string > &  path,
GROUP_METHOD  gm = ONLY_FILE,
bool  change_coords = false,
int  scale = 1 
)

Create MeshInfo instances from the OBJ files in path.

Parameters
pathPath to the OBJ to load.
gmMethod for dividing the mesh into subobjects.
change_coordsRotate the mesh from Y-up to Z-up.
Returns
A vector of meshinfo loaded from the files at path.
Parameters
scaleScaling factor to use for the imported mesh
Exceptions
HF::Exceptions::InvalidOBJOne or more of the files in paths was not a valid OBJ file.
HF::Exceptions::FileNotFoundCould not find one or more of the files at path.
std::exceptiongm did not match any valid GROUP_METHOD.
// be sure to #include "objloader.h", and #include <vector>
// Prepare the file paths
const auto PATH_0 = "big_teapot.obj";
const auto PATH_1 = "plane.obj";
const auto PATH_2 = "sibenik.obj";
// PATH_0, PATH_1, and PATH_2 each represent a path to an OBJ file. Note that if any
// path in pathvec is invalid, HF::Exceptions::FileNotFound will be thrown when
// LoadMeshObjects is called
std::vector<std::string> pathvec = { PATH_0, PATH_1, PATH_2 };
// The overload for LoadMeshObjects is called for each member (which is an OBJ file
// path) in pathvec, then IDs are reassigned for each MeshInfo object within the
// std::vector<MeshInfo> that is returned
std::vector<HF::Geometry::MeshInfo> meshvec = HF::Geometry::LoadMeshObjects(pathvec, HF::Geometry::GROUP_METHOD::ONLY_FILE, false);
std::cout << "Total loaded: " << meshvec.size() << std::endl;
// Print IDs of all mesh info
for (auto mesh_info : meshvec) {
std::cout << "Mesh ID: " << mesh_info.GetMeshID() << std::endl;
}

Definition at line 412 of file objloader.cpp.

References LoadMeshObjects().

+ Here is the call graph for this function:

◆ LoadRawVertices()

std::vector< std::array< float, 3 > > HF::Geometry::LoadRawVertices ( std::string  path)

Load a list of vertices directly from an OBJ file.

Parameters
pathLocation of the OBJ to load on disk.
Returns
A vector of x,y,z positions for each vertex of each face of the OBJ. Every 3 vertices form a single triangle. on the mesh.
Remarks
This is an alternative to the function that returns an instance of MeshInfo. Mostly for testing, since it uses a vertex array instead of an index array, making the end product take up more space in memory.
Exceptions
HF::Exceptions::InvalidOBJpath did not lead to a valid OBJ file.
Todo:
Check if the file exists then thow file not found exception instead of invalidobj.
// be sure to #include "objloader.h", and #include <vector>
// Note that filepath must lead to a valid OBJ file, or HF::Exceptions::InvalidOBJ will
// be thrown when Geometry::LoadMeshObjects is called
std::string filepath = "plane.obj";
// Vertices now contains the raw vertices loaded from the OBJ file specified at filepath
std::vector<std::array<float, 3>> vertices = HF::Geometry::LoadRawVertices(filepath);
// Display the vertices from filepath
std::cout << "Vertices from " << filepath << ": " << std::endl;
for (auto vertex : vertices) {
std::cout << "(" << vertex[0] << ", " << vertex[1] << ", " << vertex[2] << ")" << std::endl;
}
vector< array< float, 3 > > LoadRawVertices(std::string path)
Load a list of vertices directly from an OBJ file.
Definition: objloader.cpp:371

Definition at line 371 of file objloader.cpp.

◆ LoadTMPMeshObjects()

template<typename T >
HF::Geometry::MeshInfo< T > HF::Geometry::LoadTMPMeshObjects ( const std::string &  path)

Definition at line 98 of file objloader.h.

References HF::Geometry::tinyobj_geometry< T >::attributes, LoadMeshesFromTinyOBJ(), and HF::Geometry::tinyobj_geometry< T >::shapes.

+ Here is the call graph for this function:

◆ MakeMaterials()

vector< tinyobj_material > HF::Geometry::MakeMaterials ( const vector< tinyobj::material_t > &  materials)

Definition at line 168 of file objloader.cpp.

Referenced by LoadMeshesFromTinyOBJ().

+ Here is the caller graph for this function:

◆ MakeShape()

template<typename T >
tinyobj_shape< T > HF::Geometry::MakeShape ( const tinyobj::shape_t &  shape)

◆ MakeShapes()

template<typename T >
vector< tinyobj_shape< T > > HF::Geometry::MakeShapes ( const vector< tinyobj::shape_t > &  shapes)

Definition at line 159 of file objloader.cpp.

◆ MeshInfo()

template<typename T >
HF::Geometry::MeshInfo ( ) -> MeshInfo< float >

Variable Documentation

◆ test_model_paths

robin_hood::unordered_map<string, string> HF::Geometry::test_model_paths
static
Initial value:
{
{"teapot", "teapot.obj"},
{"plane", "plane.obj" },
{"big teapot", "big_teapot.obj" },
{"energy blob", "energy_blob.obj" },
{"sibenik", "sibenik.obj" },
}

Definition at line 112 of file objloader.cpp.

Referenced by GetTestOBJPath().