# get the raw pointer from the vtkPoints

**URL:** https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894
**Category:** Support
**Created:** [December 26, 2020, 2:04pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894 "2020-12-26T14:04:11Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [December 26, 2020, 2:04pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/1 "2020-12-26T14:04:11Z")

</div>

Hello,

I tried to get the raw pointer of vtkPoints and send it to other services. But it looks that the return value of GetVoidPointer is incorrect. This is the code I used for testing, although I could go through every point and copy values explicitly, I’m just curious if it is possible to get the raw pointer directly.

```auto
  std::vector<double> source;
  for (int i = 0; i < 12; i++) {
    source.push_back(0.1 * i);
  }

  auto pointarray = vtkSmartPointer<vtkPoints>::New();
  pointarray->SetNumberOfPoints(4);
  for (vtkIdType i = 0; i < 4; i++) {
    pointarray->SetPoint(i, &source[i * 3]);
  }

  double* ptr = (double*)(pointarray->GetVoidPointer(0));
  int datalen = pointarray->GetNumberOfPoints() * 3;

  // why there are errors here ?
  for (int i = 0; i < datalen; i++) {
    double v = *ptr;
    if (v != 0.1 * i) {
      throw std::runtime_error("failed for data value checking");
    }
    ptr++;
  }

```

I also tested the vtkDoubleArray by similar code, it can return expected value if i use the GetVoidPointer

Thanks a lot for your help!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [December 26, 2020, 5:28pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/2 "2020-12-26T17:28:00Z")

</div>

You need to check if the point positions are stored in a vtkFloatArray or vtkDoubleArray and interpret the buffer accordingly.

Note that it is not guaranteed that you will find coordinate values in a tightly-packed xyzxyz… memory layout at the void pointer. There are some fancy new C++ constructs in VTK that allow safe access with reasonably low overhead.

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [December 27, 2020, 2:24am UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/3 "2020-12-27T02:24:04Z")

</div>

Thanks a lot for the information, do you mean this vtkArrayDispatch? ([https://vtk.org/doc/nightly/html/VTK-7-1-ArrayDispatch.html](https://vtk.org/doc/nightly/html/VTK-7-1-ArrayDispatch.html))

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [December 27, 2020, 1:04pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/4 "2020-12-27T13:04:23Z")

</div>

> [@1116](#):
>
> Thanks a lot for the information, do you mean this vtkArrayDispatch?

Yes, that’s it.

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [January 15, 2021, 4:06pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/5 "2021-01-15T16:06:39Z")

</div>

Just for curious, why the vtkPointSet only allow the user to access the inner vtkPoint by the point id? I mean why it does not provide a function that can get the underlying point data array directly. Are there some design considerations about this? Thanks!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 15, 2021, 4:16pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/6 "2021-01-15T16:16:03Z")

</div>

VTK can use various memory layouts for storing an array. VTK developers would want to discourage you from accessing the raw memory array directly because then your code would only work with a specific memory layout.

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [January 29, 2021, 7:44am UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/7 "2021-01-29T07:44:07Z")

</div>

There is a follow up question, I noticed that it is ok to get the raw memory of the points of vtkPolyData based on GetPoints function ([https://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataGetPoint](https://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataGetPoint)). But I can not find the definition of the GetPoints function in the source code, it is not defined both in [https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkPolyData.h](https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkPolyData.h) or [https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkPointSet.h](https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkPointSet.h). I’m a little bit curious about this. Thanks for your help!

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 29, 2021, 11:34am UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/8 "2021-01-29T11:34:42Z")

</div>

As I’ve heard it said, inheritance is the “goto” of object oriented systems. Meaning, the GetPoints() method is defined in a superclass of vtkPolyData - that is vtkPointSet::GetPoints(). [https://vtk.org/doc/nightly/html/classvtkPointSet.html](https://vtk.org/doc/nightly/html/classvtkPointSet.html)

And this does not return the “raw” data, it returns a vtkPoints class. In turn, you can retrieve a vtkDataArray with vtkPoints::GetData() (which ultimately is the container that holds the data). Then if you want raw data, you have to wrap your head around vtkDataArrays and array dispatch. Some introductory resources are:  
[https://vtk.org/Wiki/VTK/Tutorials/DataArrays](https://vtk.org/Wiki/VTK/Tutorials/DataArrays)  
[https://blog.kitware.com/c11-for-range-support-in-vtk/](https://blog.kitware.com/c11-for-range-support-in-vtk/)

vtkDataArrays have undergone massive evolution over the last years, largely due to the work of Allison Vacanti. There are a lot of reasons for this, partially for performance, partially to bring VTK closer to modern C++ practices, and also to support data arrays with different memory layouts (e.g., AOS vs SOA). Also, there was a lot of ugly code for dispatching to templated methods using vtkTemplateMacro (and similar) – the ArrayDispatch mechanism is brilliant and really cleans all this up.

The moral of the story is: if you really want raw data it’s quite sophisticated and complex, and the API has evolved so that there are different access patterns depending on what you want to do. My advice for beginners is to use the virtualized API SetPoint()/GetPoint() (for points) or InsertTuple() etc for data arrays. Also, there are hundreds of examples if you dig into the various filters and see how things are implemented.

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [January 29, 2021, 3:45pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/9 "2021-01-29T15:45:40Z")

</div>

Thanks so much for the informative reply!

The interesting thing is that, for the online document here ([https://vtk.org/doc/nightly/html/classvtkPointSet.html](https://vtk.org/doc/nightly/html/classvtkPointSet.html)), there is a virtual function to get the vtkPoints by GetPoints, but when I try to check the source code to get more details ([https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkPointSet.h](https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkPointSet.h)), I could not find that function, that is a little bit annoying. Could you point out this function’s definition at the source code repo?

Some background: one part of our project need to transfer the vtkPolyData to other processes by RPC, and we use the MarshalDataObject function to do it firstly, then we realize that maybe it is faster to transfer different subcomponents (such as points, cell, etc.) separately. Then we think it might be faster if we could transfer the sub-components by shallow copy instead of iterating all points and copy value by GetPoint.

Although there is a new ArrayDispatch mechanism (we still need some time to understand it fully), it may be mainly designed to access the raw data for the efficiency of the filter algorithm (if I understand it correctly). If we want to transfer the raw data of sub-components, the original GetVoidPointer may be more convenient in this case (if we know the value type in advance)?

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 29, 2021, 3:48pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/10 "2021-01-29T15:48:33Z")

</div>

You need to learn about the magic of vtk macros: look for vtkGetObjectMacro(Points, vtkPoints); in vtkPointSet.h

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [January 29, 2021, 3:56pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/11 "2021-01-29T15:56:36Z")

</div>

Thanks a lot for the information!

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [May 24, 2021, 2:51pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/12 "2021-05-24T14:51:09Z")

</div>

Hello, Dr. Schroeder:

I try to go through the examples about different solutions to range the vtkdataArray, and run them step by step based on this blog from kitware:

> **[C++11 for-range support in VTK - Kitware Blog](https://blog.kitware.com/c11-for-range-support-in-vtk/)**
>
> Almost two years ago, the VTK development team made C++11 a hard dependency. The goal was to make VTK development less error-prone (e.g. adopting the ... Read More

There are two small questions:

For the section `Type-Explicit-Subclass APIs`, is there a way to create the vectors and magnitudes with the type of `vtkGenericDataArrayTemplate`?, it looks the latest code uses the `vtkGenericDataArray`, but how to create the array to call this function? For the sample code at this line:

> <https://github.com/wangzhezhe/5MCST/blob/master/vtk_example/array/forloops.cpp#L261>

I got this error message:

```auto
cannot convert 'vtkSmartPointer<vtkDoubleArray>' to 'vtkGenericDataArray<double, double>*'

```

Another question is about this line:

> <https://github.com/wangzhezhe/5MCST/blob/master/vtk_example/array/forloops.cpp#L145>

I got this error message:

```auto
error: no type named 'ComponentType' in 'struct vtk::detail::ValueRange<vtkDataArray, 1>'
  145 | using MagType = typename decltype(magRange)::ComponentType;

```

Could you provide some hints for solving these two issues?  
Thanks!

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [May 24, 2021, 3:14pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/13 "2021-05-24T15:14:25Z")

</div>

> I got this error message:

> [@1116](#):
>
> `cannot convert 'vtkSmartPointer<vtkDoubleArray>' to 'vtkGenericDataArray<double, double>*'`

I don’t think you should use a `vtkGenericDataArray` here. If you want to directly template on types like `double`, you could use `vtkAOSDataArrayTemplate<double>` in `mag3Explicit`. Even better, you could directly template on the type of the array. The signature of your function would look like:

```auto
template <typename ArrayT1, typename ArrayT2>
void mag3Explicit(ArrayT1* vectors, ArrayT2* magnitudes)
{
  using VecType = typename ArrayT1::ValueType;
  using MagType = typename ArrayT2::ValueType;
  // Do stuff ...
}

```

> I got this error message:

> [@1116](#):
>
> ```auto
> error: no type named 'ComponentType' in 'struct vtk::detail::ValueRange<vtkDataArray, 1>'
> 145 | using MagType = typename decltype(magRange)::ComponentType;
> 
> ```

The correct type inside `DataArrayValueRange` is `ValueType`. Note that there is also a `ReferenceType` defined, which can be used to write in the array, which is not the case when you get a `ValueType`.

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [May 25, 2021, 1:44pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/14 "2021-05-25T13:44:53Z")

</div>

Hello, Bearzi:

Thanks for the information! Could you point out a sample code that uses `DataArrayValueRange`?, I try to follow the example here [https://vtk.org/doc/nightly/html/classvtkArrayDispatch.html](https://vtk.org/doc/nightly/html/classvtkArrayDispatch.html) by using `auto magRange = vtk::DataArrayValueRange(mags);`, but I got this error message:

```auto
forloops.cpp:146:11: error: no type named 'ComponentType' in 'struct vtk::detail::ValueRange<vtkDataArray, 0>'
  146 | using MagType = typename decltype(magRange)::ComponentType;

```

Thanks!

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [May 25, 2021, 2:06pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/15 "2021-05-25T14:06:01Z")

</div>

You have three choices:

- `using MagType = typename decltype(magRange)::ValueType`
- `using MagType = typename decltype(magRange)::ReferenceType`
- `using MagType = typename decltype(magRange)::ConstReferenceType`

`ValueType` will be a `double` in your case. The reference types are “magic types” which lets you access elements of the array through references. In your case, you would want to use a `ReferenceType`.

You can access elements in your range using `operator[]` like an stl indexed container. You could rewrite your method:

```auto
struct Mag3Worker2 {
  template <typename VecArray, typename MagArray>
  void operator()(VecArray *vecs, MagArray *mags) {
    // Create range objects:
    const auto vecRange = vtk::DataArrayTupleRange<3>(vecs);
    auto magRange = vtk::DataArrayValueRange<1>(mags);

    using VecConstTupleRef = typename decltype(vecRange)::ConstTupleReferenceType;
    using VecConstCompRef = typename decltype(vecRange)::ConstComponentReferenceType
    using MagRef = typename decltype(magRange)::ReferenceType;
    using MagType = typename decltype(magRange)::ValueType;

    for (vtkIdType id = 0; id < vecRange.size(); ++id)
    {
       MagRef magRef = magRange[id] = 0;
       VecConstTupleRef vecTuple = vecRange[id];
       for (VecConstCompRef comp : vecTuple)
       {
         magRef += static_cast<MagType>(comp * comp);
       }
       magRef = std::sqrt(magRef);
      }
    }
  }
};

```

---

<div class="post-metadata">

### Author: ![1116](https://discourse.vtk.org/user_avatar/discourse.vtk.org/1116/32/1506_2.png) [@1116](https://discourse.vtk.org/u/1116)
#### Post date: [May 28, 2021, 9:05am UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/16 "2021-05-28T09:05:05Z")

</div>

Hello, Bearzi:

Thanks so much for your help, and this is workable code ([5MCST/forloops.cpp at master · wangzhezhe/5MCST · GitHub](https://github.com/wangzhezhe/5MCST/blob/master/vtk_example/array/forloops.cpp)) for all examples mentioned in this blog ([C++11 for-range support in VTK - Kitware Blog](https://blog.kitware.com/c11-for-range-support-in-vtk/))

I Just put it here in case other people may occur similar issues.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [May 28, 2021, 9:09am UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/17 "2021-05-28T09:09:44Z")

</div>

Yohann, Andrew I’m wondering if we should transform this code into a test and/or Example… it might be useful.

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [June 4, 2021, 5:42am UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/18 "2021-06-04T05:42:21Z")

</div>

@will.schroeder @Yohann_Bearzi here is a MR for vtk-examples: [ForLoop](https://gitlab.kitware.com/vtk/vtk-examples/-/merge_requests/196). Comments and improvements are welcome. The markdown description should probably have more comments. Also note that the code also tests for the expected output so this could be dropped into VTK as a test also.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [June 4, 2021, 10:56am UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/19 "2021-06-04T10:56:52Z")

</div>

Andrew I trust you saw my response. Yes, I agree making this a test would be useful.

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [June 4, 2021, 10:01pm UTC](https://discourse.vtk.org/t/get-the-raw-pointer-from-the-vtkpoints/4894/20 "2021-06-04T22:01:00Z")

</div>

I certainly did, thanks for the feedback. That’s a good idea. Also Yohann has gone through the code and added lots of excellent constructive comments.

I really appreciate the help.

Andrew Maclean
