Freestyle: remove a bunch of debug prints in the python code, these are confusing
[blender.git] / release / scripts / freestyle / style_modules / PredicatesU1D.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 #  Filename : PredicatesU1D.py
20 #  Authors  : Fredo Durand, Stephane Grabli, Francois Sillion, Emmanuel Turquin 
21 #  Date     : 08/04/2005
22 #  Purpose  : Unary predicates (functors) to be used for 1D elements
23
24 from freestyle import Curvature2DAngleF0D, CurveNatureF1D, DensityF1D, GetCompleteViewMapDensityF1D, \
25     GetDirectionalViewMapDensityF1D, GetOccludersF1D, GetProjectedZF1D, GetShapeF1D, GetSteerableViewMapDensityF1D, \
26     IntegrationType, ShapeUP1D, TVertex, UnaryPredicate1D
27 from Functions1D import pyDensityAnisotropyF1D, pyViewMapGradientNormF1D
28
29 class pyNFirstUP1D(UnaryPredicate1D):
30         def __init__(self, n):
31                 UnaryPredicate1D.__init__(self)
32                 self.__n = n
33                 self.__count = 0
34         def __call__(self, inter):
35                 self.__count = self.__count + 1
36                 if self.__count <= self.__n:
37                         return 1
38                 return 0
39
40 class pyHigherLengthUP1D(UnaryPredicate1D):
41         def __init__(self,l):
42                 UnaryPredicate1D.__init__(self)
43                 self._l = l
44         def __call__(self, inter):
45                 return (inter.length_2d > self._l)
46
47 class pyNatureUP1D(UnaryPredicate1D):
48         def __init__(self,nature):
49                 UnaryPredicate1D.__init__(self)
50                 self._nature = nature
51                 self._getNature = CurveNatureF1D()
52         def __call__(self, inter):
53                 if(self._getNature(inter) & self._nature):
54                         return 1
55                 return 0
56
57 class pyHigherNumberOfTurnsUP1D(UnaryPredicate1D):
58         def __init__(self,n,a):
59                 UnaryPredicate1D.__init__(self)
60                 self._n = n
61                 self._a = a
62         def __call__(self, inter):
63                 count = 0
64                 func = Curvature2DAngleF0D()
65                 it = inter.vertices_begin()
66                 while not it.is_end:
67                         if func(it) > self._a:
68                                 count = count+1
69                         if count > self._n:
70                                 return 1
71                         it.increment()
72                 return 0
73
74 class pyDensityUP1D(UnaryPredicate1D):
75         def __init__(self,wsize,threshold, integration = IntegrationType.MEAN, sampling=2.0):
76                 UnaryPredicate1D.__init__(self)
77                 self._wsize = wsize
78                 self._threshold = threshold
79                 self._integration = integration
80                 self._func = DensityF1D(self._wsize, self._integration, sampling)
81         def __call__(self, inter):
82                 if self._func(inter) < self._threshold:
83                         return 1
84                 return 0
85
86 class pyLowSteerableViewMapDensityUP1D(UnaryPredicate1D):
87         def __init__(self,threshold, level,integration = IntegrationType.MEAN):
88                 UnaryPredicate1D.__init__(self)
89                 self._threshold = threshold
90                 self._level = level
91                 self._integration = integration
92         def __call__(self, inter):
93                 func = GetSteerableViewMapDensityF1D(self._level, self._integration)
94                 v = func(inter)
95                 #print(v)
96                 if v < self._threshold:
97                         return 1
98                 return 0
99
100 class pyLowDirectionalViewMapDensityUP1D(UnaryPredicate1D):
101         def __init__(self,threshold, orientation, level,integration = IntegrationType.MEAN):
102                 UnaryPredicate1D.__init__(self)
103                 self._threshold = threshold
104                 self._orientation = orientation
105                 self._level = level
106                 self._integration = integration
107         def __call__(self, inter):
108                 func = GetDirectionalViewMapDensityF1D(self._orientation, self._level, self._integration)
109                 v = func(inter)
110                 #print(v)
111                 if v < self._threshold:
112                         return 1
113                 return 0
114
115 class pyHighSteerableViewMapDensityUP1D(UnaryPredicate1D):
116         def __init__(self,threshold, level,integration = IntegrationType.MEAN):
117                 UnaryPredicate1D.__init__(self)
118                 self._threshold = threshold
119                 self._level = level
120                 self._integration = integration
121                 self._func = GetSteerableViewMapDensityF1D(self._level, self._integration)      
122         def __call__(self, inter):
123                 v = self._func(inter)
124                 if v > self._threshold:
125                         return 1
126                 return 0
127
128 class pyHighDirectionalViewMapDensityUP1D(UnaryPredicate1D):
129         def __init__(self,threshold, orientation, level,integration = IntegrationType.MEAN, sampling=2.0):
130                 UnaryPredicate1D.__init__(self)
131                 self._threshold = threshold
132                 self._orientation = orientation
133                 self._level = level
134                 self._integration = integration
135                 self._sampling = sampling               
136         def __call__(self, inter):
137                 func = GetDirectionalViewMapDensityF1D(self._orientation, self._level, self._integration, self._sampling)
138                 v = func(inter)
139                 if v > self._threshold:
140                         return 1
141                 return 0
142
143 class pyHighViewMapDensityUP1D(UnaryPredicate1D):
144         def __init__(self,threshold, level,integration = IntegrationType.MEAN, sampling=2.0):
145                 UnaryPredicate1D.__init__(self)
146                 self._threshold = threshold
147                 self._level = level
148                 self._integration = integration
149                 self._sampling = sampling
150                 self._func = GetCompleteViewMapDensityF1D(self._level, self._integration, self._sampling) # 2.0 is the smpling
151         def __call__(self, inter):
152                 #print("toto")
153                 #print(func.name)
154                 #print(inter.name)
155                 v= self._func(inter)
156                 if v > self._threshold:
157                         return 1
158                 return 0
159
160 class pyDensityFunctorUP1D(UnaryPredicate1D):
161         def __init__(self,wsize,threshold, functor, funcmin=0.0, funcmax=1.0, integration = IntegrationType.MEAN):
162                 UnaryPredicate1D.__init__(self)
163                 self._wsize = wsize
164                 self._threshold = float(threshold)
165                 self._functor = functor
166                 self._funcmin = float(funcmin)
167                 self._funcmax = float(funcmax)
168                 self._integration = integration
169         def __call__(self, inter):
170                 func = DensityF1D(self._wsize, self._integration)
171                 res = self._functor(inter)
172                 k = (res-self._funcmin)/(self._funcmax-self._funcmin)
173                 if func(inter) < self._threshold*k:
174                         return 1
175                 return 0
176
177 class pyZSmallerUP1D(UnaryPredicate1D):
178         def __init__(self,z, integration=IntegrationType.MEAN):
179                 UnaryPredicate1D.__init__(self)
180                 self._z = z
181                 self._integration = integration
182         def __call__(self, inter):
183                 func = GetProjectedZF1D(self._integration)
184                 if func(inter) < self._z:
185                         return 1
186                 return 0
187
188 class pyIsOccludedByUP1D(UnaryPredicate1D):
189         def __init__(self,id):
190                 UnaryPredicate1D.__init__(self)
191                 self._id = id
192         def __call__(self, inter):
193                 func = GetShapeF1D()
194                 shapes = func(inter)
195                 for s in shapes:
196                         if(s.id == self._id):
197                                 return 0
198                 it = inter.vertices_begin()
199                 itlast = inter.vertices_end()
200                 itlast.decrement()
201                 v =  it.object
202                 vlast = itlast.object
203                 tvertex = v.viewvertex
204                 if type(tvertex) is TVertex:
205                         #print("TVertex: [ ", tvertex.id.first, ",",  tvertex.id.second," ]")
206                         eit = tvertex.edges_begin()
207                         while not eit.is_end:
208                                 ve, incoming = eit.object
209                                 if ve.id == self._id:
210                                         return 1
211                                 #print("-------", ve.id.first, "-", ve.id.second)
212                                 eit.increment()
213                 tvertex = vlast.viewvertex
214                 if type(tvertex) is TVertex:
215                         #print("TVertex: [ ", tvertex.id.first, ",",  tvertex.id.second," ]")
216                         eit = tvertex.edges_begin()
217                         while not eit.is_end:
218                                 ve, incoming = eit.object
219                                 if ve.id == self._id:
220                                         return 1
221                                 #print("-------", ve.id.first, "-", ve.id.second)
222                                 eit.increment()
223                 return 0
224
225 class pyIsInOccludersListUP1D(UnaryPredicate1D):
226         def __init__(self,id):
227                 UnaryPredicate1D.__init__(self)
228                 self._id = id
229         def __call__(self, inter):
230                 func = GetOccludersF1D()
231                 occluders = func(inter)
232                 for a in occluders:
233                         if a.id == self._id:
234                                 return 1
235                 return 0
236
237 class pyIsOccludedByItselfUP1D(UnaryPredicate1D):
238         def __init__(self):
239                 UnaryPredicate1D.__init__(self)
240                 self.__func1 = GetOccludersF1D()
241                 self.__func2 = GetShapeF1D()
242         def __call__(self, inter):
243                 lst1 = self.__func1(inter)
244                 lst2 = self.__func2(inter)
245                 for vs1 in lst1:
246                         for vs2 in lst2:
247                                 if vs1.id == vs2.id:
248                                         return 1
249                 return 0
250
251 class pyIsOccludedByIdListUP1D(UnaryPredicate1D):
252         def __init__(self, idlist):
253                 UnaryPredicate1D.__init__(self)
254                 self._idlist = idlist
255                 self.__func1 = GetOccludersF1D()
256         def __call__(self, inter):
257                 lst1 = self.__func1(inter)
258                 for vs1 in lst1:
259                         for _id in self._idlist:
260                                 if vs1.id == _id:
261                                         return 1
262                 return 0
263
264 class pyShapeIdListUP1D(UnaryPredicate1D):
265         def __init__(self,idlist):
266                 UnaryPredicate1D.__init__(self)
267                 self._idlist = idlist
268                 self._funcs = []
269                 for _id in idlist :
270                         self._funcs.append(ShapeUP1D(_id.first, _id.second))
271         def __call__(self, inter):
272                 for func in self._funcs :
273                         if func(inter) == 1:
274                                 return 1
275                 return 0
276
277 ## deprecated
278 class pyShapeIdUP1D(UnaryPredicate1D):
279         def __init__(self, _id):
280                 UnaryPredicate1D.__init__(self)
281                 self._id = _id
282         def __call__(self, inter):
283                 func = GetShapeF1D()
284                 shapes = func(inter)
285                 for a in shapes:
286                         if a.id == self._id:
287                                 return 1
288                 return 0
289
290 class pyHighDensityAnisotropyUP1D(UnaryPredicate1D):
291         def __init__(self,threshold, level, sampling=2.0):
292                 UnaryPredicate1D.__init__(self)
293                 self._l = threshold
294                 self.func = pyDensityAnisotropyF1D(level, IntegrationType.MEAN, sampling)
295         def __call__(self, inter):
296                 return (self.func(inter) > self._l)
297
298 class pyHighViewMapGradientNormUP1D(UnaryPredicate1D):
299         def __init__(self,threshold, l, sampling=2.0):
300                 UnaryPredicate1D.__init__(self)
301                 self._threshold = threshold
302                 self._GetGradient = pyViewMapGradientNormF1D(l, IntegrationType.MEAN)
303         def __call__(self, inter):
304                 gn = self._GetGradient(inter)
305                 #print(gn)
306                 return (gn > self._threshold)
307
308 class pyDensityVariableSigmaUP1D(UnaryPredicate1D):
309         def __init__(self,functor, sigmaMin,sigmaMax, lmin, lmax, tmin, tmax, integration = IntegrationType.MEAN, sampling=2.0):
310                 UnaryPredicate1D.__init__(self)
311                 self._functor = functor
312                 self._sigmaMin = float(sigmaMin)
313                 self._sigmaMax = float(sigmaMax)
314                 self._lmin = float(lmin)
315                 self._lmax = float(lmax)
316                 self._tmin = tmin
317                 self._tmax = tmax
318                 self._integration = integration
319                 self._sampling = sampling
320         def __call__(self, inter):
321                 sigma = (self._sigmaMax-self._sigmaMin)/(self._lmax-self._lmin)*(self._functor(inter)-self._lmin) + self._sigmaMin
322                 t = (self._tmax-self._tmin)/(self._lmax-self._lmin)*(self._functor(inter)-self._lmin) + self._tmin
323                 if sigma < self._sigmaMin:
324                         sigma = self._sigmaMin
325                 self._func = DensityF1D(sigma, self._integration, self._sampling)
326                 d = self._func(inter)
327                 if d < t:
328                         return 1
329                 return 0
330
331 class pyClosedCurveUP1D(UnaryPredicate1D):
332         def __call__(self, inter):
333                 it = inter.vertices_begin()
334                 itlast = inter.vertices_end()
335                 itlast.decrement()      
336                 vlast = itlast.object
337                 v = it.object
338                 #print(v.id.first, v.id.second)
339                 #print(vlast.id.first, vlast.id.second)
340                 if v.id == vlast.id:
341                         return 1
342                 return 0