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