From: cilix42 Date: Tue, 3 Jul 2007 19:20:05 +0000 (+0000) Subject: Refactoring of 3D box internals. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e296063e06f00da943e45455f31ca039c90868cc;p=inkscape.git Refactoring of 3D box internals. Handle the corners in the box itself and only link to them from the separate faces. This way we can make the code *much* cleaner and more generic. Provide generic auxiliary functions for the movement of corners in XY-plane and Z-direction. --- diff --git a/src/box3d-face.cpp b/src/box3d-face.cpp index e499fe4d0..5eefcb812 100644 --- a/src/box3d-face.cpp +++ b/src/box3d-face.cpp @@ -17,10 +17,34 @@ Box3DFace::Box3DFace(SP3DBox *box3d) : dir1 (Box3D::NONE), dir2 (Box3D::NONE), path (NULL), parent_box3d (box3d) { for (int i = 0; i < 4; ++i) { - this->corners[i] = NR::Point(0, 0); + this->corners[i] = new NR::Point(0, 0); } } +Box3DFace::Box3DFace(SP3DBox *box3d, NR::Point &A, NR::Point &B, NR::Point &C, NR::Point &D) +{ + set_corners (A, B, C, D); +} + +Box3DFace::~Box3DFace() +{ + for (int i = 0; i < 4; ++i) { + if (this->corners[i]) { + delete this->corners[i]; + this->corners[i] = NULL; + } + } +} + +void Box3DFace::set_corners(NR::Point &A, NR::Point &B, NR::Point &C, NR::Point &D) +{ + corners[0] = &A; + corners[1] = &B; + corners[2] = &C; + corners[3] = &D; +} + +/*** void Box3DFace::set_shape(NR::Point const ul, NR::Point const lr, Box3D::Axis const dir1, Box3D::Axis const dir2, unsigned int shift_count, NR::Maybe pt_align, bool align_along_PL) @@ -68,6 +92,7 @@ void Box3DFace::set_shape(NR::Point const ul, NR::Point const lr, corners[0] = tmp_pt; } } +***/ Box3DFace::Box3DFace(Box3DFace const &box3dface) { @@ -90,8 +115,8 @@ Box3DFace::Box3DFace(Box3DFace const &box3dface) void Box3DFace::set_face (NR::Point const A, NR::Point const C, Box3D::Axis const axis1, Box3D::Axis const axis2) { - corners[0] = A; - corners[2] = C; + *corners[0] = A; + *corners[2] = C; if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context())) return; SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context()); @@ -100,8 +125,8 @@ Box3DFace::set_face (NR::Point const A, NR::Point const C, Box3D::Axis const axi Box3D::PerspectiveLine line2 (C, axis2); NR::Maybe B = line1.intersect(line2); - Box3D::PerspectiveLine line3 (corners[0], axis2); - Box3D::PerspectiveLine line4 (corners[2], axis1); + Box3D::PerspectiveLine line3 (*corners[0], axis2); + Box3D::PerspectiveLine line4 (*corners[2], axis1); NR::Maybe D = line3.intersect(line4); // FIXME: How to handle the case if one of the intersections doesn't exist? @@ -109,8 +134,8 @@ Box3DFace::set_face (NR::Point const A, NR::Point const C, Box3D::Axis const axi if (!D) D = NR::Point(0.0, 0.0); if (!B) B = NR::Point(0.0, 0.0); - corners[1] = *B; - corners[3] = *D; + *corners[1] = *B; + *corners[3] = *D; this->dir1 = axis1; this->dir2 = axis2; @@ -119,7 +144,7 @@ Box3DFace::set_face (NR::Point const A, NR::Point const C, Box3D::Axis const axi NR::Point Box3DFace::operator[](unsigned int i) { - return corners[i % 4]; + return *corners[i % 4]; } /** @@ -156,10 +181,10 @@ void Box3DFace::set_curve() return; } SPCurve *curve = sp_curve_new(); - sp_curve_moveto(curve, corners[0][NR::X], height - corners[0][NR::Y]); - sp_curve_lineto(curve, corners[1][NR::X], height - corners[1][NR::Y]); - sp_curve_lineto(curve, corners[2][NR::X], height - corners[2][NR::Y]); - sp_curve_lineto(curve, corners[3][NR::X], height - corners[3][NR::Y]); + sp_curve_moveto(curve, (*corners[0])[NR::X], height - (*corners[0])[NR::Y]); + sp_curve_lineto(curve, (*corners[1])[NR::X], height - (*corners[1])[NR::Y]); + sp_curve_lineto(curve, (*corners[2])[NR::X], height - (*corners[2])[NR::Y]); + sp_curve_lineto(curve, (*corners[3])[NR::X], height - (*corners[3])[NR::Y]); sp_curve_closepath(curve); sp_shape_set_curve(SP_SHAPE(this->path), curve, true); sp_curve_unref(curve); @@ -172,10 +197,10 @@ gchar * Box3DFace::svg_repr_string() GString *pstring = g_string_new(""); g_string_sprintf (pstring, "M %f,%f L %f,%f L %f,%f L %f,%f z", - corners[0][NR::X], height - corners[0][NR::Y], - corners[1][NR::X], height - corners[1][NR::Y], - corners[2][NR::X], height - corners[2][NR::Y], - corners[3][NR::X], height - corners[3][NR::Y]); + (*corners[0])[NR::X], height - (*corners[0])[NR::Y], + (*corners[1])[NR::X], height - (*corners[1])[NR::Y], + (*corners[2])[NR::X], height - (*corners[2])[NR::Y], + (*corners[3])[NR::X], height - (*corners[3])[NR::Y]); return pstring->str; } diff --git a/src/box3d-face.h b/src/box3d-face.h index e557aebf2..3b96937b6 100644 --- a/src/box3d-face.h +++ b/src/box3d-face.h @@ -26,17 +26,23 @@ class SP3DBox; class Box3DFace { public: Box3DFace(SP3DBox *box3d); + Box3DFace(SP3DBox *box3d, NR::Point &A, NR::Point &B, NR::Point &C, NR::Point &D); //Box3DFace(SP3DBox *box3d, NR::Point const ul, NR::Point const lr, // Box3D::Axis const dir1, Box3D::Axis const dir2, // unsigned int shift_count = 0, NR::Maybe pt_align = NR::Nothing(), bool align_along_PL = false); Box3DFace(Box3DFace const &box3dface); + ~Box3DFace(); + NR::Point operator[](unsigned int i); void draw(SP3DBox *box3d, SPCurve *c); + /*** void set_shape(NR::Point const ul, NR::Point const lr, Box3D::Axis const dir1, Box3D::Axis const dir2, unsigned int shift_count = 0, NR::Maybe pt_align = NR::Nothing(), bool align_along_PL = false); + ***/ + void set_corners (NR::Point &A, NR::Point &B, NR::Point &C, NR::Point &D); void set_face (NR::Point const A, NR::Point const C, Box3D::Axis const dir1, Box3D::Axis const dir2); void hook_path_to_3dbox(); @@ -45,7 +51,7 @@ public: gchar * svg_repr_string(); private: - NR::Point corners[4]; + NR::Point *corners[4]; Box3D::Axis dir1; Box3D::Axis dir2; diff --git a/src/box3d.cpp b/src/box3d.cpp index 290b5111e..258c65879 100644 --- a/src/box3d.cpp +++ b/src/box3d.cpp @@ -61,7 +61,7 @@ static void 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); @@ -76,7 +76,6 @@ sp_3dbox_class_init(SP3DBoxClass *klass) static void sp_3dbox_init(SP3DBox *box3d) { - if (box3d == NULL) { g_warning ("box3d is NULL!\n"); } box3d->faces[4] = new Box3DFace (box3d); //box3d->faces[4]->hook_path_to_3dbox(); for (int i = 0; i < 6; ++i) { @@ -163,8 +162,6 @@ static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node box3d->faces[4]->set_path_repr(); if (bc->extruded) { - NR::Point corner1, corner2, corner3, corner4; - sp_3dbox_compute_specific_corners (bc, corner1, corner2, corner3, corner4); for (int i = 0; i < 6; ++i) { if (i == 4) continue; box3d->faces[i]->set_path_repr(); @@ -211,61 +208,87 @@ sp_3dbox_set_shape(SP3DBox *box3d) return; SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context()); - // FIXME: Why must the coordinates be flipped vertically??? - //SPDocument *doc = SP_OBJECT_DOCUMENT(box3d); - //gdouble height = sp_document_height(doc); - - /* Curve-adaption variant: */ - NR::Point corner1, corner2, corner3, corner4; - corner1 = bc->drag_origin; - + /* Only update the curves during dragging; setting the svg representations + is expensive and only done once at the end */ + sp_3dbox_recompute_corners (box3d, bc->drag_origin, bc->drag_ptB, bc->drag_ptC); if (bc->extruded) { - sp_3dbox_compute_specific_corners (bc, corner1, corner2, corner3, corner4); - for (int i=0; i < 6; ++i) { - if (!box3d->faces[i]) { - g_warning ("Face no. %d does not exist!\n", i); - return; - } - } - box3d->faces[0]->set_face (bc->drag_origin, corner4, Box3D::Z, Box3D::Y); - box3d->faces[0]->set_curve(); - box3d->faces[5]->set_face (corner3, bc->drag_ptC, Box3D::Y, Box3D::X); - box3d->faces[5]->set_curve(); - box3d->faces[3]->set_face (bc->drag_origin, corner2, Box3D::X, Box3D::Z); - box3d->faces[3]->set_curve(); - box3d->faces[2]->set_face (bc->drag_ptB, corner4, Box3D::X, Box3D::Z); - box3d->faces[2]->set_curve(); - - box3d->faces[1]->set_face (corner1, bc->drag_ptC, Box3D::Z, Box3D::Y); - box3d->faces[1]->set_curve(); + box3d->faces[0]->set_corners (box3d->corners[0], box3d->corners[4], box3d->corners[6], box3d->corners[2]); + box3d->faces[1]->set_corners (box3d->corners[1], box3d->corners[5], box3d->corners[7], box3d->corners[3]); + box3d->faces[2]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[5], box3d->corners[4]); + box3d->faces[3]->set_corners (box3d->corners[2], box3d->corners[3], box3d->corners[7], box3d->corners[6]); + box3d->faces[5]->set_corners (box3d->corners[4], box3d->corners[5], box3d->corners[7], box3d->corners[6]); } - box3d->faces[4]->set_face(bc->drag_origin, bc->drag_ptB, Box3D::Y, Box3D::X); - box3d->faces[4]->set_curve(); + box3d->faces[4]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[3], box3d->corners[2]); + + sp_3dbox_update_curves (box3d); +} + + +void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C) +{ + 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); } +void +sp_3dbox_update_curves (SP3DBox *box) { + for (int i = 0; i < 6; ++i) { + if (box->faces[i]) box->faces[i]->set_curve(); + } +} void -sp_3dbox_compute_specific_corners (SP3DBoxContext *box3d_context, NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4) +sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt) { - // TODO: Check for numerical stability and handle "wrong" cases more gracefully. - // (This now mostly applies to the intersection code in the PerspectiveLine class) - Box3D::PerspectiveLine pl1 (box3d_context->drag_origin, Box3D::X); - Box3D::PerspectiveLine pl2 (box3d_context->drag_ptB, Box3D::Y); - corner1 = pl1.meet(pl2); - - Box3D::PerspectiveLine pl3 (corner1, Box3D::Z); - Box3D::PerspectiveLine pl4 (box3d_context->drag_ptC, Box3D::Y); - corner2 = pl3.meet(pl4); - - Box3D::PerspectiveLine pl5 (corner2, Box3D::X); - Box3D::PerspectiveLine pl6 (box3d_context->drag_origin, Box3D::Z); - corner3 = pl5.meet(pl6); - - Box3D::PerspectiveLine pl7 (box3d_context->drag_ptC, Box3D::X); - Box3D::PerspectiveLine pl8 (corner3, Box3D::Y); - corner4 = pl7.meet(pl8); + NR::Point A (box->corners[id ^ Box3D::XY]); + + /* set the 'front' corners */ + box->corners[id] = pt; + + Box3D::PerspectiveLine pl_one (A, Box3D::Y); + Box3D::PerspectiveLine pl_two (pt, Box3D::X); + box->corners[id ^ Box3D::X] = pl_one.meet(pl_two); + + pl_one = Box3D::PerspectiveLine (A, Box3D::X); + pl_two = Box3D::PerspectiveLine (pt, Box3D::Y); + box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two); + + /* set the 'rear' corners */ + NR::Point B (box->corners[id ^ Box3D::XYZ]); + + pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z); + pl_two = Box3D::PerspectiveLine (B, Box3D::Y); + box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two); + + pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X); + pl_two = Box3D::PerspectiveLine (pt, Box3D::Z); + box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two); + + pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y); + pl_two = Box3D::PerspectiveLine (B, Box3D::X); + box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two); + } +void +sp_3dbox_move_corner_in_constrained_Z_direction (SP3DBox *box, guint id, NR::Point pt) +{ + /* set the four corners of the face containing corners[id] */ + box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt); + + Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X); + Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z); + box->corners[id ^ Box3D::X] = pl_one.meet(pl_two); + + pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y); + pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z); + box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two); + + pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y); + pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z); + box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two); +} /* diff --git a/src/box3d.h b/src/box3d.h index 982596d8f..1fb8280ee 100644 --- a/src/box3d.h +++ b/src/box3d.h @@ -34,6 +34,7 @@ struct SP3DBox : public SPGroup { + NR::Point corners[8]; Box3DFace *faces[6]; }; @@ -44,6 +45,11 @@ struct SP3DBoxClass { GType sp_3dbox_get_type (void); void sp_3dbox_position_set (SP3DBoxContext &bc); -void sp_3dbox_compute_specific_corners (SP3DBoxContext *box3d_context, NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4); +void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const pt1, NR::Point const pt2, NR::Point const pt3); +void sp_3dbox_update_curves (SP3DBox *box); +void sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt); +void sp_3dbox_move_corner_in_constrained_Z_direction (SP3DBox *box, guint id, NR::Point pt); + +inline NR::Point sp_3dbox_get_corner (SP3DBox *box, guint id) { return box->corners[id]; } #endif diff --git a/src/vanishing-point.h b/src/vanishing-point.h index 3be5d085e..b94193adc 100644 --- a/src/vanishing-point.h +++ b/src/vanishing-point.h @@ -28,6 +28,10 @@ enum Axis { X = 1, Y = 2, Z = 4, + XY = 3, + XZ = 5, + YZ = 6, + XYZ = 7, NONE = 0 };