Completed
Push — master ( a7a39a...dc869e )
by Grosan
02:39
created

STLSplit::setStl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the STL package.
4
 *
5
 * (c) Grosan Flaviu Gheorghe <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace php3d\stl;
12
13
/**
14
 * Class STLSplit. Extracts separate 3D objects as STL objects.
15
 * @package php3d\stl
16
 */
17
class STLSplit {
18
    /**
19
     * @var STL
20
     */
21
    private $stl;
22
23
    /**
24
     * @return STL
25
     */
26
    public function getStl(): STL
27
    {
28
        return $this->stl;
29
    }
30
31
    /**
32
     * @param STL $stl
33
     * @return STLSplit
34
     */
35
    private function setStl(STL $stl): STLSplit
36
    {
37
        $this->stl = $stl;
38
        return $this;
39
    }
40
41
    /**
42
     * STLSplit constructor.
43
     * @param STL $stl
44
     */
45
    public function __construct(STL $stl)
46
    {
47
        $this->setStl($stl);
48
    }
49
50
    /**
51
     * Splits an STL object into 3, and returns the new 3D objects in an array of STL files.
52
     *
53
     * @return array
54
     */
55
    public function split() : array
56
    {
57
        $stlArray = $this->getStl()->toArray();
58
        $facets = $stlArray["facets"];
59
60
        $objArray = array();
61
        $vertex = $facets[0];
62
        unset($facets[0]);
63
64
        $current = 0;
65
        $objArray[$current] = array($vertex);
66
        while (count($facets) != 0) {
67
            $this->findNeighbour(
68
                $vertex,
69
                $objArray[$current],
70
                $facets
71
            );
72
            $vertex = array_pop($facets);
73
            $current++;
74
            if ($vertex) {
75
                $objArray[$current] = array($vertex);
76
            }
77
        }
78
79
        $stlObjects = array();
80
        foreach ($objArray as $key => $object) {
81
            $stlObjects[] = STL::fromArray(array(
82
                "name" => $this->getStl()->getSolidName() . $key,
83
                "facets" => $object
84
            ));
85
        }
86
87
        return $stlObjects;
88
    }
89
90
    /**
91
     * Finds the neighbouring facets of a facet.
92
     *
93
     * @param array $facet
94
     * @param array $object
95
     * @param array $facets
96
     */
97
    private function findNeighbour(array $facet, array &$object, array &$facets)
98
    {
99
        foreach ($facets as $key => $_facet) {
100
            for ($i = 0; $i < 3; $i++) {
0 ignored issues
show
Unused Code introduced by
$i++; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
101
                if (in_array($_facet["vertex"][$i], $facet["vertex"])) {
102
                    unset($facets[$key]);
103
                    $object[] = $_facet;
104
                    $this->findNeighbour(
105
                        $_facet,
106
                        $object,
107
                        $facets
108
                    );
109
                }
110
                continue 2;
111
            }
112
        }
113
    }
114
}