File size: 6,085 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
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/


#include <algorithm>
#include <cmath>
#include <iostream>


#include <Base/Matrix.h>
#include <Base/Writer.h>

#include "PointsPy.h"
#include "PropertyPointKernel.h"


using namespace Points;

TYPESYSTEM_SOURCE(Points::PropertyPointKernel, App::PropertyComplexGeoData)

PropertyPointKernel::PropertyPointKernel()
    : _cPoints(new PointKernel())
{}

void PropertyPointKernel::setValue(const PointKernel& m)

{
    aboutToSetValue();
    *_cPoints = m;
    hasSetValue();
}

const PointKernel& PropertyPointKernel::getValue() const

{
    return *_cPoints;
}

const Data::ComplexGeoData* PropertyPointKernel::getComplexData() const

{
    return _cPoints;
}

void PropertyPointKernel::setTransform(const Base::Matrix4D& rclTrf)

{
    _cPoints->setTransform(rclTrf);
}

Base::Matrix4D PropertyPointKernel::getTransform() const

{
    return _cPoints->getTransform();
}

Base::BoundBox3d PropertyPointKernel::getBoundingBox() const

{
    return _cPoints->getBoundBox();
}

PyObject* PropertyPointKernel::getPyObject()

{
    PointsPy* points = new PointsPy(&*_cPoints);
    points->setConst();  // set immutable
    return points;
}

void PropertyPointKernel::setPyObject(PyObject* value)

{
    if (PyObject_TypeCheck(value, &(PointsPy::Type))) {
        PointsPy* pcObject = static_cast<PointsPy*>(value);
        setValue(*(pcObject->getPointKernelPtr()));
    }
    else {
        std::string error = std::string("type must be 'Points', not ");
        error += value->ob_type->tp_name;
        throw Base::TypeError(error);
    }
}

void PropertyPointKernel::Save(Base::Writer& writer) const

{
    _cPoints->Save(writer);
}

void PropertyPointKernel::Restore(Base::XMLReader& reader)

{
    reader.readElement("Points");
    std::string file(reader.getAttribute<const char*>("file"));

    if (!file.empty()) {
        // initiate a file read
        reader.addFile(file.c_str(), this);
    }
    if (reader.DocumentSchema > 3) {
        std::string Matrix(reader.getAttribute<const char*>("mtrx"));
        Base::Matrix4D mtrx;
        mtrx.fromString(Matrix);

        aboutToSetValue();
        _cPoints->setTransform(mtrx);
        hasSetValue();
    }
}

void PropertyPointKernel::SaveDocFile(Base::Writer& writer) const

{
    // does nothing
    (void)writer;
}

void PropertyPointKernel::RestoreDocFile(Base::Reader& reader)

{
    aboutToSetValue();
    _cPoints->RestoreDocFile(reader);
    hasSetValue();
}

App::Property* PropertyPointKernel::Copy() const

{
    PropertyPointKernel* prop = new PropertyPointKernel();
    (*prop->_cPoints) = (*this->_cPoints);
    return prop;
}

void PropertyPointKernel::Paste(const App::Property& from)

{
    aboutToSetValue();
    const PropertyPointKernel& prop = dynamic_cast<const PropertyPointKernel&>(from);
    *(this->_cPoints) = *(prop._cPoints);
    hasSetValue();
}

unsigned int PropertyPointKernel::getMemSize() const

{
    return sizeof(Base::Vector3f) * this->_cPoints->size();
}

PointKernel* PropertyPointKernel::startEditing()

{
    aboutToSetValue();
    return static_cast<PointKernel*>(_cPoints);
}

void PropertyPointKernel::finishEditing()

{
    hasSetValue();
}

void PropertyPointKernel::removeIndices(const std::vector<unsigned long>& uIndices)

{
    // We need a sorted array
    std::vector<unsigned long> uSortedInds = uIndices;
    std::sort(uSortedInds.begin(), uSortedInds.end());

    assert(uSortedInds.size() <= _cPoints->size());
    if (uSortedInds.size() > _cPoints->size()) {
        return;
    }

    PointKernel kernel;
    kernel.setTransform(_cPoints->getTransform());
    kernel.reserve(_cPoints->size() - uSortedInds.size());

    std::vector<unsigned long>::iterator pos = uSortedInds.begin();
    unsigned long index = 0;
    for (PointKernel::const_iterator it = _cPoints->begin(); it != _cPoints->end(); ++it, ++index) {
        if (pos == uSortedInds.end()) {
            kernel.push_back(*it);
        }
        else if (index != *pos) {
            kernel.push_back(*it);
        }
        else {
            ++pos;
        }
    }

    setValue(kernel);
}

void PropertyPointKernel::transformGeometry(const Base::Matrix4D& rclMat)

{
    aboutToSetValue();
    _cPoints->transformGeometry(rclMat);
    hasSetValue();
}