~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/stelutils/TreeGrid.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090313200722-l66s4zy2s3e8up0s
Tags: 0.10.2-1
New upstream release

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
 
//#define TREEGRIDDEBUG 1
24
 
 
25
 
#include <vector>
26
 
#include "Grid.hpp"
27
 
 
28
 
struct TreeGridNode
29
 
{
30
 
    TreeGridNode() {}
31
 
    TreeGridNode(const StelGeom::ConvexPolygon& s) : triangle(s) {}
32
 
        
33
 
        typedef std::vector<GridObject*> Objects;
34
 
    Objects objects;
35
 
    
36
 
    StelGeom::ConvexPolygon triangle;
37
 
    
38
 
    typedef std::vector<TreeGridNode> Children;
39
 
    Children children;
40
 
    
41
 
#ifdef TREEGRIDDEBUG
42
 
        double draw(class Projector *prj, const StelGeom::ConvexS&, float opacity = 1.) const;
43
 
#endif
44
 
};
45
 
 
46
 
class TreeGrid : public Grid, public TreeGridNode
47
 
{
48
 
public:
49
 
    TreeGrid(unsigned int maxobj = 1000);
50
 
    virtual ~TreeGrid();
51
 
    
52
 
        void insert(GridObject* obj)
53
 
    {
54
 
        insert(obj, *this);
55
 
    }
56
 
    
57
 
    template<class Shape>
58
 
    void filterIntersect(const Shape& s);
59
 
    
60
 
    unsigned int depth() const
61
 
    { return depth(*this); }
62
 
    
63
 
        //! Get all the objects loaded into the grid structure
64
 
        //! This is quite slow and should not used for time critical operations
65
 
        virtual std::vector<GridObject*> getAllObjects();
66
 
        
67
 
private:
68
 
    
69
 
        void insert(GridObject* obj, TreeGridNode& node);
70
 
    void split(TreeGridNode& node);
71
 
    
72
 
    template<class S>
73
 
    void fillIntersect(const S& s, const TreeGridNode& node, Grid& grid) const;    
74
 
    
75
 
    void fillAll(const TreeGridNode& node, Grid& grid) const;
76
 
        void fillAll(const TreeGridNode& node, std::vector<GridObject*>& result) const;
77
 
    unsigned int depth(const TreeGridNode& node) const;
78
 
    
79
 
    unsigned int maxObjects;
80
 
    
81
 
    // The last filter
82
 
    ConvexS filter;
83
 
};
84
 
 
85
 
 
86
 
template<class S>
87
 
void TreeGrid::fillIntersect(const S& s, const TreeGridNode& node, Grid& grid) const
88
 
{
89
 
    for (TreeGridNode::Objects::const_iterator io = node.objects.begin(); io != node.objects.end(); ++io)
90
 
    {
91
 
                if (intersect(s, (*io)->getPositionForGrid()))
92
 
        {
93
 
            grid.insertResult(*io);
94
 
        }
95
 
    }
96
 
    for (Children::const_iterator ic = node.children.begin(); ic != node.children.end(); ++ic)
97
 
    {
98
 
        if (contains(s, ic->triangle))
99
 
        {
100
 
            fillAll(*ic, grid);
101
 
        }
102
 
        else
103
 
                {
104
 
                        if(intersect(s, ic->triangle))
105
 
                {
106
 
                fillIntersect(s, *ic, grid);
107
 
                }
108
 
                }
109
 
    }
110
 
}
111
 
 
112
 
template<class Shape>
113
 
struct NotIntersectPred
114
 
{
115
 
    Shape shape;
116
 
    
117
 
    NotIntersectPred(const Shape& s) : shape(s) {}
118
 
        bool operator() (const GridObject* obj) const
119
 
    {
120
 
                return !intersect(shape, obj->getPositionForGrid());
121
 
    }
122
 
};
123
 
 
124
 
template<class Shape>
125
 
void TreeGrid::filterIntersect(const Shape& s)
126
 
{
127
 
    // first we remove all the objects that are not in the disk
128
 
//     this->remove_if(NotIntersectPred<Shape>(s));
129
 
//     // now we add all the objects that are in the disk, but not in the old disk
130
 
//     fillIntersect(Difference<Shape, ConvexS>(s, filter), *this, *this);
131
 
    this->clear();
132
 
    fillIntersect(s, *this, *this);
133
 
    
134
 
    //filter = ConvexS(s);
135
 
}
136
 
 
137
 
 
138
 
#endif // _TREEGRID_HPP_
139