Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
axis-manip.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Generic auxiliary routines for 3D axes
4 *
5 * Authors:
6 * Maximilian Albert <Anhalter42@gmx.de>
7 *
8 * Copyright (C) 2007 authors
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#ifndef SEEN_AXIS_MANIP_H
14#define SEEN_AXIS_MANIP_H
15
16#include <cassert>
17#include <string>
18#include <utility>
19#include <glibmm/ustring.h>
20
21namespace Proj {
22
27
28// The X-/Y-/Z-axis corresponds to the first/second/third digit
29// in binary representation, respectively.
30enum Axis {
31 X = 0,
32 Y = 1,
33 Z = 2,
34 W = 3,
35 NONE
36};
37
38extern Axis axes[4];
39
40inline char const*
42 switch (axis) {
43 case X: return "X"; break;
44 case Y: return "Y"; break;
45 case Z: return "Z"; break;
46 case W: return "W"; break;
47 case NONE: return "NONE"; break;
48 }
49 return "";
50}
51
52} // namespace Proj
53
54namespace Box3D {
55
56const double epsilon = 1e-6;
57
58// The X-/Y-/Z-axis corresponds to the first/second/third digit
59// in binary representation, respectively.
60enum Axis {
61 X = 1,
62 Y = 2,
63 Z = 4,
64 XY = 3,
65 XZ = 5,
66 YZ = 6,
67 XYZ = 7,
68 NONE = 0
69};
70
71// We use the fourth bit in binary representation
72// to indicate whether a face is front or rear.
73enum FrontOrRear { // find a better name
74 FRONT = 0,
75 REAR = 8
76};
77
78// converts X, Y, Z respectively to 0, 1, 2 (for use as array indices, e.g)
79inline int axis_to_int(Box3D::Axis axis) {
80 switch (axis) {
81 case Box3D::X:
82 return 0;
83 case Box3D::Y:
84 return 1;
85 case Box3D::Z:
86 return 2;
87 case Box3D::NONE:
88 return -1;
89 default:
90 assert(false);
91 return -1; // help compiler's flow analysis (-Werror=return-value)
92 }
93}
94
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 assert(false);
107 return Proj::NONE; // help compiler's flow analysis (-Werror=return-value)
108 }
109}
110
111extern Axis axes[3];
112extern Axis planes[3];
113extern FrontOrRear face_positions [2];
114
115} // namespace Box3D
116
117namespace Proj {
118
120 switch (axis) {
121 case Proj::X:
122 return Box3D::X;
123 case Proj::Y:
124 return Box3D::Y;
125 case Proj::Z:
126 return Box3D::Z;
127 case Proj::NONE:
128 return Box3D::NONE;
129 default:
130 assert(false);
131 return Box3D::NONE; // help compiler's flow analysis (-Werror=return-value)
132 }
133}
134
135} // namespace Proj
136
137namespace Box3D {
138
139/*
140 * Identify the axes X, Y, Z with the numbers 0, 1, 2.
141 * A box's face is identified by the axis perpendicular to it.
142 * For a rear face, add 3.
143 */
144// Given a bit sequence that unambiguously specifies the face of a 3D box,
145// return a number between 0 and 5 corresponding to that particular face
146// (which is normally used to index an array). Return -1 if the bit sequence
147// does not specify a face. A face can either be given by its plane (e.g, XY)
148// or by the axis that is orthogonal to it (e.g., Z).
149inline int face_to_int (unsigned int face_id) {
150 switch (face_id) {
151 case 1: return 0;
152 case 2: return 1;
153 case 4: return 2;
154 case 3: return 2;
155 case 5: return 1;
156 case 6: return 0;
157
158 case 9: return 3;
159 case 10: return 4;
160 case 12: return 5;
161 case 11: return 5;
162 case 13: return 4;
163 case 14: return 3;
164
165 default: return -1;
166 }
167}
168
169inline std::pair<Box3D::Axis, Box3D::FrontOrRear> int_to_face (unsigned id) {
170 switch (id) {
171 case 0: return (std::make_pair(Box3D::YZ, Box3D::FRONT));
172 case 1: return (std::make_pair(Box3D::XZ, Box3D::FRONT));
173 case 2: return (std::make_pair(Box3D::XY, Box3D::FRONT));
174 case 3: return (std::make_pair(Box3D::YZ, Box3D::REAR));
175 case 4: return (std::make_pair(Box3D::XZ, Box3D::REAR));
176 case 5: return (std::make_pair(Box3D::XY, Box3D::REAR));
177 }
178 return std::make_pair(Box3D::NONE, Box3D::FRONT); // should not be reached
179}
180
181inline bool is_face_id (unsigned int face_id) {
182 return !((face_id & 0x7) == 0x7);
183}
184
191inline unsigned int number_of_axis_directions (Box3D::Axis axis) {
192 unsigned int num = 0;
193 if (axis & Box3D::X) num++;
194 if (axis & Box3D::Y) num++;
195 if (axis & Box3D::Z) num++;
196
197 return num;
198}
199
200inline bool is_plane (Box3D::Axis plane) {
201 return (number_of_axis_directions (plane) == 2);
202}
203
205 // tests whether dir is nonzero and a power of 2
206 return (!(dir & (dir - 1)) && dir);
207}
208
214 return (Box3D::Axis) ((dir1 + dir2) ^ 0x7);
215}
217 return (Box3D::Axis) (plane ^ 0x7);
218}
219
220/* returns the first/second axis direction occurring in the (possibly compound) expression 'dirs' */
222 if (dirs & Box3D::X) return Box3D::X;
223 if (dirs & Box3D::Y) return Box3D::Y;
224 if (dirs & Box3D::Z) return Box3D::Z;
225 return Box3D::NONE;
226}
230
232 return (Box3D::Axis) (Box3D::XYZ ^ axis);
233}
234
235/* returns an axis direction perpendicular to the ones occurring in the (possibly compound) expression 'dirs' */
237 if (!(dirs & Box3D::X)) return Box3D::X;
238 if (!(dirs & Box3D::Y)) return Box3D::Y;
239 if (!(dirs & Box3D::Z)) return Box3D::Z;
240 return Box3D::NONE;
241}
242
243Glib::ustring string_from_axes (Box3D::Axis axis);
244std::pair <Axis, Axis> get_remaining_axes (Axis axis);
245
246} // namespace Box3D
247
248#endif /* !SEEN_AXIS_MANIP_H */
249
250/*
251 Local Variables:
252 mode:c++
253 c-file-style:"stroustrup"
254 c-file-offsets:((innamespace . 0)(inline-open . 0))
255 indent-tabs-mode:nil
256 fill-column:99
257 End:
258*/
259// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
Perspective line for 3D perspectives.
Proj::Axis toProj(Box3D::Axis axis)
Definition axis-manip.h:95
std::pair< Box3D::Axis, Box3D::FrontOrRear > int_to_face(unsigned id)
Definition axis-manip.h:169
Box3D::Axis get_perpendicular_axis_direction(Box3D::Axis dirs)
Definition axis-manip.h:236
const double epsilon
Definition axis-manip.h:56
int face_to_int(unsigned int face_id)
Definition axis-manip.h:149
Box3D::Axis third_axis_direction(Box3D::Axis dir1, Box3D::Axis dir2)
Given two axis directions out of {X, Y, Z} or the corresponding plane, return the remaining one We do...
Definition axis-manip.h:213
std::pair< Axis, Axis > get_remaining_axes(Axis axis)
Axis planes[3]
Box3D::Axis orth_plane_or_axis(Box3D::Axis axis)
Definition axis-manip.h:231
Glib::ustring string_from_axes(Box3D::Axis axis)
FrontOrRear face_positions[2]
FrontOrRear
Definition axis-manip.h:73
@ FRONT
Definition axis-manip.h:74
unsigned int number_of_axis_directions(Box3D::Axis axis)
inline gint opposite_face (guint face_id) { return face_id + (((face_id % 2) == 0) ?...
Definition axis-manip.h:191
Axis axes[3]
bool is_face_id(unsigned int face_id)
Definition axis-manip.h:181
Box3D::Axis extract_first_axis_direction(Box3D::Axis dirs)
Definition axis-manip.h:221
Box3D::Axis extract_second_axis_direction(Box3D::Axis dirs)
Definition axis-manip.h:227
int axis_to_int(Box3D::Axis axis)
Definition axis-manip.h:79
bool is_plane(Box3D::Axis plane)
Definition axis-manip.h:200
bool is_single_axis_direction(Box3D::Axis dir)
Definition axis-manip.h:204
Generic auxiliary routines for 3D axes.
@ VP_FINITE
Definition axis-manip.h:24
@ VP_INFINITE
Definition axis-manip.h:25
@ NONE
Definition axis-manip.h:35
char const * string_from_axis(Proj::Axis axis)
Definition axis-manip.h:41
Box3D::Axis toAffine(Proj::Axis axis)
Definition axis-manip.h:119
Axis axes[4]
int num
Definition scribble.cpp:47