Пример #1
0
void VtkVisPointSetItem::Initialize(vtkRenderer* renderer)
{
    // TODO vtkTransformFilter creates a new copy of the point coordinates which
    // conflicts with VtkMappedMeshSource. Find a workaround!
    _transformFilter = vtkTransformFilter::New();
    vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
    transform->Identity();
    _transformFilter->SetTransform(transform);

    _transformFilter->SetInputConnection(_algorithm->GetOutputPort());
    _transformFilter->Update();

    _renderer = renderer;
    _mapper = QVtkDataSetMapper::New();
    _mapper->InterpolateScalarsBeforeMappingOff();
    _mapper->SetColorModeToMapScalars();

    _mapper->SetInputConnection(_transformFilter->GetOutputPort());
    _actor = vtkActor::New();
    static_cast<vtkActor*>(_actor)->SetMapper(_mapper);
    _renderer->AddActor(_actor);

    // Determine the right pre-set properties
    // Order is: _algorithm, _compositeFilter, create a new one with props copied from parent
    VtkAlgorithmProperties* vtkProps = dynamic_cast<VtkAlgorithmProperties*>(_algorithm);
    if (!vtkProps)
    {
        vtkProps = dynamic_cast<VtkAlgorithmProperties*>(_compositeFilter);

        // Copy properties from parent or create a new VtkAlgorithmProperties
        if (!vtkProps)
        {
            VtkVisPipelineItem* parentItem = dynamic_cast<VtkVisPipelineItem*>(this->parentItem());
            while (parentItem)
            {
                VtkAlgorithmProperties* parentProps = nullptr;
                if(dynamic_cast<VtkVisPointSetItem*>(parentItem))
                    parentProps = dynamic_cast<VtkVisPointSetItem*>(parentItem)->getVtkProperties();
                if (parentProps)
                {
                    vtkProps = new VtkAlgorithmProperties(); // TODO memory leak?
                    vtkProps->SetScalarVisibility(parentProps->GetScalarVisibility());
                    vtkProps->SetTexture(parentProps->GetTexture());
                    vtkProps->SetActiveAttribute(parentProps->GetActiveAttribute());
                    parentItem = nullptr;
                }
                else
                    parentItem = dynamic_cast<VtkVisPipelineItem*>(parentItem->parentItem());
            }

            // Has no parents
            if (!vtkProps)
                vtkProps = new VtkAlgorithmProperties(); // TODO memory leak?
        }
    }
    _vtkProps = vtkProps;

    if (vtkProps->GetActiveAttribute().length() == 0)
    {
        // Get first scalar and set it to active
        QStringList arrayNames = this->getScalarArrayNames();
        if (arrayNames.length() > 0)
            vtkProps->SetActiveAttribute(arrayNames[0]);
        else
            vtkProps->SetActiveAttribute("Solid Color");
    }
    this->setVtkProperties(vtkProps);
    this->SetActiveAttribute(vtkProps->GetActiveAttribute());


    // Set global backface culling
    QSettings settings;
    bool backfaceCulling = settings.value("globalCullBackfaces", 0).toBool();
    this->setBackfaceCulling(backfaceCulling);

    // Set the correct threshold range
    if (dynamic_cast<VtkCompositeThresholdFilter*>(this->_compositeFilter) )
    {
        double range[2];
        this->GetRangeForActiveAttribute(range);
        QList<QVariant> thresholdRangeList;
        thresholdRangeList.push_back(range[0]);
        thresholdRangeList.push_back(range[1]);
        dynamic_cast<VtkCompositeFilter*>(this->_compositeFilter)
            ->SetUserVectorProperty("Range", thresholdRangeList);
    }

    // Show edges on meshes
    if (dynamic_cast<MeshLib::VtkMappedMeshSource*>(this->_algorithm))
        _vtkProps->GetProperties()->SetEdgeVisibility(1);
}
Пример #2
0
void VtkVisImageItem::Initialize(vtkRenderer* renderer)
{
	vtkImageAlgorithm* img = dynamic_cast<vtkImageAlgorithm*>(_algorithm);
	img->Update();
	//VtkGeoImageSource* img = dynamic_cast<VtkGeoImageSource*>(_algorithm);

	double origin[3];
	double spacing[3];
	double range[2];
	img->GetOutput()->GetOrigin(origin);
	img->GetOutput()->GetSpacing(spacing);
	//img->getRange(range);
	img->GetOutput()->GetPointData()->GetScalars()->GetRange(range);
	vtkImageShiftScale* scale = vtkImageShiftScale::New();
	scale->SetOutputScalarTypeToUnsignedChar();
	scale->SetInputConnection(img->GetOutputPort());
	scale->SetShift(-range[0]);
	scale->SetScale(255.0/(range[1]-range[0]));

	_transformFilter = vtkImageChangeInformation::New();
	_transformFilter->SetInputConnection(scale->GetOutputPort());
	//double origin[3];
	//img->getOrigin(origin);
	//double spacing = img->getSpacing();
	//_transformFilter->SetOutputOrigin(origin2);
	//_transformFilter->SetOutputSpacing(spacing2);
	_transformFilter->Update();

	_renderer = renderer;

	// Use a special vtkImageActor instead of vtkActor
	vtkImageActor* imageActor = vtkImageActor::New();
	imageActor->SetInput(_transformFilter->GetOutput());
	_actor = imageActor;
	_renderer->AddActor(_actor);

	// Set pre-set properties
	VtkAlgorithmProperties* vtkProps = dynamic_cast<VtkAlgorithmProperties*>(_algorithm);
	if (vtkProps)
		setVtkProperties(vtkProps);

	VtkVisPipelineItem* parentItem = dynamic_cast<VtkVisPipelineItem*>(this->parentItem());
	while (parentItem)
	{
		VtkAlgorithmProperties* parentProps =
		        dynamic_cast<VtkAlgorithmProperties*>(parentItem->algorithm());
		if (parentProps)
		{
			VtkAlgorithmProperties* newProps = new VtkAlgorithmProperties();
			newProps->SetScalarVisibility(parentProps->GetScalarVisibility());
			newProps->SetTexture(parentProps->GetTexture());
			setVtkProperties(newProps);
			vtkProps = newProps;
			parentItem = NULL;
		}
		else
			parentItem = dynamic_cast<VtkVisPipelineItem*>(parentItem->parentItem());
	}

	// Set active scalar to the desired one from VtkAlgorithmProperties
	// or to match those of the parent.
	if (vtkProps)
	{
		if (vtkProps->GetActiveAttribute().length() > 0)
			this->SetActiveAttribute(vtkProps->GetActiveAttribute());
		else
		{
			VtkVisPipelineItem* visParentItem =
			        dynamic_cast<VtkVisPipelineItem*>(this->parentItem());
			if (visParentItem)
				this->SetActiveAttribute(visParentItem->GetActiveAttribute());
			if (vtkProps->GetTexture() != NULL)
				this->SetActiveAttribute("Solid Color");
		}
	}
}