diff --git a/src/box3d.cpp b/src/box3d.cpp
index 258c658791084c66f11b9d2ecec287a9cc5edc0b..eb67269ce3975acec57e4340b3f6b0207ec9be81 100644 (file)
--- a/src/box3d.cpp
+++ b/src/box3d.cpp
* Released under GNU GPL, read the file 'COPYING' for more information
*/
+#include <glibmm/i18n.h>
+#include "attributes.h"
#include "box3d.h"
static void sp_3dbox_class_init(SP3DBoxClass *klass);
static void sp_3dbox_init(SP3DBox *box3d);
static void sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static void sp_shape_release (SPObject *object);
+//static void sp_3dbox_release (SPObject *object);
static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value);
static void sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags);
static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
-//static gchar *sp_3dbox_description(SPItem *item);
+static gchar *sp_3dbox_description(SPItem *item);
//static void sp_3dbox_set_shape(SPShape *shape);
static void sp_3dbox_set_shape(SP3DBox *box3d);
+static void sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value);
+static gchar * sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id);
+static std::pair<gdouble, gdouble> sp_3dbox_get_coord_pair_from_string (const gchar *);
+
static SPGroupClass *parent_class;
GType
sp_3dbox_class_init(SP3DBoxClass *klass)
{
SPObjectClass *sp_object_class = (SPObjectClass *) klass;
- // SPItemClass *item_class = (SPItemClass *) klass;
+ SPItemClass *item_class = (SPItemClass *) klass;
parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
sp_object_class->set = sp_3dbox_set;
sp_object_class->write = sp_3dbox_write;
sp_object_class->update = sp_3dbox_update;
+ //sp_object_class->release = sp_3dbox_release;
- //item_class->description = sp_3dbox_description;
+ item_class->description = sp_3dbox_description;
}
static void
-sp_3dbox_init(SP3DBox *box3d)
+sp_3dbox_init(SP3DBox *box)
{
- box3d->faces[4] = new Box3DFace (box3d);
- //box3d->faces[4]->hook_path_to_3dbox();
- for (int i = 0; i < 6; ++i) {
- if (i == 4) continue;
- box3d->faces[i] = NULL;
- }
+ for (int i = 0; i < 8; ++i) box->corners[i] = NR::Point(0,0);
+ for (int i = 0; i < 6; ++i) box->faces[i] = NULL;
}
static void
((SPObjectClass *) (parent_class))->build(object, document, repr);
}
- //sp_object_read_attr(object, "width");
+ SP3DBox *box = SP_3DBOX (object);
+
+ sp_object_read_attr(object, "inkscape:box3dcornerA");
+ sp_object_read_attr(object, "inkscape:box3dcornerB");
+ sp_object_read_attr(object, "inkscape:box3dcornerC");
+
+ // TODO: We create all faces in the beginning, but only the non-degenerate ones
+ // should be written to the svg representation later in sp_3dbox_write.
+ Box3D::Axis cur_plane, axis, dir1, dir2;
+ Box3D::FrontOrRear cur_pos;
+ for (int i = 0; i < 3; ++i) {
+ for (int j = 0; j < 2; ++j) {
+ cur_plane = Box3D::planes[i];
+ cur_pos = Box3D::face_positions[j];
+ // FIXME: The following code could theoretically be moved to
+ // the constructor of Box3DFace (but see the comment there).
+ axis = (cur_pos == Box3D::FRONT ? Box3D::NONE : Box3D::third_axis_direction (cur_plane));
+ dir1 = extract_first_axis_direction (cur_plane);
+ dir2 = extract_second_axis_direction (cur_plane);
+
+ box->faces[Box3D::face_to_int(cur_plane ^ cur_pos)] =
+ new Box3DFace (box, box->corners[axis], box->corners[axis ^ dir1],
+ box->corners[axis ^ dir1 ^ dir2], box->corners[axis ^ dir2],
+ cur_plane, cur_pos);
+ }
+ }
+
+ // Check whether the paths of the faces of the box need to be linked to existing paths in the
+ // document (e.g., after a 'redo' operation or after opening a file) and do so if necessary.
+ sp_3dbox_link_to_existing_paths (box, repr);
}
+/*
static void
sp_3dbox_release (SPObject *object)
{
((SPObjectClass *) parent_class)->release (object);
}
}
-
+*/
static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
{
- //SP3DBox *box3d = SP_3DBOX(object);
-
switch (key) {
- /***
- case SP_ATTR_WIDTH:
- rect->width.readOrUnset(value);
- object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+ case SP_ATTR_INKSCAPE_3DBOX_CORNER_A:
+ sp_3dbox_update_corner_with_value_from_svg (object, 2, value);
+ break;
+ case SP_ATTR_INKSCAPE_3DBOX_CORNER_B:
+ sp_3dbox_update_corner_with_value_from_svg (object, 1, value);
+ break;
+ case SP_ATTR_INKSCAPE_3DBOX_CORNER_C:
+ sp_3dbox_update_corner_with_value_from_svg (object, 5, value);
break;
- ***/
default:
if (((SPObjectClass *) (parent_class))->set) {
((SPObjectClass *) (parent_class))->set(object, key, value);
@@ -129,44 +163,57 @@ static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
}
}
-
static void
sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
{
- //SP3DBox *box3d = SP_3DBOX(object);
+ if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
+ SP3DBox *box = SP_3DBOX(object);
+ sp_3dbox_link_to_existing_paths (box, SP_OBJECT_REPR(object));
+ }
/* Invoke parent method */
if (((SPObjectClass *) (parent_class))->update)
((SPObjectClass *) (parent_class))->update(object, ctx, flags);
-
- //sp_3dbox_set_shape (box3d);
}
static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
{
- SP3DBox *box3d = SP_3DBOX(object);
+ SP3DBox *box = SP_3DBOX(object);
// FIXME: How to handle other contexts???
// FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
return repr;
- SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
repr = xml_doc->createElement("svg:g");
repr->setAttribute("sodipodi:type", "inkscape:3dbox");
+ /* Hook paths to the faces of the box */
+ for (int i = 0; i < 6; ++i) {
+ box->faces[i]->hook_path_to_3dbox();
+ }
}
+ for (int i = 0; i < 6; ++i) {
+ box->faces[i]->set_path_repr();
+ }
- box3d->faces[4]->set_path_repr();
- if (bc->extruded) {
- for (int i = 0; i < 6; ++i) {
- if (i == 4) continue;
- box3d->faces[i]->set_path_repr();
- }
+ if (flags & SP_OBJECT_WRITE_EXT) {
+ gchar *coords;
+ coords = sp_3dbox_get_corner_coords_string (box, 2);
+ repr->setAttribute("inkscape:box3dcornerA", coords);
+
+ coords = sp_3dbox_get_corner_coords_string (box, 1);
+ repr->setAttribute("inkscape:box3dcornerB", coords);
+
+ coords = sp_3dbox_get_corner_coords_string (box, 5);
+ repr->setAttribute("inkscape:box3dcornerC", coords);
+
+ g_free ((void *) coords);
}
+
if (((SPObjectClass *) (parent_class))->write) {
((SPObjectClass *) (parent_class))->write(object, repr, flags);
}
@@ -174,6 +221,14 @@ static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node
return repr;
}
+static gchar *
+sp_3dbox_description(SPItem *item)
+{
+ g_return_val_if_fail(SP_IS_3DBOX(item), NULL);
+
+ return g_strdup(_("<b>3D Box</b>"));
+}
+
void
sp_3dbox_position_set (SP3DBoxContext &bc)
{
@@ -228,7 +283,7 @@ void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point cons
{
sp_3dbox_move_corner_in_XY_plane (box, 2, A);
sp_3dbox_move_corner_in_XY_plane (box, 1, B);
- sp_3dbox_move_corner_in_constrained_Z_direction (box, 5, C);
+ sp_3dbox_move_corner_in_Z_direction (box, 5, C);
}
void
}
}
+/**
+ * In some situations (e.g., after cloning boxes, undo & redo, or reading boxes from a file) there are
+ * paths already present in the document which correspond to the faces of newly created boxes, but their
+ * 'path' members don't link to them yet. The following function corrects this if necessary.
+ */
void
-sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt)
+sp_3dbox_link_to_existing_paths (SP3DBox *box, Inkscape::XML::Node *repr) {
+ // TODO: We should probably destroy the existing paths and recreate them because we don't know
+ // precisely which path corresponds to which face. Does this make a difference?
+ // In sp_3dbox_write we write the correct paths anyway, don't we? But we could get into
+ // trouble at a later stage when we only write single faces for degenerate boxes.
+
+ SPDocument *document = SP_OBJECT_DOCUMENT(box);
+ guint face_id = 0;
+
+ for (Inkscape::XML::Node *i = sp_repr_children(repr); i != NULL; i = sp_repr_next(i)) {
+ if (face_id > 5) {
+ g_warning ("SVG representation of 3D boxes must contain 6 paths or less.\n");
+ break;
+ }
+
+ SPObject *face_object = document->getObjectByRepr((Inkscape::XML::Node *) i);
+ if (!SP_IS_PATH(face_object)) {
+ g_warning ("SVG representation of 3D boxes should only contain paths.\n");
+ continue;
+ }
+ box->faces[face_id]->hook_path_to_3dbox(SP_PATH(face_object));
+ ++face_id;
+ }
+ if (face_id < 6) {
+ //g_warning ("SVG representation of 3D boxes should contain exactly 6 paths (degenerate boxes are not yet supported).\n");
+ // TODO: Check whether it is safe to add the remaining paths to the box and do so in case it is.
+ // (But we also land here for newly created boxes where we shouldn't add any paths because
+ // This is done in sp_3dbox_write later on.
+ }
+}
+
+void
+sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
{
NR::Point A (box->corners[id ^ Box3D::XY]);
+ if (Box3D::is_single_axis_direction (axes)) {
+ pt = Box3D::PerspectiveLine (box->corners[id], axes).closest_to(pt);
+ }
/* set the 'front' corners */
box->corners[id] = pt;
}
void
-sp_3dbox_move_corner_in_constrained_Z_direction (SP3DBox *box, guint id, NR::Point pt)
+sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
{
+ if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
+
/* set the four corners of the face containing corners[id] */
box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt);
@@ -290,6 +387,75 @@ sp_3dbox_move_corner_in_constrained_Z_direction (SP3DBox *box, guint id, NR::Poi
box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
}
+NR::Maybe<NR::Point>
+sp_3dbox_get_center (SP3DBox *box)
+{
+ return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
+}
+
+NR::Maybe<NR::Point>
+sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
+{
+ Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
+
+ // Is all this sufficiently precise also for degenerate cases?
+ if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
+ Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
+
+ Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
+ Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
+ NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
+
+ if (!adjacent_face_center) return NR::Nothing();
+
+ Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir);
+ return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes));
+ } else {
+ Box3D::Axis dir = Box3D::extract_first_axis_direction (corner_axes);
+ Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
+ Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
+ return diag1.intersect(diag2);
+ }
+}
+
+static gchar *
+sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id)
+{
+ id = id % 8;
+ return g_strdup_printf ("%f,%f", box->corners[id][NR::X], box->corners[id][NR::Y]);
+}
+
+static std::pair<gdouble, gdouble>
+sp_3dbox_get_coord_pair_from_string (const gchar *coords)
+{
+ gchar **coordlist = g_strsplit( coords, ",", 0);
+ // We might as well rely on g_strtod to convert the NULL pointer to 0.0,
+ // but we include the following test anyway
+ if (coordlist[0] == NULL || coordlist[1] == NULL) {
+ g_strfreev (coordlist);
+ g_warning ("Coordinate conversion failed.\n");
+ return std::make_pair(0.0, 0.0);
+ }
+
+ gdouble coord1 = g_strtod(coordlist[0], NULL);
+ gdouble coord2 = g_strtod(coordlist[1], NULL);
+ g_strfreev (coordlist);
+
+ return std::make_pair(coord1, coord2);
+}
+
+// auxiliary function
+static void
+sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value)
+{
+ if (value == NULL) return;
+ SP3DBox *box = SP_3DBOX(object);
+
+ std::pair<gdouble, gdouble> coord_pair = sp_3dbox_get_coord_pair_from_string (value);
+ box->corners[corner_id] = NR::Point (coord_pair.first, coord_pair.second);
+ sp_3dbox_recompute_corners (box, box->corners[2], box->corners[1], box->corners[5]);
+ object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+}
/*
Local Variables: