PropertyPointKernel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <cmath>
00028 # include <iostream>
00029 # include <algorithm>
00030 #endif
00031 
00032 #include <Base/Exception.h>
00033 #include <Base/Matrix.h>
00034 #include <Base/Stream.h>
00035 #include <Base/Writer.h>
00036 
00037 #include "PropertyPointKernel.h"
00038 #include "PointsPy.h"
00039 
00040 using namespace Points;
00041 
00042 TYPESYSTEM_SOURCE(Points::PropertyPointKernel , App::PropertyComplexGeoData);
00043 
00044 PropertyPointKernel::PropertyPointKernel()
00045     : _cPoints(new PointKernel())
00046 {
00047 
00048 }
00049 
00050 PropertyPointKernel::~PropertyPointKernel()
00051 {
00052 }
00053 
00054 void PropertyPointKernel::setValue(const PointKernel& m)
00055 {
00056     aboutToSetValue();
00057     *_cPoints = m;
00058     hasSetValue();
00059 }
00060 
00061 const PointKernel& PropertyPointKernel::getValue(void) const 
00062 {
00063     return *_cPoints;
00064 }
00065 
00066 const Data::ComplexGeoData* PropertyPointKernel::getComplexData() const
00067 {
00068     return _cPoints;
00069 }
00070 
00071 Base::BoundBox3d PropertyPointKernel::getBoundingBox() const
00072 {
00073     Base::BoundBox3d box;
00074     for (PointKernel::const_iterator it = _cPoints->begin(); it != _cPoints->end(); ++it)
00075         box.Add(*it);
00076     return box;
00077 }
00078 
00079 void PropertyPointKernel::getFaces(std::vector<Base::Vector3d> &Points,
00080                                    std::vector<Data::ComplexGeoData::Facet> &Topo,
00081                                    float Accuracy, uint16_t flags) const
00082 {
00083     _cPoints->getFaces(Points, Topo, Accuracy, flags);
00084 }
00085 
00086 PyObject *PropertyPointKernel::getPyObject(void)
00087 {
00088     PointsPy* points = new PointsPy(&*_cPoints);
00089     points->setConst(); // set immutable
00090     return points;
00091 }
00092 
00093 void PropertyPointKernel::setPyObject(PyObject *value)
00094 {
00095     if (PyObject_TypeCheck(value, &(PointsPy::Type))) {
00096         PointsPy  *pcObject = (PointsPy*)value;
00097         setValue( *(pcObject->getPointKernelPtr()));
00098     }
00099     else {
00100         std::string error = std::string("type must be 'Points', not ");
00101         error += value->ob_type->tp_name;
00102         throw Py::TypeError(error);
00103     }
00104 }
00105 
00106 void PropertyPointKernel::Save (Base::Writer &writer) const
00107 {
00108     _cPoints->Save(writer);
00109 }
00110 
00111 void PropertyPointKernel::Restore(Base::XMLReader &reader)
00112 {
00113     reader.readElement("Points");
00114     std::string file (reader.getAttribute("file") );
00115 
00116     if (!file.empty()) {
00117         // initate a file read
00118         reader.addFile(file.c_str(),this);
00119     }
00120     if(reader.DocumentSchema > 3)
00121     {
00122         std::string Matrix (reader.getAttribute("mtrx") );
00123         Base::Matrix4D mtrx;
00124         mtrx.fromString(Matrix);
00125 
00126         aboutToSetValue();
00127         _cPoints->setTransform(mtrx);
00128         hasSetValue();
00129     }
00130 }
00131 
00132 void PropertyPointKernel::SaveDocFile (Base::Writer &writer) const
00133 {
00134     // does nothing
00135 }
00136 
00137 void PropertyPointKernel::RestoreDocFile(Base::Reader &reader)
00138 {
00139     aboutToSetValue();
00140     _cPoints->RestoreDocFile(reader);
00141     hasSetValue();
00142 }
00143 
00144 App::Property *PropertyPointKernel::Copy(void) const 
00145 {
00146     PropertyPointKernel* prop = new PropertyPointKernel();
00147     (*prop->_cPoints) = (*this->_cPoints);
00148     return prop;
00149 }
00150 
00151 void PropertyPointKernel::Paste(const App::Property &from)
00152 {
00153     aboutToSetValue();
00154     const PropertyPointKernel& prop = dynamic_cast<const PropertyPointKernel&>(from);
00155     *(this->_cPoints) = *(prop._cPoints);
00156     hasSetValue();
00157 }
00158 
00159 unsigned int PropertyPointKernel::getMemSize (void) const
00160 {
00161     return sizeof(Base::Vector3f) * this->_cPoints->size();
00162 }
00163 
00164 void PropertyPointKernel::removeIndices( const std::vector<unsigned long>& uIndices )
00165 {
00166     // We need a sorted array
00167     std::vector<unsigned long> uSortedInds = uIndices;
00168     std::sort(uSortedInds.begin(), uSortedInds.end());
00169 
00170     assert( uSortedInds.size() <= _cPoints->size() );
00171     if ( uSortedInds.size() > _cPoints->size() )
00172         return;
00173 
00174     PointKernel kernel;
00175     kernel.setTransform(_cPoints->getTransform());
00176     kernel.reserve(_cPoints->size() - uSortedInds.size());
00177 
00178     std::vector<unsigned long>::iterator pos = uSortedInds.begin();
00179     unsigned long index = 0;
00180     for (PointKernel::const_iterator it = _cPoints->begin(); it != _cPoints->end(); ++it, ++index) {
00181         if (pos == uSortedInds.end())
00182             kernel.push_back( *it );
00183         else if (index != *pos)
00184             kernel.push_back( *it );
00185         else 
00186             pos++;
00187     }
00188 
00189     setValue(kernel);
00190 }
00191 
00192 void PropertyPointKernel::transformGeometry(const Base::Matrix4D &rclMat)
00193 {
00194     aboutToSetValue();
00195     _cPoints->transformGeometry(rclMat);
00196     hasSetValue();
00197 }

Generated on Wed Nov 23 19:00:31 2011 for FreeCAD by  doxygen 1.6.1