File: gtswriter.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 (328 lines) | stat: -rw-r--r-- 9,809 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
// 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 Implements the GTSWriter K-3D object, which exports GTS *.gts files
		\author Tom Browder (tbrowder@home.com)
		\author Romain Behar (romainbehar@yahoo.com)
*/

#include <k3dsdk/classes.h>
#include <k3dsdk/iapplication.h>
#include <k3dsdk/ideletable.h>
#include <k3dsdk/idocument.h>
#include <k3dsdk/ifile_format.h>
#include <k3dsdk/igeometry_write_format.h>
#include <k3dsdk/imaterial.h>
#include <k3dsdk/imaterial_collection.h>
#include <k3dsdk/imesh_source.h>
#include <k3dsdk/iobject.h>
#include <k3dsdk/iobject_collection.h>
#include <k3dsdk/mesh.h>
#include <k3dsdk/module.h>
#include <k3dsdk/objects.h>
#include <k3dsdk/property.h>
#include <k3dsdk/result.h>
#include <k3dsdk/string_modifiers.h>
#include <k3dsdk/user_interface.h>
#include <k3dsdk/utility.h>
#include <k3dsdk/vectors.h>

#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/path.hpp>

#include <map>
#include <set>

namespace libk3dgeometry
{

/// Test to see if a mesh is all triangles
bool triangle_test(k3d::mesh* const Mesh)
{
	k3d::mesh::polyhedra_t::iterator polyhedron;
	for(polyhedron = Mesh->polyhedra.begin(); polyhedron != Mesh->polyhedra.end(); polyhedron++)
		{
			k3d::polyhedron::faces_t::iterator face;
			for(face = (*polyhedron)->faces.begin(); face != (*polyhedron)->faces.end(); face++)
				{
					k3d::split_edge* first = (*face)->first_edge;
					// Skip empty faces
					if(!first)
						continue;

					k3d::split_edge* current_edge = first->face_clockwise;
					// Skip one-edged faces
					if(!current_edge)
						continue;

					unsigned long edge_number = 1;

					while(current_edge != first)
						{
							edge_number++;
							current_edge = current_edge->face_clockwise;
						}

					if(edge_number != 3)
						return false;
				}
		}

	return true;
}

/// std::pair equivalent that maintains the order of its members
template<typename T1, typename T2>
class ordered_pair;

template<typename T1, typename T2>
bool operator<(const ordered_pair<T1,T2>& lhs, const ordered_pair<T1,T2>& rhs);

template<typename T1, typename T2>
class ordered_pair :
	public std::pair<T1, T2>
{
public:
	typedef T1 first_type;
	typedef T2 second_type;

	T1 first;
	T2 second;

	explicit ordered_pair()
	{
	}

	explicit ordered_pair(const T1& First, const T2& Second) :
		first(First < Second ? First : Second),
		second(First < Second ? Second : First)
	{
	}

	explicit ordered_pair(const k3d::split_edge* Edge) :
		first(Edge->vertex < Edge->face_clockwise->vertex ? Edge->vertex : Edge->face_clockwise->vertex),
		second(Edge->vertex < Edge->face_clockwise->vertex ? Edge->face_clockwise->vertex : Edge->vertex)
	{
	}

	friend bool operator< <>(const ordered_pair& lhs, const ordered_pair& rhs);
};

template<typename T1, typename T2>
bool operator<(const ordered_pair<T1,T2>& lhs, const ordered_pair<T1,T2>& rhs)
{
	if(lhs.first != rhs.first)
		return lhs.first < rhs.first;

	return lhs.second < rhs.second;
}

typedef std::vector<k3d::point*> PointCollection;
typedef std::map<k3d::point*, unsigned long> PointMap;
typedef ordered_pair<k3d::point*, k3d::point*> Edge;
typedef std::set<Edge> EdgeCollection;
typedef std::map<Edge, unsigned long> EdgeMap;
typedef std::vector<Edge> Triangle;
typedef std::vector<Triangle> TriangleCollection;

class WriteMesh
{
public:
	WriteMesh(k3d::idocument& Document, std::ostream& Stream) :
		m_Document(Document),
		m_Stream(Stream)
	{
	}

	void operator()(k3d::iobject* const Object)
	{
		k3d::imesh_source* const mesh_source = dynamic_cast<k3d::imesh_source*>(Object);
		return_if_fail(mesh_source);

		k3d::mesh* const Mesh = boost::any_cast<k3d::mesh*>(k3d::get_property_value(m_Document.dag(), mesh_source->mesh_source_output()));
		return_if_fail(Mesh);

		// Process only if object is triangulated ...
		if(!triangle_test(Mesh))
			return;

		// Get the collection of points ...
		k3d::mesh::points_t points;
		std::copy(Mesh->points.begin(), Mesh->points.end(), std::back_inserter(points));

		// Create a mapping of points-to-one-based-indices ...
		PointMap pointmap;
		for(PointCollection::iterator point = points.begin(); point != points.end(); ++point)
			pointmap[*point] = pointmap.size();

		// Build the collection of edges ...
		EdgeCollection edges;
		for(k3d::mesh::polyhedra_t::const_iterator polyhedron = Mesh->polyhedra.begin(); polyhedron != Mesh->polyhedra.end(); polyhedron++)
			for(k3d::polyhedron::edges_t::const_iterator edge = (*polyhedron)->edges.begin(); edge != (*polyhedron)->edges.end(); edge++)
				edges.insert(Edge(*edge));

		// Create a mapping of edges-to-one-based-indices ...
		EdgeMap edgemap;
		for(EdgeCollection::iterator edge = edges.begin(); edge != edges.end(); ++edge)
			edgemap[*edge] = edgemap.size();

		// Build triangle collection ...
		TriangleCollection triangle_collection;
		for(k3d::mesh::polyhedra_t::const_iterator polyhedron = Mesh->polyhedra.begin(); polyhedron != Mesh->polyhedra.end(); polyhedron++)
			for(k3d::polyhedron::faces_t::const_iterator face = (*polyhedron)->faces.begin(); face != (*polyhedron)->faces.end(); face++)
			{
				Triangle triangle;

				k3d::split_edge* first = (*face)->first_edge;
				// Skip empty faces
				if(!first)
					continue;

				triangle.push_back(Edge(first));

				k3d::split_edge* current_edge = first->face_clockwise;
				// Skip one-edged faces
				if(!current_edge)
					continue;

				triangle.push_back(Edge(current_edge));

				current_edge = current_edge->face_clockwise;
				if(current_edge)
					if(current_edge->face_clockwise == first)
						{
							triangle.push_back(Edge(current_edge));
							triangle_collection.push_back(triangle);
						}
			}

		// Write some comments
		m_Stream << "# object: " << Object->name() << std::endl;
		m_Stream << "# num points [" << points.size() << "] num edges [" << edges.size() << "] num triangles [" << triangle_collection.size() << "]" << std::endl;

		// Data header
		m_Stream << points.size() << " " << edges.size() << " " << triangle_collection.size() << std::endl;

		// Write points
		m_Stream << "# points" << std::endl;
		for(PointCollection::iterator point = points.begin(); point != points.end(); ++point)
			{
				// Transform coords back to GTS convention
				const k3d::vector3 coords = (*point)->position;
				m_Stream << -coords[0] << " " << -coords[2] << " " << coords[1] << std::endl;
			}

		// Write edges
		m_Stream << "# edges" << std::endl;
		for(EdgeCollection::iterator edge = edges.begin(); edge != edges.end(); ++edge)
			m_Stream << pointmap[edge->first] << " " << pointmap[edge->second] << std::endl;

		// Write triangles
		m_Stream << "# triangles" << std::endl;
		for(TriangleCollection::iterator triangle = triangle_collection.begin(); triangle != triangle_collection.end(); ++triangle)
			{
				for(Triangle::iterator edge = triangle->begin(); edge != triangle->end(); ++edge)
					m_Stream << edgemap[*edge] << " ";
				m_Stream << std::endl;
			}
	}

private:
	k3d::idocument& m_Document;
	std::ostream& m_Stream;
};

/////////////////////////////////////////////////////////////////////////////
// gts_writer_implementation

class gts_writer_implementation :
	public k3d::ifile_format,
	public k3d::igeometry_write_format,
	public k3d::ideletable
{
public:
	unsigned long priority()
	{
		return 0;
	}

	bool query_can_handle(const boost::filesystem::path& FilePath)
	{
		return "gts" == k3d::file_extension(FilePath);
	}

	bool pre_write(k3d::idocument& Document, const boost::filesystem::path& FilePath)
	{
		return true;
	}

	bool write_options(k3d::idocument& Document, const boost::filesystem::path& FilePath)
	{
		return true;
	}

	bool write_file(k3d::idocument& Document, const boost::filesystem::path& FilePath);

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

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<k3d::application_plugin<gts_writer_implementation>, k3d::interface_list<k3d::igeometry_write_format> > factory(
			k3d::uuid(0xc6bdb531, 0x17a74c0a, 0x99db8c94, 0x38195da7),
			"GTSWriter",
			"GNU Triangulated Surface ( .gts )",
			"");

		return factory;
	}
};

bool gts_writer_implementation::write_file(k3d::idocument& Document, const boost::filesystem::path& FilePath)
{
	// (GTS format is not a real one : it has been designed to store surfaces
	// as sets of points, edges and triangles, only;
	// each surface item can have optional arbitrary values, but no other
	// scene information can be found in a native GTS file)

	// Try to open the output file ...
	boost::filesystem::ofstream file(FilePath);
	return_val_if_fail(file.good(), false);
	file << "# Written by K-3D" << std::endl;

	// Get the set of available meshes ...
	k3d::objects_t meshes(k3d::find_objects(Document.objects(), k3d::classes::MeshInstance()));

	// Write meshes to a file ...
	std::for_each(meshes.begin(), meshes.end(), WriteMesh(Document, file));

	return true;
}

k3d::iplugin_factory& gts_writer_factory()
{
	return gts_writer_implementation::get_factory();
}

} // namespace libk3dgeometry