Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
V[3]=v.z; V1[3]=0.f; V2[3]=0.f; V3[3]=1.f;
V[4]=v.x*v.y; V1[4]=v.y; V2[4]=v.x; V3[4]=0.f;
V[5]=v.x*v.z; V1[5]=v.z; V2[5]=0.f; V3[5]=v.x;
V[6]=v.y*v.z; V1[6]=0.f; V2[6]=v.z; V3[6]=v.y;
V[7]=v.x*v.x; V1[7]=2.f*v.x; V2[7]=0.f; V3[7]=0.f;
V[8]=v.y*v.y; V1[8]=0.f; V2[8]=2.f*v.y; V3[8]=0.f;
V[9]=v.z*v.z; V1[9]=0.f; V2[9]=0.f; V3[9]=2.f*v.z;
//construct V*V' and add it to M and N
for (int j=0; j < 9; ++j) //for column
for (int i=0; i < 9; ++i) //for row; note the order optimal for Fortran
{
//C order (row-major): M_i,j -> M[i][j] -> &M +i*STRIDE +j
//Lapack/Fortran order (column-major): M_i,j -> M[i][j] -> &M +j*STRIDE +i
//const int off=i*10 +j; //C
const int off=j*9 +i; //Fortran
M[off]+= V[j+1]* V[i+1];
N[off]+=V1[j+1]*V1[i+1];
N[off]+=V2[j+1]*V2[i+1];
N[off]+=V3[j+1]*V3[i+1];
}
}
//now, solve the generalized eigenvector of the matrix pair (M,N):
//MC = nNC
//
//M,N are (reduced) 9x9 matrices constructed above,
//C is vector (infact, the coeff), n is scalar Lagrange multiplier
//
//if M,N were (full-size) 10x10 matrices, the N would be singular
//as the 1st row would contain only zeros,
//it is therefore reduced to 9x9 sacrifing the first row
//
//according to netlib (Lapack) docs, http://www.netlib.org/lapack/lug/node34.html
//type 1, Az=lBz -- A=M, B=N, z=C
//function: SSYGV
lapack_int itype=1;
char jobz='V';
char uplo='U';
lapack_int n=9;
float w[10];
float work[512];
lapack_int lwork=512;
lapack_int info;
LAPACK_ssygv(&itype,&jobz,&uplo,&n,M,&n,N,&n,w,work,&lwork,&info);
std::cout << "vertices considered: " << neigs.size() << "\n";
std::cout << "info=" << info << " (0 is OK)\n";
std::cout << "work(1)=" << work[0] << " (should be below 512)\n";
//if some error, report it to the caller
if (info != 0) return info;
//M is now matrix of eigenvectors
//it should hold (according to Lapack docs):
//Z^T N Z = I where Z is one eigenvector, I is identity matrix
//
//w holds eigenvalues in ascending order
//our result c[1]...c[9] is the eigenvector
Vladimír Ulman
committed
//corresponding to the smallest non-negative eigenvalue, so the j-th eigenvector
int j=0;
while (j < 9 && w[j] < 0.f) ++j;
//have we found some non-negative eigenvalue?
if (j == 9) return(-9999);
//also:
//the last missing coefficient c[0] we will determine by submitting
//the given input vertex to the algebraic expresion of the surface
//(given with coeffs) and equating it to zero:
coeffs[0]=0.f;
for (int i=0; i < 9; ++i)
{
Vladimír Ulman
committed
coeffs[i+1]=M[j*9 +i]; //copy eigenvector
coeffs[0]-=coeffs[i+1]*V[i+1]; //determine c[0]
Vladimír Ulman
committed
std::cout << "w(j)=" << w[j] << ", j=" << j << "\n";
bool ActiveMesh::GetPointOnQuadricSurface(const float x,const float y,
float &z1, float &z2,
const float (&coeffs)[10])
{
const float a=coeffs[9];
const float b=coeffs[3] +coeffs[5]*x +coeffs[6]*y;
const float c=coeffs[0] +coeffs[1]*x +coeffs[2]*y
+coeffs[4]*x*y +coeffs[7]*x*x +coeffs[8]*y*y;
const float sqArg=b*b - 4*a*c;
if (sqArg < 0.f) return false;
if (a == 0.f) return false;
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
z1=(-b + sqrtf(sqArg)) / (2.f*a);
z2=(-b - sqrtf(sqArg)) / (2.f*a);
return true;
}
float ActiveMesh::GetClosestPointOnQuadricSurface(Vector3F& point,
const float (&coeffs)[10])
{
//backup original input coordinate
const float x=point.x;
const float y=point.y;
const float z=point.z;
float tmp1,tmp2;
//list of possible coordinates
std::vector<Vector3F> pointAdepts;
//took a pair of coordinates, calculate the third one
//and make it an adept...
if (GetPointOnQuadricSurface(x,y,tmp1,tmp2,coeffs))
{
pointAdepts.push_back(Vector3F(x,y,tmp1));
pointAdepts.push_back(Vector3F(x,y,tmp2));
}
if (GetPointOnQuadricSurface(x,z,tmp1,tmp2,coeffs))
{
pointAdepts.push_back(Vector3F(x,tmp1,z));
pointAdepts.push_back(Vector3F(x,tmp2,z));
}
if (GetPointOnQuadricSurface(y,z,tmp1,tmp2,coeffs))
{
pointAdepts.push_back(Vector3F(tmp1,y,z));
pointAdepts.push_back(Vector3F(tmp2,y,z));
}
//are we doomed?
if (pointAdepts.size() == 0)
return (-999999.f);
//find the closest
int closestIndex=ChooseClosestPoint(pointAdepts,point);
//calc distance to it
point-=pointAdepts[closestIndex];
tmp1=point.Len();
//adjust the input/output point
point=pointAdepts[closestIndex];
return (tmp1);
}
int ActiveMesh::ChooseClosestPoint(const std::vector<Vector3F>& points,
const Vector3F& point)
{
int minIndex=-1;
float minSqDist=9999999999999.f;
Vector3F p;
for (unsigned int i=0; i < points.size(); ++i)
{
p=point;
p-=points[i];
if (p.LenQ() < minSqDist)
{
minIndex=i;
minSqDist=p.LenQ();
}
}
return minIndex;
}