@@ -730,7 +730,13 @@ struct hash< std::pair< A, B > > {
730730};
731731}
732732
733- Halfedge_Mesh Halfedge_Mesh::from_indexed_faces (std::vector< Vec3 > const &vertices_, std::vector< std::vector< Index > > const &faces_) {
733+ Halfedge_Mesh Halfedge_Mesh::from_indexed_faces (std::vector< Vec3 > const &vertices_,
734+ std::vector< std::vector< Index > > const &faces_,
735+ std::vector< std::vector< Index > > const &corner_normal_idxs,
736+ std::vector< std::vector< Index > > const &corner_uv_idxs,
737+ std::vector<Vec3> const &corner_normals_,
738+ std::vector<Vec2> const &corner_uvs_)
739+ {
734740
735741 Halfedge_Mesh mesh;
736742
@@ -744,8 +750,13 @@ Halfedge_Mesh Halfedge_Mesh::from_indexed_faces(std::vector< Vec3 > const &verti
744750
745751 std::unordered_map< std::pair< Index, Index >, HalfedgeRef > halfedges; // for quick lookup of halfedges by from/to vertex index
746752
753+ uint32_t num_faces = static_cast <uint32_t >(faces_.size ());
754+ const bool add_corner_normals = corner_normal_idxs.size () >= num_faces;
755+ const bool add_corner_uvs = corner_uv_idxs.size () >= num_faces;
747756 // helper to add a face (and, later, boundary):
748- auto add_loop = [&](std::vector< Index > const &loop, bool boundary) {
757+ auto add_loop = [&](std::vector< Index > const &loop, bool boundary,
758+ std::vector< Index> const &n_loop = std::vector<Index>{},
759+ std::vector< Index> const &uv_loop = std::vector<Index>{}) {
749760 assert (loop.size () >= 3 );
750761
751762 for (uint32_t j = 0 ; j < loop.size (); ++j) {
@@ -791,13 +802,19 @@ Halfedge_Mesh Halfedge_Mesh::from_indexed_faces(std::vector< Vec3 > const &verti
791802
792803 if (i != 0 ) prev->next = halfedge; // set previous halfedge's next pointer
793804 prev = halfedge;
805+
806+ if (add_corner_normals && i < n_loop.size ()) halfedge->corner_normal = corner_normals_[n_loop[i]];
807+ if (add_corner_uvs && i < uv_loop.size ()) halfedge->corner_uv = corner_uvs_[uv_loop[i]];
794808 }
809+
795810 prev->next = face->halfedge ; // set next pointer for last halfedge to first edge
796811 };
797812
798813 // add all faces:
799- for (auto const &loop : faces_) {
800- add_loop (loop, false );
814+ for (uint32_t i = 0 ; i < num_faces; i++) {
815+ if (add_corner_normals && add_corner_uvs) add_loop (faces_[i], false , corner_normal_idxs[i], corner_uv_idxs[i]);
816+ else if (add_corner_normals) add_loop (faces_[i], false , corner_normal_idxs[i]);
817+ else add_loop (faces_[i], false );
801818 }
802819
803820 // All halfedges created so far have valid next pointers, but some may be missing twins because they are at a boundary.
@@ -849,6 +866,49 @@ Halfedge_Mesh Halfedge_Mesh::from_indexed_faces(std::vector< Vec3 > const &verti
849866 return mesh;
850867}
851868
869+ Halfedge_Mesh Halfedge_Mesh::from_indexed_mesh_with_corner_data (
870+ const Indexed_Mesh& indexed_mesh,
871+ std::vector<Index> const &corner_normal_idxs_,
872+ std::vector<Index> const &corner_uv_idxs_,
873+ std::vector<Vec3> const &normals_,
874+ std::vector<Vec2> const &uvs_)
875+ {
876+
877+ // extract vertex positions and face indices from indexed_mesh:
878+ std::vector< Vec3 > indexed_vertices;
879+ indexed_vertices.reserve (indexed_mesh.vertices ().size ());
880+ for (auto const &v : indexed_mesh.vertices ()) {
881+ indexed_vertices.emplace_back (v.pos );
882+ }
883+
884+ std::vector< std::vector< Index > > indexed_faces;
885+ assert (indexed_mesh.indices ().size () % 3 == 0 );
886+ indexed_faces.reserve (indexed_mesh.indices ().size () / 3 );
887+ for (uint32_t i = 0 ; i < indexed_mesh.indices ().size (); i += 3 ) {
888+ indexed_faces.emplace_back (indexed_mesh.indices ().begin () + i, indexed_mesh.indices ().begin () + i + 3 );
889+ }
890+
891+ std::vector< std::vector < Index > > indexed_normals;
892+ assert (corner_normal_idxs_.size () % 3 == 0 );
893+ indexed_normals.reserve (corner_normal_idxs_.size () / 3 );
894+ for (uint32_t i = 0 ; i < corner_normal_idxs_.size (); i += 3 ) {
895+ indexed_normals.emplace_back (corner_normal_idxs_.begin () + i, corner_normal_idxs_.begin () + i + 3 );
896+ }
897+
898+ std::vector< std::vector < Index > > indexed_uvs;
899+ assert (corner_uv_idxs_.size () % 3 == 0 );
900+ indexed_uvs.reserve (corner_uv_idxs_.size () / 3 );
901+ for (uint32_t i = 0 ; i < corner_uv_idxs_.size (); i += 3 ) {
902+ indexed_uvs.emplace_back (corner_uv_idxs_.begin () + i, corner_uv_idxs_.begin () + i + 3 );
903+ }
904+
905+ // build halfedge mesh with the extracted vertex/face data:
906+ Halfedge_Mesh mesh = Halfedge_Mesh::from_indexed_faces (indexed_vertices,
907+ indexed_faces, indexed_normals, indexed_uvs, normals_, uvs_);
908+
909+ return mesh;
910+ }
911+
852912Halfedge_Mesh Halfedge_Mesh::from_indexed_mesh (Indexed_Mesh const &indexed_mesh) {
853913
854914 // extract vertex positions and face indices from indexed_mesh:
0 commit comments