File: PointLocator.cxx

package info (click to toggle)
caret 5.6.4~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 31,904 kB
  • ctags: 28,901
  • sloc: cpp: 378,050; python: 6,718; ansic: 5,507; makefile: 333; sh: 46
file content (399 lines) | stat: -rw-r--r-- 9,982 bytes parent folder | download | duplicates (2)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

/*LICENSE_START*/
/*
 *  Copyright 1995-2002 Washington University School of Medicine
 *
 *  http://brainmap.wustl.edu
 *
 *  This file is part of CARET.
 *
 *  CARET 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.
 *
 *  CARET 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 for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CARET; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*LICENSE_END*/

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <set>

#include "MathUtilities.h"
#include "PointLocator.h"

/**
 * Constructor.
 */
PointLocator::PointLocator(const float boundsIn[6],
                           const int* numBucketsInEachAxis)
{
   for (int i = 0; i < 6; i++) {
      bounds[i] = boundsIn[i];
   }
   
   numX = 10;
   numY = 10;
   numZ = 10;
   
   if (numBucketsInEachAxis != NULL) {
      numX = numBucketsInEachAxis[0];
      numY = numBucketsInEachAxis[1];
      numZ = numBucketsInEachAxis[2];
   }
   
   totalNumBuckets = numX * numY * numZ;
   buckets = new Bucket[totalNumBuckets];
   
   sizeX = (bounds[1] - bounds[0]) / numX;
   sizeY = (bounds[3] - bounds[2]) / numY;
   sizeZ = (bounds[5] - bounds[4]) / numZ;
   
   for (int i = 0; i < numX; i++) {
      for (int j = 0; j < numY; j++) {
         for (int k = 0; k < numZ; k++) {
            const int ijk[3] = { i, j, k };
            const int bi = getBucketIndex(ijk);
            Bucket& b = buckets[bi];
            b.bounds[0] = i * sizeX + bounds[0];
            b.bounds[1] = bounds[0] + sizeX;
            b.bounds[2] = j * sizeY + bounds[2];
            b.bounds[3] = bounds[2] + sizeY;
            b.bounds[4] = k * sizeZ + bounds[4];
            b.bounds[5] = bounds[4] + sizeZ;
         }
      }
   }
   
   pointCounter = 0;
}

/**
 * Constructor.
 */
PointLocator::~PointLocator()
{
   if (buckets != NULL) {
      delete[] buckets;
   }
}

/**
 * add a point to the locator.
 */
void
PointLocator::addPoint(const float xyz[3], const int idIn)
{
   //
   // Determine which bucket point is within
   //
   int ijk[3];
   if (getBucket(xyz, ijk) == false) {
      std::cout << "PointLocator: point out of bounds" << std::endl;
      return;
   }
   
   //
   // Determine the one-dim bucket index
   //
   const int bucketIndex = getBucketIndex(ijk);
   if ((bucketIndex < 0) || (bucketIndex >= totalNumBuckets)) {
      std::cout << "PointLocator: point out of bounds" << std::endl;
      return;
   }
   
   //
   // Put point into bucket
   //
   int idNum = idIn;
   if (idNum < 0) {
      idNum = pointCounter;
   }
   pointCounter++;
   Point p(xyz, idNum);
   buckets[bucketIndex].points.push_back(p);
}

/**
 * get nearest point (returns -1 if not found)
 */
int 
PointLocator::getNearestPoint(const float xyz[3]) const
{
   //
   // Determine which bucket point is within
   //
   int ijk[3];
   if (getBucket(xyz, ijk) == false) {
      return -1;
   }
   
   //
   // Determine the one-dim bucket index
   //
   const int bucketIndex = getBucketIndex(ijk);
   if ((bucketIndex < 0) || (bucketIndex > totalNumBuckets)) {
      return -1;
   }
   
   //
   // Find nearest point in containing bucket
   //
   float nearbyDistSQ, nearbyXYZ[3];
   int nearbyPt = buckets[bucketIndex].getNearestPoint(xyz, nearbyDistSQ, nearbyXYZ);
   
   //
   // Keep track of buckets already searched
   //
   std::set<int> bucketsAlreadySearched;
   bucketsAlreadySearched.insert(bucketIndex);
   
   //
   // If bucket is empty, search nearby buckets and expand search if necessary
   //
   if (nearbyPt < 0) {
      bool done = false;
      int delta = 1;
      while (done == false) {
         //
         // Expand bucket search
         //
         const int minI = std::max(ijk[0] - delta, 0);
         const int maxI = std::min(ijk[0] + delta, numX - 1);
         const int minJ = std::max(ijk[1] - delta, 0);
         const int maxJ = std::min(ijk[1] + delta, numY - 1);
         const int minK = std::max(ijk[2] - delta, 0);
         const int maxK = std::min(ijk[2] + delta, numZ - 1);
         
         for (int i = minI; (i <= maxI) && (done == false); i++) {
            for (int j = minJ; (j <= maxJ) && (done == false); j++) {
               for (int k = minK; (k <= maxK) && (done == false); k++) {
                  const int ijk2[3] = { i, j, k };
                  const int indx = getBucketIndex(ijk2);
                  if (indx >= 0) {
                     nearbyPt = buckets[indx].getNearestPoint(xyz, nearbyDistSQ, nearbyXYZ);
                     if (nearbyPt >= 0) {
                        done = true;
                     }
                     bucketsAlreadySearched.insert(indx);
                  }
               }  // for k
            }  // for j
         } // for i
         
         //
         // have not found bucket yet ?
         //
         if (done == false) {
            delta++;
            //
            // Have all buckets been searched ?
            //
            if ((minI == 0) && (maxI == (numX - 1)) &&
                (minJ == 0) && (maxJ == (numY - 1)) &&
                (minK == 0) && (maxK == (numZ - 1))) {
               done = true;
            }
         }
      }
   }
   
   //
   // Found point in a bucket
   //
   if (nearbyPt >= 0) {
      const float dist = MathUtilities::distance3D(xyz, nearbyXYZ);
      const float minSearchXYZ[3] = {
         xyz[0] - dist, 
         xyz[1] - dist, 
         xyz[2] - dist
      };
      const float maxSearchXYZ[3] = {
         xyz[0] + dist, 
         xyz[1] + dist, 
         xyz[2] + dist
      };
      
      //
      // Minimum bucket to search
      //
      int minIJK[3];
      getBucket(minSearchXYZ, minIJK);
      limitIJK(minIJK);
      
      //
      // Maximum bucket to search
      //
      int maxIJK[3];
      getBucket(maxSearchXYZ, maxIJK);
      limitIJK(maxIJK);
      
      //
      // Search in the neighboring buckets
      //
      for (int i = minIJK[0]; i <= maxIJK[0]; i++) {
         for (int j = minIJK[1]; j <= maxIJK[1]; j++) {
            for (int k = minIJK[2]; k <= maxIJK[2]; k++) {
               const int ijk2[3] = { i, j, k };
               const int bi = getBucketIndex(ijk2);
               if (std::find(bucketsAlreadySearched.begin(), bucketsAlreadySearched.end(),
                             bi) == bucketsAlreadySearched.end()) {
                  const Bucket& b = buckets[bi];
                  float ptXYZ[3], dist;
                  const int pt = b.getNearestPoint(xyz, dist, ptXYZ);
                  if ((pt >= 0) && (dist < nearbyDistSQ)) {
                     nearbyDistSQ = dist;
                     nearbyPt     = pt;
                  }
               }
            }
         }
      }
      
      return nearbyPt;
   }
   return -1;
}      

