[vtkusers] vtkSmartVolumeMapper point centered data

Alister O Maguire aom at uoregon.edu
Wed May 17 13:37:47 EDT 2017


Hi Alvaro,

Thanks for the response. The original data set is a rectilinear grid 
(which should contain cell and point data), but I'm creating a 
vtkImageData object from the data within this grid, and this image data 
is what I'm passing to the volume mapper. VTK should be built with 
OpenGL2, so I don't believe that is an issue. Currently, I'm just using 
the default render mode for the mapper. I've attached the module that 
uses the mapper below.






/*****************************************************************************
*
* Copyright (c) 2000 - 2017, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-442911
* All rights reserved.
*
* This file is  part of VisIt. For  details, see 
https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at 
the root
* of the VisIt distribution or at 
http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  
without
* modification, are permitted provided that the following conditions are 
met:
*
*  - Redistributions of  source code must  retain the above  copyright 
notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright 
notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  
in  the
*    documentation and/or other materials provided with the 
distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its 
contributors may
*    be used to endorse or promote products derived from this software 
without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS 
"AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED 
TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  
PURPOSE
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  
SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  
FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   
CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE 
GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) 
HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  
STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN 
ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH
* DAMAGE.
*
*****************************************************************************/

// 
************************************************************************* 
//
//                           avtDefaultRenderer.C                        
     //
// 
************************************************************************* 
//

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);

#include "avtDefaultRenderer.h"
#include <avtOpenGLExtensionManager.h>

#include <vtkRectilinearGrid.h>
#include <vtkUnstructuredGrid.h>
#include <vtkRenderer.h>

#include <VolumeAttributes.h>
#include <avtCallback.h>
#include <DebugStream.h>

#include <vtkColorTransferFunction.h>
#include <vtkVolumeProperty.h>
#include <vtkImageData.h>
#include <vtkPiecewiseFunction.h>
#include <LightList.h>


// 
****************************************************************************
//  Method: avtDefaultRenderer::avtDefaultRenderer
//
//  Purpose:
//    Initialize the memebers associated with the default renderer.
//
//  Programmer:  Alister Maguire
//  Creation:    April 3, 2017
//
//  Modifications:
//
// 
****************************************************************************

avtDefaultRenderer::avtDefaultRenderer()
{
     VTKRen        = NULL;
     lastVolume    = NULL;
     gridToRender  = NULL;
     resetColorMap = false;
     volumeProp    = vtkSmartPointer<vtkVolumeProperty>::New();
     mapper        = vtkSmartPointer<vtkSmartVolumeMapper>::New();
}


// 
****************************************************************************
//  Method: avtDefaultRenderer::~avtDefaultRenderer
//
//  Purpose:
//    Destructor.
//
//  Programmer:  Alister Maguire
//  Creation:    April 3, 2017
//
//  Modifications:
//
// 
****************************************************************************

avtDefaultRenderer::~avtDefaultRenderer()
{
}


// 
****************************************************************************
//  Method:  avtDefaultRenderer::Render
//
//  Purpose:
//    Render a volume using a vtkSmartVolumeMapper
//
//  Arguments:
//    props   : the rendering properties
//    volume  : the volume to be rendered
//
//  Programmer:  Alister Maguire
//  Creation:    April 3, 2017
//
//  Modifications:
//
// 
****************************************************************************

