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 bool is_single_axis_direction (Box3D::Axis dir) {
70 // tests whether dir is nonzero and a power of 2
71 return (!(dir & (dir - 1)) && dir);
72 }
74 /**
75 * Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one
76 * We don't check if 'plane' really specifies a plane (i.e., if it consists of precisely two directions).
77 */
78 inline Box3D::Axis third_axis_direction (Box3D::Axis dir1, Box3D::Axis dir2) {
79 return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
80 }
81 inline Box3D::Axis third_axis_direction (Box3D::Axis plane) {
82 return (Box3D::Axis) (plane ^ 0x7);
83 }
85 /* returns the first/second axis direction occuring in the (possibly compound) expression 'dirs' */
86 inline Box3D::Axis extract_first_axis_direction (Box3D::Axis dirs) {
87 if (dirs & Box3D::X) return Box3D::X;
88 if (dirs & Box3D::Y) return Box3D::Y;
89 if (dirs & Box3D::Z) return Box3D::Z;
90 return Box3D::NONE;
91 }
92 inline Box3D::Axis extract_second_axis_direction (Box3D::Axis dirs) {
93 return extract_first_axis_direction ((Box3D::Axis) (dirs ^ extract_first_axis_direction(dirs)));
94 }
96 inline Box3D::Axis orth_plane (Box3D::Axis axis) {
97 return (Box3D::Axis) (Box3D::XYZ ^ axis);
98 }
100 /* returns an axis direction perpendicular to the ones occuring in the (possibly compound) expression 'dirs' */
101 inline Box3D::Axis get_perpendicular_axis_direction (Box3D::Axis dirs) {
102 if (!(dirs & Box3D::X)) return Box3D::X;
103 if (!(dirs & Box3D::Y)) return Box3D::Y;
104 if (!(dirs & Box3D::Z)) return Box3D::Z;
105 return Box3D::NONE;
106 }
108 inline gchar * string_from_axes (Box3D::Axis axes) {
109 GString *pstring = g_string_new("");
110 if (axes & Box3D::X) g_string_append_printf (pstring, "X");
111 if (axes & Box3D::Y) g_string_append_printf (pstring, "Y");
112 if (axes & Box3D::Z) g_string_append_printf (pstring, "Z");
113 return pstring->str;
114 }
116 } // namespace Box3D
118 #endif /* !SEEN_AXIS_MANIP_H */
120 /*
121 Local Variables:
122 mode:c++
123 c-file-style:"stroustrup"
124 c-file-offsets:((innamespace . 0)(inline-open . 0))
125 indent-tabs-mode:nil
126 fill-column:99
127 End:
128 */
129 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :