Lagrange
RayCasterMesh.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13#include <lagrange/common.h>
14#include <lagrange/utils/assert.h>
15#include <lagrange/utils/safe_cast.h>
16
17namespace lagrange {
18namespace raycasting {
19
20
22{
23public:
24 using Index = size_t;
25
26 virtual ~RaycasterMesh() {}
27
28 virtual Index get_dim() const = 0;
29 virtual Index get_vertex_per_facet() const = 0;
30 virtual Index get_num_vertices() const = 0;
31 virtual Index get_num_facets() const = 0;
32
33 virtual std::vector<float> vertices_to_float() const = 0;
34 virtual std::vector<unsigned> indices_to_int() const = 0;
35
36 virtual void vertices_to_float(float* buf) const = 0; // buf must be preallocated
37 virtual void indices_to_int(unsigned* buf) const = 0; // buf must be preallocated
38};
39
40
41template <typename MeshType>
43{
44public:
45 using Parent = RaycasterMesh;
46 using Index = Parent::Index;
47
48 RaycasterMeshDerived(std::shared_ptr<MeshType> mesh)
49 : m_mesh(std::move(mesh))
50 {
51 la_runtime_assert(m_mesh, "Mesh cannot be null");
52 }
53
54 std::shared_ptr<MeshType> get_mesh_ptr() const { return m_mesh; }
55
56 Index get_dim() const override { return m_mesh->get_dim(); }
57 Index get_vertex_per_facet() const override { return m_mesh->get_vertex_per_facet(); }
58 Index get_num_vertices() const override { return m_mesh->get_num_vertices(); };
59 Index get_num_facets() const override { return m_mesh->get_num_facets(); };
60
61 std::vector<float> vertices_to_float() const override
62 {
63 auto& data = m_mesh->get_vertices();
64 const Index rows = safe_cast<Index>(data.rows());
65 const Index cols = (get_dim() == 3 ? safe_cast<Index>(data.cols()) : 3);
66 const Index size = rows * cols;
67
68 // Due to Embree bug, we have to reserve space for one extra entry.
69 // See https://github.com/embree/embree/issues/124
70 std::vector<float> float_data;
71 float_data.reserve(size + 1);
72 float_data.resize(size);
73 vertices_to_float(float_data.data());
74
75 return float_data;
76 }
77
78 std::vector<unsigned> indices_to_int() const override
79 {
80 auto& data = m_mesh->get_facets();
81 const Index rows = safe_cast<Index>(data.rows());
82 const Index cols = safe_cast<Index>(data.cols());
83 const Index size = rows * cols;
84
85 // Due to Embree bug, we have to reserve space for one extra entry.
86 // See https://github.com/embree/embree/issues/124
87 std::vector<unsigned> int_data;
88 int_data.reserve(size + 1);
89 int_data.resize(size);
90 indices_to_int(int_data.data());
91
92 return int_data;
93 }
94
95 void vertices_to_float(float* buf) const override
96 {
97 auto& data = m_mesh->get_vertices();
98 const Index rows = safe_cast<Index>(data.rows());
99
100 if (get_dim() == 3) {
101 const Index cols = safe_cast<Index>(data.cols());
102 for (Index i = 0; i < rows; ++i) {
103 for (Index j = 0; j < cols; ++j) {
104 *(buf++) = safe_cast<float>(data(i, j));
105 }
106 }
107 } else if (get_dim() == 2) {
108 for (Index i = 0; i < rows; ++i) {
109 for (Index j = 0; j < 2; ++j) {
110 *(buf++) = safe_cast<float>(data(i, j));
111 }
112 *(buf++) = 0.0f;
113 }
114 }
115 }
116
117 void indices_to_int(unsigned* buf) const override
118 {
119 auto& data = m_mesh->get_facets();
120 const Index rows = safe_cast<Index>(data.rows());
121 const Index cols = safe_cast<Index>(data.cols());
122 for (Index i = 0; i < rows; ++i) {
123 for (Index j = 0; j < cols; ++j) {
124 *(buf++) = safe_cast<unsigned>(data(i, j));
125 }
126 }
127 }
128
129 std::shared_ptr<MeshType> m_mesh;
130};
131
132} // namespace raycasting
133} // namespace lagrange
Definition: RayCasterMesh.h:43
Definition: RayCasterMesh.h:22
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30