Cycles microdisplacement: fix handling of triangles in DiagSplit
[blender.git] / intern / cycles / subd / subd_split.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "camera.h"
18 #include "mesh.h"
19
20 #include "subd_dice.h"
21 #include "subd_patch.h"
22 #include "subd_split.h"
23
24 #include "util_debug.h"
25 #include "util_math.h"
26 #include "util_types.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 /* DiagSplit */
31
32 DiagSplit::DiagSplit(const SubdParams& params_)
33 : params(params_)
34 {
35 }
36
37 void DiagSplit::dispatch(QuadDice::SubPatch& sub, QuadDice::EdgeFactors& ef)
38 {
39         subpatches_quad.push_back(sub);
40         edgefactors_quad.push_back(ef);
41 }
42
43 void DiagSplit::dispatch(TriangleDice::SubPatch& sub, TriangleDice::EdgeFactors& ef)
44 {
45         subpatches_triangle.push_back(sub);
46         edgefactors_triangle.push_back(ef);
47 }
48
49 float3 DiagSplit::to_world(Patch *patch, float2 uv)
50 {
51         float3 P;
52
53         patch->eval(&P, NULL, NULL, NULL, uv.x, uv.y);
54         if(params.camera)
55                 P = transform_point(&params.objecttoworld, P);
56
57         return P;
58 }
59
60 int DiagSplit::T(Patch *patch, float2 Pstart, float2 Pend)
61 {
62         float3 Plast = make_float3(0.0f, 0.0f, 0.0f);
63         float Lsum = 0.0f;
64         float Lmax = 0.0f;
65
66         for(int i = 0; i < params.test_steps; i++) {
67                 float t = i/(float)(params.test_steps-1);
68
69                 float3 P = to_world(patch, Pstart + t*(Pend - Pstart));
70
71                 if(i > 0) {
72                         float L;
73
74                         if(!params.camera) {
75                                 L = len(P - Plast);
76                         }
77                         else {
78                                 Camera* cam = params.camera;
79
80                                 float pixel_width = cam->world_to_raster_size((P + Plast) * 0.5f);
81                                 L = len(P - Plast) / pixel_width;
82                         }
83
84                         Lsum += L;
85                         Lmax = max(L, Lmax);
86                 }
87
88                 Plast = P;
89         }
90
91         int tmin = (int)ceil(Lsum/params.dicing_rate);
92         int tmax = (int)ceil((params.test_steps-1)*Lmax/params.dicing_rate); // XXX paper says N instead of N-1, seems wrong?
93
94         if(tmax - tmin > params.split_threshold)
95                 return DSPLIT_NON_UNIFORM;
96         
97         return tmax;
98 }
99
100 void DiagSplit::partition_edge(Patch *patch, float2 *P, int *t0, int *t1, float2 Pstart, float2 Pend, int t)
101 {
102         if(t == DSPLIT_NON_UNIFORM) {
103                 *P = (Pstart + Pend)*0.5f;
104                 *t0 = T(patch, Pstart, *P);
105                 *t1 = T(patch, *P, Pend);
106         }
107         else {
108                 int I = (int)floor((float)t*0.5f);
109                 *P = interp(Pstart, Pend, (t == 0)? 0: I/(float)t); /* XXX is t faces or verts */
110                 *t0 = I;
111                 *t1 = t - I;
112         }
113 }
114
115 void DiagSplit::split(TriangleDice::SubPatch& sub, TriangleDice::EdgeFactors& ef, int depth)
116 {
117         if(depth > 32) {
118                 /* We should never get here, but just in case end recursion safely. */
119                 ef.tu = 1;
120                 ef.tv = 1;
121                 ef.tw = 1;
122
123                 dispatch(sub, ef);
124                 return;
125         }
126
127         assert(ef.tu == T(sub.patch, sub.Pv, sub.Pw));
128         assert(ef.tv == T(sub.patch, sub.Pw, sub.Pu));
129         assert(ef.tw == T(sub.patch, sub.Pu, sub.Pv));
130
131         int non_uniform_count = int(ef.tu == DSPLIT_NON_UNIFORM) +
132                                 int(ef.tv == DSPLIT_NON_UNIFORM) +
133                             int(ef.tw == DSPLIT_NON_UNIFORM);
134
135         switch(non_uniform_count) {
136                 case 1: {
137                         /* TODO(mai): one edge is non-uniform, split into two triangles */
138                         // fallthru
139                 }
140                 case 2: {
141                         /* TODO(mai): two edges are non-uniform, split into triangle and quad */
142                         // fallthru
143                 }
144                 case 3: {
145                         /* all three edges are non-uniform, split into three quads */
146
147                         /* partition edges */
148                         QuadDice::EdgeFactors ef0, ef1, ef2;
149                         float2 Pu, Pv, Pw, Pcenter;
150
151                         partition_edge(sub.patch, &Pu, &ef1.tv0, &ef2.tu0, sub.Pw, sub.Pv, ef.tu);
152                         partition_edge(sub.patch, &Pv, &ef0.tv0, &ef1.tu0, sub.Pu, sub.Pw, ef.tv);
153                         partition_edge(sub.patch, &Pw, &ef2.tv0, &ef0.tu0, sub.Pv, sub.Pu, ef.tw);
154                         Pcenter = (Pu + Pv + Pw) * (1.0f / 3.0f);
155
156                         /* split */
157                         int tsplit01 = T(sub.patch, Pv, Pcenter);
158                         int tsplit12 = T(sub.patch, Pu, Pcenter);
159                         int tsplit20 = T(sub.patch, Pw, Pcenter);
160
161                         ef0.tu1 = tsplit01;
162                         ef0.tv1 = tsplit20;
163
164                         ef1.tu1 = tsplit12;
165                         ef1.tv1 = tsplit01;
166
167                         ef2.tu1 = tsplit20;
168                         ef2.tv1 = tsplit12;
169
170                         /* create subpatches */
171                         QuadDice::SubPatch sub0 = {sub.patch, sub.Pu, Pw, Pv, Pcenter};
172                         QuadDice::SubPatch sub1 = {sub.patch, sub.Pw, Pv, Pu, Pcenter};
173                         QuadDice::SubPatch sub2 = {sub.patch, sub.Pv, Pu, Pw, Pcenter};
174
175                         split(sub0, ef0, depth+1);
176                         split(sub1, ef1, depth+1);
177                         split(sub2, ef2, depth+1);
178
179                         break;
180                 }
181                 default: {
182                         /* all edges uniform, no splitting needed */
183                         dispatch(sub, ef);
184                         break;
185                 }
186         }
187 }
188
189 void DiagSplit::split(QuadDice::SubPatch& sub, QuadDice::EdgeFactors& ef, int depth)
190 {
191         if(depth > 32) {
192                 /* We should never get here, but just in case end recursion safely. */
193                 ef.tu0 = 1;
194                 ef.tu1 = 1;
195                 ef.tv0 = 1;
196                 ef.tv1 = 1;
197
198                 dispatch(sub, ef);
199                 return;
200         }
201
202         bool split_u = (ef.tu0 == DSPLIT_NON_UNIFORM || ef.tu1 == DSPLIT_NON_UNIFORM);
203         bool split_v = (ef.tv0 == DSPLIT_NON_UNIFORM || ef.tv1 == DSPLIT_NON_UNIFORM);
204
205         if(split_u && split_v) {
206                 split_u = depth % 2;
207         }
208
209         if(split_u) {
210                 /* partition edges */
211                 QuadDice::EdgeFactors ef0, ef1;
212                 float2 Pu0, Pu1;
213
214                 partition_edge(sub.patch,
215                         &Pu0, &ef0.tu0, &ef1.tu0, sub.P00, sub.P10, ef.tu0);
216                 partition_edge(sub.patch,
217                         &Pu1, &ef0.tu1, &ef1.tu1, sub.P01, sub.P11, ef.tu1);
218
219                 /* split */
220                 int tsplit = T(sub.patch, Pu0, Pu1);
221                 ef0.tv0 = ef.tv0;
222                 ef0.tv1 = tsplit;
223
224                 ef1.tv0 = tsplit;
225                 ef1.tv1 = ef.tv1;
226
227                 /* create subpatches */
228                 QuadDice::SubPatch sub0 = {sub.patch, sub.P00, Pu0, sub.P01, Pu1};
229                 QuadDice::SubPatch sub1 = {sub.patch, Pu0, sub.P10, Pu1, sub.P11};
230
231                 split(sub0, ef0, depth+1);
232                 split(sub1, ef1, depth+1);
233         }
234         else if(split_v) {
235                 /* partition edges */
236                 QuadDice::EdgeFactors ef0, ef1;
237                 float2 Pv0, Pv1;
238
239                 partition_edge(sub.patch,
240                         &Pv0, &ef0.tv0, &ef1.tv0, sub.P00, sub.P01, ef.tv0);
241                 partition_edge(sub.patch,
242                         &Pv1, &ef0.tv1, &ef1.tv1, sub.P10, sub.P11, ef.tv1);
243
244                 /* split */
245                 int tsplit = T(sub.patch, Pv0, Pv1);
246                 ef0.tu0 = ef.tu0;
247                 ef0.tu1 = tsplit;
248
249                 ef1.tu0 = tsplit;
250                 ef1.tu1 = ef.tu1;
251
252                 /* create subpatches */
253                 QuadDice::SubPatch sub0 = {sub.patch, sub.P00, sub.P10, Pv0, Pv1};
254                 QuadDice::SubPatch sub1 = {sub.patch, Pv0, Pv1, sub.P01, sub.P11};
255
256                 split(sub0, ef0, depth+1);
257                 split(sub1, ef1, depth+1);
258         }
259         else {
260                 dispatch(sub, ef);
261         }
262 }
263
264 void DiagSplit::split_triangle(Patch *patch)
265 {
266         TriangleDice::SubPatch sub_split;
267         TriangleDice::EdgeFactors ef_split;
268
269         sub_split.patch = patch;
270         sub_split.Pu = make_float2(1.0f, 0.0f);
271         sub_split.Pv = make_float2(0.0f, 1.0f);
272         sub_split.Pw = make_float2(0.0f, 0.0f);
273
274         ef_split.tu = T(patch, sub_split.Pv, sub_split.Pw);
275         ef_split.tv = T(patch, sub_split.Pw, sub_split.Pu);
276         ef_split.tw = T(patch, sub_split.Pu, sub_split.Pv);
277
278         split(sub_split, ef_split);
279
280         TriangleDice dice(params);
281
282         for(size_t i = 0; i < subpatches_triangle.size(); i++) {
283                 TriangleDice::SubPatch& sub = subpatches_triangle[i];
284                 TriangleDice::EdgeFactors& ef = edgefactors_triangle[i];
285
286                 ef.tu = max(ef.tu, 1);
287                 ef.tv = max(ef.tv, 1);
288                 ef.tw = max(ef.tw, 1);
289
290                 dice.dice(sub, ef);
291         }
292
293         subpatches_triangle.clear();
294         edgefactors_triangle.clear();
295
296         /* triangle might be split into quads so dice quad subpatches as well */
297         QuadDice qdice(params);
298
299         for(size_t i = 0; i < subpatches_quad.size(); i++) {
300                 QuadDice::SubPatch& sub = subpatches_quad[i];
301                 QuadDice::EdgeFactors& ef = edgefactors_quad[i];
302
303                 ef.tu0 = max(ef.tu0, 1);
304                 ef.tu1 = max(ef.tu1, 1);
305                 ef.tv0 = max(ef.tv0, 1);
306                 ef.tv1 = max(ef.tv1, 1);
307
308                 qdice.dice(sub, ef);
309         }
310
311         subpatches_quad.clear();
312         edgefactors_quad.clear();
313 }
314
315 void DiagSplit::split_quad(Patch *patch)
316 {
317         QuadDice::SubPatch sub_split;
318         QuadDice::EdgeFactors ef_split;
319
320         sub_split.patch = patch;
321         sub_split.P00 = make_float2(0.0f, 0.0f);
322         sub_split.P10 = make_float2(1.0f, 0.0f);
323         sub_split.P01 = make_float2(0.0f, 1.0f);
324         sub_split.P11 = make_float2(1.0f, 1.0f);
325
326         ef_split.tu0 = T(patch, sub_split.P00, sub_split.P10);
327         ef_split.tu1 = T(patch, sub_split.P01, sub_split.P11);
328         ef_split.tv0 = T(patch, sub_split.P00, sub_split.P01);
329         ef_split.tv1 = T(patch, sub_split.P10, sub_split.P11);
330
331         split(sub_split, ef_split);
332
333         QuadDice dice(params);
334
335         for(size_t i = 0; i < subpatches_quad.size(); i++) {
336                 QuadDice::SubPatch& sub = subpatches_quad[i];
337                 QuadDice::EdgeFactors& ef = edgefactors_quad[i];
338
339                 ef.tu0 = max(ef.tu0, 1);
340                 ef.tu1 = max(ef.tu1, 1);
341                 ef.tv0 = max(ef.tv0, 1);
342                 ef.tv1 = max(ef.tv1, 1);
343
344                 dice.dice(sub, ef);
345         }
346
347         subpatches_quad.clear();
348         edgefactors_quad.clear();
349 }
350
351 CCL_NAMESPACE_END
352