GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
valence_cache.h
Go to the documentation of this file.
1// Copyright 2016 The Draco Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15#ifndef DRACO_MESH_VALENCE_CACHE_H_
16#define DRACO_MESH_VALENCE_CACHE_H_
17
20#include "draco/core/macros.h"
21
22namespace draco {
23
24// ValenceCache provides support for the caching of valences off of some kind of
25// CornerTable 'type' of class.
26// No valences should be queried before Caching is
27// performed and values should be removed/recached when changes to the
28// underlying mesh are taking place.
29
30template <class CornerTableT>
32 const CornerTableT &table_;
33
34 public:
35 explicit ValenceCache(const CornerTableT &table) : table_(table) {}
36
37 // Do not call before CacheValences() / CacheValencesInaccurate().
38 inline int8_t ValenceFromCacheInaccurate(CornerIndex c) const {
39 if (c == kInvalidCornerIndex) {
40 return -1;
41 }
42 return ValenceFromCacheInaccurate(table_.Vertex(c));
43 }
44 inline int32_t ValenceFromCache(CornerIndex c) const {
45 if (c == kInvalidCornerIndex) {
46 return -1;
47 }
48 return ValenceFromCache(table_.Vertex(c));
49 }
50
51 inline int32_t ConfidentValenceFromCache(VertexIndex v) const {
52 DRACO_DCHECK_LT(v.value(), table_.num_vertices());
53 DRACO_DCHECK_EQ(vertex_valence_cache_32_bit_.size(), table_.num_vertices());
54 return vertex_valence_cache_32_bit_[v];
55 }
56
57 // Collect the valence for all vertices so they can be reused later. The
58 // 'inaccurate' versions of this family of functions clips the true valence
59 // of the vertices to 8 signed bits as a space optimization. This clipping
60 // will lead to occasionally wrong results. If accurate results are required
61 // under all circumstances, do not use the 'inaccurate' version or else
62 // use it and fetch the correct result in the event the value appears clipped.
63 // The topology of the mesh should be a constant when Valence Cache functions
64 // are being used. Modification of the mesh while cache(s) are filled will
65 // not guarantee proper results on subsequent calls unless they are rebuilt.
67 if (vertex_valence_cache_8_bit_.size() == 0) {
68 const VertexIndex vertex_count = VertexIndex(table_.num_vertices());
69 vertex_valence_cache_8_bit_.resize(vertex_count.value());
70 for (VertexIndex v = VertexIndex(0); v < vertex_count; v += 1) {
71 vertex_valence_cache_8_bit_[v] = static_cast<int8_t>(
72 (std::min)(static_cast<int32_t>(std::numeric_limits<int8_t>::max()),
73 table_.Valence(v)));
74 }
75 }
76 }
77 void CacheValences() const {
78 if (vertex_valence_cache_32_bit_.size() == 0) {
79 const VertexIndex vertex_count = VertexIndex(table_.num_vertices());
80 vertex_valence_cache_32_bit_.resize(vertex_count.value());
81 for (VertexIndex v = VertexIndex(0); v < vertex_count; v += 1) {
82 vertex_valence_cache_32_bit_[v] = table_.Valence(v);
83 }
84 }
85 }
86
88 DRACO_DCHECK_GE(c.value(), 0);
89 return ConfidentValenceFromCacheInaccurate(table_.ConfidentVertex(c));
90 }
91 inline int32_t ConfidentValenceFromCache(CornerIndex c) const {
92 DRACO_DCHECK_GE(c.value(), 0);
93 return ConfidentValenceFromCache(table_.ConfidentVertex(c));
94 }
95 inline int8_t ValenceFromCacheInaccurate(VertexIndex v) const {
96 DRACO_DCHECK_EQ(vertex_valence_cache_8_bit_.size(), table_.num_vertices());
97 if (v == kInvalidVertexIndex || v.value() >= table_.num_vertices()) {
98 return -1;
99 }
101 }
103 DRACO_DCHECK_LT(v.value(), table_.num_vertices());
104 DRACO_DCHECK_EQ(vertex_valence_cache_8_bit_.size(), table_.num_vertices());
105 return vertex_valence_cache_8_bit_[v];
106 }
107
108 // TODO(draco-eng) Add unit tests for ValenceCache functions.
109 inline int32_t ValenceFromCache(VertexIndex v) const {
110 DRACO_DCHECK_EQ(vertex_valence_cache_32_bit_.size(), table_.num_vertices());
111 if (v == kInvalidVertexIndex || v.value() >= table_.num_vertices()) {
112 return -1;
113 }
115 }
116
117 // Clear the cache of valences and deallocate the memory.
119 vertex_valence_cache_8_bit_.clear();
120 // Force erasure.
121 IndexTypeVector<VertexIndex, int8_t>().swap(vertex_valence_cache_8_bit_);
122 }
123 void ClearValenceCache() const {
124 vertex_valence_cache_32_bit_.clear();
125 // Force erasure.
126 IndexTypeVector<VertexIndex, int32_t>().swap(vertex_valence_cache_32_bit_);
127 }
128
129 bool IsCacheEmpty() const {
130 return vertex_valence_cache_8_bit_.size() == 0 &&
131 vertex_valence_cache_32_bit_.size() == 0;
132 }
133
134 private:
135 // Retain valences and clip them to char size.
136 mutable IndexTypeVector<VertexIndex, int8_t> vertex_valence_cache_8_bit_;
137 mutable IndexTypeVector<VertexIndex, int32_t> vertex_valence_cache_32_bit_;
138};
139
140} // namespace draco
141
142#endif // DRACO_MESH_VALENCE_CACHE_H_
Definition draco_index_type_vector.h:32
void swap(IndexTypeVector< IndexTypeT, ValueTypeT > &arg)
Definition draco_index_type_vector.h:55
int8_t ConfidentValenceFromCacheInaccurate(CornerIndex c) const
Definition valence_cache.h:87
void ClearValenceCacheInaccurate() const
Definition valence_cache.h:118
int8_t ValenceFromCacheInaccurate(CornerIndex c) const
Definition valence_cache.h:38
int32_t ValenceFromCache(VertexIndex v) const
Definition valence_cache.h:109
int8_t ConfidentValenceFromCacheInaccurate(VertexIndex v) const
Definition valence_cache.h:102
int8_t ValenceFromCacheInaccurate(VertexIndex v) const
Definition valence_cache.h:95
void CacheValences() const
Definition valence_cache.h:77
int32_t ConfidentValenceFromCache(CornerIndex c) const
Definition valence_cache.h:91
int32_t ValenceFromCache(CornerIndex c) const
Definition valence_cache.h:44
void ClearValenceCache() const
Definition valence_cache.h:123
ValenceCache(const CornerTableT &table)
Definition valence_cache.h:35
int32_t ConfidentValenceFromCache(VertexIndex v) const
Definition valence_cache.h:51
void CacheValencesInaccurate() const
Definition valence_cache.h:66
bool IsCacheEmpty() const
Definition valence_cache.h:129
#define DRACO_DCHECK_LT(a, b)
Definition macros.h:86
#define DRACO_DCHECK_EQ(a, b)
Definition macros.h:81
#define DRACO_DCHECK_GE(a, b)
Definition macros.h:83
Definition attribute_octahedron_transform.cc:21
VertexIndex VertexIndex
Definition mesh_edgebreaker_encoder_impl.cc:37
CornerIndex CornerIndex
Definition mesh_edgebreaker_encoder_impl.cc:35