File: mesh_writer.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 (246 lines) | stat: -rw-r--r-- 8,586 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
// K-3D
// Copyright (c) 1995-2009, 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)
*/

#include <k3d-i18n-config.h>
#include <k3d-version-config.h>
#include <k3dsdk/document_plugin_factory.h>
#include <k3dsdk/iomanip.h>
#include <k3dsdk/mesh_writer.h>
#include <k3dsdk/metadata_keys.h>
#include <k3dsdk/node.h>
#include <k3dsdk/polyhedron.h>
#include <k3dsdk/string_source.h>

#include <boost/scoped_ptr.hpp>

namespace module
{

namespace graphviz
{

/////////////////////////////////////////////////////////////////////////////
// mesh_writer

class mesh_writer :
	public k3d::mesh_writer<k3d::node >,
	public k3d::string_source<mesh_writer>
{
	typedef k3d::mesh_writer<k3d::node > base;

public:
	mesh_writer(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
		base(Factory, Document)
	{
		m_input_mesh.changed_signal().connect(k3d::hint::converter<
			k3d::hint::convert<k3d::hint::any, k3d::hint::none> >(make_update_string_slot()));
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::document_plugin_factory<mesh_writer, k3d::interface_list<k3d::imesh_sink > > factory(
			k3d::uuid(0xb0fc811a, 0xd241b237, 0xd8922faf, 0x70e32542),
			"GraphVizMeshWriter",
			_("Mesh writer that saves meshes as GraphViz (.dot) files"),
			"MeshWriter",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	template<typename ContainerT>
	static const k3d::string_t dot_array_vertex(const ContainerT& Container)
	{
		std::ostringstream buffer;

		buffer << "v" << Container.get();
		buffer << " [";
		buffer << " shape=\"plaintext\"";
		buffer << " label=<";
		buffer << " <table border=\"0\" cellborder=\"1\" cellspacing=\"0\"><tr>";
		buffer << " <td";
		buffer << " port=\"array\"";
		if(Container->type_string() == "k3d::imaterial*")
			buffer << " color=\"#00dd00\"";
		else if(Container->get_metadata_value(k3d::metadata::key::role()) == k3d::metadata::value::selection_role())
			buffer << " color=\"#0000dd\"";
		else if(Container->get_metadata_value(k3d::metadata::key::domain()) == k3d::metadata::value::point_indices_domain())
			buffer << " color=\"#dd0000\"";
		buffer << ">";
		buffer << Container->type_string() << " array";
		buffer << "</td>";
		buffer << " </tr></table>";
		buffer << " >";
		buffer << "]";
		
		return buffer.str();
	}

	template<typename ContainerT>
	static const k3d::string_t dot_array_reference(const ContainerT& Container)
	{
		std::ostringstream buffer;
		buffer << "v" << Container.get() << ":array";
		return buffer.str();
	}

	static void table_vertex(const k3d::string_t& Name, const k3d::table& Table, std::ostream& Output)
	{
		Output << k3d::standard_indent << "v" << &Table;
		Output << " [";
		Output << " shape=\"plaintext\"";
		Output << " label=<";
		Output << " <table border=\"0\" cellborder=\"1\" cellspacing=\"0\">";
		Output << " <tr><td port=\"name\" bgcolor=\"#ddddff\">Table \"" << Name << "\"</td></tr>";

		for(k3d::mesh::table_t::const_iterator array = Table.begin(); array != Table.end(); ++array)
			Output << "<tr><td port=\"" << array->first << "\">" << "\"" << array->first << "\"" << "</td></tr>";
		Output << " </table>";
        	Output << " >";
		Output << "]\n";

		for(k3d::mesh::table_t::const_iterator array = Table.begin(); array != Table.end(); ++array)
		{
			Output << k3d::standard_indent << dot_array_vertex(array->second) << "\n";
			Output << k3d::standard_indent << "v" << &Table << ":" << array->first << ":e -> " << dot_array_reference(array->second) << ":w\n";
		}

		Output << k3d::pop_indent;
	}

	static const k3d::string_t table_reference(const k3d::table& Table)
	{
		std::ostringstream buffer;
		buffer << "v" << &Table << ":name:w";
		return buffer.str();
	}

	static void write_mesh(const k3d::mesh& Input, std::ostream& Output)
	{
		Output << "// Written by K-3D " << K3D_VERSION << ", http://www.k-3d.org\n\n";

		Output << k3d::standard_indent << "digraph\n";
		Output << k3d::standard_indent << "{\n";
		Output << k3d::push_indent;

		Output << k3d::standard_indent << "graph [rankdir=\"LR\" ranksep=\"1.0\"]\n";
		Output << k3d::standard_indent << "node [shape=\"record\" fontname=\"Helvetica\" fontsize=12 height=0 width=0]\n";
		Output << k3d::standard_indent << "edge [fontname=\"Helvetica\" fontsize=10]\n";

		Output << k3d::standard_indent << "v" << &Input;
		Output << " [";
		Output << " shape=\"plaintext\"";
		Output << " label=<";
		Output << " <table border=\"0\" cellborder=\"1\" cellspacing=\"0\">";
		Output << " <tr><td port=\"geometry\" bgcolor=\"#ffdddd\">Geometry</td></tr>";
		Output << " <tr><td port=\"points\">points</td></tr>";
		Output << " <tr><td port=\"point_selection\">point_selection</td></tr>";
		Output << " <tr><td port=\"point_attributes\">point_attributes</td></tr>";
		Output << " <tr><td port=\"primitives\">primitives</td></tr>";
		Output << " </table>";
		Output << ">";
		Output << "]\n";
		Output << k3d::push_indent;

		Output << k3d::standard_indent << "subgraph cluster_0\n";
		Output << k3d::standard_indent << "{\n";
		Output << k3d::push_indent;
		Output << k3d::standard_indent << "color=white;\n";

		if(Input.points)
		{
			Output << k3d::standard_indent << dot_array_vertex(Input.points) << "\n";
			Output << k3d::standard_indent << "v" << &Input << ":points:e -> " << dot_array_reference(Input.points) << ":w\n";
		}

		if(Input.point_selection)
		{
			Output << k3d::standard_indent << dot_array_vertex(Input.point_selection) << "\n";
			Output << k3d::standard_indent << "v" << &Input << ":point_selection:e -> " << dot_array_reference(Input.point_selection) << ":w\n";
		}

		Output << k3d::pop_indent;
		Output << k3d::standard_indent << "}\n";

		for(k3d::mesh::primitives_t::const_iterator primitive = Input.primitives.begin(); primitive != Input.primitives.end(); ++primitive)
		{ 
			Output << k3d::standard_indent << "v" << (*primitive).get();
			Output << " [";
			Output << " shape=\"plaintext\"";
			Output << " label=<";
			Output << " <table border=\"0\" cellborder=\"1\" cellspacing=\"0\">";
			Output << " <tr><td port=\"primitive\" bgcolor=\"#ddffdd\">Primitive \"" << (*primitive)->type << "\"</td></tr>";
			Output << " <tr><td port=\"structure\">structure</td></tr>";
			Output << " <tr><td port=\"attributes\">attributes</td></tr>";
			Output << " </table>";
			Output << ">";
			Output << "]\n";
			Output << k3d::standard_indent << "v" << &Input << ":primitives:e -> " << "v" << (*primitive).get() << ":primitive:w\n";
			Output << k3d::push_indent;

			for(k3d::mesh::named_tables_t::const_iterator named_table = (**primitive).structure.begin(); named_table != (**primitive).structure.end(); ++named_table)
			{
				table_vertex(named_table->first, named_table->second, Output);
				Output << k3d::standard_indent << "v" << (*primitive).get() << ":structure:e -> " << "v" << &named_table->second << ":name:w\n";
			}

			for(k3d::mesh::named_tables_t::const_iterator named_table = (**primitive).attributes.begin(); named_table != (**primitive).attributes.end(); ++named_table)
			{
				table_vertex(named_table->first, named_table->second, Output);
				Output << k3d::standard_indent << "v" << (*primitive).get() << ":attributes:e -> " << "v" << &named_table->second << ":name:w\n";
			}

			Output << k3d::pop_indent;
		}

		Output << k3d::pop_indent;
		Output << k3d::pop_indent;
		Output << k3d::standard_indent << "}\n";
	}

	void on_write_mesh(const k3d::mesh& Input, const k3d::filesystem::path& OutputPath, std::ostream& Output)
	{
		write_mesh(Input, Output);
	}

	void on_update_string(k3d::string_t& Output)
	{
		std::ostringstream buffer;

		if(const k3d::mesh* const mesh = m_input_mesh.pipeline_value())
			write_mesh(*mesh, buffer);

		Output = buffer.str();	
	}
};

k3d::iplugin_factory& mesh_writer_factory()
{
	return mesh_writer::get_factory();
}

} // namespace graphviz

} // namespace module