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

WXEdge.cpp « winged_edge « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cef1a8f8f7787050bfa7c83cb8e71366f7772559 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * 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.
 */

/** \file
 * \ingroup freestyle
 * \brief Classes to define an Extended Winged Edge data structure.
 */

#include "WXEdge.h"
#include "BKE_global.h"

namespace Freestyle {

/**********************************
 *                                *
 *                                *
 *             WXFace             *
 *                                *
 *                                *
 **********************************/

unsigned int WXFaceLayer::Get0VertexIndex() const
{
  int i = 0;
  int nEdges = _pWXFace->numberOfEdges();
  for (i = 0; i < nEdges; ++i) {
    if (_DotP[i] == 0.0f) {  // TODO: this comparison is weak, check if it actually works
      return i;
    }
  }
  return -1;
}
unsigned int WXFaceLayer::GetSmoothEdgeIndex() const
{
  int i = 0;
  int nEdges = _pWXFace->numberOfEdges();
  for (i = 0; i < nEdges; ++i) {
    if ((_DotP[i] == 0.0f) && (_DotP[(i + 1) % nEdges] == 0.0f)) {  // TODO: ditto
      return i;
    }
  }
  return -1;
}

void WXFaceLayer::RetrieveCuspEdgesIndices(vector<int> &oCuspEdges)
{
  int i = 0;
  int nEdges = _pWXFace->numberOfEdges();
  for (i = 0; i < nEdges; ++i) {
    if (_DotP[i] * _DotP[(i + 1) % nEdges] < 0.0f) {
      // we got one
      oCuspEdges.push_back(i);
    }
  }
}

WXSmoothEdge *WXFaceLayer::BuildSmoothEdge()
{
  // if the smooth edge has already been built: exit
  if (_pSmoothEdge) {
    return _pSmoothEdge;
  }
  float ta, tb;
  WOEdge *woea(nullptr), *woeb(nullptr);
  bool ok = false;
  vector<int> cuspEdgesIndices;
  int indexStart, indexEnd;
  unsigned nedges = _pWXFace->numberOfEdges();
  if (_nNullDotP == nedges) {
    _pSmoothEdge = nullptr;
    return _pSmoothEdge;
  }
  if ((_nPosDotP != 0) && (_nPosDotP != _DotP.size()) && (_nNullDotP == 0)) {
    // that means that we have a smooth edge that starts from an edge and ends at an edge
    //-----------------------------
    // We retrieve the 2 edges for which we have opposite signs for each extremity
    RetrieveCuspEdgesIndices(cuspEdgesIndices);
    if (cuspEdgesIndices.size() != 2) {  // we necessarly have 2 cusp edges
      return nullptr;
    }

    // let us determine which cusp edge corresponds to the starting:
    // We can do that because we defined that a silhouette edge had the back facing part on its
    // right. So if the WOEdge woea is such that woea[0].dotp > 0 and woea[1].dotp < 0, it is the
    // starting edge.
    //-------------------------------------------

    if (_DotP[cuspEdgesIndices[0]] > 0.0f) {
      woea = _pWXFace->GetOEdge(cuspEdgesIndices[0]);
      woeb = _pWXFace->GetOEdge(cuspEdgesIndices[1]);
      indexStart = cuspEdgesIndices[0];
      indexEnd = cuspEdgesIndices[1];
    }
    else {
      woea = _pWXFace->GetOEdge(cuspEdgesIndices[1]);
      woeb = _pWXFace->GetOEdge(cuspEdgesIndices[0]);
      indexStart = cuspEdgesIndices[1];
      indexEnd = cuspEdgesIndices[0];
    }

    // Compute the interpolation:
    ta = _DotP[indexStart] / (_DotP[indexStart] - _DotP[(indexStart + 1) % nedges]);
    tb = _DotP[indexEnd] / (_DotP[indexEnd] - _DotP[(indexEnd + 1) % nedges]);
    ok = true;
  }
  else if (_nNullDotP == 1) {
    // that means that we have exactly one of the 2 extremities of our silhouette edge is a vertex
    // of the mesh
    if (ELEM(_nPosDotP, 2, 0)) {
      _pSmoothEdge = nullptr;
      return _pSmoothEdge;
    }
    RetrieveCuspEdgesIndices(cuspEdgesIndices);
    // We should have only one EdgeCusp:
    if (cuspEdgesIndices.size() != 1) {
      if (G.debug & G_DEBUG_FREESTYLE) {
        cout << "Warning in BuildSmoothEdge: weird WXFace configuration" << endl;
      }
      _pSmoothEdge = nullptr;
      return nullptr;
    }
    unsigned index0 = Get0VertexIndex();  // retrieve the 0 vertex index
    unsigned nedges = _pWXFace->numberOfEdges();
    if (_DotP[cuspEdgesIndices[0]] > 0.0f) {
      woea = _pWXFace->GetOEdge(cuspEdgesIndices[0]);
      woeb = _pWXFace->GetOEdge(index0);
      indexStart = cuspEdgesIndices[0];
      ta = _DotP[indexStart] / (_DotP[indexStart] - _DotP[(indexStart + 1) % nedges]);
      tb = 0.0f;
    }
    else {
      woea = _pWXFace->GetOEdge(index0);
      woeb = _pWXFace->GetOEdge(cuspEdgesIndices[0]);
      indexEnd = cuspEdgesIndices[0];
      ta = 0.0f;
      tb = _DotP[indexEnd] / (_DotP[indexEnd] - _DotP[(indexEnd + 1) % nedges]);
    }
    ok = true;
  }
  else if (_nNullDotP == 2) {
    // that means that the silhouette edge is an edge of the mesh
    int index = GetSmoothEdgeIndex();
    if (!_pWXFace->front()) {  // is it in the right order ?
      // the order of the WOEdge index is wrong
      woea = _pWXFace->GetOEdge((index + 1) % nedges);
      woeb = _pWXFace->GetOEdge((index - 1) % nedges);
      ta = 0.0f;
      tb = 1.0f;
      ok = true;
    }
    else {
      // here it's not good, our edge is a single point -> skip that face
      ok = false;
#if 0
      // the order of the WOEdge index is good
      woea = _pWXFace->GetOEdge((index - 1) % nedges);
      woeb = _pWXFace->GetOEdge((index + 1) % nedges);
      ta = 1.0f;
      tb = 0.0f;
#endif
    }
  }
  if (ok) {
    _pSmoothEdge = new WXSmoothEdge;
    _pSmoothEdge->setWOeA(woea);
    _pSmoothEdge->setWOeB(woeb);
    _pSmoothEdge->setTa(ta);
    _pSmoothEdge->setTb(tb);
    if (_Nature & Nature::SILHOUETTE) {
      if (_nNullDotP != 2) {
        if (_DotP[_ClosestPointIndex] + 0.01f > 0.0f) {
          _pSmoothEdge->setFront(true);
        }
        else {
          _pSmoothEdge->setFront(false);
        }
      }
    }
  }

#if 0
  // check bording edges to see if they have different dotp values in bording faces.
  for (int i = 0; i < numberOfEdges(); i++) {
    WSFace *bface = (WSFace *)GetBordingFace(i);
    if (bface) {
      if ((front()) ^
          (bface->front())) {  // fA->front XOR fB->front (true if one is 0 and the other is 1)
        // that means that the edge i of the face is a silhouette edge
        // CHECK FIRST WHETHER THE EXACTSILHOUETTEEDGE HAS
        // NOT YET BEEN BUILT ON THE OTHER FACE (1 is enough).
        if (((WSExactFace *)bface)->exactSilhouetteEdge()) {
          // that means that this silhouette edge has already been built
          return ((WSExactFace *)bface)->exactSilhouetteEdge();
        }
        // Else we must build it
        WOEdge *woea, *woeb;
        float ta, tb;
        if (!front()) {  // is it in the right order ?
          // the order of the WOEdge index is wrong
          woea = _OEdgeList[(i + 1) % numberOfEdges()];
          if (0 == i) {
            woeb = _OEdgeList[numberOfEdges() - 1];
          }
          else {
            woeb = _OEdgeList[(i - 1)];
          }
          ta = 0.0f;
          tb = 1.0f;
        }
        else {
          // the order of the WOEdge index is good
          if (0 == i) {
            woea = _OEdgeList[numberOfEdges() - 1];
          }
          else {
            woea = _OEdgeList[(i - 1)];
          }
          woeb = _OEdgeList[(i + 1) % numberOfEdges()];
          ta = 1.0f;
          tb = 0.0f;
        }

        _pSmoothEdge = new ExactSilhouetteEdge(ExactSilhouetteEdge::VERTEX_VERTEX);
        _pSmoothEdge->setWOeA(woea);
        _pSmoothEdge->setWOeA(woeb);
        _pSmoothEdge->setTa(ta);
        _pSmoothEdge->setTb(tb);

        return _pSmoothEdge;
      }
    }
  }
#endif
  return _pSmoothEdge;
}

void WXFace::ComputeCenter()
{
  vector<WVertex *> iVertexList;
  RetrieveVertexList(iVertexList);
  Vec3f center;
  for (vector<WVertex *>::iterator wv = iVertexList.begin(), wvend = iVertexList.end();
       wv != wvend;
       ++wv) {
    center += (*wv)->GetVertex();
  }
  center /= (float)iVertexList.size();
  setCenter(center);
}

/**********************************
 *                                *
 *                                *
 *             WXShape            *
 *                                *
 *                                *
 **********************************/

WFace *WXShape::MakeFace(vector<WVertex *> &iVertexList,
                         vector<bool> &iFaceEdgeMarksList,
                         unsigned iMaterialIndex)
{
  WFace *face = WShape::MakeFace(iVertexList, iFaceEdgeMarksList, iMaterialIndex);
  if (!face) {
    return nullptr;
  }

  Vec3f center;
  for (vector<WVertex *>::iterator wv = iVertexList.begin(), wvend = iVertexList.end();
       wv != wvend;
       ++wv) {
    center += (*wv)->GetVertex();
  }
  center /= (float)iVertexList.size();
  ((WXFace *)face)->setCenter(center);

  return face;
}

WFace *WXShape::MakeFace(vector<WVertex *> &iVertexList,
                         vector<Vec3f> &iNormalsList,
                         vector<Vec2f> &iTexCoordsList,
                         vector<bool> &iFaceEdgeMarksList,
                         unsigned iMaterialIndex)
{
  WFace *face = WShape::MakeFace(
      iVertexList, iNormalsList, iTexCoordsList, iFaceEdgeMarksList, iMaterialIndex);

#if 0
  Vec3f center;
  for (vector<WVertex *>::iterator wv = iVertexList.begin(), wvend = iVertexList.end();
       wv != wvend;
       ++wv) {
    center += (*wv)->GetVertex();
  }
  center /= (float)iVertexList.size();
  ((WXFace *)face)->setCenter(center);
#endif

  return face;
}

} /* namespace Freestyle */