File size: 13,018 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// SPDX-License-Identifier: LGPL-2.1-or-later

// Tests for the makeShapeWithElementMap method, extracted from the main set of tests for TopoShape
// due to length and complexity.

#include <gtest/gtest.h>
#include "src/App/InitApplication.h"
#include "PartTestHelpers.h"
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/TopoShapeOpCode.h>

#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_CompSolid.hxx>
#include <TopoDS_Compound.hxx>

using namespace Part;
using namespace Data;

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

    void SetUp() override
    {
        _docName = App::GetApplication().getUniqueDocumentName("test");
        App::GetApplication().newDocument(_docName.c_str(), "testUser");
    }

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

    Part::TopoShape* Shape()
    {
        return &_shape;
    }

    Part::TopoShape::Mapper* Mapper()
    {
        return &_mapper;
    }

    void testFindSourceSubShapesInElementMapForSource(
        const std::vector<TopoShape>& sources,
        const TopoShape& source
    );

private:
    std::string _docName;
    Data::ElementIDRefs _sid;
    Part::TopoShape _shape;
    Part::TopoShape::Mapper _mapper;
};

TEST_F(TopoShapeMakeShapeWithElementMapTests, nullShapeThrows)
{
    // Arrange
    auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes();
    std::vector<Part::TopoShape> sources {cube1, cube2};
    TopoDS_Vertex nullVertex;
    TopoDS_Edge nullEdge;
    TopoDS_Wire nullWire;
    TopoDS_Face nullFace;
    TopoDS_Shell nullShell;
    TopoDS_Solid nullSolid;
    TopoDS_CompSolid nullCompSolid;
    TopoDS_Compound nullCompound;

    // Act and assert
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullVertex, *Mapper(), sources),
        Part::NullShapeException
    );
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullEdge, *Mapper(), sources),
        Part::NullShapeException
    );
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullWire, *Mapper(), sources),
        Part::NullShapeException
    );
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullFace, *Mapper(), sources),
        Part::NullShapeException
    );
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullShell, *Mapper(), sources),
        Part::NullShapeException
    );
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullSolid, *Mapper(), sources),
        Part::NullShapeException
    );
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullCompSolid, *Mapper(), sources),
        Part::NullShapeException
    );
    EXPECT_THROW(
        Shape()->makeShapeWithElementMap(nullCompound, *Mapper(), sources),
        Part::NullShapeException
    );
}

std::map<IndexedName, MappedName> elementMap(const TopoShape& shape)
{
    std::map<IndexedName, MappedName> result {};
    auto elements = shape.getElementMap();
    for (auto const& entry : elements) {
        result[entry.index] = entry.name;
    }
    return result;
}

// TEST_F(TopoShapeMakeShapeWithElementMapTests, mapVertex)
// TEST_F(TopoShapeMakeShapeWithElementMapTests, mapEdge)
// TEST_F(TopoShapeMakeShapeWithElementMapTests, mapWire)
// TEST_F(TopoShapeMakeShapeWithElementMapTests, mapFace)
// TEST_F(TopoShapeMakeShapeWithElementMapTests, mapShell)
// TEST_F(TopoShapeMakeShapeWithElementMapTests, mapSolid)

TEST_F(TopoShapeMakeShapeWithElementMapTests, mapCompoundCount)
{
    // Arrange
    auto [cube1TS, cube2TS] = PartTestHelpers::CreateTwoTopoShapeCubes();
    std::vector<TopoShape> sources {cube1TS, cube2TS};
    TopoShape compound {3L};
    compound.makeElementCompound(sources);
    auto preElements = elementMap(compound);  // Map before mapping.
    // Act
    compound.makeShapeWithElementMap(compound.getShape(), *Mapper(), sources);
    auto postElements = elementMap(compound);  // Map after mapping
    // Assert
    EXPECT_EQ(preElements.size(), 52);   // Check the before map.
    EXPECT_EQ(postElements.size(), 52);  // 12 Edges, 8 Vertexes, 6 Faces per each Cube
    EXPECT_EQ(postElements.count(IndexedName("Edge", 24)), 1);
    EXPECT_EQ(postElements.count(IndexedName("Edge", 25)), 0);
    EXPECT_EQ(postElements.count(IndexedName("Vertex", 16)), 1);
    EXPECT_EQ(postElements.count(IndexedName("Vertex", 17)), 0);
    EXPECT_EQ(postElements.count(IndexedName("Face", 12)), 1);
    EXPECT_EQ(postElements.count(IndexedName("Face", 13)), 0);
    EXPECT_STREQ(sources[0].shapeName().c_str(), "Compound");
    EXPECT_STREQ(sources[1].shapeName().c_str(), "Compound");
    EXPECT_STREQ(compound.shapeName().c_str(), "Compound");
    EXPECT_EQ(
        22,
        compound.getMappedChildElements().size()
    );  // Changed with PR#12471. Probably will change
        // again after importing other TopoNaming logics
}

