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

Operators.h « stroke « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2da9d30f172eccb5d7058f75607801b75a825082 (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
/*
 * 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.
 */

#ifndef __FREESTYLE_OPERATORS_H__
#define __FREESTYLE_OPERATORS_H__

/** \file
 * \ingroup freestyle
 * \brief Class gathering stroke creation algorithms
 */

#include <iostream>
#include <vector>

#include "Chain.h"
#include "ChainingIterators.h"
#include "Predicates0D.h"
#include "Predicates1D.h"
#include "StrokeShader.h"

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

#include "../view_map/Interface1D.h"
#include "../view_map/ViewMap.h"

#ifdef WITH_CXX_GUARDEDALLOC
#  include "MEM_guardedalloc.h"
#endif

namespace Freestyle {

/*! Class defining the operators used in a style module.
 *  There are 4 classes of operators: Selection, Chaining, Splitting and Creating.
 *  All these operators are user controlled in the scripting language through Functors, Predicates
 *  and Shaders that are taken as arguments.
 */
class Operators {

 public:
  typedef vector<Interface1D *> I1DContainer;
  typedef vector<Stroke *> StrokesContainer;

  //
  // Operators
  //
  ////////////////////////////////////////////////

  /*! Selects the ViewEdges of the ViewMap verifying a specified condition.
   *  \param pred: The predicate expressing this condition
   */
  static int select(UnaryPredicate1D &pred);

  /*! Builds a set of chains from the current set of ViewEdges.
   *  Each ViewEdge of the current list starts a new chain.
   *  The chaining operator then iterates over the ViewEdges
   *  of the ViewMap using the user specified iterator.
   *  This operator only iterates using the increment operator and is therefore unidirectional.
   *  \param it:
   *           The iterator on the ViewEdges of the ViewMap. It contains the chaining rule.
   *  \param pred:
   *           The predicate on the ViewEdge that expresses the stopping condition.
   *  \param modifier:
   *           A function that takes a ViewEdge as argument and that is used to modify the
   *           processed ViewEdge state (the timestamp incrementation is a typical illustration of
   *           such a modifier)
   */
  static int chain(ViewEdgeInternal::ViewEdgeIterator &it,
                   UnaryPredicate1D &pred,
                   UnaryFunction1D_void &modifier);

  /*! Builds a set of chains from the current set of ViewEdges.
   *  Each ViewEdge of the current list starts a new chain. The chaining operator then iterates
   *  over the ViewEdges
   *  of the ViewMap using the user specified iterator.
   *  This operator only iterates using the increment operator and is therefore unidirectional.
   *  This chaining operator is different from the previous one because it doesn't take any
   *  modifier as argument. Indeed, the time stamp (insuring that a ViewEdge is processed one time)
   *  is automatically managed in this case.
   *  \param it:
   *           The iterator on the ViewEdges of the ViewMap. It contains the chaining rule.
   *  \param pred:
   *           The predicate on the ViewEdge that expresses the stopping condition.
   */
  static int chain(ViewEdgeInternal::ViewEdgeIterator &it, UnaryPredicate1D &pred);

  /*! Builds a set of chains from the current set of ViewEdges.
   *  Each ViewEdge of the current list potentially starts a new chain. The chaining operator then
   *  iterates over the ViewEdges of the ViewMap using the user specified iterator.
   *  This operator iterates both using the increment and decrement operators and is therefore
   *  bidirectional. This operator works with a ChainingIterator which contains the chaining rules.
   *  It is this last one which can be told to chain only edges that belong to the selection or not
   *  to process twice a ViewEdge during the chaining. Each time a ViewEdge is added to a chain,
   *  its chaining time stamp is incremented. This allows you to keep track of the number of chains
   *  to which a ViewEdge belongs to.
   *  \param it:
   *           The ChainingIterator on the ViewEdges of the ViewMap. It contains the chaining rule.
   *  \param pred:
   *           The predicate on the ViewEdge that expresses the stopping condition.
   */
  static int bidirectionalChain(ChainingIterator &it, UnaryPredicate1D &pred);

  /*! The only difference with the above bidirectional chaining algorithm is that we don't need to
   *  pass a stopping criterion. This might be desirable when the stopping criterion is already
   *  contained in the iterator definition. Builds a set of chains from the current set of
   *  ViewEdges. Each ViewEdge of the current list potentially starts a new chain. The chaining
   *  operator then iterates over the ViewEdges of the ViewMap using the user specified iterator.
   *  This operator iterates both using the increment and decrement operators and is therefore
   *  bidirectional. This operator works with a ChainingIterator which contains the chaining rules.
   *  It is this last one which can be told to chain only edges that belong to the selection or not
   *  to process twice a ViewEdge during the chaining. Each time a ViewEdge is added to a chain,
   *  its chaining time stamp is incremented. This allows you to keep track of the number of chains
   *  to which a ViewEdge belongs to.
   *  \param it:
   *           The ChainingIterator on the ViewEdges of the ViewMap. It contains the chaining rule.
   */
  static int bidirectionalChain(ChainingIterator &it);

  /*! Splits each chain of the current set of chains in a sequential way.
   *  The points of each chain are processed (with a specified sampling) sequentially.
   *  Each time a user specified starting condition is verified, a new chain begins and ends as
   *  soon as a user-defined stopping predicate is verified.
   *  This allows chains overlapping rather than chains partitioning.
   *  The first point of the initial chain is the first point of one of the resulting chains.
   *  The splitting ends when no more chain can start.
   *  \param startingPred:
   *           The predicate on a point that expresses the starting condition
   *  \param stoppingPred:
   *           The predicate on a point that expresses the stopping condition
   *  \param sampling:
   *           The resolution used to sample the chain for the predicates evaluation.
   *           (The chain is not actually resampled, a virtual point only progresses along the
   *           curve using this resolution)
   */
  static int sequentialSplit(UnaryPredicate0D &startingPred,
                             UnaryPredicate0D &stoppingPred,
                             float sampling = 0.0f);

  /*! Splits each chain of the current set of chains in a sequential way.
   *  The points of each chain are processed (with a specified sampling) sequentially and each time
   *  a user specified condition is verified, the chain is split into two chains.
   *  The resulting set of chains is a partition of the initial chain
   *  \param pred:
   *           The predicate on a point that expresses the splitting condition
   *  \param sampling:
   *           The resolution used to sample the chain for the predicate evaluation.
   *           (The chain is not actually resampled, a virtual point only progresses along the
   *           curve using this resolution)
   */
  static int sequentialSplit(UnaryPredicate0D &pred, float sampling = 0.0f);

  /*! Splits the current set of chains in a recursive way.
   *  We process the points of each chain (with a specified sampling) to find the point
   *  minimizing a specified function. The chain is split in two at this point and the two new
   *  chains are processed in the same way. The recursivity level is controlled through a
   *  predicate 1D that expresses a stopping condition on the chain that is about to be processed.
   *  \param func:
   *           The Unary Function evaluated at each point of the chain.
   *           The splitting point is the point minimizing this function
   *  \param pred:
   *           The Unary Predicate ex pressing the recursivity stopping condition.
   *           This predicate is evaluated for each curve before it actually gets split.
   *           If pred(chain) is true, the curve won't be split anymore.
   *  \param sampling:
   *           The resolution used to sample the chain for the predicates evaluation. (The chain
   *           is not actually resampled, a virtual point only progresses along the curve using
   *           this resolution)
   */
  static int recursiveSplit(UnaryFunction0D<double> &func,
                            UnaryPredicate1D &pred,
                            float sampling = 0);

  /*! Splits the current set of chains in a recursive way.
   *  We process the points of each chain (with a specified sampling) to find the point minimizing
   *  a specified function. The chain is split in two at this point and the two new chains are
   *  processed in the same way. The user can specify a 0D predicate to make a first selection on
   *  the points that can potentially be split. A point that doesn't verify the 0D predicate
   *  won't be candidate in realizing the min. The recursivity level is controlled through a
   *  predicate 1D that expresses a stopping condition on the chain that is about to be processed.
   *  \param func:
   *           The Unary Function evaluated at each point of the chain.
   *           The splitting point is the point minimizing this function
   *  \param pred0d:
   *           The Unary Predicate 0D used to select the candidate points where the split can
   *           occur. For example, it is very likely that would rather have your chain splitting
   *           around its middle point than around one of its extremities. A 0D predicate working
   *           on the curvilinear abscissa allows to add this kind of constraints.
   *  \param pred:
   *           The Unary Predicate ex pressing the recursivity stopping condition.
   *           This predicate is evaluated for each curve before it actually gets split.
   *           If pred(chain) is true, the curve won't be split anymore.
   *  \param sampling:
   *           The resolution used to sample the chain for the predicates evaluation. (The chain
   *           is not actually resampled, a virtual point only progresses along the curve using
   *           this resolution)
   */
  static int recursiveSplit(UnaryFunction0D<double> &func,
                            UnaryPredicate0D &pred0d,
                            UnaryPredicate1D &pred,
                            float sampling = 0.0f);

  /*! Sorts the current set of chains (or viewedges)
   *  according to the comparison predicate given as argument.
   *  \param pred:
   *           The binary predicate used for the comparison
   */
  static int sort(BinaryPredicate1D &pred);

  /*! Creates and shades the strokes from the current set of chains.
   *  A predicate can be specified to make a selection pass on the chains.
   *  \param pred:
   *           The predicate that a chain must verify in order to be transform as a stroke
   *  \param shaders:
   *           The list of shaders used to shade the strokes
   */
  static int create(UnaryPredicate1D &pred, vector<StrokeShader *> shaders);

  //
  // Data access
  //
  ////////////////////////////////////////////////

  static ViewEdge *getViewEdgeFromIndex(unsigned i)
  {
    return dynamic_cast<ViewEdge *>(_current_view_edges_set[i]);
  }

  static Chain *getChainFromIndex(unsigned i)
  {
    return dynamic_cast<Chain *>(_current_chains_set[i]);
  }

  static Stroke *getStrokeFromIndex(unsigned i)
  {
    return _current_strokes_set[i];
  }

  static unsigned getViewEdgesSize()
  {
    return _current_view_edges_set.size();
  }

  static unsigned getChainsSize()
  {
    return _current_chains_set.size();
  }

  static unsigned getStrokesSize()
  {
    return _current_strokes_set.size();
  }

  //
  // Not exported in Python
  //
  //////////////////////////////////////////////////

  static StrokesContainer *getStrokesSet()
  {
    return &_current_strokes_set;
  }

  static void reset(bool removeStrokes = true);

 private:
  Operators()
  {
  }

  static I1DContainer _current_view_edges_set;
  static I1DContainer _current_chains_set;
  static I1DContainer *_current_set;
  static StrokesContainer _current_strokes_set;

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Operators")
#endif
};

} /* namespace Freestyle */

#endif  // __FREESTYLE_OPERATORS_H__