File: qslim.cpp

package info (click to toggle)
k3d 0.8.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 40,692 kB
  • ctags: 39,695
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 726; sh: 22
file content (471 lines) | stat: -rw-r--r-- 16,336 bytes parent folder | download | duplicates (5)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// K-3D
// Copyright (c) 2004-2006, Romain Behar
//
// Contact: romainbehar@yahoo.com
//
// 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
	\author Romain Behar (romainbehar@yahoo.com)
	\author Timothy M. Shead (tshead@k-3d.com)
*/

#include "MxQSlim.h"

#include <k3d-i18n-config.h>
#include <k3dsdk/basic_math.h>
#include <k3dsdk/document_plugin_factory.h>
#include <k3dsdk/hints.h>
#include <k3dsdk/material_sink.h>
#include <k3dsdk/mesh_modifier.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/module.h>
#include <k3dsdk/node.h>
#include <k3dsdk/polyhedron.h>
#include <k3dsdk/property.h>
#include <k3dsdk/triangulator.h>
#include <k3dsdk/utility.h>

#include <boost/scoped_ptr.hpp>

namespace module
{

namespace qslim
{

/////////////////////////////////////////////////////////////////////////////
// qslim

class qslim :
	public k3d::material_sink<k3d::mesh_modifier<k3d::node > >
{
	typedef k3d::material_sink<k3d::mesh_modifier<k3d::node > > base;

public:
	qslim(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
		base(Factory, Document),
		m_face_count(init_owner(*this) + init_name("face_count") + init_label(_("Face Count")) + init_description(_("Desired number of outputs faces.")) + init_value(1000) + init_constraint(constraint::minimum<k3d::int32_t>(1)) + init_step_increment(1) + init_units(typeid(k3d::measurement::scalar))),
		m_contraction_type(init_owner(*this) + init_name("contraction_type") + init_label(_("Contraction type")) + init_description(_("Stop function")) + init_value(EDGE) + init_enumeration(contraction_values())),
		m_placement_policy(init_owner(*this) + init_name("placement_policy") + init_label(_("Placement policy")) + init_description(_("Placement policy (optimal, line, endpoint or midpoint, endpoints)")) + init_value(OPTIMAL) + init_enumeration(placement_values())),
		m_quadric_weighting(init_owner(*this) + init_name("quadric_weighting") + init_label(_("Quadric weighting")) + init_description(_("Quadric weighting policy (uniform, area, angle)")) + init_value(AREA) + init_enumeration(quadric_weighting_values())),
		m_boundary_weight(init_owner(*this) + init_name("boundary_weight") + init_label(_("Boundary weight")) + init_description(_("Use boundary preservation planes with given weight")) + init_value(1000.0) + init_step_increment(0.1) + init_units(typeid(k3d::measurement::scalar))),
		m_compactness_ratio(init_owner(*this) + init_name("compactness_ratio") + init_label(_("Compactness ratio")) + init_description(_("Compactness ratio")) + init_value(k3d::radians(0.0)) + init_step_increment(0.01) + init_units(typeid(k3d::measurement::angle))),
		m_meshing_penalty(init_owner(*this) + init_name("meshing_penalty") + init_label(_("Meshing penalty")) + init_description(_("Penalty for bad meshes")) + init_value(1.0) + init_step_increment(0.01) + init_units(typeid(k3d::measurement::scalar)))
	{
		m_material.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
		m_face_count.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
		m_contraction_type.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
		m_placement_policy.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
		m_quadric_weighting.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
		m_boundary_weight.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
		m_compactness_ratio.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
		m_meshing_penalty.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::mesh_topology_changed> >(make_reset_mesh_slot()));
	}

	class qslim_convert :
		public k3d::triangulator
	{
	public:
		qslim_convert(MxStdModel& Model) :
			model(Model)
		{
		}

	private:
		void add_vertex(const k3d::point3& Coordinates, k3d::uint_t Vertices[4], k3d::uint_t Edges[4], k3d::double_t Weights[4], k3d::uint_t NewVertex)
		{
			NewVertex = model.vert_count();
			model.add_vertex(-Coordinates[0], Coordinates[1], Coordinates[2]);
		}

		void add_triangle(k3d::uint_t Vertices[3], k3d::uint_t Edges[3])
		{
			model.add_face(Vertices[0], Vertices[1], Vertices[2]);
		}

		MxStdModel& model;
	};

	/// Converts a K-3D polyhedron to a QSlim model
	static MxStdModel* convert(const k3d::mesh& Mesh, const k3d::polyhedron::const_primitive& Primitive)
	{
		// Create a new QSlim model ...
		MxStdModel* const model = new MxStdModel(100, 100);

		// Add mesh points to the model ...
		return_val_if_fail(Mesh.points, model);
		const k3d::mesh::points_t& points = *Mesh.points;

		const k3d::uint_t point_begin = 0;
		const k3d::uint_t point_end = point_begin + points.size();
		for(k3d::uint_t point = point_begin; point != point_end; ++point)
			model->add_vertex(-points[point][0], points[point][1], points[point][2]);

		// Convert polygons to triangles ...
		qslim_convert(*model).process(Mesh, Primitive);
		
		return model;
	}

	/// Appends a QSlim model to a K-3D mesh as a polyhedron primitive.
	static void append(const k3d::polyhedron::const_primitive& InputPolyhedron, MxStdModel& Model, k3d::mesh& Mesh, k3d::imaterial* const Material)
	{
		k3d::mesh::points_t& points = Mesh.points.writable();
		k3d::mesh::selection_t& point_selection = Mesh.point_selection.writable();
		const k3d::uint_t point_offset = points.size();
		for(unsigned int v = 0; v < Model.vert_count(); ++v)
		{
			k3d::point3 position(Model.vertex(v));
			position[0] = -position[0];
			points.push_back(position);
			point_selection.push_back(0);
		}

		boost::scoped_ptr<k3d::polyhedron::primitive> polyhedron(k3d::polyhedron::create(Mesh));
		polyhedron->constant_attributes = InputPolyhedron.constant_attributes;

		for(unsigned int f = 0; f != Model.face_count(); ++f)
		{
			if(!Model.face_is_valid(f))
				continue;

			polyhedron->face_shells.push_back(0);
			polyhedron->face_first_loops.push_back(polyhedron->loop_first_edges.size());
			polyhedron->face_loop_counts.push_back(1);
			polyhedron->face_selections.push_back(0);
			polyhedron->face_materials.push_back(Material);

			polyhedron->loop_first_edges.push_back(polyhedron->clockwise_edges.size());

			polyhedron->clockwise_edges.push_back(polyhedron->clockwise_edges.size() + 1);
			polyhedron->edge_selections.push_back(0);
			polyhedron->vertex_points.push_back(point_offset + Model.face(f).v[0]);
			polyhedron->vertex_selections.push_back(0);

			polyhedron->clockwise_edges.push_back(polyhedron->clockwise_edges.size() + 1);
			polyhedron->edge_selections.push_back(0);
			polyhedron->vertex_points.push_back(point_offset + Model.face(f).v[1]);
			polyhedron->vertex_selections.push_back(0);

			polyhedron->clockwise_edges.push_back(polyhedron->loop_first_edges.back());
			polyhedron->edge_selections.push_back(0);
			polyhedron->vertex_points.push_back(point_offset + Model.face(f).v[2]);
			polyhedron->vertex_selections.push_back(0);
		}

		polyhedron->shell_types.push_back(k3d::polyhedron::POLYGONS);
	}

	void on_create_mesh(const k3d::mesh& Input, k3d::mesh& Output)
	{
		Output.points = Input.points;
		Output.point_selection = Input.point_selection;

		k3d::imaterial* const material = m_material.pipeline_value();

		// For each primitive ...
		for(k3d::mesh::primitives_t::const_iterator primitive = Input.primitives.begin(); primitive != Input.primitives.end(); ++primitive)
		{
			boost::scoped_ptr<k3d::polyhedron::const_primitive> polyhedron(k3d::polyhedron::validate(Input, **primitive));
			if(!polyhedron)
			{
				Output.primitives.push_back(*primitive);
				continue;
			}

			// Convert polyhedron to a QSlim model ... 
			boost::scoped_ptr<MxStdModel> model(convert(Output, *polyhedron));

			const k3d::int32_t face_number = std::max(0, std::min(static_cast<k3d::int32_t>(model->face_count()), m_face_count.pipeline_value()));
			
			boost::scoped_ptr<MxQSlim> slim;
			switch(m_contraction_type.pipeline_value())
			{
				case EDGE:
					slim.reset(new MxEdgeQSlim(*model));
					break;
				case FACE:
					slim.reset(new MxFaceQSlim(*model));
					break;
			}

			switch(m_placement_policy.pipeline_value())
			{
				case ENDPOINTS:
					slim->placement_policy = 0;
					break;
				case ENDORMID:
					slim->placement_policy = 1;
					break;
				case LINE:
					slim->placement_policy = 2;
					break;
				case OPTIMAL:
					slim->placement_policy = 3;
					break;
			}

			slim->boundary_weight = m_boundary_weight.pipeline_value();

			switch(m_quadric_weighting.pipeline_value())
			{
				case UNIFORM:
					slim->weighting_policy = 0;
					break;
				case AREA:
					slim->weighting_policy = 1;
					break;
				case ANGLE:
					slim->weighting_policy = 2;
					break;
			}

			slim->compactness_ratio = m_compactness_ratio.pipeline_value();
			slim->meshing_penalty = m_meshing_penalty.pipeline_value();
			slim->will_join_only = false;

			// Make it happen ...
			slim->initialize();
			slim->decimate(m_face_count.pipeline_value());

			append(*polyhedron, *model, Output, material);
		}

		k3d::mesh::bools_t unused_points;
		k3d::mesh::lookup_unused_points(Output, unused_points);
		k3d::mesh::delete_points(Output, unused_points);
	}

	void on_update_mesh(const k3d::mesh& Input, k3d::mesh& Output)
	{
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::document_plugin_factory<qslim,
			k3d::interface_list<k3d::imesh_source,
			k3d::interface_list<k3d::imesh_sink > > > factory(
				k3d::uuid(0x9bfe8fce, 0x3c7c4b00, 0x9e050d1e, 0x062059fa),
				"QSlim",
				"Surface simplification using quadric error metrics",
				"Polyhedron",
				k3d::iplugin_factory::EXPERIMENTAL);

		return factory;
	}

private:
	/// Enumerates contraction functions
	typedef enum
	{
		EDGE,
		FACE
	} contraction_t;

	friend std::ostream& operator << (std::ostream& Stream, const contraction_t& Value)
	{
		switch(Value)
		{
			case EDGE:
				Stream << "edge";
			break;
			case FACE:
				Stream << "face";
			break;
		}

		return Stream;
	}

	friend std::istream& operator >> (std::istream& Stream, contraction_t& Value)
	{
		std::string text;
		Stream >> text;

		if(text == "edge")
			Value = EDGE;
		else if(text == "face")
			Value = FACE;
		else
			k3d::log() << error << k3d_file_reference << ": unknown enumeration [" << text << "]" << std::endl;

		return Stream;
	}

	static const k3d::ienumeration_property::enumeration_values_t& contraction_values()
	{
		static k3d::ienumeration_property::enumeration_values_t values;
		if(values.empty())
		{
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Edge", "edge", "Use edge contraction"));
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Face", "face", "Use face contraction"));
		}

		return values;
	}

	/// Enumerates placement policies
	typedef enum
	{
		OPTIMAL,
		LINE,
		ENDORMID,
		ENDPOINTS
	} placement_policy_t;

	friend std::ostream& operator << (std::ostream& Stream, const placement_policy_t& Value)
	{
		switch(Value)
		{
			case OPTIMAL:
				Stream << "optimal";
				break;
			case LINE:
				Stream << "line";
				break;
			case ENDORMID:
				Stream << "endormid";
				break;
			case ENDPOINTS:
				Stream << "endpoints";
				break;
		}

		return Stream;
	}

	friend std::istream& operator >> (std::istream& Stream, placement_policy_t& Value)
	{
		std::string text;
		Stream >> text;

		if(text == "optimal")
			Value = OPTIMAL;
		else if(text == "line")
			Value = LINE;
		else if(text == "endormid")
			Value = ENDORMID;
		else if(text == "endpoints")
			Value = ENDPOINTS;
		else
			k3d::log() << error << k3d_file_reference << ": unknown enumeration [" << text << "]" << std::endl;

		return Stream;
	}

	const k3d::ienumeration_property::enumeration_values_t& placement_values()
	{
		static k3d::ienumeration_property::enumeration_values_t values;
		if(values.empty())
		{
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Optimal", "optimal", "Use optimal placement policy"));
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Line", "line", "Use line placement policy"));
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Endormid", "endormid", "Use end-point or mid-point placement policy"));
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Endpoints", "endpoints", "Use end-points placement policy"));
		}

		return values;
	}

	/// Enumerates quadric weighting policies
	typedef enum
	{
		UNIFORM,
		AREA,
		ANGLE
	} quadric_weighting_t;

	friend std::ostream& operator << (std::ostream& Stream, const quadric_weighting_t& Value)
	{
		switch(Value)
		{
			case UNIFORM:
				Stream << "uniform";
				break;
			case AREA:
				Stream << "area";
				break;
			case ANGLE:
				Stream << "angle";
				break;
		}

		return Stream;
	}

	friend std::istream& operator >> (std::istream& Stream, quadric_weighting_t& Value)
	{
		std::string text;
		Stream >> text;

		if(text == "uniform")
			Value = UNIFORM;
		else if(text == "area")
			Value = AREA;
		else if(text == "angle")
			Value = ANGLE;
		else
			k3d::log() << error << k3d_file_reference << ": unknown enumeration [" << text << "]" << std::endl;

		return Stream;
	}

	const k3d::ienumeration_property::enumeration_values_t& quadric_weighting_values()
	{
		static k3d::ienumeration_property::enumeration_values_t values;
		if(values.empty())
		{
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Uniform", "uniform", "Use uniform quadric weighting"));
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Area", "area", "Use area quadric weighting"));
			values.push_back(k3d::ienumeration_property::enumeration_value_t("Angle", "angle", "Use angle quadric weighting"));
		}

		return values;
	}

	k3d_data(k3d::int32_t, immutable_name, change_signal, with_undo, local_storage, with_constraint, measurement_property, with_serialization) m_face_count;
	k3d_data(contraction_t, immutable_name, change_signal, with_undo, local_storage, no_constraint, enumeration_property, with_serialization) m_contraction_type;
	k3d_data(placement_policy_t, immutable_name, change_signal, with_undo, local_storage, no_constraint, enumeration_property, with_serialization) m_placement_policy;
	k3d_data(quadric_weighting_t, immutable_name, change_signal, with_undo, local_storage, no_constraint, enumeration_property, with_serialization) m_quadric_weighting;
	k3d_data(k3d::double_t, immutable_name, change_signal, with_undo, local_storage, no_constraint, measurement_property, with_serialization) m_boundary_weight;
	k3d_data(k3d::double_t, immutable_name, change_signal, with_undo, local_storage, no_constraint, measurement_property, with_serialization) m_compactness_ratio;
	k3d_data(k3d::double_t, immutable_name, change_signal, with_undo, local_storage, no_constraint, measurement_property, with_serialization) m_meshing_penalty;
};

/////////////////////////////////////////////////////////////////////////////
// qslim_factory

k3d::iplugin_factory& qslim_factory()
{
	return qslim::get_factory();
}

} // namespace qslim

} // namespace module

K3D_MODULE_START(Registry)
	Registry.register_factory(module::qslim::qslim_factory());
K3D_MODULE_END