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