Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / blenkernel / intern / subdiv.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.h"
24
25 #include "DNA_mesh_types.h"
26 #include "DNA_meshdata_types.h"
27 #include "DNA_modifier_types.h"
28
29 #include "BLI_utildefines.h"
30
31 #include "MEM_guardedalloc.h"
32
33 #include "subdiv_converter.h"
34
35 #include "opensubdiv_capi.h"
36 #include "opensubdiv_converter_capi.h"
37 #include "opensubdiv_evaluator_capi.h"
38 #include "opensubdiv_topology_refiner_capi.h"
39
40 /* ========================== CONVERSION HELPERS ============================ */
41
42 eSubdivFVarLinearInterpolation
43 BKE_subdiv_fvar_interpolation_from_uv_smooth(int uv_smooth)
44 {
45         switch (uv_smooth) {
46                 case SUBSURF_UV_SMOOTH_NONE:
47                         return SUBDIV_FVAR_LINEAR_INTERPOLATION_ALL;
48                 case SUBSURF_UV_SMOOTH_PRESERVE_CORNERS:
49                         return SUBDIV_FVAR_LINEAR_INTERPOLATION_CORNERS_ONLY;
50                 case SUBSURF_UV_SMOOTH_PRESERVE_CORNERS_AND_JUNCTIONS:
51                         return SUBDIV_FVAR_LINEAR_INTERPOLATION_CORNERS_AND_JUNCTIONS;
52                 case SUBSURF_UV_SMOOTH_PRESERVE_CORNERS_JUNCTIONS_AND_CONCAVE:
53                         return SUBDIV_FVAR_LINEAR_INTERPOLATION_CORNERS_JUNCTIONS_AND_CONCAVE;
54                 case SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES:
55                         return SUBDIV_FVAR_LINEAR_INTERPOLATION_BOUNDARIES;
56                 case SUBSURF_UV_SMOOTH_ALL:
57                         return SUBDIV_FVAR_LINEAR_INTERPOLATION_NONE;
58         }
59         BLI_assert(!"Unknown uv smooth flag");
60         return SUBDIV_FVAR_LINEAR_INTERPOLATION_ALL;
61 }
62
63 /* ================================ SETTINGS ================================ */
64
65 static bool check_mesh_has_non_quad(const Mesh *mesh)
66 {
67         for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
68                 const MPoly *poly = &mesh->mpoly[poly_index];
69                 if (poly->totloop != 4) {
70                         return true;
71                 }
72         }
73         return false;
74 }
75
76 void BKE_subdiv_settings_validate_for_mesh(SubdivSettings *settings,
77                                            const Mesh *mesh)
78 {
79         if (settings->level != 1) {
80                 return;
81         }
82         if (check_mesh_has_non_quad(mesh)) {
83                 settings->level = 2;
84         }
85 }
86
87 bool BKE_subdiv_settings_equal(const SubdivSettings *settings_a,
88                                const SubdivSettings *settings_b)
89 {
90         return
91                 (settings_a->is_simple == settings_b->is_simple &&
92                  settings_a->is_adaptive == settings_b->is_adaptive &&
93                  settings_a->level == settings_b->level &&
94                  settings_a->vtx_boundary_interpolation ==
95                          settings_b->vtx_boundary_interpolation &&
96                  settings_a->fvar_linear_interpolation ==
97                          settings_b->fvar_linear_interpolation);
98 }
99
100 /* ============================== CONSTRUCTION ============================== */
101
102 /* Creation from scratch. */
103
104 Subdiv *BKE_subdiv_new_from_converter(const SubdivSettings *settings,
105                                       struct OpenSubdiv_Converter *converter)
106 {
107         SubdivStats stats;
108         BKE_subdiv_stats_init(&stats);
109         BKE_subdiv_stats_begin(&stats, SUBDIV_STATS_TOPOLOGY_REFINER_CREATION_TIME);
110         OpenSubdiv_TopologyRefinerSettings topology_refiner_settings;
111         topology_refiner_settings.level = settings->level;
112         topology_refiner_settings.is_adaptive = settings->is_adaptive;
113         struct OpenSubdiv_TopologyRefiner *osd_topology_refiner = NULL;
114         if (converter->getNumVertices(converter) != 0) {
115                 osd_topology_refiner =
116                         openSubdiv_createTopologyRefinerFromConverter(
117                                 converter, &topology_refiner_settings);
118         }
119         else {
120                 /* TODO(sergey): Check whether original geometry had any vertices.
121                  * The thing here is: OpenSubdiv can only deal with faces, but our
122                  * side of subdiv also deals with loose vertices and edges. */
123         }
124         Subdiv *subdiv = MEM_callocN(sizeof(Subdiv), "subdiv from converetr");
125         subdiv->settings = *settings;
126         subdiv->topology_refiner = osd_topology_refiner;
127         subdiv->evaluator = NULL;
128         subdiv->displacement_evaluator = NULL;
129         BKE_subdiv_stats_end(&stats, SUBDIV_STATS_TOPOLOGY_REFINER_CREATION_TIME);
130         subdiv->stats = stats;
131         return subdiv;
132 }
133
134 Subdiv *BKE_subdiv_new_from_mesh(const SubdivSettings *settings,
135                                  const Mesh *mesh)
136 {
137         if (mesh->totvert == 0) {
138                 return NULL;
139         }
140         OpenSubdiv_Converter converter;
141         BKE_subdiv_converter_init_for_mesh(&converter, settings, mesh);
142         Subdiv *subdiv = BKE_subdiv_new_from_converter(settings, &converter);
143         BKE_subdiv_converter_free(&converter);
144         return subdiv;
145 }
146
147 /* Creation with cached-aware semantic. */
148
149 Subdiv *BKE_subdiv_update_from_converter(Subdiv *subdiv,
150                                          const SubdivSettings *settings,
151                                          OpenSubdiv_Converter *converter)
152 {
153         /* Check if the existing descriptor can be re-used. */
154         bool can_reuse_subdiv = true;
155         if (subdiv != NULL && subdiv->topology_refiner != NULL) {
156                 if (!BKE_subdiv_settings_equal(&subdiv->settings, settings)) {
157                         can_reuse_subdiv = false;
158                 }
159                 else {
160                         BKE_subdiv_stats_begin(
161                                 &subdiv->stats, SUBDIV_STATS_TOPOLOGY_COMPARE);
162                         can_reuse_subdiv = openSubdiv_topologyRefinerCompareWithConverter(
163                                 subdiv->topology_refiner, converter);
164                         BKE_subdiv_stats_end(
165                                 &subdiv->stats, SUBDIV_STATS_TOPOLOGY_COMPARE);
166                 }
167         }
168         else {
169                 can_reuse_subdiv = false;
170         }
171         if (can_reuse_subdiv) {
172                 return subdiv;
173         }
174         /* Create new subdiv. */
175         if (subdiv != NULL) {
176                 BKE_subdiv_free(subdiv);
177         }
178         return BKE_subdiv_new_from_converter(settings, converter);
179 }
180
181 Subdiv *BKE_subdiv_update_from_mesh(Subdiv *subdiv,
182                                     const SubdivSettings *settings,
183                                     const Mesh *mesh)
184 {
185         OpenSubdiv_Converter converter;
186         BKE_subdiv_converter_init_for_mesh(&converter, settings, mesh);
187         subdiv = BKE_subdiv_update_from_converter(subdiv, settings, &converter);
188         BKE_subdiv_converter_free(&converter);
189         return subdiv;
190 }
191
192 /* Memory release. */
193
194 void BKE_subdiv_free(Subdiv *subdiv)
195 {
196         if (subdiv->evaluator != NULL) {
197                 openSubdiv_deleteEvaluator(subdiv->evaluator);
198         }
199         if (subdiv->topology_refiner != NULL) {
200                 openSubdiv_deleteTopologyRefiner(subdiv->topology_refiner);
201         }
202         BKE_subdiv_displacement_detach(subdiv);
203         if (subdiv->cache_.face_ptex_offset != NULL) {
204                 MEM_freeN(subdiv->cache_.face_ptex_offset);
205         }
206         MEM_freeN(subdiv);
207 }
208
209 /* =========================== PTEX FACES AND GRIDS ========================= */
210
211 int *BKE_subdiv_face_ptex_offset_get(Subdiv *subdiv)
212 {
213         if (subdiv->cache_.face_ptex_offset != NULL) {
214                 return subdiv->cache_.face_ptex_offset;
215         }
216         OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
217         if (topology_refiner == NULL) {
218                 return NULL;
219         }
220         const int num_coarse_faces =
221                 topology_refiner->getNumFaces(topology_refiner);
222         subdiv->cache_.face_ptex_offset = MEM_malloc_arrayN(
223                 num_coarse_faces, sizeof(int), "subdiv face_ptex_offset");
224         int ptex_offset = 0;
225         for (int face_index = 0; face_index < num_coarse_faces; face_index++) {
226                 const int num_ptex_faces =
227                         topology_refiner->getNumFacePtexFaces(
228                                 topology_refiner, face_index);
229                 subdiv->cache_.face_ptex_offset[face_index] = ptex_offset;
230                 ptex_offset += num_ptex_faces;
231         }
232         return subdiv->cache_.face_ptex_offset;
233 }