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

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

/** \file
 * \ingroup freestyle
 * \brief Chaining iterators
 */

#include "../python/Director.h"

#include "ChainingIterators.h"

#include "../system/TimeStamp.h"

namespace Freestyle {

ViewEdge *AdjacencyIterator::operator*()
{
  return (*_internalIterator).first;
}

bool AdjacencyIterator::isIncoming() const
{
  return (*_internalIterator).second;
}

int AdjacencyIterator::increment()
{
  ++_internalIterator;
  while (!_internalIterator.isEnd() && !isValid((*_internalIterator).first)) {
    ++_internalIterator;
  }
  return 0;
}

bool AdjacencyIterator::isValid(ViewEdge *edge)
{
  if (_restrictToSelection) {
    if (edge->getTimeStamp() != TimeStamp::instance()->getTimeStamp()) {
      return false;
    }
  }
  if (_restrictToUnvisited) {
    if (edge->getChainingTimeStamp() > TimeStamp::instance()->getTimeStamp()) {
      return false;
    }
  }
  return true;
}

int ChainingIterator::init()
{
  return Director_BPy_ChainingIterator_init(this);
}

int ChainingIterator::traverse(const AdjacencyIterator &it)
{
  return Director_BPy_ChainingIterator_traverse(this, const_cast<AdjacencyIterator &>(it));
}

int ChainingIterator::increment()
{
  _increment = true;
  ViewVertex *vertex = getVertex();
  if (!vertex) {
    _edge = nullptr;
    return 0;
  }
  AdjacencyIterator it = AdjacencyIterator(vertex, _restrictToSelection, _restrictToUnvisited);
  if (it.isEnd()) {
    _edge = nullptr;
    return 0;
  }
  if (traverse(it) < 0) {
    return -1;
  }
  _edge = result;
  if (_edge == nullptr) {
    return 0;
  }
  if (_edge->A() == vertex) {
    _orientation = true;
  }
  else {
    _orientation = false;
  }
  return 0;
}

int ChainingIterator::decrement()
{
  _increment = false;
  ViewVertex *vertex = getVertex();
  if (!vertex) {
    _edge = nullptr;
    return 0;
  }
  AdjacencyIterator it = AdjacencyIterator(vertex, _restrictToSelection, _restrictToUnvisited);
  if (it.isEnd()) {
    _edge = nullptr;
    return 0;
  }
  if (traverse(it) < 0) {
    return -1;
  }
  _edge = result;
  if (_edge == nullptr) {
    return 0;
  }
  if (_edge->B() == vertex) {
    _orientation = true;
  }
  else {
    _orientation = false;
  }
  return 0;
}

//
// ChainSilhouetteIterators
//
///////////////////////////////////////////////////////////

int ChainSilhouetteIterator::traverse(const AdjacencyIterator &ait)
{
  AdjacencyIterator it(ait);
  ViewVertex *nextVertex = getVertex();
  // we can't get a NULL nextVertex here, it was intercepted before
  if (nextVertex->getNature() & Nature::T_VERTEX) {
    TVertex *tvertex = (TVertex *)nextVertex;
    ViewEdge *mate = (tvertex)->mate(getCurrentEdge());
    while (!it.isEnd()) {
      ViewEdge *ve = *it;
      if (ve == mate) {
        result = ve;
        return 0;
      }
      ++it;
    }
    result = nullptr;
    return 0;
  }
  if (nextVertex->getNature() & Nature::NON_T_VERTEX) {
    // soc NonTVertex *nontvertex = (NonTVertex*)nextVertex;
    ViewEdge *newEdge(nullptr);
    // we'll try to chain the edges by keeping the same nature...
    // the preseance order is : SILHOUETTE, BORDER, CREASE, MATERIAL_BOUNDARY, EDGE_MARK,
    // SUGGESTIVE, VALLEY, RIDGE
    Nature::EdgeNature natures[8] = {
        Nature::SILHOUETTE,
        Nature::BORDER,
        Nature::CREASE,
        Nature::MATERIAL_BOUNDARY,
        Nature::EDGE_MARK,
        Nature::SUGGESTIVE_CONTOUR,
        Nature::VALLEY,
        Nature::RIDGE,
    };
    int numNatures = ARRAY_SIZE(natures);
    for (int i = 0; i < numNatures; ++i) {
      if (getCurrentEdge()->getNature() & natures[i]) {
        int n = 0;
        while (!it.isEnd()) {
          ViewEdge *ve = *it;
          if (ve->getNature() & natures[i]) {
            ++n;
            newEdge = ve;
          }
          ++it;
        }
        if (n == 1) {
          result = newEdge;
        }
        else {
          result = nullptr;
        }
        return 0;
      }
    }
  }
  result = nullptr;
  return 0;
}

int ChainPredicateIterator::traverse(const AdjacencyIterator &ait)
{
  if (!_unary_predicate || !_binary_predicate) {
    return -1;
  }
  AdjacencyIterator it(ait);
  // Iterates over next edges to see if one of them respects the predicate:
  while (!it.isEnd()) {
    ViewEdge *ve = *it;
    if (_unary_predicate->operator()(*ve) < 0) {
      return -1;
    }
    if (_unary_predicate->result) {
      if (_binary_predicate->operator()(*(getCurrentEdge()), *(ve)) < 0) {
        return -1;
      }
      if (_binary_predicate->result) {
        result = ve;
        return 0;
      }
    }
    ++it;
  }
  result = nullptr;
  return 0;
}

} /* namespace Freestyle */