source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/geom/ProxGrid.cxx @ 1025

Revision 1025, 2.1 KB checked in by gumbau, 18 years ago (diff)

namespace simplif

Line 
1//#include <iostream.h>
2#include <gfx/std.h>
3#include <gfx/math/Vec3.h>
4#include <gfx/tools/Buffer.h>
5#include <gfx/tools/Array3.h>
6#include <gfx/geom/ProxGrid.h>
7
8using namespace simplif;
9
10ProxGrid::ProxGrid(const Vec3& lo, const Vec3& hi, real dist)
11{
12    cellsize = dist;
13    cellsize2 = dist*dist;
14
15    min = lo;
16    max = hi;
17
18    xdiv = (int)ceil((max[X] - min[X])/dist);
19    ydiv = (int)ceil((max[Y] - min[Y])/dist);
20    zdiv = (int)ceil((max[Z] - min[Z])/dist);
21
22    cells.init(xdiv, ydiv, zdiv);
23}
24
25
26void ProxGrid::cell_for_point(const Vec3& v,int *i_out,int *j_out,int *k_out)
27{
28    int i = (int)floor((v[X] - min[X]) / cellsize);
29    int j = (int)floor((v[Y] - min[Y]) / cellsize);
30    int k = (int)floor((v[Z] - min[Z]) / cellsize);
31   
32    // In case we hit the max bounds
33    if( i==xdiv ) i--;
34    if( j==ydiv ) j--;
35    if( k==zdiv ) k--;
36 
37
38    *i_out = i;
39    *j_out = j;
40    *k_out = k;
41}
42
43void ProxGrid::addPoint(Vec3 *v)
44{
45    int i, j, k;
46    cell_for_point(*v, &i, &j, &k);
47    ProxGrid_Cell& cell = cells(i,j,k);
48
49    cell.add(v);
50}
51
52void ProxGrid::removePoint(Vec3 *v)
53{
54    int i, j, k;
55    cell_for_point(*v, &i, &j, &k);
56    ProxGrid_Cell& cell = cells(i,j,k);
57
58
59    int index = cell.find(v);
60    if( index >= 0 )
61        cell.remove(index);
62    else
63                std::cerr << "WARNING: ProxGrid -- removing non-member point." << std::endl;
64}
65
66
67
68void ProxGrid::maybe_collect_points(Vec3 *v, buffer<Vec3 *>& close,
69                                    ProxGrid_Cell& cell)
70{
71    for(int i=0; i<cell.length(); i++)
72    {
73        Vec3 *u = cell(i);
74
75        if( u!=v && norm2(*u - *v) < cellsize2 )
76            close.add(u);
77    }
78}
79
80
81
82void ProxGrid::proximalPoints(Vec3 *v, buffer<Vec3 *>& close)
83{
84    int i, j, k;
85    cell_for_point(*v, &i, &j, &k);
86
87    for(int dk=-1; dk<2; dk++)
88        for(int dj=-1; dj<2; dj++)
89            for(int di=-1; di<2; di++)
90            {
91                if( i+di>=0 && j+dj>=0 && k+dk>=0
92                    && i+di<cells.width()
93                    && j+dj<cells.height()
94                    && k+dk<cells.depth() )
95                {
96                    maybe_collect_points(v, close, cells(i+di, j+dj, k+dk));
97                }
98            }
99}
Note: See TracBrowser for help on using the repository browser.