Guest User

Untitled

a guest
Nov 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. //
  2. // Copyright (c) 2017 Rasmus Barringer
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in all
  12. // copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. // SOFTWARE.
  21. //
  22.  
  23. #ifndef ADJACENCY_H
  24. #define ADJACENCY_H
  25.  
  26. #include <stdint.h>
  27.  
  28. // Computes adjacency information from an index buffer (mapping triangle edges to their neighbor).
  29. // The adjacency buffer must be able to hold index_count elements.
  30. void compute_adjacency(const uint32_t* indices, unsigned index_count, uint32_t* adjacency);
  31.  
  32. #ifdef ADJACENCY_IMPLEMENTATION
  33.  
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <algorithm>
  37.  
  38. namespace {
  39. struct AdjacencyEdge {
  40. uint64_t edge_id;
  41. uint32_t triangle_edge;
  42. };
  43.  
  44. static inline bool operator < (AdjacencyEdge a, AdjacencyEdge b) {
  45. return a.edge_id < b.edge_id;
  46. }
  47.  
  48. static inline uint64_t adjacency_edge_id(unsigned v0, unsigned v1) {
  49. return v0 > v1 ? v0 | ((uint64_t)v1 << 32) : v1 | ((uint64_t)v0 << 32);
  50. }
  51. }
  52.  
  53. void compute_adjacency(const uint32_t* indices, unsigned index_count, uint32_t* adjacency) {
  54. memset(adjacency, 0xff, sizeof(adjacency[0])*index_count);
  55.  
  56. AdjacencyEdge* edges = (AdjacencyEdge*)malloc(sizeof(AdjacencyEdge)*index_count);
  57.  
  58. for (unsigned i = 0; i < index_count; i += 3) {
  59. unsigned v0 = indices[i+0];
  60. unsigned v1 = indices[i+1];
  61. unsigned v2 = indices[i+2];
  62.  
  63. AdjacencyEdge edge0 = { adjacency_edge_id(v1, v2), i+0 };
  64. AdjacencyEdge edge1 = { adjacency_edge_id(v2, v0), i+1 };
  65. AdjacencyEdge edge2 = { adjacency_edge_id(v0, v1), i+2 };
  66.  
  67. edges[i+0] = edge0;
  68. edges[i+1] = edge1;
  69. edges[i+2] = edge2;
  70. }
  71.  
  72. std::sort(edges, edges + index_count);
  73.  
  74. for (unsigned i = 0; i+1 < index_count; ++i) {
  75. if (edges[i+0].edge_id == edges[i+1].edge_id) {
  76. unsigned e0 = edges[i+0].triangle_edge;
  77. unsigned e1 = edges[i+1].triangle_edge;
  78.  
  79. adjacency[e0] = e1;
  80. adjacency[e1] = e0;
  81.  
  82. ++i;
  83. }
  84. }
  85.  
  86. free(edges);
  87. }
  88.  
  89. #endif
  90.  
  91. #endif
Add Comment
Please, Sign In to add comment