GPencil MultiStroke modifier
[blender.git] / source / blender / blenkernel / intern / subdiv_eval.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2018 by Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup bke
22  */
23
24 #include "BKE_subdiv_eval.h"
25
26 #include "DNA_mesh_types.h"
27 #include "DNA_meshdata_types.h"
28
29 #include "BLI_utildefines.h"
30 #include "BLI_bitmap.h"
31 #include "BLI_math_vector.h"
32
33 #include "BKE_customdata.h"
34 #include "BKE_subdiv.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "opensubdiv_evaluator_capi.h"
39 #include "opensubdiv_topology_refiner_capi.h"
40
41 bool BKE_subdiv_eval_begin(Subdiv *subdiv)
42 {
43   BKE_subdiv_stats_reset(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
44   if (subdiv->topology_refiner == NULL) {
45     /* Happens on input mesh with just loose geometry,
46      * or when OpenSubdiv is disabled */
47     return false;
48   }
49   else if (subdiv->evaluator == NULL) {
50     BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
51     subdiv->evaluator = openSubdiv_createEvaluatorFromTopologyRefiner(subdiv->topology_refiner);
52     BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
53     if (subdiv->evaluator == NULL) {
54       return false;
55     }
56   }
57   else {
58     /* TODO(sergey): Check for topology change. */
59   }
60   BKE_subdiv_eval_init_displacement(subdiv);
61   return true;
62 }
63
64 static void set_coarse_positions(Subdiv *subdiv,
65                                  const Mesh *mesh,
66                                  const float (*coarse_vertex_cos)[3])
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   BLI_bitmap *vertex_used_map = BLI_BITMAP_NEW(mesh->totvert, "vert used map");
76   for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
77     const MPoly *poly = &mpoly[poly_index];
78     for (int corner = 0; corner < poly->totloop; corner++) {
79       const MLoop *loop = &mloop[poly->loopstart + corner];
80       BLI_BITMAP_ENABLE(vertex_used_map, loop->v);
81     }
82   }
83   for (int vertex_index = 0, manifold_veretx_index = 0; vertex_index < mesh->totvert;
84        vertex_index++) {
85     if (!BLI_BITMAP_TEST_BOOL(vertex_used_map, vertex_index)) {
86       continue;
87     }
88     const float *vertex_co;
89     if (coarse_vertex_cos != NULL) {
90       vertex_co = coarse_vertex_cos[vertex_index];
91     }
92     else {
93       const MVert *vertex = &mvert[vertex_index];
94       vertex_co = vertex->co;
95     }
96     subdiv->evaluator->setCoarsePositions(subdiv->evaluator, vertex_co, 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(topology_refiner,
115                                                                        face_index);
116     const int *uv_indices = topology_refiner->getFaceFVarValueIndices(
117         topology_refiner, face_index, layer_index);
118     for (int vertex_index = 0; vertex_index < num_face_vertices; vertex_index++, mluv++) {
119       evaluator->setFaceVaryingData(evaluator, layer_index, mluv->uv, uv_indices[vertex_index], 1);
120     }
121   }
122 }
123
124 bool BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv,
125                                       const Mesh *mesh,
126                                       const float (*coarse_vertex_cos)[3])
127 {
128   if (!BKE_subdiv_eval_begin(subdiv)) {
129     return false;
130   }
131   if (subdiv->evaluator == NULL) {
132     /* NOTE: This situation is supposed to be handled by begin(). */
133     BLI_assert(!"Is not supposed to happen");
134     return false;
135   }
136   /* Set coordinates of base mesh vertices. */
137   set_coarse_positions(subdiv, mesh, coarse_vertex_cos);
138   /* Set face-varyign data to UV maps. */
139   const int num_uv_layers = 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(&mesh->ldata, CD_MLOOPUV, layer_index);
142     set_face_varying_data_from_uv(subdiv, mloopuv, layer_index);
143   }
144   /* Update evaluator to the new coarse geometry. */
145   BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
146   subdiv->evaluator->refine(subdiv->evaluator);
147   BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
148   return true;
149 }
150
151 void BKE_subdiv_eval_init_displacement(Subdiv *subdiv)
152 {
153   if (subdiv->displacement_evaluator == NULL) {
154     return;
155   }
156   if (subdiv->displacement_evaluator->initialize == NULL) {
157     return;
158   }
159   subdiv->displacement_evaluator->initialize(subdiv->displacement_evaluator);
160 }
161
162 /* ========================== Single point queries ========================== */
163
164 void BKE_subdiv_eval_limit_point(
165     Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3])
166 {
167   BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, NULL, NULL);
168 }
169
170 void BKE_subdiv_eval_limit_point_and_derivatives(Subdiv *subdiv,
171                                                  const int ptex_face_index,
172                                                  const float u,
173                                                  const float v,
174                                                  float r_P[3],
175                                                  float r_dPdu[3],
176                                                  float r_dPdv[3])
177 {
178   subdiv->evaluator->evaluateLimit(subdiv->evaluator, ptex_face_index, u, v, r_P, r_dPdu, r_dPdv);
179 }
180
181 void BKE_subdiv_eval_limit_point_and_normal(Subdiv *subdiv,
182                                             const int ptex_face_index,
183                                             const float u,
184                                             const float v,
185                                             float r_P[3],
186                                             float r_N[3])
187 {
188   float dPdu[3], dPdv[3];
189   BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, dPdu, dPdv);
190   cross_v3_v3v3(r_N, dPdu, dPdv);
191   normalize_v3(r_N);
192 }
193
194 void BKE_subdiv_eval_limit_point_and_short_normal(Subdiv *subdiv,
195                                                   const int ptex_face_index,
196                                                   const float u,
197                                                   const float v,
198                                                   float r_P[3],
199                                                   short r_N[3])
200 {
201   float N_float[3];
202   BKE_subdiv_eval_limit_point_and_normal(subdiv, ptex_face_index, u, v, r_P, N_float);
203   normal_float_to_short_v3(r_N, N_float);
204 }
205
206 void BKE_subdiv_eval_face_varying(Subdiv *subdiv,
207                                   const int face_varying_channel,
208                                   const int ptex_face_index,
209                                   const float u,
210                                   const float v,
211                                   float r_face_varying[2])
212 {
213   subdiv->evaluator->evaluateFaceVarying(
214       subdiv->evaluator, face_varying_channel, ptex_face_index, u, v, r_face_varying);
215 }
216
217 void BKE_subdiv_eval_displacement(Subdiv *subdiv,
218                                   const int ptex_face_index,
219                                   const float u,
220                                   const float v,
221                                   const float dPdu[3],
222                                   const float dPdv[3],
223                                   float r_D[3])
224 {
225   if (subdiv->displacement_evaluator == NULL) {
226     zero_v3(r_D);
227     return;
228   }
229   subdiv->displacement_evaluator->eval_displacement(
230       subdiv->displacement_evaluator, ptex_face_index, u, v, dPdu, dPdv, r_D);
231 }
232
233 void BKE_subdiv_eval_final_point(
234     Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3])
235 {
236   if (subdiv->displacement_evaluator) {
237     float dPdu[3], dPdv[3], D[3];
238     BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, dPdu, dPdv);
239     BKE_subdiv_eval_displacement(subdiv, ptex_face_index, u, v, dPdu, dPdv, D);
240     add_v3_v3(r_P, D);
241   }
242   else {
243     BKE_subdiv_eval_limit_point(subdiv, ptex_face_index, u, v, r_P);
244   }
245 }
246
247 /* ===================  Patch queries at given resolution =================== */
248
249 /* Move buffer forward by a given number of bytes. */
250 static void buffer_apply_offset(void **buffer, const int offset)
251 {
252   *buffer = ((unsigned char *)*buffer) + offset;
253 }
254
255 /* Write given number of floats to the beginning of given buffer.  */
256 static void buffer_write_float_value(void **buffer, const float *values_buffer, int num_values)
257 {
258   memcpy(*buffer, values_buffer, sizeof(float) * num_values);
259 }
260
261 /* Similar to above, just operates with short values. */
262 static void buffer_write_short_value(void **buffer, const short *values_buffer, int num_values)
263 {
264   memcpy(*buffer, values_buffer, sizeof(short) * num_values);
265 }
266
267 void BKE_subdiv_eval_limit_patch_resolution_point(Subdiv *subdiv,
268                                                   const int ptex_face_index,
269                                                   const int resolution,
270                                                   void *buffer,
271                                                   const int offset,
272                                                   const int stride)
273 {
274   buffer_apply_offset(&buffer, offset);
275   const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
276   for (int y = 0; y < resolution; y++) {
277     const float v = y * inv_resolution_1;
278     for (int x = 0; x < resolution; x++) {
279       const float u = x * inv_resolution_1;
280       BKE_subdiv_eval_limit_point(subdiv, ptex_face_index, u, v, buffer);
281       buffer_apply_offset(&buffer, stride);
282     }
283   }
284 }
285
286 void BKE_subdiv_eval_limit_patch_resolution_point_and_derivatives(Subdiv *subdiv,
287                                                                   const int ptex_face_index,
288                                                                   const int resolution,
289                                                                   void *point_buffer,
290                                                                   const int point_offset,
291                                                                   const int point_stride,
292                                                                   void *du_buffer,
293                                                                   const int du_offset,
294                                                                   const int du_stride,
295                                                                   void *dv_buffer,
296                                                                   const int dv_offset,
297                                                                   const int dv_stride)
298 {
299   buffer_apply_offset(&point_buffer, point_offset);
300   buffer_apply_offset(&du_buffer, du_offset);
301   buffer_apply_offset(&dv_buffer, dv_offset);
302   const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
303   for (int y = 0; y < resolution; y++) {
304     const float v = y * inv_resolution_1;
305     for (int x = 0; x < resolution; x++) {
306       const float u = x * inv_resolution_1;
307       BKE_subdiv_eval_limit_point_and_derivatives(
308           subdiv, ptex_face_index, u, v, point_buffer, du_buffer, dv_buffer);
309       buffer_apply_offset(&point_buffer, point_stride);
310       buffer_apply_offset(&du_buffer, du_stride);
311       buffer_apply_offset(&dv_buffer, dv_stride);
312     }
313   }
314 }
315
316 void BKE_subdiv_eval_limit_patch_resolution_point_and_normal(Subdiv *subdiv,
317                                                              const int ptex_face_index,
318                                                              const int resolution,
319                                                              void *point_buffer,
320                                                              const int point_offset,
321                                                              const int point_stride,
322                                                              void *normal_buffer,
323                                                              const int normal_offset,
324                                                              const int normal_stride)
325 {
326   buffer_apply_offset(&point_buffer, point_offset);
327   buffer_apply_offset(&normal_buffer, normal_offset);
328   const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
329   for (int y = 0; y < resolution; y++) {
330     const float v = y * inv_resolution_1;
331     for (int x = 0; x < resolution; x++) {
332       const float u = x * inv_resolution_1;
333       float normal[3];
334       BKE_subdiv_eval_limit_point_and_normal(subdiv, ptex_face_index, u, v, point_buffer, normal);
335       buffer_write_float_value(&normal_buffer, normal, 3);
336       buffer_apply_offset(&point_buffer, point_stride);
337       buffer_apply_offset(&normal_buffer, normal_stride);
338     }
339   }
340 }
341
342 void BKE_subdiv_eval_limit_patch_resolution_point_and_short_normal(Subdiv *subdiv,
343                                                                    const int ptex_face_index,
344                                                                    const int resolution,
345                                                                    void *point_buffer,
346                                                                    const int point_offset,
347                                                                    const int point_stride,
348                                                                    void *normal_buffer,
349                                                                    const int normal_offset,
350                                                                    const int normal_stride)
351 {
352   buffer_apply_offset(&point_buffer, point_offset);
353   buffer_apply_offset(&normal_buffer, normal_offset);
354   const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
355   for (int y = 0; y < resolution; y++) {
356     const float v = y * inv_resolution_1;
357     for (int x = 0; x < resolution; x++) {
358       const float u = x * inv_resolution_1;
359       short normal[3];
360       BKE_subdiv_eval_limit_point_and_short_normal(
361           subdiv, ptex_face_index, u, v, point_buffer, normal);
362       buffer_write_short_value(&normal_buffer, normal, 3);
363       buffer_apply_offset(&point_buffer, point_stride);
364       buffer_apply_offset(&normal_buffer, normal_stride);
365     }
366   }
367 }