File: axes.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 (319 lines) | stat: -rw-r--r-- 12,518 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
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
// 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)
*/

#include <k3d-i18n-config.h>
#include <k3dsdk/basic_math.h>
#include <k3dsdk/classes.h>
#include <k3dsdk/document_plugin_factory.h>
#include <k3dsdk/ibounded.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/node.h>
#include <k3dsdk/options.h>
#include <k3dsdk/renderable_gl.h>
#include <k3dsdk/share.h>
#include <k3dsdk/snap_target.h>
#include <k3dsdk/snappable.h>
#include <k3dsdk/transform.h>
#include <k3dsdk/transformable.h>
#include <k3dsdk/vectors.h>

#include <FTGL/ftgl.h>

#include <boost/scoped_ptr.hpp>

namespace module
{

namespace core
{

/////////////////////////////////////////////////////////////////////////////
// axes

/// Provides a configurable set of axes to assist users in visualizing the 3D workspace
class axes :
	public k3d::snappable<k3d::gl::renderable<k3d::transformable<k3d::node > > >,
	public k3d::ibounded
{
	typedef k3d::snappable<k3d::gl::renderable<k3d::transformable<k3d::node > > > base;

public:
	axes(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
		base(Factory, Document),
		m_axes(init_owner(*this) + init_name("axes") + init_label(_("Axes")) + init_description(_("Display XYZ axes")) + init_value(true)),
		m_xy_plane(init_owner(*this) + init_name("xyplane") + init_label(_("XY Plane")) + init_description(_("Display XY plane as a grid")) + init_value(true)),
		m_yz_plane(init_owner(*this) + init_name("yzplane") + init_label(_("YZ Plane")) + init_description(_("Display YZ plane as a grid")) + init_value(false)),
		m_xz_plane(init_owner(*this) + init_name("xzplane") + init_label(_("XZ Plane")) + init_description(_("Display XZ plane as a grid")) + init_value(false)),
		m_grid_size(init_owner(*this) + init_name("gridsize") + init_label(_("Grid Size")) + init_description(_("The size of each grid square")) + init_value(2.0) + init_step_increment(0.1) + init_units(typeid(k3d::measurement::distance))),
		m_grid_count(init_owner(*this) + init_name("gridcount") + init_label(_("Grid Count")) + init_description(_("Number of squares along each grid")) + init_value(5) + init_step_increment(1.0) + init_units(typeid(k3d::measurement::scalar)) + init_constraint(constraint::minimum<k3d::int32_t>(1))),
		m_x_color(init_owner(*this) + init_name("xcolor") + init_label(_("X Color")) + init_description(_("X axis color")) + init_value(k3d::color(1, 0, 0))),
		m_y_color(init_owner(*this) + init_name("ycolor") + init_label(_("Y Color")) + init_description(_("Y axis color")) + init_value(k3d::color(0, 0.7, 0))),
		m_z_color(init_owner(*this) + init_name("zcolor") + init_label(_("Z Color")) + init_description(_("Z axis color")) + init_value(k3d::color(0, 0, 0.7))),
		m_grid_color(init_owner(*this) + init_name("gridcolor") + init_label(_("Grid Color")) + init_description(_("Grid color")) + init_value(k3d::color(0.4, 0.4, 0.4))),
		m_font_path(init_owner(*this) + init_name("font") + init_label(_("Font")) + init_description(_("Font path")) + init_value(k3d::share_path() / k3d::filesystem::generic_path("fonts/Vera.ttf")) + init_path_mode(k3d::ipath_property::READ) + init_path_type(k3d::options::path::fonts())),
		m_font_size(init_owner(*this) + init_name("font_size") + init_label(_("Font Size")) + init_description(_("Font size.")) + init_value(12.0))
	{
		m_axes.changed_signal().connect(make_async_redraw_slot());
		m_xy_plane.changed_signal().connect(make_async_redraw_slot());
		m_yz_plane.changed_signal().connect(make_async_redraw_slot());
		m_xz_plane.changed_signal().connect(make_async_redraw_slot());
		m_grid_size.changed_signal().connect(make_async_redraw_slot());
		m_grid_count.changed_signal().connect(make_async_redraw_slot());
		m_x_color.changed_signal().connect(make_async_redraw_slot());
		m_y_color.changed_signal().connect(make_async_redraw_slot());
		m_z_color.changed_signal().connect(make_async_redraw_slot());
		m_grid_color.changed_signal().connect(make_async_redraw_slot());
		m_input_matrix.changed_signal().connect(make_async_redraw_slot());

		m_font_path.changed_signal().connect(sigc::mem_fun(this, &axes::on_font_changed));
		m_font_size.changed_signal().connect(sigc::mem_fun(this, &axes::on_font_changed));

		add_snap_target(new k3d::snap_target(_("Grid"), sigc::mem_fun(*this, &axes::grid_target_position), sigc::mem_fun(*this, &axes::grid_target_orientation)));
	}

	void on_font_changed(k3d::ihint*)
	{
		m_font.reset();
		async_redraw(0);
	}

	bool grid_target_position(const k3d::point3& Position, k3d::point3& TargetPosition)
	{
		const double grid_size = m_grid_size.pipeline_value();

		TargetPosition = k3d::point3(
			k3d::round(Position[0] / grid_size) * grid_size,
			k3d::round(Position[1] / grid_size) * grid_size,
			k3d::round(Position[2] / grid_size) * grid_size);

		return true;
	}

	bool grid_target_orientation(const k3d::point3& Position, k3d::vector3& Look, k3d::vector3& Up)
	{
		return false;
	}

	const k3d::bounding_box3 extents()
	{
		const double size = m_grid_size.pipeline_value() * m_grid_count.pipeline_value();
		return k3d::bounding_box3(size, -size, size, -size, size, -size);
	}

	k3d::uint_t gl_layer()
	{
		return 2048;
	}

	void on_gl_draw(const k3d::gl::render_state& State)
	{
		const long grid_count = m_grid_count.pipeline_value();
		const k3d::double_t grid_size = m_grid_size.pipeline_value();
		const k3d::color x_color = m_x_color.pipeline_value();
		const k3d::color y_color = m_y_color.pipeline_value();
		const k3d::color z_color = m_z_color.pipeline_value();
		const k3d::color grid_color = m_grid_color.pipeline_value();

		const k3d::double_t size = grid_count * grid_size;

		k3d::gl::store_attributes attributes;

		glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_1D);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_BLEND);

		glLineWidth(1.0f);
		glDisable(GL_LINE_STIPPLE);

		// Draw axes and labels
		if(m_axes.pipeline_value())
		{
			// Draw X axis
			k3d::gl::color3d(x_color);
			glBegin(GL_LINE_LOOP);
			glVertex3d(-size, 0.0, 0.0);
			glVertex3d(size, 0.0, 0.0);
			glEnd();

			// Draw Y axis
			k3d::gl::color3d(y_color);
			glBegin(GL_LINE_LOOP);
			glVertex3d(0.0, -size, 0.0);
			glVertex3d(0.0, size, 0.0);
			glEnd();

			// Draw Z axis
			k3d::gl::color3d(z_color);
			glBegin(GL_LINE_LOOP);
			glVertex3d(0.0, 0.0, -size);
			glVertex3d(0.0, 0.0, size);
			glEnd();
		}

		// Setup grid color
		k3d::gl::color3d(grid_color);

		// Draw XY plane
		if(m_xy_plane.pipeline_value())
		{
			glBegin(GL_LINES);
			for(long i = -grid_count; i <= grid_count; ++i)
			{
				glVertex3d(i * grid_size, -size, 0.0);
				glVertex3d(i * grid_size, size, 0.0);
				glVertex3d(-size, i * grid_size, 0.0);
				glVertex3d(size, i * grid_size, 0.0);
			}
			glEnd();
		}

		// Draw YZ plane
		if(m_yz_plane.pipeline_value())
		{
			glBegin(GL_LINES);
			for(long i = -grid_count; i <= grid_count; ++i)
			{
				glVertex3d(0.0, i * grid_size, -size);
				glVertex3d(0.0, i * grid_size, size);
				glVertex3d(0.0, -size, i * grid_size);
				glVertex3d(0.0, size, i * grid_size);
			}
			glEnd();
		}

		// Draw XZ plane
		if(m_xz_plane.pipeline_value())
		{
			glBegin(GL_LINES);
			for(long i = -grid_count; i <= grid_count; ++i)
			{
				glVertex3d(i * grid_size, 0.0, -size);
				glVertex3d(i * grid_size, 0.0, size);
				glVertex3d(-size, 0.0, i * grid_size);
				glVertex3d(size, 0.0, i * grid_size);
			}
			glEnd();
		}

		if(m_axes.pipeline_value())
		{
			// Draw axis labels ...
			k3d::gl::color3d(grid_color);

			k3d::double_t labelposition = size * 1.1;

			if(!m_font)
			{
				k3d::filesystem::path font_path = m_font_path.pipeline_value();
				if(!k3d::filesystem::exists(font_path))
				{
					k3d::log() << error << "axis: error loading font " << font_path.native_filesystem_string().c_str() << std::endl;
					return;
				}

				m_font.reset(new FTPixmapFont(font_path.native_filesystem_string().c_str()));
				m_font->FaceSize(static_cast<unsigned int>(m_font_size.pipeline_value()));
				m_font->UseDisplayList(true);
				if(m_font->Error())
					k3d::log() << error << "error initializing font" << std::endl;
			}

			glRasterPos3d(labelposition, 0, 0);
			m_font->Render("+X");
			glRasterPos3d(0, labelposition, 0);
			m_font->Render("+Y");
			glRasterPos3d(0, 0, labelposition);
			m_font->Render("+Z");

			glRasterPos3d(-labelposition, 0, 0);
			m_font->Render("-X");
			glRasterPos3d(0, -labelposition, 0);
			m_font->Render("-Y");
			glRasterPos3d(0, 0, -labelposition);
			m_font->Render("-Z");
		}
	}

