Skip to content

Commit

Permalink
add task7&&8
Browse files Browse the repository at this point in the history
  • Loading branch information
swayfreeda committed Nov 10, 2018
1 parent 4b73d99 commit 5541078
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 51 deletions.
3 changes: 1 addition & 2 deletions core/image_tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ gamma_correct (ByteImage::Ptr image, float power)

uint8_t lookup[256];
for (int i = 0; i < 256; ++i)
lookup[i] = static_cast<uint8_t>(std::pow(i / 255.0f, power)
* 255.0f + 0.5f);
lookup[i] = static_cast<uint8_t>(std::pow(i / 255.0f, power) * 255.0f + 0.5f);
for (int i = 0; i < image->get_value_amount(); ++i)
image->at(i) = lookup[image->at(i)];
}
Expand Down
32 changes: 18 additions & 14 deletions core/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ class TriangleMesh : public MeshBase
typedef std::shared_ptr<TriangleMesh> Ptr;
typedef std::shared_ptr<TriangleMesh const> ConstPtr;

// 法向量
// normals
typedef std::vector<math::Vec3f> NormalList;
// 纹理坐标

// texcoords
typedef std::vector<math::Vec2f> TexCoordList;
// 面片

// vertex indeices for facets
typedef std::vector<VertexID> FaceList;

typedef std::vector<bool> DeleteList;
Expand All @@ -111,26 +113,31 @@ class TriangleMesh : public MeshBase

/** Returns the vertex normals. */
NormalList const& get_vertex_normals (void) const;

/** Returns the vertex normals. */
NormalList& get_vertex_normals (void);

/** Returns the vectex texture coordinates. */
TexCoordList const& get_vertex_texcoords (void) const;

/** Returns the vectex texture coordinates. */
TexCoordList& get_vertex_texcoords (void);

/** Returns the triangle indices. */
FaceList const& get_faces (void) const;

/** Returns the triangle indices. */
FaceList& get_faces (void);

/** Returns the face normals. */
NormalList const& get_face_normals (void) const;

/** Returns the face normals. */
NormalList& get_face_normals (void);

/** Returns the face colors. */
ColorList const& get_face_colors (void) const;

/** Returns the face colors. */
ColorList& get_face_colors (void);

Expand Down Expand Up @@ -176,19 +183,19 @@ class TriangleMesh : public MeshBase

protected:

// 顶点法向量
// vertex normals
NormalList vertex_normals;

// 顶点纹理坐标
// vertex texcoords
TexCoordList vertex_texcoords;

// 面片
// facets
FaceList faces;

// 面片法向量
// facet normals
NormalList face_normals;

// 面片颜色
// facet colors
ColorList face_colors;

protected:
Expand All @@ -199,13 +206,11 @@ class TriangleMesh : public MeshBase
/* ---------------------------------------------------------------- */

