Merge commit 'master@6ed15c5a41130b55cb57a43a8a9470a91d38c3d5' into blender2.8
[blender.git] / source / blender / alembic / intern / abc_customdata.cc
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) 2016 K√©vin Dietrich.
19  * All rights reserved.
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  *
23  */
24
25 #include "abc_customdata.h"
26
27 #include <Alembic/AbcGeom/All.h>
28 #include <algorithm>
29
30 extern "C" {
31 #include "DNA_customdata_types.h"
32 #include "DNA_meshdata_types.h"
33
34 #include "BKE_customdata.h"
35 }
36
37 /* NOTE: for now only UVs and Vertex Colors are supported for streaming.
38  * Although Alembic only allows for a single UV layer per {I|O}Schema, and does
39  * not have a vertex color concept, there is a convention between DCCs to write
40  * such data in a way that lets other DCC know what they are for. See comments
41  * in the write code for the conventions. */
42
43 using Alembic::AbcGeom::kVertexScope;
44 using Alembic::AbcGeom::kFacevaryingScope;
45
46 using Alembic::Abc::C4fArraySample;
47 using Alembic::Abc::UInt32ArraySample;
48 using Alembic::Abc::V2fArraySample;
49
50 using Alembic::AbcGeom::OV2fGeomParam;
51 using Alembic::AbcGeom::OC4fGeomParam;
52
53 static void get_uvs(const CDStreamConfig &config,
54                     std::vector<Imath::V2f> &uvs,
55                     std::vector<uint32_t> &uvidx,
56                     void *cd_data)
57 {
58         MLoopUV *mloopuv_array = static_cast<MLoopUV *>(cd_data);
59
60         if (!mloopuv_array) {
61                 return;
62         }
63
64         const int num_poly = config.totpoly;
65         MPoly *polygons = config.mpoly;
66
67         if (!config.pack_uvs) {
68                 int cnt = 0;
69                 uvidx.resize(config.totloop);
70                 uvs.resize(config.totloop);
71
72                 for (int i = 0; i < num_poly; ++i) {
73                         MPoly &current_poly = polygons[i];
74                         MLoopUV *loopuvpoly = mloopuv_array + current_poly.loopstart + current_poly.totloop;
75
76                         for (int j = 0; j < current_poly.totloop; ++j, ++cnt) {
77                                 --loopuvpoly;
78
79                                 uvidx[cnt] = cnt;
80                                 uvs[cnt][0] = loopuvpoly->uv[0];
81                                 uvs[cnt][1] = loopuvpoly->uv[1];
82                         }
83                 }
84         }
85         else {
86                 for (int i = 0; i < num_poly; ++i) {
87                         MPoly &current_poly = polygons[i];
88                         MLoopUV *loopuvpoly = mloopuv_array + current_poly.loopstart + current_poly.totloop;
89
90                         for (int j = 0; j < current_poly.totloop; ++j) {
91                                 loopuvpoly--;
92                                 Imath::V2f uv(loopuvpoly->uv[0], loopuvpoly->uv[1]);
93
94                                 std::vector<Imath::V2f>::iterator it = std::find(uvs.begin(), uvs.end(), uv);
95
96                                 if (it == uvs.end()) {
97                                         uvidx.push_back(uvs.size());
98                                         uvs.push_back(uv);
99                                 }
100                                 else {
101                                         uvidx.push_back(std::distance(uvs.begin(), it));
102                                 }
103                         }
104                 }
105         }
106 }
107
108 const char *get_uv_sample(UVSample &sample, const CDStreamConfig &config, CustomData *data)
109 {
110         const int active_uvlayer = CustomData_get_active_layer(data, CD_MLOOPUV);
111
112         if (active_uvlayer < 0) {
113                 return "";
114         }
115
116         void *cd_data = CustomData_get_layer_n(data, CD_MLOOPUV, active_uvlayer);
117
118         get_uvs(config, sample.uvs, sample.indices, cd_data);
119
120         return CustomData_get_layer_name(data, CD_MLOOPUV, active_uvlayer);
121 }
122
123 /* Convention to write UVs:
124  * - V2fGeomParam on the arbGeomParam
125  * - set scope as face varying
126  * - (optional due to its behaviour) tag as UV using Alembic::AbcGeom::SetIsUV
127  */
128 static void write_uv(const OCompoundProperty &prop, const CDStreamConfig &config, void *data, const char *name)
129 {
130         std::vector<uint32_t> indices;
131         std::vector<Imath::V2f> uvs;
132
133         get_uvs(config, uvs, indices, data);
134
135         if (indices.empty() || uvs.empty()) {
136                 return;
137         }
138
139         OV2fGeomParam param(prop, name, true, kFacevaryingScope, 1);
140
141         OV2fGeomParam::Sample sample(
142                 V2fArraySample(&uvs.front(), uvs.size()),
143                 UInt32ArraySample(&indices.front(), indices.size()),
144                 kFacevaryingScope);
145
146         param.set(sample);
147 }
148
149 /* Convention to write Vertex Colors:
150  * - C3fGeomParam/C4fGeomParam on the arbGeomParam
151  * - set scope as vertex varying
152  */
153 static void write_mcol(const OCompoundProperty &prop, const CDStreamConfig &config, void *data, const char *name)
154 {
155         const float cscale = 1.0f / 255.0f;
156         MPoly *polys = config.mpoly;
157         MLoop *mloops = config.mloop;
158         MCol *cfaces = static_cast<MCol *>(data);
159
160         std::vector<Imath::C4f> buffer(config.totvert);
161
162         Imath::C4f col;
163
164         for (int i = 0; i < config.totpoly; ++i) {
165                 MPoly *p = &polys[i];
166                 MCol *cface = &cfaces[p->loopstart + p->totloop];
167                 MLoop *mloop = &mloops[p->loopstart + p->totloop];
168
169                 for (int j = 0; j < p->totloop; ++j) {
170                         cface--;
171                         mloop--;
172
173                         col[0] = cface->a * cscale;
174                         col[1] = cface->r * cscale;
175                         col[2] = cface->g * cscale;
176                         col[3] = cface->b * cscale;
177
178                         buffer[mloop->v] = col;
179                 }
180         }
181
182         OC4fGeomParam param(prop, name, true, kFacevaryingScope, 1);
183
184         OC4fGeomParam::Sample sample(
185                 C4fArraySample(&buffer.front(), buffer.size()),
186                 kVertexScope);
187
188         param.set(sample);
189 }
190
191 void write_custom_data(const OCompoundProperty &prop, const CDStreamConfig &config, CustomData *data, int data_type)
192 {
193         CustomDataType cd_data_type = static_cast<CustomDataType>(data_type);
194
195         if (!CustomData_has_layer(data, cd_data_type)) {
196                 return;
197         }
198
199         const int active_layer = CustomData_get_active_layer(data, cd_data_type);
200         const int tot_layers = CustomData_number_of_layers(data, cd_data_type);
201
202         for (int i = 0; i < tot_layers; ++i) {
203                 void *cd_data = CustomData_get_layer_n(data, cd_data_type, i);
204                 const char *name = CustomData_get_layer_name(data, cd_data_type, i);
205
206                 if (cd_data_type == CD_MLOOPUV) {
207                         /* Already exported. */
208                         if (i == active_layer) {
209                                 continue;
210                         }
211
212                         write_uv(prop, config, cd_data, name);
213                 }
214                 else if (cd_data_type == CD_MLOOPCOL) {
215                         write_mcol(prop, config, cd_data, name);
216                 }
217         }
218 }
219
220 /* ************************************************************************** */
221
222 using Alembic::Abc::C3fArraySamplePtr;
223 using Alembic::Abc::C4fArraySamplePtr;
224 using Alembic::Abc::PropertyHeader;
225
226 using Alembic::AbcGeom::IC3fGeomParam;
227 using Alembic::AbcGeom::IC4fGeomParam;
228 using Alembic::AbcGeom::IV2fGeomParam;
229
230 static void read_mcols(const CDStreamConfig &config, void *data,
231                        const C3fArraySamplePtr &c3f_ptr, const C4fArraySamplePtr &c4f_ptr)
232 {
233         MCol *cfaces = static_cast<MCol *>(data);
234         MPoly *polys = config.mpoly;
235         MLoop *mloops = config.mloop;
236
237         if (c3f_ptr) {
238                 for (int i = 0; i < config.totpoly; ++i) {
239                         MPoly *p = &polys[i];
240                         MCol *cface = &cfaces[p->loopstart + p->totloop];
241                         MLoop *mloop = &mloops[p->loopstart + p->totloop];
242
243                         for (int j = 0; j < p->totloop; ++j) {
244                                 cface--;
245                                 mloop--;
246                                 const Imath::C3f &color = (*c3f_ptr)[mloop->v];
247                                 cface->a = FTOCHAR(color[0]);
248                                 cface->r = FTOCHAR(color[1]);
249                                 cface->g = FTOCHAR(color[2]);
250                                 cface->b = 255;
251                         }
252                 }
253         }
254         else if (c4f_ptr) {
255                 for (int i = 0; i < config.totpoly; ++i) {
256                         MPoly *p = &polys[i];
257                         MCol *cface = &cfaces[p->loopstart + p->totloop];
258                         MLoop *mloop = &mloops[p->loopstart + p->totloop];
259
260                         for (int j = 0; j < p->totloop; ++j) {
261                                 cface--;
262                                 mloop--;
263                                 const Imath::C4f &color = (*c4f_ptr)[mloop->v];
264                                 cface->a = FTOCHAR(color[0]);
265                                 cface->r = FTOCHAR(color[1]);
266                                 cface->g = FTOCHAR(color[2]);
267                                 cface->b = FTOCHAR(color[3]);
268                         }
269                 }
270         }
271 }
272
273 static void read_uvs(const CDStreamConfig &config, void *data,
274                      const Alembic::AbcGeom::V2fArraySamplePtr &uvs,
275                      const Alembic::AbcGeom::UInt32ArraySamplePtr &indices)
276 {
277         MPoly *mpolys = config.mpoly;
278         MLoopUV *mloopuvs = static_cast<MLoopUV *>(data);
279
280         unsigned int uv_index, loop_index;
281
282         for (int i = 0; i < config.totpoly; ++i) {
283                 MPoly &poly = mpolys[i];
284
285                 for (int f = 0; f < poly.totloop; ++f) {
286                         loop_index = poly.loopstart + f;
287                         uv_index = (*indices)[loop_index];
288                         const Imath::V2f &uv = (*uvs)[uv_index];
289
290                         MLoopUV &loopuv = mloopuvs[loop_index];
291                         loopuv.uv[0] = uv[0];
292                         loopuv.uv[1] = uv[1];
293                 }
294         }
295 }
296
297 static void read_custom_data_ex(const ICompoundProperty &prop,
298                                 const PropertyHeader &prop_header,
299                                 const CDStreamConfig &config,
300                                 const Alembic::Abc::ISampleSelector &iss,
301                                 int data_type)
302 {
303         if (data_type == CD_MLOOPCOL) {
304                 C3fArraySamplePtr c3f_ptr = C3fArraySamplePtr();
305                 C4fArraySamplePtr c4f_ptr = C4fArraySamplePtr();
306
307                 if (IC3fGeomParam::matches(prop_header)) {
308                         IC3fGeomParam color_param(prop, prop_header.getName());
309                         IC3fGeomParam::Sample sample;
310                         color_param.getIndexed(sample, iss);
311
312                         c3f_ptr = sample.getVals();
313                 }
314                 else if (IC4fGeomParam::matches(prop_header)) {
315                         IC4fGeomParam color_param(prop, prop_header.getName());
316                         IC4fGeomParam::Sample sample;
317                         color_param.getIndexed(sample, iss);
318
319                         c4f_ptr = sample.getVals();
320                 }
321
322                 void *cd_data = config.add_customdata_cb(config.user_data,
323                                                          prop_header.getName().c_str(),
324                                                          data_type);
325
326                 read_mcols(config, cd_data, c3f_ptr, c4f_ptr);
327         }
328         else if (data_type == CD_MLOOPUV) {
329                 IV2fGeomParam uv_param(prop, prop_header.getName());
330
331                 if (!uv_param.isIndexed()) {
332                         return;
333                 }
334
335                 IV2fGeomParam::Sample sample;
336                 uv_param.getIndexed(sample, iss);
337
338                 if (uv_param.getScope() != kFacevaryingScope) {
339                         return;
340                 }
341
342                 void *cd_data = config.add_customdata_cb(config.user_data,
343                                                          prop_header.getName().c_str(),
344                                                          data_type);
345
346                 read_uvs(config, cd_data, sample.getVals(), sample.getIndices());
347         }
348 }
349
350 void read_custom_data(const ICompoundProperty &prop, const CDStreamConfig &config, const Alembic::Abc::ISampleSelector &iss)
351 {
352         if (!prop.valid()) {
353                 return;
354         }
355
356         int num_uvs = 0;
357         int num_colors = 0;
358
359         const size_t num_props = prop.getNumProperties();
360
361         for (size_t i = 0; i < num_props; ++i) {
362                 const Alembic::Abc::PropertyHeader &prop_header = prop.getPropertyHeader(i);
363
364                 /* Read UVs according to convention. */
365                 if (IV2fGeomParam::matches(prop_header) && Alembic::AbcGeom::isUV(prop_header)) {
366                         if (++num_uvs > MAX_MTFACE) {
367                                 continue;
368                         }
369
370                         read_custom_data_ex(prop, prop_header, config, iss, CD_MLOOPUV);
371                         continue;
372                 }
373
374                 /* Read vertex colors according to convention. */
375                 if (IC3fGeomParam::matches(prop_header) || IC4fGeomParam::matches(prop_header)) {
376                         if (++num_colors > MAX_MCOL) {
377                                 continue;
378                         }
379
380                         read_custom_data_ex(prop, prop_header, config, iss, CD_MLOOPCOL);
381                         continue;
382                 }
383         }
384 }