bool IfConversionPass2::VerifyIf(IfStatement* toConvert)
{
  Statement* thenPart = toConvert->get_then_part() ;
  Statement* elsePart = toConvert->get_else_part() ;

  if (thenPart == NULL || elsePart == NULL)
  {
    return false ;
  }

  StatementList* thenList = dynamic_cast<StatementList*>(thenPart) ;
  StatementList* elseList = dynamic_cast<StatementList*>(elsePart) ;
  
  if (thenList != NULL && thenList->get_statement_count() != 1)
  {
    return false ;
  }
  
  if (elseList != NULL && elseList->get_statement_count() != 1)
  {
    return false ;
  }

  thenPart = Denormalize(thenPart) ;
  elsePart = Denormalize(elsePart) ;

  return CorrectTypes(thenPart, elsePart) ;
}
// Both the if and then portion are call statements
bool IfConversionPass2::ConvertCallsIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  Statement* elsePart = Denormalize(toConvert->get_else_part()) ;

  CallStatement* thenCall = dynamic_cast<CallStatement*>(thenPart) ;
  CallStatement* elseCall = dynamic_cast<CallStatement*>(elsePart) ;

  assert(thenCall != NULL) ;
  assert(elseCall != NULL) ;

  CallStatement* boolSelectCall = 
    CreateBoolCall(thenCall->get_destination()) ;

  // Create call expression for each of the call statements
  //  and append them to the boolean select we are creating.

  Expression* thenCallValue = thenCall->get_callee_address() ;
  thenCall->set_callee_address(NULL) ;

  CallExpression* thenCallExp = 
    create_call_expression(theEnv,
			   thenCall->get_destination()->get_type()->get_base_type(),
			   thenCallValue) ;

  // Append all of the call arguments to the expression
  for (int i = 0 ; i < thenCall->get_argument_count() ; ++i)
  {
    Expression* nextArg = thenCall->get_argument(i) ;
    nextArg->set_parent(NULL) ;
    thenCallExp->append_argument(nextArg) ;
  }

  boolSelectCall->append_argument(thenCallExp) ;

  Expression* elseCallValue = elseCall->get_callee_address() ;
  elseCall->set_callee_address(NULL) ;
  CallExpression* elseCallExp =
    create_call_expression(theEnv,
			   thenCall->get_destination()->get_type()->get_base_type(),
			   elseCallValue) ;

  // Append all of the call arguments to the expression
  for (int i = 0 ; i < elseCall->get_argument_count() ; ++i)
  {
    Expression* nextArg = elseCall->get_argument(i) ;
    nextArg->set_parent(NULL) ;
    elseCallExp->append_argument(nextArg) ;
  }

  boolSelectCall->append_argument(elseCallExp) ;

  Expression* condition = toConvert->get_condition() ;
  toConvert->set_condition(NULL) ;
  boolSelectCall->append_argument(condition) ;

  toConvert->get_parent()->replace(toConvert, boolSelectCall) ;  
  return true ;
}
bool IfConversionPass2::ConvertArrayStoreIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  Statement* elsePart = Denormalize(toConvert->get_else_part()) ;
  StoreStatement* thenStore = dynamic_cast<StoreStatement*>(thenPart) ;
  StoreStatement* elseStore = dynamic_cast<StoreStatement*>(elsePart) ;
  
  assert(thenStore != NULL) ;
  assert(elseStore != NULL) ;

  ArrayReferenceExpression* thenRef = 
    dynamic_cast<ArrayReferenceExpression*>(thenStore->get_destination_address()) ;
  ArrayReferenceExpression* elseRef = 
    dynamic_cast<ArrayReferenceExpression*>(elseStore->get_destination_address()) ;

  assert(thenRef != NULL) ;
  assert(elseRef != NULL) ;

  // Verify that these array references are to the same location.
  if (!EquivalentExpressions(thenRef, elseRef))
  {
    return false ;
  }

  // This will hopefully be scalar replaced later, but for now we just need
  //  to do something...
  //  What I need is the qualified element type of the array.  
  QualifiedType* qualType = create_qualified_type(theEnv,
						  thenRef->get_result_type()) ;

  StoreStatement* replacement = CreateBoolCall(thenRef) ;

  // Append the arguments to the call expression
  CallExpression* callExpr = 
    dynamic_cast<CallExpression*>(replacement->get_value()) ;
  assert(callExpr != NULL) ;

  Expression* thenStoreValue = thenStore->get_value() ;
  thenStore->set_value(NULL) ;
  callExpr->append_argument(thenStoreValue) ;

  Expression* elseStoreValue = elseStore->get_value() ;
  elseStore->set_value(NULL) ;
  callExpr->append_argument(elseStoreValue) ;

  Expression* condition = toConvert->get_condition() ;
  toConvert->set_condition(NULL) ;
  callExpr->append_argument(condition) ;

  // Replace the if
  toConvert->get_parent()->replace(toConvert, replacement) ;
  return true ;
}
bool IfConversionPass2::ConvertIf(IfStatement* toConvert)
{
  // First, verify that this if statement is in the correct format or not

  if (!VerifyIf(toConvert))
  {
    return false ;
  }

  // This should be in the right format, but I need to decide what to do
  Statement* thenPart = toConvert->get_then_part() ;
  Statement* elsePart = toConvert->get_else_part() ;
  thenPart = Denormalize(thenPart) ;
  elsePart = Denormalize(elsePart) ;
  
  if (dynamic_cast<StoreVariableStatement*>(thenPart) != NULL && 
      dynamic_cast<StoreVariableStatement*>(elsePart) != NULL)
  {
    return ConvertStoreVariableIf(toConvert) ;
  }
  else if (dynamic_cast<StoreStatement*>(thenPart) != NULL)
  {
    return ConvertStoreIf(toConvert) ;
  } 
  else if (dynamic_cast<CallStatement*>(thenPart) != NULL &&
	   dynamic_cast<CallStatement*>(elsePart) != NULL)
  {
    return ConvertCallsIf(toConvert) ;
  }
  else if (dynamic_cast<CallStatement*>(thenPart) != NULL &&
	   dynamic_cast<StoreVariableStatement*>(elsePart) != NULL)
  {
    return ConvertCallStoreVarIf(toConvert) ;
  }
  else if (dynamic_cast<StoreVariableStatement*>(thenPart) != NULL &&
	   dynamic_cast<CallStatement*>(elsePart) != NULL)
  {
    return ConvertStoreVarCall(toConvert) ;
  }
  else
  {
    OutputError("Unknown if format caused a problem!") ;
    FormattedText tmpText ;
    toConvert->print(tmpText) ;
    std::cout << tmpText.get_value() << std::endl ;
    assert(0) ;
  }
  return false ;
}
bool IfConversionPass2::ConvertStructStoreIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  Statement* elsePart = Denormalize(toConvert->get_else_part()) ;
  StoreStatement* thenStore = dynamic_cast<StoreStatement*>(thenPart) ;
  StoreStatement* elseStore = dynamic_cast<StoreStatement*>(elsePart) ;

  assert(thenStore != NULL) ;
  assert(elseStore != NULL) ;

  FieldAccessExpression* thenField =
    dynamic_cast<FieldAccessExpression*>(thenStore->get_destination_address());
  FieldAccessExpression* elseField = 
    dynamic_cast<FieldAccessExpression*>(elseStore->get_destination_address());

  assert(thenField != NULL) ;
  assert(elseField != NULL) ;

  // Check to make sure that each field access is accessing the same field
  if (thenField->get_field() != elseField->get_field())
  {
    return false ;
  }

  CallStatement* replacement = CreateBoolCall(thenField->get_field()) ;

  // Append the arguments
  Expression* thenStoreValue = thenStore->get_value() ;
  thenStore->set_value(NULL) ;
  replacement->append_argument(thenStoreValue) ;

  Expression* elseStoreValue = elseStore->get_value() ;
  elseStore->set_value(NULL) ;
  replacement->append_argument(elseStoreValue) ;

  Expression* condition = toConvert->get_condition() ;
  toConvert->set_condition(NULL) ;
  replacement->append_argument(condition) ;

  toConvert->get_parent()->replace(toConvert, replacement) ;  
  return true ;
}
bool IfConversionPass2::ConvertStoreVariableIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  Statement* elsePart = Denormalize(toConvert->get_else_part()) ;

  StoreVariableStatement* thenStoreVar = 
    dynamic_cast<StoreVariableStatement*>(thenPart) ;
  StoreVariableStatement* elseStoreVar =
    dynamic_cast<StoreVariableStatement*>(elsePart) ;

  assert(thenStoreVar != NULL) ;
  assert(elseStoreVar != NULL) ;

  // Make sure that the variables are the same.
  if (thenStoreVar->get_destination() != elseStoreVar->get_destination())
  {
    return false ;
  }

  CallStatement* boolSelectCall = 
    CreateBoolCall(thenStoreVar->get_destination()) ;

  // Append the arguments
  Expression* thenStoreVarValue = thenStoreVar->get_value() ;
  thenStoreVar->set_value(NULL) ;
  boolSelectCall->append_argument(thenStoreVarValue) ;

  Expression* elseStoreVarValue = elseStoreVar->get_value() ;
  elseStoreVar->set_value(NULL) ;
  boolSelectCall->append_argument(elseStoreVarValue) ;

  Expression* condition = toConvert->get_condition() ;
  toConvert->set_condition(NULL) ;
  boolSelectCall->append_argument(condition) ;

  toConvert->get_parent()->replace(toConvert, boolSelectCall) ;  
  return true ;
}
bool IfConversionPass2::ConvertStoreIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  StoreStatement* thenStore = dynamic_cast<StoreStatement*>(thenPart) ;
  assert(thenStore != NULL) ;

  Expression* destination = thenStore->get_destination_address() ;
  
  if (dynamic_cast<FieldAccessExpression*>(destination) != NULL)
  {
    return ConvertStructStoreIf(toConvert) ;    
  }
  else if (dynamic_cast<ArrayReferenceExpression*>(destination) != NULL)
  {
    return ConvertArrayStoreIf(toConvert) ;
  }
  else
  {
    OutputError("Unsupported if detected!") ;
    assert(0) ;
    return false ; // To avoid a warning
  }  
}
Example #8
0
int ThinPlateSplineTransform(const char* f_config) {
  vnl_matrix<double> model, scene, ctrl_pts, /*source,*/ transformed_source;
  char f_model[256] = {0}, f_scene[256] = {0};
  char common_section[80] = "FILES";

  GetPrivateProfileString(common_section, "model", NULL,
      f_model, 256, f_config);
  if (LoadMatrixFromTxt(f_model, model) < 0) {
    return -1;
  }
  int d = model.cols();

  GetPrivateProfileString(common_section, "scene", NULL,
      f_scene, 256, f_config);
  if (LoadMatrixFromTxt(f_scene, scene) < 0) {
    return -1;
  }
  assert(scene.cols() == d);

  char f_ctrl_pts[256] = {0};
  GetPrivateProfileString(common_section, "ctrl_pts", NULL,
      f_ctrl_pts, 256, f_config);
  if (LoadMatrixFromTxt(f_ctrl_pts, ctrl_pts) < 0) {
    return -1;
  }
  assert(ctrl_pts.cols() == d);
  int n = ctrl_pts.rows();

  //char f_source[256] = {0};
  //GetPrivateProfileString(common_section, "source", NULL,
  //    f_source, 256, f_config);
  //if (LoadMatrixFromTxt(f_source, source) < 0) {
  //  return -1;
  //}
  //assert(source.cols() == d);
  //int m = source.rows();

  vnl_matrix<double> affine, tps;
  char f_init_affine[256] = {0}, f_init_tps[256] = {0};
  DWORD readCount = GetPrivateProfileString(common_section, "init_affine", NULL,
      f_init_affine, 256, f_config);
  if (readCount != 0)
  {
	  if (LoadMatrixFromTxt(f_init_affine, affine) < 0) {
		  return -1;
	  }
  }
  assert(affine.cols() == d);
  assert(affine.rows() == d+1);

  GetPrivateProfileString(common_section, "init_tps", NULL,
      f_init_tps, 256, f_config);
  if (LoadMatrixFromTxt(f_init_tps, tps) < 0) {
    return -1;
  }
  assert(tps.cols() == d);
  assert(tps.rows() == (n - d - 1));

  vnl_matrix<double> param_all;
  param_all.set_size(n, d);
  param_all.update(affine);
  param_all.update(tps, d + 1);

  // TODO: check if dimensions are consistent.
  bool b_normalize = GetPrivateProfileInt("GMMREG_OPT", "normalize", 1, f_config);

  double model_scale, scene_scale, ctrl_scale;
  vnl_vector<double> ctrl_centroid, model_centroid, scene_centroid;

  if (b_normalize) {
    Normalize(ctrl_pts, ctrl_centroid, ctrl_scale);
    Normalize(model, model_centroid, model_scale);
    Normalize(scene, scene_centroid, scene_scale);
    // perform normalization to source w.r.t to model space
    //Denormalize(source, -model_centroid/model_scale, 1.0/model_scale);
  }

  vnl_matrix<double> K, U;
  ComputeTPSKernel(source, ctrl_pts, U, K);
  vnl_matrix<double> Pm;
  Pm.set_size(m, d + 1);
  Pm.set_column(0, 1);
  Pm.update(source, 0, 1);

  vnl_matrix<double> Pn;
  Pn.set_size(n, d + 1);
  Pn.set_column(0, 1);
  Pn.update(ctrl_pts, 0, 1);

  vnl_qr<double> qr(Pn);
  vnl_matrix<double> V = qr.Q();
  vnl_matrix<double> PP = V.extract(n, n - d - 1, 0, d + 1);
  vnl_matrix<double> basis;
  basis.set_size(m, n);
  basis.update(Pm);
  basis.update(U * PP, 0, d + 1);

  transformed_source = basis * param_all;
  if (b_normalize) {
    Denormalize(transformed_source, scene_centroid, scene_scale);
  }
  char f_transformed_source[256] = {0};
  GetPrivateProfileString(common_section, "transformed_source", NULL,
      f_transformed_source, 256, f_config);
  std::ofstream outfile(f_transformed_source, std::ios_base::out);

  transformed_source.print(outfile);
  return 0;
}