# Specifing vtkDataSet::GetScalarRange()

**URL:** https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844
**Category:** Support
**Created:** [March 18, 2020, 9:32am UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844 "2020-03-18T09:32:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ChristopherWaters](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/d78d45/32.png) [@ChristopherWaters](https://discourse.vtk.org/u/ChristopherWaters)
#### Post date: [March 18, 2020, 9:32am UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/1 "2020-03-18T09:32:14Z")

</div>

**Current Implementation:**

- .vtk UnstrucredGrid file of 2D mesh that contains multiple point and cell scalar fields
- Example Fields: material (cell), grid function solution (point)

**Goal:**

- Set the scalar range of a mapper’s lookup table **only using my solution scalar**

**Current Attempt:**

```c++
unGridReader->SetFileName(fileName);
unGridReader->Update();
mapper->SetInputData(unGridReader->GetOutput());
mapper->SetLookupTable(lut);
mapper->SetScalarRange(unGridReader->GetOutput()->GetScalarRange());

```

**Problems:**

- I do not understand how to specify what scalar to use when getting the scalar range, or how to get the information of a specified scalar in general.

> Convenience method to get the range of the first component (and only the first component) of any scalars in the data set.  
> If the data has both point data and cell data, it returns the (min/max) range of **combined point and cell data**

- Does the above mean that I cannot specify which scalar to use?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 19, 2020, 12:56pm UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/2 "2020-03-19T12:56:54Z")

</div>

You can try `unGridReader->GetOutput()->[GetPointData() or GetCellData()]->GetScalars()->GetRange( integer )`. See doc here: [https://vtk.org/doc/nightly/html/classvtkDataArray.html#a825000e3c1a9d8c1177ec1df549f347f](https://vtk.org/doc/nightly/html/classvtkDataArray.html#a825000e3c1a9d8c1177ec1df549f347f) . `vtkAbstractArray::GetNumberOfComponents()` can return the number of scalars per data/cell. The number you pass to `GetRange()` should be between 0 and `vtkAbstractArray::GetNumberOfComponents()`-1.

---

<div class="post-metadata">

### Author: ![gpu](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/e47c2d/32.png) [@gpu](https://discourse.vtk.org/u/gpu)
#### Post date: [July 31, 2023, 8:22am UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/3 "2023-07-31T08:22:10Z")

</div>

ComputeFiniteScalarRange in LoadVolume is too Slow…is it able to optimize or not use the ScalarRange?

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [July 31, 2023, 12:56pm UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/4 "2023-07-31T12:56:23Z")

</div>

The first render is slow mostly because of this range computation. Successive calls to compute range use the cached range in the data. You could speed up the first call by caching the range separately. The most straightforward way is to call compute range when loading data before the first render.

---

<div class="post-metadata">

### Author: ![gpu](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/e47c2d/32.png) [@gpu](https://discourse.vtk.org/u/gpu)
#### Post date: [August 1, 2023, 1:09am UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/5 "2023-08-01T01:09:45Z")

</div>

> [@gpu](#):
>
> LoadVolume

Thanks!  
what if use fixed values for ScalarRange, eg. use short\_min and short\_max instead of the computed values? decrease the precision while store and sample the transFunc as texture?

---

<div class="post-metadata">

### Author: ![gpu](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/e47c2d/32.png) [@gpu](https://discourse.vtk.org/u/gpu)
#### Post date: [August 1, 2023, 1:13am UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/6 "2023-08-01T01:13:01Z")

</div>

> [@sankhesh](#):
>
> You could speed up the first call by caching the range separately. The most straightforward way is to call compute range when loading data before the first render.

could you please advise some good place to do this, which function? Thanks

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [August 1, 2023, 4:07am UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/7 "2023-08-01T04:07:22Z")

</div>

Once you load some data, make sure that you call the range getter

```cpp
vtkDataArray* da = data->GetScalars();
  
// cache the scalar range
double* range = da->GetRange();
  
// do the rendering
mapper->SetInputData(data);

```

---

<div class="post-metadata">

### Author: ![gpu](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/e47c2d/32.png) [@gpu](https://discourse.vtk.org/u/gpu)
#### Post date: [August 1, 2023, 4:17am UTC](https://discourse.vtk.org/t/specifing-vtkdataset-getscalarrange/2844/8 "2023-08-01T04:17:42Z")

</div>

thanks
