File: grid_static_ptr2d.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (186 lines) | stat: -rw-r--r-- 6,393 bytes parent folder | download | duplicates (3)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef __VCGLIB_UGRID_2D
#define __VCGLIB_UGRID_2D

#include <vector>
#include <algorithm>
#include <stdio.h>

#include <vcg/space/box2.h>
#include <vcg/space/line2.h>
#include <vcg/space/index/grid_util2d.h>
#include <vcg/space/index/grid_closest2d.h>

namespace vcg {

template < class OBJTYPE, class FLT=float >
class GridStaticPtr2D: public BasicGrid2D<FLT>, SpatialIndex2D<OBJTYPE,FLT>
{
public:
    typedef OBJTYPE ObjType;
    typedef ObjType* ObjPtr;
    typedef typename ObjType::ScalarType ScalarType;
    typedef Point2<ScalarType> CoordType;
    typedef Box2<ScalarType> Box2x;
    typedef GridStaticPtr2D<OBJTYPE,FLT> MyGridType;
    typedef typename std::vector<OBJTYPE*>::iterator CellIterator;
    std::vector<std::vector<std::vector<OBJTYPE*> > >  data;

private:



    void Add( const int x,
              const int y,
              ObjType *elem)
    {
        assert((x>=0)&&(x<(int)data.size()));
        assert((y>=0)&&(y<(int)data[x].size()));
        data[x][y].push_back(elem);
    }

    template <class OBJITER>
    inline void Set(const OBJITER & _oBegin,
                     const OBJITER & _oEnd,
                     const Box2x &_bbox,
                     Point2i _siz)
    {
        this->bbox=_bbox;
        this->siz=_siz;

        // find voxel size starting from the provided bbox and grid size.

        this->dim  = this->bbox.max - this->bbox.min;
        this->voxel.X() = (this->dim.X()/(ScalarType)this->siz.X());
        this->voxel.Y() = (this->dim.Y()/(ScalarType)this->siz.Y());

        ///allocate space
        data.resize(this->siz.X());
        for (size_t x=0;x<data.size();x++)
            data[x].resize(this->siz.Y());


        OBJITER IteObj;
        for (IteObj=_oBegin;IteObj!=_oEnd;IteObj++)
        {
            Box2<ScalarType> Box2D;
            (*IteObj).GetBBox(Box2D);

            //get index of intersected cells
            Point2i minIndex=this->GridP(Box2D.min);
            Point2i maxIndex=this->GridP(Box2D.max);

            for (int x=minIndex.X();x<=maxIndex.X();x++)
                for (int y=minIndex.Y();y<=maxIndex.Y();y++)
                    Add(x,y,&(*IteObj));
        }
    }

    template <class OBJITER>
    inline void Set(const OBJITER & _oBegin,
                    const OBJITER & _oEnd,
                    const Box2x &_bbox)
    {
      int _size=(int)std::distance<OBJITER>(_oBegin,_oEnd);

      Point2<FLT> _dim = _bbox.max - _bbox.min;

      Point2i _siz;

      BestDim2D( _size, _dim, _siz );

      Set(_oBegin,_oEnd,_bbox,_siz);
    }

public:

    void Grid( const int x, const int y,
               CellIterator & first,
               CellIterator & last )
    {
      first = data[x][y].begin();
      last  = data[x][y].end();
    }

    void Grid( const Point2i pi,
               CellIterator & first,
               CellIterator & last )
    {
        return Grid(pi.X(),pi.Y(),first,last);
    }

    template <class OBJITER>
    inline void Set(const OBJITER & _oBegin,
                    const OBJITER & _oEnd)
    {
      Box2x bbox,ibbox;

      OBJITER IteObj;
      for (IteObj=_oBegin;IteObj!=_oEnd;IteObj++)
      {
        (*IteObj).GetBBox(ibbox);
        bbox.Add(ibbox);
      }
      
      ScalarType diag=bbox.Diag();
      bbox.Offset(diag*0.01);
      Set<OBJITER>(_oBegin,_oEnd,bbox);
    }

    template <class OBJPOINTDISTFUNCTOR, class OBJMARKER>
    ObjPtr  GetClosest(OBJPOINTDISTFUNCTOR & _getPointDistance,
                       OBJMARKER & _marker,
                       const typename OBJPOINTDISTFUNCTOR::QueryType & _p,
                       const ScalarType & _maxDist,
                       ScalarType & _minDist,
                       CoordType & _closestPt)
    {
        return (vcg::GridClosest2D<MyGridType,OBJPOINTDISTFUNCTOR,OBJMARKER>(*this,_getPointDistance,_marker, _p,_maxDist,_minDist,_closestPt));
    }

    template <class OBJMARKER, class OBJPTRCONTAINER>
    unsigned int GetInBox(OBJMARKER & _marker,
                          const vcg::Box2<ScalarType> _bbox,
                          OBJPTRCONTAINER & _objectPtrs)
    {
        return(vcg::GridGetInBox2D<MyGridType,OBJMARKER,OBJPTRCONTAINER>
               (*this,_marker,_bbox,_objectPtrs));
    }


    template <class OBJRAYISECTFUNCTOR, class OBJMARKER>
    ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker,
                 const Ray2<ScalarType> & _ray, const ScalarType & _maxDist,
                 ScalarType & _t)
    {
        return(vcg::GridDoRay2D<MyGridType,OBJRAYISECTFUNCTOR,OBJMARKER>(*this,_rayIntersector,_marker,_ray,_maxDist,_t));
    }


}; //end class GridStaticPtr_2D

} // end namespace

#endif