Code

Various small cleanups
[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 const double epsilon = 1e-6;
22 // The X-/Y-/Z-axis corresponds to the first/second/third digit
23 // in binary representation, respectively.
24 enum Axis {
25     X = 1,
26     Y = 2,
27     Z = 4,
28     XY = 3,
29     XZ = 5,
30     YZ = 6,
31     XYZ = 7,
32     NONE = 0
33 };
35 // We use the fourth bit in binary representation
36 // to indicate whether a face is front or rear.
37 enum FrontOrRear { // find a better name
38     FRONT = 0,
39     REAR = 8
40 };
42 extern Axis axes[3];
43 extern Axis planes[3];
44 extern FrontOrRear face_positions [2];
46 // Given a bit sequence that unambiguously specifies the face of a 3D box,
47 // return a number between 0 and 5 corresponding to that particular face
48 // (which is normally used to index an array). Return -1 if the bit sequence
49 // does not specify a face. A face can either be given by its plane (e.g, XY)
50 // or by the axis that is orthogonal to it (e.g., Z).
51 inline gint face_to_int (guint face_id) {
52     switch (face_id) {
53       case 1:  return 0;
54       case 2:  return 2;
55       case 4:  return 4;
56       case 3:  return 4;
57       case 5:  return 2;
58       case 6:  return 0;
60       case 9:  return 1;
61       case 10: return 3;
62       case 12: return 5;
63       case 11: return 5;
64       case 13: return 3;
65       case 14: return 1;
67     default: return -1;
68     }
69 }
71 inline gint opposite_face (guint face_id) {
72     return face_id + ((face_id % 2 == 0) ? 1 : -1);
73 }
75 inline guint number_of_axis_directions (Box3D::Axis axis) {
76     guint num = 0;
77     if (axis & Box3D::X) num++;
78     if (axis & Box3D::Y) num++;
79     if (axis & Box3D::Z) num++;
81     return num;
82 }
84 inline bool is_plane (Box3D::Axis plane) {
85     return (number_of_axis_directions (plane) == 2);
86 }
88 inline bool is_single_axis_direction (Box3D::Axis dir) {
89     // tests whether dir is nonzero and a power of 2
90     return (!(dir & (dir - 1)) && dir);
91 }
93 // Warning: We don't check that axis really unambiguously specifies a plane.
94 //          Make sure this is the case when calling this function.
95 inline gint face_containing_corner (Box3D::Axis axis, guint corner) {
96     if (!is_single_axis_direction (axis)) {
97         axis = (Box3D::Axis) (axis ^ Box3D::XYZ);
98     }
99     return face_to_int (axis ^ ((corner & axis) ? Box3D::REAR : Box3D::FRONT));
103 /**
104  * Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one
105  * We don't check if 'plane' really specifies a plane (i.e., if it consists of precisely two directions).
106  */
107 inline Box3D::Axis third_axis_direction (Box3D::Axis dir1, Box3D::Axis dir2) {
108     return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
110 inline Box3D::Axis third_axis_direction (Box3D::Axis plane) {
111     return (Box3D::Axis) (plane ^ 0x7);
114 /* returns the first/second axis direction occuring in the (possibly compound) expression 'dirs' */
115 inline Box3D::Axis extract_first_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;
121 inline Box3D::Axis extract_second_axis_direction (Box3D::Axis dirs) {
122     return extract_first_axis_direction ((Box3D::Axis) (dirs ^ extract_first_axis_direction(dirs)));
125 inline Box3D::Axis orth_plane_or_axis (Box3D::Axis axis) {
126     return (Box3D::Axis) (Box3D::XYZ ^ axis);
129 /* returns an axis direction perpendicular to the ones occuring in the (possibly compound) expression 'dirs' */
130 inline Box3D::Axis get_perpendicular_axis_direction (Box3D::Axis dirs) {
131     if (!(dirs & Box3D::X)) return Box3D::X;
132     if (!(dirs & Box3D::Y)) return Box3D::Y;
133     if (!(dirs & Box3D::Z)) return Box3D::Z;
134     return Box3D::NONE;
137 inline gchar * string_from_axes (Box3D::Axis axes) {
138     GString *pstring = g_string_new("");
139     if (axes & Box3D::X) g_string_append_printf (pstring, "X");
140     if (axes & Box3D::Y) g_string_append_printf (pstring, "Y");
141     if (axes & Box3D::Z) g_string_append_printf (pstring, "Z");
142     return pstring->str;
145 std::pair <Axis, Axis> get_remaining_axes (Axis axis);
147 } // namespace Box3D
149 #endif /* !SEEN_AXIS_MANIP_H */
151 /*
152   Local Variables:
153   mode:c++
154   c-file-style:"stroustrup"
155   c-file-offsets:((innamespace . 0)(inline-open . 0))
156   indent-tabs-mode:nil
157   fill-column:99
158   End:
159 */
160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :