Code

more unreffing temporary styles properly
[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::PerspDir dir1, NR::Point pt2, Box3D::PerspDir 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, PerspDir 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 (PerspDir 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 (PerspDir const dir, VanishingPoint const &pt)
72 {
73     // FIXME: Also handle value 'NONE' in switch
74     switch (dir) {
75         case X:
76             vp_x = pt;
77             break;
78         case Y:
79             vp_y = pt;
80             break;
81         case Z:
82             vp_z = pt;
83             break;
84     }
85 }
87 } // namespace Box3D 
88  
89 /*
90   Local Variables:
91   mode:c++
92   c-file-style:"stroustrup"
93   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
94   indent-tabs-mode:nil
95   fill-column:99
96   End:
97 */
98 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :