17 #ifndef __itkBalloonForceFilter_txx
18 #define __itkBalloonForceFilter_txx
21 #pragma warning ( disable : 4786 )
28 template <
typename TInputMesh,
typename TOutputMesh>
34 m_DistanceToBoundary = 100;
35 m_DistanceToStop = 10;
39 m_DistanceForGradient = 0.0;
44 typename TOutputMesh::Pointer output = TOutputMesh::New();
49 template <
typename TInputMesh,
typename TOutputMesh>
55 for (
unsigned int i = 0; i < m_NewNodeLimit; i++)
76 template <
typename TInputMesh,
typename TOutputMesh>
81 Superclass::PrintSelf(os,indent);
83 os << indent <<
"Center = " << m_Center << std::endl;
84 os << indent <<
"TimeStep = " << m_TimeStep << std::endl;
87 os << indent <<
"Gradient = " << m_Gradient << std::endl;
91 os << indent <<
"Gradient = (None)" << std::endl;
95 os << indent <<
"Forces = " << m_Forces << std::endl;
99 os << indent <<
"Forces = (None)" << std::endl;
101 os << indent <<
"Stiffness = " << m_Stiffness << std::endl;
102 os << indent <<
"DistanceForGradient = " << m_DistanceForGradient << std::endl;
103 os << indent <<
"GradientBegin = " << m_GradientBegin << std::endl;
104 os << indent <<
"Resolution = " << m_Resolution << std::endl;
106 if (!m_ImageOutput.IsNull())
108 os << indent <<
"ImageOutput = " << m_ImageOutput << std::endl;
112 os << indent <<
"Potential = " << m_Potential << std::endl;
116 os << indent <<
"Potential = (None)" << std::endl;
120 os << indent <<
"Displacements = " << m_Displacements << std::endl;
124 os << indent <<
"Displacements = (None)" << std::endl;
128 os << indent <<
"Locations = " << m_Locations << std::endl;
132 os << indent <<
"Locations = (None)" << std::endl;
134 os << indent <<
"DistanceToStop = " << m_DistanceToStop << std::endl;
138 os << indent <<
"Derives = " << m_Derives << std::endl;
142 os << indent <<
"Derives = (None)" << std::endl;
146 os << indent <<
"Normals = " << m_Normals << std::endl;
150 os << indent <<
"Normals = (None)" << std::endl;
156 template <
typename TInputMesh,
typename TOutputMesh>
161 m_NumberOfNodes = this->GetInput(0)->GetNumberOfPoints();
162 m_NumberOfCells = this->GetInput(0)->GetNumberOfCells();
164 m_Forces = InputMeshType::New();
165 m_Displacements = InputMeshType::New();
166 m_Derives = InputMeshType::New();
167 m_Normals = InputMeshType::New();
168 m_Locations = InputMeshType::New();
175 myForces->Reserve(m_NumberOfNodes);
179 myDerives->Reserve(m_NumberOfNodes);
183 myDisplacements->Reserve(m_NumberOfNodes);
187 myNormals->Reserve(m_NumberOfNodes);
191 myLocations->Reserve(m_NumberOfNodes);
203 m_NewNodesExisted = 0;
204 m_NewNodeLimit = 200;
205 m_ObjectLabel = m_Potential->GetPixel(m_Center);
206 m_NewNodes = (
float**) malloc(
sizeof(
float *)*m_NewNodeLimit);
207 for (
unsigned int i = 0; i < m_NewNodeLimit; i++)
215 m_ImageWidth = ImageSize[0];
216 m_ImageHeight = ImageSize[1];
218 float d[3] = {0,0,0};
223 while( points != myPoints->End() )
225 locations.Value() = points.Value();
226 tmp = locations.Value();
231 while( forces != myForces->End() )
237 while( normals != myNormals->End() )
243 for (
unsigned int i=0; i+2 < m_NumberOfNodes; i++ )
245 m_Forces->SetPointData(i, 1.0);
246 m_Locations->SetPointData(i, 0.0);
247 m_Derives->SetPointData(i, 0.0);
248 m_Displacements->SetPointData(i, 0.0);
251 for (
unsigned int j = 0; j < m_NewNodeLimit; j++)
253 m_NewNodes[j] = (
float*) malloc(
sizeof(
float)*5);
256 while( derives != myDerives->End() )
262 while( displacements != myDisplacements->End() )
264 displacements.Value() = d;
268 typename TriCell::CellAutoPointer insertCell;
269 unsigned long tripoints[3];
270 const unsigned long *tp;
273 for (
unsigned int i=0; i<m_NumberOfCells; i++)
275 tp = cells.Value()->GetPointIds();
276 tripoints[0] = tp[0];
277 tripoints[1] = tp[1];
278 tripoints[2] = tp[2];
279 insertCell.TakeOwnership(
new TriCell );
280 insertCell->SetPointIds(tripoints);
281 m_Locations->SetCell(i, insertCell);
282 x = celldata.Value();
283 m_Locations->SetCellData(i, (
PixelType)x);
290 outputMesh->SetBufferedRegion( outputMesh->GetRequestedRegion() );
296 template <
typename TInputMesh,
typename TOutputMesh>
308 m_K = (vnl_matrix_fixed<double,4,4>**) malloc(
sizeof(vnl_matrix_fixed<double,4,4>*)*m_NumberOfCells);
311 float us = vnl_math::pi / 1;
312 float vs = 2.0*vnl_math::pi / m_Resolution;
313 float a = us*us, b = vs*vs;
314 float area = us*vs/2, k00, k01, k02, k11, k12, k22;
316 k00 = area * (m_Stiffness[1]/a + m_Stiffness[1]/b + m_Stiffness[0]);
317 k01 = area * (-m_Stiffness[1]/a + m_Stiffness[0]);
318 k02 = area * (-m_Stiffness[1]/b + m_Stiffness[0]);
319 k11 = area * (m_Stiffness[1]/a + m_Stiffness[0]);
320 k12 = area * m_Stiffness[0];
321 k22 = area * (m_Stiffness[1]/b + m_Stiffness[0]);
323 m_NStiffness[0][0] = k00;
324 m_NStiffness[0][1] = k01;
325 m_NStiffness[0][2] = k02;
326 m_NStiffness[0][3] = 0.0;
327 m_NStiffness[1][0] = k01;
328 m_NStiffness[1][1] = k11;
329 m_NStiffness[1][2] = k12;
330 m_NStiffness[1][3] = 0.0;
331 m_NStiffness[2][0] = k02;
332 m_NStiffness[2][1] = k12;
333 m_NStiffness[2][2] = k22;
334 m_NStiffness[2][3] = 0.0;
335 m_NStiffness[3][0] = 0.0;
336 m_NStiffness[3][1] = 0.0;
337 m_NStiffness[3][2] = 0.0;
338 m_NStiffness[3][3] = 1.0;
340 k00 = area * (m_Stiffness[1]/a + m_Stiffness[0]);
341 k01 = area * (-m_Stiffness[1]/a + m_Stiffness[0]);
342 k02 = area * m_Stiffness[0];
343 k11 = area * (m_Stiffness[1]/a + m_Stiffness[1]/b+m_Stiffness[0]);
344 k12 = area * (-m_Stiffness[1]/b + m_Stiffness[0]);
345 k22 = area * (m_Stiffness[1]/b + m_Stiffness[0]);
347 m_SStiffness[0][0] = k00;
348 m_SStiffness[0][1] = k01;
349 m_SStiffness[0][2] = k02;
350 m_SStiffness[0][3] = 0.0;
351 m_SStiffness[1][0] = k01;
352 m_SStiffness[1][1] = k11;
353 m_SStiffness[1][2] = k12;
354 m_SStiffness[1][3] = 0.0;
355 m_SStiffness[2][0] = k02;
356 m_SStiffness[2][1] = k12;
357 m_SStiffness[2][2] = k22;
358 m_SStiffness[2][3] = 0.0;
359 m_SStiffness[3][0] = 0.0;
360 m_SStiffness[3][1] = 0.0;
361 m_SStiffness[3][2] = 0.0;
362 m_SStiffness[3][3] = 1.0;
364 k00 = area * (m_Stiffness[1]/b + m_Stiffness[0]);
365 k01 = area * (-m_Stiffness[1]/b + m_Stiffness[0]);
366 k02 = area * m_Stiffness[0];
367 k11 = area * (m_Stiffness[1]/a + m_Stiffness[1]/b + m_Stiffness[0]);
368 k12 = area * (-m_Stiffness[1]/a + m_Stiffness[0]);
369 k22 = area * (m_Stiffness[1]/a + m_Stiffness[0]);
371 m_CStiffness[0][0] = k00;
372 m_CStiffness[0][1] = k01;
373 m_CStiffness[0][2] = k02;
374 m_CStiffness[0][3] = 0.0;
375 m_CStiffness[1][0] = k01;
376 m_CStiffness[1][1] = k11;
377 m_CStiffness[1][2] = k12;
378 m_CStiffness[1][3] = 0.0;
379 m_CStiffness[2][0] = k02;
380 m_CStiffness[2][1] = k12;
381 m_CStiffness[2][2] = k22;
382 m_CStiffness[2][3] = 0.0;
383 m_CStiffness[3][0] = 0.0;
384 m_CStiffness[3][1] = 0.0;
385 m_CStiffness[3][2] = 0.0;
386 m_CStiffness[3][3] = 1.0;
389 while (celldata != myCellData->End())
391 x = celldata.Value();
396 m_K[j] = &m_SStiffness;
399 m_K[j] = &m_NStiffness;
402 m_K[j] = &m_CStiffness;
416 template <
typename TInputMesh,
typename TOutputMesh>
424 float extends[2], fo, t, xs, ys;
430 unsigned short label;
449 while( i != m_NumberOfNodes - 2 )
454 coord[0] = (int) x[0];
455 coord[1] = (int) x[1];
457 label = (
unsigned short)m_Potential->GetPixel(coord);
458 if ( label != m_ObjectLabel )
527 extend[0] = (int) x[0];
528 extend[1] = (int) x[1];
538 if ( vcl_abs(f[1]) > max ) max = vcl_abs(f[1]);
548 extend[0] = (int) extends[0];
549 extend[1] = (int) extends[1];
551 if ( (extend[0] < 0) ||
553 (static_cast<unsigned int>(extend[0]) >= m_ImageWidth) ||
554 (static_cast<unsigned int>(extend[1]) >= m_ImageHeight ) )
559 label = (
unsigned short) m_Potential->GetPixel(extend);
560 if ( label != m_ObjectLabel )
break;
567 if (t < 2) pointstatus.Value() = 1.0;
570 pointstatus.Value() = 0.0;
573 fo = vcl_sqrt(f[0]*f[0]+f[1]*f[1]);
574 f[0] = t*100*f[0]*xs/fo;
575 f[1] = t*100*f[1]*ys/fo;
579 forcedata.Value() = 0.0;
595 template <
typename TInputMesh,
typename TOutputMesh>
600 const unsigned long *tp;
615 for(; cells_it != myCells->End(); ++cells_it, i++ )
617 tp = cells_it.Value()->GetPointIds();
618 for(
unsigned int j = 0; j < 3; j++ )
623 for(
unsigned int j = 0; j < 3; j++ )
625 m_Displacements->GetPoint (tp[j], &vd[j]);
626 m_Forces->GetPoint (tp[j], &vf[j]);
628 for(
unsigned int k = 0; k < 3; k++ )
630 u[k] += vd[j].GetVectorFromOrigin() * m_K[i]->get( k, j ) * p;
634 for(
unsigned int j = 0; j < 3; j++ )
637 m_Forces->SetPoint (tp[j], vf[j]);
647 for (; derives_it != myDerives->End(); ++derives_it, ++forces_it )
649 derives_it.Value() = forces_it.Value();
660 template <
typename TInputMesh,
typename TOutputMesh>
665 int i, j, cell=0, slice, numnewnodes, res;
666 float status, d[3] = {0,0,0}, w;
671 const unsigned long *tp;
673 unsigned long tripoints[3];
675 if (m_NumNewNodes == 0)
return;
678 m_NumberOfNodes = m_NumberOfNodes + m_NumNewNodes;
679 m_NumberOfCells = m_NumberOfCells + m_NumNewNodes*2;
683 myForces->Reserve(m_NumberOfNodes);
687 myPoints->Reserve(m_NumberOfNodes);
691 myNormals->Reserve(m_NumberOfNodes);
695 myDerives->Reserve(m_NumberOfNodes);
699 myDisplacements->Reserve(m_NumberOfNodes);
706 myCells->Reserve(m_NumberOfCells);
710 myCellData->Reserve(m_NumberOfCells);
714 myOutCells->Reserve(m_NumberOfCells);
718 myOutCellData->Reserve(m_NumberOfCells);
721 typename TriCell::CellAutoPointer insertCell =
new TriCell;
722 insertCell.TakeOwnership();
726 while (normals != myNormals->End())
728 if (i > (
int) m_NewNodes[j][3])
730 z[0] = m_NewNodes[j][0];
731 z[1] = m_NewNodes[j][1];
732 z[2] = m_NewNodes[j][2];
747 normals = myNormals->Begin();
748 points = myPoints->Begin();
750 while (points != myNormals->End())
752 points.Value() = normals.Value();
757 while( forces != myForces->End() )
763 while( derives != myDerives->End() )
769 while( displacements != myDisplacements->End() )
771 displacements.Value() = d;
778 while ( outcells != myOutCells->End() )
780 tp = cells.Value()->GetPointIds();
782 if ( tp[0] > (
unsigned long) m_NewNodes[j][3] )
787 outcells.Value() = cells.Value();
790 outcells.Value() = cells.Value();
796 if (tp[0] != tp[1] - 1)
808 for (j=0; j<m_Resolution; j++)
810 jn = (j+1)%m_Resolution;
811 tripoints[0] = m_NumberOfNodes-2;
815 m_Locations->SetCell(p, insertCell);
816 m_Locations->SetCellData(p, (
PixelType)1.0);
818 insertCell.TakeOwnership(
new TriCell );
822 for (j=0; j<m_Resolution; j++)
824 jn = (j+1)%m_Resolution;
825 tripoints[2] = (1-1)*m_Resolution+j;
826 tripoints[1] = m_NumberOfNodes-1;
827 tripoints[0] = tripoints[2]-j+jn;
828 insertCell->SetPointIds(tripoints);
829 m_Locations->SetCell(p, insertCell);
830 m_Locations->SetCellData(p, (
PixelType)2.0);
832 insertCell = TriCell::New();
837 m_K = (vnl_matrix_fixed<double,4,4>**)
838 malloc(
sizeof(vnl_matrix_fixed<double,4,4>*)*m_NumberOfCells);
840 celldata = m_Locations->GetCellData()->Begin();
843 while (celldata != m_Locations->GetCellData()->End())
845 w = celldata.Value();
850 m_K[j] = &m_SStiffness;
853 m_K[j] = &m_NStiffness;
856 m_K[j] = &m_CStiffness;
867 template <
typename TInputMesh,
typename TOutputMesh>
872 typename TInputMesh::PointType s, d, ds;
884 while( i < static_cast<int>(m_NumberOfNodes)-2 )
886 ds = derives.Value();
888 d = displacements.Value();
889 s[0] += m_TimeStep*ds[0];
890 s[1] += m_TimeStep*ds[1];
891 if ( m_GradientBegin )
893 d[0] += m_TimeStep*ds[0];
894 d[1] += m_TimeStep*ds[1];
903 if ( (s[0] > 0) && (s[1] > 0) &&
904 (s[0] < m_ImageWidth) && (s[1] < m_ImageHeight) )
907 displacements.Value() = d;
910 if ( m_GradientBegin )
912 dist += vcl_sqrt(ds[0]*ds[0]+ds[1]*ds[1])*m_TimeStep;
913 m_DistanceToBoundary = dist/((float)(m_NumberOfNodes-2));
929 template <
typename TInputMesh,
typename TOutputMesh>
934 typename TriCell::CellAutoPointer insertCell;
935 unsigned long tripoints[3];
936 const unsigned long *tp;
939 m_Output = this->GetOutput();
942 myPoints->Reserve(m_NumberOfNodes);
957 for (
unsigned int i=0; i<m_NumberOfNodes; i++)
959 points.Value() = locations.Value();
966 for (
unsigned int i=0; i<m_NumberOfCells; i++)
968 tp = cells.Value()->GetPointIds();
969 tripoints[0] = tp[0];
970 tripoints[1] = tp[1];
971 tripoints[2] = tp[2];
972 insertCell.TakeOwnership(
new TriCell );
973 insertCell->SetPointIds(tripoints);
974 m_Output->SetCell(i, insertCell );
975 x = celldata.Value();
986 template <
typename TInputMesh,
typename TOutputMesh>
992 this->SetStiffnessMatrix();
995 this->ComputeNormals();
996 if ( m_GradientBegin ) this->GradientFit();
997 else this->ComputeForce();
1001 this->NodesRearrange();
1004 this->ComputeOutput();
1011 template <
typename TInputMesh,
typename TOutputMesh>
1031 float gap, dis[3]={0, 0, 0}, st, *st_PixelType;
1033 const unsigned long *tp;
1036 while( i != m_NumberOfNodes - 2 )
1041 tp = cells.Value()->GetPointIds();
1045 if (tp[0] == tp[1] - 1) y = points.Value();
1048 m_Locations->GetPoint (tp[1], y_PixelType);
1052 dis[0] = x[0] - y[0];
1053 dis[1] = x[1] - y[1];
1054 dis[2] = x[2] - y[2];
1055 gap = vcl_sqrt(dis[0]*dis[0]+dis[1]*dis[1]+dis[2]*dis[2]);
1057 m_Locations->GetPointData(q, st_PixelType);
1060 if ( pointstatus.Value() == 1.0)
1063 if ( pointstatus.Value() == 1.0)
1065 z[0] = 0.5*(x[0]+y[0]);
1066 z[1] = 0.5*(x[1]+y[1]);
1067 z[2] = 0.5*(x[2]+y[2]);
1068 this->NodeAddition(i, res, z);
1084 template <
typename TInputMesh,
typename TOutputMesh>
1091 if (m_NumNewNodes > m_NewNodeLimit)
1093 m_NewNodes = realloc( m_NewNodes, m_NewNodeLimit*
sizeof(
float*)*2 );
1094 for (
int i = m_NewNodeLimit; i < 2*m_NewNodeLimit; i++)
1096 m_NewNodes[i] = (
float*) malloc(
sizeof(
float)*5);
1098 m_NewNodeLimit = m_NewNodeLimit*2;
1101 m_NewNodes[m_NumNewNodes-1][0] = z[0];
1102 m_NewNodes[m_NumNewNodes-1][1] = z[1];
1103 m_NewNodes[m_NumNewNodes-1][2] = z[2];
1104 m_NewNodes[m_NumNewNodes-1][3] = (float) node;
1105 m_NewNodes[m_NumNewNodes-1][4] = (float) res;
1111 template <
typename TInputMesh,
typename TOutputMesh>
1122 typename TInputMesh::PointType vec_nor, vec_loc, vec_for, tmp_vec_1, tmp_vec_2, tmp_vec_3;
1137 while( forces != myForces->End() )
1139 vec_loc = locations.Value();
1140 vec_nor = normals.Value();
1142 coord[0] =
static_cast<IndexValueType
>(vec_loc[0]);
1143 coord[1] =
static_cast<IndexValueType
>(vec_loc[1]);
1145 coord2[0] =
static_cast<IndexValueType
>( vcl_ceil( vec_loc[0] ) );
1146 coord2[1] =
static_cast<IndexValueType
>( vcl_ceil( vec_loc[1] ) );
1148 tmp_co_1[0] = coord2[0];
1149 tmp_co_1[1] = coord[1];
1150 tmp_co_2[0] = coord[0];
1151 tmp_co_2[1] = coord2[1];
1154 if ( (coord[0] >= 0) &&
1156 (static_cast<unsigned int>( coord2[0] ) < m_ImageWidth) &&
1157 (static_cast<unsigned int>( coord2[1] ) < m_ImageHeight) )
1159 vec_for[0] = m_Gradient->GetPixel(coord)[0];
1160 vec_for[1] = m_Gradient->GetPixel(coord)[1];
1162 tmp_vec_1[0] = m_Gradient->GetPixel(tmp_co_1)[0] - m_Gradient->GetPixel(coord)[0];
1163 tmp_vec_1[1] = m_Gradient->GetPixel(tmp_co_1)[1] - m_Gradient->GetPixel(coord)[1];
1164 tmp_vec_2[0] = m_Gradient->GetPixel(tmp_co_2)[0] - m_Gradient->GetPixel(coord)[0];
1165 tmp_vec_2[1] = m_Gradient->GetPixel(tmp_co_2)[1] - m_Gradient->GetPixel(coord)[1];
1167 vec_for[0] = vec_for[0] + (vec_loc[0]-coord[0])*tmp_vec_1[0] + (vec_loc[1]-coord[1])*tmp_vec_2[0];
1168 vec_for[1] = vec_for[1] + (vec_loc[1]-coord[1])*tmp_vec_2[1] + (vec_loc[0]-coord[0])*tmp_vec_1[1];
1176 mag = vec_for[0]*vec_nor[0] + vec_for[1]*vec_nor[1];
1177 vec_for[0] = mag*vec_nor[0];
1178 vec_for[1] = mag*vec_nor[1];
1181 forces.Value() = vec_for;
1194 template <
typename TInputMesh,
typename TOutputMesh>
1199 const unsigned long *tp;
1206 float coa, cob, coc;
1215 static float d[3]={0, 0, 0};
1216 while( normals != myNormals->End() )
1218 normals.Value() = d;
1222 while ( cells != myCells->End() )
1224 tp = cells.Value()->GetPointIds();
1227 m_Locations->GetPoint (tp[0], &v1 );
1228 m_Locations->GetPoint (tp[1], &v2 );
1229 m_Locations->GetPoint (tp[2], &v3 );
1231 coa = -(v1[1]*(v2[2]-v3[2]) +
1232 v2[1]*(v3[2]-v1[2]) +
1233 v3[1]*(v1[2]-v2[2]));
1234 cob = -(v1[2] * (v2[0]-v3[0]) +
1235 v2[2]*(v3[0]-v1[0]) +
1236 v3[2]*(v1[0]-v2[0]));
1237 coc = -(v1[0] * (v2[1]-v3[1]) +
1238 v2[0]*(v3[1]-v1[1]) +
1239 v3[0]*(v1[1]-v2[1]));
1241 absvec = -vcl_sqrt ((
double) ((coa*coa) + (cob*cob) + (coc*coc)));
1243 assert (absvec != 0);
1248 m_Normals->GetPoint (tp[0], &v1 );
1249 m_Normals->GetPoint (tp[1], &v2 );
1250 m_Normals->GetPoint (tp[2], &v3 );
1264 m_Normals->SetPoint (tp[0], v1);
1265 m_Normals->SetPoint (tp[1], v2);
1266 m_Normals->SetPoint (tp[2], v3);
1270 normals = myNormals->Begin();
1271 while( normals != myNormals->End() )
1273 v1 = normals.Value();
1274 absvec = vcl_sqrt( v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2] );
1275 v1[0] = v1[0]/absvec;
1276 v1[1] = v1[1]/absvec;
1277 v1[2] = v1[2]/absvec;
1278 normals.Value() = v1;
1286 template <
typename TInputMesh,
typename TOutputMesh>
1296 typename TInputMesh::PointType s, s1, d1, d2;
1311 v1 = locations.Value();
1312 s = locations.Value();
1314 s1 = locations.Value();
1316 forcescopy = forces;
1319 while ( (i+1)%m_Resolution != 0 )
1321 v2 = locations.Value();
1323 v3 = locations.Value();
1324 d1[0] = v1[0] - v2[0];
1325 d2[0] = v3[0] - v2[0];
1326 d1[1] = v1[1] - v2[1];
1327 d2[1] = v3[1] - v2[1];
1333 dis = d1[0]*d2[0]+d1[1]*d2[1];
1336 l1 = vcl_sqrt(d1[0]*d1[0]+d1[1]*d1[1]);
1337 l2 = vcl_sqrt(d2[0]*d2[0]+d2[1]*d2[1]);
1338 dis = dis/vcl_sqrt(l1*l2);
1345 d1[0] = (d1[0]+d2[0]);
1346 d1[1] = (d1[1]+d2[1]);
1348 l1 = vcl_sqrt(d1[0]*d1[0]+d1[1]*d1[1]);
1352 v2[0] = v2[0] + dis * d1[0];
1353 v2[1] = v2[1] + dis * d1[1];
1357 forces.Value() = v2;
1363 v2 = locations.Value();
1368 d1[0] = v1[0] - v2[0];
1369 d2[0] = v3[0] - v2[0];
1370 d1[1] = v1[1] - v2[1];
1371 d2[1] = v3[1] - v2[1];
1377 dis = d1[0]*d2[0]+d1[1]*d2[1];
1380 l1 = vcl_sqrt(d1[0]*d1[0]+d1[1]*d1[1]);
1381 l2 = vcl_sqrt(d2[0]*d2[0]+d2[1]*d2[1]);
1382 dis = dis/vcl_sqrt(l1*l2);
1389 d1[0] = (d1[0]+d2[0]);
1390 d1[1] = (d1[1]+d2[1]);
1392 l1 = vcl_sqrt(d1[0]*d1[0]+d1[1]*d1[1]);
1396 v2[0] = v2[0] + dis * d1[0];
1397 v2[1] = v2[1] + dis * d1[1];
1401 forces.Value() = v2;
1411 d1[0] = v1[0] - v2[0];
1412 d2[0] = v3[0] - v2[0];
1413 d1[1] = v1[1] - v2[1];
1414 d2[1] = v3[1] - v2[1];
1420 dis = d1[0]*d2[0]+d1[1]*d2[1];
1423 l1 = vcl_sqrt(d1[0]*d1[0]+d1[1]*d1[1]);
1424 l2 = vcl_sqrt(d2[0]*d2[0]+d2[1]*d2[1]);
1425 dis = dis/vcl_sqrt(l1*l2);
1432 d1[0] = (d1[0]+d2[0]);
1433 d1[1] = (d1[1]+d2[1]);
1435 l1 = vcl_sqrt(d1[0]*d1[0]+d1[1]*d1[1]);
1439 v2[0] = v2[0] + dis * d1[0];
1440 v2[1] = v2[1] + dis * d1[1];
1444 forcescopy.Value() = v2;
1446 forces = myForces->Begin();
1453 while ( i < m_Resolution - 1 )
1455 v1 = forces.Value();
1457 v2 = forces.Value();
1459 dis += vcl_sqrt((v1[0]-v2[0])*(v1[0]-v2[0])+(v1[1]-v2[1])*(v1[1]-v2[1]));
1463 dis += vcl_sqrt((s[0]-v2[0])*(s[0]-v2[0])+(s[1]-v2[1])*(s[1]-v2[1]));
1464 length = dis/m_Resolution;
1467 forces = myForces->Begin();
1472 v1 = forces.Value();
1473 normals.Value() = v1;
1475 v3 = forces.Value();
1476 while ( i < m_Resolution - 1 )
1478 v1 = forces.Value();
1480 v2 = forces.Value();
1481 dis = vcl_sqrt((v1[0]-v2[0])*(v1[0]-v2[0])+(v1[1]-v2[1])*(v1[1]-v2[1]));
1485 while ( l1 > length )
1487 if (k==m_Resolution)
break;
1488 s[0] = v1[0] + (length+l2)*(v2[0] - v1[0])/dis;
1489 s[1] = v1[1] + (length+l2)*(v2[1] - v1[1])/dis;
1491 normals.Value() = s;
1498 if (k==m_Resolution)
break;
1501 if (k==m_Resolution)
1503 while (i < m_Resolution)
1510 v1 = forces.Value();
1512 dis = vcl_sqrt((v1[0]-v3[0])*(v1[0]-v3[0])+(v1[1]-v3[1])*(v1[1]-v3[1]));
1516 while ( l1 > length )
1518 if (k==m_Resolution)
break;
1519 s[0] = v1[0] + (length+l2)*(v3[0] - v1[0])/dis;
1520 s[1] = v1[1] + (length+l2)*(v3[1] - v1[1])/dis;
1522 normals.Value() = s;
1529 locations = myLocations->Begin();
1530 normals = myNormals->Begin();
1531 displacements = myDisplacements->Begin();
1532 forces = myForces->Begin();
1536 while ( i < static_cast<int>(m_NumberOfNodes) - 2 )
1538 v1 = normals.Value();
1540 v2 = locations.Value();
1541 v3 = displacements.Value();
1542 v3[0] += v1[0] - v2[0];
1543 v3[1] += v1[1] - v2[1];
1545 locations.Value() = v1;
1546 if ( m_GradientBegin ) displacements.Value() = v3;
1555 locations = myLocations->Begin();
1561 s = locations.Value();
1562 while ( i < m_Resolution - 1 )
1564 v1 = locations.Value();
1566 v2 = locations.Value();
1575 template <
typename TInputMesh,
typename TOutputMesh>
1581 int j, l, m, n, PixelType1, PixelType2;
1584 m_ACD = (
int**) malloc(
sizeof(
int *)*m_ImageHeight/2);
1586 for (i=0; i<m_ImageHeight/2; i++)
1588 m_ACD[i] = (
int*) malloc(
sizeof(
int)*m_ImageWidth/2);
1600 for (j = 0; j < 1; j++)
1603 locationscopy = locations;
1604 dpcopy = displacements;
1606 for (
unsigned int ll=0; ll < m_ImageHeight/2; ll++)
1608 for (
unsigned int mm=0; mm < m_ImageWidth/2; mm++)
1614 for (; i < static_cast<unsigned int>( m_Resolution ); i++)
1616 v = locations.Value();
1622 int ii =
static_cast<int>( i );
1624 if (m_ACD[m][l] == -1)
1630 if (((ii - m_ACD[m][l]) > m_Resolution/10) &&
1631 ((m_Resolution-ii+m_ACD[m][l])>m_Resolution/10))
1633 locationscopy1 = locationscopy;
1638 if ( (ii - m_ACD[m][l]) < 0.5*m_Resolution )
1640 if (m_ACD[m][l] == 0)
1642 PixelType1 = m_Resolution - 1;
1646 PixelType1 = m_ACD[m][l] - 1;
1648 if (ii == m_Resolution - 1)
1654 PixelType2 = ii + 1;
1656 while (n<m_Resolution)
1658 v = locationscopy1.Value();
1659 if ((n>m_ACD[m][l]) && (n<ii))
1666 if ( n == PixelType1) v2 = locationscopy1.Value();
1667 if ( n == PixelType2 ) v3 = locationscopy1.Value();
1672 v1[0] = v1[0]/(ii-m_ACD[m][l]-1);
1673 v1[1] = v1[1]/(ii-m_ACD[m][l]-1);
1677 locationscopy1 = locationscopy;
1680 while (n<m_Resolution)
1682 if ((n>m_ACD[m][l]) && (n<ii))
1684 v1 = locationscopy1.Value();
1685 locationscopy1.Value() = v;
1686 v2 = dpcopy1.Value();
1687 v2[0] += v[0] - v1[0];
1688 v2[1] += v[1] - v1[1];
1690 dpcopy1.Value() = v2;
1692 if ( n == m_ACD[m][l] )
1694 v = locationscopy1.Value();
1704 while (n<m_Resolution)
1706 PixelType1 = m_ACD[m][l] + 1;
1707 PixelType2 = ii - 1;
1708 v = locationscopy1.Value();
1709 if ((n<m_ACD[m][l]) && (n>ii))
1716 if ( n == PixelType1 ) v2 = locationscopy1.Value();
1717 if ( n == PixelType2 ) v3 = locationscopy1.Value();
1722 v1[0] = v1[0]/(ii-m_ACD[m][l]-1);
1723 v1[1] = v1[1]/(ii-m_ACD[m][l]-1);
1727 locationscopy1 = locationscopy;
1730 while (n<m_Resolution)
1734 v = locationscopy1.Value();
1739 locationscopy1 = locationscopy;
1741 while (n<m_Resolution)
1743 if ((n<m_ACD[m][l]) && (n>ii))
1745 v1 = locationscopy1.Value();
1746 locationscopy1.Value() = v;
1747 v2 = dpcopy1.Value();
1748 v2[0] += v[0] - v1[0];
1749 v2[1] += v[1] - v1[1];
1751 dpcopy1.Value() = v2;
1768 for (i=0; i<m_ImageHeight/2; i++)