# VTK Real Time 3D Model Orientation Tracker

**URL:** https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415
**Category:** Support
**Tags:** python
**Created:** [February 29, 2024, 2:47pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415 "2024-02-29T14:47:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cam](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://discourse.vtk.org/u/cam)
#### Post date: [February 29, 2024, 2:47pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/1 "2024-02-29T14:47:17Z")

</div>

Hey all,

I’m trying to build a real-time 3D model visualization tool with VTK and PyQt5 and I’m looking for some assistance.

I am currently reading data from a 6 axis IMU over serial as the following:

```auto
"second, accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z"

```

with accel values in m/s^2 and gyro values in degrees/second.

Currently I have a skeleton of a function to read values over serial and I want to be able to then rotate my 3D model to match the orientation of my sensor.

```auto
    def display_rotations_on_visual(self, data):
        if not self.uic.buttonConnectSerial.isChecked():
            pass
        else:
            line = data.strip().split(',')
            if len(line) != 7: # Checks if len is correct
                pass 
            else:
                try:
                    seconds = line[0]

                    # Accel values
                    accelX = float(line[1])
                    accelY = float(line[2])
                    accelZ = float(line[3])

                    # Gyro values 
                    gyroX = float(line[4])
                    gyroY = float(line[5])
                    gyroZ = float(line[6])
 
                    # Additional data processing and rotation handling

                except:
                    pass

```

Ideally I’m looking for some sort of data fusion with quaternions (to help negate gyro drift), but anything that remotely tracks my sensor is helpful and can be iterated on.  
  
  
Now, how would I go about this? How can I apply rotations and re-render my model in real time without causing any memory issues? Any information is useful, and I appreciate any messages.

Thank you in advance!

---

<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 1, 2024, 3:35pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/2 "2024-03-01T15:35:42Z")

</div>

Hello,

Integrating gyroscope and accelerometer inputs to update a model is not a trivial task, but doable if you pay attention. Here, the following instructable drills down the math involved such you can implement it in any programming language with basic math functions without scratching your head too much. It’s a long read but I believe all the necessary details are there. In the end, you get good estimates of the current position and orientation with respect to some starting position and orientation.

> **[Accelerometer & Gyro Tutorial](https://www.instructables.com/Accelerometer-Gyro-Tutorial/)**
>
> Accelerometer & Gyro Tutorial: Introduction
> 
> This guide is intended to everyone interested in in using Accelerometers and Gyroscopes as well as combination IMU devices (Inertial Measurement Unit) in their electronics projects
> 
> We'll cover:
> 
> 
> ...

best,

PC

---

<div class="post-metadata">

### Author: ![cam](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://discourse.vtk.org/u/cam)
#### Post date: [March 1, 2024, 4:01pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/3 "2024-03-01T16:01:56Z")

</div>

I see. I already have a basic implementation in PyOpenGl, but I’m more so looking for code specifics within VTK.

---

<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 1, 2024, 4:32pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/4 "2024-03-01T16:32:44Z")

</div>

I don’t think VTK (let alone OpenGL) has a class or algorithm in it to do that in a single shot.

---

<div class="post-metadata">

### Author: ![cam](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://discourse.vtk.org/u/cam)
#### Post date: [March 1, 2024, 4:35pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/5 "2024-03-01T16:35:29Z")

</div>

I need to apply the rotations to the rendering continuously, but I don’t know how to do that.

---

<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 1, 2024, 4:42pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/6 "2024-03-01T16:42:32Z")

</div>

> [@cam](#):
>
> but I don’t know how to do that.

I’ve just posted a detailed guide to do exactly that. But if you don’t care about the acceleration input to update position, just take a look at the following example on how to apply rotations in VTK: [https://examples.vtk.org/site/Python/Rendering/Rotations/](https://examples.vtk.org/site/Python/Rendering/Rotations/) .

---

<div class="post-metadata">

### Author: ![cam](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cam](https://discourse.vtk.org/u/cam)
#### Post date: [March 1, 2024, 4:45pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/7 "2024-03-01T16:45:16Z")

</div>

I’ve read the guide before and can do/have done the data fusion. I’m just having trouble implementing it within VTK. I’ll check out the rotation link you posted. Thank you so much for your help!

---

<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 1, 2024, 4:47pm UTC](https://discourse.vtk.org/t/vtk-real-time-3d-model-orientation-tracker/13415/8 "2024-03-01T16:47:48Z")

</div>

If you want to learn how to do stuff with VTK, then I suggest reading its primer: [VTK Textbook | VTK](https://vtk.org/vtk-textbook/)
