Code

8574bf3ff522846523dec4c3c3464c3f7f7b3b58
[inkscape.git] / src / axis-manip.h
1 /*
2  * Generic auxiliary routines for 3D axes
3  *
4  * Authors:
5  *   Maximilian Albert <Anhalter42@gmx.de>
6  *
7  * Copyright (C) 2007 authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #ifndef SEEN_AXIS_MANIP_H
13 #define SEEN_AXIS_MANIP_H
15 #include <gtk/gtk.h>
16 #include "libnr/nr-point.h"
18 namespace Box3D {
20 // The X-/Y-/Z-axis corresponds to the first/second/third digit
21 // in binary representation, respectively.
22 enum Axis {
23     X = 1,
24     Y = 2,
25     Z = 4,
26     XY = 3,
27     XZ = 5,
28     YZ = 6,
29     XYZ = 7,
30     NONE = 0
31 };
33 // We use the fourth bit in binary representation
34 // to indicate whether a face is front or rear.
35 enum FrontOrRear { // find a better name
36     FRONT = 0,
37     REAR = 8
38 };
40 extern Axis axes[3];
41 extern Axis planes[3];
42 extern FrontOrRear face_positions [2];
44 // Given a bit sequence that unambiguously specifies the face of a 3D box,
45 // return a number between 0 and 5 corresponding to that particular face
46 // (which is normally used to index an array). Return -1 if the bit sequence
47 // does not specify a face. A face can either be given by its plane (e.g, XY)
48 // or by the axis that is orthogonal to it (e.g., Z).
49 inline gint face_to_int (guint face_id) {
50     switch (face_id) {
51       case 1:  return 0;
52       case 2:  return 2;
53       case 4:  return 4;
54       case 3:  return 4;
55       case 5:  return 2;
56       case 6:  return 0;
58       case 9:  return 1;
59       case 10: return 3;
60       case 12: return 5;
61       case 11: return 5;
62       case 13: return 3;
63       case 14: return 1;
65     default: return -1;
66     }
67 }
69 inline guint opposite_face (guint face_id) {
70     return face_id + ((face_id % 2 == 0) ? 1 : -1);
71 }
73 inline bool is_single_axis_direction (Box3D::Axis dir) {
74     // tests whether dir is nonzero and a power of 2
75     return (!(dir & (dir - 1)) && dir);
76 }
78 // Warning: We don't check that axis really unamiguously specifies a plane.
79 //          Make sure this is the case when calling this function.
80 inline guint face_containing_corner (Box3D::Axis axis, guint corner) {
81     if (!is_single_axis_direction (axis)) {
82         axis = (Box3D::Axis) (axis ^ Box3D::XYZ);
83     }
84     return face_to_int (axis ^ ((corner & axis) ? Box3D::REAR : Box3D::FRONT));
85 }
88 /**
89  * Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one
90  * We don't check if 'plane' really specifies a plane (i.e., if it consists of precisely two directions).
91  */
92 inline Box3D::Axis third_axis_direction (Box3D::Axis dir1, Box3D::Axis dir2) {
93     return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
94 }
95 inline Box3D::Axis third_axis_direction (Box3D::Axis plane) {
96     return (Box3D::Axis) (plane ^ 0x7);
97 }
99 /* returns the first/second axis direction occuring in the (possibly compound) expression 'dirs' */
100 inline Box3D::Axis extract_first_axis_direction (Box3D::Axis dirs) {
101     if (dirs & Box3D::X) return Box3D::X;
102     if (dirs & Box3D::Y) return Box3D::Y;
103     if (dirs & Box3D::Z) return Box3D::Z;
104     return Box3D::NONE;
106 inline Box3D::Axis extract_second_axis_direction (Box3D::Axis dirs) {
107     return extract_first_axis_direction ((Box3D::Axis) (dirs ^ extract_first_axis_direction(dirs)));
110 inline Box3D::Axis orth_plane (Box3D::Axis axis) {
111     return (Box3D::Axis) (Box3D::XYZ ^ axis);
114 /* returns an axis direction perpendicular to the ones occuring in the (possibly compound) expression 'dirs' */
115 inline Box3D::Axis get_perpendicular_axis_direction (Box3D::Axis dirs) {
116     if (!(dirs & Box3D::X)) return Box3D::X;
117     if (!(dirs & Box3D::Y)) return Box3D::Y;
118     if (!(dirs & Box3D::Z)) return Box3D::Z;
119     return Box3D::NONE;
122 inline gchar * string_from_axes (Box3D::Axis axes) {
123     GString *pstring = g_string_new("");
124     if (axes & Box3D::X) g_string_append_printf (pstring, "X");
125     if (axes & Box3D::Y) g_string_append_printf (pstring, "Y");
126     if (axes & Box3D::Z) g_string_append_printf (pstring, "Z");
127     return pstring->str;
130 std::pair <Axis, Axis> get_remaining_axes (Axis axis);
132 } // namespace Box3D
134 #endif /* !SEEN_AXIS_MANIP_H */
136 /*
137   Local Variables:
138   mode:c++
139   c-file-style:"stroustrup"
140   c-file-offsets:((innamespace . 0)(inline-open . 0))
141   indent-tabs-mode:nil
142   fill-column:99
143   End:
144 */
145 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :