File: coarsen_polyhedra.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (394 lines) | stat: -rw-r--r-- 14,887 bytes parent folder | download
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
// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.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
		\brief Creates a coarsened version of the input
		\author Romain Behar (romainbehar@yahoo.com)
*/

#include <k3dsdk/basic_math.h>
#include <k3dsdk/object.h>
#include <k3dsdk/persistence.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/mesh_filter.h>
#include <k3dsdk/module.h>
#include <k3dsdk/plugins.h>
#include <k3dsdk/utility.h>

#include "gts_interface.h"

namespace libk3dgts
{

static gdouble cost_angle(GtsEdge* e)
{
	if(e->triangles && e->triangles->next)
		{
			GtsTriangle* t1 = static_cast<GtsTriangle*>(e->triangles->data);
			GtsTriangle* t2 = static_cast<GtsTriangle*>(e->triangles->next->data);
			return std::fabs(gts_triangles_angle(t1, t2));
		}

	return G_MAXDOUBLE;
}

static GtsVolumeOptimizedParams volume_params = { 0.5, 0.5, 0. };

/////////////////////////////////////////////////////////////////////////////
// coarsen_polyhedra_implementation

class coarsen_polyhedra_implementation :
	public k3d::mesh_filter<k3d::persistent<k3d::object> >
{
	typedef k3d::mesh_filter<k3d::persistent<k3d::object> > base;

public:
	coarsen_polyhedra_implementation(k3d::idocument& Document) :
		base(Document),
		m_cost_function(k3d::init_name("cost_function") + k3d::init_description("Cost function [enumeration]") + k3d::init_value(LENGTH) + k3d::init_enumeration(cost_values()) + k3d::init_document(Document)),
		m_midvertex_function(k3d::init_name("midvertex_function") + k3d::init_description("Mid-vertex function [enumeration]") + k3d::init_value(VOLUMEOPTIMIZED) + k3d::init_enumeration(midvertex_values()) + k3d::init_document(Document)),
		m_stop_function(k3d::init_name("stop_function") + k3d::init_description("Stop function [enumeration]") + k3d::init_value(EDGENUMBER) + k3d::init_enumeration(stop_values()) + k3d::init_document(Document)),
		m_max_fold_angle(k3d::init_name("max_fold_angle") + k3d::init_description("Maximum fold angle [number]") + k3d::init_value(k3d::radians<double>(1.0)) + k3d::init_precision(2) + k3d::init_step_increment(0.01) + k3d::init_units(typeid(k3d::measurement::angle)) + k3d::init_document(Document)),
		m_edge_number(k3d::init_name("edge_number") + k3d::init_description("Final edge number [integer]") + k3d::init_value(100) + k3d::init_constraint(k3d::constraint::minimum(1UL)) + k3d::init_precision(0) + k3d::init_step_increment(1) + k3d::init_units(typeid(k3d::measurement::scalar)) + k3d::init_document(Document)),
		m_max_edge_cost(k3d::init_name("max_edge_cost") + k3d::init_description("Maximum edge cost [number]") + k3d::init_value(1.0) + k3d::init_precision(2) + k3d::init_step_increment(0.01) + k3d::init_units(typeid(k3d::measurement::scalar)) + k3d::init_document(Document)),
		m_volume_weight(k3d::init_name("volume_weight") + k3d::init_description("Weight used for volume optimization [number]") + k3d::init_value(0.5) + k3d::init_precision(2) + k3d::init_step_increment(0.1) + k3d::init_units(typeid(k3d::measurement::scalar)) + k3d::init_document(Document)),
		m_boundary_weight(k3d::init_name("boundary_weight") + k3d::init_description("Weight used for boundary optimization [number]") + k3d::init_value(0.5) + k3d::init_precision(2) + k3d::init_step_increment(0.1) + k3d::init_units(typeid(k3d::measurement::scalar)) + k3d::init_document(Document)),
		m_shape_weight(k3d::init_name("shape_weight") + k3d::init_description("Weight used for shape optimization [number]") + k3d::init_value(0.0) + k3d::init_precision(2) + k3d::init_step_increment(0.1) + k3d::init_units(typeid(k3d::measurement::scalar)) + k3d::init_document(Document))
	{
		enable_serialization(k3d::persistence::proxy(m_cost_function));
		enable_serialization(k3d::persistence::proxy(m_midvertex_function));
		enable_serialization(k3d::persistence::proxy(m_stop_function));
		enable_serialization(k3d::persistence::proxy(m_max_fold_angle));
		enable_serialization(k3d::persistence::proxy(m_edge_number));
		enable_serialization(k3d::persistence::proxy(m_max_edge_cost));
		enable_serialization(k3d::persistence::proxy(m_volume_weight));
		enable_serialization(k3d::persistence::proxy(m_boundary_weight));
		enable_serialization(k3d::persistence::proxy(m_shape_weight));

		register_property(m_cost_function);
		register_property(m_midvertex_function);
		register_property(m_stop_function);
		register_property(m_max_fold_angle);
		register_property(m_edge_number);
		register_property(m_max_edge_cost);
		register_property(m_volume_weight);
		register_property(m_boundary_weight);
		register_property(m_shape_weight);

		m_input_mesh.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_cost_function.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_midvertex_function.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_stop_function.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_max_fold_angle.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_edge_number.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_max_edge_cost.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_volume_weight.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_boundary_weight.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_shape_weight.changed_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_reset_geometry));
		m_output_mesh.need_data_signal().connect(SigC::slot(*this, &coarsen_polyhedra_implementation::on_create_geometry));
	}

	void on_reset_geometry()
	{
		m_output_mesh.reset();
	}

	k3d::mesh* on_create_geometry()
	{
		// TODO : process each polyhedron (+ commit_mesh_changes)

		// Get the input geometry ...
		k3d::mesh* const input = m_input_mesh.property_value();
		if(!input)
			return 0;

		const double fold = k3d::radians<double>(m_max_fold_angle.property_value());

		GtsSurface* surface = gts_surface(*input);

		// Select the right coarsening process
		GtsKeyFunc cost_func = 0;
		gpointer cost_data = 0;
		switch(m_cost_function.property_value())
			{
				case OPTIMIZED:
					cost_func = (GtsKeyFunc) gts_volume_optimized_cost;
					cost_data = &volume_params;

					volume_params.volume_weight = static_cast<gdouble>(m_volume_weight.property_value());
					volume_params.boundary_weight = static_cast<gdouble>(m_boundary_weight.property_value());
					volume_params.shape_weight = static_cast<gdouble>(m_shape_weight.property_value());
				break;
				case LENGTH:
					cost_func = 0;
				break;
				case ANGLE:
					cost_func = (GtsKeyFunc) cost_angle;
				break;
				default:
					assert_not_reached();
			}

		GtsCoarsenFunc coarsen_func = 0;
		gpointer coarsen_data = 0;
		GtsStopFunc stop_func = 0;
		gpointer stop_data = 0;
		switch(m_midvertex_function.property_value())
			{
				case MIDVERTEX:
					coarsen_func = 0;
				break;
				case VOLUMEOPTIMIZED:
					coarsen_func = (GtsCoarsenFunc) gts_volume_optimized_vertex;
					coarsen_data = &volume_params;

					volume_params.volume_weight = static_cast<gdouble>(m_volume_weight.property_value());
					volume_params.boundary_weight = static_cast<gdouble>(m_boundary_weight.property_value());
					volume_params.shape_weight = static_cast<gdouble>(m_shape_weight.property_value());
				break;
				default:
					assert_not_reached();
			}

		guint edge_number = static_cast<guint>(m_edge_number.property_value());
		gdouble max_edge_cost = static_cast<gdouble>(m_max_edge_cost.property_value());
		switch(m_stop_function.property_value())
			{
				case EDGENUMBER:
					stop_func = (GtsStopFunc) gts_coarsen_stop_number;
					stop_data = &edge_number;
				break;
				case COST:
					stop_func = (GtsStopFunc) gts_coarsen_stop_cost;
					stop_data = &max_edge_cost;
					break;
				default:
					assert_not_reached ();
			}

		gts_surface_coarsen(surface, cost_func, cost_data, coarsen_func, coarsen_data, stop_func, stop_data, fold);

		// Create output geometry ...
		k3d::mesh* const output = new k3d::mesh();
		copy_surface(surface, *output);

		// Copy materials ...
		if(input->polyhedra.size() && output->polyhedra.size())
			output->polyhedra.front()->material = input->polyhedra.front()->material;

		return output;
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<
			k3d::document_plugin<coarsen_polyhedra_implementation>,
				k3d::interface_list<k3d::imesh_source,
				k3d::interface_list<k3d::imesh_sink > > > factory(
				k3d::uuid(0xdaae61bd, 0xd5b94f9b, 0x90a54f79, 0xf3f78729),
				"CoarsenPolyhedra",
				"Coarsens polygonal surfaces",
				"Objects",
				k3d::iplugin_factory::EXPERIMENTAL);

		return factory;
	}

