summaryrefslogtreecommitdiffstats
path: root/src/perspective3d.cpp
blob: 678b695d0b31ffe8ea4dbd82f624c54e87442f2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#define __PERSPECTIVE3D_C__

/*
 * Class modelling a 3D perspective
 *
 * Authors:
 *   Maximilian Albert <Anhalter42@gmx.de>
 *
 * Copyright (C) 2007 authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "box3d-context.h"
#include "perspective-line.h"
#include <iostream>

namespace Box3D {

/**
 * Computes the intersection of the two perspective lines from pt1 and pt2 to the respective
 * vanishing points in the given directions.
 */
// FIXME: This has been moved to a virtual method inside PerspectiveLine; can probably be purged
NR::Point perspective_intersection (NR::Point pt1, Box3D::PerspDir dir1, NR::Point pt2, Box3D::PerspDir dir2)
{
    VanishingPoint const *vp1 = SP3DBoxContext::current_perspective->get_vanishing_point(dir1);
    VanishingPoint const *vp2 = SP3DBoxContext::current_perspective->get_vanishing_point(dir2);
    NR::Maybe<NR::Point> meet = Line(pt1, *vp1).intersect(Line(pt2, *vp2));
    // FIXME: How to handle parallel lines (also depends on the type of the VPs)?
    if (!meet) { meet = NR::Point (0.0, 0.0); }
    return *meet;
}

/**
 * Find the point on the perspective line from line_pt to the
 * vanishing point in direction dir that is closest to ext_pt.
 */
NR::Point perspective_line_snap (NR::Point line_pt, PerspDir dir, NR::Point ext_pt)
{
    return PerspectiveLine(line_pt, dir).closest_to(ext_pt);
}  

Perspective3D::Perspective3D (VanishingPoint const &pt_x, VanishingPoint const &pt_y, VanishingPoint const &pt_z)
        : vp_x (pt_x),
          vp_y (pt_y),
          vp_z (pt_z)
{
	// Draw the three vanishing points
    vp_x.draw(X);
    vp_y.draw(Y);
    vp_z.draw(Z);
}

VanishingPoint *Perspective3D::get_vanishing_point (PerspDir const dir)
{
    // FIXME: Also handle value 'NONE' in switch
    switch (dir) {
        case X:
            return &vp_x;
            break;
        case Y:
            return &vp_y;
            break;
        case Z:
            return &vp_z;
            break;
    }
}

void Perspective3D::set_vanishing_point (PerspDir const dir, VanishingPoint const &pt)
{
    // FIXME: Also handle value 'NONE' in switch
    switch (dir) {
        case X:
            vp_x = pt;
            break;
        case Y:
            vp_y = pt;
            break;
        case Z:
            vp_z = pt;
            break;
    }
}

} // 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 :