~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/TreeGrid.hpp

Tags: upstream-0.9.0
Import upstream version 0.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2007 Guillaume Chereau
 
4
 * 
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
 
 
20
#ifndef _TREEGRID_HPP_
 
21
#define _TREEGRID_HPP_
 
22
 
 
23
#include <list>
 
24
#include <vector>
 
25
 
 
26
#include "spgrid.hpp"
 
27
#include "SphereGeometry.hpp"
 
28
#include "Navigator.hpp"
 
29
 
 
30
template<class Obj>
 
31
class TreeGrid;
 
32
 
 
33
using namespace StelGeom;
 
34
 
 
35
//! This a convenient class that contains the methods of StelGridPolicy<T>
 
36
//! that don't depend of the template type, so we can put them in the .cpp file
 
37
class TreeGridPolicyBase
 
38
{
 
39
public:
 
40
    //! This method is called when we need to split a triangle
 
41
    //! into smaller sub triangles
 
42
    std::list<const ConvexPolygon*> split(const Polygon* p);
 
43
};
 
44
 
 
45
//! This class define the way the StelGrid<Obj> should behave
 
46
template<class Obj>
 
47
class TreeGridPolicy : public TreeGridPolicyBase
 
48
{
 
49
public:
 
50
    typedef SPGrid<Obj*, const ConvexPolygon*, TreeGridPolicy<Obj> > Grid_t;
 
51
 
 
52
    //! Constructor
 
53
    TreeGridPolicy(TreeGrid<Obj>* grid) : _grid(grid)
 
54
    {}
 
55
 
 
56
    // The contains method for normal shapes
 
57
    template<class T1, class T2>
 
58
    bool contains(const T1* s1 ,const T2* s2) const
 
59
    {
 
60
        return ::contains(*s1, *s2);
 
61
    }
 
62
    
 
63
    // Now for the objects
 
64
    template<class T>
 
65
    bool contains(const T* s, const Obj* o) const
 
66
    {
 
67
        // TODO: use the FOV information,
 
68
        // Because it is not correct to consider the object as a point
 
69
        return ::contains(*s, getObsJ2000Pos(o));
 
70
    }
 
71
    
 
72
    // The intersect method for normal shapes
 
73
    template<class T1, class T2>
 
74
    bool intersect(const T1* s1, const T2* s2) const
 
75
    {
 
76
        return ::intersect(*s1, *s2);
 
77
    }
 
78
 
 
79
    // Now for StelObjects
 
80
    template<class T>
 
81
    bool intersect(const T* s, const Obj* o) const
 
82
    {
 
83
        return ::contains(*s, getObsJ2000Pos(o));
 
84
    }
 
85
    
 
86
    bool split_cond(const typename Grid_t::node_t& node) const
 
87
    {
 
88
        return node.children().empty() && 
 
89
               node.objects().size() > 1000;
 
90
    }
 
91
    
 
92
private:
 
93
    Vec3d getObsJ2000Pos(const Obj* o) const
 
94
    {
 
95
        return o->getObsJ2000Pos(_grid->_nav);
 
96
    }
 
97
    
 
98
private:
 
99
    // The TreeGrid object :
 
100
    TreeGrid<Obj>* _grid;
 
101
 
 
102
};
 
103
 
 
104
class TreeGridBase
 
105
{
 
106
public:
 
107
    // destructor
 
108
    virtual ~TreeGridBase();
 
109
protected:
 
110
    //! The only empty ConvexPolygon
 
111
    static const ConvexPolygon _empty_convex;
 
112
    
 
113
    std::list<const ConvexPolygon*> create_tetrahedron() const;
 
114
    
 
115
    //! All the shapes
 
116
    std::vector<const ConvexPolygon*> _shapes;
 
117
};
 
118
 
 
119
//! The TreeGrid can be used to store Obj in a optimized way
 
120
template<class Obj>
 
121
class TreeGrid : public SPGrid<Obj*, const ConvexPolygon*, TreeGridPolicy<Obj> >, public TreeGridBase
 
122
{
 
123
    friend class TreeGridPolicy<Obj>;
 
124
public:
 
125
    //! Constructor
 
126
    TreeGrid(const Navigator* nav = NULL) :
 
127
        SPGrid<Obj*, const ConvexPolygon*, TreeGridPolicy<Obj> >(
 
128
            &_empty_convex, _policy
 
129
        ),
 
130
        _policy(TreeGridPolicy<Obj>(this))
 
131
    {
 
132
        // We insert the initial tetrahedron
 
133
        typedef std::list<const ConvexPolygon*> Tetrahedron;
 
134
        Tetrahedron tetrahedron = create_tetrahedron();
 
135
        for (Tetrahedron::const_iterator i = tetrahedron.begin();
 
136
                i != tetrahedron.end(); ++i) {
 
137
            _shapes.push_back(*i);
 
138
            this->root_node().children().push_back(typename TreeGrid<Obj>::node_t(*i));
 
139
        }
 
140
    }
 
141
    
 
142
private:
 
143
    //! The navigator
 
144
    // We need it to get the StelObject positions
 
145
    // TODO: why do we need it to get the position ???
 
146
    const Navigator* _nav;
 
147
    
 
148
    //! The grid policy
 
149
    TreeGridPolicy<Obj> _policy;
 
150
    
 
151
};
 
152
 
 
153
 
 
154
#endif // _TREEGRID_HPP_