Blender  V3.3
subpatch.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __SUBD_SUBPATCH_H__
5 #define __SUBD_SUBPATCH_H__
6 
7 #include "util/map.h"
8 #include "util/types.h"
9 
11 
12 /* Subpatch */
13 
14 class Subpatch {
15  public:
16  class Patch *patch; /* Patch this is a subpatch of. */
18 
19  struct edge_t {
20  int T;
21  int offset; /* Offset along main edge, interpretation depends on the two flags below. */
22 
25 
26  struct Edge *edge;
27 
28  int get_vert_along_edge(int n) const;
29  };
30 
31  /*
32  * eu1
33  * c01 --------- c11
34  * | |
35  * ev0 | | ev1
36  * | |
37  * c00 --------- c10
38  * eu0
39  */
40 
41  union {
42  float2 corners[4]; /* UV within patch, clockwise starting from uv (0, 0) towards (0, 1) etc. */
43  struct {
45  };
46  };
47 
48  union {
49  edge_t
50  edges[4]; /* Edges of this subpatch, each edge starts at the corner of the same index. */
51  struct {
53  };
54  };
55 
56  explicit Subpatch(Patch *patch = nullptr)
57  : patch(patch),
58  c00(zero_float2()),
59  c01(make_float2(0.0f, 1.0f)),
60  c11(one_float2()),
61  c10(make_float2(1.0f, 0.0f))
62  {
63  }
64 
66  : patch(patch), c00(c00), c01(c01), c11(c11), c10(c10)
67  {
68  }
69 
71  {
72  int Mu = max(edge_u0.T, edge_u1.T);
73  int Mv = max(edge_v0.T, edge_v1.T);
74  Mu = max(Mu, 2);
75  Mv = max(Mv, 2);
76  return (Mu - 1) * (Mv - 1);
77  }
78 
79  int calc_num_triangles() const
80  {
81  int Mu = max(edge_u0.T, edge_u1.T);
82  int Mv = max(edge_v0.T, edge_v1.T);
83  Mu = max(Mu, 2);
84  Mv = max(Mv, 2);
85 
86  int inner_triangles = (Mu - 2) * (Mv - 2) * 2;
87  int edge_triangles = edge_u0.T + edge_u1.T + edge_v0.T + edge_v1.T + (Mu - 2) * 2 +
88  (Mv - 2) * 2;
89 
90  return inner_triangles + edge_triangles;
91  }
92 
93  int get_vert_along_edge(int e, int n) const;
94 
95  int get_vert_along_grid_edge(int edge, int n) const
96  {
97  int Mu = max(edge_u0.T, edge_u1.T);
98  int Mv = max(edge_v0.T, edge_v1.T);
99  Mu = max(Mu, 2);
100  Mv = max(Mv, 2);
101 
102  switch (edge) {
103  case 0:
104  return inner_grid_vert_offset + n * (Mu - 1);
105  case 1:
106  return inner_grid_vert_offset + (Mu - 1) * (Mv - 2) + n;
107  case 2:
108  return inner_grid_vert_offset + ((Mu - 1) * (Mv - 1) - 1) - n * (Mu - 1);
109  case 3:
110  return inner_grid_vert_offset + (Mu - 2) - n;
111  }
112 
113  return -1;
114  }
115 };
116 
117 struct Edge {
118  /* Number of segments the edge will be diced into, see DiagSplit paper. */
119  int T;
120 
121  /* top is edge adjacent to start, bottom is adjacent to end. */
123 
126 
129 
130  /* Index of the second vert from this edges corner along the edge towards the next corner. */
132 
133  /* Vertices on edge are to be stitched. */
135 
136  /* Key to match this edge with others to be stitched with.
137  * The ints in the pair are ordered stitching indices */
138  pair<int, int> stitch_edge_key;
139 
140  /* Full T along edge (may be larger than T for edges split from ngon edges) */
146 
148  : T(0),
149  top(nullptr),
150  bottom(nullptr),
151  top_offset(-1),
152  bottom_offset(-1),
153  top_indices_decrease(false),
155  start_vert_index(-1),
156  end_vert_index(-1),
157  second_vert_index(-1),
158  is_stitch_edge(false),
159  stitch_edge_T(0),
160  stitch_offset(0)
161  {
162  }
163 
164  int get_vert_along_edge(int n) const
165  {
166  assert(n >= 0 && n <= T);
167 
168  if (n == 0) {
169  return start_vert_index;
170  }
171  else if (n == T) {
172  return end_vert_index;
173  }
174 
175  return second_vert_index + n - 1;
176  }
177 };
178 
180 {
181  assert(n >= 0 && n <= T);
182 
184  n = offset + n;
185  }
187  n = edge->T - offset - T + n;
188  }
190  n = offset + T - n;
191  }
193  n = edge->T - offset - n;
194  }
195 
196  return edge->get_vert_along_edge(n);
197 }
198 
199 inline int Subpatch::get_vert_along_edge(int edge, int n) const
200 {
201  return edges[edge].get_vert_along_edge(n);
202 }
203 
205 
206 #endif /* __SUBD_SUBPATCH_H__ */
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
int get_vert_along_grid_edge(int edge, int n) const
Definition: subpatch.h:95
edge_t edges[4]
Definition: subpatch.h:50
float2 c11
Definition: subpatch.h:44
Subpatch(Patch *patch=nullptr)
Definition: subpatch.h:56
int inner_grid_vert_offset
Definition: subpatch.h:17
edge_t edge_v1
Definition: subpatch.h:52
edge_t edge_u0
Definition: subpatch.h:52
float2 c01
Definition: subpatch.h:44
class Patch * patch
Definition: subpatch.h:16
int calc_num_triangles() const
Definition: subpatch.h:79
edge_t edge_u1
Definition: subpatch.h:52
Subpatch(Patch *patch, float2 c00, float2 c01, float2 c11, float2 c10)
Definition: subpatch.h:65
int calc_num_inner_verts() const
Definition: subpatch.h:70
float2 corners[4]
Definition: subpatch.h:42
edge_t edge_v0
Definition: subpatch.h:52
float2 c10
Definition: subpatch.h:44
float2 c00
Definition: subpatch.h:44
int get_vert_along_edge(int e, int n) const
Definition: subpatch.h:199
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
ccl_device_inline float2 one_float2()
Definition: math_float2.h:67
ccl_device_inline float2 zero_float2()
Definition: math_float2.h:62
#define make_float2(x, y)
Definition: metal/compat.h:203
int stitch_offset
Definition: subpatch.h:142
int T
Definition: subpatch.h:119
bool bottom_indices_decrease
Definition: subpatch.h:125
int second_vert_index
Definition: subpatch.h:131
Edge * bottom
Definition: subpatch.h:122
Edge()
Definition: subpatch.h:147
int stitch_end_vert_index
Definition: subpatch.h:145
int start_vert_index
Definition: subpatch.h:127
int bottom_offset
Definition: subpatch.h:124
Edge * top
Definition: subpatch.h:122
int stitch_top_offset
Definition: subpatch.h:143
pair< int, int > stitch_edge_key
Definition: subpatch.h:138
bool is_stitch_edge
Definition: subpatch.h:134
bool top_indices_decrease
Definition: subpatch.h:125
int get_vert_along_edge(int n) const
Definition: subpatch.h:164
int stitch_edge_T
Definition: subpatch.h:141
int top_offset
Definition: subpatch.h:124
int stitch_start_vert_index
Definition: subpatch.h:144
int end_vert_index
Definition: subpatch.h:128
bool sub_edges_created_in_reverse_order
Definition: subpatch.h:24
int get_vert_along_edge(int n) const
Definition: subpatch.h:179
struct Edge * edge
Definition: subpatch.h:26
bool indices_decrease_along_edge
Definition: subpatch.h:23
float max