Skip to content

Commit d16719c

Browse files
authored
Improved mesh parsing to include normals, vertex color, material and texture (#490)
1 parent dc48a1a commit d16719c

File tree

22 files changed

+864
-160
lines changed

22 files changed

+864
-160
lines changed

tesseract/tesseract_common/include/tesseract_common/resource.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ class Resource
9696
* @return A std::istream shared pointer for the resource data
9797
*/
9898
virtual std::shared_ptr<std::istream> getResourceContentStream() = 0;
99+
100+
/**
101+
* @brief Retrive a sub-resource relative to the current resource
102+
*
103+
* Retrive a sub-resource related to the current resource. For instance, retrieve
104+
* an image resource for a mesh. The relative_path should be relative to the
105+
* parent directory containing the current resource.
106+
*
107+
* This function is optional, and will only be available on resource locators
108+
* that support retrieving relative resources.
109+
*
110+
* @param relative_path Path relative to the parent directory of the current resource
111+
* @return Resource::Ptr The located resource, or nullptr if not found
112+
*/
113+
virtual Resource::Ptr locateSubResource(const std::string& relative_path) { return nullptr; }
99114
};
100115

101116
class BytesResource : public tesseract_common::Resource

tesseract/tesseract_common/include/tesseract_common/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ using AlignedUnorderedMap = std::unordered_map<Key,
5454
using VectorIsometry3d = AlignedVector<Eigen::Isometry3d>;
5555
using VectorVector4d = AlignedVector<Eigen::Vector4d>;
5656
using VectorVector3d = std::vector<Eigen::Vector3d>;
57+
using VectorVector2d = AlignedVector<Eigen::Vector2d>;
5758
using TransformMap = AlignedMap<std::string, Eigen::Isometry3d>;
5859

5960
using TrajArray = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;

tesseract/tesseract_geometry/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ if(NOT TARGET octomath)
4141
set_target_properties(octomath PROPERTIES INTERFACE_LINK_LIBRARIES "${OCTOMAP_LIBRARIES}")
4242
endif()
4343

