Skip to content

Commit 5541078

Browse files
author
swayfreeda
committed
add task7&&8
1 parent 4b73d99 commit 5541078

15 files changed

+267
-51
lines changed

core/image_tools.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ gamma_correct (ByteImage::Ptr image, float power)
175175

176176
uint8_t lookup[256];
177177
for (int i = 0; i < 256; ++i)
178-
lookup[i] = static_cast<uint8_t>(std::pow(i / 255.0f, power)
179-
* 255.0f + 0.5f);
178+
lookup[i] = static_cast<uint8_t>(std::pow(i / 255.0f, power) * 255.0f + 0.5f);
180179
for (int i = 0; i < image->get_value_amount(); ++i)
181180
image->at(i) = lookup[image->at(i)];
182181
}

core/mesh.h

+18-14
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ class TriangleMesh : public MeshBase
9393
typedef std::shared_ptr<TriangleMesh> Ptr;
9494
typedef std::shared_ptr<TriangleMesh const> ConstPtr;
9595

96-
// 法向量
96+
// normals
9797
typedef std::vector<math::Vec3f> NormalList;
98-
// 纹理坐标
98+
99+
// texcoords
99100
typedef std::vector<math::Vec2f> TexCoordList;
100-
// 面片
101+
102+
// vertex indeices for facets
101103
typedef std::vector<VertexID> FaceList;
102104

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

112114
/** Returns the vertex normals. */
113115
NormalList const& get_vertex_normals (void) const;
116+
114117
/** Returns the vertex normals. */
115118
NormalList& get_vertex_normals (void);
116119

117120
/** Returns the vectex texture coordinates. */
118121
TexCoordList const& get_vertex_texcoords (void) const;
122+
119123
/** Returns the vectex texture coordinates. */
120124
TexCoordList& get_vertex_texcoords (void);
121125

122126
/** Returns the triangle indices. */
123127
FaceList const& get_faces (void) const;
128+
124129
/** Returns the triangle indices. */
125130
FaceList& get_faces (void);
126131

127132
/** Returns the face normals. */
128133
NormalList const& get_face_normals (void) const;
134+
129135
/** Returns the face normals. */
130136
NormalList& get_face_normals (void);
131137

132138
/** Returns the face colors. */
133139
ColorList const& get_face_colors (void) const;
140+
134141
/** Returns the face colors. */
135142
ColorList& get_face_colors (void);
136143

@@ -176,19 +183,19 @@ class TriangleMesh : public MeshBase
176183

177184
protected:
178185

179-
// 顶点法向量
186+
// vertex normals
180187
NormalList vertex_normals;
181188

182-
// 顶点纹理坐标
189+
// vertex texcoords
183190
TexCoordList vertex_texcoords;
184191

185-
// 面片
192+
// facets
186193
FaceList faces;
187194

188-
// 面片法向量
195+
// facet normals
189196
NormalList face_normals;
190197

191-
// 面片颜色
198+
// facet colors
192199
ColorList face_colors;
193200

194201
protected:
@@ -199,13 +206,11 @@ class TriangleMesh : public MeshBase
199206
/* ---------------------------------------------------------------- */
200207

