Subsurf: Support subdivision of loose elements
[blender.git] / source / blender / blenkernel / intern / subdiv_eval.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2018 by Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/subdiv_eval.c
27  *  \ingroup bke
28  */
29
30 #include "BKE_subdiv.h"
31
32 #include "DNA_mesh_types.h"
33 #include "DNA_meshdata_types.h"
34
35 #include "BLI_utildefines.h"
36 #include "BLI_bitmap.h"
37 #include "BLI_math_vector.h"
38
39 #include "BKE_customdata.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #ifdef WITH_OPENSUBDIV
44 #  include "opensubdiv_evaluator_capi.h"
45 #  include "opensubdiv_topology_refiner_capi.h"
46 #endif
47
48 void BKE_subdiv_eval_begin(Subdiv *subdiv)
49 {
50 #ifdef WITH_OPENSUBDIV
51         if (subdiv->evaluator == NULL) {
52                 BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
53                 subdiv->evaluator = openSubdiv_createEvaluatorFromTopologyRefiner(
54                         subdiv->topology_refiner);
55                 BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
56         }
57         else {
58                 /* TODO(sergey): Check for topology change. */
59         }
60 #else
61         UNUSED_VARS(subdiv);
62 #endif
63 }
64
65 #ifdef WITH_OPENSUBDIV
66 static void set_coarse_positions(Subdiv *subdiv, const Mesh *mesh)
67 {
68         const MVert *mvert = mesh->mvert;
69         const MLoop *mloop = mesh->mloop;
70         const MPoly *mpoly = mesh->mpoly;
71         /* Mark vertices which needs new coordinates. */
72         /* TODO(sergey): This is annoying to calculate this on every update,
73          * maybe it's better to cache this mapping. Or make it possible to have
74          * OpenSubdiv's vertices match mesh ones?
75          */
76         BLI_bitmap *vertex_used_map =
77                 BLI_BITMAP_NEW(mesh->totvert, "vert used map");
78         for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
79                 const MPoly *poly = &mpoly[poly_index];
80                 for (int corner = 0; corner < poly->totloop; corner++) {
81                         const MLoop *loop = &mloop[poly->loopstart + corner];
82                         BLI_BITMAP_ENABLE(vertex_used_map, loop->v);
83                 }
84         }
85         for (int vertex_index = 0, manifold_veretx_index = 0;
86              vertex_index < mesh->totvert;
87              vertex_index++)
88         {
89                 if (!BLI_BITMAP_TEST_BOOL(vertex_used_map, vertex_index)) {
90                         continue;
91                 }
92                 const MVert *vertex = &mvert[vertex_index];
93                 subdiv->evaluator->setCoarsePositions(
94                         subdiv->evaluator,
95                         vertex->co,
96                         manifold_veretx_index, 1);
97                 manifold_veretx_index++;
98         }
99         MEM_freeN(vertex_used_map);
100 }
101
102 static void set_face_varying_data_from_uv(Subdiv *subdiv,
103                                           const MLoopUV *mloopuv,
104                                           const int layer_index)
105 {
106         OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
107         OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
108         const int num_faces = topology_refiner->getNumFaces(topology_refiner);
109         const MLoopUV *mluv = mloopuv;
110         /* TODO(sergey): OpenSubdiv's C-API converter can change winding of
111          * loops of a face, need to watch for that, to prevent wrong UVs assigned.
112          */
113         for (int face_index = 0; face_index < num_faces; ++face_index) {
114                 const int num_face_vertices = topology_refiner->getNumFaceVertices(
115                         topology_refiner, face_index);
116                 const int *uv_indicies = topology_refiner->getFaceFVarValueIndices(
117                         topology_refiner, face_index, layer_index);
118                 for (int vertex_index = 0;
119                      vertex_index < num_face_vertices;
120                      vertex_index++, mluv++)
121                 {
122                         evaluator->setFaceVaryingData(evaluator,
123                                                       mluv->uv,
124                                                       uv_indicies[vertex_index],
125                                                       1);
126                 }
127         }
128 }
129 #endif
130
131 void BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const Mesh *mesh)
132 {
133 #ifdef WITH_OPENSUBDIV
134         BKE_subdiv_eval_begin(subdiv);
135         /* Set coordinates of base mesh vertices. */
136         set_coarse_positions(subdiv, mesh);
137         /* Set face-varyign data to UV maps. */
138         const int num_uv_layers =
139                 CustomData_number_of_layers(&mesh->ldata, CD_MLOOPUV);
140         for (int layer_index = 0; layer_index < num_uv_layers; layer_index++) {
141                 const MLoopUV *mloopuv = CustomData_get_layer_n(
142                         &mesh->ldata, CD_MLOOPUV, layer_index);
143                 set_face_varying_data_from_uv(subdiv, mloopuv, layer_index);
144                 /* NOTE: Currently evaluator can only handle single face varying layer.
145                  * This is a limitation of C-API and some underlying helper classes from
146                  * our side which will get fixed.
147                  */
148                 break;
149         }
150         /* Update evaluator to the new coarse geometry. */
151         BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
152         subdiv->evaluator->refine(subdiv->evaluator);
153         BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
154 #else
155         UNUSED_VARS(subdiv, mesh);
156 #endif
157 }
158
159 /* ========================== Single point queries ========================== */
160
161 void BKE_subdiv_eval_limit_point(
162         Subdiv *subdiv,
163         const int ptex_face_index,
164         const float u, const float v,
165         float P[3])
166 {
167         BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
168                                                     ptex_face_index,
169                                                     u, v,
170                                                     P, NULL, NULL);
171 }
172
173 void BKE_subdiv_eval_limit_point_and_derivatives(
174         Subdiv *subdiv,
175         const int ptex_face_index,
176         const float u, const float v,
177         float P[3], float dPdu[3], float dPdv[3])
178 {
179 #ifdef WITH_OPENSUBDIV
180         subdiv->evaluator->evaluateLimit(subdiv->evaluator,
181                                          ptex_face_index,
182                                          u, v,
183                                          P, dPdu, dPdv);
184 #else
185         UNUSED_VARS(subdiv, ptex_face_index, u, v, P, dPdu, dPdv);
186 #endif
187 }
188
189 void BKE_subdiv_eval_limit_point_and_normal(
190         Subdiv *subdiv,
191         const int ptex_face_index,
192         const float u, const float v,
193         float P[3], float N[3])
194 {
195         float dPdu[3], dPdv[3];
196         BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
197                                                     ptex_face_index,
198                                                     u, v,
199                                                     P, dPdu, dPdv);
200         cross_v3_v3v3(N, dPdu, dPdv);
201         normalize_v3(N);
202 }
203
204 void BKE_subdiv_eval_limit_point_and_short_normal(
205         Subdiv *subdiv,
206         const int ptex_face_index,
207         const float u, const float v,
208         float P[3], short N[3])
209 {
210         float N_float[3];
211         BKE_subdiv_eval_limit_point_and_normal(subdiv,
212                                                ptex_face_index,
213                                                u, v,
214                                                P, N_float);
215         normal_float_to_short_v3(N, N_float);
216 }
217
218 void BKE_subdiv_eval_face_varying(
219         Subdiv *subdiv,
220         const int ptex_face_index,
221         const float u, const float v,
222         float face_varying[2])
223 {
224 #ifdef WITH_OPENSUBDIV
225         subdiv->evaluator->evaluateFaceVarying(subdiv->evaluator,
226                                                ptex_face_index,
227                                                u, v,
228                                                face_varying);
229 #else
230         UNUSED_VARS(subdiv, ptex_face_index, u, v, face_varying);
231 #endif
232 }
233
234 /* ===================  Patch queries at given resolution =================== */
235
236 /* Move buffer forward by a given number of bytes. */
237 static void buffer_apply_offset(void **buffer, const int offset)
238 {
239   *buffer = ((unsigned char *)*buffer) + offset;
240 }
241
242 /* Write given number of floats to the beginning of given buffer.  */
243 static void buffer_write_float_value(void **buffer,
244                                      const float *values_buffer, int num_values)
245 {
246         memcpy(*buffer, values_buffer, sizeof(float) * num_values);
247 }
248
249 /* Similar to above, just operates with short values. */
250 static void buffer_write_short_value(void **buffer,
251                                      const short *values_buffer, int num_values)
252 {
253         memcpy(*buffer, values_buffer, sizeof(short) * num_values);
254 }
255
256 void BKE_subdiv_eval_limit_patch_resolution_point(
257         Subdiv *subdiv,
258         const int ptex_face_index,
259         const int resolution,
260         void *buffer, const int offset, const int stride)
261 {
262         buffer_apply_offset(&buffer, offset);
263         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
264         for (int y = 0; y < resolution; y++) {
265                 const float v = y * inv_resolution_1;
266                 for (int x = 0; x < resolution; x++) {
267                         const float u = x * inv_resolution_1;
268                         BKE_subdiv_eval_limit_point(subdiv,
269                                                     ptex_face_index,
270                                                     u, v,
271                                                     buffer);
272                         buffer_apply_offset(&buffer, stride);
273                 }
274         }
275 }
276
277 void BKE_subdiv_eval_limit_patch_resolution_point_and_derivatives(
278         Subdiv *subdiv,
279         const int ptex_face_index,
280         const int resolution,
281         void *point_buffer, const int point_offset, const int point_stride,
282         void *du_buffer, const int du_offset, const int du_stride,
283         void *dv_buffer, const int dv_offset, const int dv_stride)
284 {
285         buffer_apply_offset(&point_buffer, point_offset);
286         buffer_apply_offset(&du_buffer, du_offset);
287         buffer_apply_offset(&dv_buffer, dv_offset);
288         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
289         for (int y = 0; y < resolution; y++) {
290                 const float v = y * inv_resolution_1;
291                 for (int x = 0; x < resolution; x++) {
292                         const float u = x * inv_resolution_1;
293                         BKE_subdiv_eval_limit_point_and_derivatives(
294                                 subdiv,
295                                 ptex_face_index,
296                                 u, v,
297                                 point_buffer, du_buffer, dv_buffer);
298                         buffer_apply_offset(&point_buffer, point_stride);
299                         buffer_apply_offset(&du_buffer, du_stride);
300                         buffer_apply_offset(&dv_buffer, dv_stride);
301                 }
302         }
303 }
304
305 void BKE_subdiv_eval_limit_patch_resolution_point_and_normal(
306         Subdiv *subdiv,
307         const int ptex_face_index,
308         const int resolution,
309         void *point_buffer, const int point_offset, const int point_stride,
310         void *normal_buffer, const int normal_offset, const int normal_stride)
311 {
312         buffer_apply_offset(&point_buffer, point_offset);
313         buffer_apply_offset(&normal_buffer, normal_offset);
314         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
315         for (int y = 0; y < resolution; y++) {
316                 const float v = y * inv_resolution_1;
317                 for (int x = 0; x < resolution; x++) {
318                         const float u = x * inv_resolution_1;
319                         float normal[3];
320                         BKE_subdiv_eval_limit_point_and_normal(
321                                 subdiv,
322                                 ptex_face_index,
323                                 u, v,
324                                 point_buffer, normal);
325                         buffer_write_float_value(&normal_buffer, normal, 3);
326                         buffer_apply_offset(&point_buffer, point_stride);
327                         buffer_apply_offset(&normal_buffer, normal_stride);
328                 }
329         }
330 }
331
332 void BKE_subdiv_eval_limit_patch_resolution_point_and_short_normal(
333         Subdiv *subdiv,
334         const int ptex_face_index,
335         const int resolution,
336         void *point_buffer, const int point_offset, const int point_stride,
337         void *normal_buffer, const int normal_offset, const int normal_stride)
338 {
339         buffer_apply_offset(&point_buffer, point_offset);
340         buffer_apply_offset(&normal_buffer, normal_offset);
341         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
342         for (int y = 0; y < resolution; y++) {
343                 const float v = y * inv_resolution_1;
344                 for (int x = 0; x < resolution; x++) {
345                         const float u = x * inv_resolution_1;
346                         short normal[3];
347                         BKE_subdiv_eval_limit_point_and_short_normal(
348                                 subdiv,
349                                 ptex_face_index,
350                                 u, v,
351                                 point_buffer, normal);
352                         buffer_write_short_value(&normal_buffer, normal, 3);
353                         buffer_apply_offset(&point_buffer, point_stride);
354                         buffer_apply_offset(&normal_buffer, normal_stride);
355                 }
356         }
357 }