double ManualImage2ImageRegistrationWidget::getAccuracy(QString uid)
{
    DataPtr fixedData = mServices->registration()->getFixedData();
    if (!fixedData)
        return 1000.0;
    DataPtr movingData = mServices->registration()->getMovingData();
    if (!movingData)
        return 1000.0;

    Landmark masterLandmark = fixedData->getLandmarks()->getLandmarks()[uid];
    Landmark targetLandmark = movingData->getLandmarks()->getLandmarks()[uid];
    if (masterLandmark.getUid().isEmpty() || targetLandmark.getUid().isEmpty())
        return 1000.0;

    Vector3D p_master_master = masterLandmark.getCoord();
    Vector3D p_target_target = targetLandmark.getCoord();
    Transform3D rMmaster = fixedData->get_rMd();
    Transform3D rMtarget = movingData->get_rMd();

    Vector3D p_target_r = rMtarget.coord(p_target_target);
    Vector3D p_master_r = rMmaster.coord(p_master_master);

    double  targetPoint[3];
    double  masterPoint[3];
    targetPoint[0] = p_target_r[0];
    targetPoint[1] = p_target_r[1];
    targetPoint[2] = p_target_r[2];
    masterPoint[0] = p_master_r[0];
    masterPoint[1] = p_master_r[1];
    masterPoint[2] = p_master_r[2];

    return (vtkMath::Distance2BetweenPoints(targetPoint, masterPoint));
}
void CustomMetric::updateTexture(MeshPtr model, Transform3D rMrr)
{
	if (!model)
		return;

	if (!this->getTextureFollowTool() || !model->hasTexture())
		return;

	// special case:
	// Project tool position down to the model, then set that position as
	// the texture x pos.

	Transform3D rMt = mSpaceProvider->getActiveToolTipTransform(CoordinateSystem::reference());
	Transform3D rMd = rMrr * model->get_rMd();
	Vector3D t_r = rMt.coord(Vector3D::Zero());
	Vector3D td_r = rMt.vector(Vector3D::UnitZ());

	DoubleBoundingBox3D bb_d = model->boundingBox();
	Vector3D bl = bb_d.bottomLeft();
	Vector3D tr = bb_d.topRight();
	Vector3D c = (bl+tr)/2;
	Vector3D x_min_r(c[0], bl[1], c[2]);
	Vector3D x_max_r(c[0], tr[1], c[2]);
	x_min_r = rMd.coord(x_min_r);
	x_max_r = rMd.coord(x_max_r);

	double t_x = dot(t_r, td_r);
	double bbmin_x = dot(x_min_r, td_r);
	double bbmax_x = dot(x_max_r, td_r);
	double range = bbmax_x-bbmin_x;
	if (similar(range, 0.0))
		range = 1.0E-6;
	double s = (t_x-bbmin_x)/range;
	model->getTextureData().getPositionY()->setValue(s);
}
Exemple #3
0
void Rect3D::updatePosition(const DoubleBoundingBox3D bb, const Transform3D& M)
{
	mPoints = vtkPointsPtr::New();
	mPoints->InsertPoint(0, M.coord(bb.corner(0,0,0)).begin());
	mPoints->InsertPoint(1, M.coord(bb.corner(0,1,0)).begin());
	mPoints->InsertPoint(2, M.coord(bb.corner(1,1,0)).begin());
	mPoints->InsertPoint(3, M.coord(bb.corner(1,0,0)).begin());
	mPolyData->SetPoints(mPoints);
}
void EraserWidget::eraseVolume(TYPE* volumePointer)
{
	ImagePtr image = mPatientModelService->getActiveImage();
	vtkImageDataPtr img = image->getBaseVtkImageData();


	Eigen::Array3i dim(img->GetDimensions());
	Vector3D spacing(img->GetSpacing());

	Transform3D rMd = mVisualizationService->getGroup(0)->getOptions().mPickerGlyph->get_rMd();
	Vector3D c(mSphere->GetCenter());
	c = rMd.coord(c);
	double r = mSphere->GetRadius();
	mPreviousCenter = c;
	mPreviousRadius = r;

	DoubleBoundingBox3D bb_r(c[0]-r, c[0]+r, c[1]-r, c[1]+r, c[2]-r, c[2]+r);

	Transform3D dMr = image->get_rMd().inv();
	Transform3D rawMd = createTransformScale(spacing).inv();
	Transform3D rawMr = rawMd * dMr;
	Vector3D c_d = dMr.coord(c);
	double r_d = dMr.vector(r * Vector3D::UnitX()).length();
	c = rawMr.coord(c);
	r = rawMr.vector(r * Vector3D::UnitX()).length();
	DoubleBoundingBox3D bb0_raw = transform(rawMr, bb_r);
	IntBoundingBox3D bb1_raw(0, dim[0], 0, dim[1], 0, dim[2]);

//	std::cout << "     sphere: " << bb0_raw << std::endl;
//	std::cout << "        raw: " << bb1_raw << std::endl;

	for (int i=0; i<3; ++i)
	{
		bb1_raw[2*i] = std::max<double>(bb1_raw[2*i], bb0_raw[2*i]);
		bb1_raw[2*i+1] = std::min<double>(bb1_raw[2*i+1], bb0_raw[2*i+1]);
	}

	int replaceVal = mEraseValueAdapter->getValue();

	for (int x = bb1_raw[0]; x < bb1_raw[1]; ++x)
		for (int y = bb1_raw[2]; y < bb1_raw[3]; ++y)
			for (int z = bb1_raw[4]; z < bb1_raw[5]; ++z)
			{
				int index = x + y * dim[0] + z * dim[0] * dim[1];
				if ((Vector3D(x*spacing[0], y*spacing[1], z*spacing[2]) - c_d).length() < r_d)
					volumePointer[index] = replaceVal;
			}
}
Exemple #5
0
vtkPolyDataPtr polydataFromTransforms(TimedTransformMap transformMap_prMt, Transform3D rMpr)
{
  vtkPolyDataPtr retval = vtkPolyDataPtr::New();

  vtkPointsPtr points = vtkPointsPtr::New();
  vtkCellArrayPtr lines = vtkCellArrayPtr::New();

  points->Allocate(transformMap_prMt.size());

  TimedTransformMap::iterator mapIter = transformMap_prMt.begin();
  while(mapIter != transformMap_prMt.end())
  {
    Vector3D point_t = Vector3D(0,0,0);

    Transform3D prMt = mapIter->second;
    Transform3D rMt = rMpr * prMt;
    Vector3D p = rMt.coord(point_t);
    points->InsertNextPoint(p.begin());

		++mapIter;
  }

  lines->Initialize();
  std::vector<vtkIdType> ids(points->GetNumberOfPoints());
  for (unsigned i=0; i<ids.size(); ++i)
    ids[i] = i;
  lines->InsertNextCell(ids.size(), &(*ids.begin()));

  retval->SetPoints(points);
  retval->SetLines(lines);
  return retval;
}
std::vector<Vector3D> CustomMetric::getPointCloud() const
{
	std::vector<Vector3D> retval;

	DataPtr model = this->getModel();

	std::vector<Transform3D> pos = this->calculateOrientations();
	std::vector<Vector3D> cloud;
	Transform3D rrMd;

	if (model)
	{
		rrMd = model->get_rMd();
		cloud = model->getPointCloud();
	}
	else
	{
		cloud.push_back(Vector3D::Zero());
		rrMd = Transform3D::Identity();
	}

	for (unsigned i=0; i<pos.size(); ++i)
	{
		Transform3D rMd = pos[i] * rrMd;

		for (unsigned j=0; j<cloud.size(); ++j)
		{
			Vector3D p_r = rMd.coord(cloud[j]);
			retval.push_back(p_r);
		}
	}

	return retval;
}
Vector3D ViewFollower::findCenter_r_fromShift_s(Vector3D shift_s)
{
	Transform3D sMr = mSliceProxy->get_sMr();
	Vector3D c_s = sMr.coord(mDataManager->getCenter());

	Vector3D newcenter_s = c_s + shift_s;

	Vector3D newcenter_r = sMr.inv().coord(newcenter_s);
	return newcenter_r;
}
Vector3D ViewFollower::findVirtualTooltip_s()
{
	ToolPtr tool = mSliceProxy->getTool();
	Transform3D sMr = mSliceProxy->get_sMr();
	Transform3D rMpr = mDataManager->get_rMpr();
	Transform3D prMt = tool->get_prMt();
	Vector3D pt_s = sMr * rMpr * prMt.coord(Vector3D(0,0,tool->getTooltipOffset()));
	pt_s[2] = 0; // project into plane
	return pt_s;
}
Vector3D ToolTipCalibrationCalculator::get_sampledPoint_ref()
{
  CoordinateSystem csT = mSpaces->getT(mTool); //from
  CoordinateSystem csRef = mSpaces->getT(mRef); //to

  Transform3D refMt = mSpaces->get_toMfrom(csT, csRef);

  Vector3D P_ref = refMt.coord(mP_t);

  return P_ref;
}
double LandmarkRegistrationWidget::getAccuracy(QString uid)
{
	DataPtr fixedData = mServices->registration()->getFixedData();
	if (!fixedData)
		return 1000.0;

	Landmark masterLandmark = fixedData->getLandmarks()->getLandmarks()[uid];
	Landmark targetLandmark = this->getTargetLandmarks()[uid];
	if (masterLandmark.getUid().isEmpty() || targetLandmark.getUid().isEmpty())
		return 1000.0;

	Vector3D p_master_master = masterLandmark.getCoord();
	Vector3D p_target_target = targetLandmark.getCoord();
	Transform3D rMmaster = fixedData->get_rMd();
	Transform3D rMtarget = this->getTargetTransform();

	Vector3D p_target_r = rMtarget.coord(p_target_target);
	Vector3D p_master_r = rMmaster.coord(p_master_master);

	return (p_target_r - p_master_r).length();
}
std::vector<Vector3D> RegistrationImplService::convertAndTransformToPoints(const std::vector<QString>& uids, const LandmarkMap& data, Transform3D M)
{
	std::vector<Vector3D> retval;

	for (unsigned i=0; i<uids.size(); ++i)
	{
		QString uid = uids[i];
		Vector3D p = M.coord(data.find(uid)->second.getCoord());
		retval.push_back(p);
	}
	return retval;
}
/**Convert the landmarks given by uids to vtk points.
 * The coordinates are given by the input data,
 * and should be transformed by M.
 *
 * Prerequisite: all uids exist in data.
 */