44+
find_file(ASSIMP_PBR_MATERIAL_FOUND assimp/pbrmaterial.h HINTS ${assimp_INCLUDE_DIRS})
45+
46+
if(ASSIMP_PBR_MATERIAL_FOUND)
47+
set(TESSERACT_ASSIMP_USE_PBRMATERIAL TESSERACT_ASSIMP_USE_PBRMATERIAL=1)
48+
else()
49+
set(TESSERACT_ASSIMP_USE_PBRMATERIAL)
50+
endif()
51+
4452
initialize_code_coverage()
4553
set(COVERAGE_EXCLUDE /usr/* /opt/* ${CMAKE_CURRENT_LIST_DIR}/test/* /*/gtest/*)
4654
add_code_coverage_all_targets(EXCLUDE ${COVERAGE_EXCLUDE})
@@ -51,7 +59,7 @@ tesseract_variables()
5159
add_library(${PROJECT_NAME} INTERFACE)
5260
target_link_libraries(${PROJECT_NAME} INTERFACE tesseract::tesseract_common console_bridge::console_bridge octomap octomath ${assimp_LIBRARIES})
5361
target_compile_options(${PROJECT_NAME} INTERFACE ${TESSERACT_COMPILE_OPTIONS_PUBLIC})
54-
target_compile_definitions(${PROJECT_NAME} INTERFACE ${TESSERACT_COMPILE_DEFINITIONS})
62+
target_compile_definitions(${PROJECT_NAME} INTERFACE ${TESSERACT_COMPILE_DEFINITIONS} ${TESSERACT_ASSIMP_USE_PBRMATERIAL})
5563
target_clang_tidy(${PROJECT_NAME} ARGUMENTS ${TESSERACT_CLANG_TIDY_ARGS} ENABLE ${TESSERACT_ENABLE_CLANG_TIDY})
5664
target_cxx_version(${PROJECT_NAME} INTERFACE VERSION ${TESSERACT_CXX_VERSION})
5765
target_code_coverage(${PROJECT_NAME} INTERFACE ALL EXCLUDE ${COVERAGE_EXCLUDE} ENABLE ${TESSERACT_ENABLE_CODE_COVERAGE})

tesseract/tesseract_geometry/include/tesseract_geometry/geometries.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
%include <tesseract_geometry/impl/capsule.h>
6060
%include <tesseract_geometry/impl/cone.h>
6161
%include <tesseract_geometry/impl/plane.h>
62+
%include <tesseract_geometry/impl/mesh_material.h>
6263
%include <tesseract_geometry/impl/mesh.h>
6364
%include <tesseract_geometry/impl/convex_mesh.h>
6465
%include <tesseract_geometry/impl/octree.h>

tesseract/tesseract_geometry/include/tesseract_geometry/impl/convex_mesh.h

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
3535
#include <tesseract_geometry/geometry.h>
3636
#include <tesseract_common/types.h>
3737
#include <tesseract_common/resource.h>
38+
#include <tesseract_geometry/impl/mesh_material.h>
3839

3940
#ifdef SWIG
4041
%shared_ptr(tesseract_geometry::ConvexMesh)
@@ -43,10 +44,6 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
4344

4445
namespace tesseract_geometry
4546
{
46-
#ifdef SWIG
47-
%nodefaultctor ConvexMesh;
48-
#endif // SWIG
49-
5047
class ConvexMesh : public Geometry
5148
{
5249
public:
@@ -55,7 +52,6 @@ class ConvexMesh : public Geometry
5552
using Ptr = std::shared_ptr<ConvexMesh>;
5653
using ConstPtr = std::shared_ptr<const ConvexMesh>;
5754

58-
#ifndef SWIG
5955
/**
6056
* @brief Convex Mesh geometry
6157
* @param vertices A vector of vertices associated with the mesh
@@ -65,16 +61,28 @@ class ConvexMesh : public Geometry
6561
* three vertices that define this face followed by three indices.
6662
* @param resource A resource locator for locating resource
6763
* @param scale Scale the mesh
64+
* @param normals A vector of normals for the vertices (optional)
65+
* @param vertex_colors A vector of colors (RGBA) for the vertices (optional)
66+
* @param mesh_material A MeshMaterial describing the color and material properties of the mesh (optional)
67+
* @param mesh_textures A vector of MeshTexture to apply to the mesh (optional)
6868
*/
6969
ConvexMesh(std::shared_ptr<const tesseract_common::VectorVector3d> vertices,
7070
std::shared_ptr<const Eigen::VectorXi> faces,
7171
tesseract_common::Resource::Ptr resource = nullptr,
72-
Eigen::Vector3d scale = Eigen::Vector3d(1, 1, 1))
72+
Eigen::Vector3d scale = Eigen::Vector3d(1, 1, 1),
73+
std::shared_ptr<const tesseract_common::VectorVector3d> normals = nullptr,
74+
std::shared_ptr<const tesseract_common::VectorVector4d> vertex_colors = nullptr,
75+
MeshMaterial::Ptr mesh_material = nullptr,
76+
std::shared_ptr<const std::vector<MeshTexture::Ptr>> mesh_textures = nullptr)
7377
: Geometry(GeometryType::CONVEX_MESH)
7478
, vertices_(std::move(vertices))
7579
, faces_(std::move(faces))
7680
, resource_(std::move(resource))
7781
, scale_(std::move(scale))
82+
, normals_(std::move(normals))
83+
, vertex_colors_(std::move(vertex_colors))
84+
, mesh_material_(std::move(mesh_material))
85+
, mesh_textures_(std::move(mesh_textures))
7886
{
7987
vertice_count_ = static_cast<int>(vertices_->size());
8088

@@ -97,46 +105,40 @@ class ConvexMesh : public Geometry
97105
* @param face_count Provide the number of faces. This is faster because it does not need to loop over triangles.
98106
* @param resource A resource locator for locating resource
99107
* @param scale Scale the mesh
108+
* @param normals A vector of normals for the vertices (optional)
109+
* @param vertex_colors A vector of colors (RGBA) for the vertices (optional)
110+
* @param mesh_material A MeshMaterial describing the color and material properties of the mesh (optional)
111+
* @param mesh_textures A vector of MeshTexture to apply to the mesh (optional)
100112
*/
101113
ConvexMesh(std::shared_ptr<const tesseract_common::VectorVector3d> vertices,
102114
std::shared_ptr<const Eigen::VectorXi> faces,
103115
int face_count,
104116
tesseract_common::Resource::Ptr resource = nullptr,
105-
Eigen::Vector3d scale = Eigen::Vector3d(1, 1, 1))
117+
Eigen::Vector3d scale = Eigen::Vector3d(1, 1, 1),
118+
std::shared_ptr<const tesseract_common::VectorVector3d> normals = nullptr,
119+
std::shared_ptr<const tesseract_common::VectorVector4d> vertex_colors = nullptr,
120+
MeshMaterial::Ptr mesh_material = nullptr,
121+
std::shared_ptr<const std::vector<MeshTexture::Ptr>> mesh_textures = nullptr)
106122
: Geometry(GeometryType::CONVEX_MESH)
107123
, vertices_(std::move(vertices))
108124
, faces_(std::move(faces))
109125
, face_count_(face_count)
110126
, resource_(std::move(resource))
111127
, scale_(std::move(scale))
128+
, normals_(std::move(normals))
129+
, vertex_colors_(std::move(vertex_colors))
130+
, mesh_material_(std::move(mesh_material))
131+
, mesh_textures_(std::move(mesh_textures))
112132
{
113133
vertice_count_ = static_cast<int>(vertices_->size());
114134
}
115135