	void on_gl_select(const k3d::gl::render_state& State, const k3d::gl::selection_state& SelectState)
	{
	}

	void snap(const k3d::point3& InputCoordinates, k3d::point3& SnapCoordinates, std::string& SnapDescription)
	{
		// Convert coordinates to our frame ...
		k3d::point3 input_coordinates = k3d::inverse(k3d::node_to_world_matrix(*this)) * InputCoordinates;

		// Snap coordinates ...
		input_coordinates[0] = k3d::round(input_coordinates[0] / m_grid_size.pipeline_value()) * m_grid_size.pipeline_value();
		input_coordinates[1] = k3d::round(input_coordinates[1] / m_grid_size.pipeline_value()) * m_grid_size.pipeline_value();
		input_coordinates[2] = k3d::round(input_coordinates[2] / m_grid_size.pipeline_value()) * m_grid_size.pipeline_value();

		// Convert coordinates back to world frame ...
		SnapCoordinates = k3d::node_to_world_matrix(*this) * input_coordinates;
		SnapDescription = "Axes Grid";
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::document_plugin_factory<axes,
			k3d::interface_list<k3d::imatrix_source,
			k3d::interface_list<k3d::imatrix_sink > > >factory(
			k3d::classes::Axes(),
			"Axes",
			_("Configurable set of axes to help in visualizing the 3D workspace"),
			"Annotation",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data(bool, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_axes;
	k3d_data(bool, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_xy_plane;
	k3d_data(bool, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_yz_plane;
	k3d_data(bool, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_xz_plane;
	k3d_data(double, immutable_name, change_signal, with_undo, local_storage, no_constraint, measurement_property, with_serialization) m_grid_size;
	k3d_data(k3d::int32_t, immutable_name, change_signal, with_undo, local_storage, with_constraint, measurement_property, with_serialization) m_grid_count;
	k3d_data(k3d::color, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_x_color;
	k3d_data(k3d::color, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_y_color;
	k3d_data(k3d::color, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_z_color;
	k3d_data(k3d::color, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_grid_color;
	k3d_data(k3d::filesystem::path, immutable_name, change_signal, with_undo, local_storage, no_constraint, path_property, path_serialization) m_font_path;
	k3d_data(k3d::double_t, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_font_size;

	boost::scoped_ptr<FTFont> m_font;
};

/////////////////////////////////////////////////////////////////////////////
// axes_factory

k3d::iplugin_factory& axes_factory()
{
	return axes::get_factory();
}

} // namespace core

} // namespace module