Exemplo n.º 1
0
/**
 * \brief main function for the convolve program
 */
int	main(int argc, char *argv[]) {
	// parse command line
	int	c;
	int	longindex;
	while (EOF != (c = getopt_long(argc, argv, "dh?",
		longopts, &longindex)))
		switch (c) {
		case 'd':
			debuglevel = LOG_DEBUG;
			break;
		case 'h':
		case '?':
			usage(argv[0]);
			return EXIT_SUCCESS;
		default:
			throw std::runtime_error("unknown option");
		}

	// next two arguments must be filenames
	if ((argc - optind) != 3) {
		std::cerr << "need exactly three file name arguments"
			<< std::endl;
		return EXIT_FAILURE;
	}
	const char	*in1filename = argv[optind++];
	const char	*in2filename = argv[optind++];
	const char	*outfilename = argv[optind++];

	// read the image from the file
	FITSin	in1file(in1filename);
	ImagePtr	image1 = in1file.read();
	Image<double>	*img1 = dynamic_cast<Image<double> *>(&*image1);
	if (NULL == img1) {
		debug(LOG_ERR, DEBUG_LOG, 0, "can only convolve double images");
	}
	ConvolutionResult	factor1(*img1, ImagePoint(0, 0));

	FITSin	in2file(in2filename);
	ImagePtr	image2 = in2file.read();
	Image<double>	*img2 = dynamic_cast<Image<double> *>(&*image2);
	if (NULL == img2) {
		debug(LOG_ERR, DEBUG_LOG, 0, "can only convolve double images");
	}
	ConvolutionResult	factor2(*img2, ImagePoint(0, 0));

	// compute the convolution
	ConvolutionResultPtr	result = factor1 * factor2;

	// write the result image
	FITSout	outfile(outfilename);
	outfile.setPrecious(false);
	outfile.write(result->image());

	// that's it
	return EXIT_SUCCESS;
}
pointField quadrilateralDistribution::evaluate()
{
    // Read needed material
    label N0( readLabel(pointDict_.lookup("N0")) );
    label N1( readLabel(pointDict_.lookup("N1")) );

    point xs0( pointDict_.lookup("linestart0") );
    point xe0( pointDict_.lookup("lineend0") );
    point xe1( pointDict_.lookup("lineend1") );

    scalar stretch0( pointDict_.lookupOrDefault<scalar>("stretch0", 1.0) );
    scalar stretch1( pointDict_.lookupOrDefault<scalar>("stretch1", 1.0) );

    // Define the return field
    pointField res(N0*N1, xs0);

    // Compute the scaling factor
    scalar factor0(0.0);
    scalar factor1(0.0);

    for (int i=1; i < N0; i++)
    {
        factor0 += Foam::pow( stretch0, static_cast<scalar>(i) );
    }

    for (int i=1; i < N1; i++)
    {
        factor1 += Foam::pow( stretch1, static_cast<scalar>(i) );
    }

    point dx0( (xe0 - xs0)/factor0 );
    point dx1( (xe1 - xs0)/factor1 );

    // Compute points
    for (int j=0; j < N1; j++)
    {
        if (j != 0)
        {
            res[j*N0] = res[(j - 1)*N0]
                + Foam::pow( stretch1, static_cast<scalar>(j))*dx1;
        }

        for (int i=1; i < N0; i++)
        {
            res[i + j*N0] = res[i - 1 + j*N0]
                + Foam::pow( stretch0, static_cast<scalar>(i) )*dx0;
        }
    }

    return res;
}
Exemplo n.º 3
0
/*
 calculate the scaliung factor to pull spectra to measurement
 */