TEST_F(TopoShapeMakeShapeWithElementMapTests, emptySourceShapes)
{
    // Arrange
    auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes();
    std::vector<Part::TopoShape> emptySources;
    std::vector<Part::TopoShape> nonEmptySources {cube1, cube2};

    // Act and assert
    for (auto& source : nonEmptySources) {
        Part::TopoShape& modifiedShape = source;
        Part::TopoShape& originalShape = source;

        EXPECT_EQ(
            &originalShape,
            &modifiedShape.makeShapeWithElementMap(source.getShape(), *Mapper(), emptySources)
        );
    }
}

TEST_F(TopoShapeMakeShapeWithElementMapTests, nonMappableSources)
{
    // Arrange
    auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes();
    std::vector<Part::TopoShape> sources {cube1, cube2};

    // Act and assert
    for (auto& source : sources) {
        size_t canMap = 0;
        for (const auto& mappableSource : sources) {
            if (source.canMapElement(mappableSource)) {
                ++canMap;
            }
        }

        if (canMap == 0U) {
            EXPECT_EQ(&source, &source.makeShapeWithElementMap(source.getShape(), *Mapper(), sources));
        }
    }
}

void testFindSourceShapesInSingleShape(
    const Part::TopoShape& cmpdShape,
    const Part::TopoShape& source,
    const std::vector<Part::TopoShape>& sources,
    const TopoShape::Mapper& mapper
)
{
    std::vector<Part::TopoShape> tmpSources {source};
    for (const auto& subSource : sources) {
        Part::TopoShape tmpShape {source.getShape()};
        tmpShape.makeShapeWithElementMap(source.getShape(), mapper, tmpSources);
        if (&source == &subSource) {
            EXPECT_NE(
                tmpShape.findShape(subSource.getShape()),
                0
            );  // if tmpShape uses, for example, cube1 and we search for cube1 than
                // we should find it
        }
        else {
            EXPECT_EQ(
                tmpShape.findShape(subSource.getShape()),
                0
            );  // if tmpShape uses, for example, cube1 and we search for cube2 than
                // we shouldn't find it
        }
    }
    EXPECT_NE(
        cmpdShape.findShape(source.getShape()),
        0
    );  // as cmpdShape is made with cube1 and cube2 we should find both of them
}

TEST_F(TopoShapeMakeShapeWithElementMapTests, findSourceShapesInShape)
{
    // Arrange
    auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes();
    std::vector<Part::TopoShape> sources {cube1, cube2};
    sources[0].Tag = 1;  // setting Tag explicitly otherwise it is likely that this test will be
                         // more or less the same of nonMappableSources
    sources[1].Tag = 2;  // setting Tag explicitly otherwise it is likely that this test will be
                         // more or less the same of nonMappableSources
    Part::TopoShape cmpdShape;
    cmpdShape.makeElementCompound(sources);

    // Act and assert
    for (const auto& source : sources) {
        testFindSourceShapesInSingleShape(cmpdShape, source, sources, *Mapper());
    }
}

void testFindSubShapesForSourceWithTypeAndIndex(
    const std::string& shapeTypeStr,
    std::map<IndexedName, MappedName>& elementStdMap,
    unsigned long shapeIndex
)
{
    std::string shapeIndexStr = std::to_string(shapeIndex);
    std::string shapeName {shapeTypeStr + shapeIndexStr};

    IndexedName indexedName {shapeTypeStr.c_str(), (int)shapeIndex};
    MappedName mappedName {elementStdMap[indexedName]};
    const char shapeTypePrefix {indexedName.toString()[0]};

    QT_WARNING_PUSH
    QT_WARNING_DISABLE_MSVC(4834)  // Discarding a [[nodiscard]], which we are about to do...
    // We check that the IndexedName is one of the keys...
    EXPECT_NO_THROW(elementStdMap.at(indexedName));
    // ... that the element name is in the MappedName...
    EXPECT_NE(mappedName.find(shapeName.c_str()), -1);
    EXPECT_EQ(mappedName.toString().back(), shapeTypePrefix);
    QT_WARNING_POP
}

void testFindSubShapesForSourceWithType(
    const TopoShape& source,
    const char* shapeType,
    std::map<IndexedName, MappedName>& elementStdMap
)
{
    std::string shapeTypeStr {shapeType};

    // ... and all the elements of the various types in the source TopoShape ...
    for (unsigned long shapeIndex = 1U; shapeIndex <= source.countSubElements(shapeType);
         shapeIndex++) {
        testFindSubShapesForSourceWithTypeAndIndex(shapeTypeStr, elementStdMap, shapeIndex);
    }

    QT_WARNING_PUSH
    QT_WARNING_DISABLE_MSVC(4834)  // Discarding a [[nodiscard]], which we are about to do...
    // ... we also check that we don't find shapes that don't exist and therefore don't
    // have either an IndexedName or a MappedName
    IndexedName fakeIndexedName {shapeTypeStr.c_str(), (int)source.countSubElements(shapeType) + 1};
    EXPECT_THROW(elementStdMap.at(fakeIndexedName), std::out_of_range);
    QT_WARNING_POP
}

void TopoShapeMakeShapeWithElementMapTests::testFindSourceSubShapesInElementMapForSource(
    const std::vector<TopoShape>& sources,
    const TopoShape& source
)
{
    TopoShape tmpShape {source.getShape()};
    tmpShape.makeShapeWithElementMap(source.getShape(), *Mapper(), sources);

    // First we create a map with the IndexedNames and MappedNames
    std::map<IndexedName, MappedName> elementStdMap;
    for (const auto& mappedElement : tmpShape.getElementMap()) {
        elementStdMap.emplace(mappedElement.index, mappedElement.name);
    }

    // Then for all the elements types (Vertex, Edge, Face) ...
    for (const auto& shapeType : source.getElementTypes()) {
        testFindSubShapesForSourceWithType(source, shapeType, elementStdMap);
    }
}

TEST_F(TopoShapeMakeShapeWithElementMapTests, findSourceSubShapesInElementMap)
{
    // Arrange
    auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes();
    std::vector<TopoShape> sources {cube1, cube2};
    sources[0].Tag = 1;  // setting Tag explicitly otherwise it is likely that this test will be
                         // more or less the same of nonMappableSources
    sources[1].Tag = 2;  // setting Tag explicitly otherwise it is likely that this test will be
                         // more or less the same of nonMappableSources

    // Act and assert
    // Testing with all the source TopoShapes
    for (const auto& source : sources) {
        testFindSourceSubShapesInElementMapForSource(sources, source);
    }
}

TEST_F(TopoShapeMakeShapeWithElementMapTests, findMakerOpInElementMap)
{
    // Arrange
    auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes();
    std::vector<TopoShape> sources {cube1, cube2};
    sources[0].Tag = 1;  // setting Tag explicitly otherwise it is likely that this test will be
                         // more or less the same of nonMappableSources
    sources[1].Tag = 2;  // setting Tag explicitly otherwise it is likely that this test will be
                         // more or less the same of nonMappableSources

    // Act and assert
    // Testing with all the source TopoShapes
    for (const auto& source : sources) {
        TopoShape tmpShape {source.getShape()};
        tmpShape.makeShapeWithElementMap(source.getShape(), *Mapper(), sources);
        EXPECT_EQ(tmpShape.getElementMapSize(), 26);

        // For all the mappedElements ...
        // for (const auto& mappedElement : tmpShape.getElementMap()) {
        // TODO:  This no longer works, it needs a different check.  We don't set MAK
        //            EXPECT_NE(mappedElement.name.find(OpCodes::Maker),
        //                      -1);  // ... we check that there's the "MAK" OpCode
        //}
    }
}

std::string composeTagInfo(const MappedElement& element, const TopoShape& shape)
{
    std::string elementNameStr {element.name.constPostfix()};
    std::string tagInfo = POSTFIX_TAG + std::to_string(shape.Tag);
    tagInfo += ":" + std::to_string(elementNameStr.substr(0, elementNameStr.find(tagInfo)).length());

    return tagInfo;
}