Esempio n. 1
0
int cg(MAT &A,VEC b,VEC &x,int maxIter,double tol){
	int n = A.dim();
	int k = 0;
	VEC x_next(n);
	VEC r_next(n);
	VEC p_next(n);
	VEC r(n);
	VEC p(n);
	MAT L(n);
    VEC cpr(n);
	double alpha,beta;
	double err;
	double diff;
	r = b - A*x;					//initial condition
	p = r;
	while(k<maxIter){				//conjugate gradient decent
		alpha = (r*r)/(p*(A*p));
		x_next = x + alpha*p;
		r_next = r - alpha*(A*p);
		beta = (r_next*r_next)/(r*r);
		p_next = r_next + beta*p;
		k++;
		x = x_next;					//assign to the x,r,p of the next iteration 
		r = r_next;
		p = p_next;
		err = pow((r*r)/n,0.5);
		if(err<tol)					//see if the error is smaller than the defined tolerance. if so, break out of the loop 
			break;
	}
        diff = 0;
        //the answer from hw4

        L = cholesky(A);                        //in-place Cholesky Decomposition
        cpr = choSolve(L,b);                      //solve the linear system by forward and backward substitution
        //use the same method to compute the error between hw4 and hw5, see if < 10^-7. if not, decrease the tol
        for(int i=0;i<n;i++){
                diff = max(diff,fabs(x[i]-cpr[i]));		//use infinity norm to compute the error
        }
        printf("error between hw4 and hw6(infinity-norm): %e\n",diff);

	return k;	
}
Esempio n. 2
0
static void ShowEigs(const MAT& eigvals)
{
    double min_eig = eigvals(0) / 1000;
    double var_all = SumElems(eigvals);
    double var10 = 0;
    int neigs = NSIZE(eigvals);
    for (int i = 0; i < MIN(10, neigs); i++)
        var10 += eigvals(i);
    int iprint = 0;
    while (iprint < neigs && eigvals(iprint) > min_eig && iprint < 9)
        iprint++;
    if (iprint < neigs + 1) // show first small eig value, for context
        iprint++;
    lprintf("%.0f%% percent variance is explained by %s%d shape eigs:",
            100 * var10 / var_all, (iprint < neigs? "the first ": ""), iprint);
    const MAT eigvals_t(eigvals.t());
    for (int i = 0; i < iprint; i++)
        lprintf(" %.0f", 100 * eigvals_t(i) / var_all);
    lprintf("%%\n");
}
Esempio n. 3
0
int EVqr(MAT &A,double tol,int maxiter){
    int n=A.dim();
    int k=0;
    double err=10;
    MAT Q(n),R(n),T(A); 
    while(err>tol && k<maxiter){
        QRDecomp(T,Q,R);  			//QR factorization
        T=R*Q;						//matrix multiplication
        err = ConvErr(T);			//get the error term in each iteration
        k++;
    }
        
    printf("the three largest EV:\n");
	printf("%lf %lf %lf\n",T[0][0],T[1][1],T[2][2]);
	printf("the three smallest EV:\n");
	printf("%lf %lf %lf\n",T[n-1][n-1],T[n-2][n-2],T[n-3][n-3]);
	printf("iter = %d\n",k);
	printf("error = %e\n",err);
    A=T;
    return k;
}
Esempio n. 4
0
void compute_M_row_column_up(int row, int column, MAT & M, MAT2& sign_Fs, MAT2& Fe_M, typename MAT::type det_rat) {

  typedef typename MAT::type SCALAR;

  SCALAR det_rat_inv=1./det_rat;
  double sign=((row+column)%2==0 ? 1 : -1);
  Eigen::Matrix<SCALAR,Eigen::Dynamic,Eigen::Dynamic> M_sign_Fs(sign_Fs.size(),1);
  M_sign_Fs = M.block()*sign_Fs;

  assert(M.size1()==M.size2());
  MAT M_new(M.size1()+1,M.size2()+1);
  int i_new;
  int j_new;
    
  // element (j,i)
  M_new(column,row) = sign*det_rat_inv;
  
  // row j and column i
  for (int k=0; k<M.size1(); k++) {
    i_new = (k<column ? k : k+1);
    j_new = (k<row ? k : k+1);	
	  M_new(i_new,row) = -M_sign_Fs(k)*det_rat_inv;
	  M_new(column,j_new) = -sign*Fe_M(k)*det_rat_inv;
  }
  
  // remaining elements
  for (int k=0; k<M.size1(); k++) {
    i_new = (k<column ? k : k+1);
    for (int l=0; l<M.size1(); l++) {
      j_new = (l<row ? l : l+1);
	    M_new(i_new, j_new) = M(k,l) + M_sign_Fs(k)*Fe_M(l)*det_rat_inv;
    }
  }
  
  M_new.swap(M);
  return;
}  
Esempio n. 5
0
static void GenShapeMod(
    Shape&           meanshape,      // out: n x 2
    VEC&             eigvals,        // out: 2n x 1
    MAT&             eigvecs,        // out: 2n x 2n, inverse of eigs of cov mat
    vec_Shape&       aligned_shapes, // out: shapes aligned to refshape
    const ShapeFile& sh,             // in
    int              irefshape,      // in: index of shape used to align other shapes
    const vec_int&   nused_vec,      // in: vec [nshapes] of int
    const char*      outdir,         // in: output directory (-d flag)
    const char*      modname,        // in: e.g. "yaw00"
    const char*      cmdline,        // in: command line used to invoke tasm
    bool             force_facedet,  // in: facedet the images themselves (ignore shapefile facedets)
    bool             write_detpars)  // in: write the image detpars to facedet.shape
{
    CopyShapeVec(aligned_shapes, sh.shapes_);

    // after this, mean shape and aligned_shapes will be aligned to ref shape
    AlignTrainingShapes(meanshape, aligned_shapes,
                        aligned_shapes[irefshape]);

    MAT cov(CalcShapeCov(aligned_shapes, meanshape));
    EigsOfSymMat(eigvecs, eigvals, cov);

    if (eigvals(0) < 0.1) // number is arbitrary
        lprintf("eigen data invalid "
                "(not enough variation in shapes, max eigenvalue is %g)", eigvals(0));

    eigvals *= 1000 / eigvals(0); // normalize so biggest eigval is 1000 (to match old Stasm)

    ZapSmallEigs(eigvals, eigvecs);

    eigvecs = eigvecs.t(); // invert (eigvecs is orthog so transpose is inverse)

    ShowEigs(eigvals);

    // now align mean shape to the face detector frame
    MeanShapeAlignedToFaceDets(meanshape,
        sh, nused_vec, outdir, modname, cmdline, force_facedet, write_detpars);
}
Esempio n. 6
0
int EVqrShifted(MAT &A,double mu,double tol,int maxiter){
    int iter = 0 ;
    MAT T(A);
    MAT R(A.dim()), Q(A.dim()),muI(A.dim());
    
    //since we are not able to access column of a matrix easily, so we access row of matrices as substitute.
    for (int i =0 ; i < A.dim(); i++)
		muI[i][i] = mu;

    do{
	for(int i=0;i<A.dim();i++){           //subtract mu*I before QR factorization
		T[i][i] = T[i][i] - mu;
	}
	
	//QR decomposition
	QRDecomp(T,Q,R);	
	T = R * Q;
	//QR decomposition ends		
	for(int i=0;i<A.dim();i++){           //add mu*I back
		T[i][i] = T[i][i] + mu;
    }

	iter++;	
	
    }while(  (iter < maxiter) && (QRerror(T) > tol)  );
        
    printf("Largest 3 eigenvalues: ");
    for (int i = 0; i < 3; i++)
	printf(" %lf;",T[i][i]);
    printf("\nSmallest 3 eigenvalues: ");
    for (int i = A.dim()-1; i > A.dim()-4; i--)
	printf(" %lf;",T[i][i]);
    //printf("\niter %d\nerror: %E\n",iter,QRerror(A_k));
    return iter;

}
Esempio n. 7
0
VEC choSolve(MAT &L,VEC b)
{
    VEC x(b);

	//forward substitutions
	for (int i=0; i<x.len(); i++)
	{	
		x[i] /= L[i][i];
		for (int j=i+1; j<L.dim(); j++)
			x[j] -= L[j][i]*x[i];
		
	}
	
	//backward substitutions
	for (int i=(x.len()-1); i>=0; i--)
	{
		x[i] /= L[i][i];
		for (int j=i-1; j>=0; j--)
			x[j] -= L[i][j]*x[i];
	}	

	return x;
} 
Esempio n. 8
0
MAT inverse(MAT &A)
{
    VEC temp(A.dim());
    // I: identity matrix; inv: inverse of A; LU: LU in-place
    MAT LU(A.dim()), a = A, I(A.dim()), inv(A.dim());
    LU = luFact(a);       //LU in-place decomposition
    // Form identity matrix
    for (int i =0; i < A.dim(); i++){
	I[i][i] = 1;
    }
    // solve inverse of A. Answer is stored row by row
    for (int i = 0; i< A.dim(); i++){	
	temp=fwdSubs(LU,I[i]);   //forward substitution
	inv[i]= bckSubs(LU,temp);//backward substitution
    }
    // we need the result of inverse() column by column, so return transpose matrix of inv
    return inv.tpose();
}
Esempio n. 9
0
void construct_blas_matrix(MAT & M, const operator_container_t &creation_operators,
  const operator_container_t &annihilation_operators, double BETA, const HYB & F)
{

  int N = creation_operators.size();
  M.resize(N, N);
  int row = -1;
  int column = -1;
  for (operator_container_t::iterator ita = annihilation_operators.begin(); ita != annihilation_operators.end(); ita++) {
    row++;
    for (operator_container_t::iterator itc = creation_operators.begin(); itc != creation_operators.end(); itc++) {
      column++;

      double argument = ita->time() - itc->time();
      double sign = 1;
      if (argument < 0) {
        argument += BETA;
        sign = -1;
      }
      M(row, column) = interpolate_F(argument, BETA, F[ita->flavor()][itc->flavor()]) * sign;
    }
    column = -1;
  }
}
Esempio n. 10
0
void printMat(MAT& mat, std::string label="", int precision=2)
{
	int width = precision + 7;
	std::cout<<label<<"["<<mat.size1()<<","<<mat.size2()<<"] "<<"\n";
	std::cout<<"[";
	for(int i=0;i<mat.size1();i++)
	{
		if ( i != 0) std::cout<<" ";
		for(int j=0;j<mat.size2();j++)
		{
			printf("%*.*e",width,precision,mat(i,j));
			//std::cout<<mat(i,j);
			if(j!=mat.size2()-1) std::cout<<", ";
		}
		std::cout<<";";
		if ( i == mat.size1()-1) std::cout<<"]";
		std::cout<<std::endl<<std::endl;
	}
}
Esempio n. 11
0
static void InitGradMagAndOrientMats(
    MAT&         magmat,    // out: grad mag mat
    MAT&         orientmat, // out: grad ori mat
    const Image& img)       // in:  ROI scaled to current pyramid level
{
    const int nrows = img.rows, nrows1 = img.rows-1;
    const int ncols = img.cols, ncols1 = img.cols-1;
    const double bins_per_degree = BINS_PER_HIST / 360.;

    magmat.create(nrows, ncols);
    orientmat.create(nrows, ncols);

    for (int y = 0; y < nrows1; y++)
    {
        const byte* const buf    = (byte*)(img.data) + y     * ncols;
        const byte* const buf_x1 = (byte*)(img.data) + y     * ncols + 1;
        const byte* const buf_y1 = (byte*)(img.data) + (y+1) * ncols;

        double* const magbuf    = Buf(magmat)    + y * ncols;
        double* const orientbuf = Buf(orientmat) + y * ncols;

        for (int x = 0; x < ncols1; x++)
        {
            const byte   pixel  = buf[x];
            const double xdelta = buf_x1[x] - pixel;
            const double ydelta = buf_y1[x] - pixel;

            magbuf[x] = sqrt(SQ(xdelta) + SQ(ydelta));

            double orient =
                RadsToDegrees(atan2(ydelta, xdelta)); // -180 <= orient < 180
            if (orient < 0)
                orient += 360;                        // 0 <= orient < 360
            orientbuf[x] = orient * bins_per_degree;  // 0 <= orient < BINS_PER_HIST
        }
    }
    // fill bottom and right edges
    magmat.row(nrows1) = 0;
    magmat.col(ncols1) = 0;
    orientmat.row(nrows1) = 0;
    orientmat.col(ncols1) = 0;
}
Esempio n. 12
0
int compute_M_shift_start(double new_t_start, int k, int new_position, int flavor_ins,
        MAT & M, const operator_container_t & annihilation_operators, const HYB & F, double BETA, SCALAR det_rat) {

  std::vector<SCALAR> R(M.size1(), 0), M_k(M.size1(), 0), Fs(M.size1(), 0);

  operator_container_t::const_iterator ita = annihilation_operators.begin();
  for (int i = 0; i < (int) M_k.size(); i++) {
    M_k[i] = M(k, i);
    Fs[i] = interpolate_F(ita->time() - new_t_start, BETA, F[ita->flavor()][flavor_ins]);
    ita++;
  }

  for (int i = 0; i < (int) R.size(); i++) {
    if (i != k) {
      for (int j = 0; j < (int) R.size(); j++)
        R[i] += M(i, j) * Fs[j];
    }
  }

  for (int n = 0; n < (int) M.size1(); n++) {
    if (n != k) {
      for (int m = 0; m < (int) M.size1(); m++) {
        M(n, m) -= M_k[m] * R[n] / det_rat;
      }
    } else {
      for (int m = 0; m < (int) M.size1(); m++) {
        M(n, m) = M_k[m] / det_rat;
      }
    }
  }

  //swap rows
  move_row(M, k, new_position);

  return std::abs(k-new_position);
}  
Esempio n. 13
0
double QRerror(MAT &A){
    double max = std::abs(A[1][0]);
    for (int i = 2; i< A.dim(); i++)
	max = std::max(std::abs(A[i][i-1]),max);
    return max;
}
Esempio n. 14
0
int jacobi(MAT &A,VEC b,VEC &x, int maxIter, double tol){
	int n = A.dim();
	VEC S(n);			//summation vector
	VEC G(n);			//diagonal of A
	VEC tmp(n);			//save the x before iterative method
	MAT L(n);			
	VEC cpr(n);			//the result of hw4
	//iterative
	int k=0;
	double err1;
	double err2;
	double errinf;
	double diff1,diff2,diffinf;
	while(k<maxIter){
		for(int i=0;i<n;i++){
			S[i] = 0;		//initialize the sum vector to 0
		}
		for(int i=0;i<n;i++){
			G[i] = A[i][i];		
			//save the previous vector x to tmp(for comparison between iteration k and k+1)
			tmp[i] = x[i]; 
			for(int j=0;j<n;j++){
				if(j!=i){
					S[i] = S[i] + A[i][j]*x[j];	//sum the Aij*xj, where i is not equal to j
				}
			}
		}
		for(int i=0;i<n;i++){
			x[i] = (1/G[i])*(b[i]-S[i]);		//compute the new solution of x at iteration k
		}
		k++;						//after each iteration, k=k+1
		err1 = 0;	//1-norm
		err2 = 0;	//2-norm
		errinf = 0;	//infinity-norm
		
		
		for(int i=0;i<n;i++){
			err1 += fabs(x[i] - tmp[i]);		//sum of absolute error
			err2 += pow(x[i] - tmp[i], 2);		//sum of square error
			errinf = max(errinf,fabs(x[i]-tmp[i]));	//take the maximum absolute difference as error
		}						//max(a,b) defined on the top
		err2 = pow(err2,0.5);				//find the square root of err2 and get the 2-norm
//you can change err1 to err2 or errinf to get different number of iterations k based on which p-norm you preferred
        		
		if(err1<tol){
			break;
		}
	}
	diff1 = 0;
        diff2 = 0;
        diffinf = 0;
	//the answer from hw4

        L = cholesky(A);                        //in-place Cholesky Decomposition
        cpr = choSolve(L,b);                      //solve the linear system by forward and backward substitution
        //use the same method to compute the error between hw4 and hw5, see if < 10^-7. if not, decrease the tol
        for(int i=0;i<n;i++){
        	diff1 += fabs(x[i] - cpr[i]);
                diff2 += pow(x[i] - cpr[i], 2);
                diffinf = max(diffinf,fabs(x[i]-cpr[i]));
        }
        diff2 = pow(diff2,0.5);
        printf("error between hw4 and hw5(1-norm 2-norm infinity-norm): %e %e %e\n",diff1,diff2,diffinf);
	return k;
}
Esempio n. 15
0
int gaussSeidel(MAT &A,VEC b,VEC &x,int maxIter,double tol)
{
	int n = A.dim();
	VEC S(n);
	VEC G(n);
	VEC tmp(n);
	MAT L(n);
	VEC cpr(n);
	//iterative
	int k=0;
	double err1;
	double err2;
	double errinf;
	double diff1,diff2,diffinf;
	while(k<maxIter){
		for(int i=0;i<n;i++){
			S[i] = 0;
		}
		for(int i=0;i<n;i++){
			G[i] = A[i][i];
			tmp[i] = x[i];
			for(int j=0;j<n;j++){
				if(j!=i){
					S[i] = S[i] + A[i][j]*x[j];
				}
			}
			x[i] = (1/G[i])*(b[i]-S[i]);	
		//both x[i] and S[i] are computed in the same for loop, so some updated values are used in the current iteration
		}
		k++;
		err1 = 0;
		err2 = 0;
		errinf = 0;
		for(int i=0;i<n;i++){
			err1 += fabs(x[i] - tmp[i]);
			err2 += pow(x[i] - tmp[i], 2);
			errinf = max(errinf,fabs(x[i]-tmp[i]));
		}
		err2 = pow(err2,0.5);
	
		if(err1<tol){
			break;
		}
	}
	diff1 = 0;
        diff2 = 0;
        diffinf = 0;
	//the answer from hw4

        L = cholesky(A);                        //in-place Cholesky Decomposition
        cpr = choSolve(L,b);                      //solve the linear system by forward and backward substitution
        //use the same method to compute the error between hw4 and hw5, see if < 10^-7. if not, decrease the tol
        for(int i=0;i<n;i++){
        	diff1 += fabs(x[i] - cpr[i]);
                diff2 += pow(x[i] - cpr[i], 2);
                diffinf = max(diffinf,fabs(x[i]-cpr[i]));
        }
        diff2 = pow(diff2,0.5);
        printf("error between hw4 and hw5(1-norm 2-norm infinity-norm): %e %e %e\n",diff1,diff2,diffinf);

	return k;	
}
Esempio n. 16
0
int sgs(MAT &A,VEC b,VEC &x,int maxIter,double tol)
{
        int n = A.dim();
        VEC S(n);
        VEC G(n);
        VEC tmp(n);
	MAT L(n);
	VEC cpr(n);
        //iterative
        int k=0;
        double err1;
        double err2;
        double errinf;
	double diff1,diff2,diffinf;
        while(k<maxIter){
                for(int i=0;i<n;i++){
                        S[i] = 0;
                }
		//forward gauss-seidel 
		//compute vector x from x[0] to x[n-1]
                for(int i=0;i<n;i++){
                        G[i] = A[i][i];
                        tmp[i] = x[i];
                        for(int j=0;j<n;j++){
                                if(j!=i){
                                        S[i] = S[i] + A[i][j]*x[j];
                                }
                        }
                        x[i] = (1/G[i])*(b[i]-S[i]);
              	}
		//backward gauss-seidel
		//use the reslults of forward gauss-seidel to compute vector x
		//direction : from x[n-1] to x[0]
		for(int i=0;i<n;i++){
                        S[i] = 0;	//we also need to initialize the sum vector.
                }
		for(int i=n-1;i>=0;i--){
                        for(int j=n-1;j>=0;j--){
                                if(j!=i){
                                        S[i] = S[i] + A[i][j]*x[j];
                                }
                        }
                        x[i] = (1/G[i])*(b[i]-S[i]);
                }
                k++;
                err1 = 0;
                err2 = 0;
                errinf = 0;
                for(int i=0;i<n;i++){
                        err1 += fabs(x[i] - tmp[i]);
                        err2 += pow(x[i] - tmp[i], 2);
                        errinf = max(errinf,fabs(x[i]-tmp[i]));
                }
                err2 = pow(err2,0.5);
               
		if(err1<tol){
			break;
		}

	}
	diff1 = 0;
        diff2 = 0;
        diffinf = 0;
	//the answer from hw4

        L = cholesky(A);                        //in-place Cholesky Decomposition
        cpr = choSolve(L,b);                      //solve the linear system by forward and backward substitution
        //use the same method to compute the error between hw4 and hw5, see if < 10^-7. if not, decrease the tol
        for(int i=0;i<n;i++){
        	diff1 += fabs(x[i] - cpr[i]);
                diff2 += pow(x[i] - cpr[i], 2);
                diffinf = max(diffinf,fabs(x[i]-cpr[i]));
        }
        diff2 = pow(diff2,0.5);
        printf("error between hw4 and hw5(1-norm 2-norm infinity-norm): %e %e %e\n",diff1,diff2,diffinf);
        return k;
}
Esempio n. 17
0
  /// \brief Solve AX=B for X with Chebyshev iteration with left
  ///   diagonal scaling, imitating ML's implementation.
  ///
  /// \pre A must be real-valued and symmetric positive definite.
  /// \pre numIters >= 0
  /// \pre eigRatio >= 1
  /// \pre 0 < lambdaMax
  /// \pre All entries of D_inv are positive.
  ///
  /// \param A [in] The matrix A in the linear system to solve.
  /// \param B [in] Right-hand side(s) in the linear system to solve.
  /// \param X [in] Initial guess(es) for the linear system to solve.
  /// \param numIters [in] Number of Chebyshev iterations.
  /// \param lambdaMax [in] Estimate of max eigenvalue of D_inv*A.
  /// \param lambdaMin [in] Estimate of min eigenvalue of D_inv*A.  We
  ///   only use this to determine if A is the identity matrix.
  /// \param eigRatio [in] Estimate of max / min eigenvalue ratio of
          ///   D_inv*A.  We use this along with lambdaMax to compute the
          ///   Chebyshev coefficients.  This need not be the same as
          ///   lambdaMax/lambdaMin.
          /// \param D_inv [in] Vector of diagonal entries of A.  It must have
          ///   the same distribution as b.
          void
          mlApplyImpl (const MAT& A,
                   const MV& B,
                   MV& X,
                   const int numIters,
                   const ST lambdaMax,
                   const ST lambdaMin,
                   const ST eigRatio,
                   const V& D_inv)
          {
            const ST zero = Teuchos::as<ST> (0);
            const ST one = Teuchos::as<ST> (1);
            const ST two = Teuchos::as<ST> (2);

            MV pAux (B.getMap (), B.getNumVectors ()); // Result of A*X
            MV dk (B.getMap (), B.getNumVectors ()); // Solution update
            MV R (B.getMap (), B.getNumVectors ()); // Not in original ML; need for B - pAux

            ST beta = Teuchos::as<ST> (1.1) * lambdaMax;
            ST alpha = lambdaMax / eigRatio;

            ST delta = (beta - alpha) / two;
            ST theta = (beta + alpha) / two;
            ST s1 = theta / delta;
            ST rhok = one / s1;

            // Diagonal: ML replaces entries containing 0 with 1.  We
            // shouldn't have any entries like that in typical test problems,
            // so it's OK not to do that here.

            // The (scaled) matrix is the identity: set X = D_inv * B.  (If A
            // is the identity, then certainly D_inv is too.  D_inv comes from
            // A, so if D_inv * A is the identity, then we still need to apply
            // the "preconditioner" D_inv to B as well, to get X.)
            if (lambdaMin == one && lambdaMin == lambdaMax) {
              solve (X, D_inv, B);
              return;
            }

            // The next bit of code is a direct translation of code from ML's
            // ML_Cheby function, in the "normal point scaling" section, which
            // is in lines 7365-7392 of ml_smoother.c.

            if (! zeroStartingSolution_) {
              // dk = (1/theta) * D_inv * (B - (A*X))
              A.apply (X, pAux); // pAux = A * X
              R = B;
              R.update (-one, pAux, one); // R = B - pAux
              dk.elementWiseMultiply (one/theta, D_inv, R, zero); // dk = (1/theta)*D_inv*R
              X.update (one, dk, one); // X = X + dk
            } else {
              dk.elementWiseMultiply (one/theta, D_inv, B, zero); // dk = (1/theta)*D_inv*B
              X = dk;
            }

            ST rhokp1, dtemp1, dtemp2;
            for (int k = 0; k < numIters-1; ++k) {
              A.apply (X, pAux);
              rhokp1 = one / (two*s1 - rhok);
              dtemp1 = rhokp1*rhok;
              dtemp2 = two*rhokp1/delta;
              rhok = rhokp1;

              R = B;
              R.update (-one, pAux, one); // R = B - pAux
              // dk = dtemp1 * dk + dtemp2 * D_inv * (B - pAux)
              dk.elementWiseMultiply (dtemp2, D_inv, B, dtemp1);
              X.update (one, dk, one); // X = X + dk
            }
          }
