File: morph_points.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 (139 lines) | stat: -rw-r--r-- 4,570 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
// K-3D
// Copyright (c) 1995-2008, 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
	\author Tim Shead (tshead@k-3d.com) parts of code
	\author Joaquin Duo (joaduo at lugmen org ar)
*/

#include <k3d-i18n-config.h>
#include <k3dsdk/algebra.h>
#include <k3dsdk/document_plugin_factory.h>
#include <k3dsdk/iuser_property.h>
#include <k3dsdk/mesh_simple_deformation_modifier.h>
#include <k3dsdk/property.h>
#include <k3dsdk/type_registry.h>
#include <k3dsdk/user_property_changed_signal.h>

namespace module
{

namespace deformation
{

/////////////////////////////////////////////////////////////////////////////
// morph_points

class morph_points :
	public k3d::mesh_simple_deformation_modifier
{
	typedef k3d::mesh_simple_deformation_modifier base;

public:
	morph_points(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
		base(Factory, Document),
		m_user_property_changed_signal(*this)
	{
		m_mesh_selection.changed_signal().connect(make_update_mesh_slot());
		m_user_property_changed_signal.connect(make_update_mesh_slot());
	}

	void on_deform_mesh(const k3d::mesh::points_t& InputPoints, const k3d::mesh::selection_t& PointSelection, k3d::mesh::points_t& OutputPoints)
	{
		std::vector<const k3d::mesh::points_t*> inputs;
		std::vector<k3d::double_t> amounts;
		
		//Search for the Meshes targets and its morph percent multiplier
		const k3d::iproperty_collection::properties_t& properties = k3d::node::properties();
		for(k3d::iproperty_collection::properties_t::const_iterator property = properties.begin(); property != properties.end(); ++property)
		{
			if(!dynamic_cast<k3d::iuser_property*>(*property))
				continue;
			
			if((**property).property_type() == typeid(k3d::mesh*))
			{
				if(k3d::mesh* const mesh = k3d::property::pipeline_value<k3d::mesh*>(**property))
				{
					if(const k3d::mesh::points_t* const points = mesh->points.get())
						inputs.push_back(points);
				}
			}
			else if((**property).property_type() == typeid(k3d::double_t))
			{
				amounts.push_back(k3d::property::pipeline_value<k3d::double_t>(**property));
			}
			else
			{
				k3d::log() << warning << factory().name() << ": user property [" << (**property).property_name() << "] with unsupported type [" << k3d::demangle((**property).property_type()) << "] will be ignored" << std::endl;
			}
		}

		if(inputs.size() != amounts.size())
		{
			k3d::log() << error << "mismatch in number of morph inputs and amounts." << std::endl;
			return;
		}

		OutputPoints = InputPoints;

		const k3d::uint_t input_begin = 0;
		const k3d::uint_t input_end = input_begin + inputs.size();
		for(k3d::uint_t i = input_begin; i != input_end; ++i)
		{
			const k3d::mesh::points_t& input_points = *inputs[i];
			const k3d::double_t amount = amounts[i];

			const k3d::uint_t point_begin = 0;
			const k3d::uint_t point_end = point_begin + std::min(OutputPoints.size(), input_points.size());
			for(k3d::uint_t point = point_begin; point != point_end; ++point)
				OutputPoints[point] += PointSelection[point] * amount * (input_points[point] - InputPoints[point]);
		}
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::document_plugin_factory<morph_points,
			k3d::interface_list<k3d::imesh_source,
			k3d::interface_list<k3d::imesh_sink > > > factory(
				k3d::uuid(0xc52c53c2, 0x3fca4d28, 0x88db7096, 0xea60f819),
				"MorphPoints",
				_("Applies morph to different targets, interpolating between target and source position"),
				"Deformation",
				k3d::iplugin_factory::EXPERIMENTAL);

		return factory;
	}

private:
	k3d::user_property_changed_signal m_user_property_changed_signal;
	
};

/////////////////////////////////////////////////////////////////////////////
// morph_points_factory

k3d::iplugin_factory& morph_points_factory()
{
	return morph_points::get_factory();
}

} // namespace deformation

} // namespace module