1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// Copyright 2018 Blender Foundation. All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Author: Sergey Sharybin
#include "internal/topology/topology_refiner_impl.h"
#include "internal/base/type.h"
#include "internal/base/type_convert.h"
#include "internal/topology/mesh_topology.h"
#include "internal/topology/topology_refiner_impl.h"
#include "opensubdiv_converter_capi.h"
namespace blender {
namespace opensubdiv {
namespace {
const OpenSubdiv::Far::TopologyRefiner *getOSDTopologyRefiner(
const TopologyRefinerImpl *topology_refiner_impl)
{
return topology_refiner_impl->topology_refiner;
}
const OpenSubdiv::Far::TopologyLevel &getOSDTopologyBaseLevel(
const TopologyRefinerImpl *topology_refiner_impl)
{
return getOSDTopologyRefiner(topology_refiner_impl)->GetLevel(0);
}
////////////////////////////////////////////////////////////////////////////////
// Quick preliminary checks.
bool checkSchemeTypeMatches(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
const OpenSubdiv::Sdc::SchemeType converter_scheme_type =
blender::opensubdiv::getSchemeTypeFromCAPI(converter->getSchemeType(converter));
return (converter_scheme_type == getOSDTopologyRefiner(topology_refiner_impl)->GetSchemeType());
}
bool checkOptionsMatches(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
typedef OpenSubdiv::Sdc::Options Options;
const Options options = getOSDTopologyRefiner(topology_refiner_impl)->GetSchemeOptions();
const Options::FVarLinearInterpolation fvar_interpolation = options.GetFVarLinearInterpolation();
const Options::FVarLinearInterpolation converter_fvar_interpolation =
blender::opensubdiv::getFVarLinearInterpolationFromCAPI(
converter->getFVarLinearInterpolation(converter));
if (fvar_interpolation != converter_fvar_interpolation) {
return false;
}
return true;
}
bool checkGeometryCountersMatches(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
using OpenSubdiv::Far::TopologyLevel;
const TopologyLevel &base_level = getOSDTopologyBaseLevel(topology_refiner_impl);
return ((converter->getNumVertices(converter) == base_level.GetNumVertices()) &&
(converter->getNumEdges(converter) == base_level.GetNumEdges()) &&
(converter->getNumFaces(converter) == base_level.GetNumFaces()));
}
bool checkPreliminaryMatches(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
return checkSchemeTypeMatches(topology_refiner_impl, converter) &&
checkOptionsMatches(topology_refiner_impl, converter) &&
checkGeometryCountersMatches(topology_refiner_impl, converter);
}
////////////////////////////////////////////////////////////////////////////////
// Geometry comparison.
// A thin wrapper around index like array which does cyclic access. This means,
// it basically does indices[requested_index % num_indices].
//
// NOTE: This array does not own the memory.
//
// TODO(sergey): Consider moving this to a more reusable place.
class CyclicArray {
public:
typedef int value_type;
typedef int size_type;
static constexpr size_type npos = -1;
explicit CyclicArray(const std::vector<int> &data) : data_(data.data()), size_(data.size())
{
}
explicit CyclicArray(const OpenSubdiv::Far::ConstIndexArray &data)
: data_(&data[0]), size_(data.size())
{
}
inline value_type operator[](int index) const
{
assert(index >= 0);
// TODO(sergey): Check whether doing check for element index exceeding total
// number of indices prior to modulo helps performance.
return data_[index % size()];
}
inline size_type size() const
{
return size_;
}
// Find index of first occurrence of a given value.
inline size_type find(const value_type value) const
{
const int num_indices = size();
for (size_type i = 0; i < num_indices; ++i) {
if (value == (*this)[i]) {
return i;
}
}
return npos;
}
protected:
const value_type *data_;
const size_type size_;
};
bool compareCyclicForward(const CyclicArray &array_a,
const int start_a,
const CyclicArray &array_b,
const int start_b)
{
const int num_elements = array_a.size();
for (int i = 0; i < num_elements; ++i) {
if (array_a[start_a + i] != array_b[start_b + i]) {
return false;
}
}
return true;
}
bool compareCyclicBackward(const CyclicArray &array_a,
const int start_a,
const CyclicArray &array_b,
const int start_b)
{
const int num_elements = array_a.size();
// TODO(sergey): Some optimization might be possible with memcmp trickery.
for (int i = 0; i < num_elements; ++i) {
if (array_a[start_a + (num_elements - i - 1)] != array_b[start_b + (num_elements - i - 1)]) {
return false;
}
}
return true;
}
// Utility function dedicated for checking whether whether vertices indices
// used by two faces match.
// The tricky part here is that we can't trust 1:1 array match here, since it's
// possible that OpenSubdiv oriented edges of a face to make it compatible with
// an internal representation of non-manifold meshes.
//
// TODO(sergey): Check whether this is needed, ot whether OpenSubdiv is only
// creating edges in a proper orientation without modifying indices of face
// vertices.
bool checkVerticesOfFacesMatch(const CyclicArray &indices_a, const CyclicArray &indices_b)
{
if (indices_a.size() != indices_b.size()) {
return false;
}
// "Align" the arrays so we know first matched element.
const int start_b = indices_b.find(indices_a[0]);
if (start_b == indices_b.npos) {
return false;
}
// Check match in both directions, for the case OpenSubdiv did orient face in
// a way which made normals more consistent internally.
if (compareCyclicForward(indices_a, 0, indices_b, start_b)) {
return true;
}
if (compareCyclicBackward(indices_a, 0, indices_b, start_b)) {
return true;
}
return false;
}
bool checkGeometryFacesMatch(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
using OpenSubdiv::Far::ConstIndexArray;
using OpenSubdiv::Far::TopologyLevel;
const TopologyLevel &base_level = getOSDTopologyBaseLevel(topology_refiner_impl);
const int num_faces = base_level.GetNumFaces();
// TODO(sergey): Consider using data structure which keeps handful of
// elements on stack before doing heep allocation.
vector<int> conv_face_vertices;
for (int face_index = 0; face_index < num_faces; ++face_index) {
const ConstIndexArray &face_vertices = base_level.GetFaceVertices(face_index);
const int num_face_vertices = face_vertices.size();
if (num_face_vertices != converter->getNumFaceVertices(converter, face_index)) {
return false;
}
conv_face_vertices.resize(num_face_vertices);
converter->getFaceVertices(converter, face_index, &conv_face_vertices[0]);
if (!checkVerticesOfFacesMatch(CyclicArray(conv_face_vertices), CyclicArray(face_vertices))) {
return false;
}
}
return true;
}
bool checkGeometryMatches(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
// NOTE: Since OpenSubdiv's topology refiner doesn't contain loose edges, we
// are only checking for faces to be matched. Changes in edges we don't care
// here too much (they'll be checked for creases changes later).
return checkGeometryFacesMatch(topology_refiner_impl, converter);
}
////////////////////////////////////////////////////////////////////////////////
// Compare attributes which affects on topology.
bool checkSingleUVLayerMatch(const OpenSubdiv::Far::TopologyLevel &base_level,
const OpenSubdiv_Converter *converter,
const int layer_index)
{
converter->precalcUVLayer(converter, layer_index);
const int num_faces = base_level.GetNumFaces();
// TODO(sergey): Need to check whether converter changed the winding of
// face to match OpenSubdiv's expectations.
for (int face_index = 0; face_index < num_faces; ++face_index) {
OpenSubdiv::Far::ConstIndexArray base_level_face_uvs = base_level.GetFaceFVarValues(
face_index, layer_index);
for (int corner = 0; corner < base_level_face_uvs.size(); ++corner) {
const int uv_index = converter->getFaceCornerUVIndex(converter, face_index, corner);
if (base_level_face_uvs[corner] != uv_index) {
converter->finishUVLayer(converter);
return false;
}
}
}
converter->finishUVLayer(converter);
return true;
}
bool checkUVLayersMatch(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
using OpenSubdiv::Far::TopologyLevel;
const int num_layers = converter->getNumUVLayers(converter);
const TopologyLevel &base_level = getOSDTopologyBaseLevel(topology_refiner_impl);
// Number of UV layers should match.
if (base_level.GetNumFVarChannels() != num_layers) {
return false;
}
for (int layer_index = 0; layer_index < num_layers; ++layer_index) {
if (!checkSingleUVLayerMatch(base_level, converter, layer_index)) {
return false;
}
}
return true;
}
bool checkTopologyAttributesMatch(const TopologyRefinerImpl *topology_refiner_impl,
const OpenSubdiv_Converter *converter)
{
if (!topology_refiner_impl->base_mesh_topology.isEqualToConverter(converter)) {
return false;
}
return checkUVLayersMatch(topology_refiner_impl, converter);
}
} // namespace
bool TopologyRefinerImpl::isEqualToConverter(const OpenSubdiv_Converter *converter) const
{
return (blender::opensubdiv::checkPreliminaryMatches(this, converter) &&
blender::opensubdiv::checkGeometryMatches(this, converter) &&
blender::opensubdiv::checkTopologyAttributesMatch(this, converter));
}
} // namespace opensubdiv
} // namespace blender
|