int Storm3D_ParticleSystem::QuadArray::lock(VXFORMAT_PART *pointer, int particleOffset, Storm3D_Scene *scene)
{
	m_partOffset = particleOffset;
	pointer += particleOffset * 4;
	VXFORMAT_PART *vp = pointer;

	float frameWidth = 0.0f;
	float frameHeight = 0.0f;
	if(m_animInfo.numFrames > 1) 
	{
		frameWidth = 1.0f / m_animInfo.textureUSubDivs;
		frameHeight = 1.0f / m_animInfo.textureVSubDivs;
	}
	
	BYTE *verts = (BYTE*)&vp[0].position;
	BYTE *uvs = (BYTE*)&vp[0].texcoords;
	BYTE *colors = (BYTE*)&vp[0].color;
	
	DWORD stride = sizeof(VXFORMAT_PART);
	D3DXMATRIX mv = scene->camera.GetViewMatrix();
	DWORD c0 = 0;

	// Up rotation
	MAT view(scene->camera.GetViewMatrix());
	VC3 worldUp(0, 1.f, 0);
	view.RotateVector(worldUp);
	QUAT q;
	rotateToward(worldUp, VC3(0, 0, 1.f), q);
	MAT g;
	g.CreateRotationMatrix(q);

	for(int i = 0; i < m_numParts; i++) 
	{
		Storm3D_PointParticle& p = m_parts[i];

		float sa = sinf(p.angle);
		float ca = cosf(p.angle);
		float hsize = 0.5f*p.size;
			
		float x1,y1,x2,y2,x3,y3,x4,y4;

		quad_util::rotatePointFast(x1, y1, -hsize, hsize, ca, sa);
		quad_util::rotatePointFast(x2, y2,  hsize, hsize, ca, sa);
		quad_util::rotatePointFast(x3, y3, -hsize, -hsize, ca, sa);
		quad_util::rotatePointFast(x4, y4,  hsize, -hsize, ca, sa);

		VC3 v;
			
		v.x = p.position.x * mv.m[0][0] + 
			p.position.y * mv.m[1][0] + 
			p.position.z * mv.m[2][0] + mv.m[3][0];
		
		v.y = p.position.x * mv.m[0][1] + 
			p.position.y * mv.m[1][1] + 
			p.position.z * mv.m[2][1] + mv.m[3][1];

		v.z = p.position.x * mv.m[0][2] + 
			p.position.y * mv.m[1][2] + 
			p.position.z * mv.m[2][2] + mv.m[3][2];

		VC3 v1(x1, y1, 0);
		VC3 v2(x2, y2, 0);
		VC3 v3(x3, y3, 0);
		VC3 v4(x4, y4, 0);

		if(faceUp)
		{
			g.RotateVector(v1);
			g.RotateVector(v2);
			g.RotateVector(v3);
			g.RotateVector(v4);
		}

		v1 += v;
		v2 += v;
		v3 += v;
		v4 += v;

		/*
		VC3 v1(v.x + x1, v.y + y1, v.z);
		VC3 v2(v.x + x2, v.y + y2, v.z);
		VC3 v3(v.x + x3, v.y + y3, v.z);
		VC3 v4(v.x + x4, v.y + y4, v.z);

		{
			v1 -= v;
			g.RotateVector(v1);
			v1 += v;

			v2 -= v;
			g.RotateVector(v2);
			v2 += v;

			v3 -= v;
			g.RotateVector(v3);
			v3 += v;

			v4 -= v;
			g.RotateVector(v4);
			v4 += v;
		}
		*/

		*((Vector*)verts) = v1; verts += stride;
		*((Vector*)verts) = v2; verts += stride;
		*((Vector*)verts) = v3; verts += stride;
		*((Vector*)verts) = v4; verts += stride;

		// Fill texturecoords
		if(m_animInfo.numFrames > 1) {
			
			int frame = (int)p.frame % m_animInfo.numFrames;
			
			int col = frame % m_animInfo.textureUSubDivs;
			int row = frame / m_animInfo.textureUSubDivs;
			
			float tx = frameWidth * (float)col;
			float ty = frameHeight * (float)row;				
			
			*((float*)uvs) = tx; uvs += 4; *((float*)uvs) = ty; uvs += (stride - 4);
			*((float*)uvs) = tx + frameWidth; uvs += 4; *((float*)uvs) = ty; uvs += (stride - 4);
			*((float*)uvs) = tx; uvs += 4; *((float*)uvs) = ty + frameHeight; uvs += (stride - 4);
			*((float*)uvs) = tx + frameWidth; uvs += 4; *((float*)uvs) = ty + frameHeight; uvs += (stride - 4);

		} else {

			*((float*)uvs) = 0.0f; uvs += 4; *((float*)uvs) = 0.0f; uvs += (stride - 4);
			*((float*)uvs) = 1.0f; uvs += 4; *((float*)uvs) = 0.0f; uvs += (stride - 4);
			*((float*)uvs) = 0.0f; uvs += 4; *((float*)uvs) = 1.0f; uvs += (stride - 4);
			*((float*)uvs) = 1.0f; uvs += 4; *((float*)uvs) = 1.0f; uvs += (stride - 4);
			
		}

		c0 = (((DWORD)(p.alpha * 255.0f) &0xff) << 24) |
			(((DWORD)(factor.r * p.color.r * 255.0f) &0xff) << 16) |
			(((DWORD)(factor.g * p.color.g * 255.0f) &0xff) << 8) |
			(((DWORD)(factor.b * p.color.b * 255.0f) &0xff) );
	
		*((DWORD*)colors) = c0; colors += stride;
		*((DWORD*)colors) = c0; colors += stride;
		*((DWORD*)colors) = c0; colors += stride;
		*((DWORD*)colors) = c0; colors += stride;

	}

	return m_numParts;
}
Esempio n. 19
0
File: st2010.hpp Progetto: cmsi/bcl
 static void generate_transition_matrix_resize(VEC const& weights, MAT& tm) {
   std::size_t n = weights.size();
   tm.resize(n);
   for (std::size_t i = 0; i < n; ++i) tm[i].resize(n);
   generate_transition_matrix(weights, tm);
 }
	// return true if position ok and decal should be added
	//        false, if position NOT ok and decal should NOT be added
	bool DecalPositionCalculator::calculateDecalPosition(
		game::GameScene *gameScene,
		const VC3 &origin, const VC3 &velocity, 
		DECAL_POSITIONING positioning, int positionRandom,
		VC3 *resultPosition, QUAT *resultRotation)
	{
		assert(positioning != DecalPositionCalculator::DECAL_POSITIONING_INVALID);

		game::GameMap *gameMap = gameScene->getGameMap();

		bool hitWall = false;

		*resultPosition = origin;
		*resultRotation = QUAT((-3.1415926f / 2.0f),0,0);

		// if velocity positioning...
		if (positioning == DecalPositionCalculator::DECAL_POSITIONING_VELOCITY)
		{
			VC3 velocityRandomized;
			if (positionRandom > 0)
			{
				velocityRandomized = velocity * GAME_TICKS_PER_SECOND;

	// TEMP
	//char buf[64];
	//sprintf(buf, "%f,%f,%f", velocity.x, velocity.y, velocity.z);
	//Logger::getInstance()->error(buf);

				// TODO: add positionRandom to velocity _angle_...
				// (or maybe just do a quick hack and add it to xz-coordinates?)
				velocityRandomized.x += float((SystemRandom::getInstance()->nextInt() % (positionRandom * 2 + 1)) - positionRandom) / 100.0f;
				velocityRandomized.z += float((SystemRandom::getInstance()->nextInt() % (positionRandom * 2 + 1)) - positionRandom) / 100.0f;
				
				// add to y too? maybe should add downward only? 
				// NOTE: biased downward
				velocityRandomized.y += float((SystemRandom::getInstance()->nextInt() % (positionRandom * 2 + 1)) - positionRandom) / 100.0f;
				velocityRandomized.y -= float(positionRandom) / 100.0f * 0.5f;

			} else {
				velocityRandomized = velocity;
			}

			velocityRandomized *= 2.0f;

			IStorm3D_Scene *scene = gameScene->getStormScene();

			VC3 dir = velocityRandomized.GetNormalized();			
			VC3 rayOrigin = origin;
			float rayLen = velocityRandomized.GetLength();

			Storm3D_CollisionInfo sceneColl;
			sceneColl.includeTerrainObjects = false;
			scene->RayTrace(rayOrigin, dir, rayLen, sceneColl, true);

			/*
			if (sceneColl.hit)
			{
				VC3 hitNormal = sceneColl.plane_normal;
				// make a "wall" hit if normal-y is not nearly 1
				//if (fabs(hitNormal.y) < 0.8f)
				{
					hitWall = true;

					VC3 x(rand() % 1000 / 999.f, 0, rand() % 1000 / 999.f);
					x.Normalize();
					x -= hitNormal * x.GetDotWith(hitNormal);
					VC3 y = -x.GetCrossWith(hitNormal);

					assert(fabsf(x.GetDotWith(y)) < 0.0001f);
					assert(fabsf(x.GetDotWith(hitNormal)) < 0.0001f);

					MAT tm;
					tm.Set(0, x.x);
					tm.Set(1, x.y);
					tm.Set(2, x.z);
					tm.Set(4, y.x);
					tm.Set(5, y.y);
					tm.Set(6, y.z);
					tm.Set(8, hitNormal.x);
					tm.Set(9, hitNormal.y);
					tm.Set(10, hitNormal.z);

					*resultRotation = tm.GetRotation();
				}
				*resultPosition = sceneColl.position;
			} else {
				*resultPosition += velocityRandomized;
			}
			*/

			// New version
			{
				VC3 hitNormal(0, 1.f, 0);
				if(sceneColl.hit)
				{
					hitNormal = sceneColl.plane_normal;
					*resultPosition = sceneColl.position;
				}
				else
				{
					*resultPosition += velocityRandomized;
					VC2 p2(resultPosition->x, resultPosition->z);
					hitNormal = gameScene->getTerrain()->getFaceNormal(p2);
				}

				{
					if(sceneColl.hit)
						hitWall = true;

		/*
		VC3 y = dir;
		VC3 z = hitNormal;
		y -= hitNormal * y.GetDotWith(hitNormal);
		VC3 x = z.GetCrossWith(y);
		*/

		VC3 x = dir;
		x.y = 0.f;
		x.Normalize();
		x -= hitNormal * x.GetDotWith(hitNormal);
		VC3 y = -x.GetCrossWith(hitNormal);
		VC3 z = hitNormal;

		MAT tm;
		tm.Set(0, x.x);
		tm.Set(1, x.y);
		tm.Set(2, x.z);
		tm.Set(4, y.x);
		tm.Set(5, y.y);
		tm.Set(6, y.z);
		tm.Set(8, z.x);
		tm.Set(9, z.y);
		tm.Set(10, z.z);

		resultRotation->MakeFromAngles(0.f, 0.f, -PI*0.5f);
		*resultRotation = (*resultRotation) * tm.GetRotation();

					/*
					VC3 x(rand() % 1000 / 999.f, 0, rand() % 1000 / 999.f);
					x.Normalize();
					x -= hitNormal * x.GetDotWith(hitNormal);
					VC3 y = -x.GetCrossWith(hitNormal);

					assert(fabsf(x.GetDotWith(y)) < 0.0001f);
					assert(fabsf(x.GetDotWith(hitNormal)) < 0.0001f);

					MAT tm;
					tm.Set(0, x.x);
					tm.Set(1, x.y);
					tm.Set(2, x.z);
					tm.Set(4, y.x);
					tm.Set(5, y.y);
					tm.Set(6, y.z);
					tm.Set(8, hitNormal.x);
					tm.Set(9, hitNormal.y);
					tm.Set(10, hitNormal.z);

					*resultRotation = tm.GetRotation();
					*/
				}
			}


			// TODO: some kind of terrain raytrace maybe...?
			// should collide to walls, etc.
		}

		// if downward positioning...
		if (positioning == DecalPositionCalculator::DECAL_POSITIONING_DOWNWARD)
		{
			if (positionRandom > 0)
			{
				// TODO: add a random xz-offset to result position
				//*resultPosition += randomizedOffset;
				resultPosition->x += float((SystemRandom::getInstance()->nextInt() % (positionRandom * 2 + 1)) - positionRandom) / 100.0f;
				resultPosition->z += float((SystemRandom::getInstance()->nextInt() % (positionRandom * 2 + 1)) - positionRandom) / 100.0f;
			}			

			/*
			// psd
			{
				VC2 p2(resultPosition->x, resultPosition->z);
				VC3 hitNormal = gameScene->getTerrain()->getFaceNormal(p2);

				VC3 x(rand() % 1000 / 999.f, 0, rand() % 1000 / 999.f);
				x.Normalize();
				x -= hitNormal * x.GetDotWith(hitNormal);
				VC3 y = -x.GetCrossWith(hitNormal);

				assert(fabsf(x.GetDotWith(y)) < 0.0001f);
				assert(fabsf(x.GetDotWith(hitNormal)) < 0.0001f);

				MAT tm;
				tm.Set(0, x.x);
				tm.Set(1, x.y);
				tm.Set(2, x.z);
				tm.Set(4, y.x);
				tm.Set(5, y.y);
				tm.Set(6, y.z);
				tm.Set(8, hitNormal.x);
				tm.Set(9, hitNormal.y);
				tm.Set(10, hitNormal.z);

				*resultRotation = tm.GetRotation();
			}
			*/
			VC3 fooNormal;
			calculateDecalRotation(gameScene, *resultPosition, *resultRotation, 0.f, fooNormal);
		}

		// now check that we're still inside map boundaries
		if (!gameMap->isWellInScaledBoundaries(resultPosition->x, resultPosition->z))
		{
			// out of map.
			return false;
		}

		// then fix decal height to ground height
		if (positioning == DecalPositionCalculator::DECAL_POSITIONING_DOWNWARD
			|| positioning == DecalPositionCalculator::DECAL_POSITIONING_VELOCITY)
		{
			if (!hitWall)
			{
				resultPosition->y = gameMap->getScaledHeightAt(resultPosition->x, resultPosition->z);
			}
		}

		// check that not on top of metal grid area...
		if (game::MaterialManager::isMaterialUnderPosition(gameMap, 
			*resultPosition, MATERIAL_METAL_GRATE))
		{
			// on top of grid, no decal here.
			return false;
		}

		// TODO: check that not inside a wall, terrainobject, etc.
		// if so, return false

		return true;
	}