vtkPointsPtr RegistrationImplService::convertTovtkPoints(const std::vector<QString>& uids, const LandmarkMap& data, Transform3D M)
{
	vtkPointsPtr retval = vtkPointsPtr::New();

	for (unsigned i=0; i<uids.size(); ++i)
	{
		QString uid = uids[i];
		Vector3D p = M.coord(data.find(uid)->second.getCoord());
		retval->InsertNextPoint(p.begin());
	}
	return retval;
}
Exemple #13
0
void PickerRep::toolHasChanged()
{
	if (!mTool)
		return;
	Transform3D prMt = mTool->get_prMt();
	Transform3D rMpr = mDataManager->get_rMpr();
	Transform3D rMt = rMpr * prMt;
	Vector3D p_r = rMt.coord(Vector3D(0, 0, mTool->getTooltipOffset()));

	mPickedPoint = p_r;
	if (mGraphicalPoint)
		mGraphicalPoint->setValue(mPickedPoint);
	this->setGlyphCenter(mPickedPoint);
}
void SlicePlaneClipper::updateClipPlane()
{
	if (!mSlicer)
		return;
	if (!mClipPlane)
		mClipPlane = vtkPlanePtr::New();

	Transform3D rMs = mSlicer->get_sMr().inv();

	Vector3D n = rMs.vector(this->getUnitNormal());
	Vector3D p = rMs.coord(Vector3D(0,0,0));
	mClipPlane->SetNormal(n.begin());
	mClipPlane->SetOrigin(p.begin());
}
Exemple #15
0
void EraserWidget::continousRemoveSlot()
{
	Transform3D rMd = mVisualizationService->getGroup(0)->getOptions().mPickerGlyph->get_rMd();
	//Transform3D rMd = mVisualizationService->getViewGroupDatas().front()->getData()->getOptions().mPickerGlyph->get_rMd();
	Vector3D c(mSphere->GetCenter());
	c = rMd.coord(c);
	double r = mSphere->GetRadius();

	// optimization: dont remove if idle
	if (similar(mPreviousCenter, c) && similar(mPreviousRadius, r))
		return;

	this->removeSlot();
}
void LandmarkRegistrationWidget::setManualToolPosition(Vector3D p_r)
{
	Transform3D rMpr = mServices->patient()->get_rMpr();
	Vector3D p_pr = rMpr.inv().coord(p_r);

	// set the picked point as offset tip
	ToolPtr tool = mServices->tracking()->getManualTool();
	Vector3D offset = tool->get_prMt().vector(Vector3D(0, 0, tool->getTooltipOffset()));
	p_pr -= offset;
	p_r = rMpr.coord(p_pr);

	// TODO set center here will not do: must handle
	mServices->patient()->setCenter(p_r);
	Vector3D p0_pr = tool->get_prMt().coord(Vector3D(0, 0, 0));
	tool->set_prMt(createTransformTranslate(p_pr - p0_pr) * tool->get_prMt());
}
Exemple #17
0
void GraphicalAxes3D::rescale()
{
	if (!mViewportListener->isListening())
		return;

	double size = mViewportListener->getVpnZoom();
	double axisSize = mSize/size;
	double scale = axisSize / m_vtkAxisLength;

	// NOTE: vtkAxesActor dislikes small values for SetTotalLength, thus we
	// keep that value constant at m_vtkAxisLength and instead scale the transform.
	Transform3D rMq = m_rMt * createTransformScale(Vector3D(scale,scale,scale));

	mActor->SetUserMatrix(rMq.getVtkMatrix());

	for (unsigned i=0; i<mCaption.size(); ++i)
	{
		Vector3D pos = rMq.coord(axisSize*mCaptionPos[i]);
		mCaption[i]->SetAttachmentPoint(pos.begin());
	}
}
std::vector<double> TemporalCalibration::computeTrackingMovement()
{
  std::vector<double> retval;
  Vector3D e_z(0,0,1);
  Vector3D origin(0,0,0);
  double zero = 0;
  Transform3D prM0t = mFileData.mPositions[0].mPos;
  Vector3D ez_pr = prM0t.vector(e_z);

  for (unsigned i=0; i<mFileData.mPositions.size(); ++i)
  {
    Transform3D prMt = mFileData.mPositions[i].mPos;
    Vector3D p_pr = prMt.coord(origin);

    double val = dot(ez_pr, p_pr);

    if (retval.empty())
      zero = val;

    retval.push_back(val-zero);
  }

  if (mAddRawToDebug)
  {
		mDebugStream << "=======================================" << std::endl;
		mDebugStream << "tracking raw data:" << std::endl;
		mDebugStream << std::endl;
		mDebugStream << "timestamp" << "\t" << "pos" << std::endl;
		for (unsigned x = 0; x < mFileData.mPositions.size(); ++x)
		{
			mDebugStream << mFileData.mPositions[x].mTime << "\t" << retval[x] << std::endl;
		}
		mDebugStream << std::endl;
		mDebugStream << "=======================================" << std::endl;
  }

  return retval;
}
Exemple #19
0
/**Transform bb using the transform m.
 * Only the two defining corners are actually transformed.
 */
