Ejemplo n.º 1
0
void MainWindow::loadInitFile(const string filename){

  this->init_filename = filename;
  if(boost::filesystem::exists(filename)){

	DEBUG_LOG("begin to load vol_obj");
	bool succ = _volObjCtrl->initialize(filename);
        DEBUG_LOG("begin to init _dataModel");
	succ &= _dataModel->loadSetting(filename);
	_dataModel->prepareSimulation();
        
	JsonFilePaser jsonf;
	jsonf.open(filename);
	double collision_k_stiffness, collision_k_limit, con_penalty;
	jsonf.read("con_penalty", con_penalty, 1.0);
	_perturb->setPerturCompilance(con_penalty);
	INFO_LOG("con_penalty: "<<con_penalty );
	_fileDialog->warning(succ);

        
        
        string passive_obj_file;
	if (_passiveObject&&jsonf.readFilePath("passive_object", passive_obj_file, true)){
          _passiveObject->load(passive_obj_file);
          double collision_penalty = 1.0f;
          if (jsonf.read("collision_penalty", collision_penalty)){
            _passiveObject->setCollisionPenalty(collision_penalty);
          }
          if (jsonf.read("collision_k_stiffness", collision_k_stiffness)){
            _passiveObject->setKStiffness(collision_k_stiffness);
          }
          if (jsonf.read("collision_k_limit", collision_k_limit)){
            _passiveObject->setKLimit(collision_k_limit);
          }

	}
  }else{

	ERROR_LOG("file " << filename <<" is not exist!" );
	_fileDialog->warning(false);
  }
}
Ejemplo n.º 2
0
void simulateAndSave(const string ini_file){

  INFO_LOG("init file: "<< ini_file);

  JsonFilePaser jsonf;
  bool succ = jsonf.open(ini_file); assert(succ);
  string save_to;
  int num_frames;
  succ = jsonf.readFilePath("save_to",save_to,false); assert(succ);
  succ = jsonf.read("num_frames",num_frames,200); assert_ge(num_frames,0);
  INFO_LOG("total frames: "<<num_frames);

  pTetMesh tet_mesh = pTetMesh(new TetMesh());
  { // load tet mesh
	string tet_file;
	succ = jsonf.readFilePath("vol_file",tet_file); assert(succ);
	succ = tet_mesh->load(tet_file); assert(succ);
	INFO_LOG("number of nodes: " << tet_mesh->nodes().size());
	INFO_LOG("number of tets: " << tet_mesh->tets().size());
	vector<double> move;
	if (jsonf.read("move_mesh",move)){
	  assert_eq(move.size(),3);
	  VectorXd u(tet_mesh->nodes().size()*3);
	  for (int i = 0; i < u.size()/3; ++i){
		u[i*3+0] = move[0];
		u[i*3+1] = move[1];
		u[i*3+2] = move[2];
	  }
	  tet_mesh->applyDeformation(u);
	}

	string mtl_file;
	succ = jsonf.readFilePath("elastic_mtl",mtl_file); assert(succ);
	succ = tet_mesh->loadElasticMtl(mtl_file); assert(succ);
  }

  pFullStVKSimModel def_model = pFullStVKSimModel(new FullStVKSimModel());
  def_model->setTetMesh(tet_mesh);

  // BoxCollisionHandling collision_sim(def_model);
  PlaneCollisionHandling collision_sim(def_model);
  succ = collision_sim.prepare(); assert(succ);
  succ = collision_sim.init(ini_file); assert(succ);

  // set gravity
  vector<double> gravity;
  succ &= jsonf.read("gravity",gravity);
  assert_eq(gravity.size(),3);
  VectorXd gravity_forces;
  tet_mesh->computeGravity(gravity, gravity_forces);
  collision_sim.setExtForce(gravity_forces);
  
  {// simulation, save results
	vector<VectorXd> record_u;
	
	Timer timer;
	timer.start();
	for (int i = 0; i < num_frames; ++i){
	  INFO_LOG("step: " << i);
	  collision_sim.forward();
	  record_u.push_back(collision_sim.getU());
	}
	timer.stop("total simulation time: ");

	bool succ = tet_mesh->writeVTK(save_to, record_u);
	assert(succ);
  }
}
Ejemplo n.º 3
0
/**
 * Required input (using a json file):
 * 
 * B: basis matrix, generated by model derivative or by extending MA basis.
 * tet_mesh: tetrahedron mesh.
 * training_full_disp: displacements in full space, e.g. u[i].
 * training_full_forces: forces in full sapce, e.g. f[i].
 * rel_err_tol: relative error tolerance, usually set as 0.05.
 * max_points: desired number of samples, usually set as 80.
 * cands_per_iter: usually set as 400.
 * iters_per_full_nnls: usually set as 10.
 * samples_per_subtrain: usually set as 100.
 * 
 * @see 1. An efficient construction of reduced deformable objects, asia 2013.
 *      2. Optimizing Cubature for Efficient Integration of Subspace Deformations. asia 2008.
 * 
 */
int main(int argc, char *argv[]){

  // check
  if (argc < 2){
	ERROR_LOG("ussage: ./cubature json_file_name");
	return -1;
  }
  JsonFilePaser jsonf;
  if(!jsonf.open(argv[1])){
	ERROR_LOG("failed to open json file: " << argv[1]);
	return -1;
  }

  // load basis
  INFO_LOG("loading data....");
  MatrixXd B;
  jsonf.readMatFile("subspace_basis",B);

  // load tetrahedron mesh, and elastic material
  pTetMesh tet_mesh = pTetMesh(new TetMesh());
  string tet_file, elastic_mtl;
  if(jsonf.readFilePath("vol_file", tet_file)){
	if(!tet_mesh->load(tet_file)){
	  ERROR_LOG("failed to load tet mesh file: " << tet_file);
	  return -1;
	}else if(jsonf.readFilePath("elastic_mtl", elastic_mtl)){
	  const bool s = tet_mesh->loadElasticMtl(elastic_mtl);
	  ERROR_LOG_COND("failed to load the elastic material from: "<<elastic_mtl,s);
	}
  }
  assert_eq(B.rows(), tet_mesh->nodes().size()*3);

  // load training data
  MatrixXd training_full_disp, training_full_forces;
  jsonf.readMatFile("training_full_u", training_full_disp);
  jsonf.readMatFile("training_full_f", training_full_forces);

  // load other parameters
  double rel_err_tol;
  int max_points, cands_per_iter, iters_per_full_nnls, samples_per_subtrain;
  jsonf.read("rel_err_tol", rel_err_tol,0.05);
  jsonf.read("max_samples", max_points,80);
  jsonf.read("cands_per_iter", cands_per_iter,400);
  jsonf.read("iters_per_full_nnls", iters_per_full_nnls,10);
  jsonf.read("samples_per_subtrain", samples_per_subtrain,100);

  // run cubature
  INFO_LOG("running cubature....");
  ReducedSimCubature cuba(B, tet_mesh);
  cuba.run(training_full_disp, 
  		   training_full_forces, 
  		   rel_err_tol, 
  		   max_points, 
  		   cands_per_iter, 
  		   iters_per_full_nnls, 
  		   samples_per_subtrain);

  // save cubature
  cout<< "cubature weights:\n " << cuba.getWeights().transpose() << endl << endl;;
  string save_points;
  jsonf.readFilePath("cubature_points", save_points,string("cubpoints.txt"),false);
  INFO_LOG("save samples to: "<< save_points);
  UTILITY::writeVec(save_points,cuba.getSelectedTets(),UTILITY::TEXT);

  string save_weights;
  jsonf.readFilePath("cubature_weights", save_weights,string("cubweights.b"),false);
  INFO_LOG("save weights to: "<< save_weights);
  UTILITY::writeVec(save_weights,cuba.getWeights());

  bool succ = cuba.saveAsVTK(save_points+".vtk");
  ERROR_LOG_COND("failed to save as vtk: "<< save_points+".vtk",succ);
  
  return 0;
}
Ejemplo n.º 4
0
MtlOptModel::MtlOptModel(const string initf){

  // load mtl
  JsonFilePaser jsonf;
  TEST_ASSERT ( jsonf.open(initf) );
  TEST_ASSERT ( jsonf.read("T",T) );
  TEST_ASSERT ( jsonf.read("h",h) );
  TEST_ASSERT ( jsonf.read("alpha_k",alphaK) );
  TEST_ASSERT ( jsonf.read("alpha_m",alphaM) );
  
  // read modes, and lambda  
  VectorXd t_lambda;
  TEST_ASSERT ( jsonf.readVecFile("eigenvalues",t_lambda) );
  double lambda0_scale = 1.0f;
  if ( jsonf.read("lambda0_scale", lambda0_scale) ){
	assert_gt(lambda0_scale,0);
	t_lambda *= lambda0_scale;
  }

  int number_modes = 0;
  TEST_ASSERT ( jsonf.read("rw", number_modes) );
  if (number_modes <= 0)
	number_modes = t_lambda.size();
  testModeId.resize(number_modes);
  for (int i = 0; i < number_modes; ++i)
	testModeId[i] = i;

  lambda.resize(testModeId.size());
  ASSERT_LE(testModeId.size(),t_lambda.size());
  for (int i = 0; i < testModeId.size(); ++i)
  	lambda[i] = t_lambda[testModeId[i]];

  if(!jsonf.read("rs",r_s)){
	r_s = lambda.size();
  }

  // keyframe constraints
  int fix_head = 0, fix_end = 0;
  TEST_ASSERT ( jsonf.read("fix_head", fix_head) );
  TEST_ASSERT ( jsonf.read("fix_end", fix_end) );
  Kid.clear();
  for (int i = 0; i < fix_head; ++i){
    Kid.push_back(i);
  }
  for (int i = T-fix_end; i < T; ++i){
    Kid.push_back(i);
  }
  this->Kz.resize(selectedRedDim(),Kid.size());
  this->Kz.setZero();

  // partial constraints
  partialConPenalty = 10.0f;
  string partial_con_str;
  if (jsonf.readFilePath("partial_con",partial_con_str)){
	
  	TEST_ASSERT ( jsonf.read("con_penalty",partialConPenalty) );
  	PartialConstraintsSet AllConNodes;
  	TEST_ASSERT ( AllConNodes.load(partial_con_str) );
  	const set<pPartialConstraints> &con_groups=AllConNodes.getPartialConSet();
  	BOOST_FOREACH(pPartialConstraints pc, con_groups){
  	  if ( pc != NULL && !pc->isEmpty() ) {
  		vector<int> conNod;
		VectorXd conPos;
		pc->getPartialCon(conNod,conPos);
  		conFrames.push_back(pc->getFrameId());
  		conNodes.push_back(conNod);
  		uc.push_back(conPos);
  	  }
  	}
  }
Ejemplo n.º 5
0
int main(int argc, char *argv[]){

  // check
  if (argc < 2){
	ERROR_LOG("ussage: extend_basis ini_json_file_name");
	return -1;
  }
  JsonFilePaser jsonf;
  if(!jsonf.open(argv[1])){
	ERROR_LOG("failed to open json file: " << argv[1]);
	return -1;
  }

  // load data
  INFO_LOG("loading data....");
  pTetMesh tet_mesh = pTetMesh(new TetMesh());
  string tet_file, elastic_mtl;
  if(jsonf.readFilePath("vol_file", tet_file)){
	if(!tet_mesh->load(tet_file)){
	  ERROR_LOG("failed to load tet mesh file: " << tet_file);
	  return -1;
	}else if(jsonf.readFilePath("elastic_mtl", elastic_mtl)){
	  const bool s = tet_mesh->loadElasticMtl(elastic_mtl);
	  ERROR_LOG_COND("failed to load the elastic material from: "<<elastic_mtl,s);
	}
  }

  set<int> fixed_nodes;
  jsonf.readVecFile("fixed_nodes", fixed_nodes,UTILITY::TEXT);
  INFO_LOG("number of fixed nodes: " << fixed_nodes.size());
  int num_linear_modes = fixed_nodes.size()>0 ? 15:20;
  jsonf.read("num_linear_modes",num_linear_modes,num_linear_modes);
  INFO_LOG("number of linear modes: " << num_linear_modes);

  /// compute full K, M
  INFO_LOG("computing full K, M....");
  ElasticForceTetFullStVK ela(tet_mesh);
  ela.prepare();
  VectorXd x0;
  tet_mesh->nodes(x0);
  SparseMatrix<double> K = ela.K(x0)*(-1.0f);

  MassMatrix mass;
  DiagonalMatrix<double,-1> diagM;
  mass.compute(diagM,*tet_mesh);

  /// remove fixed nodes
  INFO_LOG("remove fixed dofs in K, M....");
  SparseMatrix<double> P;
  EIGEN3EXT::genReshapeMatrix(K.rows(),3,fixed_nodes,P);
  K = P*(K*P.transpose());
  const SparseMatrix<double> M = P*(diagM*P.transpose());

  /// solve general eigen value problem for W, lambda
  INFO_LOG("solving general eigen value problem K*x = la*M*x....");
  MatrixXd W;
  VectorXd lambda;
  const SparseMatrix<double> Klower = getLower(K);
  if(!EigenSparseGenEigenSolver::solve(Klower,M,W,lambda,num_linear_modes)){
  	ERROR_LOG("failed to solve the general eigen value problem.");
  	return -1;
  }
  if (fixed_nodes.size() > 0){
	W = P.transpose()*W;
  }

  // extend basis W to obtain B
  INFO_LOG("extending basis....");
  MatrixXd B;
  if (fixed_nodes.size() > 0){
	ExtendModalBasis::construct(W, B);
  }else{
	ExtendModalBasis::construct(W, x0, B);
  }
  
  // save
  string save_b;
  jsonf.readFilePath("subspace_basis",save_b,string("B.b"),false);
  INFO_LOG("save extended basis to: "<< save_b);
  if(!write(save_b, B)){
	ERROR_LOG("failed to save the extended basis to: " << save_b);
  }

  return 0;
}