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 Proj {
20 enum VPState {
21 VP_FINITE = 0,
22 VP_INFINITE
23 };
25 // The X-/Y-/Z-axis corresponds to the first/second/third digit
26 // in binary representation, respectively.
27 enum Axis {
28 X = 0,
29 Y = 1,
30 Z = 2,
31 W = 3,
32 NONE
33 };
35 extern Axis axes[4];
37 inline gchar const *
38 string_from_axis(Proj::Axis axis) {
39 switch (axis) {
40 case X: return "X"; break;
41 case Y: return "Y"; break;
42 case Z: return "Z"; break;
43 case W: return "W"; break;
44 case NONE: return "NONE"; break;
45 }
46 return "";
47 }
49 } // namespace Proj
51 namespace Box3D {
53 const double epsilon = 1e-6;
55 // The X-/Y-/Z-axis corresponds to the first/second/third digit
56 // in binary representation, respectively.
57 enum Axis {
58 X = 1,
59 Y = 2,
60 Z = 4,
61 XY = 3,
62 XZ = 5,
63 YZ = 6,
64 XYZ = 7,
65 NONE = 0
66 };
68 // We use the fourth bit in binary representation
69 // to indicate whether a face is front or rear.
70 enum FrontOrRear { // find a better name
71 FRONT = 0,
72 REAR = 8
73 };
75 // converts X, Y, Z respectively to 0, 1, 2 (for use as array indices, e.g)
76 inline int axis_to_int(Box3D::Axis axis) {
77 switch (axis) {
78 case Box3D::X:
79 return 0;
80 break;
81 case Box3D::Y:
82 return 1;
83 break;
84 case Box3D::Z:
85 return 2;
86 break;
87 case Box3D::NONE:
88 return -1;
89 break;
90 default:
91 g_assert_not_reached();
92 }
93 }
95 inline Proj::Axis toProj(Box3D::Axis axis) {
96 switch (axis) {
97 case Box3D::X:
98 return Proj::X;
99 case Box3D::Y:
100 return Proj::Y;
101 case Box3D::Z:
102 return Proj::Z;
103 case Box3D::NONE:
104 return Proj::NONE;
105 default:
106 g_assert_not_reached();
107 }
108 }
110 extern Axis axes[3];
111 extern Axis planes[3];
112 extern FrontOrRear face_positions [2];
114 } // namespace Box3D
116 namespace Proj {
118 inline Box3D::Axis toAffine(Proj::Axis axis) {
119 switch (axis) {
120 case Proj::X:
121 return Box3D::X;
122 case Proj::Y:
123 return Box3D::Y;
124 case Proj::Z:
125 return Box3D::Z;
126 case Proj::NONE:
127 return Box3D::NONE;
128 default:
129 g_assert_not_reached();
130 }
131 }
133 } // namespace Proj
135 namespace Box3D {
137 /*
138 * Identify the axes X, Y, Z with the numbers 0, 1, 2.
139 * A box's face is identified by the axis perpendicular to it.
140 * For a rear face, add 3.
141 */
142 // Given a bit sequence that unambiguously specifies the face of a 3D box,
143 // return a number between 0 and 5 corresponding to that particular face
144 // (which is normally used to index an array). Return -1 if the bit sequence
145 // does not specify a face. A face can either be given by its plane (e.g, XY)
146 // or by the axis that is orthogonal to it (e.g., Z).
147 inline gint face_to_int (guint face_id) {
148 switch (face_id) {
149 case 1: return 0;
150 case 2: return 1;
151 case 4: return 2;
152 case 3: return 2;
153 case 5: return 1;
154 case 6: return 0;
156 case 9: return 3;
157 case 10: return 4;
158 case 12: return 5;
159 case 11: return 5;
160 case 13: return 4;
161 case 14: return 3;
163 default: return -1;
164 }
165 }
167 inline gint int_to_face (guint id) {
168 switch (id) {
169 case 0: return Box3D::YZ ^ Box3D::FRONT;
170 case 1: return Box3D::XZ ^ Box3D::FRONT;
171 case 2: return Box3D::XY ^ Box3D::FRONT;
172 case 3: return Box3D::YZ ^ Box3D::REAR;
173 case 4: return Box3D::XZ ^ Box3D::REAR;
174 case 5: return Box3D::XY ^ Box3D::REAR;
175 }
176 return Box3D::NONE; // should not be reached
177 }
179 inline bool is_face_id (guint face_id) {
180 return !((face_id & 0x7) == 0x7);
181 }
183 /**
184 inline gint opposite_face (guint face_id) {
185 return face_id + (((face_id % 2) == 0) ? 1 : -1);
186 }
187 **/
189 inline guint number_of_axis_directions (Box3D::Axis axis) {
190 guint num = 0;
191 if (axis & Box3D::X) num++;
192 if (axis & Box3D::Y) num++;
193 if (axis & Box3D::Z) num++;
195 return num;
196 }
198 inline bool is_plane (Box3D::Axis plane) {
199 return (number_of_axis_directions (plane) == 2);
200 }
202 inline bool is_single_axis_direction (Box3D::Axis dir) {
203 // tests whether dir is nonzero and a power of 2
204 return (!(dir & (dir - 1)) && dir);
205 }
207 /**
208 * Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one
209 * We don't check if 'plane' really specifies a plane (i.e., if it consists of precisely two directions).
210 */
211 inline Box3D::Axis third_axis_direction (Box3D::Axis dir1, Box3D::Axis dir2) {
212 return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
213 }
214 inline Box3D::Axis third_axis_direction (Box3D::Axis plane) {
215 return (Box3D::Axis) (plane ^ 0x7);
216 }
218 /* returns the first/second axis direction occuring in the (possibly compound) expression 'dirs' */
219 inline Box3D::Axis extract_first_axis_direction (Box3D::Axis dirs) {
220 if (dirs & Box3D::X) return Box3D::X;
221 if (dirs & Box3D::Y) return Box3D::Y;
222 if (dirs & Box3D::Z) return Box3D::Z;
223 return Box3D::NONE;
224 }
225 inline Box3D::Axis extract_second_axis_direction (Box3D::Axis dirs) {
226 return extract_first_axis_direction ((Box3D::Axis) (dirs ^ extract_first_axis_direction(dirs)));
227 }
229 inline Box3D::Axis orth_plane_or_axis (Box3D::Axis axis) {
230 return (Box3D::Axis) (Box3D::XYZ ^ axis);
231 }
233 /* returns an axis direction perpendicular to the ones occuring in the (possibly compound) expression 'dirs' */
234 inline Box3D::Axis get_perpendicular_axis_direction (Box3D::Axis dirs) {
235 if (!(dirs & Box3D::X)) return Box3D::X;
236 if (!(dirs & Box3D::Y)) return Box3D::Y;
237 if (!(dirs & Box3D::Z)) return Box3D::Z;
238 return Box3D::NONE;
239 }
241 inline gchar * string_from_axes (Box3D::Axis axes) {
242 GString *pstring = g_string_new("");
243 if (axes & Box3D::X) g_string_append_printf (pstring, "X");
244 if (axes & Box3D::Y) g_string_append_printf (pstring, "Y");
245 if (axes & Box3D::Z) g_string_append_printf (pstring, "Z");
246 return pstring->str;
247 }
249 std::pair <Axis, Axis> get_remaining_axes (Axis axis);
251 } // namespace Box3D
253 #endif /* !SEEN_AXIS_MANIP_H */
255 /*
256 Local Variables:
257 mode:c++
258 c-file-style:"stroustrup"
259 c-file-offsets:((innamespace . 0)(inline-open . 0))
260 indent-tabs-mode:nil
261 fill-column:99
262 End:
263 */
264 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :