File: document_exporter.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 (203 lines) | stat: -rw-r--r-- 7,289 bytes parent folder | download | duplicates (3)
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
// K-3D
// Copyright (c) 1995-2006, 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 Carlos Andres Dominguez Caballero (carlosadc@gmail.com)
*/

#include <dae.h>
#include <dae/daeUtils.h>
#include <dom/domCOLLADA.h>
#include <dom/domLight.h>
#include <dom/domCamera.h>

#include <k3d-i18n-config.h>
#include <k3d-version-config.h>
#include <k3dsdk/application_plugin_factory.h>
#include <k3dsdk/classes.h>
#include <k3dsdk/dependencies.h>
#include <k3dsdk/fstream.h>
#include <k3dsdk/idocument.h>
#include <k3dsdk/idocument_exporter.h>
#include <k3dsdk/inode.h>
#include <k3dsdk/inode_collection.h>
#include <k3dsdk/string_modifiers.h>
#include <k3dsdk/xml.h>
#include <k3dsdk/iproperty.h>
#include <k3dsdk/property.h>
#include <k3dsdk/data.h>
#include <k3dsdk/algebra.h>
#include <k3dsdk/plugin.h>
#include <k3dsdk/mesh.h>
#include <k3dsdk/transformable.h>

#include <string>
#include <list>

#include "collada.h"

namespace module
{
namespace collada
{
namespace io
{

template <class T>
std::string to_string(T & t)
{
        std::stringstream s;
        s << t;
        return s.str();
}

class document_exporter :
	public k3d::idocument_exporter
{
public:
	bool write_file(k3d::idocument& Document, const k3d::filesystem::path& Path)
	{
		k3d::log() << info << "Writing " << Path.native_console_string() << " using " << get_factory().name() << std::endl;
		DAE dae;
		daeElement *root = dae.add(k3d::string_cast<k3d::filesystem::path>(Path));

		daeElement *asset = root->add("asset");
		daeElement *library_cameras = root->add("library_cameras");
		daeElement *library_lights = root->add("library_lights");
		daeElement *library_geometries = root->add("library_geometries");

		daeElement* contributor = asset->add("contributor");
		daeElement* created = asset->add("created");
		daeElement* modified = asset->add("modified");

		std::stringstream author;
		author << "K-3D " << K3D_PACKAGE << " , " << K3D_VERSION << " , " << K3D_HOST;
		contributor->setCharData(author.str().c_str());
		const char* date = "2008-04-08T13:07:52-08:00";
		created->setCharData(date);
		modified->setCharData(date);

		// Sort objects by ID before saving them ...
		const k3d::inode_collection::nodes_t nodes = Document.nodes().collection();

		for(k3d::inode_collection::nodes_t::const_iterator node = nodes.begin(); node != nodes.end(); ++node)
		{
			// Add cameras into file
			if((*node)->factory().factory_id() == k3d::plugin::factory::lookup("Camera")->factory_id())
			{
				domCamera *camera = daeSafeCast<domCamera>(library_cameras->add("camera"));
				domCamera::domOptics::domTechnique_common *technique = daeSafeCast<domCamera::domOptics::domTechnique_common>(camera->add("optics technique_common"));
				camera->setName((*node)->name().c_str());
				camera->setId((*node)->name().c_str());

				double zfar = boost::any_cast<double>(k3d::property::get(**node, "far")->property_internal_value());
				double znear = boost::any_cast<double>(k3d::property::get(**node, "near")->property_internal_value());
				double left = boost::any_cast<double>(k3d::property::get(**node, "left")->property_internal_value());
				double right = boost::any_cast<double>(k3d::property::get(**node, "right")->property_internal_value());
				double top = boost::any_cast<double>(k3d::property::get(**node, "top")->property_internal_value());
				double bottom = boost::any_cast<double>(k3d::property::get(**node, "bottom")->property_internal_value());
				double aspect_ratio = (right-left)/(top-bottom);
				

				bool ortho = boost::any_cast<bool>(k3d::property::get(**node, "orthographic")->property_internal_value());

				// attributes differ with camera type, some attributes had to be computed manually 
				// as K-3D use them in a different way
				domElement *cam_type;
				if(ortho)
				{
					cam_type = technique->add("orthographic");
					double xmag = (right-left)/2;
					domElement *xm = cam_type->add("xmag");
					xm->setCharData(to_string(xmag).c_str());
					double ymag = (top-bottom)/2;
				}
				else
				{
					cam_type = technique->add("perspective");
					double yfov = k3d::degrees(2*atan((top-bottom)/(2*znear)));
					domElement *yf = cam_type->add("yfov");
					yf->setCharData(to_string(yfov).c_str());
				}

				// Add common attributes
				domElement *near = cam_type->add("znear");
				near->setCharData(to_string(znear).c_str());
				domElement *far = cam_type->add("zfar");
				far->setCharData(to_string(zfar).c_str());
				domElement *aspect = cam_type->add("aspect_ratio");
				aspect->setCharData(to_string(aspect_ratio).c_str());
			}

			// Add geometry to the collada file, this will take the Frozen mesh
			// and store it into the library_geometries
			k3d::iproperty *mesh_property = boost::any_cast<k3d::iproperty*>(k3d::property::get(**node, "output_mesh"));
			if(mesh_property != NULL && (*node)->factory().factory_id() != k3d::plugin::factory::lookup("MeshInstance")->factory_id())
			{
				k3d::mesh *mesh = boost::any_cast<k3d::mesh*>(mesh_property->property_internal_value());
				addGeometry(library_geometries, (*node)->name(), mesh);
			}
		}

		//Setup visual scenes library and the main scene
		SafeAdd(root, "library_visual_scenes", visualSceneLib);
		SafeAdd(visualSceneLib, "visual_scene", visualScene);
		visualScene->setAttribute("id", "mainScene");


		for(k3d::inode_collection::nodes_t::const_iterator node = nodes.begin(); node != nodes.end(); ++node)
		{
			// Add MeshInstance to instanciate the input_mesh in
			// the visual scene with it's corresponding transformation
			if((*node)->factory().factory_id() == k3d::plugin::factory::lookup("MeshInstance")->factory_id())
				addMeshInstance(visualScene,(*node));

			// Add Camera to instanciate the camera in
			// the visual scene with it's corresponding transformation
			if((*node)->factory().factory_id() == k3d::plugin::factory::lookup("Camera")->factory_id())
				addCameraInstance(visualScene,(*node));
		}

		// Add the main scene to root file and instanciate the created visual_scene
		root->add("scene instance_visual_scene")->setAttribute("url", makeUriRef("mainScene").c_str());

		dae.writeAll();
		return true;
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::application_plugin_factory<document_exporter, k3d::interface_list<k3d::idocument_exporter> > factory(
			k3d::uuid(0x33f1ba2c, 0x5b41392d, 0xe9dc9d99, 0x66a8129d),
			"COLLADADocumentExporter",
			_("COLLADA ( .dae )"),
			"");

		return factory;
	}
};

k3d::iplugin_factory& document_exporter_factory()
{
	return document_exporter::get_factory();
}

}
}
}