inline
MeshBase::MeshBase (void)
{
MeshBase::MeshBase (void) {
}

inline
MeshBase::~MeshBase (void)
{
MeshBase::~MeshBase (void) {
}

inline MeshBase::VertexList const&
Expand All @@ -215,8 +220,7 @@ MeshBase::get_vertices (void) const
}

inline MeshBase::VertexList&
MeshBase::get_vertices (void)
{
MeshBase::get_vertices (void) {
return this->vertices;
}

Expand Down
1 change: 1 addition & 0 deletions core/mesh_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class VertexInfoList : public std::vector<MeshVertexInfo>
public:
/* Creates an uninitialized vertex info list. */
VertexInfoList (void);

/* Creates and initializes a vertex info list. */
VertexInfoList (TriangleMesh::ConstPtr mesh);

Expand Down
142 changes: 135 additions & 7 deletions examples/task7/class7_texrecon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <util/system.h>
#include <util/file_system.h>
#include <core/mesh_io_ply.h>
#include <core/image_tools.h>
#include <core/image_drawing.h>
#include "core/image_io.h"

#include "texturing/util.h"
#include "texturing/timer.h"
Expand Down Expand Up @@ -65,7 +68,6 @@ int main(int argc, char **argv) {
tex::prepare_mesh(vertex_infos, mesh);



//=================================Geneatring texture views=====================//
std::size_t const num_faces = mesh->get_faces().size() / 3;
std::cout << "Generating texture views: " << std::endl;
Expand All @@ -76,16 +78,12 @@ int main(int argc, char **argv) {


//===============================Building adjacency graph=======================//
std::cout << "Building adjacency graph: " << std::endl;
std::cout << "Building adjacency graph: " << std::endl; // each facet is corresponding a facet
tex::Graph graph(num_faces);
tex::build_adjacency_graph(mesh, vertex_infos, &graph);
wtimer.reset();


/****** 2015/12/11 yangliang *************/



//===============================View Selection ================================//
// if labeling file does not exist, compute a view label for each facet via MRF
if (conf.labeling_file.empty()) {
Expand Down Expand Up @@ -154,8 +152,62 @@ int main(int argc, char **argv) {
std::cout << "done." << std::endl;
}
std::cout << "\tTook: " << wtimer.get_elapsed_sec() << "s" << std::endl;
// todo rendering the mesh with different colors
{
std::vector<std::size_t> labeling(graph.num_nodes());
for (std::size_t i = 0; i < graph.num_nodes(); ++i) {
labeling[i] = graph.get_label(i);
}
std::vector<std::size_t>::iterator max_iter = std::max_element(labeling.begin(), labeling.end());
int n_labels = *max_iter + 1;
std::vector<math::Vec3f> colors(n_labels);
colors[0][0] = 0;
colors[0][1] = 0;
colors[0][2] = 0;

for(int i=1; i< colors.size(); i++){
colors[i][0] = rand()&255;
colors[i][1] = rand()&255;
colors[i][2] = rand()&255;
}

std::ofstream out("./examples/task7/view_selection_result.ply");
assert(out.is_open());
out<<"ply"<<std::endl;
out<<"format ascii 1.0"<<std::endl;
out<<"element vertex "<<mesh->get_vertices().size()<<std::endl;
out<<"property float x"<<std::endl;
out<<"property float y"<<std::endl;
out<<"property float z"<<std::endl;
out<<"element face "<<mesh->get_faces().size()/3<<std::endl;
out<<"property list uchar int vertex_indices"<<std::endl;
out<<"property uchar red"<<std::endl;
out<<"property uchar green"<<std::endl;
out<<"property uchar blue"<<std::endl;
out<<"end_header"<<std::endl;

// Face List
core::TriangleMesh::FaceList const & mesh_faces = mesh->get_faces();
// Vertices
core::TriangleMesh::VertexList const & vertices = mesh->get_vertices();
for(int i=0; i< vertices.size(); i++){
out<<vertices[i][0]<<" "<<vertices[i][1]<<" "<<vertices[i][2]<<std::endl;
}

std::cout<<"labeling size: "<<labeling.size()<<" faces size: "<<num_faces<<std::endl;
for(int i=0; i< labeling.size(); i++){
int label = labeling[i];
assert(label>=0 && label <labeling.size());
int v0 = mesh_faces[3*i + 0];
int v1 = mesh_faces[3*i + 1];
int v2 = mesh_faces[3*i + 2];
int r = colors[label][0];
int g = colors[label][1];
int b = colors[label][2];
out<<"3 "<<v0<<" "<<v1<<" "<<v2<<" "<<r<<" "<<g<<" "<<b<<std::endl;
}
out.close();
}

//=============================================texture_atlases====================================================//
tex::TextureAtlases texture_atlases;
Expand All @@ -171,6 +223,44 @@ int main(int argc, char **argv) {
&vertex_projection_infos,
&texture_patches);

// todo save texturePatches and the projection of facets
{
for(int i=0; i< texture_patches.size(); i++)
{
if(texture_patches[i]->get_faces().size()<10000)continue;

char image_name[255];
char validity_mask_name[255];
char blending_mask_name[255];

sprintf(image_name,"examples/task7/texture_patches_init/texture_patch%d.jpg", i);
sprintf(blending_mask_name,"examples/task7/texture_patches_init/blending_mask%d.jpg", i);
sprintf(validity_mask_name,"examples/task7/texture_patches_init/validity_mask%d.jpg", i);

core::FloatImage::Ptr image = texture_patches[i]->get_image()->duplicate();
core::ByteImage::Ptr validity_mask = texture_patches[i]->get_validity_mask()->duplicate();
core::ByteImage::Ptr blending_mask = texture_patches[i]->get_blending_mask()->duplicate();

float color[3]={255, 0,0};
std::vector<math::Vec2f> texcoords = texture_patches[i]->get_texcoords();
for(int i=0; i< texcoords.size(); i+=3){
math::Vec2f v0 = texcoords[i+0];
math::Vec2f v1 = texcoords[i+1];
math::Vec2f v2 = texcoords[i+2];

core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v1[0]), int(v1[1]), color);
core::image::draw_line<float>(*image, int(v1[0]), int(v1[1]), int(v2[0]), int(v2[1]), color);
core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v2[0]), int(v2[1]), color);

}

core::image::save_file(core::image::float_to_byte_image(image), image_name);
core::image::save_file(validity_mask, validity_mask_name);
core::image::save_file(blending_mask, blending_mask_name);

}
}

// Global seam leveling
if (conf.settings.global_seam_leveling) {
std::cout << "Running global seam leveling:" << std::endl;
Expand All @@ -180,6 +270,44 @@ int main(int argc, char **argv) {
vertex_projection_infos,
&texture_patches);
timer.measure("Running global seam leveling");

{
for(int i=0; i< texture_patches.size(); i++)
{
if(texture_patches[i]->get_faces().size()<10000)continue;

char image_name[255];
char validity_mask_name[255];
char blending_mask_name[255];

sprintf(image_name,"examples/task7/texture_pathes_color_adjustment/texture_patch%d.jpg", i);
sprintf(blending_mask_name,"examples/task7/texture_pathes_color_adjustment/blending_mask%d.jpg", i);
sprintf(validity_mask_name,"examples/task7/texture_pathes_color_adjustment/validity_mask%d.jpg", i);

core::FloatImage::Ptr image = texture_patches[i]->get_image()->duplicate();
core::ByteImage::Ptr validity_mask = texture_patches[i]->get_validity_mask()->duplicate();
core::ByteImage::Ptr blending_mask = texture_patches[i]->get_blending_mask()->duplicate();

float color[3]={255, 0,0};
std::vector<math::Vec2f> texcoords = texture_patches[i]->get_texcoords();
for(int i=0; i< texcoords.size(); i+=3){
math::Vec2f v0 = texcoords[i+0];
math::Vec2f v1 = texcoords[i+1];
math::Vec2f v2 = texcoords[i+2];

core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v1[0]), int(v1[1]), color);
core::image::draw_line<float>(*image, int(v1[0]), int(v1[1]), int(v2[0]), int(v2[1]), color);
core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v2[0]), int(v2[1]), color);

}

