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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
|
//
// # Yocto/Shape: Tiny Library for shape operations for graphics
//
//
// Yocto/Shape is a collection of utilities for manipulating shapes in 3D
// graphics, with a focus on triangle and quad meshes. We support both low-level
// geometry operation and whole shape operations.
//
//
// ## Geometry functions
//
// The library supports basic geomtry functions such as computing
// line/triangle/quad normals and areas, picking points on triangles
// and the like. In these functions triangles are parameterized with us written
// w.r.t the (p1-p0) and (p2-p0) axis respectively. Quads are internally handled
// as pairs of two triangles p0,p1,p3 and p2,p3,p1, with the u/v coordinates
// of the second triangle corrected as 1-u and 1-v to produce a quad
// parametrization where u and v go from 0 to 1. Degenerate quads with p2==p3
// represent triangles correctly, an this convention is used throught the
// library. This is equivalent to Intel's Embree.
//
//
// ## Shape functions
//
// We provide a small number of utilities for shape manipulation for index
// triangle and quad meshes, indexed line and point sets and indexed beziers.
// The utliities collected here are written to support a global illumination
// rendering and not for generic geometry processing. We support operation for
// shape smoothing, shape subdivision (including Catmull-Clark subdivs), and
// example shape creation.
//
// 1. compute line tangents, and triangle and quad areas and normals with
// `line_tangent()`, `triamgle_normal()`, `quad_normal()` and
// `line_length()`, `triangle_area()` and `quad_normal()`
// 2. interpolate values over primitives with `interpolate_line()`,
// `interpolate_triangle()` and `interpolate_quad()`
// 3. evaluate Bezier curves and derivatives with `interpolate_bezier()` and
// `interpolate_bezier_derivative()`
// 4. compute smooth normals and tangents with `compute_normals()`
// `compute_tangents()`
// 5. compute tangent frames from texture coordinates with
// `compute_tangent_spaces()`
// 6. compute skinning with `compute_skinning()` and
// `compute_matrix_skinning()`
// 6. create shapes with `make_proc_image()`, `make_hair()`,
// `make_points()`
// 7. merge element with `marge_lines()`, `marge_triangles()`, `marge_quads()`
// 8. shape sampling with `sample_points()`, `sample_lines()`,
// `sample_triangles()`; initialize the sampling CDFs with
// `sample_points_cdf()`, `sample_lines_cdf()`,
// `sample_triangles_cdf()`
// 9. sample a could of point over a surface with `sample_triangles()`
// 10. get edges and boundaries with `get_edges()`
// 11. convert quads to triangles with `quads_to_triangles()`
// 12. convert face varying to vertex shared representations with
// `convert_face_varying()`
// 13. subdivide elements by edge splits with `subdivide_lines()`,
// `subdivide_triangles()`, `subdivide_quads()`, `subdivide_beziers()`
// 14. Catmull-Clark subdivision surface with `subdivide_catmullclark()`
//
//
// ## Shape IO
//
// We support reading and writing shapes in OBJ and PLY.
//
// 1. load/save shapes with `load_shape()`/`save_shape()`
//
//
//
// LICENSE:
//
// Copyright (c) 2016 -- 2019 Fabio Pellacini
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
#ifndef _YOCTO_SHAPE_H_
#define _YOCTO_SHAPE_H_
#ifndef YOCTO_QUADS_AS_TRIANGLES
#define YOCTO_QUADS_AS_TRIANGLES 1
#endif
// -----------------------------------------------------------------------------
// INCLUDES
// -----------------------------------------------------------------------------
#include "yocto_math.h"
// -----------------------------------------------------------------------------
// COMPUTATION OF PER_VERTEX PROPETIES
// -----------------------------------------------------------------------------
namespace yocto {
// Compute per-vertex normals/tangents for lines/triangles/quads.
vector<vec3f> compute_tangents(
const vector<vec2i>& lines, const vector<vec3f>& positions);
vector<vec3f> compute_normals(
const vector<vec3i>& triangles, const vector<vec3f>& positions);
vector<vec3f> compute_normals(
const vector<vec4i>& quads, const vector<vec3f>& positions);
void compute_tangents(vector<vec3f>& tangents, const vector<vec2i>& lines,
const vector<vec3f>& positions);
void compute_normals(vector<vec3f>& normals, const vector<vec3i>& triangles,
const vector<vec3f>& positions);
void compute_normals(vector<vec3f>& normals, const vector<vec4i>& quads,
const vector<vec3f>& positions);
// Compute per-vertex tangent space for triangle meshes.
// Tangent space is defined by a four component vector.
// The first three components are the tangent with respect to the u texcoord.
// The fourth component is the sign of the tangent wrt the v texcoord.
// Tangent frame is useful in normal mapping.
vector<vec4f> compute_tangent_spaces(const vector<vec3i>& triangles,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords);
void compute_tangent_spaces(vector<vec4f>& tangents,
const vector<vec3i>& triangles, const vector<vec3f>& positions,
const vector<vec3f>& normals, const vector<vec2f>& texcoords);
// Apply skinning to vertex position and normals.
pair<vector<vec3f>, vector<vec3f>> compute_skinning(
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec4f>& weights, const vector<vec4i>& joints,
const vector<frame3f>& xforms);
void compute_skinning(vector<vec3f>& skinned_positions,
vector<vec3f>& skinned_normals, const vector<vec3f>& positions,
const vector<vec3f>& normals, const vector<vec4f>& weights,
const vector<vec4i>& joints, const vector<frame3f>& xforms);
// Apply skinning as specified in Khronos glTF.
pair<vector<vec3f>, vector<vec3f>> compute_matrix_skinning(
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec4f>& weights, const vector<vec4i>& joints,
const vector<mat4f>& xforms);
void compute_matrix_skinning(vector<vec3f>& skinned_positions,
vector<vec3f>& skinned_normals, const vector<vec3f>& positions,
const vector<vec3f>& normals, const vector<vec4f>& weights,
const vector<vec4i>& joints, const vector<mat4f>& xforms);
} // namespace yocto
// -----------------------------------------------------------------------------
// COMPUTATION OF PER_VERTEX PROPETIES
// -----------------------------------------------------------------------------
namespace yocto {
// Flip vertex normals
vector<vec3f> flip_normals(const vector<vec3f>& normals);
void flip_normals(vector<vec3f>& flipped, const vector<vec3f>& normals);
// Flip face orientation
vector<vec3i> flip_triangles(const vector<vec3i>& triangles);
vector<vec4i> flip_quads(const vector<vec4i>& quads);
void flip_triangles(vector<vec3i>& flipped, const vector<vec3i>& triangles);
void flip_quads(vector<vec4i>& flipped, const vector<vec4i>& quads);
// Align vertex positions. Alignment is 0: none, 1: min, 2: max, 3: center.
vector<vec3f> align_vertices(
const vector<vec3f>& positions, const vec3i& alignment);
void align_vertices(vector<vec3f>& aligned, const vector<vec3f>& positions,
const vec3i& alignment);
} // namespace yocto
// -----------------------------------------------------------------------------
// EDGE AND GRID DATA STRUCTURES
// -----------------------------------------------------------------------------
namespace yocto {
// Dictionary to store edge information. `index` is the index to the edge
// array, `edges` the array of edges and `nfaces` the number of adjacent faces.
// We store only bidirectional edges to keep the dictionary small. Use the
// functions below to access this data.
struct edge_map {
unordered_map<vec2i, int> index = {};
vector<vec2i> edges = {};
vector<int> nfaces = {};
};
// Initialize an edge map with elements.
edge_map make_edge_map(const vector<vec3i>& triangles);
edge_map make_edge_map(const vector<vec4i>& quads);
void insert_edges(edge_map& emap, const vector<vec3i>& triangles);
void insert_edges(edge_map& emap, const vector<vec4i>& quads);
// Insert an edge and return its index
int insert_edge(edge_map& emap, const vec2i& edge);
// Get the edge index / insertion count
int edge_index(const edge_map& emap, const vec2i& edge);
// Get list of edges / boundary edges
int num_edges(const edge_map& emap);
vector<vec2i> get_edges(const edge_map& emap);
vector<vec2i> get_boundary(const edge_map& emap);
void get_edges(const edge_map& emap, vector<vec2i>& edges);
void get_boundary(const edge_map& emap, vector<vec2i>& edges);
vector<vec2i> get_edges(const vector<vec3i>& triangles);
vector<vec2i> get_edges(const vector<vec4i>& quads);
// A sparse grid of cells, containing list of points. Cells are stored in
// a dictionary to get sparsity. Helpful for nearest neighboor lookups.
struct hash_grid {
float cell_size = 0;
float cell_inv_size = 0;
vector<vec3f> positions = {};
unordered_map<vec3i, vector<int>> cells = {};
};
// Create a hash_grid
hash_grid make_hash_grid(float cell_size);
hash_grid make_hash_grid(const vector<vec3f>& positions, float cell_size);
// Inserts a point into the grid
int insert_vertex(hash_grid& grid, const vec3f& position);
// Finds the nearest neighboors within a given radius
void find_neightbors(const hash_grid& grid, vector<int>& neighboors,
const vec3f& position, float max_radius);
void find_neightbors(const hash_grid& grid, vector<int>& neighboors, int vertex,
float max_radius);
} // namespace yocto
// -----------------------------------------------------------------------------
// SHAPE ELEMENT CONVERSION AND GROUPING
// -----------------------------------------------------------------------------
namespace yocto {
// Convert quads to triangles
vector<vec3i> quads_to_triangles(const vector<vec4i>& quads);
void quads_to_triangles(vector<vec3i>& triangles, const vector<vec4i>& quads);
// Convert triangles to quads by creating degenerate quads
vector<vec4i> triangles_to_quads(const vector<vec3i>& triangles);
void triangles_to_quads(vector<vec4i>& quads, const vector<vec3i>& triangles);
// Convert beziers to lines using 3 lines for each bezier.
vector<vec4i> bezier_to_lines(vector<vec2i>& lines);
void bezier_to_lines(vector<vec2i>& lines, const vector<vec4i>& beziers);
// Convert face-varying data to single primitives. Returns the quads indices
// and face ids and filled vectors for pos, norm, texcoord and colors.
void split_facevarying(vector<vec4i>& split_quads,
vector<vec3f>& split_positions, vector<vec3f>& split_normals,
vector<vec2f>& split_texcoords, const vector<vec4i>& quadspos,
const vector<vec4i>& quadsnorm, const vector<vec4i>& quadstexcoord,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords);
// Split primitives per id
vector<vector<vec2i>> ungroup_lines(
const vector<vec2i>& lines, const vector<int>& ids);
vector<vector<vec3i>> ungroup_triangles(
const vector<vec3i>& triangles, const vector<int>& ids);
vector<vector<vec4i>> ungroup_quads(
const vector<vec4i>& quads, const vector<int>& ids);
void ungroup_lines(vector<vector<vec2i>>& split_lines,
const vector<vec2i>& lines, const vector<int>& ids);
void ungroup_triangles(vector<vector<vec3i>>& split_triangles,
const vector<vec3i>& triangles, const vector<int>& ids);
void ungroup_quads(vector<vector<vec4i>>& split_quads,
const vector<vec4i>& quads, const vector<int>& ids);
// Weld vertices within a threshold.
pair<vector<vec3f>, vector<int>> weld_vertices(
const vector<vec3f>& positions, float threshold);
pair<vector<vec3i>, vector<vec3f>> weld_triangles(
const vector<vec3i>& triangles, const vector<vec3f>& positions,
float threshold);
pair<vector<vec4i>, vector<vec3f>> weld_quads(const vector<vec4i>& quads,
const vector<vec3f>& positions, float threshold);
// Weld vertices within a threshold.
void weld_vertices(vector<vec3f>& welded_positions, vector<int>& indices,
const vector<vec3f>& positions, float threshold);
void weld_triangles(vector<vec3i>& welded_triangles,
vector<vec3f>& welded_positions, const vector<vec3i>& triangles,
const vector<vec3f>& positions, float threshold);
void weld_quads(vector<vec4i>& welded_quads, vector<vec3f>& welded_positions,
const vector<vec4i>& quads, const vector<vec3f>& positions,
float threshold);
// Merge shape elements
void merge_lines(
vector<vec2i>& lines, const vector<vec2i>& merge_lines, int num_verts);
void merge_triangles(vector<vec3i>& triangles,
const vector<vec2i>& merge_triangles, int num_verts);
void merge_quads(
vector<vec4i>& quads, const vector<vec4i>& merge_quads, int num_verts);
void merge_lines(vector<vec2i>& lines, vector<vec3f>& positions,
vector<vec3f>& tangents, vector<vec2f>& texcoords, vector<float>& radius,
const vector<vec2i>& merge_lines, const vector<vec3f>& merge_positions,
const vector<vec3f>& merge_tangents,
const vector<vec2f>& merge_texturecoords,
const vector<float>& merge_radius);
void merge_triangles(vector<vec3i>& triangles, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords,
const vector<vec2i>& merge_triangles, const vector<vec3f>& merge_positions,
const vector<vec3f>& merge_normals,
const vector<vec2f>& merge_texturecoords);
void merge_quads(vector<vec4i>& quads, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords,
const vector<vec4i>& merge_quads, const vector<vec3f>& merge_positions,
const vector<vec3f>& merge_normals,
const vector<vec2f>& merge_texturecoords);
// Merge quads and triangles
void merge_triangles_and_quads(
vector<vec3i>& triangles, vector<vec4i>& quads, bool force_triangles);
} // namespace yocto
// -----------------------------------------------------------------------------
// SHAPE SUBDIVISION
// -----------------------------------------------------------------------------
namespace yocto {
// Subdivide lines by splitting each line in half.
pair<vector<vec2i>, vector<float>> subdivide_lines(
const vector<vec2i>& lines, const vector<float>& vert, int level);
pair<vector<vec2i>, vector<vec2f>> subdivide_lines(
const vector<vec2i>& lines, const vector<vec2f>& vert, int level);
pair<vector<vec2i>, vector<vec3f>> subdivide_lines(
const vector<vec2i>& lines, const vector<vec3f>& vert, int level);
pair<vector<vec2i>, vector<vec4f>> subdivide_lines(
const vector<vec2i>& lines, const vector<vec4f>& vert, int level);
void subdivide_lines(vector<vec2i>& slines, vector<float>& svert,
const vector<vec2i>& lines, const vector<float>& vert, int level);
void subdivide_lines(vector<vec2i>& slines, vector<vec2f>& svert,
const vector<vec2i>& lines, const vector<vec2f>& vert, int level);
void subdivide_lines(vector<vec2i>& slines, vector<vec3f>& svert,
const vector<vec2i>& lines, const vector<vec3f>& vert, int level);
void subdivide_lines(vector<vec2i>& slines, vector<vec4f>& svert,
const vector<vec2i>& lines, const vector<vec4f>& vert, int level);
void subdivide_lines(vector<vec2i>& slines, vector<vec3f>& spositions,
vector<vec3f>& snormals, vector<vec2f>& stexcoords, vector<vec4f>& scolors,
vector<float>& sradius, const vector<vec2i>& lines,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords, const vector<vec4f>& colors,
const vector<float>& radius, int level);
// Subdivide triangle by splitting each triangle in four, creating new
// vertices for each edge.
pair<vector<vec3i>, vector<float>> subdivide_triangles(
const vector<vec3i>& triangles, const vector<float>& vert, int level);
pair<vector<vec3i>, vector<vec2f>> subdivide_triangles(
const vector<vec3i>& triangles, const vector<vec2f>& vert, int level);
pair<vector<vec3i>, vector<vec3f>> subdivide_triangles(
const vector<vec3i>& triangles, const vector<vec3f>& vert, int level);
pair<vector<vec3i>, vector<vec4f>> subdivide_triangles(
const vector<vec3i>& triangles, const vector<vec4f>& vert, int level);
void subdivide_triangles(vector<vec3i>& striangles, vector<float>& svert,
const vector<vec3i>& triangles, const vector<float>& vert, int level);
void subdivide_triangles(vector<vec3i>& striangles, vector<vec2f>& svert,
const vector<vec3i>& triangles, const vector<vec2f>& vert, int level);
void subdivide_triangles(vector<vec3i>& striangles, vector<vec3f>& svert,
const vector<vec3i>& triangles, const vector<vec3f>& vert, int level);
void subdivide_triangles(vector<vec3i>& striangles, vector<vec4f>& svert,
const vector<vec3i>& triangles, const vector<vec4f>& vert, int level);
void subdivide_triangles(vector<vec3i>& striangles, vector<vec3f>& spositions,
vector<vec3f>& snormals, vector<vec2f>& stexcoords, vector<vec4f>& scolors,
vector<float>& sradius, const vector<vec3i>& triangles,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords, const vector<vec4f>& colors,
const vector<float>& radius, int level);
// Subdivide quads by splitting each quads in four, creating new
// vertices for each edge and for each face.
pair<vector<vec4i>, vector<float>> subdivide_quads(
const vector<vec4i>& quads, const vector<float>& vert, int level);
pair<vector<vec4i>, vector<vec2f>> subdivide_quads(
const vector<vec4i>& quads, const vector<vec2f>& vert, int level);
pair<vector<vec4i>, vector<vec3f>> subdivide_quads(
const vector<vec4i>& quads, const vector<vec3f>& vert, int level);
pair<vector<vec4i>, vector<vec4f>> subdivide_quads(
const vector<vec4i>& quads, const vector<vec4f>& vert, int level);
void subdivide_quads(vector<vec4i>& squads, vector<float>& svert,
const vector<vec4i>& quads, const vector<float>& vert, int level);
void subdivide_quads(vector<vec4i>& squads, vector<vec2f>& svert,
const vector<vec4i>& quads, const vector<vec2f>& vert, int level);
void subdivide_quads(vector<vec4i>& squads, vector<vec3f>& svert,
const vector<vec4i>& quads, const vector<vec3f>& vert, int level);
void subdivide_quads(vector<vec4i>& squads, vector<vec4f>& svert,
const vector<vec4i>& quads, const vector<vec4f>& vert, int level);
void subdivide_quads(vector<vec4i>& squads, vector<vec3f>& spositions,
vector<vec3f>& snormals, vector<vec2f>& stexcoords, vector<vec4f>& scolors,
vector<float>& sradius, const vector<vec4i>& quads,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords, const vector<vec4f>& colors,
const vector<float>& radius, int level);
// Subdivide beziers by splitting each segment in two.
pair<vector<vec4i>, vector<float>> subdivide_beziers(
const vector<vec4i>& beziers, const vector<float>& vert, int level);
pair<vector<vec4i>, vector<vec2f>> subdivide_beziers(
const vector<vec4i>& beziers, const vector<vec2f>& vert, int level);
pair<vector<vec4i>, vector<vec3f>> subdivide_beziers(
const vector<vec4i>& beziers, const vector<vec3f>& vert, int level);
pair<vector<vec4i>, vector<vec4f>> subdivide_beziers(
const vector<vec4i>& beziers, const vector<vec4f>& vert, int level);
void subdivide_beziers(vector<vec4i>& sbeziers, vector<float>& svert,
const vector<vec4i>& beziers, const vector<float>& vert, int level);
void subdivide_beziers(vector<vec4i>& sbeziers, vector<float>& svert,
const vector<vec4i>& beziers, const vector<vec2f>& vert, int level);
void subdivide_beziers(vector<vec4i>& sbeziers, vector<float>& svert,
const vector<vec4i>& beziers, const vector<vec3f>& vert, int level);
void subdivide_beziers(vector<vec4i>& sbeziers, vector<float>& svert,
const vector<vec4i>& beziers, const vector<vec4f>& vert, int level);
void subdivide_beziers(vector<vec4i>& sbeziers, vector<vec3f>& spositions,
vector<vec3f>& snormals, vector<vec2f>& stexcoords, vector<vec4f>& scolors,
vector<float>& sradius, const vector<vec4i>& beziers,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords, const vector<vec4f>& colors,
const vector<float>& radius, int level);
// Subdivide quads using Carmull-Clark subdivision rules.
pair<vector<vec4i>, vector<float>> subdivide_catmullclark(
const vector<vec4i>& quads, const vector<float>& vert, int level,
bool lock_boundary = false);
pair<vector<vec4i>, vector<vec2f>> subdivide_catmullclark(
const vector<vec4i>& quads, const vector<vec2f>& vert, int level,
bool lock_boundary = false);
pair<vector<vec4i>, vector<vec3f>> subdivide_catmullclark(
const vector<vec4i>& quads, const vector<vec3f>& vert, int level,
bool lock_boundary = false);
pair<vector<vec4i>, vector<vec4f>> subdivide_catmullclark(
const vector<vec4i>& quads, const vector<vec4f>& vert, int level,
bool lock_boundary = false);
void subdivide_catmullclark(vector<vec4i>& squads, vector<float>& svert,
const vector<vec4i>& quads, const vector<float>& vert, int level,
bool lock_boundary = false);
void subdivide_catmullclark(vector<vec4i>& squads, vector<vec2f>& svert,
const vector<vec4i>& quads, const vector<vec2f>& vert, int level,
bool lock_boundary = false);
void subdivide_catmullclark(vector<vec4i>& squads, vector<vec3f>& svert,
const vector<vec4i>& quads, const vector<vec3f>& vert, int level,
bool lock_boundary = false);
void subdivide_catmullclark(vector<vec4i>& squads, vector<vec4f>& svert,
const vector<vec4i>& quads, const vector<vec4f>& vert, int level,
bool lock_boundary = false);
void subdivide_catmullclark(vector<vec4i>& squads, vector<vec3f>& spositions,
vector<vec3f>& snormals, vector<vec2f>& stexcoords, vector<vec4f>& scolors,
vector<float>& sradius, const vector<vec4i>& quads,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords, const vector<vec4f>& colors,
const vector<float>& radius, int level);
} // namespace yocto
// -----------------------------------------------------------------------------
// SHAPE SAMPLING
// -----------------------------------------------------------------------------
namespace yocto {
// Pick a point in a point set uniformly.
int sample_points(int npoints, float re);
int sample_points(const vector<float>& cdf, float re);
vector<float> sample_points_cdf(int npoints);
void sample_points_cdf(vector<float>& cdf, int npoints);
// Pick a point on lines uniformly.
pair<int, float> sample_lines(const vector<float>& cdf, float re, float ru);
vector<float> sample_lines_cdf(
const vector<vec2i>& lines, const vector<vec3f>& positions);
void sample_lines_cdf(vector<float>& cdf, const vector<vec2i>& lines,
const vector<vec3f>& positions);
// Pick a point on a triangle mesh uniformly.
pair<int, vec2f> sample_triangles(
const vector<float>& cdf, float re, const vec2f& ruv);
vector<float> sample_triangles_cdf(
const vector<vec3i>& triangles, const vector<vec3f>& positions);
void sample_triangles_cdf(vector<float>& cdf, const vector<vec3i>& triangles,
const vector<vec3f>& positions);
// Pick a point on a quad mesh uniformly.
pair<int, vec2f> sample_quads(
const vector<float>& cdf, float re, const vec2f& ruv);
pair<int, vec2f> sample_quads(const vector<vec4i>& quads,
const vector<float>& cdf, float re, const vec2f& ruv);
vector<float> sample_quads_cdf(
const vector<vec4i>& quads, const vector<vec3f>& positions);
void sample_quads_cdf(vector<float>& cdf, const vector<vec4i>& quads,
const vector<vec3f>& positions);
// Samples a set of points over a triangle/quad mesh uniformly. Returns pos,
// norm and texcoord of the sampled points.
void sample_triangles(vector<vec3f>& sampled_positions,
vector<vec3f>& sampled_normals, vector<vec2f>& sampled_texturecoords,
const vector<vec3i>& triangles, const vector<vec3f>& positions,
const vector<vec3f>& normals, const vector<vec2f>& texcoords, int npoints,
int seed = 7);
void sample_quads(vector<vec3f>& sampled_positions,
vector<vec3f>& sampled_normals, vector<vec2f>& sampled_texturecoords,
const vector<vec4i>& quads, const vector<vec3f>& positions,
const vector<vec3f>& normals, const vector<vec2f>& texcoords, int npoints,
int seed = 7);
} // namespace yocto
// -----------------------------------------------------------------------------
// SHAPE GEODESICS
// -----------------------------------------------------------------------------
namespace yocto {
// Data structure used for geodesic computation
struct geodesic_solver {
struct arc_ {
int node = 0;
float length = 0;
};
struct index_ {
int node = 0;
int index = 0;
};
vector<vector<arc_>> graph = {};
vector<vector<index_>> edge_index = {};
vector<vec3f> positions = {};
vector<vec2i> edges = {};
};
// Construct an edge graph
void init_geodesic_solver(geodesic_solver& solver,
const vector<vec3i>& triangles, const vector<vec3f>& positions);
void compute_geodesic_distances(geodesic_solver& solver,
vector<float>& distances, const vector<int>& sources);
void convert_distance_to_color(
vector<vec4f>& colors, const vector<float>& distances);
} // namespace yocto
// -----------------------------------------------------------------------------
// SHAPE IO FUNCTIONS
// -----------------------------------------------------------------------------
namespace yocto {
// Load/Save a shape
void load_shape(const string& filename, vector<int>& points,
vector<vec2i>& lines, vector<vec3i>& triangles, vector<vec4i>& quads,
vector<vec4i>& quadspos, vector<vec4i>& quadsnorm,
vector<vec4i>& quadstexcoord, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords, vector<vec4f>& colors,
vector<float>& radius, bool facevarying);
void save_shape(const string& filename, const vector<int>& points,
const vector<vec2i>& lines, const vector<vec3i>& triangles,
const vector<vec4i>& quads, const vector<vec4i>& quadspos,
const vector<vec4i>& quadsnorm, const vector<vec4i>& quadstexcoord,
const vector<vec3f>& positions, const vector<vec3f>& normals,
const vector<vec2f>& texcoords, const vector<vec4f>& colors,
const vector<float>& radius, bool ascii = false);
} // namespace yocto
// -----------------------------------------------------------------------------
// SHAPE EXAMPLES
// -----------------------------------------------------------------------------
namespace yocto {
// Parameters for make shape function
struct proc_shape_params {
// clang-format off
enum struct type_t {
quad, floor, cube, sphere, disk, matball, suzanne, box, rect, rect_stack,
uvsphere, uvdisk, uvcylinder, geosphere };
// clang-format on
type_t type = type_t::quad;
int subdivisions = 0;
float scale = 1;
float uvscale = 1;
float rounded = 0;
vec3f aspect = {1, 1, 1}; // for rect, box, cylinder
frame3f frame = identity3x4f;
};
// Make a procedural shape
void make_proc_shape(vector<vec3i>& triangles, vector<vec4i>& quads,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
const proc_shape_params& params);
// Make face-varying quads. For now supports only quad, cube, suzanne, sphere,
// rect, box. Rounding not supported for now.
void make_proc_fvshape(vector<vec4i>& quadspos, vector<vec4i>& quadsnorm,
vector<vec4i>& quadstexcoord, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords,
const proc_shape_params& params);
// Generate lines set along a quad. Returns lines, pos, norm, texcoord, radius.
void make_lines(vector<vec2i>& lines, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords, vector<float>& radius,
int num, int subdivisions, const vec2f& size, const vec2f& uvsize,
const vec2f& line_radius);
// Make point primitives. Returns points, pos, norm, texcoord, radius.
void make_points(vector<int>& points, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords, vector<float>& radius,
int num, float uvsize, float point_radius);
void make_random_points(vector<int>& points, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords, vector<float>& radius,
int num, const vec3f& size, float uvsize, float point_radius,
uint64_t seed);
// Make fair params
struct hair_params {
int num = 0;
int subdivisions = 0;
float length_min = 0.1;
float length_max = 0.1;
float radius_base = 0.001;
float radius_tip = 0.001;
float noise_strength = 0;
float noise_scale = 10;
float clump_strength = 0;
int clump_num = 0;
float rotation_strength = 0;
float rotation_minchia = 0;
int seed = 7;
};
// Make a hair ball around a shape. Returns lines, pos, norm, texcoord, radius.
// length: minimum and maximum length
// rad: minimum and maximum radius from base to tip
// noise: noise added to hair (strength/scale)
// clump: clump added to hair (number/strength)
// rotation: rotation added to hair (angle/strength)
void make_hair(vector<vec2i>& lines, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords, vector<float>& radius,
const vector<vec3i>& striangles, const vector<vec4i>& squads,
const vector<vec3f>& spos, const vector<vec3f>& snorm,
const vector<vec2f>& stexcoord, const hair_params& params);
// Thickens a shape by copying the shape content, rescaling it and flipping its
// normals. Note that this is very much not robust and only useful for trivial
// cases.
void make_shell(vector<vec4i>& quads, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords, float thickness);
// Shape presets used ofr testing.
void make_shape_preset(vector<int>& points, vector<vec2i>& lines,
vector<vec3i>& triangles, vector<vec4i>& quads, vector<vec4i>& quadspos,
vector<vec4i>& quadsnorm, vector<vec4i>& quadstexcoord,
vector<vec3f>& positions, vector<vec3f>& normals, vector<vec2f>& texcoords,
vector<vec4f>& colors, vector<float>& radius, const string& type);
} // namespace yocto
// -----------------------------------------------------------------------------
// GEOMETRY UTILITIES
// -----------------------------------------------------------------------------
namespace yocto {
// Line properties.
inline vec3f line_tangent(const vec3f& p0, const vec3f& p1) {
return normalize(p1 - p0);
}
inline float line_length(const vec3f& p0, const vec3f& p1) {
return length(p1 - p0);
}
// Triangle properties.
inline vec3f triangle_normal(
const vec3f& p0, const vec3f& p1, const vec3f& p2) {
return normalize(cross(p1 - p0, p2 - p0));
}
inline float triangle_area(const vec3f& p0, const vec3f& p1, const vec3f& p2) {
return length(cross(p1 - p0, p2 - p0)) / 2;
}
// Quad propeties.
inline vec3f quad_normal(
const vec3f& p0, const vec3f& p1, const vec3f& p2, const vec3f& p3) {
return normalize(triangle_normal(p0, p1, p3) + triangle_normal(p2, p3, p1));
}
inline float quad_area(
const vec3f& p0, const vec3f& p1, const vec3f& p2, const vec3f& p3) {
return triangle_area(p0, p1, p3) + triangle_area(p2, p3, p1);
}
// Triangle tangent and bitangent from uv
inline pair<vec3f, vec3f> triangle_tangents_fromuv(const vec3f& p0,
const vec3f& p1, const vec3f& p2, const vec2f& uv0, const vec2f& uv1,
const vec2f& uv2);
// Quad tangent and bitangent from uv. Note that we pass a current_uv since
// internally we may want to split the quad in two and we need to known where
// to do it. If not interested in the split, just pass zero2f here.
inline pair<vec3f, vec3f> quad_tangents_fromuv(const vec3f& p0, const vec3f& p1,
const vec3f& p2, const vec3f& p3, const vec2f& uv0, const vec2f& uv1,
const vec2f& uv2, const vec2f& uv3, const vec2f& current_uv);
// Interpolates values over a line parameterized from a to b by u. Same as lerp.
template <typename T>
inline T interpolate_line(const T& p0, const T& p1, float u) {
return p0 * (1 - u) + p1 * u;
}
// Interpolates values over a triangle parameterized by u and v along the
// (p1-p0) and (p2-p0) directions. Same as barycentric interpolation.
template <typename T>
inline T interpolate_triangle(
const T& p0, const T& p1, const T& p2, const vec2f& uv) {
return p0 * (1 - uv.x - uv.y) + p1 * uv.x + p2 * uv.y;
}
// Interpolates values over a quad parameterized by u and v along the
// (p1-p0) and (p2-p1) directions. Same as bilinear interpolation.
template <typename T>
inline T interpolate_quad(
const T& p0, const T& p1, const T& p2, const T& p3, const vec2f& uv) {
#if YOCTO_QUADS_AS_TRIANGLES
if (uv.x + uv.y <= 1) {
return interpolate_triangle(p0, p1, p3, uv);
} else {
return interpolate_triangle(p2, p3, p1, 1 - uv);
}
#else
return p0 * (1 - uv.x) * (1 - uv.y) + p1 * uv.x * (1 - uv.y) +
p2 * uv.x * uv.y + p3 * (1 - uv.x) * uv.y;
#endif
}
// Interpolates values along a cubic Bezier segment parametrized by u.
template <typename T>
inline T interpolate_bezier(
const T& p0, const T& p1, const T& p2, const T& p3, float u) {
return p0 * (1 - u) * (1 - u) * (1 - u) + p1 * 3 * u * (1 - u) * (1 - u) +
p2 * 3 * u * u * (1 - u) + p3 * u * u * u;
}
// Computes the derivative of a cubic Bezier segment parametrized by u.
template <typename T>
inline T interpolate_bezier_derivative(
const T& p0, const T& p1, const T& p2, const T& p3, float u) {
return (p1 - p0) * 3 * (1 - u) * (1 - u) + (p2 - p1) * 6 * u * (1 - u) +
(p3 - p2) * 3 * u * u;
}
// Triangle tangent and bitangent from uv
inline pair<vec3f, vec3f> triangle_tangents_fromuv(const vec3f& p0,
const vec3f& p1, const vec3f& p2, const vec2f& uv0, const vec2f& uv1,
const vec2f& uv2) {
// Follows the definition in http://www.terathon.com/code/tangent.html and
// https://gist.github.com/aras-p/2843984
// normal points up from texture space
auto p = p1 - p0;
auto q = p2 - p0;
auto s = vec2f{uv1.x - uv0.x, uv2.x - uv0.x};
auto t = vec2f{uv1.y - uv0.y, uv2.y - uv0.y};
auto div = s.x * t.y - s.y * t.x;
if (div != 0) {
auto tu = vec3f{t.y * p.x - t.x * q.x, t.y * p.y - t.x * q.y,
t.y * p.z - t.x * q.z} /
div;
auto tv = vec3f{s.x * q.x - s.y * p.x, s.x * q.y - s.y * p.y,
s.x * q.z - s.y * p.z} /
div;
return {tu, tv};
} else {
return {{1, 0, 0}, {0, 1, 0}};
}
}
// Quad tangent and bitangent from uv.
inline pair<vec3f, vec3f> quad_tangents_fromuv(const vec3f& p0, const vec3f& p1,
const vec3f& p2, const vec3f& p3, const vec2f& uv0, const vec2f& uv1,
const vec2f& uv2, const vec2f& uv3, const vec2f& current_uv) {
#if YOCTO_QUADS_AS_TRIANGLES
if (current_uv.x + current_uv.y <= 1) {
return triangle_tangents_fromuv(p0, p1, p3, uv0, uv1, uv3);
} else {
return triangle_tangents_fromuv(p2, p3, p1, uv2, uv3, uv1);
}
#else
return triangle_tangents_fromuv(p0, p1, p3, uv0, uv1, uv3);
#endif
}
} // namespace yocto
#endif
|