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
81         std::vector<Imath::V3f> verts;
82         std::vector<int32_t> hvertices;
83         std::vector<Imath::V2f> uv_values;
84         std::vector<Imath::V3f> norm_values;
85
86         if (m_psys->pathcache) {
87                 ParticleSettings *part = m_psys->part;
88
89                 write_hair_sample(dm, part, verts, norm_values, uv_values, hvertices);
90
91                 if (m_settings.export_child_hairs && m_psys->childcache) {
92                         write_hair_child_sample(dm, part, verts, norm_values, uv_values, hvertices);
93                 }
94         }
95
96         dm->release(dm);
97
98         Alembic::Abc::P3fArraySample iPos(verts);
99         m_sample = OCurvesSchema::Sample(iPos, hvertices);
100         m_sample.setBasis(Alembic::AbcGeom::kNoBasis);
101         m_sample.setType(Alembic::AbcGeom::kLinear);
102         m_sample.setWrap(Alembic::AbcGeom::kNonPeriodic);
103
104         if (!uv_values.empty()) {
105                 OV2fGeomParam::Sample uv_smp;
106                 uv_smp.setVals(uv_values);
107                 m_sample.setUVs(uv_smp);
108         }
109
110         if (!norm_values.empty()) {
111                 ON3fGeomParam::Sample norm_smp;
112                 norm_smp.setVals(norm_values);
113                 m_sample.setNormals(norm_smp);
114         }
115
116         m_sample.setSelfBounds(bounds());
117         m_schema.set(m_sample);
118 }
119
120 void AbcHairWriter::write_hair_sample(DerivedMesh *dm,
121                                       ParticleSettings *part,
122                                       std::vector<Imath::V3f> &verts,
123                                       std::vector<Imath::V3f> &norm_values,
124                                       std::vector<Imath::V2f> &uv_values,
125                                       std::vector<int32_t> &hvertices)
126 {
127         /* Get untransformed vertices, there's a xform under the hair. */
128         float inv_mat[4][4];
129         invert_m4_m4_safe(inv_mat, m_object->obmat);
130
131         MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
132         MFace *mface = dm->getTessFaceArray(dm);
133         MVert *mverts = dm->getVertArray(dm);
134
135         if (!mtface || !mface) {
136                 std::fprintf(stderr, "Warning, no UV set found for underlying geometry.\n");
137         }
138
139         ParticleData * pa = m_psys->particles;
140         int k;
141
142         ParticleCacheKey **cache = m_psys->pathcache;
143         ParticleCacheKey *path;
144         float normal[3];
145         Imath::V3f tmp_nor;
146
147         for (int p = 0; p < m_psys->totpart; ++p, ++pa) {
148                 /* underlying info for faces-only emission */
149                 path = cache[p];
150
151                 if (part->from == PART_FROM_FACE && mtface) {
152                         const int num = pa->num_dmcache >= 0 ? pa->num_dmcache : pa->num;
153
154                         if (num < dm->getNumTessFaces(dm)) {
155                                 MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
156                                 MTFace *tface = mtface + num;
157
158                                 if (mface) {
159                                         float r_uv[2], mapfw[4], vec[3];
160
161                                         psys_interpolate_uvs(tface, face->v4, pa->fuv, r_uv);
162                                         uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
163
164                                         psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, normal, NULL, NULL, NULL, NULL);
165
166                                         copy_yup_from_zup(tmp_nor.getValue(), normal);
167                                         norm_values.push_back(tmp_nor);
168                                 }
169                         }
170                         else {
171                                 std::fprintf(stderr, "Particle to faces overflow (%d/%d)\n", num, dm->getNumTessFaces(dm));
172                         }
173                 }
174                 else if (part->from == PART_FROM_VERT && mtface) {
175                         /* vertex id */
176                         const int num = (pa->num_dmcache >= 0) ? pa->num_dmcache : pa->num;
177
178                         /* iterate over all faces to find a corresponding underlying UV */
179                         for (int n = 0; n < dm->getNumTessFaces(dm); ++n) {
180                                 MFace *face  = static_cast<MFace *>(dm->getTessFaceData(dm, n, CD_MFACE));
181                                 MTFace *tface = mtface + n;
182                                 unsigned int vtx[4];
183                                 vtx[0] = face->v1;
184                                 vtx[1] = face->v2;
185                                 vtx[2] = face->v3;
186                                 vtx[3] = face->v4;
187                                 bool found = false;
188
189                                 for (int o = 0; o < 4; ++o) {
190                                         if (o > 2 && vtx[o] == 0) {
191                                                 break;
192                                         }
193
194                                         if (vtx[o] == num) {
195                                                 uv_values.push_back(Imath::V2f(tface->uv[o][0], tface->uv[o][1]));
196
197                                                 MVert *mv = mverts + vtx[o];
198
199                                                 normal_short_to_float_v3(normal, mv->no);
200                                                 copy_yup_from_zup(tmp_nor.getValue(), normal);
201                                                 norm_values.push_back(tmp_nor);
202                                                 found = true;
203                                                 break;
204                                         }
205                                 }
206
207                                 if (found) {
208                                         break;
209                                 }
210                         }
211                 }
212
213                 int steps = path->segments + 1;
214                 hvertices.push_back(steps);
215
216                 for (k = 0; k < steps; ++k) {
217                         float vert[3];
218                         copy_v3_v3(vert, path->co);
219                         mul_m4_v3(inv_mat, vert);
220
221                         /* Convert Z-up to Y-up. */
222                         verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));
223
224                         ++path;
225                 }
226         }
227 }
228
229 void AbcHairWriter::write_hair_child_sample(DerivedMesh *dm,
230                                             ParticleSettings *part,
231                                             std::vector<Imath::V3f> &verts,
232                                             std::vector<Imath::V3f> &norm_values,
233                                             std::vector<Imath::V2f> &uv_values,
234                                             std::vector<int32_t> &hvertices)
235 {
236         /* Get untransformed vertices, there's a xform under the hair. */
237         float inv_mat[4][4];
238         invert_m4_m4_safe(inv_mat, m_object->obmat);
239
240         MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
241         MFace *mface = dm->getTessFaceArray(dm);
242         MVert *mverts = dm->getVertArray(dm);
243
244         if (!mtface || !mface) {
245                 std::fprintf(stderr, "Warning, no UV set found for underlying geometry.\n");
246         }
247
248         ParticleCacheKey **cache = m_psys->childcache;
249         ParticleCacheKey *path;
250
251         ChildParticle *pc = m_psys->child;
252
253         for (int p = 0; p < m_psys->totchild; ++p, ++pc) {
254                 path = cache[p];
255
256                 if (part->from == PART_FROM_FACE) {
257                         const int num = pc->num;
258
259                         MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
260                         MTFace *tface = mtface + num;
261
262                         if (mface && mtface) {
263                                 float r_uv[2], tmpnor[3], mapfw[4], vec[3];
264
265                                 psys_interpolate_uvs(tface, face->v4, pc->fuv, r_uv);
266                                 uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
267
268                                 psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, tmpnor, NULL, NULL, NULL, NULL);
269
270                                 /* Convert Z-up to Y-up. */
271                                 norm_values.push_back(Imath::V3f(tmpnor[0], tmpnor[2], -tmpnor[1]));
272                         }
273                 }
274
275                 int steps = path->segments + 1;
276                 hvertices.push_back(steps);
277
278                 for (int k = 0; k < steps; ++k) {
279                         float vert[3];
280                         copy_v3_v3(vert, path->co);
281                         mul_m4_v3(inv_mat, vert);
282
283                         /* Convert Z-up to Y-up. */
284                         verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));
285
286                         ++path;
287                 }
288         }
289 }