File size: 11,792 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// SPDX-License-Identifier: LGPL-2.1-or-later

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

 *   Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de>              *

 *                                                                         *

 *   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 <QtConcurrentMap>
#include <boost/math/special_functions/fpclassify.hpp>
#include <cmath>
#include <iostream>


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

#include "Points.h"
#include "PointsAlgos.h"


#ifdef _MSC_VER
# include <ppl.h>
#endif

using namespace Points;
using namespace std;

TYPESYSTEM_SOURCE(Points::PointKernel, Data::ComplexGeoData)

PointKernel::PointKernel(const PointKernel& pts)
    : _Mtrx(pts._Mtrx)
    , _Points(pts._Points)
{}

PointKernel::PointKernel(PointKernel&& pts) noexcept
    : _Mtrx(pts._Mtrx)
    , _Points(std::move(pts._Points))
{}

std::vector<const char*> PointKernel::getElementTypes() const

{
    std::vector<const char*> temp;
    // temp.push_back("Segment");

    return temp;
}

unsigned long PointKernel::countSubElements(const char* /*Type*/) const

{
    return 0;
}

Data::Segment* PointKernel::getSubElement(const char* /*Type*/, unsigned long /*n*/) const

{
    // unsigned long i = 1;

    // if (strcmp(Type,"Segment") == 0) {
    //     // not implemented
    //     assert(0);
    //     return 0;
    // }

    return nullptr;
}

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

{
    std::vector<value_type>& kernel = getBasicPoints();
#ifdef _MSC_VER
    // Win32-only at the moment since ppl.h is a Microsoft library. Points is not using Qt so we
    // cannot use QtConcurrent We could also rewrite Points to leverage SIMD instructions Other
    // option: openMP. But with VC2013 results in high CPU usage even after computation (busy-waits
    // for >100ms)
    Concurrency::parallel_for_each(kernel.begin(), kernel.end(), [rclMat](value_type& value) {
        value = rclMat * value;
    });
#else
    QtConcurrent::blockingMap(kernel, [rclMat](value_type& value) { rclMat.multVec(value, value); });
#endif
}

void PointKernel::moveGeometry(const Base::Vector3d& vec)

{
    Base::Vector3f offset = Base::toVector<float>(vec);
    std::vector<value_type>& kernel = getBasicPoints();
#ifdef _MSC_VER
    Concurrency::parallel_for_each(kernel.begin(), kernel.end(), [offset](value_type& value) {
        value += offset;
    });
#else
    QtConcurrent::blockingMap(kernel, [offset](value_type& value) { value += offset; });
#endif
}

Base::BoundBox3d PointKernel::getBoundBox() const

{
    Base::BoundBox3d bnd;

#ifdef _MSC_VER
    // Thread-local bounding boxes
    Concurrency::combinable<Base::BoundBox3d> bbs;
    // Cannot use a const_point_iterator here as it is *not* a proper iterator (fails the for_each
    // template)
    Concurrency::parallel_for_each(_Points.begin(), _Points.end(), [this, &bbs](const value_type& value) {
        Base::Vector3d vertd(value.x, value.y, value.z);
        bbs.local().Add(this->_Mtrx * vertd);
    });
    // Combine each thread-local bounding box in the final bounding box
    bbs.combine_each([&bnd](const Base::BoundBox3d& lbb) { bnd.Add(lbb); });
#else
    for (const auto& it : *this) {
        bnd.Add(it);
    }
#endif
    return bnd;
}

PointKernel& PointKernel::operator=(const PointKernel& Kernel)
{
    if (this != &Kernel) {
        // copy the mesh structure
        setTransform(Kernel._Mtrx);
        this->_Points = Kernel._Points;
    }

    return *this;
}

PointKernel& PointKernel::operator=(PointKernel&& Kernel) noexcept
{
    if (this != &Kernel) {
        // copy the mesh structure
        setTransform(Kernel._Mtrx);
        this->_Points = std::move(Kernel._Points);
    }

    return *this;
}

unsigned int PointKernel::getMemSize() const

{
    return _Points.size() * sizeof(value_type);
}

PointKernel::size_type PointKernel::countValid() const

{
    size_type num = 0;
    for (const auto& it : *this) {
        if (!(boost::math::isnan(it.x) || boost::math::isnan(it.y) || boost::math::isnan(it.z))) {
            num++;
        }
    }
    return num;
}

std::vector<PointKernel::value_type> PointKernel::getValidPoints() const

{
    std::vector<PointKernel::value_type> valid;
    valid.reserve(countValid());
    for (const auto& it : *this) {
        if (!(boost::math::isnan(it.x) || boost::math::isnan(it.y) || boost::math::isnan(it.z))) {
            valid.emplace_back(
                static_cast<float_type>(it.x),
                static_cast<float_type>(it.y),
                static_cast<float_type>(it.z)
            );
        }
    }
    return valid;
}

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

{
    if (!writer.isForceXML()) {
        writer.Stream() << writer.ind() << "<Points file=\""
                        << writer.addFile(writer.ObjectName.c_str(), this) << "\" "
                        << "mtrx=\"" << _Mtrx.toString() << "\"/>" << std::endl;
    }
}

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

