Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestConvexHullDecorator.py « tests - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4205ae3a372d718d7eb8feab98b856748b958c4e (plain)
1
2
3
4
5
6
7
8
9
10
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import copy
from unittest.mock import patch, MagicMock

import pytest

from UM.Math.Polygon import Polygon
from UM.Mesh.MeshBuilder import MeshBuilder
from UM.Scene.GroupDecorator import GroupDecorator
from UM.Scene.SceneNode import SceneNode
from UM.Scene.SceneNodeDecorator import SceneNodeDecorator
from cura.Scene.ConvexHullDecorator import ConvexHullDecorator

mocked_application = MagicMock()
mocked_controller = MagicMock()
# We need to mock out this function, otherwise we get a recursion
mocked_controller.isToolOperationActive = MagicMock(return_value = False)
mocked_application.getController = MagicMock(return_value = mocked_controller)


class NonPrintingDecorator(SceneNodeDecorator):
    def isNonPrintingMesh(self):
        return True


class PrintingDecorator(SceneNodeDecorator):
    def isNonPrintingMesh(self):
        return False


@pytest.fixture
def convex_hull_decorator():
    with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = mocked_application)):
        with patch("UM.Application.Application.getInstance", MagicMock(return_value = mocked_application)):
            with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
                return ConvexHullDecorator()


def test_getSetNode(convex_hull_decorator):
    node = SceneNode()
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)
    assert convex_hull_decorator.getNode() == node


def test_getConvexHullBoundaryNoNode(convex_hull_decorator):
    assert convex_hull_decorator.getConvexHullBoundary() is None


def test_getConvexHullHeadNoNode(convex_hull_decorator):
    assert convex_hull_decorator.getConvexHullHead() is None


def test_getConvexHullHeadNotPrintingMesh(convex_hull_decorator):
    node = SceneNode()
    node.addDecorator(NonPrintingDecorator())
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)
    assert convex_hull_decorator.getConvexHullHead() is None


def test_getConvexHullNoNode(convex_hull_decorator):
    assert convex_hull_decorator.getConvexHull() is None


def test_getConvexHeadFullNoNode(convex_hull_decorator):
    assert convex_hull_decorator.getConvexHullHeadFull() is None


def test_getConvexHullNotPrintingMesh(convex_hull_decorator):
    node = SceneNode()
    node.addDecorator(NonPrintingDecorator())
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)
    assert convex_hull_decorator.getConvexHull() is None


def test_getConvexHullPrintingMesh(convex_hull_decorator):
    node = SceneNode()
    node.addDecorator(PrintingDecorator())
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)
    convex_hull_decorator._compute2DConvexHull = MagicMock(return_value = Polygon.approximatedCircle(10))
    assert convex_hull_decorator.getConvexHull() == Polygon.approximatedCircle(10)

def test_getConvexHullBoundaryNotPrintingMesh(convex_hull_decorator):
    node = SceneNode()
    node.addDecorator(NonPrintingDecorator())
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)
    assert convex_hull_decorator.getConvexHullBoundary() is None


def test_getConvexHulLBoundaryPrintingMesh(convex_hull_decorator):
    node = SceneNode()
    node.addDecorator(PrintingDecorator())
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)
    # Should still be None, since print sequence is not one at a time
    assert convex_hull_decorator.getConvexHullBoundary() is None


def test_getConvexHulLBoundaryPrintingMeshOneAtATime(convex_hull_decorator):
    node = SceneNode()
    node.addDecorator(PrintingDecorator())
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)
    convex_hull_decorator._global_stack = MagicMock()
    convex_hull_decorator._global_stack.getProperty = MagicMock(return_value = "one_at_a_time")
    # In this test we don't care for the result of the function, just that the convex hull computation is called.
    convex_hull_decorator._compute2DConvexHull = MagicMock()
    convex_hull_decorator.getConvexHullBoundary()
    convex_hull_decorator._compute2DConvexHull.assert_called_once_with()


def value_changed(convex_hull_decorator, key):
    convex_hull_decorator._onChanged = MagicMock()
    convex_hull_decorator._onSettingValueChanged(key, "value")
    convex_hull_decorator._onChanged.assert_called_once_with()

    # This should have no effect at all
    convex_hull_decorator._onSettingValueChanged(key, "not value")
    convex_hull_decorator._onChanged.assert_called_once_with()


@pytest.mark.parametrize("key", ConvexHullDecorator._affected_settings)
def test_onSettingValueChangedAffectedSettings(convex_hull_decorator, key):
    value_changed(convex_hull_decorator, key)


@pytest.mark.parametrize("key", ConvexHullDecorator._influencing_settings)
def test_onSettingValueChangedInfluencingSettings(convex_hull_decorator, key):
    convex_hull_decorator._init2DConvexHullCache = MagicMock()
    value_changed(convex_hull_decorator, key)
    convex_hull_decorator._init2DConvexHullCache.assert_called_once_with()


def test_compute2DConvexHullNoNode(convex_hull_decorator):
    assert convex_hull_decorator._compute2DConvexHull() is None


def test_compute2DConvexHullNoMeshData(convex_hull_decorator):
    node = SceneNode()
    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)

    assert convex_hull_decorator._compute2DConvexHull() == Polygon([])


def test_compute2DConvexHullMeshData(convex_hull_decorator):
    node = SceneNode()
    mb = MeshBuilder()
    mb.addCube(10,10,10)
    node.setMeshData(mb.build())

    convex_hull_decorator._getSettingProperty = MagicMock(return_value = 0)

    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(node)

    assert convex_hull_decorator._compute2DConvexHull() == Polygon([[5.0,-5.0], [-5.0,-5.0], [-5.0,5.0], [5.0,5.0]])


def test_compute2DConvexHullMeshDataGrouped(convex_hull_decorator):
    parent_node = SceneNode()
    parent_node.addDecorator(GroupDecorator())
    node = SceneNode()
    parent_node.addChild(node)

    mb = MeshBuilder()
    mb.addCube(10, 10, 10)
    node.setMeshData(mb.build())

    convex_hull_decorator._getSettingProperty = MagicMock(return_value=0)

    with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
        convex_hull_decorator.setNode(parent_node)
        with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
            copied_decorator = copy.deepcopy(convex_hull_decorator)
            copied_decorator._getSettingProperty = MagicMock(return_value=0)
        node.addDecorator(copied_decorator)
    assert convex_hull_decorator._compute2DConvexHull() == Polygon([[-5.0,5.0], [5.0,5.0], [5.0,-5.0], [-5.0,-5.0]])