@@ -120,7 +120,7 @@ NexusBuilder::NexusBuilder(Signature &signature): chunks("cache_chunks"), scalin
120120 header.nvert = header.nface = header.n_nodes = header.n_patches = header.n_textures = 0 ;
121121}
122122
123- void NexusBuilder::initAtlas (const std::vector<QImage>& textures) {
123+ void NexusBuilder::initAtlas (std::vector<QImage>& textures) {
124124 if (textures.size ()) {
125125 atlas.addTextures (textures);
126126 }
@@ -143,7 +143,6 @@ void NexusBuilder::create(KDTree *tree, Stream *stream, uint top_node_size) {
143143 int level = 0 ;
144144 int last_top_level_size = 0 ;
145145 do {
146- // cout << "Creating level " << level << endl;
147146 tree->clear ();
148147 if (level % 2 ) tree->setAxesDiagonal ();
149148 else tree->setAxesOrthogonal ();
@@ -207,15 +206,17 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
207206 std::vector<int > vertex_to_box;
208207
209208
209+ // find connected pieces of triangles belonging to the same texture
210+ // iterate over the triangles and connect the vertices.
210211 UnionFind components;
211212 components.init (mesh.vert .size ());
212213
213214 for (auto &face: mesh.face ) {
214215 int v[3 ];
215216 for (int i = 0 ; i < 3 ; i++) {
216217 v[i] = face.V (i) - &*mesh.vert .begin ();
217-
218-
218+
219+
219220 int &t = vertex_to_tex[v[i]];
220221
221222 if (t != -1 && t != face.tex ) qDebug () << " Missing vertex replication across seams\n " ;
@@ -227,17 +228,15 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
227228 }
228229 int n_boxes = components.compact (vertex_to_box);
229230
231+ // assign a texture to each vertex (we already split)
230232 for (auto &face: mesh.face ) {
231233 int v[3 ];
232234 for (int i = 0 ; i < 3 ; i++) {
233235 int v = face.V (i) - &*mesh.vert .begin ();
234236 vertex_to_tex[v] = face.tex ;
235237 }
236- /* assert(vertex_to_box[v[0]] == vertex_to_box[v[1]]);
237- assert(vertex_to_box[v[0]] == vertex_to_box[v[2]]);
238- assert(components.root(v[0]) == components.root(v[2]));
239- assert(components.root(v[0]) == components.root(v[1])); */
240238 }
239+
241240 // assign all boxes to a tex (and remove boxes where the tex is -1
242241
243242 // compute boxes
@@ -251,13 +250,15 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
251250 vcg::Box2f &box = boxes[b];
252251 box_texture[b] = tex;
253252 auto &t = mesh.vert [i].T ().P ();
254- t[0 ] = fmod (t[0 ], 1.0 );
255- t[1 ] = fmod (t[1 ], 1.0 );
256- // if(isnan(t[0]) || isnan(t[1]) || t[0] < 0 || t[1] < 0 || t[0] > 1 || t[1] > 1)
257- // cout << "T: " << t[0] << " " << t[1] << endl;
253+ if (t[0 ] != 1.0 )
254+ t[0 ] = fmod (t[0 ], 1.0 );
255+ if (t[1 ] != 1.0 )
256+ t[1 ] = fmod (t[1 ], 1.0 );
257+
258258 if (t[0 ] != 0 .0f || t[1 ] != 0 .0f )
259259 box.Add (t);
260260 }
261+
261262 // erase boxes assigned to no texture, and remap vertex_to_box
262263 int count = 0 ;
263264 std::vector<int > remap (mesh.vert .size (), -1 );
@@ -277,9 +278,17 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
277278 std::vector<vcg::Point2i> origins (boxes.size ());
278279 for (size_t b = 0 ; b < boxes.size (); b++) {
279280 auto &box = boxes[b];
281+ if (box.DimX () > 0.9 ) {
282+ for (auto &face: mesh.face ) {
283+ int v[3 ];
284+ for (int i = 0 ; i < 3 ; i++) {
285+ auto v = face.V (i);
286+ int j = (i+1 )%3 ;
287+ }
288+ }
289+ }
280290 int tex = box_texture[b];
281291
282- // enlarge 1 pixel
283292 float w = atlas.width (tex, level); // img->size().width();
284293 float h = atlas.height (tex, level); // img->size().height();
285294 float px = 1 /(float )w;
@@ -299,10 +308,6 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
299308 size[1 ] = std::min (h, ceil (box.max [1 ]/py)) - origin[1 ];
300309 if (size[0 ] <= 0 ) size[0 ] = 1 ;
301310 if (size[1 ] <= 0 ) size[1 ] = 1 ;
302-
303- // cout << "Box: " << box_texture[b] << " [" << box.min[0] << " " << box.min[1] << " ] [ " << box.max[0] << " " << box.max[1] << "]" << std::endl;
304- // cout << "Size: " << size[0] << " - " << size[1] << endl << endl;
305- // getchar();
306311 }
307312
308313 // pack boxes;
@@ -342,7 +347,6 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
342347 finalSize[ 1 ] = (int ) nextPowerOf2 ( finalSize[ 1 ] );
343348 }
344349
345- // std::cout << "Boxes: " << boxes.size() << " Final size: " << finalSize[0] << " " << finalSize[1] << std::endl;
346350 QImage image (finalSize[0 ], finalSize[1 ], QImage::Format_RGB32);
347351 image.fill (QColor (127 , 127 , 127 ));
348352 // copy boxes using mapping
@@ -361,10 +365,9 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
361365 vcg::Point2i &o = origins[b];
362366 vcg::Point2i m = mapping[b];
363367
364- // QImageReader &img = textures[box_texture[b]];
365368 int tex = box_texture[b];
366- float w = atlas.width (tex, level); // img->size().width();
367- float h = atlas.height (tex, level); // img->size().height();
369+ float w = atlas.width (tex, level);
370+ float h = atlas.height (tex, level);
368371 float px = 1 /(float )w;
369372 float py = 1 /(float )h;
370373
@@ -404,7 +407,7 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
404407 }
405408 pixelXedge = sqrt (pixelXedge/mesh.face .size ()*3 );
406409 error = sqrt (error/mesh.face .size ()*3 );
407-
410+
408411 double areausage = 0.0 ;
409412 // compute area waste
410413 for (int i = 0 ; i < mesh.face .size (); i++) {
@@ -416,7 +419,6 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
416419 auto V1 = face.V (1 )->T ().P ();
417420 auto V2 = face.V (2 )->T ().P ();
418421 areausage += (V2 - V0 )^(V2 - V1 )/2 ;
419-
420422 }
421423
422424 {
@@ -443,14 +445,14 @@ QImage NexusBuilder::extractNodeTex(TMesh &mesh, int level, float &error, float
443445 // boxid++;
444446 }
445447
446-
448+
447449 /* painter.setPen(QColor(255,0,255));
448450 for(int i = 0; i < mesh.face.size(); i++) {
449451 auto &face = mesh.face[i];
450452 int b = vertex_to_box[face.V(0) - &(mesh.vert[0])];
451453 vcg::Point2i &o = origins[b];
452454 vcg::Point2i m = mapping[b];
453-
455+
454456 for(int k = 0; k < 3; k++) {
455457 int j = (k==2)?0:k+1;
456458 auto V0 = face.V(k);
@@ -865,7 +867,6 @@ void NexusBuilder::reverseDag() {
865867
866868void NexusBuilder::save (QString filename) {
867869
868- // cout << "Saving to file " << qPrintable(filename) << endl;
869870 // cout << "Input squaresize " << sqrt(input_pixels) << " Output size " << sqrt(output_pixels) << "\n";
870871
871872 file.setFileName (filename);
@@ -1102,7 +1103,7 @@ void NexusBuilder::appendBorderVertices(uint32_t origin, uint32_t destination, s
11021103 uint16_t *face = (uint16_t *)(buffer + header.signature .vertex .size ()*node.nvert );
11031104
11041105 NodeBox &nodebox = boxes[origin];
1105-
1106+
11061107 vector<bool > border = nodebox.markBorders (node, point, face);
11071108 for (int i = 0 ; i < node.nvert ; i++) {
11081109 if (border[i])
@@ -1168,7 +1169,7 @@ void NexusBuilder::uniformNormals() {
11681169 uint start = 0 ;
11691170 while (start < vertices.size ()) {
11701171 NVertex &v = vertices[start];
1171-
1172+
11721173 uint last = start+1 ;
11731174 while (last < vertices.size () && vertices[last].point == v.point )
11741175 last++;
@@ -1187,15 +1188,15 @@ void NexusBuilder::uniformNormals() {
11871188
11881189 for (uint k = start; k < last; k++)
11891190 *vertices[k].normal = normals;
1190-
1191+
11911192 } else // just copy from first one (coming from lower level due to sorting
11921193 for (uint k = start; k < last; k++)
11931194 *vertices[k].normal =*v.normal ;
1194-
1195+
11951196 start = last;
11961197 }
11971198
1198-
1199+
11991200 /* for(uint k = 0; k < vertices.size(); k++) {
12001201 NVertex &v = vertices[k];
12011202 if(v.point != previous) {
0 commit comments