DoubleBoundingBox3D transform(const Transform3D& m, const DoubleBoundingBox3D& bb)
{
	Vector3D a = m.coord(bb.bottomLeft());
	Vector3D b = m.coord(bb.topRight());
	return DoubleBoundingBox3D(a, b);
}
void LandmarkRegistrationWidget::prePaintEvent()
{
	mLandmarkTableWidget->blockSignals(true);
	mLandmarkTableWidget->clear();

	QString fixedName;
	DataPtr fixedData = boost::dynamic_pointer_cast<Data>(mServices->registration()->getFixedData());
	if (fixedData)
		fixedName = fixedData->getName();

	std::vector<Landmark> landmarks = this->getAllLandmarks();
	LandmarkMap targetData = this->getTargetLandmarks();
	Transform3D rMtarget = this->getTargetTransform();

	//ready the table widget
	mLandmarkTableWidget->setRowCount((int)landmarks.size());
	mLandmarkTableWidget->setColumnCount(4);
	QStringList headerItems(QStringList() << "Name" << "Status" << "Coordinates" << "Accuracy (mm)");
	mLandmarkTableWidget->setHorizontalHeaderLabels(headerItems);
	mLandmarkTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
	mLandmarkTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);

	for (unsigned i = 0; i < landmarks.size(); ++i)
	{
		std::vector<QTableWidgetItem*> items(4); // name, status, coordinates, accuracy

		LandmarkProperty prop = mServices->patient()->getLandmarkProperties()[landmarks[i].getUid()];
		Vector3D coord = landmarks[i].getCoord();
		coord = rMtarget.coord(coord); // display coordinates in space r (in principle, this means all coords should be equal)

		items[0] = new QTableWidgetItem(qstring_cast(prop.getName()));
		items[0]->setToolTip(QString("Landmark name. Double-click to rename."));

		items[1] = new QTableWidgetItem;

		if (prop.getActive())
			items[1]->setCheckState(Qt::Checked);
		else
			items[1]->setCheckState(Qt::Unchecked);
		items[1]->setToolTip(QString("Check to use landmark in current registration."));

		QString coordText = "Not sampled";
		if (targetData.count(prop.getUid()))
		{
			int width = 5;
			int prec = 1;
			coordText = tr("(%1, %2, %3)").arg(coord[0], width, 'f', prec).arg(coord[1], width, 'f', prec).arg(
				coord[2], width, 'f', prec);
		}

		items[2] = new QTableWidgetItem(coordText);
		items[2]->setToolTip(QString("Landmark coordinates of target [%1] in reference space.").arg(this->getTargetName()));

		items[3] = new QTableWidgetItem(tr("%1").arg(this->getAccuracy(landmarks[i].getUid())));
		items[3]->setToolTip(QString("Distance from target [%1] to fixed [%2].").arg(this->getTargetName()).arg(fixedName));

		for (unsigned j = 0; j < items.size(); ++j)
		{
			items[j]->setData(Qt::UserRole, qstring_cast(prop.getUid()));
			mLandmarkTableWidget->setItem(i, j, items[j]);
		}

		//highlight selected row
		if (prop.getUid() == mActiveLandmark)
		{
			mLandmarkTableWidget->setCurrentItem(items[2]);
		}
	}

	this->updateAverageAccuracyLabel();
	mLandmarkTableWidget->blockSignals(false);
}