File size: 8,361 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: LGPL-2.1-or-later

#include <gtest/gtest.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/TopoShapeCache.h>

#include <src/App/InitApplication.h>
#include <gp_Quaternion.hxx>
#include <TopoDS_TVertex.hxx>
#include <BRep_TVertex.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <TopoDS_Edge.hxx>

// NOLINTBEGIN(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers)

class ShapeRelationKey: public ::testing::Test
{
protected:
    static void SetUpTestSuite()
    {
        tests::initApplication();
    }
};

TEST_F(ShapeRelationKey, HistoryTraceTypeComparison)
{
    // Arrange
    Data::MappedName mappedName {"mappedName"};
    Part::HistoryTraceType htt1 {Part::HistoryTraceType::stopOnTypeChange};
    Part::HistoryTraceType htt2 {Part::HistoryTraceType::followTypeChange};
    Part::ShapeRelationKey key1 {mappedName, htt1};
    Part::ShapeRelationKey key2 {mappedName, htt2};

    // Act
    bool key1LessThanKey2 = key1 < key2;

    // Assert
    ASSERT_TRUE(key1LessThanKey2);
}

class TopoShapeCacheTest: public ::testing::Test
{
protected:
    static void SetUpTestSuite()
    {
        tests::initApplication();
    }

    void SetUp() override
    {
        _docName = App::GetApplication().getUniqueDocumentName("test");
        App::GetApplication().newDocument(_docName.c_str(), "testUser");
        _sids = &_sid;
        _hasher = Base::Reference<App::StringHasher>(new App::StringHasher);
        ASSERT_EQ(_hasher.getRefCount(), 1);
    }

    void TearDown() override
    {
        App::GetApplication().closeDocument(_docName.c_str());
    }

private:
    std::string _docName;
    Data::ElementIDRefs _sid;
    QVector<App::StringIDRef>* _sids = nullptr;
    App::StringHasherRef _hasher;
};

TEST_F(TopoShapeCacheTest, ConstructionFromTopoDS_Shape)
{
    // Arrange - create a TopoDS shape with some location transformation applied
    TopoDS_Vertex vertex;
    gp_Quaternion quaternion(1.0, 2.0, 3.0, 4.0);
    gp_Trsf transform;
    transform.SetRotation(quaternion);
    auto location = TopLoc_Location(transform);
    vertex.Location(location);

    // Act
    auto cache = Part::TopoShapeCache(vertex);

    // Assert - ensure the location of the cached shape was zeroed out
    EXPECT_NE(cache.shape.Location(), vertex.Location());
}

TEST_F(TopoShapeCacheTest, InsertRelationIntoEmptyTableCompacts)
{
    // Arrange
    Data::IndexedName indexedName {"EDGE1"};
    auto mappedName = Data::MappedName::fromRawData("#94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F");
    ASSERT_TRUE(mappedName.isRaw());
    Data::MappedElement mappedElement1 {indexedName, mappedName};
    QVector<Data::MappedElement> vectorOfElements {mappedElement1};
    TopoDS_Vertex vertex;
    Part::TopoShapeCache cache(vertex);
    Part::ShapeRelationKey key {mappedName, Part::HistoryTraceType::followTypeChange};

    // Act
    cache.insertRelation(key, vectorOfElements);

    // Assert
    auto foundIterator = cache.relations.find(key);
    EXPECT_NE(foundIterator, cache.relations.end());
    EXPECT_FALSE(foundIterator->first.name.isRaw());  // compact() was called
}

TEST_F(TopoShapeCacheTest, InsertAlreadyExistsUpdatesExisting)
{
    // Arrange
    Data::IndexedName indexedName {"EDGE1"};
    Data::MappedName mappedName("#94;:G0;XTR;:H19:8,F;:H1a,F;BND:-1:0;:H1b:10,F");
    Data::MappedElement mappedElement1 {indexedName, mappedName};
    QVector<Data::MappedElement> vectorOfElements {mappedElement1};
    TopoDS_Vertex vertex;
    Part::TopoShapeCache cache(vertex);
    Part::ShapeRelationKey key {mappedName, Part::HistoryTraceType::followTypeChange};

    // Act
    cache.insertRelation(key, vectorOfElements);
    QVector<Data::MappedElement> emptyVector;
    cache.insertRelation(key, emptyVector);

    // Assert
    EXPECT_TRUE(cache.relations.find(key)->second.empty());
}

TEST_F(TopoShapeCacheTest, IsTouchedNotPartners)
{
    // Arrange
    BRep_TVertex* vertex1 = new BRep_TVertex;
    vertex1->Pnt(gp_Pnt(1.0, 1.0, 1.0));
    BRep_TVertex* vertex2 = new BRep_TVertex;
    vertex2->Pnt(gp_Pnt(2.0, 2.0, 2.0));
    opencascade::handle<TopoDS_TShape> handle1(vertex1);
    opencascade::handle<TopoDS_TShape> handle2(vertex2);
    TopoDS_Vertex tds1;
    TopoDS_Vertex tds2;
    tds1.TShape(handle1);
    tds2.TShape(handle2);
    ASSERT_FALSE(tds1.IsPartner(tds2));
    Part::TopoShapeCache cache(tds1);

    // Act & Assert
    EXPECT_TRUE(cache.isTouched(tds2));
}

TEST_F(TopoShapeCacheTest, IsTouchedArePartners)
{
    // Arrange
    BRep_TVertex* vertex1 = new BRep_TVertex;
    vertex1->Pnt(gp_Pnt(1.0, 1.0, 1.0));
    opencascade::handle<TopoDS_TShape> handle1(vertex1);
    TopoDS_Vertex tds1;
    TopoDS_Vertex tds2;
    tds1.TShape(handle1);
    tds2.TShape(handle1);
    ASSERT_TRUE(tds1.IsPartner(tds2));
    Part::TopoShapeCache cache(tds1);

    // Act & Assert
    EXPECT_FALSE(cache.isTouched(tds2));
}

std::tuple<TopoDS_Shape, std::pair<TopoDS_Shape, TopoDS_Shape>> CreateShapeWithSubshapes()
{
    auto edge1 = BRepBuilderAPI_MakeEdge(gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(1.0, 0.0, 0.0)).Edge();
    auto edge2 = BRepBuilderAPI_MakeEdge(gp_Pnt(1.0, 0.0, 0.0), gp_Pnt(2.0, 0.0, 0.0)).Edge();
    auto fuse = BRepAlgoAPI_Fuse(edge1, edge2);
    fuse.Build();
    return {fuse.Shape(), {edge1, edge2}};
}

TEST_F(TopoShapeCacheTest, GetAncestrySHAPE)
{
    // Arrange
    auto shape = std::get<0>(CreateShapeWithSubshapes());
    Part::TopoShapeCache cache(shape);

    // Act
    auto ancestry = cache.getAncestry(TopAbs_SHAPE);

    // Assert
    EXPECT_EQ(2, ancestry.count());
}

TEST_F(TopoShapeCacheTest, GetAncestryEDGE)
{
    // Arrange
    auto shape = std::get<0>(CreateShapeWithSubshapes());
    Part::TopoShapeCache cache(shape);

    // Act
    auto ancestry = cache.getAncestry(TopAbs_EDGE);

    // Assert
    EXPECT_EQ(2, ancestry.count());
}

TEST_F(TopoShapeCacheTest, GetAncestryFACE)
{
    // Arrange
    auto shape = std::get<0>(CreateShapeWithSubshapes());
    Part::TopoShapeCache cache(shape);

    // Act
    auto ancestry = cache.getAncestry(TopAbs_FACE);

    // Assert
    EXPECT_EQ(0, ancestry.count());
}

TEST_F(TopoShapeCacheTest, CountShape)
{
    // Arrange
    auto shape = std::get<0>(CreateShapeWithSubshapes());
    Part::TopoShapeCache cache(shape);

    // Act
    int countOfEdges = cache.countShape(TopAbs_EDGE);
    int countOfFaces = cache.countShape(TopAbs_FACE);
    int countOfShapes = cache.countShape(TopAbs_SHAPE);

    // Assert
    EXPECT_EQ(2, countOfEdges);
    EXPECT_EQ(0, countOfFaces);
    EXPECT_EQ(2, countOfShapes);
}

TEST_F(TopoShapeCacheTest, FindShapeGivenSubshape)
{
    // Arrange
    const auto [shape, ancestors] = CreateShapeWithSubshapes();
    Part::TopoShapeCache cache(shape);

    // Act
    auto shapeResult1 = cache.findShape(ancestors.first, shape);
    auto shapeResult2 = cache.findShape(ancestors.second, shape);

    // Assert
    EXPECT_NE(0, shapeResult1);
    EXPECT_NE(0, shapeResult2);
}

TEST_F(TopoShapeCacheTest, FindShapeGivenTypeAndIndex)
{
    // Arrange
    const auto [shape, ancestors] = CreateShapeWithSubshapes();
    Part::TopoShapeCache cache(shape);

    // Act
    auto shapeResult = cache.findShape(ancestors.first, TopAbs_EDGE, 1);  // NOT zero-indexed!

    // Assert
    EXPECT_FALSE(shapeResult.IsNull());
}

std::tuple<TopoDS_Shape, std::pair<TopoDS_Shape, TopoDS_Shape>> CreateFusedCubes()
{
    auto boxMaker1 = BRepPrimAPI_MakeBox(1.0, 1.0, 1.0);
    boxMaker1.Build();
    auto box1 = boxMaker1.Shape();

    auto boxMaker2 = BRepPrimAPI_MakeBox(1.0, 1.0, 1.0);
    boxMaker2.Build();
    auto box2 = boxMaker2.Shape();
    auto transform = gp_Trsf();
    transform.SetTranslation(gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(1.0, 0.0, 0.0));
    box2.Location(TopLoc_Location(transform));

    auto fuse = BRepAlgoAPI_Fuse(box1, box2);
    fuse.Build();

    return {fuse, {box1, box2}};
}

TEST_F(TopoShapeCacheTest, FindAncestor)
{
    // Arrange
    const auto [shape, ancestors] = CreateFusedCubes();
    Part::TopoShapeCache cache(shape);

    // Act
    auto ancestorResultCompound = cache.findAncestor(ancestors.first, shape, TopAbs_COMPOUND);

    // Assert
    EXPECT_FALSE(ancestorResultCompound.IsNull());
}

// NOLINTEND(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers)