Merge branch 'blender2.7'
[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_eval.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 #include "BKE_subdiv.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "opensubdiv_evaluator_capi.h"
45 #include "opensubdiv_topology_refiner_capi.h"
46
47 bool BKE_subdiv_eval_begin(Subdiv *subdiv)
48 {
49         if (subdiv->topology_refiner == NULL) {
50                 /* Happens on input mesh with just loose geometry,
51                  * or when OpenSubdiv is disabled
52                  */
53                 return false;
54         }
55         else if (subdiv->evaluator == NULL) {
56                 BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
57                 subdiv->evaluator = openSubdiv_createEvaluatorFromTopologyRefiner(
58                         subdiv->topology_refiner);
59                 BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
60                 if (subdiv->evaluator == NULL) {
61                         return false;
62                 }
63         }
64         else {
65                 /* TODO(sergey): Check for topology change. */
66         }
67         BKE_subdiv_eval_init_displacement(subdiv);
68         return true;
69 }
70
71 static void set_coarse_positions(Subdiv *subdiv, const Mesh *mesh)
72 {
73         const MVert *mvert = mesh->mvert;
74         const MLoop *mloop = mesh->mloop;
75         const MPoly *mpoly = mesh->mpoly;
76         /* Mark vertices which needs new coordinates. */
77         /* TODO(sergey): This is annoying to calculate this on every update,
78          * maybe it's better to cache this mapping. Or make it possible to have
79          * OpenSubdiv's vertices match mesh ones?
80          */
81         BLI_bitmap *vertex_used_map =
82                 BLI_BITMAP_NEW(mesh->totvert, "vert used map");
83         for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
84                 const MPoly *poly = &mpoly[poly_index];
85                 for (int corner = 0; corner < poly->totloop; corner++) {
86                         const MLoop *loop = &mloop[poly->loopstart + corner];
87                         BLI_BITMAP_ENABLE(vertex_used_map, loop->v);
88                 }
89         }
90         for (int vertex_index = 0, manifold_veretx_index = 0;
91              vertex_index < mesh->totvert;
92              vertex_index++)
93         {
94                 if (!BLI_BITMAP_TEST_BOOL(vertex_used_map, vertex_index)) {
95                         continue;
96                 }
97                 const MVert *vertex = &mvert[vertex_index];
98                 subdiv->evaluator->setCoarsePositions(
99                         subdiv->evaluator,
100                         vertex->co,
101                         manifold_veretx_index, 1);
102                 manifold_veretx_index++;
103         }
104         MEM_freeN(vertex_used_map);
105 }
106
107 static void set_face_varying_data_from_uv(Subdiv *subdiv,
108                                           const MLoopUV *mloopuv,
109                                           const int layer_index)
110 {
111         OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
112         OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
113         const int num_faces = topology_refiner->getNumFaces(topology_refiner);
114         const MLoopUV *mluv = mloopuv;
115         /* TODO(sergey): OpenSubdiv's C-API converter can change winding of
116          * loops of a face, need to watch for that, to prevent wrong UVs assigned.
117          */
118         for (int face_index = 0; face_index < num_faces; ++face_index) {
119                 const int num_face_vertices = topology_refiner->getNumFaceVertices(
120                         topology_refiner, face_index);
121                 const int *uv_indicies = topology_refiner->getFaceFVarValueIndices(
122                         topology_refiner, face_index, layer_index);
123                 for (int vertex_index = 0;
124                      vertex_index < num_face_vertices;
125                      vertex_index++, mluv++)
126                 {
127                         evaluator->setFaceVaryingData(evaluator,
128                                                       layer_index,
129                                                       mluv->uv,
130                                                       uv_indicies[vertex_index],
131                                                       1);
132                 }
133         }
134 }
135
136 bool BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const Mesh *mesh)
137 {
138         if (!BKE_subdiv_eval_begin(subdiv)) {
139                 return false;
140         }
141         if (subdiv->evaluator == NULL) {
142                 /* NOTE: This situation is supposed to be handled by begin(). */
143                 BLI_assert(!"Is not supposed to happen");
144                 return false;
145         }
146         /* Set coordinates of base mesh vertices. */
147         set_coarse_positions(subdiv, mesh);
148         /* Set face-varyign data to UV maps. */
149         const int num_uv_layers =
150                 CustomData_number_of_layers(&mesh->ldata, CD_MLOOPUV);
151         for (int layer_index = 0; layer_index < num_uv_layers; layer_index++) {
152                 const MLoopUV *mloopuv = CustomData_get_layer_n(
153                         &mesh->ldata, CD_MLOOPUV, layer_index);
154                 set_face_varying_data_from_uv(subdiv, mloopuv, layer_index);
155         }
156         /* Update evaluator to the new coarse geometry. */
157         BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
158         subdiv->evaluator->refine(subdiv->evaluator);
159         BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
160         return true;
161 }
162
163 void BKE_subdiv_eval_init_displacement(Subdiv *subdiv)
164 {
165         if (subdiv->displacement_evaluator == NULL) {
166                 return;
167         }
168         if (subdiv->displacement_evaluator->initialize == NULL) {
169                 return;
170         }
171         subdiv->displacement_evaluator->initialize(subdiv->displacement_evaluator);
172 }
173
174 /* ========================== Single point queries ========================== */
175
176 void BKE_subdiv_eval_limit_point(
177         Subdiv *subdiv,
178         const int ptex_face_index,
179         const float u, const float v,
180         float r_P[3])
181 {
182         BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
183                                                     ptex_face_index,
184                                                     u, v,
185                                                     r_P, NULL, NULL);
186 }
187
188 void BKE_subdiv_eval_limit_point_and_derivatives(
189         Subdiv *subdiv,
190         const int ptex_face_index,
191         const float u, const float v,
192         float r_P[3], float r_dPdu[3], float r_dPdv[3])
193 {
194         subdiv->evaluator->evaluateLimit(subdiv->evaluator,
195                                          ptex_face_index,
196                                          u, v,
197                                          r_P, r_dPdu, r_dPdv);
198 }
199
200 void BKE_subdiv_eval_limit_point_and_normal(
201         Subdiv *subdiv,
202         const int ptex_face_index,
203         const float u, const float v,
204         float r_P[3], float r_N[3])
205 {
206         float dPdu[3], dPdv[3];
207         BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
208                                                     ptex_face_index,
209                                                     u, v,
210                                                     r_P, dPdu, dPdv);
211         cross_v3_v3v3(r_N, dPdu, dPdv);
212         normalize_v3(r_N);
213 }
214
215 void BKE_subdiv_eval_limit_point_and_short_normal(
216         Subdiv *subdiv,
217         const int ptex_face_index,
218         const float u, const float v,
219         float r_P[3], short r_N[3])
220 {
221         float N_float[3];
222         BKE_subdiv_eval_limit_point_and_normal(subdiv,
223                                                ptex_face_index,
224                                                u, v,
225                                                r_P, N_float);
226         normal_float_to_short_v3(r_N, N_float);
227 }
228
229 void BKE_subdiv_eval_face_varying(
230         Subdiv *subdiv,
231         const int face_varying_channel,
232         const int ptex_face_index,
233         const float u, const float v,
234         float r_face_varying[2])
235 {
236         subdiv->evaluator->evaluateFaceVarying(subdiv->evaluator,
237                                                face_varying_channel,
238                                                ptex_face_index,
239                                                u, v,
240                                                r_face_varying);
241 }
242
243 void BKE_subdiv_eval_displacement(
244         Subdiv *subdiv,
245         const int ptex_face_index,
246         const float u, const float v,
247         const float dPdu[3], const float dPdv[3],
248         float r_D[3])
249 {
250         if (subdiv->displacement_evaluator == NULL) {
251                 zero_v3(r_D);
252                 return;
253         }
254         subdiv->displacement_evaluator->eval_displacement(
255                 subdiv->displacement_evaluator,
256                 ptex_face_index,
257                 u, v,
258                 dPdu, dPdv,
259                 r_D);
260 }
261
262 void BKE_subdiv_eval_final_point(
263         Subdiv *subdiv,
264         const int ptex_face_index,
265         const float u, const float v,
266         float r_P[3])
267 {
268         if (subdiv->displacement_evaluator) {
269                 float dPdu[3], dPdv[3], D[3];
270                 BKE_subdiv_eval_limit_point_and_derivatives(
271                         subdiv, ptex_face_index, u, v, r_P, dPdu, dPdv);
272                 BKE_subdiv_eval_displacement(subdiv,
273                                              ptex_face_index, u, v,
274                                              dPdu, dPdv,
275                                              D);
276                 add_v3_v3(r_P, D);
277         }
278         else {
279                 BKE_subdiv_eval_limit_point(subdiv, ptex_face_index, u, v, r_P);
280         }
281 }
282
283 /* ===================  Patch queries at given resolution =================== */
284
285 /* Move buffer forward by a given number of bytes. */
286 static void buffer_apply_offset(void **buffer, const int offset)
287 {
288   *buffer = ((unsigned char *)*buffer) + offset;
289 }
290
291 /* Write given number of floats to the beginning of given buffer.  */
292 static void buffer_write_float_value(void **buffer,
293                                      const float *values_buffer, int num_values)
294 {
295         memcpy(*buffer, values_buffer, sizeof(float) * num_values);
296 }
297
298 /* Similar to above, just operates with short values. */
299 static void buffer_write_short_value(void **buffer,
300                                      const short *values_buffer, int num_values)
301 {
302         memcpy(*buffer, values_buffer, sizeof(short) * num_values);
303 }
304
305 void BKE_subdiv_eval_limit_patch_resolution_point(
306         Subdiv *subdiv,
307         const int ptex_face_index,
308         const int resolution,
309         void *buffer, const int offset, const int stride)
310 {
311         buffer_apply_offset(&buffer, offset);
312         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
313         for (int y = 0; y < resolution; y++) {
314                 const float v = y * inv_resolution_1;
315                 for (int x = 0; x < resolution; x++) {
316                         const float u = x * inv_resolution_1;
317                         BKE_subdiv_eval_limit_point(subdiv,
318                                                     ptex_face_index,
319                                                     u, v,
320                                                     buffer);
321                         buffer_apply_offset(&buffer, stride);
322                 }
323         }
324 }
325
326 void BKE_subdiv_eval_limit_patch_resolution_point_and_derivatives(
327         Subdiv *subdiv,
328         const int ptex_face_index,
329         const int resolution,
330         void *point_buffer, const int point_offset, const int point_stride,
331         void *du_buffer, const int du_offset, const int du_stride,
332         void *dv_buffer, const int dv_offset, const int dv_stride)
333 {
334         buffer_apply_offset(&point_buffer, point_offset);
335         buffer_apply_offset(&du_buffer, du_offset);
336         buffer_apply_offset(&dv_buffer, dv_offset);
337         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
338         for (int y = 0; y < resolution; y++) {
339                 const float v = y * inv_resolution_1;
340                 for (int x = 0; x < resolution; x++) {
341                         const float u = x * inv_resolution_1;
342                         BKE_subdiv_eval_limit_point_and_derivatives(
343                                 subdiv,
344                                 ptex_face_index,
345                                 u, v,
346                                 point_buffer, du_buffer, dv_buffer);
347                         buffer_apply_offset(&point_buffer, point_stride);
348                         buffer_apply_offset(&du_buffer, du_stride);
349                         buffer_apply_offset(&dv_buffer, dv_stride);
350                 }
351         }
352 }
353
354 void BKE_subdiv_eval_limit_patch_resolution_point_and_normal(
355         Subdiv *subdiv,
356         const int ptex_face_index,
357         const int resolution,
358         void *point_buffer, const int point_offset, const int point_stride,
359         void *normal_buffer, const int normal_offset, const int normal_stride)
360 {
361         buffer_apply_offset(&point_buffer, point_offset);
362         buffer_apply_offset(&normal_buffer, normal_offset);
363         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
364         for (int y = 0; y < resolution; y++) {
365                 const float v = y * inv_resolution_1;
366                 for (int x = 0; x < resolution; x++) {
367                         const float u = x * inv_resolution_1;
368                         float normal[3];
369                         BKE_subdiv_eval_limit_point_and_normal(
370                                 subdiv,
371                                 ptex_face_index,
372                                 u, v,
373                                 point_buffer, normal);
374                         buffer_write_float_value(&normal_buffer, normal, 3);
375                         buffer_apply_offset(&point_buffer, point_stride);
376                         buffer_apply_offset(&normal_buffer, normal_stride);
377                 }
378         }
379 }
380
381 void BKE_subdiv_eval_limit_patch_resolution_point_and_short_normal(
382         Subdiv *subdiv,
383         const int ptex_face_index,
384         const int resolution,
385         void *point_buffer, const int point_offset, const int point_stride,
386         void *normal_buffer, const int normal_offset, const int normal_stride)
387 {
388         buffer_apply_offset(&point_buffer, point_offset);
389         buffer_apply_offset(&normal_buffer, normal_offset);
390         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
391         for (int y = 0; y < resolution; y++) {
392                 const float v = y * inv_resolution_1;
393                 for (int x = 0; x < resolution; x++) {
394                         const float u = x * inv_resolution_1;
395                         short normal[3];
396                         BKE_subdiv_eval_limit_point_and_short_normal(
397                                 subdiv,
398                                 ptex_face_index,
399                                 u, v,
400                                 point_buffer, normal);
401                         buffer_write_short_value(&normal_buffer, normal, 3);
402                         buffer_apply_offset(&point_buffer, point_stride);
403                         buffer_apply_offset(&normal_buffer, normal_stride);
404                 }
405         }
406 }