Lagrange
MeshTrait.h
1/*
2 * Copyright 2019 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
14#include <type_traits>
15
16namespace lagrange {
17namespace MeshTraitHelper {
18
19// It turns out that what we are doing here is pretty standard since
20// c++17! https://en.cppreference.com/w/cpp/types/void_t
21
22
23// This type allows asking for the type void only if certain types exist
24// Void<double, int>::type -> void
25// Void<IAmATypeThatDontExist>::type -> compiler error
26template <typename... Ts>
27struct Void
28{
29 using type = void;
30};
31
32// ================ Is mesh
33
34// This block of code defines is_mesh_helper<T, AnyOtherType>
35// to be std::false_type.
36// But since in this application we only set the AnyOtherType to void,
37// let's check it with a static_assert() anyway.
38template <typename T, typename _>
39struct is_mesh_helper : std::false_type
40{
41 static_assert(std::is_same<_, void>::value, "second template argument is always void");
42};
43
44// This block attemps to override is_mesh_helper<T, void> (i.e,
45// special case of AnyOtherType=void). This only succeeds if the
46// expression Void<typename T::VertexArray, typename T::FacetArray>::type,
47// is not erronous to the compiler (i.e., T::VertexArray and T::FacetArray
48// exist).
49template <typename MeshType>
52 typename Void<typename MeshType::VertexArray, typename MeshType::FacetArray>::type>
53 : public std::true_type
54{
55};
56
57// The trait to detect if a type T is a mesh.
58// We just use is_mesh_helper<T, void>.
59// If the compiler can specialize the second void specialization, we get
60// std::true_type. Otherwise, we get std::false_type.
61template <typename T>
63
64
65// ================ Is mesh smart ptr
66// Works very similar to Is mesh
67
68template <typename T, typename _>
69struct is_mesh_smart_ptr_helper : std::false_type
70{
71 static_assert(std::is_same<_, void>::value, "second template argument is always void");
72};
73
74template <typename MeshTypePtr>
76 MeshTypePtr,
77 typename Void<
78 typename MeshTypePtr::element_type,
79 typename MeshTypePtr::element_type::VertexArray,
80 typename MeshTypePtr::element_type::FacetArray>::type> : public std::true_type
81{
82};
83
84template <typename T>
86
87// ================ Is mesh raw ptr
88// We simply check if the type is ptr and then the type
89// it points to is a mesh.
90
91template <typename MeshTypePtr>
92using is_mesh_raw_ptr = std::conditional_t<
93 std::is_pointer<MeshTypePtr>::value && is_mesh<std::remove_pointer_t<MeshTypePtr>>::value,
94 std::true_type,
95 std::false_type>;
96
97} // End of namespace MeshTraitHelper
98
99
106template <typename MeshType_>
108{
109 using MeshType = typename std::remove_reference<typename std::remove_cv<MeshType_>::type>::type;
110
111 static constexpr bool is_mesh() { return lagrange::MeshTraitHelper::is_mesh<MeshType>::value; }
112
113 static constexpr bool is_mesh_smart_ptr()
114 {
116 }
117
118 static constexpr bool is_mesh_raw_ptr()
119 {
120 return lagrange::MeshTraitHelper::is_mesh_raw_ptr<MeshType>::value;
121 }
122
123 static constexpr bool is_mesh_ptr() { return is_mesh_raw_ptr() || is_mesh_smart_ptr(); }
124};
125
126} // namespace lagrange
Definition: Mesh.h:48
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: MeshTrait.h:28
MeshTrait class provide compiler check for different mesh types.
Definition: MeshTrait.h:108