DHART
Loading...
Searching...
No Matches
objloader_C.cpp
Go to the documentation of this file.
1#include <objloader_C.h>
2#include <objloader.h>
3#include <meshinfo.h>
4#include <vector>
5
6#include <HFExceptions.h>
7#include <iostream>
8#include <cinterface_utils.h>
9using namespace HF::Geometry;
10using namespace HF::Exceptions;
11using std::vector;
12
14 const char* obj_path,
16 float xrot,
17 float yrot,
18 float zrot,
19 MeshInfo<float> *** out_data_array,
20 int* num_meshes
21) {
22 // Copy marshalled char array into strings
23 std::string filepath(obj_path);
24
25 // Create a new array of meshinfo pointers
26 try {
27 // Try to load the mesh
28 auto loaded_objs = HF::Geometry::LoadMeshObjects(filepath, gm, false);
29
30 // Rotate the meshes if necessary
31 *num_meshes = loaded_objs.size();
32 for (auto& mesh : loaded_objs)
33 mesh.PerformRotation(xrot, yrot, zrot);
34
35 // Insert into output array (this should be an instance of copy elision.
36 MeshInfo<float> ** data_array = new MeshInfo<float>*[*num_meshes];
37 for (int i = 0; i < *num_meshes; i++) {
38 data_array[i] = new MeshInfo<float>(loaded_objs[i]);
39 assert(data_array[i]->GetMeshID() == loaded_objs[i].GetMeshID());
40 }
41 *out_data_array = data_array;
42
43 return HF_STATUS::OK;
44 }
45 catch (const HF::Exceptions::InvalidOBJ & e) {
46 return HF_STATUS::INVALID_OBJ;
47 }
48 catch (const HF::Exceptions::FileNotFound & e) {
49 return HF_STATUS::NOT_FOUND;
50 }
51 catch (...) {
52 std::cerr << "Generic Error" << std::endl;
53 return HF_STATUS::GENERIC_ERROR;
54 }
55
56 return HF_STATUS::OK;
57}
58
60 MeshInfo<float> ** out_info,
61 const int* indices,
62 int num_indices,
63 const float* vertices,
64 int num_vertices,
65 const char* name,
66 int id
67) {
68 // Map vertex/index arrays and name
69 vector<float> vertex_array(vertices, vertices + num_vertices);
70 vector<int> index_array(indices, indices + num_indices);
71 std::string mesh_name(name);
72
73 // Try to load mesh
74 try {
75 *out_info = new MeshInfo<float>(vertex_array, index_array, id, mesh_name);
76 }
78 // If the input wasn't correctly formed, or contained nans, then return an error code
80 }
81 return OK;
82}
83
84C_INTERFACE RotateMesh(MeshInfo<float> * mesh_to_rotate, float xrot, float yrot, float zrot)
85{
86 if (mesh_to_rotate->meshid < 0)
87 return HF_STATUS::INVALID_OBJ;
88
89 mesh_to_rotate->PerformRotation(xrot, yrot, zrot);
90
91 return HF_STATUS::OK;
92}
93
94C_INTERFACE GetVertsAndTris(const MeshInfo<float> * MI, int** index_out, int* num_triangles, float** vertex_out, int* num_vertices)
95{
96 auto mesh_vertices = MI->GetVertexPointer();
97 auto mesh_indices = MI->GetIndexPointer();
98
99 *vertex_out = mesh_vertices.data;
100 *num_vertices = mesh_vertices.size / 3;
101
102 *index_out = mesh_indices.data;
103 *num_triangles = mesh_indices.size / 3;
104
105 return HF_STATUS::OK;
106}
107
109{
110 auto name = MI->name;
111
112 int num_characters = name.size()+1; // Need extra space for null terminator
113 *out_name = new char[num_characters];
114 std::copy(name.c_str(), name.c_str() + num_characters, *out_name);
115
116 return HF_STATUS::OK;
117}
118
120 *out_id = MI->meshid;
121 return HF_STATUS::OK;
122}
123
124
125
127{
128 DeleteRawPtr(mesh_to_destroy);
129 return OK;
130}
131
133{
134 if (*data_array)
135 delete[] data_array;
136 return OK;
137}
Contains definitions for the Exceptions namespace.
Contains definitions for the MeshInfo class.
Contains definitions for the Geometry namespace.
#define C_INTERFACE
Definition: analysis_C.h:16
void DeleteRawPtr(T *ptr)
Delete some object pointed to by ptr
Header file for functions related to loading/storing an OBJ file (a mesh)
C_INTERFACE LoadOBJ(const char *obj_path, HF::Geometry::GROUP_METHOD gm, float xrot, float yrot, float zrot, MeshInfo< float > ***out_data_array, int *num_meshes)
Load an obj from the given path then rotate it by x,y, and z.
Definition: objloader_C.cpp:13
C_INTERFACE DestroyMeshInfo(MeshInfo< float > *mesh_to_destroy)
Free the memory addressed by mesh_to_destroy, which was allocated by either LoadOBJ or StoreMesh.
C_INTERFACE GetMeshName(const HF::Geometry::MeshInfo< float > *MI, char **out_name)
Get the name of a mesh.
C_INTERFACE StoreMesh(MeshInfo< float > **out_info, const int *indices, int num_indices, const float *vertices, int num_vertices, const char *name, int id)
Store a mesh in a format usable with DHARTAPI.
Definition: objloader_C.cpp:59
C_INTERFACE GetVertsAndTris(const MeshInfo< float > *MI, int **index_out, int *num_triangles, float **vertex_out, int *num_vertices)
Get a pointer to and the size of a mesh's triangle and vertex arrays.
Definition: objloader_C.cpp:94
C_INTERFACE RotateMesh(MeshInfo< float > *mesh_to_rotate, float xrot, float yrot, float zrot)
Rotate an existing mesh (HF::Geometry::MeshInfo)
Definition: objloader_C.cpp:84
C_INTERFACE DestroyMeshInfoPtrArray(MeshInfo< float > **data_array)
C_INTERFACE GetMeshID(const HF::Geometry::MeshInfo< float > *MI, int *out_id)
Get the ID of a mesh.
Custom exceptions and error codes used interally by DHARTAPI.
Definition: HFExceptions.h:22
@ INVALID_OBJ
The given path did not point to a valid obj file.
Definition: HFExceptions.h:40
@ OK
Operation was successful.
Definition: HFExceptions.h:32
Manipulate and load geometry from disk.
Definition: meshinfo.cpp:21
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
GROUP_METHOD
Method of grouping submeshes in OBJ files.
Definition: objloader.h:64
Thrown when desired file is not found.
Definition: HFExceptions.h:56
The OBJ file was not valid.
Definition: HFExceptions.h:65
A collection of vertices and indices representing geometry.
Definition: meshinfo.h:124
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
int meshid
Identifier for this mesh.
Definition: meshinfo.h:126
std::string name
A human-readable title.
Definition: meshinfo.h:127
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