Skip to content

line

LineEntity #

Bases: QEntity

A simple entity drawing a line between two vertices.

Source code in cogip/entities/line.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class LineEntity(Qt3DCore.QEntity):
    """
    A simple entity drawing a line between two vertices.
    """

    def __init__(self, color: QtGui.QColor = QtCore.Qt.blue, parent: Qt3DCore.QEntity | None = None):
        """
        Class constructor.

        Arguments:
            color: color
            parent: parent entity
        """
        super().__init__(parent)
        self.color = color

        self.geometry = Qt3DCore.QGeometry(self)

        self.position_buffer = Qt3DCore.QBuffer(self.geometry)

        self.position_attribute = Qt3DCore.QAttribute(self.geometry)
        self.position_attribute.setName(Qt3DCore.QAttribute.defaultPositionAttributeName())
        self.position_attribute.setAttributeType(Qt3DCore.QAttribute.VertexAttribute)
        self.position_attribute.setVertexBaseType(Qt3DCore.QAttribute.Float)
        self.position_attribute.setVertexSize(3)
        self.position_attribute.setCount(2)
        self.position_attribute.setBuffer(self.position_buffer)
        self.geometry.addAttribute(self.position_attribute)

        # Connectivity between vertices
        self.indices = array("I", [0, 1])
        self.indices_bytes = QtCore.QByteArray(self.indices.tobytes())
        self.indices_buffer = Qt3DCore.QBuffer(self.geometry)
        self.indices_buffer.setData(self.indices_bytes)

        self.indices_attribute = Qt3DCore.QAttribute(self.geometry)
        self.indices_attribute.setVertexBaseType(Qt3DCore.QAttribute.UnsignedInt)
        self.indices_attribute.setAttributeType(Qt3DCore.QAttribute.IndexAttribute)
        self.indices_attribute.setBuffer(self.indices_buffer)
        self.indices_attribute.setCount(2)
        self.geometry.addAttribute(self.indices_attribute)

        # Mesh
        self.line = Qt3DRender.QGeometryRenderer(self)
        self.line.setGeometry(self.geometry)
        self.line.setPrimitiveType(Qt3DRender.QGeometryRenderer.Lines)
        self.material = Qt3DExtras.QPhongMaterial(self)
        self.material.setAmbient(self.color)

        # Entity
        self.addComponent(self.line)
        self.addComponent(self.material)

    def set_points(self, start: models.Vertex, end: models.Vertex):
        """
        Set start and end vertices.

        Arguments:
            start: start position
            end: end position
        """

        # Position vertices (start and end)
        positions = array("f")
        positions.fromlist([start.x, start.y, start.z])
        positions.fromlist([end.x, end.y, end.z])
        buffer_bytes = QtCore.QByteArray(positions.tobytes())
        self.position_buffer.setData(buffer_bytes)

__init__(color=QtCore.Qt.blue, parent=None) #

Class constructor.

Parameters:

Name Type Description Default
color QColor

color

blue
parent QEntity | None

parent entity

None
Source code in cogip/entities/line.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(self, color: QtGui.QColor = QtCore.Qt.blue, parent: Qt3DCore.QEntity | None = None):
    """
    Class constructor.

    Arguments:
        color: color
        parent: parent entity
    """
    super().__init__(parent)
    self.color = color

    self.geometry = Qt3DCore.QGeometry(self)

    self.position_buffer = Qt3DCore.QBuffer(self.geometry)

    self.position_attribute = Qt3DCore.QAttribute(self.geometry)
    self.position_attribute.setName(Qt3DCore.QAttribute.defaultPositionAttributeName())
    self.position_attribute.setAttributeType(Qt3DCore.QAttribute.VertexAttribute)
    self.position_attribute.setVertexBaseType(Qt3DCore.QAttribute.Float)
    self.position_attribute.setVertexSize(3)
    self.position_attribute.setCount(2)
    self.position_attribute.setBuffer(self.position_buffer)
    self.geometry.addAttribute(self.position_attribute)

    # Connectivity between vertices
    self.indices = array("I", [0, 1])
    self.indices_bytes = QtCore.QByteArray(self.indices.tobytes())
    self.indices_buffer = Qt3DCore.QBuffer(self.geometry)
    self.indices_buffer.setData(self.indices_bytes)

    self.indices_attribute = Qt3DCore.QAttribute(self.geometry)
    self.indices_attribute.setVertexBaseType(Qt3DCore.QAttribute.UnsignedInt)
    self.indices_attribute.setAttributeType(Qt3DCore.QAttribute.IndexAttribute)
    self.indices_attribute.setBuffer(self.indices_buffer)
    self.indices_attribute.setCount(2)
    self.geometry.addAttribute(self.indices_attribute)

    # Mesh
    self.line = Qt3DRender.QGeometryRenderer(self)
    self.line.setGeometry(self.geometry)
    self.line.setPrimitiveType(Qt3DRender.QGeometryRenderer.Lines)
    self.material = Qt3DExtras.QPhongMaterial(self)
    self.material.setAmbient(self.color)

    # Entity
    self.addComponent(self.line)
    self.addComponent(self.material)

set_points(start, end) #

Set start and end vertices.

Parameters:

Name Type Description Default
start Vertex

start position

required
end Vertex

end position

required
Source code in cogip/entities/line.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def set_points(self, start: models.Vertex, end: models.Vertex):
    """
    Set start and end vertices.

    Arguments:
        start: start position
        end: end position
    """

    # Position vertices (start and end)
    positions = array("f")
    positions.fromlist([start.x, start.y, start.z])
    positions.fromlist([end.x, end.y, end.z])
    buffer_bytes = QtCore.QByteArray(positions.tobytes())
    self.position_buffer.setData(buffer_bytes)