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