示例#1
0
void wrap(long double * result, long double * elow, long double * eup, int * n, int * l, long double * a, long double * t, long double * m){

    *result =  eigenvalue(elow[0],eup[0],n[0],a[0],t[0],m[0],l[0]);

}
示例#2
0
void wrap(long double * result, long double * elow, long double * eup, int * n, int * l){

    *result =  eigenvalue(elow[0],eup[0],n[0],l[0]);

}
示例#3
0
static void eigen(std::vector<double> &inList, int inNb,
                  std::vector<double> &outList, int *outNb, int nbTime,
                  int nbNod, int nbComp, int lam)
{
  if(!inNb || (nbComp != 3 && nbComp != 9) || lam < 1 || lam > 3) return;

  // loop on elements
  int nb = inList.size() / inNb;
  for(std::size_t i = 0; i < inList.size(); i += nb) {
    // copy node coordinates
    for(int j = 0; j < 3 * nbNod; j++) outList.push_back(inList[i + j]);

    // loop on time steps
    for(int j = 0; j < nbTime; j++) {
      double *x = &inList[i];
      double *y = &inList[i + nbNod];
      double *z = &inList[i + 2 * nbNod];

      double GradVel[3][3];

      if(nbComp == 9) {
        // val is the velocity gradient tensor: we assume that it is
        // constant per element
        double *v = &inList[i + 3 * nbNod + nbNod * nbComp * j + nbComp * 0];
        GradVel[0][0] = v[0];
        GradVel[0][1] = v[1];
        GradVel[0][2] = v[2];
        GradVel[1][0] = v[3];
        GradVel[1][1] = v[4];
        GradVel[1][2] = v[5];
        GradVel[2][0] = v[6];
        GradVel[2][1] = v[7];
        GradVel[2][2] = v[8];
      }
      else if(nbComp == 3) {
        // FIXME: the following could be greatly simplified and
        // generalized by using the classes in shapeFunctions.h

        // val contains the velocities: compute the gradient tensor
        // from them
        const int MAX_NOD = 4;
        double val[3][MAX_NOD];
        for(int k = 0; k < nbNod; k++) {
          double *v = &inList[i + 3 * nbNod + nbNod * nbComp * j + nbComp * k];
          for(int l = 0; l < 3; l++) {
            val[l][k] = v[l];
          }
        }
        // compute gradient of shape functions
        double GradPhi_x[MAX_NOD][3];
        double GradPhi_ksi[MAX_NOD][3];
        double dx_dksi[3][3];
        double dksi_dx[3][3];
        double det;
        if(nbNod == 3) { // triangles
          double a[3], b[3], cross[3];
          a[0] = x[1] - x[0];
          a[1] = y[1] - y[0];
          a[2] = z[1] - z[0];
          b[0] = x[2] - x[0];
          b[1] = y[2] - y[0];
          b[2] = z[2] - z[0];
          prodve(a, b, cross);
          dx_dksi[0][0] = x[1] - x[0];
          dx_dksi[0][1] = x[2] - x[0];
          dx_dksi[0][2] = cross[0];
          dx_dksi[1][0] = y[1] - y[0];
          dx_dksi[1][1] = y[2] - y[0];
          dx_dksi[1][2] = cross[1];
          dx_dksi[2][0] = z[1] - z[0];
          dx_dksi[2][1] = z[2] - z[0];
          dx_dksi[2][2] = cross[2];
          inv3x3tran(dx_dksi, dksi_dx, &det);
          GradPhi_ksi[0][0] = -1;
          GradPhi_ksi[0][1] = -1;
          GradPhi_ksi[0][2] = 0;
          GradPhi_ksi[1][0] = 1;
          GradPhi_ksi[1][1] = 0;
          GradPhi_ksi[1][2] = 0;
          GradPhi_ksi[2][0] = 0;
          GradPhi_ksi[2][1] = 1;
          GradPhi_ksi[2][2] = 0;
        }
        else if(nbNod == 4) { // tetrahedra
          dx_dksi[0][0] = x[1] - x[0];
          dx_dksi[0][1] = x[2] - x[0];
          dx_dksi[0][2] = x[3] - x[0];
          dx_dksi[1][0] = y[1] - y[0];
          dx_dksi[1][1] = y[2] - y[0];
          dx_dksi[1][2] = y[3] - y[0];
          dx_dksi[2][0] = z[1] - z[0];
          dx_dksi[2][1] = z[2] - z[0];
          dx_dksi[2][2] = z[3] - z[0];
          inv3x3tran(dx_dksi, dksi_dx, &det);
          GradPhi_ksi[0][0] = -1;
          GradPhi_ksi[0][1] = -1;
          GradPhi_ksi[0][2] = -1;
          GradPhi_ksi[1][0] = 1;
          GradPhi_ksi[1][1] = 0;
          GradPhi_ksi[1][2] = 0;
          GradPhi_ksi[2][0] = 0;
          GradPhi_ksi[2][1] = 1;
          GradPhi_ksi[2][2] = 0;
          GradPhi_ksi[3][0] = 0;
          GradPhi_ksi[3][1] = 0;
          GradPhi_ksi[3][2] = 1;
        }
        else {
          Msg::Error("Lambda2 not ready for this type of element");
          return;
        }
        for(int k = 0; k < nbNod; k++) {
          for(int l = 0; l < 3; l++) {
            GradPhi_x[k][l] = 0.0;
            for(int m = 0; m < 3; m++) {
              GradPhi_x[k][l] += GradPhi_ksi[k][m] * dksi_dx[l][m];
            }
          }
        }
        // compute gradient of velocities
        for(int k = 0; k < 3; k++) {
          for(int l = 0; l < 3; l++) {
            GradVel[k][l] = 0.0;
            for(int m = 0; m < nbNod; m++) {
              GradVel[k][l] += val[k][m] * GradPhi_x[m][l];
            }
          }
        }
      }
      else
        for(int k = 0; k < 3; k++)
          for(int l = 0; l < 3; l++) GradVel[k][l] = 0.0;

      // compute the sym and antisymetric parts
      double sym[3][3];
      double asym[3][3];
      for(int m = 0; m < 3; m++) {
        for(int n = 0; n < 3; n++) {
          sym[m][n] = 0.5 * (GradVel[m][n] + GradVel[n][m]);
          asym[m][n] = 0.5 * (GradVel[m][n] - GradVel[n][m]);
        }
      }
      double a[3][3];
      for(int m = 0; m < 3; m++) {
        for(int n = 0; n < 3; n++) {
          a[m][n] = 0.0;
          for(int l = 0; l < 3; l++)
            a[m][n] += sym[m][l] * sym[l][n] + asym[m][l] * asym[l][n];
        }
      }

      // compute the eigenvalues
      double lambda[3];
      eigenvalue(a, lambda);
      for(int k = 0; k < nbNod; k++) outList.push_back(lambda[lam - 1]);
    }

    (*outNb)++;
  }
}
示例#4
0
Normal::Normal(PCD* pointcloud, float radius) {
	if (!pointcloud || pointcloud->numPoints == 0)
		return;

	//set up data structures
	cloud = pointcloud;
	norm = new float[pointcloud->numPoints * 3];
	curvature = new float[pointcloud->numPoints]();
	if (!pointcloud->kdtree)
		pointcloud->kdtree = new KdTree(pointcloud);
	
	for (int i=0;i<pointcloud->numPoints;i++) {
		float P[3] = {
			pointcloud->float_data[i * 4],
			pointcloud->float_data[i * 4 + 1],
			pointcloud->float_data[i * 4 + 2]
		};
		//form covariance matrix
		std::vector<int> neighbors;
		pointcloud->kdtree->search(&neighbors,P[0],P[1],P[2],radius);
		PCD::Quaternion center = pointcloud->getCentroid(&neighbors);
		double cov[9] = {}; //column major
		for (size_t j=0;j<neighbors.size();j++) {
			float deltaP[3] = {
				pointcloud->float_data[neighbors[j] * 4] - center.i,
				pointcloud->float_data[neighbors[j] * 4 + 1] - center.j,
				pointcloud->float_data[neighbors[j] * 4 + 2] - center.k,
			};
			cov[0] += deltaP[0] * deltaP[0];
			cov[1] += deltaP[1] * deltaP[0];
			cov[2] += deltaP[2] * deltaP[0];
			cov[3] += deltaP[0] * deltaP[1];
			cov[4] += deltaP[1] * deltaP[1];
			cov[5] += deltaP[2] * deltaP[1];
			cov[6] += deltaP[0] * deltaP[2];
			cov[7] += deltaP[1] * deltaP[2];
			cov[8] += deltaP[2] * deltaP[2];
		}
		//compute PCA
		double lambda_real[3], lambda_imag[3], v[9];
		eigenvalue(3,cov,lambda_real,lambda_imag,v);
		//get normal and curvature
		double minLambda;
		int minIndex;
		for (int j=0;j<3;j++) {
			curvature[i] += lambda_real[j];
			if (j==0 || lambda_real[j] < minLambda) {
				minIndex = j;
				minLambda = lambda_real[j];
			}
		}
		curvature[i] = minLambda / curvature[i];
		double N[3] = {
			v[minIndex * 3],
			v[minIndex * 3 + 1],
			v[minIndex * 3 + 2]
		};
		//normal points to origin?
		if (P[0] * N[0] + P[1] * N[1] + P[2] * N[2] < 0) {
			norm[i * 3] = N[0];
			norm[i * 3 + 1] = N[1];
			norm[i * 3 + 2] = N[2];
		} else {
			norm[i * 3] = -N[0];
			norm[i * 3 + 1] = -N[1];
			norm[i * 3 + 2] = -N[2];
		}
//		printf("%4f,%4f,%4f: %lu neighbors %4f,%4f,%4f %4f\n",
//			P[0],P[1],P[2],neighbors.size(),N[0],N[1],N[2],curvature[i]);
	}
}
	void SpatialCompositionAction::Perform(ArtifactSet &artifacts, ProgressListener &progress)
	{
		size_t imageCount = artifacts.ContaminatedData().ImageCount();
		std::vector<Image2DPtr> images(imageCount);
		for(size_t p=0;p<imageCount;++p)
			images[p] = Image2D::CreateZeroImagePtr(artifacts.ContaminatedData().ImageWidth(), artifacts.ContaminatedData().ImageHeight());

		std::string filename = artifacts.ImageSet()->File();
		SpatialMSImageSet set(filename);
		ImageSetIndex *index = set.StartIndex();
		size_t progressStep = 0, totalProgress = artifacts.ContaminatedData().ImageWidth() * artifacts.ContaminatedData().ImageHeight()/256;
		while(index->IsValid())
		{
			TimeFrequencyData *data = set.LoadData(*index);
			SpatialMatrixMetaData metaData(set.SpatialMetaData(*index));
			for(size_t p=0;p!=imageCount;++p)
			{
				switch(_operation)
				{
					case SumCrossCorrelationsOperation:
						images[p]->SetValue(metaData.TimeIndex(), metaData.ChannelIndex(), sumCrossCorrelations(data->GetImage(p)));
						break;
					case SumAutoCorrelationsOperation:
						images[p]->SetValue(metaData.TimeIndex(), metaData.ChannelIndex(), sumAutoCorrelations(data->GetImage(p)));
						break;
					case EigenvalueDecompositionOperation: {
						num_t value = eigenvalue(data->GetImage(p), data->GetImage(p+1));
						images[p]->SetValue(metaData.TimeIndex(), metaData.ChannelIndex(), value);
						images[p+1]->SetValue(metaData.TimeIndex(), metaData.ChannelIndex(), 0.0);
						++p;
						} break;
					case EigenvalueRemovalOperation: {
						std::pair<num_t, num_t> value = removeEigenvalue(data->GetImage(p), data->GetImage(p+1));
						images[p]->SetValue(metaData.TimeIndex(), metaData.ChannelIndex(), value.first);
						images[p+1]->SetValue(metaData.TimeIndex(), metaData.ChannelIndex(), value.second);
						++p;
						} break;
				}
			}
			delete data;
			index->Next();
			++progressStep;
			progress.OnProgress(*this, progressStep/256, totalProgress);
		}
		delete index;

		TimeFrequencyData newRevisedData = artifacts.RevisedData();
		for(size_t p=0;p<imageCount;++p)
			newRevisedData.SetImage(p, images[p]);
		
		newRevisedData.SetMask(artifacts.RevisedData());

		TimeFrequencyData *contaminatedData =
			TimeFrequencyData::CreateTFDataFromDiff(artifacts.ContaminatedData(), newRevisedData);
		contaminatedData->SetMask(artifacts.ContaminatedData());

		artifacts.SetRevisedData(newRevisedData);
		artifacts.SetContaminatedData(*contaminatedData);

		delete contaminatedData;

	}
示例#6
0
PView *GMSH_EigenvaluesPlugin::execute(PView *v)
{
  int iView = (int)EigenvaluesOptions_Number[0].def;

  PView *v1 = getView(iView, v);
  if(!v1) return v;

  PViewData *data1 = getPossiblyAdaptiveData(v1);
  if(data1->hasMultipleMeshes()){
    Msg::Error("Eigenvalues plugin cannot be run on multi-mesh views");
    return v;
  }

  PView *min = new PView();
  PView *mid = new PView();
  PView *max = new PView();

  PViewDataList *dmin = getDataList(min);
  PViewDataList *dmid = getDataList(mid);
  PViewDataList *dmax = getDataList(max);

  for(int ent = 0; ent < data1->getNumEntities(0); ent++){
    for(int ele = 0; ele < data1->getNumElements(0, ent); ele++){
      if(data1->skipElement(0, ent, ele)) continue;
      int numComp = data1->getNumComponents(0, ent, ele);
      if(numComp != 9) continue;
      int type = data1->getType(0, ent, ele);
      int numNodes = data1->getNumNodes(0, ent, ele);
      std::vector<double> *outmin = dmin->incrementList(1, type, numNodes);
      std::vector<double> *outmid = dmid->incrementList(1, type, numNodes);
      std::vector<double> *outmax = dmax->incrementList(1, type, numNodes);
      if(!outmin || !outmid || !outmax) continue;
      double xyz[3][8];
      for(int nod = 0; nod < numNodes; nod++)
        data1->getNode(0, ent, ele, nod, xyz[0][nod], xyz[1][nod], xyz[2][nod]);
      for(int i = 0; i < 3; i++){
        for(int nod = 0; nod < numNodes; nod++){
          outmin->push_back(xyz[i][nod]);
          outmid->push_back(xyz[i][nod]);
          outmax->push_back(xyz[i][nod]);
        }
      }
      for(int step = 0; step < data1->getNumTimeSteps(); step++){
        for(int nod = 0; nod < numNodes; nod++){
          double val[9], w[3];
          for(int comp = 0; comp < numComp; comp++)
            data1->getValue(step, ent, ele, nod, comp, val[comp]);
          double A[3][3] = {{val[0], val[1], val[2]},
                            {val[3], val[4], val[5]},
                            {val[6], val[7], val[8]}};
          eigenvalue(A, w);
          outmin->push_back(w[2]);
          outmid->push_back(w[1]);
          outmax->push_back(w[0]);
        }
      }
    }
  }
  
  for(int i = 0; i < data1->getNumTimeSteps(); i++){
    double time = data1->getTime(i);
    dmin->Time.push_back(time);
    dmid->Time.push_back(time);
    dmax->Time.push_back(time);
  }
  dmin->setName(data1->getName() + "_MinEigenvalues");
  dmin->setFileName(data1->getName() + "_MinEigenvalues.pos");
  dmin->finalize();
  dmid->setName(data1->getName() + "_MidEigenvalues");
  dmid->setFileName(data1->getName() + "_MidEigenvalues.pos");
  dmid->finalize();
  dmax->setName(data1->getName() + "_MaxEigenvalues");
  dmax->setFileName(data1->getName() + "_MaxEigenvalues.pos");
  dmax->finalize();

  return 0;
}
void  Rotation_matrix(double **experiment_matrix,char base,double ** rotation_matrix)
{
    int N;
    double ** strandard_matrix;
    double ** covariance_matrix;
    double ** symmetric_matrix;
    double * eigenvector_matrix;

    eigenvector_matrix=dvector(0,3);
    covariance_matrix=dmatrix(0,2,0,2);
    symmetric_matrix=dmatrix(0,3,0,3);

//
    switch(base)
    {
    case 'A':
        N=9;
        strandard_matrix=dmatrix(0,N-1,0,3);
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<3; j++)
            {
                strandard_matrix[i][j]=A_origin[i][j];
            }
        }
        break;
    case 'C':
        N=6;
        strandard_matrix=dmatrix(0,N-1,0,3);
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<3; j++)
            {
                strandard_matrix[i][j]=C_origin[i][j];
            }
        }
        break;
    case 'G':
        N=9;
        strandard_matrix=dmatrix(0,N-1,0,3);
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<3; j++)
            {
                strandard_matrix[i][j]=G_origin[i][j];
            }
        }
        break;
    case 'T':
        N=6;
        strandard_matrix=dmatrix(0,N-1,0,3);
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<3; j++)
            {
                strandard_matrix[i][j]=T_origin[i][j];
            }
        }
        break;
    case 'U':
        N=6;
        strandard_matrix=dmatrix(0,N-1,0,3);
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<3; j++)
            {
                strandard_matrix[i][j]=U_origin[i][j];
            }
        }
        break;
    default:
        break;
    }
    //create the covariance matrix
    double s_sum_x=0,s_sum_y=0,s_sum_z=0;
    double e_sum_x=0,e_sum_y=0,e_sum_z=0;
    double ** se;
    double **siie;
    se=dmatrix(0,2,0,2);
    siie=dmatrix(0,2,0,2);
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            se[i][j]=0;
        }
    }
    for(int m=0; m<3; m++)
    {
        for(int n=0; n<3; n++)
        {
            for(int t=0; t<N; t++)
            {
                se[m][n]+=(strandard_matrix[t][m]*experiment_matrix[t][n]);
            }
            //	cout<<se[m][n]<<endl;
        }
    }
    for(int i=0; i<N; i++)
    {
        s_sum_x+=strandard_matrix[i][0];
        //cout<<s_sum_x<<endl;
        s_sum_y+=strandard_matrix[i][1];
        s_sum_z+=strandard_matrix[i][2];
        e_sum_x+=experiment_matrix[i][0];
        //	cout<<e_sum_x<<endl;
        e_sum_y+=experiment_matrix[i][1];
        e_sum_z+=experiment_matrix[i][2];
    }
    siie[0][0]=s_sum_x*e_sum_x/N;
    //cout<<siie[0][0]<<endl;
    siie[0][1]=s_sum_x*e_sum_y/N;
    siie[0][2]=s_sum_x*e_sum_z/N;
    siie[1][0]=s_sum_y*e_sum_x/N;
    siie[1][1]=s_sum_y*e_sum_y/N;
    siie[1][2]=s_sum_y*e_sum_z/N;
    siie[2][0]=s_sum_z*e_sum_x/N;
    siie[2][1]=s_sum_z*e_sum_y/N;
    siie[2][2]=s_sum_z*e_sum_z/N;

    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            covariance_matrix[i][j]=(se[i][j]-siie[i][j])/(N-1);
            //	cout<<covariance_matrix[i][j]<<endl;
        }
    }

    symmetric_matrix[0][0]=covariance_matrix[0][0]+covariance_matrix[1][1]+covariance_matrix[2][2];
    symmetric_matrix[0][1]=covariance_matrix[1][2]-covariance_matrix[2][1];
    symmetric_matrix[0][2]=covariance_matrix[2][0]-covariance_matrix[0][2];
    symmetric_matrix[0][3]=covariance_matrix[0][1]-covariance_matrix[1][0];
    symmetric_matrix[1][0]=covariance_matrix[1][2]-covariance_matrix[2][1];
    symmetric_matrix[1][1]=covariance_matrix[0][0]-covariance_matrix[1][1]-covariance_matrix[2][2];
    symmetric_matrix[1][2]=covariance_matrix[0][1]+covariance_matrix[1][0];
    symmetric_matrix[1][3]=covariance_matrix[2][0]+covariance_matrix[0][2];
    symmetric_matrix[2][0]=covariance_matrix[2][0]-covariance_matrix[0][2];
    symmetric_matrix[2][1]=covariance_matrix[0][1]+covariance_matrix[1][0];
    symmetric_matrix[2][2]=-covariance_matrix[0][0]+covariance_matrix[1][1]-covariance_matrix[2][2];
    symmetric_matrix[2][3]=covariance_matrix[1][2]+covariance_matrix[2][1];
    symmetric_matrix[3][0]=covariance_matrix[0][1]-covariance_matrix[1][0];
    symmetric_matrix[3][1]=covariance_matrix[2][0]+covariance_matrix[0][2];
    symmetric_matrix[3][2]=covariance_matrix[1][2]+covariance_matrix[2][1];
    symmetric_matrix[3][3]=-covariance_matrix[0][0]-covariance_matrix[1][1]+covariance_matrix[2][2];


    double eigenva=eigenvalue(symmetric_matrix,4);

    eigenvector(symmetric_matrix,4,eigenva,eigenvector_matrix);

    rotation_matrix[0][0]=eigenvector_matrix[0]*eigenvector_matrix[0]+eigenvector_matrix[1]*eigenvector_matrix[1]-eigenvector_matrix[2]*eigenvector_matrix[2]-eigenvector_matrix[3]*eigenvector_matrix[3];
    rotation_matrix[0][1]=2*(eigenvector_matrix[1]*eigenvector_matrix[2]-eigenvector_matrix[0]*eigenvector_matrix[3]);
    rotation_matrix[0][2]=2*(eigenvector_matrix[1]*eigenvector_matrix[3]+eigenvector_matrix[0]*eigenvector_matrix[2]);
    rotation_matrix[1][0]=2*(eigenvector_matrix[2]*eigenvector_matrix[1]+eigenvector_matrix[0]*eigenvector_matrix[3]);
    rotation_matrix[1][1]=eigenvector_matrix[0]*eigenvector_matrix[0]-eigenvector_matrix[1]*eigenvector_matrix[1]+eigenvector_matrix[2]*eigenvector_matrix[2]-eigenvector_matrix[3]*eigenvector_matrix[3];
    rotation_matrix[1][2]=2*(eigenvector_matrix[2]*eigenvector_matrix[3]-eigenvector_matrix[0]*eigenvector_matrix[1]);
    rotation_matrix[2][0]=2*(eigenvector_matrix[3]*eigenvector_matrix[1]-eigenvector_matrix[0]*eigenvector_matrix[2]);
    rotation_matrix[2][1]=2*(eigenvector_matrix[3]*eigenvector_matrix[2]+eigenvector_matrix[0]*eigenvector_matrix[1]);
    rotation_matrix[2][2]=eigenvector_matrix[0]*eigenvector_matrix[0]-eigenvector_matrix[1]*eigenvector_matrix[1]-eigenvector_matrix[2]*eigenvector_matrix[2]+eigenvector_matrix[3]*eigenvector_matrix[3];

}