Esempio n. 21
0
//------------------------------------------------------------------
// Storm3D_Material::Apply
// Applies material. Use pass parameter to tell with pass to
// render. Start with pass=0, and increase it until this routine
// returns false. (=all passes rendered)
// 
// Returns:
// false = this was last pass
// true = new pass must be rendered
//------------------------------------------------------------------
bool Storm3D_Material::Apply(Storm3D_Scene *scene,int pass,DWORD fvf,D3DMATRIX *mtx)
{
	/*
	Basic config:

	All textures = NULL

	Stage0:
	colorop = modulate
	colorarg1 = texture
	colorarg2 = diffuse
	alphaop = disable
	texturetransformflags = disable
	texcoordindex = 0

	Stage1-3:
	colorarg1 = texture
	colorarg2 = current
	alphaop = disable
	texturetransformflags = disable
	texcoordindex = 0
	
	After rendering material's UnApply() is called, and it
	returns these states, if it changes them.

	*/

	// BETA: UnApply() all (very slow!)
	Storm3D2->device.SetRenderState(D3DRS_LIGHTING,TRUE);
	Storm3D2->device.SetRenderState(D3DRS_ALPHATESTENABLE,FALSE);
	Storm3D2->device.SetTexture(0,NULL);
	Storm3D2->device.SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
	Storm3D2->device.SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
	Storm3D2->device.SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);
	Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE);
	Storm3D2->device.SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,0);
	Storm3D2->device.SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_DISABLE);

	//	FixMe:
	//	Setting this causes debug runtime to halt (set only to supported stages)
		//for (int i=1;i<3;i++)
	for (int i=0;i<2;i++)
	{
		Storm3D2->device.SetTexture(i,NULL);
		Storm3D2->device.SetTextureStageState(i,D3DTSS_COLORARG1,D3DTA_TEXTURE);
		Storm3D2->device.SetTextureStageState(i,D3DTSS_COLORARG2,D3DTA_CURRENT);
		Storm3D2->device.SetTextureStageState(i,D3DTSS_ALPHAOP,D3DTOP_DISABLE);
		Storm3D2->device.SetTextureStageState(i,D3DTSS_TEXCOORDINDEX,i);
		Storm3D2->device.SetTextureStageState(i,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_DISABLE);
	}

	// Animate textures
	if (texture_base) texture_base->texture->AnimateVideo();
	if (texture_base2) texture_base2->texture->AnimateVideo();
	if (texture_reflection) texture_reflection->texture->AnimateVideo();
	if (texture_bump) texture_bump->texture->AnimateVideo();

	// The superb if(TM) multitexturing (no effects/techniques needed anymore!)	
	// This routine is much faster than DX8-effect system (and bugfree also;)...
	if (multitexture_type==MTYPE_COLORONLY)			// COL only
	{
		// Set stages (color only)
		//Storm3D2->device.SetTexture(0,NULL);

		// psd: assume lightmap if base2 defined
		if(texture_base2)
		{
			texture_base2->texture->Apply(0);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_COLOROP,texture_base2->GetDX8MultitexBlendingOp());
		}
		else
		{
			Storm3D2->device.SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_DIFFUSE);
			Storm3D2->device.SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
		}
	}
	else if (multitexture_type==MTYPE_TEXTURE)		// Base
	{
		// Set stage (base)
		texture_base->texture->Apply(0);

		// Disable last stage
		Storm3D2->device.SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
	}
	else if (multitexture_type==MTYPE_DUALTEX)		// Base+Base2
	{
		// Set stage (base)
		texture_base->texture->Apply(0);
		
		// Set stage (base2)
		texture_base2->texture->Apply(1);
		Storm3D2->device.SetTextureStageState(1,D3DTSS_COLOROP,texture_base2->GetDX8MultitexBlendingOp());

		// Disable last stage
		Storm3D2->device.SetTextureStageState(2,D3DTSS_COLOROP,D3DTOP_DISABLE);
	}
	else if (multitexture_type==MTYPE_REF)			// Reflection
	{		
		// Set stage (reflection)
		texture_reflection->texture->Apply(0);
	
		// Reflection texture (or projective?)
		if (texture_reflection->texcoord_gen==TEX_GEN_REFLECTION)
		{
			Storm3D2->device.SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);

			// Correct number of texturecoordinates
			if (texture_reflection->texture->IsCube())
				Storm3D2->device.SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT3);
			else
				Storm3D2->device.SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT2);
		

			// Rotate the reflection
			VC3 camdir=scene->camera.GetDirection();
			float angle_y=VC2(camdir.x,camdir.z).CalculateAngle();
			float angle_x=VC2(VC2(camdir.x,camdir.z).GetLength(),camdir.y).CalculateAngle();
			MAT mat;
			mat.CreateRotationMatrix(QUAT(-angle_x,-angle_y,0));

			if (!texture_reflection->texture->IsCube())
			{	
				// Fix reflection (v2.3)
				float mt[16]=
				{
					0.5,	0,		0,	0,
					0,		-0.5,	0,	0,
					0,		0,		1,	0,
					0.5,	0.5,	0,	1
				};
				MAT mat2(mt);
				mat.Multiply(mat2);
			
				// Flip texture upside down
				//mat.Multiply(MAT(VC3(1,-1,1)));	
			}

			// Set texture rotation matrix
			D3DMATRIX dxmat;
			mat.GetAsD3DCompatible4x4((float*)&dxmat);
			Storm3D2->device.SetTransform(D3DTS_TEXTURE0,&dxmat);
		}
		else	// Projective texture
		{
			// Create matrix, tu=(0.5+0.87*x)/z, tv=(0.5-0.87*y)/z 
		    D3DXMATRIX mat;
			mat._11=0.866f;mat._12=0.0f;mat._13=0.0f;
			mat._21=0.0f;mat._22=-0.866f;mat._23=0.0f;
			mat._31=0.5f;mat._32=0.5f;mat._33=1.0f;
			mat._41=0.0f;mat._42=0.0f;mat._43=0.0f;
			
			// If it's mirror negate _11
			if (texture_reflection->texcoord_gen==TEX_GEN_PROJECTED_MIRROR)
				mat._11*=-1;

		    Storm3D2->device.SetTransform(D3DTS_TEXTURE0,&mat);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT3|D3DTTFF_PROJECTED);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,D3DTSS_TCI_CAMERASPACEPOSITION);
		}

		// Disable last stage
		Storm3D2->device.SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
	}
	else if (multitexture_type==MTYPE_TEX_REF)		// Base+Reflection
	{
		// Set stage (base)
		texture_base->texture->Apply(0);

		// Set stage (reflection)
		texture_reflection->texture->Apply(1);
		Storm3D2->device.SetTextureStageState(1,D3DTSS_COLOROP,texture_reflection->GetDX8MultitexBlendingOp());
		
		// Reflection texture (or projective?)
		if (texture_reflection->texcoord_gen==TEX_GEN_REFLECTION)
		{
			Storm3D2->device.SetTextureStageState(1,D3DTSS_TEXCOORDINDEX,D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);

			// Correct number of texturecoordinates
			if (texture_reflection->texture->IsCube())
				Storm3D2->device.SetTextureStageState(1,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT3);
			else
				Storm3D2->device.SetTextureStageState(1,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT2);

			// Rotate the reflection
			VC3 camdir=scene->camera.GetDirection();
			float angle_y=VC2(camdir.x,camdir.z).CalculateAngle();
			float angle_x=VC2(VC2(camdir.x,camdir.z).GetLength(),camdir.y).CalculateAngle();
			MAT mat;
			mat.CreateRotationMatrix(QUAT(-angle_x,-angle_y,0));

			if (!texture_reflection->texture->IsCube())
			{	
				// Fix reflection (v2.3)
				float mt[16]=
				{
					0.5,	0,		0,	0,
					0,		-0.5,	0,	0,
					0,		0,		1,	0,
					0.5,	0.5,	0,	1
				};
				MAT mat2(mt);
				mat.Multiply(mat2);
			}

			// Set texture rotation matrix
			D3DMATRIX dxmat;
			mat.GetAsD3DCompatible4x4((float*)&dxmat);
			Storm3D2->device.SetTransform(D3DTS_TEXTURE1,&dxmat);
		}
		else	// Projective mirror texture
		{
			// Create matrix, tu=(0.5+0.87*x)/z, tv=(0.5-0.87*y)/z 
			D3DXMATRIX mat;
			mat._11=0.866f;mat._12=0.0f;mat._13=0.0f;
			mat._21=0.0f;mat._22=-0.866f;mat._23=0.0f;
			mat._31=0.5f;mat._32=0.5f;mat._33=1.0f;
			mat._41=0.0f;mat._42=0.0f;mat._43=0.0f;

			// If it's mirror negate _11
			if (texture_reflection->texcoord_gen==TEX_GEN_PROJECTED_MIRROR)
				mat._11*=-1;

			Storm3D2->device.SetTransform(D3DTS_TEXTURE1,&mat);
			Storm3D2->device.SetTextureStageState(1,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT3|D3DTTFF_PROJECTED);
			Storm3D2->device.SetTextureStageState(1,D3DTSS_TEXCOORDINDEX,D3DTSS_TCI_CAMERASPACEPOSITION);
		}

		// Disable last stage
		Storm3D2->device.SetTextureStageState(2,D3DTSS_COLOROP,D3DTOP_DISABLE);
	}

	// Setup material
	D3DMATERIAL9 mat;

	// Set diffuse
	mat.Diffuse.r=mat.Ambient.r=color.r;
	mat.Diffuse.g=mat.Ambient.g=color.g;
	mat.Diffuse.b=mat.Ambient.b=color.b;
	mat.Diffuse.a=mat.Ambient.a=0;

	// Set self.illum
	mat.Emissive.r=self_illum.r;
	mat.Emissive.g=self_illum.g;
	mat.Emissive.b=self_illum.b;
	mat.Emissive.a=0;

	// Set specular
	mat.Specular.r=specular.r;
	mat.Specular.g=specular.g;
	mat.Specular.b=specular.b;
	mat.Specular.a=0;
	mat.Power=specular_sharpness;

	// Set specular on only if it's used
	if ((specular_sharpness>1)&&
		((specular.r>0.01f)||(specular.g>0.01f)||(specular.b>0.01f)))
	{
		Storm3D2->device.SetRenderState(D3DRS_SPECULARENABLE,TRUE);
	} else Storm3D2->device.SetRenderState(D3DRS_SPECULARENABLE,FALSE);

	// Set 2sided
//	if (doublesided) Storm3D2->device.SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
//		else Storm3D2->device.SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);

	// Set wireframe
	if (wireframe) Storm3D2->device.SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
		else Storm3D2->device.SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
	
	// BETA!!!
	//Storm3D2->device.SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);

	// Set alphablending
	if (alphablend_type==ATYPE_NONE)
	{
		Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
		Storm3D2->device.SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
	}
	else if (alphablend_type==ATYPE_USE_TRANSPARENCY)
	{
		if (transparency>0.001f)
		{
			mat.Diffuse.a=1.0f-transparency;
			Storm3D2->device.SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
			Storm3D2->device.SetRenderState(D3DRS_ALPHATESTENABLE,FALSE);
			Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_DIFFUSE);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
			Storm3D2->device.SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
			Storm3D2->device.SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);				
		}
		else
		{
			Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
			Storm3D2->device.SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
		}
	}
	else if (alphablend_type==ATYPE_USE_TEXTRANSPARENCY || alphablend_type==IStorm3D_Material::ATYPE_USE_ALPHATEST)
	{
		mat.Diffuse.a=1.0f-transparency;

		Storm3D2->device.SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);

		if(alphablend_type==IStorm3D_Material::ATYPE_USE_ALPHATEST)
			Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
		else
			Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,FALSE);

		if ((multitexture_type==MTYPE_DOT3_TEX)||(multitexture_type==MTYPE_DOT3_REF))
		{
			// Stage 1 alphamap (no diffuse)
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG2);
			Storm3D2->device.SetTextureStageState(1,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
			Storm3D2->device.SetTextureStageState(1,D3DTSS_ALPHAARG2,D3DTA_CURRENT);
			Storm3D2->device.SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
		}
		else	
		{
			// Stage 0 alphamap * diffuse
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE);
			Storm3D2->device.SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);
		}
		Storm3D2->device.SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
		Storm3D2->device.SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);

		Storm3D2->device.SetRenderState(D3DRS_ALPHAREF,(DWORD)0x00000001);
		Storm3D2->device.SetRenderState(D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL);
		Storm3D2->device.SetRenderState(D3DRS_ALPHATESTENABLE,TRUE);
	}
	else if (alphablend_type==ATYPE_ADD)
	{
		Storm3D2->device.SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
		Storm3D2->device.SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
		Storm3D2->device.SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
		Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
	}
	else if (alphablend_type==ATYPE_MUL)
	{
		Storm3D2->device.SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
		Storm3D2->device.SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ZERO);
		Storm3D2->device.SetRenderState(D3DRS_DESTBLEND,D3DBLEND_SRCCOLOR);
		Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
	}
	else if (alphablend_type==ATYPE_MUL2X)
	{
		Storm3D2->device.SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
		Storm3D2->device.SetRenderState(D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR);
		Storm3D2->device.SetRenderState(D3DRS_DESTBLEND,D3DBLEND_SRCCOLOR);
		Storm3D2->device.SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
	}

	// Use this material
	Storm3D2->device.SetMaterial(&mat);
/* PSD
	// Apply shader
	if (!ApplyShaderIfAvailable(scene,mtx)) Storm3D2->device.SetVertexShader(fvf);
*/
	// Set active material
	Storm3D2->active_material=this;

	return false;
}
Esempio n. 22
0
int main(int argc, char *argv[]) {
  Teuchos::GlobalMPISession mpiSession(&argc,&argv);

  typedef Epetra_CrsMatrix MAT;
  typedef Epetra_MultiVector MV;

  using Tpetra::global_size_t;
  using Teuchos::tuple;
  using Teuchos::RCP;
  using Teuchos::rcp;

#ifdef HAVE_MPI
  const Epetra_MpiComm comm (MPI_COMM_WORLD);
#else
  const Epetra_SerialComm comm;
#endif
  size_t myRank = comm.MyPID();

  RCP<Teuchos::FancyOStream> fos = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));

  *fos << myRank << " : " << Amesos2::version() << std::endl << std::endl;

  bool printMatrix   = false;
  bool printSolution = false;
  bool printTiming   = false;
  bool verbose       = false;
  std::string solver_name = "SuperLU";
  std::string filedir = "../test/matrices/";
  std::string filename = "arc130.mtx";
  Teuchos::CommandLineProcessor cmdp(false,true);
  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
  cmdp.setOption("filedir",&filedir,"Directory where matrix-market files are located");
  cmdp.setOption("filename",&filename,"Filename for Matrix-Market test matrix.");
  cmdp.setOption("print-matrix","no-print-matrix",&printMatrix,"Print the full matrix after reading it.");
  cmdp.setOption("print-solution","no-print-solution",&printSolution,"Print solution vector after solve.");
  cmdp.setOption("print-timing","no-print-timing",&printTiming,"Print solver timing statistics");
  cmdp.setOption("solver", &solver_name, "Which TPL solver library to use.");
  if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
    return -1;
  }

  // Before we do anything, check that the solver is enabled
  if( !Amesos2::query(solver_name) ){
    std::cerr << solver_name << " not enabled.  Exiting..." << std::endl;
    return EXIT_SUCCESS;	// Otherwise CTest will pick it up as
				// failure, which it isn't really
  }

  const size_t numVectors = 1;

  std::string mat_pathname = filedir + filename;
  
  MAT* A;
  int ret = EpetraExt::MatrixMarketFileToCrsMatrix(mat_pathname.c_str(), comm, A, false, false);
  if( ret == -1 ){
    *fos << "error reading matrix file from disk, aborting..." << std::endl;
    return EXIT_FAILURE;
  }

  if( printMatrix ){
    A->Print(*(fos->getOStream()));
  }

  // get the maps
  const Epetra_Map dmnmap = A->DomainMap();
  const Epetra_Map rngmap = A->RangeMap();

  // Create random X
  RCP<MV> X = rcp( new MV(dmnmap,numVectors) );
  X->Random();

  RCP<MV> B = rcp(new MV(rngmap,numVectors));
  B->Random();

  // Constructor from Factory
  RCP<Amesos2::Solver<MAT,MV> > solver;
  try{
    solver = Amesos2::create<MAT,MV>(solver_name, rcp(A), X, B);
  } catch (std::invalid_argument e){
    *fos << e.what() << std::endl;
    return 0;
  }

  #ifdef HAVE_AMESOS2_SHYLUBASKER
  if( Amesos2::query("shylubasker") ) {
    Teuchos::ParameterList amesos2_params("Amesos2");
      amesos2_params.sublist(solver_name).set("num_threads", 1, "Number of threads");
    solver->setParameters( Teuchos::rcpFromRef(amesos2_params) );
  }
  #endif

  solver->solve();

  if( printSolution ){
    // Print the solution
    X->Print(*(fos->getOStream()));
  }
  
  if( printTiming ){
    // Print some timing statistics
    solver->printTiming(*fos);
  }

  // We are done.
  return 0;
}
Esempio n. 23
0
 inline void updateGSBasis_noapprox() {
     int n = L.NumRows();
     GScomputed = 0;
     updateGSBasis_noapprox(1,n);
 }
Esempio n. 24
0
 void resize(int n,int m) {
     L.SetDims(n,m);
     dim = n;    //number of rows
     isNecessaryGSUpdate=true;        
 }
Esempio n. 25
0
        MatrixRow(const MAT& mat, uint32_t row) : matrix(mat.Cast()), row(row) {

        }