// [[Rcpp::export]]
arma::mat BeQTL2(const arma::mat & A, const arma::mat & B, const arma::umat & Bootmat){
  int bsi= Bootmat.n_rows;
  arma::mat C(A.n_cols*B.n_cols,Bootmat.n_rows);
  arma::mat tC(A.n_rows,B.n_rows);
  for(int i=0; i<bsi; i++){
    tC = cor(A.rows(Bootmat.row(i)),B.rows(Bootmat.row(i)));
    C.col(i) = vectorise(tC,0);
  }
  C.elem(find_nonfinite(C)).zeros();

  return reshape(median(C,1),A.n_cols,B.n_cols);
}
//Script that takes two matrices, performs bootstrapped correlation, and returns the median
// [[Rcpp::export]]
arma::mat BeQTL(const arma::mat & A, const arma::mat & B, const arma::umat & Bootmat){
  int bsi= Bootmat.n_rows;
  Rcpp::Rcout<<"Starting Bootstrap!"<<std::endl;
  arma::mat C(A.n_cols*B.n_cols,Bootmat.n_rows);
  arma::mat tA(A.n_rows,A.n_cols);
  arma::mat tB(B.n_rows,B.n_cols);
  arma::mat tC(A.n_rows,B.n_rows);
  for(int i=0; i<bsi; i++){
    tA = A.rows(Bootmat.row(i));
    tB = B.rows(Bootmat.row(i));
    tC = cor(tA,tB);
    C.col(i) = vectorise(tC,0);
  }
  C.elem(find_nonfinite(C)).zeros();

 return reshape(median(C,1),A.n_cols,B.n_cols);
}
예제 #3
0
파일: main.cpp 프로젝트: henrical/PO
int main()
{
	EconomicEngine eE1;
	EconomicEngine eE2;
	EconomicEngine eE3;
	XtremeEngine xE1;

	EconomicCar eC(eE1);
	MediumCar mC(eE2);
	TopCar tC(eE3, xE1);

	eC.drive();
	eC.stop();

	mC.changeEngine(eE3);
        
	tC.drive();
	tC.stop();
}
예제 #4
0
void Scene::AddTexturedObject(const std::string fname, Material* material, Shapes &objects, const std::string textName, const Point &ofs) const
{
	size_t index = Shape::GetUniqueID();

	// —читывание меша из файла
	L3DS *l3ds = new L3DS(fname.c_str());
	if(!l3ds || !l3ds->GetMeshCount())
		throw Error("Error in loading extern files");

	for(int i = 0; i<l3ds->GetMeshCount(); i++) {
		LMesh *mesh = l3ds->GetMesh(i);

		Texture *texture = new Texture(textName.c_str());

		for(int j = 0; j<mesh->GetTriangleCount(); j++) {
			LTriangle tr = mesh->GetTriangle(j);

			Point a(mesh->GetVertex(tr.a).x, mesh->GetVertex(tr.a).y, mesh->GetVertex(tr.a).z);
			Point b(mesh->GetVertex(tr.b).x, mesh->GetVertex(tr.b).y, mesh->GetVertex(tr.b).z);
			Point c(mesh->GetVertex(tr.c).x, mesh->GetVertex(tr.c).y, mesh->GetVertex(tr.c).z);	

			TextureCoords tA(mesh->GetUV(tr.a).u, mesh->GetUV(tr.a).v);
			TextureCoords tB(mesh->GetUV(tr.b).u, mesh->GetUV(tr.b).v);
			TextureCoords tC(mesh->GetUV(tr.c).u, mesh->GetUV(tr.c).v);

			tA.InvertU();
			tB.InvertU();
			tC.InvertU();

			tA*=8;
			tB*=8;
			tC*=8;

			if (Triangle::IsValidTrangle(a,b,c))
				objects.push_back(new Triangle(a-ofs, b-ofs, c-ofs, material, index, texture, tA, tB, tC));
		}
	}
}
// identifierar alla mesharna i scenen och extraherar data fr�n dem
bool Exporter::IdentifyAndExtractMeshes()
{
	UINT index = 0;
	scene_.meshes.clear();

	//itererar �ver DG:n och lagrar rgba-v�rden och texturnamn i ett tempor�rt material
	material tempmaterial;
	MItDependencyNodes matIt(MFn::kLambert);
	MString aC("ambientColor"), dC("color"), sC("specularColor"), gC("incandescence"), tC("transparency");
	while (!matIt.isDone()){
		if (matIt.item().hasFn(MFn::kPhong))
		{
			MFnPhongShader tempphong(matIt.item());
			tempmaterial.type = PHONG;
			extractColor(tempmaterial.ambient, tempphong, aC);
			extractColor(tempmaterial.diffuse, tempphong, dC);
			extractColor(tempmaterial.specular, tempphong, sC);
			extractColor(tempmaterial.glow, tempphong, gC);
			extractColor(tempmaterial.transparency, tempphong, tC);
		}
		else if (matIt.thisNode().hasFn(MFn::kBlinn))
		{
			MFnBlinnShader tempblinn(matIt.item());
			tempmaterial.type = BLINN;
			extractColor(tempmaterial.ambient, tempblinn, aC);
			extractColor(tempmaterial.diffuse, tempblinn, dC);
			extractColor(tempmaterial.specular, tempblinn, sC);
			extractColor(tempmaterial.glow, tempblinn, gC);
			extractColor(tempmaterial.transparency, tempblinn, tC);
		}
		else if (matIt.item().hasFn(MFn::kLambert))
		{
			MFnLambertShader templamb(matIt.item());
			tempmaterial.type = LAMBERT;
			extractColor(tempmaterial.ambient, templamb, aC);
			extractColor(tempmaterial.diffuse, templamb, dC);
			extractColor(tempmaterial.specular, templamb, sC);
			extractColor(tempmaterial.glow, templamb, gC);
			extractColor(tempmaterial.transparency, templamb, tC);
		}
		else
			printf("No material found\n");
		scene_.materials.push_back(tempmaterial);
		matIt.next();
	}

	//Turn off or on Blendshapes
	matIt.reset(MFn::kBlendShape);
	while (!matIt.isDone())
	{
		MFnBlendShapeDeformer bs(matIt.item());

		//Get the envelope attribute plug
		MPlug pl = bs.findPlug("en");

		//Set the 0 to disable FFD effect, enable by setting it to 1:
		pl.setValue(1.0f);

		matIt.next();
	}

	//Get Actual Blendshapes
	matIt.reset(MFn::kBlendShape);
	while (!matIt.isDone())
	{
		MFnBlendShapeDeformer bs(matIt.item());

		MObjectArray base_objects;

		//print blend shape name
		cout << "Blendshape " << bs.name().asChar() << endl;

		//Get a list of objects that this blend shape deforms
		bs.getBaseObjects(base_objects);

		cout << "NumBaseOBjects " << base_objects.length() << endl;

		//loop through each blendshaped object
		for (int i = 0; i < base_objects.length(); ++i)
		{
			//Get the base shape
			MObject Base = base_objects[i];

			//Output all of the target shapes and weights
			OutputBlendShapes(bs, Base);
		}
		//Get next blend shapes
		matIt.next();
	}

	MDagPath dag_path;
	MItDag dag_iter(MItDag::kBreadthFirst, MFn::kMesh);

	while (!dag_iter.isDone())
	{
		if (dag_iter.getPath(dag_path))
		{
			MFnDagNode dag_node = dag_path.node();

			// vill endast ha "icke-history"-f�rem�l
			if (!dag_node.isIntermediateObject())
			{
				// triangulera meshen innan man h�mtar punkterna
				MFnMesh mesh(dag_path);
				ExtractMeshData(mesh, index);
				index++;
			}
		}

		dag_iter.next();
	}

	MItDependencyNodes it(MFn::kSkinClusterFilter);
	for (; !it.isDone(); it.next()) {


		MObject object = it.item();


		OutputSkinCluster(object);

	}

	//Hitta kamera data
	dag_iter.reset(dag_iter.root(), MItDag::kBreadthFirst, MFn::kCamera);
	while (!dag_iter.isDone())
	{

		extractCamera(dag_iter.item());
		dag_iter.next();
	}

	//itererar dag och s�ker data f�r tillg�ngliga ljus
	//om ej ljus finns i scenen ignoreras denna iteration f�r sagda scen.
	dag_iter.reset(dag_iter.root(), MItDag::kBreadthFirst, MFn::kLight);
	while (!dag_iter.isDone())
	{
		//funktion till v�r iterator
		MFnLight func(dag_iter.item());

		//namn:
		export_stream_ << "Light: " << func.name().asChar() << std::endl;

		//kalla p�EextractLight function
		extractLight(dag_iter.item());

		//vidare till n�sta ljus i dag'en
		dag_iter.next();


		/*
		if (dag_iter.getPath(dag_path))
		{
		auto test = dag_path.fullPathName();
		export_stream_ << "light: " << test << std::endl;
		}
		dag_iter.next();
		*/
	}

	dag_iter.reset(dag_iter.root(), MItDag::kBreadthFirst, MFn::kJoint);
	while (!dag_iter.isDone())
	{
		if (dag_iter.getPath(dag_path))
		{
			MFnDagNode dag_node = dag_path.node();

			if (!dag_node.isIntermediateObject())
			{
				extractJointData(dag_path);
			}
		}
		dag_iter.next();
	}
	int breadth=0;
	dag_iter.reset(dag_iter.root(), MItDag::kBreadthFirst, MFn::kTransform);
	while (!dag_iter.isDone())
	{
		int depth = dag_iter.depth();
		if (depth > 1)
			break;
		if (dag_iter.getPath(dag_path))
		{
			createSceneGraph(MFnDagNode(dag_path),-1);
		}
		breadth++;
		dag_iter.next();
	}
	/*
	//general purpose iterator, sista argument �r filtret
		dag_iter.reset(dag_iter.root(), MItDag::kBreadthFirst, MFn::kLight);
		while (!dag_iter.isDone())
		{
		if (dag_iter.getPath(dag_path))
		{

		}
		dag_iter.next();
		}
		*/


	return true;
}
void SimultaneousImpulseBasedConstraintSolverStrategy::computeConstraintsANDJacobian(std::vector<std::unique_ptr<IConstraint> >& c, const Mat<float>& q, const Mat<float>& qdot)
{
	//MAJ qdot :
	int b1 = 0;
	int b2 = 0;
	
	/*
	for( auto& o : sim->simulatedObjects ) 
	{	
		//((RigidBody*)(o.get()))->setPosition( extract(q, b1+1,1, b1+3,1) );	
		//((RigidBody*)(o.get()))->setMatOrientation( extract(q, b1+4,1, b1+7,1) );
		
		((RigidBody*)(o.get()))->setLinearVelocity( extract( qdot, b2+1,1, b2+3,1) );
		((RigidBody*)(o.get()))->setAngularVelocity( extract( qdot, b2+4,1, b2+6,1) );
		
		b1+=7;
		b2+=6;	
	}
	*/
	//-------------------------------------
	//-------------------------------------
	//-------------------------------------
	
	size_t size = c.size();
	int n = sim->simulatedObjects.size();

	c[0]->computeJacobians();
	
	int idA = 6 * ( c[0]->rbA.getID() );
	int idB = 6 * ( c[0]->rbB.getID() );
	
	Mat<float> tJA(c[0]->getJacobianA());
	Mat<float> tJB(c[0]->getJacobianB());
	//Constraint :
	Mat<float> tC(c[0]->getConstraint());
	
	int sl = tJA.getLine();
	
	Mat<float> temp((float)0,sl, 6*n );
	
	for(int i=1;i<=sl;i++)
	{
		for(int j=1;j<=6;j++)
		{
			temp.set( tJA.get(i,j) , i, idA+j);
			temp.set( tJB.get(i,j), i, idB+j  );
		}
	}
	
	constraintsJacobian = temp;
	//Constraint :
	float baumgarteBAS = 1e-1f;
	float baumgarteC = 1e-1f;
	C = tC;
	//----------------------------------------
	//BAUMGARTE STABILIZATION
	//----------------------------------------
	//Contact offset :
	if( c[0]->getType() == CTContactConstraint)
	{
		//Baumgarte stabilization :
		//temp *= 0.1f/this->dt;
		//float slop = -5e-1f;
		//float pdepth = ((ContactConstraint*)(c[0].get()))->penetrationDepth;
		//tC *= baumgarteC/this->dt*(pdepth-slop);
		tC *= baumgarteC/this->dt;
	}
	//BAS JOINT :
	if( c[0]->getType() == CTBallAndSocketJoint)
	{
		tC*= baumgarteBAS/this->dt;
	}
	//----------------------------------------
	//----------------------------------------
	offset = tC;
	
	
#ifdef debuglvl1
std::cout << "CONSTRAINTS : nbr = " << size << std::endl;
std::cout << "CONSTRAINTS : 0 : type = " << c[0]->getType() << " ; ids are : " << c[0]->rbA.getID() << " : " << c[0]->rbB.getID() << std::endl;
offset.afficher();
/*std::cout << "local Anchor A : " << std::endl;
c[0]->AnchorAL.afficher();
std::cout << "global Anchor A : " << std::endl;
c[0]->rbA.getPointInWorld(c[0]->AnchorAL).afficher();
std::cout << "local Anchor B : " << std::endl;
c[0]->AnchorBL.afficher();
std::cout << "global Anchor B : " << std::endl;
c[0]->rbB.getPointInWorld(c[0]->AnchorBL).afficher();*/
#endif	
	
	for(int i=1;i<size;i++)
	{

		c[i]->computeJacobians();
		
		tJA = c[i]->getJacobianA();
		tJB = c[i]->getJacobianB();
		
		//Constraint :
		tC = c[i]->getConstraint();
		
		size_t sl = tJA.getLine();
		int idA = 6 * ( c[i]->rbA.getID() );
		int idB = 6 * ( c[i]->rbB.getID() );
		
		temp = Mat<float>((float)0,sl, 6*n );
		
		//Constraints :
		C = operatorC(C, tC);
		//----------------------------------------
		//BAUMGARTE STABILIZATION
		//----------------------------------------
		//Contact offset :
		if( c[i]->getType() == CTContactConstraint)
		{
			//Baumgarte stabilization :
			//temp *= 0.1f/this->dt;
			tC *= baumgarteC/this->dt;
		}
		//BAS JOINT :
		if( c[i]->getType() == CTBallAndSocketJoint)
		{
			tC*= baumgarteBAS/this->dt;
		}
		//----------------------------------------
		//----------------------------------------
		
						
		for(int i=1;i<=sl;i++)
		{
			for(int j=1;j<=6;j++)
			{
				temp.set( tJA.get(i,j) , i, idA+j);
				temp.set( tJB.get(i,j), i, idB+j  );
			}
		}
		
		constraintsJacobian = operatorC(constraintsJacobian, temp );
		//Constraint :
		offset = operatorC( offset, tC);
		
#ifdef debuglvl1
std::cout << "CONSTRAINTS : " << i << " type = " << c[i]->getType() << " ; ids are : " << c[i]->rbA.getID() << " : " << c[i]->rbB.getID() << std::endl;
offset.afficher();
#endif
		
	}
	
	
	//Let us delete the contact constraints that are ephemerous by essence :
	std::vector<std::unique_ptr<IConstraint> >::iterator itC = c.begin();
	bool erased = false;
	while(itC != c.end())
	{
		if( (itC->get())->getType() == CTContactConstraint )
		{
			erased = true;
			c.erase(itC);
		}
	
		if(!erased)
			itC++;
			
		erased = false;
	}
}
void IterativeImpulseBasedConstraintSolverStrategy::computeConstraintsANDJacobian(std::vector<std::unique_ptr<IConstraint> >& c, const Mat<float>& q, const Mat<float>& qdot, const SparseMat<float>& invM)
{
	//-------------------------------------
	//-------------------------------------
	//-------------------------------------
	
	size_t size = c.size();
	int n = sim->simulatedObjects.size();
	float baumgarteBAS = 0.0f;//1e-1f;
	float baumgarteC = -2e0f;
	float baumgarteH = 0.0f;//1e-1f;
	
	//---------------
	//	RESETTING :
	constraintsC.clear();
	constraintsJacobians.clear();
	constraintsOffsets.clear();
	constraintsIndexes.clear();
	constraintsInvM.clear();
	constraintsV.clear();
	//----------------------
	
	if( size > 0)
	{
		
		for(int k=0;k<size;k++)
		{
			int idA = ( c[k]->rbA.getID() );
			int idB = ( c[k]->rbB.getID() );
			std::vector<int> indexes(2);
			//indexes are set during the creation of the simulation and they begin at 0.
			indexes[0] = idA;
			indexes[1] = idB;
			
			constraintsIndexes.push_back( indexes );
			
			//---------------------------
			//Constraint :
			c[k]->computeJacobians();
			
			
			Mat<float> tJA(c[k]->getJacobianA());
			Mat<float> tJB(c[k]->getJacobianB());
		
			
			Mat<float> tC(c[k]->getConstraint());
			constraintsC.push_back( tC );
	
			int nbrlineJ = tJA.getLine();
			Mat<float> JacobianAB( operatorL(tJA, tJB)  );
			constraintsJacobians.push_back( JacobianAB );
			
			//----------------------------------------
			//BAUMGARTE STABILIZATION
			//----------------------------------------
			//Contact offset :
			if( c[k]->getType() == CTContactConstraint)
			{
				//----------------------------------------
				//SLOP METHOD :
				/*
				float slop = 1e0f;
				float pdepth = ((ContactConstraint*)(c[k].get()))->penetrationDepth;
				std::cout << " ITERATIVE SOLVER :: CONTACT : pDepth = " << pdepth << std::endl;
				tC *= baumgarteC/this->dt * fabs_(fabs_(pdepth)-slop);			
				*/
				//----------------------------------------
				
				//----------------------------------------
				//DEFAULT METHOD :
				tC *= baumgarteC/this->dt;
				//----------------------------------------
				
				//----------------------------------------
				//METHOD 2 :
				/*
				float restitFactor = ( (ContactConstraint*) (c[k].get()) )->getRestitutionFactor();
				std::cout << " ITERATIVE SOLVER :: CONTACT : restitFactor = " << restitFactor << std::endl;
				Mat<float> Vrel( ( (ContactConstraint*) (c[k].get()) )->getRelativeVelocity() );
				Mat<float> normal( ( (ContactConstraint*) (c[k].get()) )->getNormalVector() );
				std::cout << " ITERATIVE SOLVER :: CONTACT : Normal vector : " << std::endl;
				transpose(normal).afficher();
				tC +=  restitFactor * transpose(Vrel)*normal; 
				*/
				//----------------------------------------
				
				std::cout << " ITERATIVE SOLVER :: CONTACT : Contact Constraint : " << std::endl;
				transpose(tC).afficher();
				std::cout << " ITERATIVE SOLVER :: CONTACT : Relative Velocity vector : " << std::endl;
				transpose(( (ContactConstraint*) (c[k].get()) )->getRelativeVelocity()).afficher();
				//std::cout << " ITERATIVE SOLVER :: CONTACT : First derivative of Contact Constraint : " << std::endl;
				//(transpose(tJA)*).afficher();
					
			}
			//BAS JOINT :
			if( c[k]->getType() == CTBallAndSocketJoint)
			{
				tC *= baumgarteBAS/this->dt;
			}
			
			//HINGE JOINT :
			if( c[k]->getType() == CTHingeJoint)
			{
				tC *= baumgarteH/this->dt;
			}
			
			//BAUMGARTE OFFSET for the moments...
			constraintsOffsets.push_back( tC );
			
			//----------------------------------------
			//----------------------------------------
			
			
			//-------------------
			//	invM matrixes :
			Mat<float> invmij(0.0f,12,12);
			for(int k=0;k<=1;k++)
			{
				for(int i=1;i<=6;i++)
				{
					for(int j=1;j<=6;j++)
					{
						invmij.set( invM.get( indexes[k]*6+i, indexes[k]*6+j), k*6+i,k*6+j);
						
					}
				}
			}
			
			constraintsInvM.push_back( invmij);
			
			
			//-------------------
			//	Vdot matrixes :
			Mat<float> vij(0.0f,12,1);
			for(int k=0;k<=1;k++)
			{
				for(int i=1;i<=6;i++)
				{
					vij.set( qdot.get( indexes[k]*6+i, 1), k*6+i,1);
				}
			}
			
			constraintsV.push_back( vij);
			
			
			
		}
		
	}	
}