summaryrefslogtreecommitdiffstats
path: root/src/axis-manip.h
blob: a0d019110163e7f60f5e6f880cc30ddbadc54c1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Generic auxiliary routines for 3D axes
 *
 * Authors:
 *   Maximilian Albert <Anhalter42@gmx.de>
 *
 * Copyright (C) 2007 authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#ifndef SEEN_AXIS_MANIP_H
#define SEEN_AXIS_MANIP_H

#include <cassert>
#include <string>
#include <utility>
#include <glibmm/ustring.h>

namespace Proj {

enum VPState {
    VP_FINITE = 0,
    VP_INFINITE
};

// The X-/Y-/Z-axis corresponds to the first/second/third digit
// in binary representation, respectively.
enum Axis {
    X = 0,
    Y = 1,
    Z = 2,
    W = 3,
    NONE
};

extern Axis axes[4];

inline char const*
string_from_axis(Proj::Axis axis) {
    switch (axis) {
    case X: return "X"; break;
    case Y: return "Y"; break;
    case Z: return "Z"; break;
    case W: return "W"; break;
    case NONE: return "NONE"; break;
    }
    return "";
}

} // namespace Proj

namespace Box3D {

const double epsilon = 1e-6;

// The X-/Y-/Z-axis corresponds to the first/second/third digit
// in binary representation, respectively.
enum Axis {
    X = 1,
    Y = 2,
    Z = 4,
    XY = 3,
    XZ = 5,
    YZ = 6,
    XYZ = 7,
    NONE = 0
};

// We use the fourth bit in binary representation
// to indicate whether a face is front or rear.
enum FrontOrRear { // find a better name
    FRONT = 0,
    REAR = 8
};

// converts X, Y, Z respectively to 0, 1, 2 (for use as array indices, e.g)
inline int axis_to_int(Box3D::Axis axis) {
    switch (axis) {
    case Box3D::X:
        return 0;
    case Box3D::Y:
        return 1;
    case Box3D::Z:
        return 2;
    case Box3D::NONE:
        return -1;
    default:
        assert(false);
        return -1; // help compiler's flow analysis (-Werror=return-value)
    }
}

inline Proj::Axis toProj(Box3D::Axis axis) {
    switch (axis) {
    case Box3D::X:
        return Proj::X;
    case Box3D::Y:
        return Proj::Y;
    case Box3D::Z:
        return Proj::Z;
    case Box3D::NONE:
        return Proj::NONE;
    default:
        assert(false);
        return Proj::NONE; // help compiler's flow analysis (-Werror=return-value)
    }
}

extern Axis axes[3];
extern Axis planes[3];
extern FrontOrRear face_positions [2];

} // namespace Box3D

namespace Proj {

inline Box3D::Axis toAffine(Proj::Axis axis) {
    switch (axis) {
    case Proj::X:
        return Box3D::X;
    case Proj::Y:
        return Box3D::Y;
    case Proj::Z:
        return Box3D::Z;
    case Proj::NONE:
        return Box3D::NONE;
    default:
        assert(false);
        return Box3D::NONE; // help compiler's flow analysis (-Werror=return-value)
    }
}

} // namespace Proj

namespace Box3D {

/* 
 * Identify the axes X, Y, Z with the numbers 0, 1, 2.
 * A box's face is identified by the axis perpendicular to it.
 * For a rear face, add 3.
 */
// Given a bit sequence that unambiguously specifies the face of a 3D box,
// return a number between 0 and 5 corresponding to that particular face
// (which is normally used to index an array). Return -1 if the bit sequence
// does not specify a face. A face can either be given by its plane (e.g, XY)
// or by the axis that is orthogonal to it (e.g., Z).
inline int face_to_int (unsigned int face_id) {
    switch (face_id) {
      case 1:  return 0;
      case 2:  return 1;
      case 4:  return 2;
      case 3:  return 2;
      case 5:  return 1;
      case 6:  return 0;

      case 9:  return 3;
      case 10: return 4;
      case 12: return 5;
      case 11: return 5;
      case 13: return 4;
      case 14: return 3;

    default: return -1;
    }
}

inline int int_to_face (unsigned id) {
    switch (id) {
    case 0: return Box3D::YZ ^ Box3D::FRONT;
    case 1: return Box3D::XZ ^ Box3D::FRONT;
    case 2: return Box3D::XY ^ Box3D::FRONT;
    case 3: return Box3D::YZ ^ Box3D::REAR;
    case 4: return Box3D::XZ ^ Box3D::REAR;
    case 5: return Box3D::XY ^ Box3D::REAR;
    }
    return Box3D::NONE; // should not be reached
}

inline bool is_face_id (unsigned int face_id) {
    return !((face_id & 0x7) == 0x7);
}

/**
inline gint opposite_face (guint face_id) {
    return face_id + (((face_id % 2) == 0) ? 1 : -1);
}
**/

inline unsigned int number_of_axis_directions (Box3D::Axis axis) {
    unsigned int num = 0;
    if (axis & Box3D::X) num++;
    if (axis & Box3D::Y) num++;
    if (axis & Box3D::Z) num++;

    return num;
}

inline bool is_plane (Box3D::Axis plane) {
    return (number_of_axis_directions (plane) == 2);
}

inline bool is_single_axis_direction (Box3D::Axis dir) {
    // tests whether dir is nonzero and a power of 2
    return (!(dir & (dir - 1)) && dir);
}

/**
 * Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one
 * We don't check if 'plane' really specifies a plane (i.e., if it consists of precisely two directions).
 */
inline Box3D::Axis third_axis_direction (Box3D::Axis dir1, Box3D::Axis dir2) {
    return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
}
inline Box3D::Axis third_axis_direction (Box3D::Axis plane) {
    return (Box3D::Axis) (plane ^ 0x7);
}

/* returns the first/second axis direction occurring in the (possibly compound) expression 'dirs' */
inline Box3D::Axis extract_first_axis_direction (Box3D::Axis dirs) {
    if (dirs & Box3D::X) return Box3D::X;
    if (dirs & Box3D::Y) return Box3D::Y;
    if (dirs & Box3D::Z) return Box3D::Z;
    return Box3D::NONE;
}
inline Box3D::Axis extract_second_axis_direction (Box3D::Axis dirs) {
    return extract_first_axis_direction ((Box3D::Axis) (dirs ^ extract_first_axis_direction(dirs)));
}

inline Box3D::Axis orth_plane_or_axis (Box3D::Axis axis) {
    return (Box3D::Axis) (Box3D::XYZ ^ axis);
}

/* returns an axis direction perpendicular to the ones occurring in the (possibly compound) expression 'dirs' */
inline Box3D::Axis get_perpendicular_axis_direction (Box3D::Axis dirs) {
    if (!(dirs & Box3D::X)) return Box3D::X;
    if (!(dirs & Box3D::Y)) return Box3D::Y;
    if (!(dirs & Box3D::Z)) return Box3D::Z;
    return Box3D::NONE;
}

Glib::ustring string_from_axes (Box3D::Axis axis);
std::pair <Axis, Axis> get_remaining_axes (Axis axis);

} // namespace Box3D

#endif /* !SEEN_AXIS_MANIP_H */

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :