File: vtkvmtkCenterlineSphereDistance.cxx

package info (click to toggle)
vmtk 1.0.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 8,612 kB
  • sloc: cpp: 79,872; ansic: 31,817; python: 18,860; perl: 381; makefile: 118; sh: 15; tcl: 1
file content (262 lines) | stat: -rw-r--r-- 7,460 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
/*=========================================================================

Program:   VMTK
Module:    $RCSfile: vtkvmtkCenterlineSphereDistance.cxx,v $
Language:  C++
Date:      $Date: 2006/10/17 15:16:16 $
Version:   $Revision: 1.7 $

  Copyright (c) Luca Antiga, David Steinman. All rights reserved.
  See LICENCE file for details.

  Portions of this code are covered under the VTK copyright.
  See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm 
  for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "vtkvmtkCenterlineSphereDistance.h"
#include "vtkvmtkPolyBallLine.h"
#include "vtkPolyData.h"
#include "vtkPolyLine.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkCellArray.h"
#include "vtkIdList.h"
#include "vtkIntArray.h"
#include "vtkDoubleArray.h"
#include "vtkAppendPolyData.h"
#include "vtkvmtkConstants.h"
#include "vtkMath.h"
#include "vtkvmtkMath.h"
#include "vtkObjectFactory.h"


vtkCxxRevisionMacro(vtkvmtkCenterlineSphereDistance, "$Revision: 1.7 $");
vtkStandardNewMacro(vtkvmtkCenterlineSphereDistance);


void vtkvmtkCenterlineSphereDistance::FindNTouchingSphereCenter(vtkPolyData* centerlines, const char* radiusArrayName, vtkIdType cellId, vtkIdType subId, double pcoord, int numberOfTouchingSpheres, vtkIdType& touchingSubId, double& touchingPCoord, bool forward)
{
  vtkIdType dummySubId0, dummySubId1;
  double dummyPCoord0, dummyPCoord1;

  if (numberOfTouchingSpheres == 0)
  {
    touchingSubId = subId;
    touchingPCoord = pcoord;
    return;
  }
  
  dummySubId0 = subId;
  dummyPCoord0 = pcoord;

  for (int i=0; i<numberOfTouchingSpheres; i++)
    {
    vtkvmtkCenterlineSphereDistance::FindTouchingSphereCenter(centerlines,radiusArrayName,cellId,dummySubId0,dummyPCoord0,dummySubId1,dummyPCoord1,forward);
    if (dummySubId1 == -1)
      {
      break;
      }
    dummySubId0 = dummySubId1;
    dummyPCoord0 = dummyPCoord1;
    }

  touchingSubId = dummySubId1;
  touchingPCoord = dummyPCoord1;
}

void vtkvmtkCenterlineSphereDistance::FindTouchingSphereCenter(vtkPolyData* centerlines, const char* radiusArrayName, vtkIdType cellId, vtkIdType subId, double pcoord, vtkIdType& touchingSubId, double& touchingPCoord, bool forward)
{
  touchingSubId = -1;
  touchingPCoord = 0.0;

  vtkCell* centerline = centerlines->GetCell(cellId);
  
  if (centerline->GetCellType() != VTK_LINE && centerline->GetCellType() != VTK_POLY_LINE)
    {
    return;
    }

  if (!radiusArrayName)
    {
    return;
    }

  vtkDataArray* radiusArray = centerlines->GetPointData()->GetArray(radiusArrayName);
  
  if (!radiusArray)
    {
    return;
    }

  vtkIdList* subIds = vtkIdList::New();

  int k;
  if (forward)
    {
    for (k=0; k<subId; k++)
      {
      subIds->InsertNextId(k);
      }
    }
  else
    {
    for (k=centerline->GetNumberOfPoints()-2; k>subId; k--)
      {
      subIds->InsertNextId(k);
      }
    }

  double point[3], point0[3], point1[3];
  centerline->GetPoints()->GetPoint(subId,point0);
  centerline->GetPoints()->GetPoint(subId+1,point1);
  for (k=0; k<3; k++) 
    {
    point[k] = point0[k] + pcoord*(point1[k] - point0[k]);
    }

  vtkIdType centerId0, centerId1;
  double center0[3], center1[3], radius0, radius1;
  vtkIdType currentSubId;
  double currentPCoord;
  double sphereDistance0, sphereDistance1;

  touchingSubId = -1;
  touchingPCoord = 0.0;

  int i;
  for (i=0; i<subIds->GetNumberOfIds(); i++)
    {
    currentSubId = subIds->GetId(i);
    
    centerId0 = centerline->GetPointId(currentSubId);
    centerId1 = centerline->GetPointId(currentSubId+1);
    centerlines->GetPoint(centerId0,center0);
    centerlines->GetPoint(centerId1,center1);
    radius0 = radiusArray->GetComponent(centerId0,0);
    radius1 = radiusArray->GetComponent(centerId1,0);    
    sphereDistance0 = vtkvmtkMath::EvaluateSphereFunction(center0,radius0,point);
    sphereDistance1 = vtkvmtkMath::EvaluateSphereFunction(center1,radius1,point);

    if (forward)
      {
      if ((sphereDistance0 > 0.0) && (sphereDistance1 <= 0.0))
        {
        touchingSubId = currentSubId;
        break;
        }
      }
    else
      {
      if ((sphereDistance0 <= 0.0) && (sphereDistance1 > 0.0))
        {
        touchingSubId = currentSubId;
        break;
        }
      }
    }

  if (touchingSubId == -1)
    {
    centerId0 = centerline->GetPointId(subId);
    centerId1 = centerline->GetPointId(subId+1);
    centerlines->GetPoint(centerId0,center0);
    centerlines->GetPoint(centerId1,center1);
    radius0 = radiusArray->GetComponent(centerId0,0);
    radius1 = radiusArray->GetComponent(centerId1,0);

    sphereDistance0 = vtkvmtkMath::EvaluateSphereFunction(center0,radius0,point);
    sphereDistance1 = vtkvmtkMath::EvaluateSphereFunction(center1,radius1,point);

    if ((sphereDistance0<=0.0) && (sphereDistance1<=0.0))
      {
      touchingSubId = -1;
      return;
      }

    touchingSubId = subId;
    }

  // optimize between (touchingSubId, touchingSubId+1)

  // linear search, step proportional to sphere radius.

  centerId0 = centerline->GetPointId(touchingSubId);
  centerId1 = centerline->GetPointId(touchingSubId+1);
  centerlines->GetPoint(centerId0,center0);
  centerlines->GetPoint(centerId1,center1);
  radius0 = radiusArray->GetComponent(centerId0,0);
  radius1 = radiusArray->GetComponent(centerId1,0);

  double segmentLength, stepSize, pcoordStepSize;
  int numberOfSteps;
  double subCenter0[3], subCenter1[3], subRadius0, subRadius1;
  segmentLength = vtkMath::Distance2BetweenPoints(center0,center1);
  stepSize = 1E-6 * (radius0 + radius1) / 2.0;
  numberOfSteps = (int)ceil(segmentLength / stepSize);
  stepSize = segmentLength / numberOfSteps;
  pcoordStepSize = 1.0 / (double)numberOfSteps;

  touchingPCoord = 0.0;
  currentPCoord = 0.0;
  for (i=0; i<numberOfSteps; i++)
    {
    for (int k=0; k<3; k++) 
      {
      subCenter0[k] = center0[k] + currentPCoord*(center1[k] - center0[k]);
      subCenter1[k] = center0[k] + (currentPCoord+pcoordStepSize)*(center1[k] - center0[k]);
      }
    subRadius0 = radius0 + currentPCoord*(radius1 - radius0);
    subRadius1 = radius0 + (currentPCoord+pcoordStepSize)*(radius1 - radius0);

    sphereDistance0 = vtkvmtkMath::EvaluateSphereFunction(subCenter0,subRadius0,point);
    sphereDistance1 = vtkvmtkMath::EvaluateSphereFunction(subCenter1,subRadius1,point);

    if (forward)
      {
      if ((sphereDistance0 > 0.0) && (sphereDistance1 <= 0.0))
        {
        touchingPCoord = currentPCoord;
        break;
        }
      }
    else
      {
      if ((sphereDistance0 <= 0.0) && (sphereDistance1 > 0.0))
        {
        touchingPCoord = currentPCoord+pcoordStepSize;
        break;
        }
      }
    currentPCoord += pcoordStepSize;
    }

  if (forward)
    {
    if ((touchingSubId == subId) && (touchingPCoord > pcoord))
      {
      touchingSubId = -1;
      touchingPCoord = 0.0;
      }
    }
  else
    {
    if ((touchingSubId == subId) && (touchingPCoord < pcoord))
      {
      touchingSubId = -1;
      touchingPCoord = 0.0;
      }
    }

  subIds->Delete();
}

void vtkvmtkCenterlineSphereDistance::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}