Fix T49249: Alembic export with multiple hair systems crash blender
[blender.git] / source / blender / alembic / intern / abc_hair.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  * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "abc_hair.h"
24
25 #include <cstdio>
26
27 #include "abc_transform.h"
28 #include "abc_util.h"
29
30 extern "C" {
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_modifier_types.h"
34
35 #include "BLI_listbase.h"
36 #include "BLI_math_geom.h"
37
38 #include "BKE_DerivedMesh.h"
39 #include "BKE_object.h"
40 #include "BKE_particle.h"
41 }
42
43 using Alembic::Abc::P3fArraySamplePtr;
44
45 using Alembic::AbcGeom::OCurves;
46 using Alembic::AbcGeom::OCurvesSchema;
47 using Alembic::AbcGeom::ON3fGeomParam;
48 using Alembic::AbcGeom::OV2fGeomParam;
49
50 /* ************************************************************************** */
51
52 AbcHairWriter::AbcHairWriter(Scene *scene,
53                              Object *ob,
54                              AbcTransformWriter *parent,
55                              uint32_t time_sampling,
56                              ExportSettings &settings,
57                              ParticleSystem *psys)
58     : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
59 {
60         m_psys = psys;
61
62         OCurves curves(parent->alembicXform(), psys->name, m_time_sampling);
63         m_schema = curves.getSchema();
64 }
65
66 void AbcHairWriter::do_write()
67 {
68         if (!m_psys) {
69                 return;
70         }
71
72         ParticleSystemModifierData *psmd = psys_get_modifier(m_object, m_psys);
73
74         if (!psmd->dm_final) {
75                 return;
76         }
77
78         DerivedMesh *dm = mesh_create_derived_view(m_scene, m_object, CD_MASK_MESH);
79         DM_ensure_tessface(dm);
80         DM_update_tessface_data(dm);
81
82         std::vector<Imath::V3f> verts;
83         std::vector<int32_t> hvertices;
84         std::vector<Imath::V2f> uv_values;
85         std::vector<Imath::V3f> norm_values;
86
87         if (m_psys->pathcache) {
88                 ParticleSettings *part = m_psys->part;
89
90                 write_hair_sample(dm, part, verts, norm_values, uv_values, hvertices);
91
92                 if (m_settings.export_child_hairs && m_psys->childcache) {
93                         write_hair_child_sample(dm, part, verts, norm_values, uv_values, hvertices);
94                 }
95         }
96
97         dm->release(dm);
98
99         Alembic::Abc::P3fArraySample iPos(verts);
100         m_sample = OCurvesSchema::Sample(iPos, hvertices);
101         m_sample.setBasis(Alembic::AbcGeom::kNoBasis);
102         m_sample.setType(Alembic::AbcGeom::kLinear);
103         m_sample.setWrap(Alembic::AbcGeom::kNonPeriodic);
104
105         if (!uv_values.empty()) {
106                 OV2fGeomParam::Sample uv_smp;
107                 uv_smp.setVals(uv_values);
108                 m_sample.setUVs(uv_smp);
109         }
110
111         if (!norm_values.empty()) {
112                 ON3fGeomParam::Sample norm_smp;
113                 norm_smp.setVals(norm_values);
114                 m_sample.setNormals(norm_smp);
115         }
116
117         m_sample.setSelfBounds(bounds());
118         m_schema.set(m_sample);
119 }
120
121 void AbcHairWriter::write_hair_sample(DerivedMesh *dm,
122                                       ParticleSettings *part,
123                                       std::vector<Imath::V3f> &verts,
124                                       std::vector<Imath::V3f> &norm_values,
125                                       std::vector<Imath::V2f> &uv_values,
126                                       std::vector<int32_t> &hvertices)
127 {
128         /* Get untransformed vertices, there's a xform under the hair. */
129         float inv_mat[4][4];
130         invert_m4_m4_safe(inv_mat, m_object->obmat);
131
132         MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
133         MFace *mface = dm->getTessFaceArray(dm);
134         MVert *mverts = dm->getVertArray(dm);
135
136         if (!mtface || !mface) {
137                 std::fprintf(stderr, "Warning, no UV set found for underlying geometry.\n");
138         }
139
140         ParticleData * pa = m_psys->particles;
141         int k;
142
143         ParticleCacheKey **cache = m_psys->pathcache;
144         ParticleCacheKey *path;
145         float normal[3];
146         Imath::V3f tmp_nor;
147
148         for (int p = 0; p < m_psys->totpart; ++p, ++pa) {
149                 /* underlying info for faces-only emission */
150                 path = cache[p];
151
152                 if (part->from == PART_FROM_FACE && mtface) {
153                         const int num = pa->num_dmcache >= 0 ? pa->num_dmcache : pa->num;
154
155                         if (num < dm->getNumTessFaces(dm)) {
156                                 MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
157                                 MTFace *tface = mtface + num;
158
159                                 if (mface) {
160                                         float r_uv[2], mapfw[4], vec[3];
161
162                                         psys_interpolate_uvs(tface, face->v4, pa->fuv, r_uv);
163                                         uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
164
165                                         psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, normal, NULL, NULL, NULL, NULL);
166
167                                         copy_zup_yup(tmp_nor.getValue(), normal);
168                                         norm_values.push_back(tmp_nor);
169                                 }
170                         }
171                         else {
172                                 std::fprintf(stderr, "Particle to faces overflow (%d/%d)\n", num, dm->getNumTessFaces(dm));
173                         }
174                 }
175                 else if (part->from == PART_FROM_VERT && mtface) {
176                         /* vertex id */
177                         const int num = (pa->num_dmcache >= 0) ? pa->num_dmcache : pa->num;
178
179                         /* iterate over all faces to find a corresponding underlying UV */
180                         for (int n = 0; n < dm->getNumTessFaces(dm); ++n) {
181                                 MFace *face  = static_cast<MFace *>(dm->getTessFaceData(dm, n, CD_MFACE));
182                                 MTFace *tface = mtface + n;
183                                 unsigned int vtx[4];
184                                 vtx[0] = face->v1;
185                                 vtx[1] = face->v2;
186                                 vtx[2] = face->v3;
187                                 vtx[3] = face->v4;
188                                 bool found = false;
189
190                                 for (int o = 0; o < 4; ++o) {
191                                         if (o > 2 && vtx[o] == 0) {
192                                                 break;
193                                         }
194
195                                         if (vtx[o] == num) {
196                                                 uv_values.push_back(Imath::V2f(tface->uv[o][0], tface->uv[o][1]));
197
198                                                 MVert *mv = mverts + vtx[o];
199
200                                                 normal_short_to_float_v3(normal, mv->no);
201                                                 copy_zup_yup(tmp_nor.getValue(), normal);
202                                                 norm_values.push_back(tmp_nor);
203                                                 found = true;
204                                                 break;
205                                         }
206                                 }
207
208                                 if (found) {
209                                         break;
210                                 }
211                         }
212                 }
213
214                 int steps = path->segments + 1;
215                 hvertices.push_back(steps);
216
217                 for (k = 0; k < steps; ++k) {
218                         float vert[3];
219                         copy_v3_v3(vert, path->co);
220                         mul_m4_v3(inv_mat, vert);
221
222                         /* Convert Z-up to Y-up. */
223                         verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));
224
225                         ++path;
226                 }
227         }
228 }
229
230 void AbcHairWriter::write_hair_child_sample(DerivedMesh *dm,
231                                             ParticleSettings *part,
232                                             std::vector<Imath::V3f> &verts,
233                                             std::vector<Imath::V3f> &norm_values,
234                                             std::vector<Imath::V2f> &uv_values,
235                                             std::vector<int32_t> &hvertices)
236 {
237         /* Get untransformed vertices, there's a xform under the hair. */
238         float inv_mat[4][4];
239         invert_m4_m4_safe(inv_mat, m_object->obmat);
240
241         MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
242         MFace *mface = dm->getTessFaceArray(dm);
243         MVert *mverts = dm->getVertArray(dm);
244
245         if (!mtface || !mface) {
246                 std::fprintf(stderr, "Warning, no UV set found for underlying geometry.\n");
247         }
248
249         ParticleCacheKey **cache = m_psys->childcache;
250         ParticleCacheKey *path;
251
252         ChildParticle *pc = m_psys->child;
253
254         for (int p = 0; p < m_psys->totchild; ++p, ++pc) {
255                 path = cache[p];
256
257                 if (part->from == PART_FROM_FACE) {
258                         const int num = pc->num;
259
260                         MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
261                         MTFace *tface = mtface + num;
262
263                         if (mface && mtface) {
264                                 float r_uv[2], tmpnor[3], mapfw[4], vec[3];
265
266                                 psys_interpolate_uvs(tface, face->v4, pc->fuv, r_uv);
267                                 uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
268
269                                 psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, tmpnor, NULL, NULL, NULL, NULL);
270
271                                 /* Convert Z-up to Y-up. */
272                                 norm_values.push_back(Imath::V3f(tmpnor[0], tmpnor[2], -tmpnor[1]));
273                         }
274                 }
275
276                 int steps = path->segments + 1;
277                 hvertices.push_back(steps);
278
279                 for (int k = 0; k < steps; ++k) {
280                         float vert[3];
281                         copy_v3_v3(vert, path->co);
282                         mul_m4_v3(inv_mat, vert);
283
284                         /* Convert Z-up to Y-up. */
285                         verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));
286
287                         ++path;
288                 }
289         }
290 }