Code

0021630a099448d9c99c8e71a2d7559131854815
[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 <glib/gtestutils.h>
16 #include <gtk/gtk.h>
17 #include "libnr/nr-point.h"
19 namespace Proj {
21 enum VPState {
22     FINITE = 0,
23     INFINITE
24 };
26 // The X-/Y-/Z-axis corresponds to the first/second/third digit
27 // in binary representation, respectively.
28 enum Axis {
29     X = 0,
30     Y = 1,
31     Z = 2,
32     W = 3,
33     NONE
34 };
36 extern Axis axes[4];
38 inline gchar * string_from_axis (Proj::Axis axis) {
39     switch (axis) {
40     case X:
41         return "X";
42         break;
43     case Y:
44         return "Y";
45         break;
46     case Z:
47         return "Z";
48         break;
49     case W:
50         return "W";
51         break;
52     case NONE:
53         return "NONE";
54         break;
55     }
56     return "";
57 }
59 } // namespace Proj
61 namespace Box3D {
63 const double epsilon = 1e-6;
65 // The X-/Y-/Z-axis corresponds to the first/second/third digit
66 // in binary representation, respectively.
67 enum Axis {
68     X = 1,
69     Y = 2,
70     Z = 4,
71     XY = 3,
72     XZ = 5,
73     YZ = 6,
74     XYZ = 7,
75     NONE = 0
76 };
78 // We use the fourth bit in binary representation
79 // to indicate whether a face is front or rear.
80 enum FrontOrRear { // find a better name
81     FRONT = 0,
82     REAR = 8
83 };
85 // converts X, Y, Z respectively to 0, 1, 2 (for use as array indices, e.g)
86 inline int axis_to_int(Box3D::Axis axis) {
87     switch (axis) {
88     case Box3D::X:
89         return 0;
90         break;
91     case Box3D::Y:
92         return 1;
93         break;
94     case Box3D::Z:
95         return 2;
96         break;
97     case Box3D::NONE:
98         return -1;
99         break;
100     default:
101         g_assert_not_reached();
102     }
105 inline Proj::Axis toProj(Box3D::Axis axis) {
106     switch (axis) {
107     case Box3D::X:
108         return Proj::X;
109     case Box3D::Y:
110         return Proj::Y;
111     case Box3D::Z:
112         return Proj::Z;
113     case Box3D::NONE:
114         return Proj::NONE;
115     default:
116         g_assert_not_reached();
117     }
120 extern Axis axes[3];
121 extern Axis planes[3];
122 extern FrontOrRear face_positions [2];
124 } // namespace Box3D
126 namespace Proj {
128 inline Box3D::Axis toAffine(Proj::Axis axis) {
129     switch (axis) {
130     case Proj::X:
131         return Box3D::X;
132     case Proj::Y:
133         return Box3D::Y;
134     case Proj::Z:
135         return Box3D::Z;
136     case Proj::NONE:
137         return Box3D::NONE;
138     default:
139         g_assert_not_reached();
140     }
143 } // namespace Proj
145 namespace Box3D {
147 // Given a bit sequence that unambiguously specifies the face of a 3D box,
148 // return a number between 0 and 5 corresponding to that particular face
149 // (which is normally used to index an array). Return -1 if the bit sequence
150 // does not specify a face. A face can either be given by its plane (e.g, XY)
151 // or by the axis that is orthogonal to it (e.g., Z).
152 /***
153 inline gint face_to_int (guint face_id) {
154     switch (face_id) {
155       case 1:  return 0;
156       case 2:  return 2;
157       case 4:  return 4;
158       case 3:  return 4;
159       case 5:  return 2;
160       case 6:  return 0;
162       case 9:  return 1;
163       case 10: return 3;
164       case 12: return 5;
165       case 11: return 5;
166       case 13: return 3;
167       case 14: return 1;
169     default: return -1;
170     }
172 ***/
174 /***
175 inline gint int_to_face (guint id) {
176     switch (id) {
177       case 0: return  6;
178       case 1: return 14;
179       case 2: return  5;
180       case 3: return 13;
181       case 4: return  3;
182       case 5: return 11;
184     default: return -1;
185     }
187 ***/
189 /* 
190  * New version:
191  * Identify the axes X, Y, Z with the numbers 0, 1, 2.
192  * A box's face is identified by the axis perpendicular to it.
193  * For a rear face, add 3.
194  */
195 // Given a bit sequence that unambiguously specifies the face of a 3D box,
196 // return a number between 0 and 5 corresponding to that particular face
197 // (which is normally used to index an array). Return -1 if the bit sequence
198 // does not specify a face. A face can either be given by its plane (e.g, XY)
199 // or by the axis that is orthogonal to it (e.g., Z).
200 inline gint face_to_int (guint face_id) {
201     switch (face_id) {
202       case 1:  return 0;
203       case 2:  return 1;
204       case 4:  return 2;
205       case 3:  return 2;
206       case 5:  return 1;
207       case 6:  return 0;
209       case 9:  return 3;
210       case 10: return 4;
211       case 12: return 5;
212       case 11: return 5;
213       case 13: return 4;
214       case 14: return 3;
216     default: return -1;
217     }
220 inline gint int_to_face (guint id) {
221     switch (id) {
222     case 0: return Box3D::YZ ^ Box3D::FRONT;
223     case 1: return Box3D::XZ ^ Box3D::FRONT;
224     case 2: return Box3D::XY ^ Box3D::FRONT;
225     case 3: return Box3D::YZ ^ Box3D::REAR;
226     case 4: return Box3D::XZ ^ Box3D::REAR;
227     case 5: return Box3D::XY ^ Box3D::REAR;
228     }
229     return Box3D::NONE; // should not be reached
232 inline bool is_face_id (guint face_id) {
233     return !((face_id & 0x7) == 0x7);
236 /**
237 inline gint opposite_face (guint face_id) {
238     return face_id + (((face_id % 2) == 0) ? 1 : -1);
240 **/
242 inline guint number_of_axis_directions (Box3D::Axis axis) {
243     guint num = 0;
244     if (axis & Box3D::X) num++;
245     if (axis & Box3D::Y) num++;
246     if (axis & Box3D::Z) num++;
248     return num;
251 inline bool is_plane (Box3D::Axis plane) {
252     return (number_of_axis_directions (plane) == 2);
255 inline bool is_single_axis_direction (Box3D::Axis dir) {
256     // tests whether dir is nonzero and a power of 2
257     return (!(dir & (dir - 1)) && dir);
260 /***
261 // Warning: We don't check that axis really unambiguously specifies a plane.
262 //          Make sure this is the case when calling this function.
263 inline gint face_containing_corner (Box3D::Axis axis, guint corner) {
264     if (!is_single_axis_direction (axis)) {
265         axis = (Box3D::Axis) (axis ^ Box3D::XYZ);
266     }
267     return face_to_int (axis ^ ((corner & axis) ? Box3D::REAR : Box3D::FRONT));
269 ***/
271 /**
272  * Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one
273  * We don't check if 'plane' really specifies a plane (i.e., if it consists of precisely two directions).
274  */
275 inline Box3D::Axis third_axis_direction (Box3D::Axis dir1, Box3D::Axis dir2) {
276     return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
278 inline Box3D::Axis third_axis_direction (Box3D::Axis plane) {
279     return (Box3D::Axis) (plane ^ 0x7);
282 /* returns the first/second axis direction occuring in the (possibly compound) expression 'dirs' */
283 inline Box3D::Axis extract_first_axis_direction (Box3D::Axis dirs) {
284     if (dirs & Box3D::X) return Box3D::X;
285     if (dirs & Box3D::Y) return Box3D::Y;
286     if (dirs & Box3D::Z) return Box3D::Z;
287     return Box3D::NONE;
289 inline Box3D::Axis extract_second_axis_direction (Box3D::Axis dirs) {
290     return extract_first_axis_direction ((Box3D::Axis) (dirs ^ extract_first_axis_direction(dirs)));
293 inline Box3D::Axis orth_plane_or_axis (Box3D::Axis axis) {
294     return (Box3D::Axis) (Box3D::XYZ ^ axis);
297 /* returns an axis direction perpendicular to the ones occuring in the (possibly compound) expression 'dirs' */
298 inline Box3D::Axis get_perpendicular_axis_direction (Box3D::Axis dirs) {
299     if (!(dirs & Box3D::X)) return Box3D::X;
300     if (!(dirs & Box3D::Y)) return Box3D::Y;
301     if (!(dirs & Box3D::Z)) return Box3D::Z;
302     return Box3D::NONE;
305 inline gchar * string_from_axes (Box3D::Axis axes) {
306     GString *pstring = g_string_new("");
307     if (axes & Box3D::X) g_string_append_printf (pstring, "X");
308     if (axes & Box3D::Y) g_string_append_printf (pstring, "Y");
309     if (axes & Box3D::Z) g_string_append_printf (pstring, "Z");
310     return pstring->str;
313 std::pair <Axis, Axis> get_remaining_axes (Axis axis);
315 } // namespace Box3D
317 #endif /* !SEEN_AXIS_MANIP_H */
319 /*
320   Local Variables:
321   mode:c++
322   c-file-style:"stroustrup"
323   c-file-offsets:((innamespace . 0)(inline-open . 0))
324   indent-tabs-mode:nil
325   fill-column:99
326   End:
327 */
328 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :