From ba257634f203c1efc3ba1781350a3aa3fe739516 Mon Sep 17 00:00:00 2001
From: sabrina-shanman <sabrina@highfidelity.io>
Date: Thu, 31 Oct 2019 15:54:17 -0700
Subject: [PATCH] Introduce extents for TriangleListMesh and calculate them

---
 libraries/hfm/src/hfm/HFM.h            |  1 +
 libraries/hfm/src/hfm/HFMModelMath.cpp | 16 ++++++++++++++++
 libraries/hfm/src/hfm/HFMModelMath.h   |  2 ++
 3 files changed, 19 insertions(+)

diff --git a/libraries/hfm/src/hfm/HFM.h b/libraries/hfm/src/hfm/HFM.h
index ec06832f22..d141c88cd8 100644
--- a/libraries/hfm/src/hfm/HFM.h
+++ b/libraries/hfm/src/hfm/HFM.h
@@ -235,6 +235,7 @@ struct TriangleListMesh {
     std::vector<glm::vec3> vertices;
     std::vector<uint32_t> indices;
     std::vector<glm::ivec2> parts; // Offset in the indices, Number of indices
+    std::vector<Extents> partExtents; // Extents of each part with no transform applied. Same length as parts.
 };
 
 /// A single mesh (with optional blendshapes).
diff --git a/libraries/hfm/src/hfm/HFMModelMath.cpp b/libraries/hfm/src/hfm/HFMModelMath.cpp
index d0288d684c..7ce06821ec 100644
--- a/libraries/hfm/src/hfm/HFMModelMath.cpp
+++ b/libraries/hfm/src/hfm/HFMModelMath.cpp
@@ -40,6 +40,20 @@ void thickenFlatExtents(Extents& extents) {
     extents.maximum += glm::vec3(EPSILON, EPSILON, EPSILON);
 }
 
+void calculateExtentsForTriangleListMesh(TriangleListMesh& triangleListMesh) {
+    triangleListMesh.partExtents.resize(triangleListMesh.parts.size());
+    for (size_t partIndex = 0; partIndex < triangleListMesh.parts.size(); ++partIndex) {
+        const auto& part = triangleListMesh.parts[partIndex];
+        auto& extents = triangleListMesh.partExtents[partIndex];
+        int partEnd = part.x + part.y;
+        for (int i = part.x; i < partEnd; ++i) {
+            auto index = triangleListMesh.indices[i];
+            const auto& position = triangleListMesh.vertices[index];
+            extents.addPoint(position);
+        }
+    }
+}
+
 void calculateExtentsForShape(hfm::Shape& shape, const std::vector<hfm::Mesh>& meshes, const std::vector<hfm::Joint> joints) {
     auto& shapeExtents = shape.transformedExtents;
     shapeExtents.reset();
@@ -196,6 +210,8 @@ const TriangleListMesh generateTriangleListMesh(const std::vector<glm::vec3>& sr
         }
     }
 
+    calculateExtentsForTriangleListMesh(dest);
+
     return dest;
 }
 
diff --git a/libraries/hfm/src/hfm/HFMModelMath.h b/libraries/hfm/src/hfm/HFMModelMath.h
index dc397c5e6f..3ed0584fac 100644
--- a/libraries/hfm/src/hfm/HFMModelMath.h
+++ b/libraries/hfm/src/hfm/HFMModelMath.h
@@ -20,6 +20,8 @@ void forEachIndex(const hfm::MeshPart& meshPart, std::function<void(uint32_t)> f
 
 void initializeExtents(Extents& extents);
 
+void calculateExtentsForTriangleListMesh(TriangleListMesh& triangleListMesh);
+
 // This can't be moved to model-baker until
 void calculateExtentsForShape(hfm::Shape& shape, const std::vector<hfm::Mesh>& meshes, const std::vector<hfm::Joint> joints);