Code

Filter effects dialog:
[inkscape.git] / src / perspective3d.cpp
1 #define __PERSPECTIVE3D_C__
3 /*
4  * Class modelling a 3D perspective
5  *
6  * Authors:
7  *   Maximilian Albert <Anhalter42@gmx.de>
8  *
9  * Copyright (C) 2007 authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "box3d-context.h"
15 #include "perspective-line.h"
16 #include <iostream>
18 namespace Box3D {
20 /**
21  * Computes the intersection of the two perspective lines from pt1 and pt2 to the respective
22  * vanishing points in the given directions.
23  */
24 // FIXME: This has been moved to a virtual method inside PerspectiveLine; can probably be purged
25 NR::Point perspective_intersection (NR::Point pt1, Box3D::Axis dir1, NR::Point pt2, Box3D::Axis dir2)
26 {
27     VanishingPoint const *vp1 = SP3DBoxContext::current_perspective->get_vanishing_point(dir1);
28     VanishingPoint const *vp2 = SP3DBoxContext::current_perspective->get_vanishing_point(dir2);
29     NR::Maybe<NR::Point> meet = Line(pt1, *vp1).intersect(Line(pt2, *vp2));
30     // FIXME: How to handle parallel lines (also depends on the type of the VPs)?
31     if (!meet) { meet = NR::Point (0.0, 0.0); }
32     return *meet;
33 }
35 /**
36  * Find the point on the perspective line from line_pt to the
37  * vanishing point in direction dir that is closest to ext_pt.
38  */
39 NR::Point perspective_line_snap (NR::Point line_pt, Box3D::Axis dir, NR::Point ext_pt)
40 {
41     return PerspectiveLine(line_pt, dir).closest_to(ext_pt);
42 }  
44 Perspective3D::Perspective3D (VanishingPoint const &pt_x, VanishingPoint const &pt_y, VanishingPoint const &pt_z)
45         : vp_x (pt_x),
46           vp_y (pt_y),
47           vp_z (pt_z)
48 {
49         // Draw the three vanishing points
50     vp_x.draw(X);
51     vp_y.draw(Y);
52     vp_z.draw(Z);
53 }
55 VanishingPoint *Perspective3D::get_vanishing_point (Box3D::Axis const dir)
56 {
57     // FIXME: Also handle value 'NONE' in switch
58     switch (dir) {
59         case X:
60             return &vp_x;
61             break;
62         case Y:
63             return &vp_y;
64             break;
65         case Z:
66             return &vp_z;
67             break;
68     }
69 }
71 void Perspective3D::set_vanishing_point (Box3D::Axis const dir, VanishingPoint const &pt)
72 {
73     switch (dir) {
74         case X:
75             vp_x = pt;
76             break;
77         case Y:
78             vp_y = pt;
79             break;
80         case Z:
81             vp_z = pt;
82             break;
83         case NONE:
84             // no vanishing point to set
85             break;
86     }
87 }
89 } // namespace Box3D 
90  
91 /*
92   Local Variables:
93   mode:c++
94   c-file-style:"stroustrup"
95   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
96   indent-tabs-mode:nil
97   fill-column:99
98   End:
99 */
100 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :