-
Notifications
You must be signed in to change notification settings - Fork 53
/
PointAreaComputer.cpp
170 lines (141 loc) · 4.97 KB
/
PointAreaComputer.cpp
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
/* ********************************************************************
* PointAreaComputer
*
* Copyright 2012 Joachim Giard
* Université catholique de Louvain, Belgium
* Apache License, Version 2.0
*
* Computes the voronoi area of each point
*
* Arno Klein added lines to avoid NaN errors in 2017.
*
* *******************************************************************/
#include "PointAreaComputer.h"
#include <vtkCellArray.h>
#include <vtkDoubleArray.h>
#include <vtkMath.h>
#include <vtkPolyDataWriter.h>
#include <vtkPointData.h>
PointAreaComputer::PointAreaComputer(vtkPolyData *mesh)
{
m_mesh = mesh;
m_pointsArea = vtkDoubleArray::New();
}
PointAreaComputer::~PointAreaComputer()
{
m_pointsArea->Delete();
}
void PointAreaComputer::ComputeArea()
{
m_pointsArea = ComputeVoronoiArea();
}
vtkDoubleArray* PointAreaComputer::GetArea()
{
return m_pointsArea;
}
void PointAreaComputer::WriteIntoFile(char *fileName)
{
vtkPolyDataWriter* writer=vtkPolyDataWriter::New();
writer->SetFileName(fileName);
m_mesh->GetPointData()->SetScalars(m_pointsArea);
writer->SetInputData(m_mesh);
writer->Write();
writer->Delete();
}
vtkDoubleArray* PointAreaComputer::ComputeVoronoiArea()
{
// initialisation
vtkCellArray* cells=m_mesh->GetPolys();
int nbPolys = cells->GetNumberOfCells();
vtkDoubleArray* pointsArea = vtkDoubleArray::New();
for(int i=0;i<m_mesh->GetNumberOfPoints();i++)
{
pointsArea->InsertNextValue(0);
}
int cellIds[3];
double pos1[3];
double pos2[3];
double pos3[3];
float a;
float b;
float c;
float alpha;
float beta;
float gamma;
float area, areaA, areaB, areaC;
bool obtuse = false;
for (int i=0;i<nbPolys;i++)
{
cellIds[0]=m_mesh->GetCell(i)->GetPointId(0);
cellIds[1]=m_mesh->GetCell(i)->GetPointId(1);
cellIds[2]=m_mesh->GetCell(i)->GetPointId(2);
if(cellIds[0]>m_mesh->GetNumberOfPoints()||cellIds[1]>m_mesh->GetNumberOfPoints()||cellIds[2]>m_mesh->GetNumberOfPoints())
{
cout<<"point surface cell id problem: "<<i<<" "<<cellIds[0]<<" "<<cellIds[1]<<" "<<cellIds[2]<<endl;
}
else
{
m_mesh->GetPoint(cellIds[0],pos1);
m_mesh->GetPoint(cellIds[1],pos2);
m_mesh->GetPoint(cellIds[2],pos3);
// distances between points of each triangle
a=sqrt(vtkMath::Distance2BetweenPoints(pos3,pos2));
b=sqrt(vtkMath::Distance2BetweenPoints(pos1,pos3));
c=sqrt(vtkMath::Distance2BetweenPoints(pos2,pos1));
// set 1/3 areas to zero by default
areaA = 0;
areaB = 0;
areaC = 0;
// if distances greater than zero
if(a>0 && b>0 && c>0)
{
// angles
alpha = acos( (pow(b,2) + pow(c,2) - pow(a,2)) / (2*b*c) );
beta = acos( (pow(a,2) + pow(c,2) - pow(b,2)) / (2*a*c) );
gamma = acos( (pow(b,2) + pow(a,2) - pow(c,2)) / (2*b*a) );
// don't set 1/3 areas if angles are not numbers
if( vtkMath::IsNan(alpha) || vtkMath::IsNan(beta) || vtkMath::IsNan(gamma) )
{
}
// else set 1/3 areas
else
{
if( alpha > vtkMath::Pi()/2 || beta > vtkMath::Pi()/2 || gamma > vtkMath::Pi()/2 )
{
// Herons's formula
area = 0.25 * sqrt( (a+b+c)*(b+c-a)*(a-b+c)*(a+b-c) );
if(alpha > vtkMath::Pi()/2)
{
areaA = area/2;
areaB = area/4;
areaC = area/4;
}
else if (beta > vtkMath::Pi()/2)
{
areaA = area/4;
areaB = area/2;
areaC = area/4;
}
else if (gamma > vtkMath::Pi()/2)
{
areaA = area/4;
areaB = area/4;
areaC = area/2;
}
}
else
{
areaA = (pow(b,2) / tan(beta) + pow(c,2) / tan(gamma)) /8;
areaB = (pow(a,2) / tan(alpha) + pow(c,2) / tan(gamma)) /8;
areaC = (pow(b,2) / tan(beta) + pow(a,2) / tan(alpha)) /8;
}
}
}
// add 1/3 of the face area to each point of the triangle
pointsArea->SetValue(cellIds[0],pointsArea->GetValue(cellIds[0])+ areaA);
pointsArea->SetValue(cellIds[1],pointsArea->GetValue(cellIds[1])+ areaB);
pointsArea->SetValue(cellIds[2],pointsArea->GetValue(cellIds[2])+ areaC);
}
}
return pointsArea;
}