Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abc_nurbs.cc « intern « alembic « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eaef06fd6d11022a6ba358dab7a5d77c83b47e9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "abc_nurbs.h"

#include "abc_transform.h"
#include "abc_util.h"

extern "C" {
#include "MEM_guardedalloc.h"

#include "DNA_curve_types.h"
#include "DNA_object_types.h"

#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_string.h"

#include "BKE_curve.h"
#include "BKE_object.h"
}

using Alembic::AbcGeom::bool_t;
using Alembic::AbcGeom::FloatArraySample;
using Alembic::AbcGeom::FloatArraySamplePtr;
using Alembic::AbcGeom::MetaData;
using Alembic::AbcGeom::P3fArraySamplePtr;
using Alembic::AbcGeom::kWrapExisting;

using Alembic::AbcGeom::IBoolProperty;
using Alembic::AbcGeom::ICompoundProperty;
using Alembic::AbcGeom::INuPatch;
using Alembic::AbcGeom::INuPatchSchema;
using Alembic::AbcGeom::IObject;
using Alembic::AbcGeom::ISampleSelector;

using Alembic::AbcGeom::OBoolProperty;
using Alembic::AbcGeom::OCompoundProperty;
using Alembic::AbcGeom::ONuPatch;
using Alembic::AbcGeom::ONuPatchSchema;

/* ************************************************************************** */

AbcNurbsWriter::AbcNurbsWriter(Scene *scene,
                               Object *ob,
                               AbcTransformWriter *parent,
                               uint32_t time_sampling,
                               ExportSettings &settings)
    : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
{
	m_is_animated = isAnimated();

	/* if the object is static, use the default static time sampling */
	if (!m_is_animated) {
		m_time_sampling = 0;
	}

	Curve *curve = static_cast<Curve *>(m_object->data);
	size_t numNurbs = BLI_listbase_count(&curve->nurb);

	for (size_t i = 0; i < numNurbs; ++i) {
		std::stringstream str;
		str << m_name << '_' << i;

		while (parent->alembicXform().getChildHeader(str.str())) {
			str << "_";
		}

		ONuPatch nurbs(parent->alembicXform(), str.str().c_str(), m_time_sampling);
		m_nurbs_schema.push_back(nurbs.getSchema());
	}
}

bool AbcNurbsWriter::isAnimated() const
{
	/* check if object has shape keys */
	Curve *cu = static_cast<Curve *>(m_object->data);
	return (cu->key != NULL);
}

static void get_knots(std::vector<float> &knots, const int num_knots, float *nu_knots)
{
	if (num_knots <= 1) {
		return;
	}

	/* Add an extra knot at the beggining and end of the array since most apps
	 * require/expect them. */
	knots.reserve(num_knots + 2);

	knots.push_back(0.0f);

	for (int i = 0; i < num_knots; ++i) {
		knots.push_back(nu_knots[i]);
	}

	knots[0] = 2.0f * knots[1] - knots[2];
	knots.push_back(2.0f * knots[num_knots] - knots[num_knots - 1]);
}

void AbcNurbsWriter::do_write()
{
	/* we have already stored a sample for this object. */
	if (!m_first_frame && !m_is_animated) {
		return;
	}

	if (!ELEM(m_object->type, OB_SURF, OB_CURVE)) {
		return;
	}

	Curve *curve = static_cast<Curve *>(m_object->data);
	ListBase *nulb;

	if (m_object->curve_cache->deformed_nurbs.first != NULL) {
		nulb = &m_object->curve_cache->deformed_nurbs;
	}
	else {
		nulb = BKE_curve_nurbs_get(curve);
	}

	size_t count = 0;
	for (Nurb *nu = static_cast<Nurb *>(nulb->first); nu; nu = nu->next, ++count) {
		std::vector<float> knotsU;
		get_knots(knotsU, KNOTSU(nu), nu->knotsu);

		std::vector<float> knotsV;
		get_knots(knotsV, KNOTSV(nu), nu->knotsv);

		const int size = nu->pntsu * nu->pntsv;
		std::vector<Imath::V3f> positions(size);
		std::vector<float> weights(size);

		const BPoint *bp = nu->bp;

		for (int i = 0; i < size; ++i, ++bp) {
			copy_yup_from_zup(positions[i].getValue(), bp->vec);
			weights[i] = bp->vec[3];
		}

		ONuPatchSchema::Sample sample;
		sample.setUOrder(nu->orderu + 1);
		sample.setVOrder(nu->orderv + 1);
		sample.setPositions(positions);
		sample.setPositionWeights(weights);
		sample.setUKnot(FloatArraySample(knotsU));
		sample.setVKnot(FloatArraySample(knotsV));
		sample.setNu(nu->pntsu);
		sample.setNv(nu->pntsv);

		/* TODO(kevin): to accomodate other software we should duplicate control
		 * points to indicate that a NURBS is cyclic. */
		OCompoundProperty user_props = m_nurbs_schema[count].getUserProperties();

		if ((nu->flagu & CU_NURB_ENDPOINT) != 0) {
			OBoolProperty prop(user_props, "endpoint_u");
			prop.set(true);
		}

		if ((nu->flagv & CU_NURB_ENDPOINT) != 0) {
			OBoolProperty prop(user_props, "endpoint_v");
			prop.set(true);
		}

		if ((nu->flagu & CU_NURB_CYCLIC) != 0) {
			OBoolProperty prop(user_props, "cyclic_u");
			prop.set(true);
		}

		if ((nu->flagv & CU_NURB_CYCLIC) != 0) {
			OBoolProperty prop(user_props, "cyclic_v");
			prop.set(true);
		}

		m_nurbs_schema[count].set(sample);
	}
}