void
avtDefaultRenderer::Render(
     const avtVolumeRendererImplementation::RenderProperties &props,
     const avtVolumeRendererImplementation::VolumeData &volume)
{
     if (props.dataIs2D)
         return;

     bool needsReset   = false;
     const char *mName = "avtDefaultRenderer::Render ";

     //Add volume data to mapper
     //Need to convert it from RectilinearGrid to ImageData
     if(gridToRender == NULL)
     {
         debug5 << mName << "Converting from rectilinear grid to image 
data" << endl;

         int dims[3], extent[6];
         ((vtkRectilinearGrid *)volume.grid)->GetDimensions(dims);
         ((vtkRectilinearGrid *)volume.grid)->GetExtent(extent);
         vtkDataArray *da = ((vtkRectilinearGrid 
*)volume.grid)->GetPointData()->GetScalars();

         vtkRectilinearGrid *rgrid = (vtkRectilinearGrid *) volume.grid;
         double spacingX = rgrid->GetXCoordinates()->GetTuple1(1)-
             rgrid->GetXCoordinates()->GetTuple1(0);
         double spacingY = rgrid->GetYCoordinates()->GetTuple1(1)-
             rgrid->GetYCoordinates()->GetTuple1(0);
         double spacingZ = rgrid->GetZCoordinates()->GetTuple1(1)-
             rgrid->GetZCoordinates()->GetTuple1(0);

         gridToRender = vtkSmartPointer<vtkImageData>::New();
         gridToRender->SetDimensions(dims);
         gridToRender->SetExtent(extent);
         gridToRender->SetSpacing(spacingX, spacingY, spacingZ);
         gridToRender->AllocateScalars(VTK_FLOAT, 1);
         int limit = dims[0] * dims[1] * dims[2];
         float *p  = (float *)gridToRender->GetScalarPointer();

         //Set the origin to match the lower bounds of the grid
         double bounds[6];
         ((vtkRectilinearGrid *)volume.grid)->GetBounds(bounds);
         gridToRender->SetOrigin(bounds[0], bounds[2], bounds[4]);

         for (int i = 0 ; i < limit ; i++)
         {
             // z-axis is reversed somehow
             p[i] = da->GetTuple1(i);
         }


         debug5 << mName << "Adding data to the mapper" << endl;

         mapper->SetInputData(gridToRender);
         mapper->SetScalarModeToUsePointData();
         mapper->SetBlendModeToComposite();
         resetColorMap = true;
         needsReset    = true;
     }

     if(resetColorMap || oldAtts != props.atts)
     {

         debug5 << mName << "Resetting color" << endl;

         //getting color/alpha transfer function from VisIt
         unsigned char rgba[256*4];
         props.atts.GetTransferFunction(rgba);
         float min = volume.data.min;
         float max = volume.data.max;
         float range = max - min;
         vtkColorTransferFunction *trans_func = 
vtkColorTransferFunction::New();
         vtkPiecewiseFunction *opacity = vtkPiecewiseFunction::New();
         for(int i = 0; i < 256; i++) {
             float pos = min + (i/255.f)*range;
             trans_func->AddRGBPoint(pos, rgba[4*i]/255.f, 
rgba[4*i+1]/255.f, rgba[4*i+2]/255.f);
             opacity->AddPoint(pos, rgba[i*4+3]/255.f);
         }

         volumeProp->SetColor(trans_func);
         volumeProp->SetScalarOpacity(opacity);

         resetColorMap = false;
         needsReset    = true;
     }

     debug5 << mName << "Rendering!" << endl;

     //Create the volume to be rendered and
     //set its mapper to our SmartVolumeMapper.
     if (lastVolume == NULL || needsReset)
     {
         if (lastVolume != NULL)
             lastVolume->Delete();
         lastVolume = vtkSmartPointer<vtkVolume>::New();
         lastVolume->SetMapper(mapper);
         lastVolume->SetProperty(volumeProp);
     }

     mapper->Render(VTKRen, lastVolume);
}






On 2017/05/17 09:14, Alvaro Sanchez wrote:
> Hi,
> I assume your dataset contains both cell and point data.  Did you
> build VTK with OpenGL2?
> Are you using any particular configuration in vtkSmartVolumeMapper
> (GPURenderMode,
> VectorMode, etc.)?
> 
> It would be nice If you could provide some sample code.
> 
> On Sun, May 14, 2017 at 11:01 PM, Alister O Maguire <aom at uoregon.edu>
> wrote:
> 
>> Hello,
>> 
>> I'm using the vtkSmartVolumeMapper for volume rendering, but it
>> appears to only be rendering as cell-centered data (you can clearly
>> see the cell boundaries). I'm calling SetScalarModeToUsePointData(),
>> but this doesn't seem to be having any affect. It actually looks the
>> same as when I call SetScalarModeToUseCellData(). Any ideas on how
>> to get this mapper to use point centered data when rendering?
>> 
>> Best,
>> Alister
>> _______________________________________________
>> Powered by www.kitware.com [1]
>> 
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html [2]
>> 
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ [3]
>> 
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>> [4]
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtkusers [5]
> 
> --
> 
> Alvaro Sanchez
> Kitware, Inc.
> Senior R&D Engineer
> 21 Corporate Drive
> Clifton Park, NY 12065-8662
> Phone: 518-881-4901
> 
> Links:
> ------
> [1] http://www.kitware.com
> [2] http://www.kitware.com/opensource/opensource.html
> [3] http://www.vtk.org/Wiki/VTK_FAQ
> [4] http://markmail.org/search/?q=vtkusers
> [5] http://public.kitware.com/mailman/listinfo/vtkusers


More information about the vtkusers mailing list