Lagrange
Loading...
Searching...
No Matches
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/legacy/inline.h>
15#include <lagrange/utils/assert.h>
16#include <lagrange/utils/safe_cast.h>
17
18namespace lagrange {
19namespace raycasting {
20LAGRANGE_LEGACY_INLINE
21namespace legacy {
22
23
25{
26public:
27 using Index = size_t;
28
29 virtual ~RaycasterMesh() {}
30
31 virtual Index get_dim() const = 0;
32 virtual Index get_vertex_per_facet() const = 0;
33 virtual Index get_num_vertices() const = 0;
34 virtual Index get_num_facets() const = 0;
35
36 virtual std::vector<float> vertices_to_float() const = 0;
37 virtual std::vector<unsigned> indices_to_int() const = 0;
38
39 virtual void vertices_to_float(float* buf) const = 0; // buf must be preallocated
40 virtual void indices_to_int(unsigned* buf) const = 0; // buf must be preallocated
41};
42
43
44template <typename MeshType>
45class RaycasterMeshDerived : public RaycasterMesh
46{
47public:
48 using Parent = RaycasterMesh;
49 using Index = Parent::Index;
50
51 RaycasterMeshDerived(std::shared_ptr<MeshType> mesh)
52 : m_mesh(std::move(mesh))
53 {
54 la_runtime_assert(m_mesh, "Mesh cannot be null");
55 }
56
57 std::shared_ptr<MeshType> get_mesh_ptr() const { return m_mesh; }
58
59 Index get_dim() const override { return m_mesh->get_dim(); }
60 Index get_vertex_per_facet() const override { return m_mesh->get_vertex_per_facet(); }
61 Index get_num_vertices() const override { return m_mesh->get_num_vertices(); };
62 Index get_num_facets() const override { return m_mesh->get_num_facets(); };
63
64 std::vector<float> vertices_to_float() const override
65 {
66 auto& data = m_mesh->get_vertices();
67 const Index rows = safe_cast<Index>(data.rows());
68 const Index cols = (get_dim() == 3 ? safe_cast<Index>(data.cols()) : 3);
69 const Index size = rows * cols;
70
71 // Due to Embree bug, we have to reserve space for one extra entry.
72 // See https://github.com/embree/embree/issues/124
73 std::vector<float> float_data;
74 float_data.reserve(size + 1);
75 float_data.resize(size);
76 vertices_to_float(float_data.data());
77
78 return float_data;
79 }
80
81 std::vector<unsigned> indices_to_int() const override
82 {
83 auto& data = m_mesh->get_facets();
84 const Index rows = safe_cast<Index>(data.rows());
85 const Index cols = safe_cast<Index>(data.cols());
86 const Index size = rows * cols;
87
88 // Due to Embree bug, we have to reserve space for one extra entry.
89 // See https://github.com/embree/embree/issues/124
90 std::vector<unsigned> int_data;
91 int_data.reserve(size + 1);
92 int_data.resize(size);
93 indices_to_int(int_data.data());
94
95 return int_data;
96 }
97
98 void vertices_to_float(float* buf) const override
99 {
100 auto& data = m_mesh->get_vertices();
101 const Index rows = safe_cast<Index>(data.rows());
102
103 if (get_dim() == 3) {
104 const Index cols = safe_cast<Index>(data.cols());
105 for (Index i = 0; i < rows; ++i) {
106 for (Index j = 0; j < cols; ++j) {
107 *(buf++) = safe_cast<float>(data(i, j));
108 }
109 }
110 } else if (get_dim() == 2) {
111 for (Index i = 0; i < rows; ++i) {
112 for (Index j = 0; j < 2; ++j) {
113 *(buf++) = safe_cast<float>(data(i, j));
114 }
115 *(buf++) = 0.0f;
116 }
117 }
118 }
119
120 void indices_to_int(unsigned* buf) const override
121 {
122 auto& data = m_mesh->get_facets();
123 const Index rows = safe_cast<Index>(data.rows());
124 const Index cols = safe_cast<Index>(data.cols());
125 for (Index i = 0; i < rows; ++i) {
126 for (Index j = 0; j < cols; ++j) {
127 *(buf++) = safe_cast<unsigned>(data(i, j));
128 }
129 }
130 }
131
132 std::shared_ptr<MeshType> m_mesh;
133};
134
135} // namespace legacy
136} // namespace raycasting
137} // namespace lagrange
Definition RayCasterMesh.h:25
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
constexpr auto safe_cast(SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
Perform safe cast from SourceType to TargetType, where "safe" means:
Definition safe_cast.h:50
Raycasting operations.
Definition ClosestPointResult.h:22
Main namespace for Lagrange.