double spectrum_generator_cpu::get_factor_to_scale_spectra_to_measurement(){
    //weighted squared errors
    std::vector<double> factor1(this->mes_nspecsteps);
    std::vector<double> factor2(this->mes_nspecsteps);
    
    //loop over wavelenghts
    for( int wave=0 ; wave<this->mes_nspecsteps;wave++){
        //counting the factor with err
        if ( this->mes_spec_mask[wave] == 0 && this->mes_spec_err[wave]!=0  ){
            factor1[wave]= (this->mes_spec[wave] * this->result[wave])  / (this->mes_spec_err[wave] * this->mes_spec_err[wave] );
            factor2[wave]= (this->result[wave] * this->result[wave] ) / (this->mes_spec_err[wave] * this->mes_spec_err[wave]);
        }
        else { //problems in mes
            factor1[wave]=0;
            factor2[wave]=0;
        }
    }
    
    //temp1, temp2 are sums of vectors factor1,factor2
    //they are use to calculate the "factor" that pulls together observed
    //and model spectra
    double temp_1,temp_2,scale_factor;
    
    //get factors
    
    //summing factors, to pull spectra together
    //summing is sequential on CPU paralell summing would an overkill for 3-4000 numbers i guess
    temp_1=0;
    temp_2=0;
    for(int i=0;i<this->mes_nspecsteps;i++){
        temp_1+=factor1[i];
        temp_2+=factor2[i];
    }
    scale_factor=temp_1/temp_2;
    
    return scale_factor;
}
Exemplo n.º 4
0
/**
* Creates a tangent between two circles or arcs.
* Out of the 4 possible tangents, the one closest to
* the given coordinate is returned.
*
* @param coord Coordinate to define which tangent we want (typically a
*              mouse coordinate).
* @param circle1 1st circle or arc entity.
* @param circle2 2nd circle or arc entity.
*/
RS_Line* RS_Creation::createTangent2(const RS_Vector& coord,
                                     RS_Entity* circle1,
                                     RS_Entity* circle2) {
	RS_Line* ret = nullptr;
    RS_Vector circleCenter1;
    RS_Vector circleCenter2;
    double circleRadius1 = 0.0;
    double circleRadius2 = 0.0;

    // check given entities:
	if(! (circle1 && circle2))
		return nullptr;
	if( !(circle1->isArc() && circle2->isArc()))
		return nullptr;

	std::vector<RS_Line*> poss;
    //        for (int i=0; i<4; ++i) {
	//            poss[i] = nullptr;
    //        }
    RS_LineData d;
    if( circle1->rtti() == RS2::EntityEllipse) {
        std::swap(circle1,circle2);//move Ellipse to the second place
    }
    circleCenter1=circle1->getCenter();
    circleRadius1=circle1->getRadius();
    circleCenter2=circle2->getCenter();
    circleRadius2=circle2->getRadius();
    if(circle2->rtti() != RS2::EntityEllipse) {
        //no ellipse

        // create all possible tangents:

        double angle1 = circleCenter1.angleTo(circleCenter2);
        double dist1 = circleCenter1.distanceTo(circleCenter2);

        if (dist1>1.0e-6) {
            // outer tangents:
            double dist2 = circleRadius2 - circleRadius1;
            if (dist1>dist2) {
                double angle2 = asin(dist2/dist1);
				double angt1 = angle1 + angle2 + M_PI_2;
				double angt2 = angle1 - angle2 - M_PI_2;
				RS_Vector offs1 = RS_Vector::polar(circleRadius1, angt1);
				RS_Vector offs2 = RS_Vector::polar(circleRadius2, angt1);

				poss.push_back( new RS_Line{circleCenter1 + offs1,
													  circleCenter2 + offs2});


                offs1.setPolar(circleRadius1, angt2);
                offs2.setPolar(circleRadius2, angt2);

				poss.push_back( new RS_Line{circleCenter1 + offs1,
													  circleCenter2 + offs2});
            }

            // inner tangents:
            double dist3 = circleRadius2 + circleRadius1;
            if (dist1>dist3) {
                double angle3 = asin(dist3/dist1);
				double angt3 = angle1 + angle3 + M_PI_2;
				double angt4 = angle1 - angle3 - M_PI_2;
                RS_Vector offs1;
                RS_Vector offs2;

                offs1.setPolar(circleRadius1, angt3);
                offs2.setPolar(circleRadius2, angt3);

				poss.push_back( new RS_Line{circleCenter1 - offs1,
													  circleCenter2 + offs2});


                offs1.setPolar(circleRadius1, angt4);
                offs2.setPolar(circleRadius2, angt4);

				poss.push_back( new RS_Line{circleCenter1 - offs1,
													  circleCenter2 + offs2});
            }

        }
    }else{
        //circle2 is Ellipse
		std::unique_ptr<RS_Ellipse> e2((RS_Ellipse*)circle2->clone());
//        RS_Ellipse* e2=new RS_Ellipse(nullptr,RS_EllipseData(RS_Vector(4.,1.),RS_Vector(2.,0.),0.5,0.,0.,false));
//        RS_Ellipse  e3(nullptr,RS_EllipseData(RS_Vector(4.,1.),RS_Vector(2.,0.),0.5,0.,0.,false));
//        RS_Ellipse* circle1=new RS_Ellipse(nullptr,RS_EllipseData(RS_Vector(0.,0.),RS_Vector(1.,0.),1.,0.,0.,false));
        RS_Vector m0(circle1->getCenter());
//        std::cout<<"translation: "<<-m0<<std::endl;
        e2->move(-m0); //circle1 centered at origin

        double a,b;
        double a0(0.);
        if(circle1->rtti() != RS2::EntityEllipse){//circle1 is either arc or circle
            a=fabs(circle1->getRadius());
            b=a;
			if(fabs(a)<RS_TOLERANCE) return nullptr;
        }else{//circle1 is ellipse
            RS_Ellipse* e1=static_cast<RS_Ellipse*>(circle1);
            a0=e1->getAngle();
//            std::cout<<"rotation: "<<-a0<<std::endl;
            e2->rotate(-a0);//e1 major axis along x-axis
            a=e1->getMajorRadius();
            b=e1->getRatio()*a;
			if(fabs(a)<RS_TOLERANCE || fabs(b)<RS_TOLERANCE) return nullptr;
        }
        RS_Vector factor1(1./a,1./b);
//        std::cout<<"scaling: factor1="<<factor1<<std::endl;
        e2->scale(RS_Vector(0.,0.),factor1);//circle1 is a unit circle
        factor1.set(a,b);
        double a2(e2->getAngle());
//        std::cout<<"rotation: a2="<<-a2<<std::endl;
        e2->rotate(-a2); //ellipse2 with major axis in x-axis direction
        a=e2->getMajorP().x;
        b=a*e2->getRatio();
        RS_Vector v(e2->getCenter());
//        std::cout<<"Center: (x,y)="<<v<<std::endl;


        std::vector<double> m(0,0.);
        m.push_back(1./(a*a)); //ma000
        m.push_back(1./(b*b)); //ma000
        m.push_back(v.y*v.y-1.); //ma100
        m.push_back(v.x*v.y); //ma101
        m.push_back(v.x*v.x-1.); //ma111
        m.push_back(2.*a*b*v.y); //mb10
        m.push_back(2.*a*b*v.x); //mb11
        m.push_back(a*a*b*b); //mc1

		auto vs0=RS_Math::simultaneousQuadraticSolver(m); //to hold solutions
		if (vs0.getNumber()<1) return nullptr;
//        for(size_t i=0;i<vs0.getNumber();i++){
		for(RS_Vector vpec: vs0){
			RS_Vector vpe2(e2->getCenter()+
						   RS_Vector(vpec.y/e2->getRatio(),vpec.x*e2->getRatio()));
            vpec.x *= -1.;//direction vector of tangent
            RS_Vector vpe1(vpe2 - vpec*(RS_Vector::dotP(vpec,vpe2)/vpec.squared()));
//            std::cout<<"vpe1.squared()="<<vpe1.squared()<<std::endl;
			RS_Line *l=new RS_Line{vpe1, vpe2};
            l->rotate(a2);
            l->scale(factor1);
            l->rotate(a0);
            l->move(m0);
            poss.push_back(l);

        }
        //debugging

    }
    // find closest tangent:
	if(poss.size()<1) return nullptr;
    double minDist = RS_MAXDOUBLE;
    double dist;
    int idx = -1;
	for (size_t i=0; i<poss.size(); ++i) {
		if (poss[i]) {
            poss[i]->getNearestPointOnEntity(coord,false,&dist);
//        std::cout<<poss.size()<<": i="<<i<<" dist="<<dist<<"\n";
            if (dist<minDist) {
                minDist = dist;
                idx = i;
            }
        }
    }
//idx=static_cast<int>(poss.size()*(random()/(double(1.0)+RAND_MAX)));
    if (idx!=-1) {
        RS_LineData d = poss[idx]->getData();
		for(auto p: poss){
			if(p)
				delete p;
		}

		if (document && handleUndo) {
            document->startUndoCycle();
        }

		ret = new RS_Line{container, d};
		setEntity(ret);
    } else {
		ret = nullptr;
    }

    return ret;
}