3
* Copyright (C) 2007 Guillaume Chereau
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.
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.
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.
20
#ifndef _TREEGRID_HPP_
21
#define _TREEGRID_HPP_
27
#include "SphereGeometry.hpp"
28
#include "Navigator.hpp"
33
using namespace StelGeom;
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
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);
45
//! This class define the way the StelGrid<Obj> should behave
47
class TreeGridPolicy : public TreeGridPolicyBase
50
typedef SPGrid<Obj*, const ConvexPolygon*, TreeGridPolicy<Obj> > Grid_t;
53
TreeGridPolicy(TreeGrid<Obj>* grid) : _grid(grid)
56
// The contains method for normal shapes
57
template<class T1, class T2>
58
bool contains(const T1* s1 ,const T2* s2) const
60
return ::contains(*s1, *s2);
63
// Now for the objects
65
bool contains(const T* s, const Obj* o) const
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));
72
// The intersect method for normal shapes
73
template<class T1, class T2>
74
bool intersect(const T1* s1, const T2* s2) const
76
return ::intersect(*s1, *s2);
79
// Now for StelObjects
81
bool intersect(const T* s, const Obj* o) const
83
return ::contains(*s, getObsJ2000Pos(o));
86
bool split_cond(const typename Grid_t::node_t& node) const
88
return node.children().empty() &&
89
node.objects().size() > 1000;
93
Vec3d getObsJ2000Pos(const Obj* o) const
95
return o->getObsJ2000Pos(_grid->_nav);
99
// The TreeGrid object :
100
TreeGrid<Obj>* _grid;
108
virtual ~TreeGridBase();
110
//! The only empty ConvexPolygon
111
static const ConvexPolygon _empty_convex;
113
std::list<const ConvexPolygon*> create_tetrahedron() const;
116
std::vector<const ConvexPolygon*> _shapes;
119
//! The TreeGrid can be used to store Obj in a optimized way
121
class TreeGrid : public SPGrid<Obj*, const ConvexPolygon*, TreeGridPolicy<Obj> >, public TreeGridBase
123
friend class TreeGridPolicy<Obj>;
126
TreeGrid(const Navigator* nav = NULL) :
127
SPGrid<Obj*, const ConvexPolygon*, TreeGridPolicy<Obj> >(
128
&_empty_convex, _policy
130
_policy(TreeGridPolicy<Obj>(this))
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));
144
// We need it to get the StelObject positions
145
// TODO: why do we need it to get the position ???
146
const Navigator* _nav;
149
TreeGridPolicy<Obj> _policy;
154
#endif // _TREEGRID_HPP_