116-
#endif // SWIG
117-
118-
#ifdef SWIG
119-
%extend
120-
{
121-
ConvexMesh(const tesseract_common::VectorVector3d& vertices,
122-
const Eigen::VectorXi& triangles,
123-
tesseract_common::Resource::Ptr resource = nullptr,
124-
Eigen::Vector3d scale = Eigen::Vector3d(1, 1, 1))
125-
{
126-
return new tesseract_geometry::ConvexMesh(std::make_shared<tesseract_common::VectorVector3d>(vertices),
127-
std::make_shared<Eigen::VectorXi>(triangles),
128-
resource, scale);
129-
}
130-
}
131-
#endif // SWIG
132-
133136
~ConvexMesh() override = default;
134137
ConvexMesh(const ConvexMesh&) = delete;
135138
ConvexMesh& operator=(const ConvexMesh&) = delete;
136139
ConvexMesh(ConvexMesh&&) = delete;
137140
ConvexMesh& operator=(ConvexMesh&&) = delete;
138141

139-
#ifndef SWIG
140142
/**
141143
* @brief Get convex mesh vertices
142144
* @return A vector of vertices
@@ -149,17 +151,6 @@ class ConvexMesh : public Geometry
149151
*/
150152
const std::shared_ptr<const Eigen::VectorXi>& getFaces() const { return faces_; }
151153

152-
#else // SWIG
153-
// clang-format off
154-
%extend
155-
{
156-
tesseract_common::VectorVector3d getVertices() { return *$self->getVertices(); }
157-
158-
Eigen::VectorXi getFaces() { return *$self->getFaces(); }
159-
}
160-
// clang-format on
161-
#endif // SWIG
162-
163154
/**
164155
* @brief Get vertice count
165156
* @return Number of vertices
@@ -187,6 +178,46 @@ class ConvexMesh : public Geometry
187178
*/
188179
const Eigen::Vector3d& getScale() const { return scale_; }
189180

181+
/**
182+
* @brief Get the vertice normal vectors
183+
*
184+
* Optional, may be nullptr
185+
*
186+
* @return The vertice normal vector
187+
*/
188+
std::shared_ptr<const tesseract_common::VectorVector3d> getNormals() const { return normals_; }
189+
190+
/**
191+
* @brief Get the vertex colors
192+
*
193+
* Optional, may be nullptr
194+
*
195+
* @return Vertex colors
196+
*/
197+
std::shared_ptr<const tesseract_common::VectorVector4d> getVertexColors() const { return vertex_colors_; }
198+
199+
/**
200+
* @brief Get material data extracted from the mesh file
201+
*
202+
* Mesh files contain material information. The mesh parser will
203+
* extract the material information and store it in a MeshMaterial structure.
204+
*
205+
* @return The MeshMaterial data extracted from mesh file
206+
*/
207+
MeshMaterial::ConstPtr getMaterial() const { return mesh_material_; }
208+
209+
/**
210+
* @brief Get textures extracted from the mesh file
211+
*
212+
* Mesh files contain (or reference) image files that form textures on the surface
213+
* of the mesh. UV coordinates specify how the image is applied to the mesh. The
214+
* MeshTexture structure contains a resource to the image, and the UV coordinates.
215+
* Currently only jpg and png image formats are supported.
216+
*
217+
* @return Vector of mesh textures
218+
*/
219+
std::shared_ptr<const std::vector<MeshTexture::Ptr>> getTextures() const { return mesh_textures_; }
220+
190221
Geometry::Ptr clone() const override
191222
{
192223
return std::make_shared<ConvexMesh>(vertices_, faces_, face_count_, resource_, scale_);
@@ -200,6 +231,10 @@ class ConvexMesh : public Geometry
200231
int face_count_;
201232
tesseract_common::Resource::Ptr resource_;
202233
Eigen::Vector3d scale_;
234+
std::shared_ptr<const tesseract_common::VectorVector3d> normals_;
235+
std::shared_ptr<const tesseract_common::VectorVector4d> vertex_colors_;
236+
MeshMaterial::Ptr mesh_material_;
237+
std::shared_ptr<const std::vector<MeshTexture::Ptr>> mesh_textures_;
203238
};
204239
} // namespace tesseract_geometry
205240
#endif

0 commit comments

Comments
 (0)