Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GeomCleaner.cpp « geometry « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c17fb92a58c568c67f9d6e503e68f67c712debcf (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup freestyle
 * \brief Class to define a cleaner of geometry providing a set of useful tools
 */

#if 0
#  if defined(__GNUC__) && (__GNUC__ >= 3)
// hash_map is not part of the C++ standard anymore;
// hash_map.h has been kept though for backward compatibility
#    include <hash_map.h>
#  else
#    include <hash_map>
#  endif
#endif

#include <cstdio>
#include <list>
#include <map>

#include "GeomCleaner.h"

#include "../system/TimeUtils.h"

#include "BKE_global.h"

using namespace std;

namespace Freestyle {

void GeomCleaner::SortIndexedVertexArray(const float *iVertices,
                                         uint iVSize,
                                         const uint *iIndices,
                                         uint iISize,
                                         float **oVertices,
                                         uint **oIndices)
{
  // First, we build a list of IndexVertex:
  list<IndexedVertex> indexedVertices;
  uint i;
  for (i = 0; i < iVSize; i += 3) {
    indexedVertices.emplace_back(Vec3f(iVertices[i], iVertices[i + 1], iVertices[i + 2]), i / 3);
  }

  // q-sort
  indexedVertices.sort();

  // build the indices mapping array:
  uint *mapIndices = new uint[iVSize / 3];
  *oVertices = new float[iVSize];
  list<IndexedVertex>::iterator iv;
  uint newIndex = 0;
  uint vIndex = 0;
  for (iv = indexedVertices.begin(); iv != indexedVertices.end(); iv++) {
    // Build the final results:
    (*oVertices)[vIndex] = iv->x();
    (*oVertices)[vIndex + 1] = iv->y();
    (*oVertices)[vIndex + 2] = iv->z();

    mapIndices[iv->index()] = newIndex;
    newIndex++;
    vIndex += 3;
  }

  // Build the final index array:
  *oIndices = new uint[iISize];
  for (i = 0; i < iISize; i++) {
    (*oIndices)[i] = 3 * mapIndices[iIndices[i] / 3];
  }

  delete[] mapIndices;
}

void GeomCleaner::CompressIndexedVertexArray(const float *iVertices,
                                             uint iVSize,
                                             const uint *iIndices,
                                             uint iISize,
                                             float **oVertices,
                                             uint *oVSize,
                                             uint **oIndices)
{
  // First, we build a list of IndexVertex:
  vector<Vec3f> vertices;
  uint i;
  for (i = 0; i < iVSize; i += 3) {
    vertices.emplace_back(iVertices[i], iVertices[i + 1], iVertices[i + 2]);
  }

  uint *mapVertex = new uint[iVSize];
  vector<Vec3f>::iterator v = vertices.begin();

  vector<Vec3f> compressedVertices;
  Vec3f previous = *v;
  mapVertex[0] = 0;
  compressedVertices.push_back(vertices.front());

  v++;
  Vec3f current;
  i = 1;
  for (; v != vertices.end(); v++) {
    current = *v;
    if (current == previous) {
      mapVertex[i] = compressedVertices.size() - 1;
    }
    else {
      compressedVertices.push_back(current);
      mapVertex[i] = compressedVertices.size() - 1;
    }
    previous = current;
    i++;
  }

  // Builds the resulting vertex array:
  *oVSize = 3 * compressedVertices.size();
  *oVertices = new float[*oVSize];
  i = 0;
  for (v = compressedVertices.begin(); v != compressedVertices.end(); v++) {
    (*oVertices)[i] = (*v)[0];
    (*oVertices)[i + 1] = (*v)[1];
    (*oVertices)[i + 2] = (*v)[2];
    i += 3;
  }

  // Map the index array:
  *oIndices = new uint[iISize];
  for (i = 0; i < iISize; i++) {
    (*oIndices)[i] = 3 * mapVertex[iIndices[i] / 3];
  }

  delete[] mapVertex;
}

void GeomCleaner::SortAndCompressIndexedVertexArray(const float *iVertices,
                                                    uint iVSize,
                                                    const uint *iIndices,
                                                    uint iISize,
                                                    float **oVertices,
                                                    uint *oVSize,
                                                    uint **oIndices)
{
  // tmp arrays used to store the sorted data:
  float *tmpVertices;
  uint *tmpIndices;

  Chronometer chrono;
  // Sort data
  chrono.start();
  GeomCleaner::SortIndexedVertexArray(
      iVertices, iVSize, iIndices, iISize, &tmpVertices, &tmpIndices);
  if (G.debug & G_DEBUG_FREESTYLE) {
    printf("Sorting: %lf sec.\n", chrono.stop());
  }

  // compress data
  chrono.start();
  GeomCleaner::CompressIndexedVertexArray(
      tmpVertices, iVSize, tmpIndices, iISize, oVertices, oVSize, oIndices);
  real duration = chrono.stop();
  if (G.debug & G_DEBUG_FREESTYLE) {
    printf("Merging: %lf sec.\n", duration);
  }

  // deallocates memory:
  delete[] tmpVertices;
  delete[] tmpIndices;
}

/** Defines a hash table used for searching the Cells */
struct GeomCleanerHasher {
#define _MUL 950706376UL
#define _MOD 2147483647UL
  inline size_t operator()(const Vec3r &p) const
  {
    size_t res = ulong(p[0] * _MUL) % _MOD;
    res = (res + ulong(p[1]) * _MUL) % _MOD;
    return (res + ulong(p[2]) * _MUL) % _MOD;
  }
#undef _MUL
#undef _MOD
};

void GeomCleaner::CleanIndexedVertexArray(const float *iVertices,
                                          uint iVSize,
                                          const uint *iIndices,
                                          uint iISize,
                                          float **oVertices,
                                          uint *oVSize,
                                          uint **oIndices)
{
  using cleanHashTable = map<Vec3f, uint>;
  vector<Vec3f> vertices;
  uint i;
  for (i = 0; i < iVSize; i += 3) {
    vertices.emplace_back(iVertices[i], iVertices[i + 1], iVertices[i + 2]);
  }

  cleanHashTable ht;
  vector<uint> newIndices;
  vector<Vec3f> newVertices;

  // elimination of needless points
  uint currentIndex = 0;
  vector<Vec3f>::const_iterator v = vertices.begin();
  vector<Vec3f>::const_iterator end = vertices.end();
  cleanHashTable::const_iterator found;
  for (; v != end; v++) {
    found = ht.find(*v);
    if (found != ht.end()) {
      // The vertex is already in the new array.
      newIndices.push_back((*found).second);
    }
    else {
      newVertices.push_back(*v);
      newIndices.push_back(currentIndex);
      ht[*v] = currentIndex;
      currentIndex++;
    }
  }

  // creation of oVertices array:
  *oVSize = 3 * newVertices.size();
  *oVertices = new float[*oVSize];
  currentIndex = 0;
  end = newVertices.end();
  for (v = newVertices.begin(); v != end; v++) {
    (*oVertices)[currentIndex++] = (*v)[0];
    (*oVertices)[currentIndex++] = (*v)[1];
    (*oVertices)[currentIndex++] = (*v)[2];
  }

  // map new indices:
  *oIndices = new uint[iISize];
  for (i = 0; i < iISize; i++) {
    (*oIndices)[i] = 3 * newIndices[iIndices[i] / 3];
  }
}

} /* namespace Freestyle */