20 #ifndef TEXTURE_PROCESSING_H_
21 #define TEXTURE_PROCESSING_H_
27 #include <Eigen/Geometry>
31 #include <mve/mesh_io_ply.h>
32 #include <mve/mesh_info.h>
34 #include <mve/image.h>
35 #include <acc/bvh_tree.h>
36 #include <tex/timer.h>
37 #include <tex/texturing.h>
39 #include <tex/texture_patch.h>
40 #include <tex/rectangular_bin.h>
41 #include <tex/material_lib.h>
42 #include <util/exception.h>
43 #include <math/vector.h>
46 #include <opencv2/highgui/highgui.hpp>
49 #include <camera/camera_model.h>
50 #include <dense_map_utils.h>
57 typedef acc::BVHTree<unsigned int, math::Vec3f> BVHTree;
74 double x, min_y, min_z;
78 int64_t width, height;
81 int64_t face_info_padding;
85 int64_t shift_u, shift_v;
91 Eigen::Affine3d YZPlaneToTriangleFace;
94 std::vector<Eigen::Vector3d> TransformedVertices;
104 face_info_padding = 1L;
106 shift_u = std::numeric_limits<int>::max();
108 YZPlaneToTriangleFace = Eigen::Affine3d::Identity();
109 TransformedVertices.resize(3);
114 Eigen::Vector3d vec3f_to_eigen(math::Vec3f
const& v);
115 math::Vec3f eigen_to_vec3f(Eigen::Vector3d
const& V);
120 typedef std::shared_ptr<IsaacTexturePatch> Ptr;
121 typedef std::shared_ptr<const IsaacTexturePatch> ConstPtr;
122 typedef std::vector<std::size_t> Faces;
123 typedef std::vector<Eigen::Vector2d> Texcoords;
129 int64_t width, height;
134 std::vector<Eigen::Vector2d>
const& texcoords,
135 int64_t width, int64_t height);
139 static IsaacTexturePatch::Ptr create(IsaacTexturePatch::ConstPtr texture_patch);
140 static IsaacTexturePatch::Ptr create(int64_t label, std::vector<std::size_t>
const& faces,
141 std::vector<Eigen::Vector2d>
const& texcoords,
142 int64_t width, int64_t height);
144 IsaacTexturePatch::Ptr duplicate(
void);
146 std::vector<std::size_t>& get_faces(
void);
147 std::vector<std::size_t>
const& get_faces(
void)
const;
148 std::vector<Eigen::Vector2d> & get_texcoords(
void);
149 std::vector<Eigen::Vector2d>
const& get_texcoords(
void)
const;
151 int64_t get_label(
void)
const;
152 int64_t get_width(
void)
const;
153 int64_t get_height(
void)
const;
154 int64_t get_size(
void)
const;
158 std::vector<Eigen::Vector2d>
const& texcoords,
159 int64_t width, int64_t height)
160 : label(label), faces(faces), texcoords(texcoords), width(width), height(height) {}
163 label = texture_patch.label;
164 faces = std::vector<std::size_t>(texture_patch.faces);
165 texcoords = std::vector<Eigen::Vector2d>(texture_patch.texcoords);
166 width = texture_patch.width;
167 height = texture_patch.height;
170 inline IsaacTexturePatch::Ptr IsaacTexturePatch::create(IsaacTexturePatch::ConstPtr texture_patch) {
171 return std::make_shared<IsaacTexturePatch>(*texture_patch);
174 inline IsaacTexturePatch::Ptr IsaacTexturePatch::create(
175 int64_t label, std::vector<std::size_t>
const& faces,
176 std::vector<Eigen::Vector2d>
const& texcoords, int64_t width, int64_t height) {
177 return std::make_shared<IsaacTexturePatch>(label, faces, texcoords, width, height);
180 inline IsaacTexturePatch::Ptr IsaacTexturePatch::duplicate(
void) {
184 inline int64_t IsaacTexturePatch::get_label(
void)
const {
return label; }
186 inline int64_t IsaacTexturePatch::get_width(
void)
const {
return width; }
188 inline int64_t IsaacTexturePatch::get_height(
void)
const {
return height; }
190 inline std::vector<Eigen::Vector2d>& IsaacTexturePatch::get_texcoords(
void) {
194 inline std::vector<std::size_t>& IsaacTexturePatch::get_faces(
void) {
return faces; }
196 inline std::vector<Eigen::Vector2d>
const& IsaacTexturePatch::get_texcoords(
void)
const {
200 inline std::vector<std::size_t>
const& IsaacTexturePatch::get_faces(
void)
const {
return faces; }
202 inline int64_t IsaacTexturePatch::get_size(
void)
const {
return get_width() * get_height(); }
210 typedef std::shared_ptr<IsaacTextureAtlas> Ptr;
212 typedef std::vector<std::size_t> Faces;
213 typedef std::vector<std::size_t> TexcoordIds;
214 typedef std::vector<Eigen::Vector2d> Texcoords;
217 int64_t get_height();
220 int64_t width, height, determined_height;
225 TexcoordIds texcoord_ids;
227 mve::ByteImage::Ptr image;
229 RectangularBin::Ptr bin;
231 void resize_atlas(
void);
236 static IsaacTextureAtlas::Ptr create(int64_t width, int64_t height);
238 Faces& get_faces(
void);
239 TexcoordIds& get_texcoord_ids(
void);
240 Texcoords& get_texcoords(
void);
241 mve::ByteImage::Ptr& get_image(
void);
243 bool insert(IsaacTexturePatch::ConstPtr texture_patch);
245 void scale_texcoords(
void);
247 void merge_texcoords(
void);
252 inline IsaacTextureAtlas::Ptr IsaacTextureAtlas::create(int64_t width, int64_t height) {
256 inline IsaacTextureAtlas::Faces& IsaacTextureAtlas::get_faces(
void) {
return faces; }
258 inline IsaacTextureAtlas::TexcoordIds& IsaacTextureAtlas::get_texcoord_ids(
void) {
262 inline IsaacTextureAtlas::Texcoords& IsaacTextureAtlas::get_texcoords(
void) {
return texcoords; }
264 inline mve::ByteImage::Ptr& IsaacTextureAtlas::get_image(
void) {
return image; }
266 inline int64_t IsaacTextureAtlas::get_width() {
return width; }
268 inline int64_t IsaacTextureAtlas::get_height() {
return height; }
278 std::size_t vertex_ids[3];
279 std::size_t texcoord_ids[3];
280 std::size_t normal_ids[3];
284 std::string material_name;
285 std::vector<Face> faces;
288 typedef std::vector<math::Vec3f> Vertices;
289 typedef std::vector<Eigen::Vector2d> TexCoords;
290 typedef std::vector<math::Vec3f> Normals;
291 typedef std::vector<Group> Groups;
298 MaterialLib material_lib;
305 MaterialLib & get_material_lib(
void);
306 Vertices & get_vertices(
void);
307 TexCoords & get_texcoords(
void);
308 Normals & get_normals(
void);
309 Groups & get_groups(
void);
311 static void save(
IsaacObjModel const & model, std::string
const & prefix);
316 IsaacObjModel::get_material_lib(
void) {
321 IsaacObjModel::Vertices &
322 IsaacObjModel::get_vertices(
void) {
327 IsaacObjModel::TexCoords &
328 IsaacObjModel::get_texcoords(
void) {
333 IsaacObjModel::Normals &
334 IsaacObjModel::get_normals(
void) {
339 IsaacObjModel::Groups &
340 IsaacObjModel::get_groups(
void) {
345 void loadMeshBuildTree(std::string
const& mesh_file, mve::TriangleMesh::Ptr& mesh,
346 std::shared_ptr<mve::MeshInfo>& mesh_info,
347 std::shared_ptr<tex::Graph>& graph,
348 std::shared_ptr<BVHTree>& bvh_tree);
350 void formModel(mve::TriangleMesh::ConstPtr mesh,
double pixel_size, int64_t num_threads,
352 std::vector<FaceInfo>& face_projection_info,
353 std::vector<IsaacTextureAtlas::Ptr>& texture_atlases,
354 IsaacObjModel& model);
357 void formObj(IsaacObjModel& texture_model, std::string
const& out_prefix, std::string& obj_str);
360 void formObjCustomUV(mve::TriangleMesh::ConstPtr mesh, std::vector<Eigen::Vector3i>
const& face_vec,
361 std::map<int, Eigen::Vector2d>
const& uv_map,
362 std::string
const& out_prefix, std::string& obj_str);
364 void formMtl(std::string
const& out_prefix, std::string& mtl_str);
368 void adjustImageSize(camera::CameraParameters
const& cam_params, cv::Mat & image);
371 void projectTexture(mve::TriangleMesh::ConstPtr mesh, std::shared_ptr<BVHTree> bvh_tree,
372 cv::Mat
const& image, camera::CameraModel
const& cam,
373 double num_exclude_boundary_pixels,
375 std::vector<double>& smallest_cost_per_face,
376 std::vector<Eigen::Vector3i>& face_vec,
377 std::map<int, Eigen::Vector2d>& uv_map);
381 void projectTexture(mve::TriangleMesh::ConstPtr mesh, std::shared_ptr<BVHTree> bvh_tree,
382 cv::Mat
const& image, camera::CameraModel
const& cam,
383 std::vector<double>& smallest_cost_per_face,
double pixel_size,
384 int64_t num_threads, std::vector<FaceInfo>
const& face_projection_info,
385 std::vector<IsaacTextureAtlas::Ptr>& texture_atlases,
386 IsaacObjModel& model, cv::Mat& out_texture);
390 bool ray_mesh_intersect(Eigen::Vector2d
const& dist_pix,
391 camera::CameraParameters
const& cam_params,
392 Eigen::Affine3d
const& world_to_cam,
393 mve::TriangleMesh::Ptr
const& mesh,
394 std::shared_ptr<BVHTree>
const& bvh_tree,
395 double min_ray_dist,
double max_ray_dist,
397 Eigen::Vector3d& intersection);
399 void meshProject(mve::TriangleMesh::Ptr
const& mesh, std::shared_ptr<BVHTree>
const& bvh_tree,
400 cv::Mat
const& image,
401 Eigen::Affine3d
const& world_to_cam, camera::CameraParameters
const& cam_params,
402 int64_t num_exclude_boundary_pixels, std::string
const& out_prefix);
405 void isaac_save_model(IsaacObjModel* obj_model, std::string
const& prefix);
409 #endif // TEXTURE_PROCESSING_H_