Skip to content

Commit ecbe049

Browse files
committed
finished with patch
1 parent 3dd2a3f commit ecbe049

File tree

1 file changed

+59
-30
lines changed

1 file changed

+59
-30
lines changed

libs/poly_algos.h

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,17 @@ requires is_vec<t_vec>
213213
while(itervert != vertices.end())
214214
{
215215
const t_vec& vec1 = *itervert;
216-
std::advance(itervert, 1); if(itervert == vertices.end()) break;
216+
217+
std::advance(itervert, 1);
218+
if(itervert == vertices.end())
219+
break;
220+
217221
const t_vec& vec2 = *itervert;
218-
std::advance(itervert, 1); if(itervert == vertices.end()) break;
222+
223+
std::advance(itervert, 1);
224+
if(itervert == vertices.end())
225+
break;
226+
219227
const t_vec& vec3 = *itervert;
220228
std::advance(itervert, 1);
221229

@@ -261,9 +269,15 @@ requires is_vec<t_vec>
261269
{
262270
// uv coords at vertices
263271
const t_vec& uv1 = *iteruv;
264-
std::advance(iteruv, 1); if(iteruv == uvs.end()) break;
272+
std::advance(iteruv, 1);
273+
if(iteruv == uvs.end())
274+
break;
275+
265276
const t_vec& uv2 = *iteruv;
266-
std::advance(iteruv, 1); if(iteruv == uvs.end()) break;
277+
std::advance(iteruv, 1);
278+
if(iteruv == uvs.end())
279+
break;
280+
267281
const t_vec& uv3 = *iteruv;
268282
std::advance(iteruv, 1);
269283

@@ -308,7 +322,7 @@ subdivide_triangles(const std::tuple<t_cont<t_vec>, t_cont<t_vec>, t_cont<t_vec>
308322
requires is_vec<t_vec>
309323
{
310324
auto tupDiv = tup;
311-
for(std::size_t i=0; i<iters; ++i)
325+
for(std::size_t i = 0; i < iters; ++i)
312326
tupDiv = subdivide_triangles<t_vec, t_cont>(tupDiv);
313327
return tupDiv;
314328
}
@@ -383,7 +397,7 @@ requires is_vec<t_vec>
383397
*/
384398
template<class t_mat, class t_vec, template<class...> class t_cont = std::vector>
385399
std::tuple<t_cont<t_vec>, t_cont<t_cont<std::size_t>>, t_cont<t_vec>, t_cont<t_cont<t_vec>>>
386-
create_plane(const t_vec& norm, typename t_vec::value_type l0 = 1,
400+
create_plane(const t_vec& normal, typename t_vec::value_type l0 = 1,
387401
std::optional<typename t_vec::value_type> _l1 = std::nullopt)
388402
requires is_vec<t_vec>
389403
{
@@ -399,12 +413,12 @@ requires is_vec<t_vec>
399413

400414
// rotate vertices according to given normal
401415
t_vec norm_old = create<t_vec>({ 0, 0, -1 });
402-
t_mat rot = rotation<t_mat, t_vec>(norm_old, norm);
416+
t_mat rot = rotation<t_mat, t_vec>(norm_old, normal);
403417
for(t_vec& vec : vertices)
404418
vec = rot * vec;
405419

406420
t_cont<t_cont<std::size_t>> faces = { { 0, 1, 2, 3 } };
407-
t_cont<t_vec> normals = { norm };
421+
t_cont<t_vec> normals = { normal };
408422

409423
t_cont<t_cont<t_vec>> uvs =
410424
{{
@@ -419,7 +433,7 @@ requires is_vec<t_vec>
419433

420434

421435
/**
422-
* create a patch
436+
* create a patch, z = f(x, y)
423437
* @returns [vertices, face vertex indices, face normals, face uvs]
424438
*/
425439
template<class t_func, class t_mat, class t_vec,
@@ -428,57 +442,72 @@ template<class t_func, class t_mat, class t_vec,
428442
std::tuple<t_cont<t_vec>, t_cont<t_cont<std::size_t>>, t_cont<t_vec>, t_cont<t_cont<t_vec>>>
429443
create_patch(const t_func& func,
430444
t_real width = 1., t_real height = 1.,
431-
std::size_t num_points = 16)
445+
std::size_t num_points_x = 16, std::size_t num_points_y = 16,
446+
const t_vec& normal = create<t_vec>({ 0, 0, 1 }))
432447
requires is_vec<t_vec>
433448
{
449+
// rotate according to given normal
450+
t_vec norm_old = create<t_vec>({ 0, 0, 1 });
451+
t_mat rot = rotation<t_mat, t_vec>(norm_old, normal);
452+
434453
// create 2d grid in (x, y) for patch
435454
t_cont<t_vec> vertices;
436455
t_cont<t_cont<std::size_t>> faces;
437456
t_cont<t_vec> normals;
438457
t_cont<t_cont<t_vec>> uvs;
439458

440-
vertices.reserve(num_points * num_points);
441-
faces.reserve((num_points - 1) * (num_points - 1));
442-
normals.reserve((num_points - 1) * (num_points - 1));
443-
uvs.reserve((num_points - 1) * (num_points - 1));
459+
vertices.reserve(num_points_x * num_points_y);
460+
faces.reserve((num_points_x - 1) * (num_points_y - 1));
461+
normals.reserve((num_points_x - 1) * (num_points_y - 1));
462+
uvs.reserve((num_points_x - 1) * (num_points_y - 1));
444463

445-
for(std::size_t j = 0; j < num_points; ++j)
464+
for(std::size_t j = 0; j < num_points_y; ++j)
446465
{
447466
t_real y = -height*0.5 + height *
448-
static_cast<t_real>(j)/static_cast<t_real>(num_points - 1);
467+
static_cast<t_real>(j)/static_cast<t_real>(num_points_y - 1);
468+
469+
t_real v0 = static_cast<t_real>(j - 1) / static_cast<t_real>(num_points_x - 1);
470+
t_real v1 = static_cast<t_real>(j) / static_cast<t_real>(num_points_y - 1);
449471

450-
for(std::size_t i = 0; i < num_points; ++i)
472+
for(std::size_t i = 0; i < num_points_x; ++i)
451473
{
474+
// create vertices
452475
t_real x = -width*0.5 + width *
453-
static_cast<t_real>(i)/static_cast<t_real>(num_points - 1);
476+
static_cast<t_real>(i)/static_cast<t_real>(num_points_x - 1);
454477

455478
t_real z = func(x, y);
456-
vertices.emplace_back(m::create<t_vec>({ x, y, z }));
479+
vertices.emplace_back(rot * m::create<t_vec>({ x, y, z }));
457480

481+
// create faces, normals and uv coords
458482
if(i > 0 && j > 0)
459483
{
460-
std::size_t idx_ij = j*num_points + i;
461-
std::size_t idx_im1j = j*num_points + i - 1;
462-
std::size_t idx_i1jm1 = (j - 1)*num_points + i;
463-
std::size_t idx_im1jm1 = (j - 1)*num_points + i - 1;
484+
// face
485+
std::size_t idx_ij = j*num_points_x + i;
486+
std::size_t idx_im1j = j*num_points_x + i - 1;
487+
std::size_t idx_i1jm1 = (j - 1)*num_points_x + i;
488+
std::size_t idx_im1jm1 = (j - 1)*num_points_x + i - 1;
464489

465490
faces.emplace_back(t_cont<std::size_t>{{
466491
idx_im1jm1, idx_i1jm1, idx_ij, idx_im1j
467492
}});
468493

494+
// normal
469495
t_vec n = cross<t_vec>({
470496
vertices[idx_i1jm1] - vertices[idx_im1jm1],
471497
vertices[idx_ij] - vertices[idx_im1jm1]
472498
});
473499
n /= norm<t_vec>(n);
500+
normals.emplace_back(rot * n);
474501

475-
normals.emplace_back(std::move(n));
502+
// uv
503+
t_real u0 = static_cast<t_real>(i - 1) / static_cast<t_real>(num_points_x - 1);
504+
t_real u1 = static_cast<t_real>(i) / static_cast<t_real>(num_points_x - 1);
476505

477506
uvs.emplace_back(t_cont<t_vec>{{
478-
create<t_vec>({ 0, 0 }),
479-
create<t_vec>({ 1, 0 }),
480-
create<t_vec>({ 1, 1 }),
481-
create<t_vec>({ 0, 1 }),
507+
create<t_vec>({ u0, v0 }), // face vertex 0
508+
create<t_vec>({ u1, v0 }), // face vertex 1
509+
create<t_vec>({ u1, v1 }), // face vertex 2
510+
create<t_vec>({ u0, v1 }), // face vertex 3
482511
}});
483512
}
484513
}
@@ -641,7 +670,7 @@ requires is_vec<t_vec>
641670
t_cont<t_vec> vertices;
642671
t_cont<t_real> vertices_u;
643672

644-
for(std::size_t pt=0; pt<num_points; ++pt)
673+
for(std::size_t pt = 0; pt < num_points; ++pt)
645674
{
646675
const t_real u = t_real(pt)/t_real(num_points);
647676
const t_real phi = u * t_real(2)*pi<t_real>;
@@ -662,7 +691,7 @@ requires is_vec<t_vec>
662691
t_cont<t_vec> normals;
663692
t_cont<t_cont<t_vec>> uvs;
664693

665-
for(std::size_t face=0; face<num_points; ++face)
694+
for(std::size_t face = 0; face < num_points; ++face)
666695
{
667696
std::size_t idx0 = face*2 + 0; // top 1
668697
std::size_t idx1 = face*2 + 1; // bottom 1

0 commit comments

Comments
 (0)