I paste the full code to if anybody can help me out.
the function refalifn and refalifnfast are good to compile on vc++.
But the function refalidf and refalifdf will have this LNK2019 error on vc++
. Same code compiles OK on g++. It must be some kind of restraint with
visual c++.
static double refalifn(const gsl_vector * v, void *params)
{
Dict *dict = (Dict *) params;
double x = gsl_vector_get(v, 0);
double y = gsl_vector_get(v, 1);
double a = gsl_vector_get(v, 2);
EMData *this_img = (*dict)["this"];
EMData *with = (*dict)["with"];
bool mirror = (*dict)["mirror"];
Transform t(Dict("type","2d","alpha",static_cast(a)));
t.set_trans((float)x,(float)y);
t.set_mirror(mirror);
if (v->size>3) {
float sca=(float)gsl_vector_get(v, 3);
if (sca<.7 sca="">1.3) return 1.0e20;
t.set_scale((float)gsl_vector_get(v, 3));
}
EMData *tmp = this_img->process("xform",Dict("transform",&t));
if (dict->has_key("mask")) tmp->mult(*(EMData *)((*dict)["mask"]));
Cmp* c = (Cmp*) ((void*)(*dict)["cmp"]);
double result = c->cmp(tmp,with);
if (tmp != 0) delete tmp;
return result;
}
static void refalidf(const gsl_vector * v, void *params,gsl_vector * df) {
static double lstep[4] = { 0.05, 0.05, 0.1, 0.01 };
gsl_vector *vc = gsl_vector_alloc(v->size);
gsl_vector_memcpy(vc,v);
double f = refalifn(v,params);
for (unsigned int i=0; isize; i++) {
double *vp = gsl_vector_ptr(vc,i);
*vp+=lstep[i];
double f2 = refalifn(vc,params);
*vp-=lstep[i];
gsl_vector_set(df,i,(f2-f)/lstep[i]);
}
gsl_vector_free(vc);
return;
}
static void refalifdf(const gsl_vector * v, void *params, double * f, gsl_
vector * df) {
static double lstep[4] = { 0.05, 0.05, 0.1, 0.01 };
gsl_vector *vc = gsl_vector_alloc(v->size);
gsl_vector_memcpy(vc,v);
*f = refalifn(v,params);
for (unsigned int i=0; isize; i++) {
double *vp = gsl_vector_ptr(vc,i);
*vp+=lstep[i];
double f2 = refalifn(vc,params);
*vp-=lstep[i];
gsl_vector_set(df,i,(f2-*f)/lstep[i]);
}
gsl_vector_free(vc);
return;
}
static double refalifnfast(const gsl_vector * v, void *params)
{
Dict *dict = (Dict *) params;
EMData *this_img = (*dict)["this"];
EMData *img_to = (*dict)["with"];
bool mirror = (*dict)["mirror"];
double x = gsl_vector_get(v, 0);
double y = gsl_vector_get(v, 1);
double a = gsl_vector_get(v, 2);
double r = this_img->dot_rotate_translate(img_to, (float)x, (float)y, (
float)a, mirror);
int nsec = this_img->get_xsize() * this_img->get_ysize();
double result = 1.0 - r / nsec;
return result;
}
EMData *RefineAlignerCG::align(EMData * this_img, EMData *to,
const string & cmp_name, const Dict& cmp_params) const
{
if (!to) {
return 0;
}
EMData *result;
int mode = params.set_default("mode", 0);
float saz = 0.0;
float sdx = 0.0;
float sdy = 0.0;
float sscale = 1.0;
bool mirror = false;
Transform* t;
if (params.has_key("xform.align2d") ) {
t = params["xform.align2d"];
Dict params = t->get_params("2d");
saz = params["alpha"];
sdx = params["tx"];
sdy = params["ty"];
mirror = params["mirror"];
sscale = params["scale"];
} else {
t = new Transform(); // is the identity
}
if ((float)(this_img->get_attr("sigma"))==0.0 || (float)(to->get_attr("
sigma"))==0.0) {
result = this_img->process("xform",Dict("transform",t));
result->set_attr("xform.align2d",t);
delete t;
return result;
}
float step = params.set_default("step",0.1f);
float stepscale = params.set_default("stepscale",0.0f);
int np = 3;
if (stepscale!=0.0) np++;
Dict gsl_params;
gsl_params["this"] = this_img;
gsl_params["with"] = to;
gsl_params["snr"] = params["snr"];
gsl_params["mirror"] = mirror;
if (params.has_key("mask")) gsl_params["mask"]=params["mask"];
const gsl_multimin_fdfminimizer_type *T = gsl_multimin_fdfminimizer_
vector_bfgs;
gsl_vector *x = gsl_vector_alloc(np);
gsl_vector_set(x, 0, sdx);
gsl_vector_set(x, 1, sdy);
gsl_vector_set(x, 2, saz);
if (stepscale!=0.0) gsl_vector_set(x,3,1.0);
Cmp *c = 0;
gsl_multimin_function_fdf minex_func;
if (mode == 2) {
minex_func.f = &refalifnfast;
}
else {
c = Factory < Cmp >::get(cmp_name, cmp_params);
gsl_params["cmp"] = (void *) c;
minex_func.f = &refalifn;
}
minex_func.df = &refalidf;
minex_func.fdf = &refalifdf;
minex_func.n = np;
minex_func.params = (void *) &gsl_params;
gsl_multimin_fdfminimizer *s = gsl_multimin_fdfminimizer_alloc(T, np);
gsl_multimin_fdfminimizer_set(s, &minex_func, x, step, 0.001f);
int rval = GSL_CONTINUE;
int status = GSL_SUCCESS;
int iter = 1;
float precision = params.set_default("precision",0.02f);
int maxiter = params.set_default("maxiter",12);
int verbose = params.set_default("verbose",0);
while (rval == GSL_CONTINUE && iter < maxiter) {
iter++;
status = gsl_multimin_fdfminimizer_iterate(s);
if (status) {
break;
}
rval = gsl_multimin_test_gradient (s->gradient, precision);
}
int maxshift = params.set_default("maxshift",-1);
if (maxshift <= 0) {
maxshift = this_img->get_xsize() / 4;
}
float fmaxshift = static_cast(maxshift);
if ( fmaxshift >= fabs((float)gsl_vector_get(s->x, 0)) && fmaxshift >=
fabs((float)gsl_vector_get(s->x, 1)) && (stepscale==0 || (((float)gsl_vector
_get(s->x, 3))<1.3 && ((float)gsl_vector_get(s->x, 3))<0.7)) )
{
Transform tsoln(Dict("type","2d","alpha",(float)gsl_vector_get(s->x
, 2)));
tsoln.set_mirror(mirror);
tsoln.set_trans((float)gsl_vector_get(s->x, 0),(float)gsl_vector_get
(s->x, 1));
if (stepscale!=0.0) tsoln.set_scale((float)gsl_vector_get(s->x, 3));
result = this_img->process("xform",Dict("transform",&tsoln));
result->set_attr("xform.align2d",&tsoln);
} else {
if (verbose>1) printf(" Refine Failed %1.2f %1.2f %1.1f\n",(float)
gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1),(float)gsl_vector_get
(s->x, 2));
result = this_img->process("xform",Dict("transform",t));
result->set_attr("xform.align2d",t);
}
delete t;
t = 0;
gsl_vector_free(x);
gsl_multimin_fdfminimizer_free(s);
if (c != 0) delete c;
return result;
}