core::image::save_file(core::image::float_to_byte_image(image), image_name);
core::image::save_file(validity_mask, validity_mask_name);
core::image::save_file(blending_mask, blending_mask_name);

}
}

} else {
ProgressCounter texture_patch_counter("Calculating validity masks for texture patches", texture_patches.size());
#pragma omp parallel for schedule(dynamic)
Expand All @@ -193,6 +321,7 @@ int main(int argc, char **argv) {
timer.measure("Calculating texture patch validity masks");
}


//======================================local seam leveling===========================//
// Local seam leveling (Poisson Editing???)
if (conf.settings.local_seam_leveling) {
Expand All @@ -203,7 +332,6 @@ int main(int argc, char **argv) {


//====================================Generating textgure atlases====================//
// Generate texture atlases
/* Generate texture atlases. */
std::cout << "Generating texture atlases:" << std::endl;
tex::generate_texture_atlases(&texture_patches, &texture_atlases);
Expand Down
2 changes: 2 additions & 0 deletions texturing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ project(texturing)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-fPIC")

add_definitions(-DRESEARCH)

include_directories(..)
include_directories(../3rdParty/mrf)
include_directories(../3rdParty/coldet/src)
Expand Down
3 changes: 3 additions & 0 deletions texturing/build_adjacency_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ void
build_adjacency_graph(core::TriangleMesh::ConstPtr mesh,
core::VertexInfoList::ConstPtr vertex_infos, UniGraph * graph) {

// vertex indices of facets
core::TriangleMesh::FaceList const & faces = mesh->get_faces();
// number of facets
std::size_t const num_faces = faces.size() / 3;

ProgressCounter face_counter("\tAdding edges", num_faces);
for (std::size_t i = 0; i < faces.size(); i += 3) {
face_counter.progress<SIMPLE>();

//vertex index of facets
std::size_t v1 = faces[i];
std::size_t v2 = faces[i + 1];
std::size_t v3 = faces[i + 2];
Expand Down
1 change: 1 addition & 0 deletions texturing/generate_texture_atlases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ std::pair<float, float>
calculate_mapping_function(std::list<TexturePatch::ConstPtr> const & texture_patches) {
float min = std::numeric_limits<float>::max();
float max = std::numeric_limits<float>::lowest();
// for each texture patch
for (TexturePatch::ConstPtr texture_patch : texture_patches) {
core::FloatImage::ConstPtr image = texture_patch->get_image();
core::ByteImage::ConstPtr validity_mask = texture_patch->get_validity_mask();
Expand Down
10 changes: 5 additions & 5 deletions texturing/generate_texture_patches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct TexturePatchCandidate {
/** Create a TexturePatchCandidate by calculating
* the faces' bounding box projected into the view,
* relative texture coordinates
* and extacting the texture views relevant part
* and extracting the texture views relevant part
*/
TexturePatchCandidate
generate_candidate(int label, TextureView const & texture_view,
Expand Down Expand Up @@ -115,7 +115,7 @@ generate_candidate(int label, TextureView const & texture_view,
TexturePatch::create(label, faces, texcoords, image)};
return texture_patch_candidate;
}

// generate texture patches
void
generate_texture_patches(UniGraph const & graph, core::TriangleMesh::ConstPtr mesh,
core::VertexInfoList::ConstPtr vertex_infos,
Expand All @@ -138,7 +138,7 @@ generate_texture_patches(UniGraph const & graph, core::TriangleMesh::ConstPtr me
#pragma omp parallel for schedule(dynamic)
for (std::size_t i = 0; i < texture_views->size(); ++i) {

// get all the patches that are seedn from view i+1
// get all the patches that are seen from view i+1
std::vector<std::vector<std::size_t> > subgraphs;
int const label = i + 1;
graph.get_subgraphs(label, &subgraphs);
Expand Down Expand Up @@ -206,8 +206,8 @@ generate_texture_patches(UniGraph const & graph, core::TriangleMesh::ConstPtr me
vertex_projection_infos->at(vertex_id).push_back(info);
}
}
}
}
} // for each candidata
} // for each texture view

// merge vertex projection information
merge_vertex_projection_infos(vertex_projection_infos);
Expand Down
Loading

0 comments on commit 5541078

Please sign in to comment.