private:
	/// Enumerates cost function types
	typedef enum
	{
		OPTIMIZED,
		LENGTH,
		ANGLE
	} cost_t;

	friend std::ostream& operator << (std::ostream& Stream, const cost_t& Value)
	{
		switch(Value)
			{
				case OPTIMIZED:
					Stream << "optimized";
					break;
				case LENGTH:
					Stream << "length";
					break;
				case ANGLE:
					Stream << "angle";
					break;
			}

		return Stream;
	}

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

		if(text == "optimized")
			Value = OPTIMIZED;
		else if(text == "length")
			Value = LENGTH;
		else if(text == "angle")
			Value = ANGLE;
		else
			std::cerr << __PRETTY_FUNCTION__ << ": unknown enumeration [" << text << "]" << std::endl;

		return Stream;
	}

	const k3d::ienumeration_property::values_t& cost_values()
	{
		static k3d::ienumeration_property::values_t values;
		if(values.empty())
			{
				values.push_back(k3d::ienumeration_property::value_t("Optimized", "optimized", "Use optimized point cost"));
				values.push_back(k3d::ienumeration_property::value_t("Length", "length", "Use length^2 as cost function"));
				values.push_back(k3d::ienumeration_property::value_t("Angle", "angle", "Use angle as cost function"));
			}

		return values;
	}

	/// Enumerates mid-vertex functions
	typedef enum
	{
		MIDVERTEX,
		VOLUMEOPTIMIZED
	} midvertex_t;

	friend std::ostream& operator << (std::ostream& Stream, const midvertex_t& Value)
	{
		switch(Value)
			{
				case MIDVERTEX:
					Stream << "midvertex";
				break;
				case VOLUMEOPTIMIZED:
					Stream << "volumeoptimized";
				break;
			}

		return Stream;
	}

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

		if(text == "midvertex")
			Value = MIDVERTEX;
		else if(text == "volumeoptimized")
			Value = VOLUMEOPTIMIZED;
		else
			std::cerr << __PRETTY_FUNCTION__ << ": unknown enumeration [" << text << "]" << std::endl;

		return Stream;
	}

	const k3d::ienumeration_property::values_t& midvertex_values()
	{
		static k3d::ienumeration_property::values_t values;
		if(values.empty())
			{
				values.push_back(k3d::ienumeration_property::value_t("Mid-vertex", "midvertex", "Use mid-vertex as replacement vertex"));
				values.push_back(k3d::ienumeration_property::value_t("Volume optimized", "volumeoptimized", "Use volume optimized point"));
			}

		return values;
	}

	/// Enumerates stop functions
	typedef enum
	{
		EDGENUMBER,
		COST
	} stop_t;

	friend std::ostream& operator << (std::ostream& Stream, const stop_t& Value)
	{
		switch(Value)
			{
				case EDGENUMBER:
					Stream << "number";
				break;
				case COST:
					Stream << "cost";
				break;
			}

		return Stream;
	}

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

		if(text == "number")
			Value = EDGENUMBER;
		else if(text == "cost")
			Value = COST;
		else
			std::cerr << __PRETTY_FUNCTION__ << ": unknown enumeration [" << text << "]" << std::endl;

		return Stream;
	}

	const k3d::ienumeration_property::values_t& stop_values()
	{
		static k3d::ienumeration_property::values_t values;
		if(values.empty())
			{
				values.push_back(k3d::ienumeration_property::value_t("Number", "number", "Stop he coarsening process if the number of edges was to fall below"));
				values.push_back(k3d::ienumeration_property::value_t("Cost", "cost", "Stop the coarsening process if the cost of collapsing an edge is larger"));
			}

		return values;
	}

	k3d_enumeration_property(cost_t, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_cost_function;
	k3d_enumeration_property(midvertex_t, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_midvertex_function;
	k3d_enumeration_property(stop_t, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_stop_function;
	k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_max_fold_angle;
	k3d_measurement_property(unsigned long, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::with_constraint) m_edge_number;
	k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_max_edge_cost;
	k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_volume_weight;
	k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_boundary_weight;
	k3d_measurement_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_shape_weight;
};

/////////////////////////////////////////////////////////////////////////////
// coarsen_polyhedra_factory

k3d::iplugin_factory& coarsen_polyhedra_factory()
{
	return coarsen_polyhedra_implementation::get_factory();
}

} // namespace libk3dgts