|
| 1 | +// |
| 2 | +// VROARDepthMesh.h |
| 3 | +// ViroRenderer |
| 4 | +// |
| 5 | +// Copyright © 2024 Viro Media. All rights reserved. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining |
| 8 | +// a copy of this software and associated documentation files (the |
| 9 | +// "Software"), to deal in the Software without restriction, including |
| 10 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 11 | +// distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | +// permit persons to whom the Software is furnished to do so, subject to |
| 13 | +// the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included |
| 16 | +// in all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 21 | +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 22 | +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 23 | +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 24 | +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | + |
| 26 | +#ifndef VROARDepthMesh_h |
| 27 | +#define VROARDepthMesh_h |
| 28 | + |
| 29 | +#include <vector> |
| 30 | +#include <memory> |
| 31 | +#include "VROVector3f.h" |
| 32 | + |
| 33 | +/** |
| 34 | + * Represents a triangulated mesh generated from AR depth data. |
| 35 | + * Used for physics collision and hit testing against real-world surfaces. |
| 36 | + * |
| 37 | + * The mesh is generated by sampling the depth image at regular intervals |
| 38 | + * (controlled by stride) and connecting adjacent valid depth samples into |
| 39 | + * triangles. Depth discontinuities are detected to avoid connecting |
| 40 | + * surfaces that are far apart in depth (e.g., a table edge and the floor). |
| 41 | + */ |
| 42 | +class VROARDepthMesh { |
| 43 | +public: |
| 44 | + VROARDepthMesh(); |
| 45 | + |
| 46 | + /** |
| 47 | + * Construct a depth mesh from vertex and index data. |
| 48 | + * |
| 49 | + * @param vertices Array of 3D vertex positions in world space |
| 50 | + * @param indices Triangle indices (every 3 values form a triangle) |
| 51 | + * @param confidences Per-vertex confidence values (0.0-1.0) |
| 52 | + */ |
| 53 | + VROARDepthMesh(std::vector<VROVector3f> vertices, |
| 54 | + std::vector<int> indices, |
| 55 | + std::vector<float> confidences); |
| 56 | + |
| 57 | + ~VROARDepthMesh(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Get the vertex positions in world space. |
| 61 | + */ |
| 62 | + const std::vector<VROVector3f>& getVertices() const { return _vertices; } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get the triangle indices. Every 3 values form a triangle. |
| 66 | + */ |
| 67 | + const std::vector<int>& getIndices() const { return _indices; } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the per-vertex confidence values (0.0-1.0). |
| 71 | + */ |
| 72 | + const std::vector<float>& getConfidences() const { return _confidences; } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get the number of vertices in the mesh. |
| 76 | + */ |
| 77 | + int getVertexCount() const { return static_cast<int>(_vertices.size()); } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get the number of triangles in the mesh. |
| 81 | + */ |
| 82 | + int getTriangleCount() const { return static_cast<int>(_indices.size() / 3); } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the average confidence across all vertices. |
| 86 | + */ |
| 87 | + float getAverageConfidence() const; |
| 88 | + |
| 89 | + /** |
| 90 | + * Check if the mesh is valid (has vertices and valid triangle indices). |
| 91 | + */ |
| 92 | + bool isValid() const; |
| 93 | + |
| 94 | +private: |
| 95 | + std::vector<VROVector3f> _vertices; |
| 96 | + std::vector<int> _indices; |
| 97 | + std::vector<float> _confidences; |
| 98 | +}; |
| 99 | + |
| 100 | +#endif /* VROARDepthMesh_h */ |
0 commit comments