From 536ac52055722b168e4482f612302fa754dc4f72 Mon Sep 17 00:00:00 2001 From: cilix42 Date: Wed, 11 Jul 2007 08:01:56 +0000 Subject: [PATCH] Move 3D axis manipulation functions to separate file --- src/Makefile_insert | 1 + src/axis-manip.cpp | 33 ++++++++++ src/axis-manip.h | 129 ++++++++++++++++++++++++++++++++++++++++ src/line-geometry.h | 4 +- src/perspective-line.h | 1 + src/vanishing-point.cpp | 5 -- src/vanishing-point.h | 100 +------------------------------ 7 files changed, 169 insertions(+), 104 deletions(-) create mode 100644 src/axis-manip.cpp create mode 100644 src/axis-manip.h diff --git a/src/Makefile_insert b/src/Makefile_insert index f1ba21920..d2ce0c193 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -40,6 +40,7 @@ libinkpre_a_SOURCES = \ approx-equal.h remove-last.h \ arc-context.cpp arc-context.h \ attributes.cpp attributes.h \ + axis-manip.cpp axis-manip.h \ bad-uri-exception.h \ box3d.cpp box3d.h \ box3d-context.cpp box3d-context.h \ diff --git a/src/axis-manip.cpp b/src/axis-manip.cpp new file mode 100644 index 000000000..094da7dde --- /dev/null +++ b/src/axis-manip.cpp @@ -0,0 +1,33 @@ +#define __AXIS_MANIP_C__ + +/* + * Generic auxiliary routines for 3D axes + * + * Authors: + * Maximilian Albert + * + * Copyright (C) 2007 authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "axis-manip.h" + +namespace Box3D { + +Axis axes[3] = { X, Y, Z }; +Axis planes[3] = { XY, XZ, YZ }; +FrontOrRear face_positions [2] = { FRONT, REAR }; + +} // namespace Box3D + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : diff --git a/src/axis-manip.h b/src/axis-manip.h new file mode 100644 index 000000000..7ad716046 --- /dev/null +++ b/src/axis-manip.h @@ -0,0 +1,129 @@ +/* + * Generic auxiliary routines for 3D axes + * + * Authors: + * Maximilian Albert + * + * Copyright (C) 2007 authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#ifndef SEEN_AXIS_MANIP_H +#define SEEN_AXIS_MANIP_H + +#include +#include "libnr/nr-point.h" + +namespace Box3D { + +// 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 +}; + +extern Axis axes[3]; +extern Axis planes[3]; +extern FrontOrRear face_positions [2]; + +// 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 gint face_to_int (guint face_id) { + switch (face_id) { + case 1: return 0; + case 2: return 2; + case 4: return 4; + case 3: return 4; + case 5: return 2; + case 6: return 0; + + case 9: return 1; + case 10: return 3; + case 12: return 5; + case 11: return 5; + case 13: return 3; + case 14: return 1; + + default: return -1; + } +} + +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 occuring 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 (Box3D::Axis axis) { + return (Box3D::Axis) (Box3D::XYZ ^ axis); +} + +/* returns an axis direction perpendicular to the ones occuring 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; +} + +inline gchar * string_from_axes (Box3D::Axis axes) { + GString *pstring = g_string_new(""); + if (axes & Box3D::X) g_string_append_printf (pstring, "X"); + if (axes & Box3D::Y) g_string_append_printf (pstring, "Y"); + if (axes & Box3D::Z) g_string_append_printf (pstring, "Z"); + return pstring->str; +} + +} // 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 : diff --git a/src/line-geometry.h b/src/line-geometry.h index 53f39db39..b1eae366c 100644 --- a/src/line-geometry.h +++ b/src/line-geometry.h @@ -50,8 +50,8 @@ void create_canvas_point(NR::Point const &pos, double size = 4.0, guint32 rgba = void create_canvas_line(NR::Point const &p1, NR::Point const &p2, guint32 rgba = 0xff00007f); -/** A function to print out the Line. It just prints out the coordinates of start end end point - on the given output stream */ +/** A function to print out the Line. It just prints out the coordinates of start point and + direction on the given output stream */ inline std::ostream &operator<< (std::ostream &out_file, const Line &in_line) { out_file << "Start: " << in_line.pt << " Direction: " << in_line.v_dir; return out_file; diff --git a/src/perspective-line.h b/src/perspective-line.h index 45a9b0be6..a02737692 100644 --- a/src/perspective-line.h +++ b/src/perspective-line.h @@ -13,6 +13,7 @@ #define SEEN_PERSPECTIVE_LINE_H #include "vanishing-point.h" +#include "line-geometry.h" #include "box3d-context.h" #include diff --git a/src/vanishing-point.cpp b/src/vanishing-point.cpp index c4540fa73..7d5b24a86 100644 --- a/src/vanishing-point.cpp +++ b/src/vanishing-point.cpp @@ -12,14 +12,9 @@ */ #include "vanishing-point.h" -#include namespace Box3D { -Axis axes[3] = { X, Y, Z }; -Axis planes[3] = { XY, XZ, YZ }; -FrontOrRear face_positions [2] = { FRONT, REAR }; - // FIXME: We should always require to have both the point (for finite VPs) // and the direction (for infinite VPs) set. Otherwise toggling // shows very unexpected behaviour. diff --git a/src/vanishing-point.h b/src/vanishing-point.h index b7f3d12a1..e6c106481 100644 --- a/src/vanishing-point.h +++ b/src/vanishing-point.h @@ -13,7 +13,9 @@ #define SEEN_VANISHING_POINT_H #include "libnr/nr-point.h" -#include "line-geometry.h" +#include "axis-manip.h" + +#include "line-geometry.h" // TODO: Remove this include as soon as we don't need create_canvas_(point|line) any more. namespace Box3D { @@ -22,102 +24,6 @@ enum VPState { VP_INFINITE // perspective lines are parallel }; -// 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 -}; - -extern Axis axes[3]; -extern Axis planes[3]; -extern FrontOrRear face_positions [2]; - -// 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 gint face_to_int (guint face_id) { - switch (face_id) { - case 1: return 0; - case 2: return 2; - case 4: return 4; - case 3: return 4; - case 5: return 2; - case 6: return 0; - - case 9: return 1; - case 10: return 3; - case 12: return 5; - case 11: return 5; - case 13: return 3; - case 14: return 1; - - default: return -1; - } -} - -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 occuring 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 (Box3D::Axis axis) { - return (Box3D::Axis) (Box3D::XYZ ^ axis); -} - -/* returns an axis direction perpendicular to the ones occuring 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; -} - -inline gchar * string_from_axes (Box3D::Axis axes) { - GString *pstring = g_string_new(""); - if (axes & Box3D::X) g_string_append_printf (pstring, "X"); - if (axes & Box3D::Y) g_string_append_printf (pstring, "Y"); - if (axes & Box3D::Z) g_string_append_printf (pstring, "Z"); - return pstring->str; -} - // FIXME: Store the Axis of the VP inside the class class VanishingPoint : public NR::Point { public: -- 2.30.2