/* ************************************************************************** */

AbcNurbsReader::AbcNurbsReader(const IObject &object, ImportSettings &settings)
    : AbcObjectReader(object, settings)
{
	getNurbsPatches(m_iobject);
	get_min_max_time(m_iobject, m_schemas[0].first, m_min_time, m_max_time);
}

bool AbcNurbsReader::valid() const
{
	if (m_schemas.empty()) {
		return false;
	}

	std::vector< std::pair<INuPatchSchema, IObject> >::const_iterator it;
	for (it = m_schemas.begin(); it != m_schemas.end(); ++it) {
		const INuPatchSchema &schema = it->first;

		if (!schema.valid()) {
			return false;
		}
	}

	return true;
}

static bool set_knots(const FloatArraySamplePtr &knots, float *&nu_knots)
{
	if (!knots || knots->size() == 0) {
		return false;
	}

	/* Skip first and last knots, as they are used for padding. */
	const size_t num_knots = knots->size() - 2;
	nu_knots = static_cast<float *>(MEM_callocN(num_knots * sizeof(float), "abc_setsplineknotsu"));

	for (size_t i = 0; i < num_knots; ++i) {
		nu_knots[i] = (*knots)[i + 1];
	}

	return true;
}

void AbcNurbsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
{
	Curve *cu = static_cast<Curve *>(BKE_curve_add(bmain, "abc_curve", OB_SURF));
	cu->actvert = CU_ACT_NONE;

	std::vector< std::pair<INuPatchSchema, IObject> >::iterator it;

	for (it = m_schemas.begin(); it != m_schemas.end(); ++it) {
		Nurb *nu = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "abc_getnurb"));
		nu->flag  = CU_SMOOTH;
		nu->type = CU_NURBS;
		nu->resolu = cu->resolu;
		nu->resolv = cu->resolv;

		const INuPatchSchema &schema = it->first;
		const INuPatchSchema::Sample smp = schema.getValue(sample_sel);

		nu->orderu = smp.getUOrder() - 1;
		nu->orderv = smp.getVOrder() - 1;
		nu->pntsu = smp.getNumU();
		nu->pntsv = smp.getNumV();

		/* Read positions and weights. */

		const P3fArraySamplePtr positions = smp.getPositions();
		const FloatArraySamplePtr weights = smp.getPositionWeights();

		const size_t num_points = positions->size();

		nu->bp = static_cast<BPoint *>(MEM_callocN(num_points * sizeof(BPoint), "abc_setsplinetype"));

		BPoint *bp = nu->bp;
		float posw_in = 1.0f;

		for (int i = 0; i < num_points; ++i, ++bp) {
			const Imath::V3f &pos_in = (*positions)[i];

			if (weights) {
				posw_in = (*weights)[i];
			}

			copy_zup_from_yup(bp->vec, pos_in.getValue());
			bp->vec[3] = posw_in;
			bp->f1 = SELECT;
			bp->radius = 1.0f;
			bp->weight = 1.0f;
		}

		/* Read knots. */

		if (!set_knots(smp.getUKnot(), nu->knotsu)) {
			BKE_nurb_knot_calc_u(nu);
		}

		if (!set_knots(smp.getVKnot(), nu->knotsv)) {
			BKE_nurb_knot_calc_v(nu);
		}

		/* Read flags. */

		ICompoundProperty user_props = schema.getUserProperties();

		if (has_property(user_props, "enpoint_u")) {
			nu->flagu |= CU_NURB_ENDPOINT;
		}

		if (has_property(user_props, "enpoint_v")) {
			nu->flagv |= CU_NURB_ENDPOINT;
		}

		if (has_property(user_props, "cyclic_u")) {
			nu->flagu |= CU_NURB_CYCLIC;
		}

		if (has_property(user_props, "cyclic_v")) {
			nu->flagv |= CU_NURB_CYCLIC;
		}

		BLI_addtail(BKE_curve_nurbs_get(cu), nu);
	}

	BLI_strncpy(cu->id.name + 2, m_data_name.c_str(), m_data_name.size() + 1);

	m_object = BKE_object_add_only_object(bmain, OB_SURF, m_object_name.c_str());
	m_object->data = cu;
}

void AbcNurbsReader::getNurbsPatches(const IObject &obj)
{
	if (!obj.valid()) {
		return;
	}

	const int num_children = obj.getNumChildren();

	if (num_children == 0) {
		INuPatch abc_nurb(obj, kWrapExisting);
		INuPatchSchema schem = abc_nurb.getSchema();
		m_schemas.push_back(std::pair<INuPatchSchema, IObject>(schem, obj));
		return;
	}

	for (int i = 0; i < num_children; ++i) {
		bool ok = true;
		IObject child(obj, obj.getChildHeader(i).getName());

		if (!m_name.empty() && child.valid() && !begins_with(child.getFullName(), m_name)) {
			ok = false;
		}

		if (!child.valid()) {
			continue;
		}

		const MetaData &md = child.getMetaData();

		if (INuPatch::matches(md) && ok) {
			INuPatch abc_nurb(child, kWrapExisting);
			INuPatchSchema schem = abc_nurb.getSchema();
			m_schemas.push_back(std::pair<INuPatchSchema, IObject>(schem, child));
		}

		getNurbsPatches(child);
	}
}