Code

Draw perspective lines for infinite VPs, too (they are updated during scrolling or...
[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 guint opposite_face (guint face_id) {
72     return face_id + ((face_id % 2 == 0) ? 1 : -1);
73 }
75 inline bool is_single_axis_direction (Box3D::Axis dir) {
76     // tests whether dir is nonzero and a power of 2
77     return (!(dir & (dir - 1)) && dir);
78 }
80 // Warning: We don't check that axis really unamiguously specifies a plane.
81 //          Make sure this is the case when calling this function.
82 inline guint face_containing_corner (Box3D::Axis axis, guint corner) {
83     if (!is_single_axis_direction (axis)) {
84         axis = (Box3D::Axis) (axis ^ Box3D::XYZ);
85     }
86     return face_to_int (axis ^ ((corner & axis) ? Box3D::REAR : Box3D::FRONT));
87 }
90 /**
91  * Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one
92  * We don't check if 'plane' really specifies a plane (i.e., if it consists of precisely two directions).
93  */
94 inline Box3D::Axis third_axis_direction (Box3D::Axis dir1, Box3D::Axis dir2) {
95     return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
96 }
97 inline Box3D::Axis third_axis_direction (Box3D::Axis plane) {
98     return (Box3D::Axis) (plane ^ 0x7);
99 }
101 /* returns the first/second axis direction occuring in the (possibly compound) expression 'dirs' */
102 inline Box3D::Axis extract_first_axis_direction (Box3D::Axis dirs) {
103     if (dirs & Box3D::X) return Box3D::X;
104     if (dirs & Box3D::Y) return Box3D::Y;
105     if (dirs & Box3D::Z) return Box3D::Z;
106     return Box3D::NONE;
108 inline Box3D::Axis extract_second_axis_direction (Box3D::Axis dirs) {
109     return extract_first_axis_direction ((Box3D::Axis) (dirs ^ extract_first_axis_direction(dirs)));
112 inline Box3D::Axis orth_plane (Box3D::Axis axis) {
113     return (Box3D::Axis) (Box3D::XYZ ^ axis);
116 /* returns an axis direction perpendicular to the ones occuring in the (possibly compound) expression 'dirs' */
117 inline Box3D::Axis get_perpendicular_axis_direction (Box3D::Axis dirs) {
118     if (!(dirs & Box3D::X)) return Box3D::X;
119     if (!(dirs & Box3D::Y)) return Box3D::Y;
120     if (!(dirs & Box3D::Z)) return Box3D::Z;
121     return Box3D::NONE;
124 inline gchar * string_from_axes (Box3D::Axis axes) {
125     GString *pstring = g_string_new("");
126     if (axes & Box3D::X) g_string_append_printf (pstring, "X");
127     if (axes & Box3D::Y) g_string_append_printf (pstring, "Y");
128     if (axes & Box3D::Z) g_string_append_printf (pstring, "Z");
129     return pstring->str;
132 std::pair <Axis, Axis> get_remaining_axes (Axis axis);
134 } // namespace Box3D
136 #endif /* !SEEN_AXIS_MANIP_H */
138 /*
139   Local Variables:
140   mode:c++
141   c-file-style:"stroustrup"
142   c-file-offsets:((innamespace . 0)(inline-open . 0))
143   indent-tabs-mode:nil
144   fill-column:99
145   End:
146 */
147 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :