Blender V5.3
dice.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/camera.h"
6#include "scene/mesh.h"
7
8#include "subd/dice.h"
10#include "subd/patch.h"
11#include "subd/split.h"
12
13#include "util/tbb.h"
14
16
18 const int num_verts,
19 const int num_triangles,
22{
23 Mesh *mesh = params.mesh;
24
25 /* Keep the coarse vertex count when tessellating an already diced mesh. */
26 mesh->num_subd_added_verts = num_verts - mesh->get_num_subd_base_verts();
27 mesh->resize_mesh(num_verts, num_triangles);
28
30
32
33 /* Get pointers after interpolation.setup() since it may reallocate
34 * attribute buffers when setting motion steps. */
36 mesh_triangles = mesh->triangles.data();
37 mesh_shader = mesh->shader.data();
38 mesh_smooth = mesh->smooth.data();
41
42 if (params.ptex) {
43 Attribute *attr_ptex_face_id = params.mesh->attributes.add(ATTR_STD_PTEX_FACE_ID);
44 Attribute *attr_ptex_uv = params.mesh->attributes.add(ATTR_STD_PTEX_UV);
45
46 mesh_ptex_face_id = attr_ptex_face_id->data_for_write<float>();
47 mesh_ptex_uv = attr_ptex_uv->data_for_write<float2>();
48 }
49}
50
52{
53 float3 P;
54
55 sub.patch->eval(&P, nullptr, nullptr, nullptr, uv.x, uv.y);
56 if (params.camera) {
58 }
59
60 return P;
61}
62
63float EdgeDice::quad_area(const float3 &a, const float3 &b, const float3 &c, const float3 &d)
64{
65 return triangle_area(a, b, d) + triangle_area(a, d, c);
66}
67
68float EdgeDice::scale_factor(const SubPatch &sub, const int Mu, const int Mv)
69{
70 /* estimate area as 4x largest of 4 quads */
71 float3 P[3][3];
72
73 for (int i = 0; i < 3; i++) {
74 for (int j = 0; j < 3; j++) {
75 P[i][j] = eval_projected(sub, sub.map_uv(make_float2(i * 0.5f, j * 0.5f)));
76 }
77 }
78
79 const float A1 = quad_area(P[0][0], P[1][0], P[0][1], P[1][1]);
80 const float A2 = quad_area(P[1][0], P[2][0], P[1][1], P[2][1]);
81 const float A3 = quad_area(P[0][1], P[1][1], P[0][2], P[1][2]);
82 const float A4 = quad_area(P[1][1], P[2][1], P[1][2], P[2][2]);
83 const float Apatch = max(A1, max(A2, max(A3, A4))) * 4.0f;
84
85 /* solve for scaling factor */
86 const float Atri = params.dicing_rate * params.dicing_rate * 0.5f;
87 const float Ntris = Apatch / Atri;
88
89 // XXX does the -sqrt solution matter
90 // XXX max(D, 0.0) is highly suspicious, need to test cases
91 // where D goes negative
92 const float N = 0.5f * (Ntris - (sub.edges[0].edge->T + sub.edges[2].edge->T +
93 sub.edges[3].edge->T + sub.edges[1].edge->T));
94 const float D = (4.0f * N * Mu * Mv) + ((Mu + Mv) * (Mu + Mv));
95 const float S = (Mu + Mv + sqrtf(max(D, 0.0f))) / (2 * Mu * Mv);
96
97 return S;
98}
99
100void EdgeDice::set_vertex(const SubPatch &sub, const int index, const float2 uv)
101{
102 assert(index < params.mesh->num_verts());
103
104 float3 P;
105 float3 N;
106
107 sub.patch->eval(&P, nullptr, nullptr, &N, uv.x, uv.y);
108
109 mesh_P[index] = P;
111
112 for (const SubdAttribute &attr : interpolation.vertex_attributes) {
113 attr.interp(sub.patch->patch_index, sub.face_index, sub.corner, &index, &uv, 1);
114 }
115}
116
118 const int triangle_index,
119 const int v0,
120 const int v1,
121 const int v2,
122 const float2 uv0,
123 const float2 uv1,
124 const float2 uv2)
125{
126 assert(triangle_index * 3 < params.mesh->triangles.size());
127
128 const Patch *patch = sub.patch;
129
130 mesh_triangles[triangle_index * 3 + 0] = v0;
131 mesh_triangles[triangle_index * 3 + 1] = v1;
132 mesh_triangles[triangle_index * 3 + 2] = v2;
133 mesh_shader[triangle_index] = patch->shader;
134 mesh_smooth[triangle_index] = patch->smooth;
135
136 if (mesh_ptex_face_id) {
137 mesh_ptex_face_id[triangle_index] = patch->patch_index;
138 }
139 if (mesh_ptex_uv) {
140 mesh_ptex_uv[triangle_index * 3 + 0] = uv0;
141 mesh_ptex_uv[triangle_index * 3 + 0] = uv1;
142 mesh_ptex_uv[triangle_index * 3 + 0] = uv2;
143 }
144
145 /* TODO: batch together multiple triangles. */
146 float2 uv[3] = {uv0, uv1, uv2};
148 attr.interp(sub.patch->patch_index, sub.face_index, sub.corner, &triangle_index, uv, 1);
149 }
150}
151
152void EdgeDice::add_grid_triangles_and_stitch(const SubPatch &sub, const int Mu, const int Mv)
153{
154 const float du = 1.0f / (float)Mu;
155 const float dv = 1.0f / (float)Mv;
156 const int grid_vertex_offset = sub.inner_grid_vert_offset;
157 int triangle_index = sub.triangles_offset;
158
159 /* Create inner grid. */
160 for (int j = 1; j < Mv; j++) {
161 for (int i = 1; i < Mu; i++) {
162 const float u = i * du;
163 const float v = j * dv;
164 const int center_i = grid_vertex_offset + (i - 1) + (j - 1) * (Mu - 1);
165
166 set_vertex(sub, center_i, sub.map_uv(make_float2(u, v)));
167
168 if (i < Mu - 1 && j < Mv - 1) {
169 const int i1 = grid_vertex_offset + (i - 1) + (j - 1) * (Mu - 1);
170 const int i2 = grid_vertex_offset + i + (j - 1) * (Mu - 1);
171 const int i3 = grid_vertex_offset + i + j * (Mu - 1);
172 const int i4 = grid_vertex_offset + (i - 1) + j * (Mu - 1);
173
174 const float2 uv1 = sub.map_uv(make_float2(u, v));
175 const float2 uv2 = sub.map_uv(make_float2(u + du, v));
176 const float2 uv3 = sub.map_uv(make_float2(u + du, v + dv));
177 const float2 uv4 = sub.map_uv(make_float2(u, v + dv));
178
179 set_triangle(sub, triangle_index++, i1, i2, i3, uv1, uv2, uv3);
180 set_triangle(sub, triangle_index++, i1, i3, i4, uv1, uv3, uv4);
181 }
182 }
183 }
184
185 /* Stitch inner grid to edges. */
186 for (int edge = 0; edge < 4; edge++) {
187 const int outer_T = sub.edges[edge].edge->T;
188 const int inner_T = ((edge % 2) == 0) ? Mu - 2 : Mv - 2;
189
190 float2 inner_uv, outer_uv, inner_uv_step, outer_uv_step;
191 switch (edge) {
192 case 0:
193 inner_uv = make_float2(du, dv);
194 outer_uv = make_float2(0.0f, 0.0f);
195 inner_uv_step = make_float2(du, 0.0f);
196 outer_uv_step = make_float2(1.0f / (float)outer_T, 0.0f);
197 break;
198 case 1:
199 inner_uv = make_float2(1.0f - du, dv);
200 outer_uv = make_float2(1.0f, 0.0f);
201 inner_uv_step = make_float2(0.0f, dv);
202 outer_uv_step = make_float2(0.0f, 1.0f / (float)outer_T);
203 break;
204 case 2:
205 inner_uv = make_float2(1.0f - du, 1.0f - dv);
206 outer_uv = make_float2(1.0f, 1.0f);
207 inner_uv_step = make_float2(-du, 0.0f);
208 outer_uv_step = make_float2(-1.0f / (float)outer_T, 0.0f);
209 break;
210 case 3:
211 default:
212 inner_uv = make_float2(du, 1.0f - dv);
213 outer_uv = make_float2(0.0f, 1.0f);
214 inner_uv_step = make_float2(0.0f, -dv);
215 outer_uv_step = make_float2(0.0f, -1.0f / (float)outer_T);
216 break;
217 }
218
219 /* Stitch together two arrays of verts with triangles. At each step, we compare using
220 * the next verts on both sides, to find the split direction with the smallest
221 * diagonal, and use that in order to keep the triangle shape reasonable. */
222 for (size_t i = 0, j = 0; i < inner_T || j < outer_T;) {
223 const int v0 = sub.get_vert_along_grid_edge(edge, i);
224 const int v1 = sub.get_vert_along_edge(edge, j);
225 int v2;
226
227 const float2 uv0 = sub.map_uv(inner_uv);
228 const float2 uv1 = sub.map_uv(outer_uv);
229 float2 uv2;
230
231 if (j == outer_T) {
232 v2 = sub.get_vert_along_grid_edge(edge, ++i);
233 inner_uv += inner_uv_step;
234 uv2 = sub.map_uv(inner_uv);
235 }
236 else if (i == inner_T) {
237 v2 = sub.get_vert_along_edge(edge, ++j);
238 outer_uv += outer_uv_step;
239 uv2 = sub.map_uv(outer_uv);
240 }
241 else {
242 /* Length of diagonals. */
243 const int v2_a = sub.get_vert_along_edge(edge, j + 1);
244 const int v2_b = sub.get_vert_along_grid_edge(edge, i + 1);
245
246 const float len_a = len_squared(mesh_P[v0] - mesh_P[v2_a]);
247 const float len_b = len_squared(mesh_P[v1] - mesh_P[v2_b]);
248
249 /* Use smallest diagonal. */
250 if (len_a < len_b) {
251 v2 = v2_a;
252 outer_uv += outer_uv_step;
253 uv2 = sub.map_uv(outer_uv);
254 j++;
255 }
256 else {
257 v2 = v2_b;
258 inner_uv += inner_uv_step;
259 uv2 = sub.map_uv(inner_uv);
260 i++;
261 }
262 }
263
264 set_triangle(sub, triangle_index++, v0, v1, v2, uv0, uv1, uv2);
265 }
266 }
267}
268
269void EdgeDice::add_triangle_strip(const SubPatch &sub, const int left_edge, const int right_edge)
270{
271 /* Stitch triangles from side to side, edge in the other direction has T = 1. */
272 const int left_T = sub.edges[left_edge].edge->T;
273 const int right_T = sub.edges[right_edge].edge->T;
274
275 float2 left_uv, right_uv, left_uv_step, right_uv_step;
276 if (right_edge == 0) {
277 left_uv = make_float2(0.0f, 1.0f);
278 right_uv = make_float2(0.0f, 0.0f);
279 left_uv_step = make_float2(1.0f / (float)left_T, 0.0f);
280 right_uv_step = make_float2(1.0f / (float)right_T, 0.0f);
281 }
282 else {
283 left_uv = make_float2(0.0f, 0.0f);
284 right_uv = make_float2(1.0f, 0.0f);
285 left_uv_step = make_float2(0.0f, 1.0f / (float)left_T);
286 right_uv_step = make_float2(0.0f, 1.0f / (float)right_T);
287 }
288
289 /* Stitch together two arrays of verts with triangles. at each step, we compare using the next
290 * verts on both sides, to find the split direction with the smallest diagonal, and use that
291 * in order to keep the triangle shape reasonable. */
292 int triangle_index = sub.triangles_offset;
293
294 for (size_t i = 0, j = 0; i < left_T || j < right_T;) {
295 const int v0 = sub.get_vert_along_edge_reverse(left_edge, i);
296 const int v1 = sub.get_vert_along_edge(right_edge, j);
297 int v2;
298
299 const float2 uv0 = sub.map_uv(left_uv);
300 const float2 uv1 = sub.map_uv(right_uv);
301 float2 uv2;
302
303 if (j == right_T) {
304 v2 = sub.get_vert_along_edge_reverse(left_edge, ++i);
305 left_uv += left_uv_step;
306 uv2 = sub.map_uv(left_uv);
307 }
308 else if (i == left_T) {
309 v2 = sub.get_vert_along_edge(right_edge, ++j);
310 right_uv += right_uv_step;
311 uv2 = sub.map_uv(right_uv);
312 }
313 else {
314 /* Length of diagonals. */
315 const int v2_a = sub.get_vert_along_edge(right_edge, j + 1);
316 const int v2_b = sub.get_vert_along_edge_reverse(left_edge, i + 1);
317
318 const float len_a = len_squared(mesh_P[v0] - mesh_P[v2_a]);
319 const float len_b = len_squared(mesh_P[v1] - mesh_P[v2_b]);
320
321 /* Use smallest diagonal. */
322 if (len_a < len_b) {
323 v2 = v2_a;
324 right_uv += right_uv_step;
325 uv2 = sub.map_uv(right_uv);
326 j++;
327 }
328 else {
329 v2 = v2_b;
330 left_uv += left_uv_step;
331 uv2 = sub.map_uv(left_uv);
332 i++;
333 }
334 }
335
336 set_triangle(sub, triangle_index++, v0, v1, v2, uv0, uv1, uv2);
337 }
338}
339
341{
342 for (int edge = 0; edge < 4; edge++) {
343 const int t = sub.edges[edge].edge->T;
344 const int i_start = (sub.edges[edge].own_vertex) ? 0 : 1;
345 const int i_end = (sub.edges[edge].own_edge) ? t : 1;
346
347 /* set verts on the edge of the patch */
348 for (int i = i_start; i < i_end; i++) {
349 const float f = i / (float)t;
350
351 float2 uv;
352 switch (edge) {
353 case 0:
354 uv = make_float2(f, 0.0f);
355 break;
356 case 1:
357 uv = make_float2(1.0f, f);
358 break;
359 case 2:
360 uv = make_float2(1.0f - f, 1.0f);
361 break;
362 case 3:
363 default:
364 uv = make_float2(0.0f, 1.0f - f);
365 break;
366 }
367
368 const int vert_index = sub.get_vert_along_edge(edge, i);
369 set_vertex(sub, vert_index, sub.map_uv(uv));
370 }
371 }
372}
373
375{
376 /* Compute inner grid size with scale factor. */
377 const int Mu = max(sub.edges[0].edge->T, sub.edges[2].edge->T);
378 const int Mv = max(sub.edges[3].edge->T, sub.edges[1].edge->T);
379
380 if (Mv == 1) {
381 /* No inner grid, stitch triangles from side to side. */
382 add_triangle_strip(sub, 2, 0);
383 }
384 else if (Mu == 1) {
385 /* No inner grid, stitch triangles from side to side. */
386 add_triangle_strip(sub, 3, 1);
387 }
388 else {
389#if 0
390 /* Doesn't work very well, especially at grazing angles. */
391 const float S = scale_factor(sub, ef, Mu, Mv);
392 const int grid_Mu = max((int)ceilf(S * Mu), 1); // XXX handle 0 & 1?
393 const int grid_Mv = max((int)ceilf(S * Mv), 1); // XXX handle 0 & 1?
394 add_grid_triangles_and_stitch(sub, grid_Mu, grid_Mv);
395#else
397#endif
398 }
399}
400
402{
403 for (int edge = 0; edge < 3; edge++) {
404 const int t = sub.edges[edge].edge->T;
405 const int i_start = (sub.edges[edge].own_vertex) ? 0 : 1;
406 const int i_end = (sub.edges[edge].own_edge) ? t : 1;
407
408 /* set verts on the edge of the patch */
409 for (int i = i_start; i < i_end; i++) {
410 const float f = i / (float)t;
411
412 float2 uv;
413 switch (edge) {
414 case 0:
415 uv = make_float2(f, 0.0f);
416 break;
417 case 1:
418 uv = make_float2(1.0f - f, f);
419 break;
420 case 2:
421 default:
422 uv = make_float2(0.0f, 1.0f - f);
423 break;
424 }
425
426 const int vert_index = sub.get_vert_along_edge(edge, i);
427 set_vertex(sub, vert_index, sub.map_uv(uv));
428 }
429 }
430}
431
433{
434 const int M = max(max(sub.edges[0].edge->T, sub.edges[1].edge->T), sub.edges[2].edge->T);
435 const float d = 1.0f / (float)(M + 1);
436
437 int triangle_index = sub.triangles_offset;
438
439 if (M == 1) {
440 /* Single triangle. */
441 set_triangle(sub,
442 triangle_index++,
443 sub.edges[0].start_vert_index(),
444 sub.edges[1].start_vert_index(),
445 sub.edges[2].start_vert_index(),
446 sub.map_uv(make_float2(0.0f, 0.0f)),
447 sub.map_uv(make_float2(1.0f, 0.0f)),
448 sub.map_uv(make_float2(0.0f, 1.0f)));
449 assert(triangle_index == sub.triangles_offset + sub.calc_num_triangles());
450 return;
451 }
452 if (M == 2) {
453 /* Edges have 2 segments or less. */
454 int num_split = 0;
455 int split_0 = -1;
456 for (int i = 0; i < 3; i++) {
457 if (sub.edges[i].edge->T == 2) {
458 num_split++;
459 if (split_0 == -1) {
460 split_0 = i;
461 }
462 }
463 }
464 /* When two edges have 2 segments, we assume split_0 is the first of two consecutive edges. */
465 if (split_0 == 0 && sub.edges[2].edge->T == 2) {
466 split_0 = 2;
467 }
468
469 const int split_1 = (split_0 + 1) % 3;
470 const int split_2 = (split_0 + 2) % 3;
471
472 const int v[3] = {sub.edges[0].start_vert_index(),
473 sub.edges[1].start_vert_index(),
474 sub.edges[2].start_vert_index()};
475 const int mid_v[3] = {sub.get_vert_along_edge(0, 1),
476 sub.get_vert_along_edge(1, 1),
477 sub.get_vert_along_edge(2, 1)};
478 const float2 uv[3] = {sub.map_uv(make_float2(0.0f, 0.0f)),
479 sub.map_uv(make_float2(1.0f, 0.0f)),
480 sub.map_uv(make_float2(0.0f, 1.0f))};
481 const float2 mid_uv[3] = {sub.map_uv(make_float2(0.5f, 0.0f)),
482 sub.map_uv(make_float2(0.5f, 0.5f)),
483 sub.map_uv(make_float2(0.0f, 0.5f))};
484
485 if (num_split == 3) {
486 /* All edges have two segments
487 * /\
488 * /--\
489 * / \/ \
490 * ------- */
491 set_triangle(sub, triangle_index++, v[0], mid_v[0], mid_v[2], uv[0], mid_uv[0], mid_uv[2]);
492 set_triangle(sub, triangle_index++, v[1], mid_v[1], mid_v[0], uv[1], mid_uv[1], mid_uv[0]);
493 set_triangle(sub, triangle_index++, v[2], mid_v[2], mid_v[1], uv[2], mid_uv[2], mid_uv[1]);
495 sub, triangle_index++, mid_v[0], mid_v[1], mid_v[2], mid_uv[0], mid_uv[1], mid_uv[2]);
496 }
497 else {
498 /* One edge has two segments.
499 * / \
500 * / | \
501 * / | \
502 * ------- */
503 set_triangle(sub,
504 triangle_index++,
505 v[split_0],
506 mid_v[split_0],
507 v[split_2],
508 uv[split_0],
509 mid_uv[split_0],
510 uv[split_2]);
511 if (num_split == 1) {
512 set_triangle(sub,
513 triangle_index++,
514 mid_v[split_0],
515 v[split_1],
516 v[split_2],
517 mid_uv[split_0],
518 uv[split_1],
519 uv[split_2]);
520 }
521 else {
522 /* Two edges have two segments.
523 * /|\
524 * / | \
525 * / |/ \
526 * ------- */
527 set_triangle(sub,
528 triangle_index++,
529 mid_v[split_0],
530 v[split_1],
531 mid_v[split_1],
532 mid_uv[split_0],
533 uv[split_1],
534 mid_uv[split_1]);
535 set_triangle(sub,
536 triangle_index++,
537 mid_v[split_0],
538 mid_v[split_1],
539 v[split_2],
540 mid_uv[split_0],
541 mid_uv[split_1],
542 uv[split_2]);
543 }
544 }
545 assert(triangle_index == sub.triangles_offset + sub.calc_num_triangles());
546 return;
547 }
548
549 const int inner_M = M - 2;
550
551 for (int j = 0; j < inner_M; j++) {
552 for (int i = 0; i < j + 1; i++) {
553 const int i_next = i + 1;
554 const int j_next = j + 1;
555
556 const float2 inner_uv = make_float2(d, d);
557
558 const int v0 = sub.get_inner_grid_vert_triangle(i, j);
559 const int v1 = sub.get_inner_grid_vert_triangle(i_next, j_next);
560 const int v2 = sub.get_inner_grid_vert_triangle(i, j_next);
561
562 const float2 uv0 = sub.map_uv(inner_uv + make_float2(i, j - i) * d);
563 const float2 uv1 = sub.map_uv(inner_uv + make_float2(i_next, j - i) * d);
564 const float2 uv2 = sub.map_uv(inner_uv + make_float2(i, j_next - i) * d);
565
566 set_vertex(sub, v0, uv0);
567 if (j == inner_M - 1) {
568 set_vertex(sub, v1, uv1);
569 set_vertex(sub, v2, uv2);
570 }
571
572 set_triangle(sub, triangle_index++, v0, v1, v2, uv0, uv1, uv2);
573
574 if (i < j) {
575 const int v3 = sub.get_inner_grid_vert_triangle(i_next, j);
576 const float2 uv3 = sub.map_uv(inner_uv + make_float2(i_next, j - i_next) * d);
577
578 set_vertex(sub, v3, uv3);
579
580 set_triangle(sub, triangle_index++, v0, v3, v1, uv0, uv3, uv1);
581 }
582 }
583 }
584
585 assert(triangle_index == sub.triangles_offset + inner_M * inner_M);
586
587 /* Stitch inner grid to edges. */
588 for (int edge = 0; edge < 3; edge++) {
589 const int outer_T = sub.edges[edge].edge->T;
590 const int inner_T = inner_M;
591
592 float2 inner_uv, outer_uv, inner_uv_step, outer_uv_step;
593 switch (edge) {
594 case 0:
595 inner_uv = make_float2(d, d);
596 outer_uv = make_float2(0.0f, 0.0f);
597 inner_uv_step = make_float2(d, 0.0f);
598 outer_uv_step = make_float2(1.0f / (float)outer_T, 0.0f);
599 break;
600 case 1:
601 inner_uv = make_float2(1.0f - 2.0f * d, d);
602 outer_uv = make_float2(1.0f, 0.0f);
603 inner_uv_step = make_float2(-d, d);
604 outer_uv_step = make_float2(-1.0f / (float)outer_T, 1.0f / (float)outer_T);
605 break;
606 case 2:
607 default:
608 inner_uv = make_float2(d, 1.0f - 2.0f * d);
609 outer_uv = make_float2(0.0f, 1.0f);
610 inner_uv_step = make_float2(0.0f, -d);
611 outer_uv_step = make_float2(0.0f, -1.0f / (float)outer_T);
612 break;
613 }
614
615 /* Stitch together two arrays of verts with triangles. At each step, we compare using
616 * the next verts on both sides, to find the split direction with the smallest
617 * diagonal, and use that in order to keep the triangle shape reasonable. */
618 for (size_t i = 0, j = 0; i < inner_T || j < outer_T;) {
619 const int v0 = sub.get_vert_along_grid_edge(edge, i);
620 const int v1 = sub.get_vert_along_edge(edge, j);
621 int v2;
622
623 const float2 uv0 = sub.map_uv(inner_uv);
624 const float2 uv1 = sub.map_uv(outer_uv);
625 float2 uv2;
626
627 if (j == outer_T) {
628 v2 = sub.get_vert_along_grid_edge(edge, ++i);
629 inner_uv += inner_uv_step;
630 uv2 = sub.map_uv(inner_uv);
631 }
632 else if (i == inner_T) {
633 v2 = sub.get_vert_along_edge(edge, ++j);
634 outer_uv += outer_uv_step;
635 uv2 = sub.map_uv(outer_uv);
636 }
637 else {
638 /* Length of diagonals. */
639 const int v2_a = sub.get_vert_along_edge(edge, j + 1);
640 const int v2_b = sub.get_vert_along_grid_edge(edge, i + 1);
641
642 const float len_a = len_squared(mesh_P[v0] - mesh_P[v2_a]);
643 const float len_b = len_squared(mesh_P[v1] - mesh_P[v2_b]);
644
645 /* Use smallest diagonal. */
646 if (len_a < len_b) {
647 v2 = v2_a;
648 outer_uv += outer_uv_step;
649 uv2 = sub.map_uv(outer_uv);
650 j++;
651 }
652 else {
653 v2 = v2_b;
654 inner_uv += inner_uv_step;
655 uv2 = sub.map_uv(inner_uv);
656 i++;
657 }
658 }
659
660 set_triangle(sub, triangle_index++, v0, v1, v2, uv0, uv1, uv2);
661 }
662 }
663
664 assert(triangle_index == sub.triangles_offset + sub.calc_num_triangles());
665}
666
667void EdgeDice::dice(const DiagSplit &split)
668{
669 const size_t num_subpatches = split.get_num_subpatches();
670
671 /* Vertex coordinates for sides. Needs to be done first because tessellation depends
672 * on these coordinates and they are unique assigned to a subpatch for determinism. */
673 parallel_for(blocked_range<size_t>(0, num_subpatches, 8), [&](const blocked_range<size_t> &r) {
674 for (size_t i = r.begin(); i != r.end(); i++) {
675 const SubPatch &subpatch = split.get_subpatch(i);
676 if (subpatch.shape == SubPatch::TRIANGLE) {
677 tri_set_sides(subpatch);
678 }
679 else {
680 quad_set_sides(subpatch);
681 }
682 }
683 });
684
685 /* Inner vertex coordinates and triangles. */
686 parallel_for(blocked_range<size_t>(0, num_subpatches, 8), [&](const blocked_range<size_t> &r) {
687 for (size_t i = r.begin(); i != r.end(); i++) {
688 const SubPatch &subpatch = split.get_subpatch(i);
689 if (subpatch.shape == SubPatch::TRIANGLE) {
690 tri_dice(subpatch);
691 }
692 else {
693 quad_dice(subpatch);
694 }
695 }
696 });
697}
698
#define D
#define A2
Definition RandGen.cpp:28
#define A1
Definition RandGen.cpp:27
btScalar max
Attribute * find(ustring name) const
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
T * data_for_write(const int step=0)
ProjectionTransform worldtoraster
void tri_set_sides(const SubPatch &sub)
Definition dice.cpp:401
void set_vertex(const SubPatch &sub, const int index, const float2 uv)
Definition dice.cpp:100
void quad_dice(const SubPatch &sub)
Definition dice.cpp:374
packed_normal * mesh_N
Definition dice.h:47
void add_triangle_strip(const SubPatch &sub, const int left_edge, const int right_edge)
Definition dice.cpp:269
float quad_area(const float3 &a, const float3 &b, const float3 &c, const float3 &d)
Definition dice.cpp:63
bool * mesh_smooth
Definition dice.h:45
int * mesh_triangles
Definition dice.h:43
float2 * mesh_ptex_uv
Definition dice.h:49
float * mesh_ptex_face_id
Definition dice.h:48
int * mesh_shader
Definition dice.h:44
float3 eval_projected(const SubPatch &sub, const float2 uv)
Definition dice.cpp:51
float scale_factor(const SubPatch &sub, const int Mu, const int Mv)
Definition dice.cpp:68
void tri_dice(const SubPatch &sub)
Definition dice.cpp:432
SubdAttributeInterpolation & interpolation
Definition dice.h:42
void add_grid_triangles_and_stitch(const SubPatch &sub, const int Mu, const int Mv)
Definition dice.cpp:152
EdgeDice(const SubdParams &params, const int num_verts, const int num_triangles, SubdAttributeInterpolation &interpolation)
Definition dice.cpp:17
packed_float3 * mesh_P
Definition dice.h:46
void quad_set_sides(const SubPatch &sub)
Definition dice.cpp:340
void set_triangle(const SubPatch &sub, const int triangle_index, const int v0, const int v1, const int v2, const float2 uv0, const float2 uv1, const float2 uv2)
Definition dice.cpp:117
SubdParams params
Definition dice.h:41
void dice(const DiagSplit &split)
Definition dice.cpp:667
AttributeSet attributes
packed_float3 * get_position_for_write()
size_t get_num_subd_base_verts() const
Definition scene/mesh.h:229
void resize_mesh(const int numverts, const int numtris)
Definition patch.h:12
virtual void eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, const float u, float v) const =0
int shader
Definition patch.h:21
bool smooth
Definition patch.h:22
int patch_index
Definition patch.h:20
int get_inner_grid_vert_triangle(int i, int j) const
Definition subpatch.h:234
int get_vert_along_edge_reverse(const int edge, const int n) const
Definition subpatch.h:229
int get_vert_along_edge(const int edge, const int n) const
Definition subpatch.h:224
int inner_grid_vert_offset
Definition subpatch.h:101
enum SubPatch::@276156351240133265277051231052000353032241376365 shape
@ TRIANGLE
Definition subpatch.h:99
int calc_num_triangles() const
Definition subpatch.h:189
const Patch * patch
Definition subpatch.h:94
int face_index
Definition subpatch.h:96
int get_vert_along_grid_edge(const int edge, const int n) const
Definition subpatch.h:242
Edge edges[4]
Definition subpatch.h:165
float2 map_uv(float2 uv) const
Definition subpatch.h:295
int corner
Definition subpatch.h:97
int triangles_offset
Definition subpatch.h:103
vector< SubdAttribute > triangle_attributes
vector< SubdAttribute > vertex_attributes
CCL_NAMESPACE_END CCL_NAMESPACE_BEGIN ccl_device_inline float3 transform_perspective(const ccl_private ProjectionTransform *t, const float3 a)
#define CCL_NAMESPACE_END
#define assert(assertion)
#define index(i)
@ ATTR_STD_VERTEX_NORMAL
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
ccl_device_inline float triangle_area(const ccl_private float3 &v1, const ccl_private float3 &v2, const ccl_private float3 &v3)
#define M
#define N
#define sqrtf
#define make_float2
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
int T
Definition subpatch.h:36
int start_vert_index() const
Definition subpatch.h:119
SubEdge * edge
Definition subpatch.h:107
std::function< void(const int, const int, const int, const int *, const float2 *, const int)> interp
bool ptex
Definition dice.h:27
Mesh * mesh
Definition dice.h:26
Camera * camera
Definition dice.h:33
float dicing_rate
Definition dice.h:31
float x
float y
packed_normal()=default