201208
inline
202-
MeshBase::MeshBase (void)
203-
{
209+
MeshBase::MeshBase (void) {
204210
}
205211

206212
inline
207-
MeshBase::~MeshBase (void)
208-
{
213+
MeshBase::~MeshBase (void) {
209214
}
210215

211216
inline MeshBase::VertexList const&
@@ -215,8 +220,7 @@ MeshBase::get_vertices (void) const
215220
}
216221

217222
inline MeshBase::VertexList&
218-
MeshBase::get_vertices (void)
219-
{
223+
MeshBase::get_vertices (void) {
220224
return this->vertices;
221225
}
222226

core/mesh_info.h

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class VertexInfoList : public std::vector<MeshVertexInfo>
7373
public:
7474
/* Creates an uninitialized vertex info list. */
7575
VertexInfoList (void);
76+
7677
/* Creates and initializes a vertex info list. */
7778
VertexInfoList (TriangleMesh::ConstPtr mesh);
7879

examples/task7/class7_texrecon.cpp

+135-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <util/system.h>
1616
#include <util/file_system.h>
1717
#include <core/mesh_io_ply.h>
18+
#include <core/image_tools.h>
19+
#include <core/image_drawing.h>
20+
#include "core/image_io.h"
1821

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

6770

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

7779

7880
//===============================Building adjacency graph=======================//
79-
std::cout << "Building adjacency graph: " << std::endl;
81+
std::cout << "Building adjacency graph: " << std::endl; // each facet is corresponding a facet
8082
tex::Graph graph(num_faces);
8183
tex::build_adjacency_graph(mesh, vertex_infos, &graph);
8284
wtimer.reset();
8385

8486

85-
/****** 2015/12/11 yangliang *************/
86-
87-
88-
8987
//===============================View Selection ================================//
9088
// if labeling file does not exist, compute a view label for each facet via MRF
9189
if (conf.labeling_file.empty()) {
@@ -154,8 +152,62 @@ int main(int argc, char **argv) {
154152
std::cout << "done." << std::endl;
155153
}
156154
std::cout << "\tTook: " << wtimer.get_elapsed_sec() << "s" << std::endl;
155+
// todo rendering the mesh with different colors
156+
{
157+
std::vector<std::size_t> labeling(graph.num_nodes());
158+
for (std::size_t i = 0; i < graph.num_nodes(); ++i) {
159+
labeling[i] = graph.get_label(i);
160+
}
161+
std::vector<std::size_t>::iterator max_iter = std::max_element(labeling.begin(), labeling.end());
162+
int n_labels = *max_iter + 1;
163+
std::vector<math::Vec3f> colors(n_labels);
164+
colors[0][0] = 0;
165+
colors[0][1] = 0;
166+
colors[0][2] = 0;
167+
168+
for(int i=1; i< colors.size(); i++){
169+
colors[i][0] = rand()&255;
170+
colors[i][1] = rand()&255;
171+
colors[i][2] = rand()&255;
172+
}
157173

174+
std::ofstream out("./examples/task7/view_selection_result.ply");
175+
assert(out.is_open());
176+
out<<"ply"<<std::endl;
177+
out<<"format ascii 1.0"<<std::endl;
178+
out<<"element vertex "<<mesh->get_vertices().size()<<std::endl;
179+
out<<"property float x"<<std::endl;
180+
out<<"property float y"<<std::endl;
181+
out<<"property float z"<<std::endl;
182+
out<<"element face "<<mesh->get_faces().size()/3<<std::endl;
183+
out<<"property list uchar int vertex_indices"<<std::endl;
184+
out<<"property uchar red"<<std::endl;
185+
out<<"property uchar green"<<std::endl;
186+
out<<"property uchar blue"<<std::endl;
187+
out<<"end_header"<<std::endl;
188+
189+
// Face List
190+
core::TriangleMesh::FaceList const & mesh_faces = mesh->get_faces();
191+
// Vertices
192+
core::TriangleMesh::VertexList const & vertices = mesh->get_vertices();
193+
for(int i=0; i< vertices.size(); i++){
194+
out<<vertices[i][0]<<" "<<vertices[i][1]<<" "<<vertices[i][2]<<std::endl;
195+
}
158196

197+
std::cout<<"labeling size: "<<labeling.size()<<" faces size: "<<num_faces<<std::endl;
198+
for(int i=0; i< labeling.size(); i++){
199+
int label = labeling[i];
200+
assert(label>=0 && label <labeling.size());
201+
int v0 = mesh_faces[3*i + 0];
202+
int v1 = mesh_faces[3*i + 1];
203+
int v2 = mesh_faces[3*i + 2];
204+
int r = colors[label][0];
205+
int g = colors[label][1];
206+
int b = colors[label][2];
207+
out<<"3 "<<v0<<" "<<v1<<" "<<v2<<" "<<r<<" "<<g<<" "<<b<<std::endl;
208+
}
209+
out.close();
210+
}
159211

160212
//=============================================texture_atlases====================================================//
161213
tex::TextureAtlases texture_atlases;
@@ -171,6 +223,44 @@ int main(int argc, char **argv) {
171223
&vertex_projection_infos,
172224
&texture_patches);
173225

226+
// todo save texturePatches and the projection of facets
227+
{
228+
for(int i=0; i< texture_patches.size(); i++)
229+
{
230+
if(texture_patches[i]->get_faces().size()<10000)continue;
231+
232+
char image_name[255];
233+
char validity_mask_name[255];
234+
char blending_mask_name[255];
235+
236+
sprintf(image_name,"examples/task7/texture_patches_init/texture_patch%d.jpg", i);
237+
sprintf(blending_mask_name,"examples/task7/texture_patches_init/blending_mask%d.jpg", i);
238+
sprintf(validity_mask_name,"examples/task7/texture_patches_init/validity_mask%d.jpg", i);
239+
240+
core::FloatImage::Ptr image = texture_patches[i]->get_image()->duplicate();
241+
core::ByteImage::Ptr validity_mask = texture_patches[i]->get_validity_mask()->duplicate();
242+
core::ByteImage::Ptr blending_mask = texture_patches[i]->get_blending_mask()->duplicate();
243+
244+
float color[3]={255, 0,0};
245+
std::vector<math::Vec2f> texcoords = texture_patches[i]->get_texcoords();
246+
for(int i=0; i< texcoords.size(); i+=3){
247+
math::Vec2f v0 = texcoords[i+0];
248+
math::Vec2f v1 = texcoords[i+1];
249+
math::Vec2f v2 = texcoords[i+2];
250+
251+
core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v1[0]), int(v1[1]), color);
252+
core::image::draw_line<float>(*image, int(v1[0]), int(v1[1]), int(v2[0]), int(v2[1]), color);
253+
core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v2[0]), int(v2[1]), color);
254+
255+
}
256+
257+
core::image::save_file(core::image::float_to_byte_image(image), image_name);
258+
core::image::save_file(validity_mask, validity_mask_name);
259+
core::image::save_file(blending_mask, blending_mask_name);
260+
261+
}
262+
}
263+
174264
// Global seam leveling
175265
if (conf.settings.global_seam_leveling) {
176266
std::cout << "Running global seam leveling:" << std::endl;
@@ -180,6 +270,44 @@ int main(int argc, char **argv) {
180270
vertex_projection_infos,
181271
&texture_patches);
182272
timer.measure("Running global seam leveling");
273+
274+
{
275+
for(int i=0; i< texture_patches.size(); i++)
276+
{
277+
if(texture_patches[i]->get_faces().size()<10000)continue;
278+
279+
char image_name[255];
280+
char validity_mask_name[255];
281+
char blending_mask_name[255];
282+
283+
sprintf(image_name,"examples/task7/texture_pathes_color_adjustment/texture_patch%d.jpg", i);
284+
sprintf(blending_mask_name,"examples/task7/texture_pathes_color_adjustment/blending_mask%d.jpg", i);
285+
sprintf(validity_mask_name,"examples/task7/texture_pathes_color_adjustment/validity_mask%d.jpg", i);
286+
287+
core::FloatImage::Ptr image = texture_patches[i]->get_image()->duplicate();
288+
core::ByteImage::Ptr validity_mask = texture_patches[i]->get_validity_mask()->duplicate();
289+
core::ByteImage::Ptr blending_mask = texture_patches[i]->get_blending_mask()->duplicate();
290+
291+
float color[3]={255, 0,0};
292+
std::vector<math::Vec2f> texcoords = texture_patches[i]->get_texcoords();
293+
for(int i=0; i< texcoords.size(); i+=3){
294+
math::Vec2f v0 = texcoords[i+0];
295+
math::Vec2f v1 = texcoords[i+1];
296+
math::Vec2f v2 = texcoords[i+2];
297+
298+
core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v1[0]), int(v1[1]), color);
299+
core::image::draw_line<float>(*image, int(v1[0]), int(v1[1]), int(v2[0]), int(v2[1]), color);
300+
core::image::draw_line<float>(*image, int(v0[0]), int(v0[1]), int(v2[0]), int(v2[1]), color);
301+
302+
}
303+
304+
core::image::save_file(core::image::float_to_byte_image(image), image_name);
305+
core::image::save_file(validity_mask, validity_mask_name);
306+
core::image::save_file(blending_mask, blending_mask_name);
307+
308+
}
309+
}
310+
183311
} else {
184312
ProgressCounter texture_patch_counter("Calculating validity masks for texture patches", texture_patches.size());
185313
#pragma omp parallel for schedule(dynamic)
@@ -193,6 +321,7 @@ int main(int argc, char **argv) {
193321
timer.measure("Calculating texture patch validity masks");
194322
}
195323

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

204333

205334
//====================================Generating textgure atlases====================//
206-
// Generate texture atlases
207335
/* Generate texture atlases. */
208336
std::cout << "Generating texture atlases:" << std::endl;
209337
tex::generate_texture_atlases(&texture_patches, &texture_atlases);

texturing/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ project(texturing)
22
set(CMAKE_CXX_STANDARD 11)
33
set(CMAKE_CXX_FLAGS "-fPIC")
44

5+
add_definitions(-DRESEARCH)
6+
57
include_directories(..)
68
include_directories(../3rdParty/mrf)
79
include_directories(../3rdParty/coldet/src)

texturing/build_adjacency_graph.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ void
1717
build_adjacency_graph(core::TriangleMesh::ConstPtr mesh,
1818
core::VertexInfoList::ConstPtr vertex_infos, UniGraph * graph) {
1919

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

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

29+
//vertex index of facets
2730
std::size_t v1 = faces[i];
2831
std::size_t v2 = faces[i + 1];
2932
std::size_t v3 = faces[i + 2];

texturing/generate_texture_atlases.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ std::pair<float, float>
8686
calculate_mapping_function(std::list<TexturePatch::ConstPtr> const & texture_patches) {
8787
float min = std::numeric_limits<float>::max();
8888
float max = std::numeric_limits<float>::lowest();
89+
// for each texture patch
8990
for (TexturePatch::ConstPtr texture_patch : texture_patches) {
9091
core::FloatImage::ConstPtr image = texture_patch->get_image();
9192
core::ByteImage::ConstPtr validity_mask = texture_patch->get_validity_mask();

texturing/generate_texture_patches.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct TexturePatchCandidate {
5757
/** Create a TexturePatchCandidate by calculating
5858
* the faces' bounding box projected into the view,
5959
* relative texture coordinates
60-
* and extacting the texture views relevant part
60+
* and extracting the texture views relevant part
6161
*/
6262
TexturePatchCandidate
6363
generate_candidate(int label, TextureView const & texture_view,
@@ -115,7 +115,7 @@ generate_candidate(int label, TextureView const & texture_view,
115115
TexturePatch::create(label, faces, texcoords, image)};
116116
return texture_patch_candidate;
117117
}
118-
118+
// generate texture patches
119119
void
120120
generate_texture_patches(UniGraph const & graph, core::TriangleMesh::ConstPtr mesh,
121121
core::VertexInfoList::ConstPtr vertex_infos,
@@ -138,7 +138,7 @@ generate_texture_patches(UniGraph const & graph, core::TriangleMesh::ConstPtr me
138138
#pragma omp parallel for schedule(dynamic)
139139
for (std::size_t i = 0; i < texture_views->size(); ++i) {
140140

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

212212
// merge vertex projection information
213213
merge_vertex_projection_infos(vertex_projection_infos);

0 commit comments

Comments
 (0)