diff --git a/happly.h b/happly.h index 84c780f..ca76d87 100644 --- a/happly.h +++ b/happly.h @@ -29,6 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include #include #include #include @@ -1206,6 +1207,44 @@ class PLYData { return result; } + /** + * @brief Common-case helper get mesh vertex UVs + * + * @param vertexElementName The element name to use (default: "vertex") + * + * @return A vector of vertex UVs. + */ + std::vector> getVertexUV(const std::string& vertexElementName = "vertex") { + bool hasUV = false; + for (std::unique_ptr& prop : getElement(vertexElementName).properties) { + if (prop->name == "u") { + hasUV = true; + } + } + + if(!hasUV) { + return std::vector>(); + } + + std::vector uPos = getElement(vertexElementName).getProperty("u"); + std::vector vPos = getElement(vertexElementName).getProperty("v"); + bool allzeros = std::all_of(uPos.begin(), uPos.end(), [](double i) { return i==0.0f; }); + allzeros = allzeros || std::all_of(vPos.begin(), vPos.end(), [](double i) { return i==0.0f; }); + + if(allzeros) { + return std::vector>(); + } + + // else, we have valid UVs + std::vector> result(uPos.size()); + for (size_t i = 0; i < result.size(); i++) { + result[i][0] = uPos[i]; + result[i][1] = vPos[i]; + } + + return result; + } + /** * @brief Common-case helper get mesh vertex colors *