/**
 * limit the IJK index to valid buckets.
 */
void 
PointLocator::limitIJK(int ijk[3]) const
{
   ijk[0] = std::max(0, ijk[0]);
   ijk[0] = std::min(numX - 1, ijk[0]);
   ijk[1] = std::max(0, ijk[1]);
   ijk[1] = std::min(numY - 1, ijk[1]);
   ijk[2] = std::max(0, ijk[2]);
   ijk[2] = std::min(numZ - 1, ijk[2]);
}
      
/**
 * get bucket for a point (returns false if not in a bucket - out of bounds).
 */
bool 
PointLocator::getBucket(const float xyz[3], int ijk[3]) const
{
   ijk[0] = static_cast<int>((xyz[0] - bounds[0]) / sizeX);
   ijk[1] = static_cast<int>((xyz[1] - bounds[2]) / sizeY);
   ijk[2] = static_cast<int>((xyz[2] - bounds[4]) / sizeZ);
   
   if ((ijk[0] < 0) || (ijk[0] >= numX) ||
       (ijk[1] < 0) || (ijk[1] >= numY) ||
       (ijk[2] < 0) || (ijk[2] >= numZ)) {
      return false;
   }
   
   
   return true;
}

/**
 * get bucket index.
 */
int 
PointLocator::getBucketIndex(const int ijk[3]) const
{
   const int indx = ijk[0] + ijk[1] * numX + ijk[2] * numX * numY;
   return indx;
}
      
/**
 * get one dimensional bucket index.
 */
int 
PointLocator::getBucketIndex(const float xyz[3]) const
{
   int ijk[3];
   if (getBucket(xyz, ijk) == false) {
      return -1;
   }
   return getBucketIndex(ijk);
}
      
//******************************************************************************

/**
 * Constructor. 
 */
PointLocator::Point::Point(const float xyzIn[3], const int idIn)
{
   xyz[0] = xyzIn[0];
   xyz[1] = xyzIn[1];
   xyz[2] = xyzIn[2];
   idNum  = idIn;
}

/**
 * Destructor
 */
PointLocator::Point::~Point()
{
}            

//******************************************************************************

/**
 * constructor.
 */
PointLocator::Bucket::Bucket()
{
   for (int i = 0; i < 6; i++) {
      bounds[i] = 0.0;
   }
}

/**
 * destructor.
 */
PointLocator::Bucket::~Bucket()
{
}
            
/**
 * get nearest point.
 */
int 
PointLocator::Bucket::getNearestPoint(const float xyz[3], float& nearbyDistSQ,
                                      float nearbyXYZ[3]) const
{
   int   n    = -1;
   float dist = std::numeric_limits<float>::max();
   
   const int num = static_cast<int>(points.size());
   for (int i = 0; i < num; i++) {
      const Point& p = points[i];
      const float dx = p.xyz[0] - xyz[0];
      const float dy = p.xyz[1] - xyz[1];
      const float dz = p.xyz[2] - xyz[2];
      const float d  = dx*dx + dy*dy + dz*dz;
      if (d < dist) {
         dist = d;
         n = i;
      }
   }
   
   if (n >= 0) {
      nearbyXYZ[0] = points[n].xyz[0];
      nearbyXYZ[1] = points[n].xyz[1];
      nearbyXYZ[2] = points[n].xyz[2];
      nearbyDistSQ = dist;
      const int pointID = points[n].idNum;
      return pointID;
   }
   
   return -1;
}