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

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

/** \file
 * \ingroup freestyle
 * \brief Convenient access to the steerable ViewMap to which any element of the ViewMap belongs
 * to.
 */

#include <sstream>

#include "Silhouette.h"
#include "SteerableViewMap.h"

#include "../geometry/Geom.h"

#include "../image/Image.h"
#include "../image/ImagePyramid.h"

#include "BLI_math.h"
#include "BLI_sys_types.h"

#include "BKE_global.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

namespace Freestyle {

using namespace Geometry;

SteerableViewMap::SteerableViewMap(uint nbOrientations)
{
  _nbOrientations = nbOrientations;
  _bound = cos(M_PI / float(_nbOrientations));
  for (uint i = 0; i < _nbOrientations; ++i) {
    _directions.emplace_back(cos(float(i) * M_PI / float(_nbOrientations)),
                             sin(float(i) * M_PI / float(_nbOrientations)));
  }
  Build();
}

void SteerableViewMap::Build()
{
  _imagesPyramids =
      new ImagePyramid *[_nbOrientations + 1];  // one more map to store the complete visible VM
  memset((_imagesPyramids), 0, (_nbOrientations + 1) * sizeof(ImagePyramid *));
}

SteerableViewMap::SteerableViewMap(const SteerableViewMap &iBrother)
{
  _nbOrientations = iBrother._nbOrientations;
  uint i;
  _bound = iBrother._bound;
  _directions = iBrother._directions;
  _mapping = iBrother._mapping;
  _imagesPyramids =
      new ImagePyramid *[_nbOrientations + 1];  // one more map to store the complete visible VM
  for (i = 0; i <= _nbOrientations; ++i) {
    _imagesPyramids[i] = new GaussianPyramid(
        *(dynamic_cast<GaussianPyramid *>(iBrother._imagesPyramids[i])));
  }
}

SteerableViewMap::~SteerableViewMap()
{
  Clear();
}

void SteerableViewMap::Clear()
{
  uint i;
  if (_imagesPyramids) {
    for (i = 0; i <= _nbOrientations; ++i) {
      if (_imagesPyramids[i]) {
        delete (_imagesPyramids)[i];
      }
    }
    delete[] _imagesPyramids;
    _imagesPyramids = nullptr;
  }
  if (!_mapping.empty()) {
    for (map<uint, double *>::iterator m = _mapping.begin(), mend = _mapping.end(); m != mend;
         ++m) {
      delete[](*m).second;
    }
    _mapping.clear();
  }
}

void SteerableViewMap::Reset()
{
  Clear();
  Build();
}

double SteerableViewMap::ComputeWeight(const Vec2d &dir, uint i)
{
  double dotp = fabs(dir * _directions[i]);
  if (dotp < _bound) {
    return 0.0;
  }
  if (dotp > 1.0) {
    dotp = 1.0;
  }

  return cos(float(_nbOrientations) / 2.0 * acos(dotp));
}

double *SteerableViewMap::AddFEdge(FEdge *iFEdge)
{
  uint i;
  uint id = iFEdge->getId().getFirst();
  map<uint, double *>::iterator o = _mapping.find(id);
  if (o != _mapping.end()) {
    return (*o).second;
  }
  double *res = new double[_nbOrientations];
  for (i = 0; i < _nbOrientations; ++i) {
    res[i] = 0.0;
  }
  Vec3r o2d3 = iFEdge->orientation2d();
  Vec2r o2d2(o2d3.x(), o2d3.y());
  real norm = o2d2.norm();
  if (norm < 1.0e-6) {
    return res;
  }
  o2d2 /= norm;

  for (i = 0; i < _nbOrientations; ++i) {
    res[i] = ComputeWeight(o2d2, i);
  }
  _mapping[id] = res;
  return res;
}

uint SteerableViewMap::getSVMNumber(Vec2f dir)
{
  // soc unsigned res = 0;
  real norm = dir.norm();
  if (norm < 1.0e-6) {
    return _nbOrientations + 1;
  }
  dir /= norm;
  double maxw = 0.0f;
  uint winner = _nbOrientations + 1;
  for (uint i = 0; i < _nbOrientations; ++i) {
    double w = ComputeWeight(dir, i);
    if (w > maxw) {
      maxw = w;
      winner = i;
    }
  }
  return winner;
}

uint SteerableViewMap::getSVMNumber(uint id)
{
  map<uint, double *>::iterator o = _mapping.find(id);
  if (o != _mapping.end()) {
    double *wvalues = (*o).second;
    double maxw = 0.0;
    uint winner = _nbOrientations + 1;
    for (uint i = 0; i < _nbOrientations; ++i) {
      double w = wvalues[i];
      if (w > maxw) {
        maxw = w;
        winner = i;
      }
    }
    return winner;
  }
  return _nbOrientations + 1;
}

void SteerableViewMap::buildImagesPyramids(GrayImage **steerableBases,
                                           bool copy,
                                           uint iNbLevels,
                                           float iSigma)
{
  for (uint i = 0; i <= _nbOrientations; ++i) {
    ImagePyramid *svm = (_imagesPyramids)[i];
    delete svm;
    if (copy) {
      svm = new GaussianPyramid(*(steerableBases[i]), iNbLevels, iSigma);
    }
    else {
      svm = new GaussianPyramid(steerableBases[i], iNbLevels, iSigma);
    }
    _imagesPyramids[i] = svm;
  }
}

float SteerableViewMap::readSteerableViewMapPixel(uint iOrientation, int iLevel, int x, int y)
{
  ImagePyramid *pyramid = _imagesPyramids[iOrientation];
  if (!pyramid) {
    if (G.debug & G_DEBUG_FREESTYLE) {
      cout << "Warning: this steerable ViewMap level doesn't exist" << endl;
    }
    return 0.0f;
  }
  if ((x < 0) || (x >= pyramid->width()) || (y < 0) || (y >= pyramid->height())) {
    return 0;
  }
  // float v = pyramid->pixel(x, pyramid->height() - 1 - y, iLevel) * 255.0f;
  // We encode both the directionality and the lines counting on 8 bits (because of frame buffer).
  // Thus, we allow until 8 lines to pass through the same pixel, so that we can discretize the
  // Pi/_nbOrientations angle into 32 slices. Therefore, for example, in the vertical direction, a
  // vertical line will have the value 32 on each pixel it passes through.
  float v = pyramid->pixel(x, pyramid->height() - 1 - y, iLevel) / 32.0f;
  return v;
}

float SteerableViewMap::readCompleteViewMapPixel(int iLevel, int x, int y)
{
  return readSteerableViewMapPixel(_nbOrientations, iLevel, x, y);
}

uint SteerableViewMap::getNumberOfPyramidLevels() const
{
  if (_imagesPyramids[0]) {
    return _imagesPyramids[0]->getNumberOfLevels();
  }
  return 0;
}

void SteerableViewMap::saveSteerableViewMap() const
{
  for (uint i = 0; i <= _nbOrientations; ++i) {
    if (_imagesPyramids[i] == nullptr) {
      cerr << "SteerableViewMap warning: orientation " << i
           << " of steerable View Map whas not been computed yet" << endl;
      continue;
    }
    int ow = _imagesPyramids[i]->width(0);
    int oh = _imagesPyramids[i]->height(0);

    // soc QString base("SteerableViewMap");
    string base("SteerableViewMap");
    stringstream filename;

    for (int j = 0; j < _imagesPyramids[i]->getNumberOfLevels(); ++j) {  // soc
      float coeff = 1.0f;  // 1 / 255.0f; // 100 * 255; // * pow(2, j);
      // soc QImage qtmp(ow, oh, QImage::Format_RGB32);
      ImBuf *ibuf = IMB_allocImBuf(ow, oh, 32, IB_rect);
      int rowbytes = ow * 4;
      char *pix;

      for (int y = 0; y < oh; ++y) {    // soc
        for (int x = 0; x < ow; ++x) {  // soc
          int c = int(coeff * _imagesPyramids[i]->pixel(x, y, j));
          if (c > 255) {
            c = 255;
          }
          // int c = (int)(_imagesPyramids[i]->pixel(x, y, j));

          // soc qtmp.setPixel(x, y, qRgb(c, c, c));
          pix = (char *)ibuf->rect + y * rowbytes + x * 4;
          pix[0] = pix[1] = pix[2] = c;
        }
      }

      // soc qtmp.save(base+QString::number(i)+"-"+QString::number(j)+".png", "PNG");
      filename << base;
      filename << i << "-" << j << ".png";
      ibuf->ftype = IMB_FTYPE_PNG;
      IMB_saveiff(ibuf, const_cast<char *>(filename.str().c_str()), 0);
    }
#if 0
    QString base("SteerableViewMap");
    for (unsigned j = 0; j < _imagesPyramids[i]->getNumberOfLevels(); ++j) {
      GrayImage *img = _imagesPyramids[i]->getLevel(j);
      int ow = img->width();
      int oh = img->height();
      float coeff = 1.0f;  // 100 * 255; // * pow(2, j);
      QImage qtmp(ow, oh, 32);
      for (unsigned int y = 0; y < oh; ++y) {
        for (unsigned int x = 0; x < ow; ++x) {
          int c = (int)(coeff * img->pixel(x, y));
          if (c > 255) {
            c = 255;
          }
          //int c = (int)(_imagesPyramids[i]->pixel(x, y, j));
          qtmp.setPixel(x, y, qRgb(c, c, c));
        }
      }
      qtmp.save(base + QString::number(i) + "-" + QString::number(j) + ".png", "PNG");
    }
#endif
  }
}

} /* namespace Freestyle */