{
    Base::OutputStream str(writer.Stream());
    uint32_t uCt = (uint32_t)size();
    str << uCt;
    // store the data without transforming it
    for (const auto& pnt : _Points) {
        str << pnt.x << pnt.y << pnt.z;
    }
}

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

{
    clear();

    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"));
        _Mtrx.fromString(Matrix);
    }
}

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

{
    Base::InputStream str(reader);
    uint32_t uCt = 0;
    str >> uCt;
    _Points.resize(uCt);
    for (unsigned long i = 0; i < uCt; i++) {
        float x {};
        float y {};
        float z {};
        str >> x >> y >> z;
        _Points[i].Set(x, y, z);
    }
}

void PointKernel::save(const char* file) const

{
    Base::ofstream out(Base::FileInfo(file), std::ios::out);
    save(out);
}

void PointKernel::load(const char* file)

{
    PointsAlgos::Load(*this, file);
}

void PointKernel::save(std::ostream& out) const

{
    out << "# ASCII" << std::endl;
    for (const auto& pnt : _Points) {
        out << pnt.x << " " << pnt.y << " " << pnt.z << std::endl;
    }
}

void PointKernel::getPoints(

    std::vector<Base::Vector3d>& Points,

    std::vector<Base::Vector3d>& /*Normals*/,

    double /*Accuracy*/,

    uint16_t /*flags*/

) const

{
    unsigned long ctpoints = _Points.size();
    Points.reserve(ctpoints);
    for (unsigned long i = 0; i < ctpoints; i++) {
        Points.push_back(this->getPoint(i));
    }
}

// ----------------------------------------------------------------------------

PointKernel::const_point_iterator::const_point_iterator(
    const PointKernel* kernel,
    std::vector<kernel_type>::const_iterator index
)
    : _kernel(kernel)
    , _p_it(index)
{
    if (_p_it != kernel->_Points.end()) {
        value_type vertd(_p_it->x, _p_it->y, _p_it->z);
        this->_point = _kernel->_Mtrx * vertd;
    }
}

PointKernel::const_point_iterator::const_point_iterator(
    const PointKernel::const_point_iterator& fi
) = default;

PointKernel::const_point_iterator::const_point_iterator(
    PointKernel::const_point_iterator&& fi
) = default;

PointKernel::const_point_iterator::~const_point_iterator() = default;

PointKernel::const_point_iterator& PointKernel::const_point_iterator::operator=(
    const PointKernel::const_point_iterator& pi
) = default;

PointKernel::const_point_iterator& PointKernel::const_point_iterator::operator=(
    PointKernel::const_point_iterator&& pi
) = default;

void PointKernel::const_point_iterator::dereference()
{
    value_type vertd(_p_it->x, _p_it->y, _p_it->z);
    this->_point = _kernel->_Mtrx * vertd;
}

const PointKernel::const_point_iterator::value_type& PointKernel::const_point_iterator::operator*()
{
    dereference();
    return this->_point;
}

const PointKernel::const_point_iterator::value_type* PointKernel::const_point_iterator::operator->()
{
    dereference();
    return &(this->_point);
}

bool PointKernel::const_point_iterator::operator==(const PointKernel::const_point_iterator& pi) const
{
    return (this->_kernel == pi._kernel) && (this->_p_it == pi._p_it);
}

bool PointKernel::const_point_iterator::operator!=(const PointKernel::const_point_iterator& pi) const
{
    return !operator==(pi);
}

PointKernel::const_point_iterator& PointKernel::const_point_iterator::operator++()
{
    ++(this->_p_it);
    return *this;
}

PointKernel::const_point_iterator PointKernel::const_point_iterator::operator++(int)
{
    PointKernel::const_point_iterator tmp = *this;
    ++(this->_p_it);
    return tmp;
}

PointKernel::const_point_iterator& PointKernel::const_point_iterator::operator--()
{
    --(this->_p_it);
    return *this;
}

PointKernel::const_point_iterator PointKernel::const_point_iterator::operator--(int)
{
    PointKernel::const_point_iterator tmp = *this;
    --(this->_p_it);
    return tmp;
}

PointKernel::const_point_iterator PointKernel::const_point_iterator::operator+(difference_type off) const
{
    PointKernel::const_point_iterator tmp = *this;
    return (tmp += off);
}

PointKernel::const_point_iterator PointKernel::const_point_iterator::operator-(difference_type off) const
{
    PointKernel::const_point_iterator tmp = *this;
    return (tmp -= off);
}

PointKernel::const_point_iterator& PointKernel::const_point_iterator::operator+=(difference_type off)
{
    (this->_p_it) += off;
    return *this;
}

PointKernel::const_point_iterator& PointKernel::const_point_iterator::operator-=(difference_type off)
{
    (this->_p_it) -= off;
    return *this;
}

PointKernel::difference_type PointKernel::const_point_iterator::operator-(
    const PointKernel::const_point_iterator& right
) const
{
    return this->_p_it - right._p_it;
}