//Function to summarize results from BeQTL and return a dataframe
// [[Rcpp::export]]
Rcpp::DataFrame SumRes(const arma::mat  & cormat, const arma::mat & errmat, const Rcpp::DataFrame SnpDF, const Rcpp::DataFrame Genedf, const int samplesize, const double tcutoff){
  Rcpp::Rcout<<"Generating Tmat"<<std::endl;
  arma::mat tmat = sqrt(samplesize-2)*(cormat/(sqrt(1-square(cormat))));
  Rcpp::Rcout<<"Finding strong t"<<std::endl;
  arma::uvec goods = find(abs(tmat)>tcutoff);
  Rcpp::Rcout<<"Generating matrix index"<<std::endl;
  arma::umat goodmat = Ind(tmat.n_rows,goods);
  Rcpp::Rcout<<"Subsetting tmat"<<std::endl;
  Rcpp::Rcout<<"This many good results"<<goodmat.n_rows<<std::endl;
  arma::vec tvec = tmat(goods);
  Rcpp::Rcout<<"Subsetting Errmat"<<std::endl;
  arma::vec errvec = errmat(goods);
  Rcpp::Rcout<<"Generating SNP and Gene lists"<<std::endl;
  Rcpp::IntegerVector GoodGenes = Rcpp::wrap(arma::conv_to<arma::ivec>::from(goodmat.col(0)));
  Rcpp::IntegerVector GoodSNPs = Rcpp::wrap(arma::conv_to<arma::ivec>::from(goodmat.col(1)));

//Subset SNP anno

  Rcpp::CharacterVector SNPnames = SnpDF["rsid"];
  SNPnames = SNPnames[GoodSNPs];
  arma::ivec SNPchr = Rcpp::as<arma::ivec>(SnpDF["Chrom"]);
  SNPchr = SNPchr(goodmat.col(1));
  arma::ivec SNPpos = Rcpp::as<arma::ivec>(SnpDF["Pos"]);
  SNPpos = SNPpos(goodmat.col(1));
//Subset Geneanno
  Rcpp::CharacterVector GeneNames = Genedf["Symbol"];
  GeneNames = GeneNames[GoodGenes];
  arma::ivec Genechr = Rcpp::as<arma::ivec>(Genedf["Chrom"]);
  Genechr = Genechr(goodmat.col(0));
  arma::ivec Genestart = Rcpp::as<arma::ivec>(Genedf["Start"]);
  Genestart = Genestart(goodmat.col(0));
  arma::ivec Genestop = Rcpp::as<arma::ivec>(Genedf["Stop"]);
  Genestop = Genestop(goodmat.col(0));

  arma::ivec CisDist(GoodGenes.length());
  Rcpp::Rcout<<"Calculating Cisdist"<<std::endl;
  CisDist = arma::min(arma::join_cols(abs(Genestop-SNPpos),abs(Genestart-SNPpos)),1);
  Rcpp::Rcout<<"CisDist Calculated"<<std::endl;
  CisDist.elem(find(Genechr!=SNPchr)).fill(-1);
  return  Rcpp::DataFrame::create(Rcpp::Named("SNP")=SNPnames,
                                  Rcpp::Named("Gene")=GeneNames,
                                  Rcpp::Named("t-stat")=Rcpp::wrap(tvec),
                                  Rcpp::Named("err")=Rcpp::wrap(errvec),
                                  Rcpp::Named("CisDist")=Rcpp::wrap(CisDist));

}
	PeopleStatePtr ObservationManager::getStateFromRect(cv::Rect &rt)
	{
		assert(img_depth_.rows > 0);
		assert(img_depth_.cols > 0);
		
		cv::Point im_top(rt.x + rt.width / 2, rt.y);
		btVector3 pt3;
		// get median depth of the bounding box.
		cv::Rect roi = cv::Rect(rt.x + 0.3 * rt.width, rt.y + 0.1 * rt.height, rt.width * 0.4, rt.height * 0.7);
		// TODO : may need to ensure roi is inside of the image
		if((roi.x < 0) ||  (roi.y < 0) 
			|| (roi.x + roi.width >= img_depth_.cols) || (roi.y + roi.height >= img_depth_.rows)
			|| (roi.width <= 0) || (roi.height <= 0))
		{
			PeopleStatePtr state = boost::make_shared<PeopleState>(PeopleState());
			state->x_ = 0;		state->y_ = 0;		state->z_ = 0;
			return state;
		}

		// cv::Mat depth_roi_temp(img_depth_, roi);
		cv::Mat tmat(1, roi.width * roi.height, CV_32FC1);
		// = depth_roi_temp.reshape(1, depth_roi_temp.rows * depth_roi_temp.cols);
		
		int cnt = 0;
		for(int i = 0; i < roi.width; i++) {
			for(int j = 0; j < roi.height; j++, cnt++)
				tmat.at<float>(0, cnt) = img_depth_.at<float>(roi.y + j, roi.x + i);
		}

		cv::Mat tmat_sorted;
		cv::sort(tmat, tmat_sorted, CV_SORT_EVERY_COLUMN + CV_SORT_DESCENDING);
		// in camera coordinate, depth is z!
		pt3[ 2 ] = tmat_sorted.at<float>(floor(cv::countNonZero(tmat_sorted) / 2.0));
		if((pt3[ 2 ] == 0) || (pt3[ 2 ] > 15))
		{
			// need to find alternative way to get the proposals.
			PeopleStatePtr state = boost::make_shared<PeopleState>(PeopleState());
#if 1
			cv::Point im_bottom(rt.x + rt.width / 2, rt.y + rt.height);

			cv::Point3d ray1 = cam_model_.projectPixelTo3dRay(im_top);
			cv::Point3d ray2 = cam_model_.projectPixelTo3dRay(im_bottom);
			assert(ray1.z == ray2.z);

			pt3[ 2 ] = UB_HEIGHT * ray1.z / (ray2.y - ray1.y);

			pt3[ 0 ] = pt3[ 2 ] * ray1.x / ray1.z;
			pt3[ 1 ] = pt3[ 2 ] * ray1.y / ray1.z;

			pt3 = cam_to_ref_rot_ * pt3 + cam_to_ref_trans_;
			state->x_ = pt3[ 0 ];		state->y_ = pt3[ 1 ];		state->z_ = pt3[ 2 ];
#else
			state->x_ = 0;		state->y_ = 0;		state->z_ = 0;
#endif
			return state;
		}
		// get inverse-projection
		cv::Point3d ray = cam_model_.projectPixelTo3dRay(im_top);
		pt3[ 0 ] = pt3[ 2 ] * ray.x / ray.z;
		pt3[ 1 ] = pt3[ 2 ] * ray.y / ray.z;
		// transform to reference co-ordinate
		pt3 = cam_to_ref_rot_ * pt3 + cam_to_ref_trans_;

		PeopleStatePtr state = boost::make_shared<PeopleState>(PeopleState());
		state->x_ = pt3[ 0 ];		state->y_ = pt3[ 1 ];		state->z_ = pt3[ 2 ];
		return state;
	}
Example #3
0
/* ****************************************************************
 * clr__cspace_to_xyz (cspace, t_mat)
 * CLR_XYZ       cspace[4]   (in)  - the color space definition,
 *                                      3 primaries and white
 * double        t_mat[3][3] (mod) - the color transformation
 *
 * Builds the transformation from a set of primaries to the CIEXYZ
 * color space.  This is the basis for the generation of the color
 * transformations in the CLR_ routine set.  The method used is
 * that detailed in Sect 3.2 Colorimetry and the RGB monitor.
 * Returns RGB to XYZ matrix.
 * From Roy Hall, pg 239-240.
 *
 * The RGB white point of (1, 1, 1) times this matrix gives the
 * (Y=1 normalized) XYZ white point of (0.951368, 1, 1.08815)
 * From Roy Hall, pg 54.
 * MAT3X3VEC(xyz, rgb2xyz, rgb);
 *
 * Returns -
 * 0 if there is a singularity.
 * !0 if OK
 */
int
rt_clr__cspace_to_xyz (const point_t cspace[4],
		       mat_t rgb2xyz)
{
    int ii, jj, kk, tmp_i, ind[3];
    fastf_t mult, white[3], scale[3];
    mat_t t_mat;

    /* Might want to enforce X+Y+Z=1 for 4 inputs.  Roy does, on pg 229. */

    /* normalize the white point to Y=1 */
#define WHITE 3
    if (cspace[WHITE][Y] <= 0.0) return 0;
    white[0] = cspace[WHITE][X] / cspace[WHITE][Y];
    white[1] = 1.0;
    white[2] = cspace[WHITE][Z] / cspace[WHITE][Y];

#define tmat(a, b) t_mat[(a)*4+(b)]
    MAT_IDN(t_mat);
    for (ii=0; ii<=2; ii++) {
	tmat(0, ii) = cspace[ii][X];
	tmat(1, ii) = cspace[ii][Y];
	tmat(2, ii) = cspace[ii][Z];
	ind[ii] = ii;
    }

    /* gaussian elimination with partial pivoting */
    for (ii=0; ii<2; ii++) {
	for (jj=ii+1; jj<=2; jj++) {
	    if (fabs(tmat(ind[jj], ii)) > fabs(tmat(ind[ii], ii))) {
		tmp_i=ind[jj];
		ind[jj]=ind[ii];
		ind[ii]=tmp_i;
	    }
	}
	if (ZERO(tmat(ind[ii], ii))) return 0;

	for (jj=ii+1; jj<=2; jj++) {
	    mult = tmat(ind[jj], ii) / tmat(ind[ii], ii);
	    for (kk=ii+1; kk<=2; kk++)
		tmat(ind[jj], kk) -= tmat(ind[ii], kk) * mult;
	    white[ind[jj]] -= white[ind[ii]] * mult;
	}
    }
    if (ZERO(tmat(ind[2], 2))) return 0;

    /* back substitution to solve for scale */
    scale[ind[2]] = white[ind[2]] / tmat(ind[2], 2);
    scale[ind[1]] = (white[ind[1]] - (tmat(ind[1], 2) *
				      scale[ind[2]])) / tmat(ind[1], 1);
    scale[ind[0]] = (white[ind[0]] - (tmat(ind[0], 1) *
				      scale[ind[1]]) - (tmat(ind[0], 2) *
							scale[ind[2]])) / tmat(ind[0], 0);

    /* build matrix.  Embed 3x3 in BRL-CAD 4x4 */
    for (ii=0; ii<=2; ii++) {
	rgb2xyz[0*4+ii] = cspace[ii][X] * scale[ii];
	rgb2xyz[1*4+ii] = cspace[ii][Y] * scale[ii];
	rgb2xyz[2*4+ii] = cspace[ii][Z] * scale[ii];
	rgb2xyz[3*4+ii] = 0;
    }
    rgb2xyz[12] = rgb2xyz[13] = rgb2xyz[14];
    rgb2xyz[15] = 1;

    return 1;
}