Chaste Release::3.1
ChasteNodesList.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 
00037 #ifndef CHASTENODESLIST_HPP_
00038 #define CHASTENODESLIST_HPP_
00039 
00040 #include "ChasteSerialization.hpp"
00041 #include <boost/serialization/base_object.hpp>
00042 
00043 #include "AbstractChasteRegion.hpp"
00044 #include "Node.hpp"
00045 #include "ChastePoint.hpp"
00046 
00047 #include <vector>
00048 using namespace std;
00053 template <unsigned SPACE_DIM>
00054 class ChasteNodesList : public AbstractChasteRegion<SPACE_DIM>
00055 {
00057     friend class boost::serialization::access;
00064     template<class Archive>
00065     void serialize(Archive & archive, const unsigned int version)
00066     {
00067         archive & boost::serialization::base_object<AbstractChasteRegion<SPACE_DIM> >(*this);
00068     }
00069 
00070 private:
00071 
00073     std::vector< Node<SPACE_DIM>*> mListOfNodes;
00074 
00076     bool mOwnNodes;
00077 
00078 public:
00079 
00086     ChasteNodesList(const std::vector<Node<SPACE_DIM>*> rNodesList,
00087                     bool ownNodes=false);
00088 
00092     ~ChasteNodesList();
00093 
00095     const std::vector< Node<SPACE_DIM>*>& rGetNodesList() const;
00096 
00102     bool DoesContain(const ChastePoint<SPACE_DIM>& rPointToCheck) const;
00103 
00107     unsigned GetSize() const;
00108 
00109 };
00110 
00111 // Declare identifier for the serializer
00112 #include "SerializationExportWrapper.hpp"
00113 EXPORT_TEMPLATE_CLASS_SAME_DIMS(ChasteNodesList)
00114 
00115 namespace boost
00116 {
00117 namespace serialization
00118 {
00119 
00120 template<class Archive, unsigned SPACE_DIM>
00121 inline void save_construct_data(
00122     Archive & ar, const ChasteNodesList<SPACE_DIM> * t, const unsigned int file_version)
00123 {
00124     const std::vector<Node<SPACE_DIM>* > node_list = t->rGetNodesList();
00125 
00126     // Archive the size first
00127     unsigned size = t->GetSize();
00128     ar & size;
00129 
00130     // The ChastePoints have to be at unique addresses, otherwise Boost thinks that the first archive is sufficient
00131     std::vector <ChastePoint<SPACE_DIM>* > point_list;
00132 
00133     for (unsigned i = 0; i < node_list.size(); i++)
00134     {
00135         c_vector<double, SPACE_DIM> loc =  node_list[i]->rGetLocation();
00136         point_list.push_back(new  ChastePoint<SPACE_DIM>(loc));
00137         ar & point_list[i];
00138         unsigned index = node_list[i]->GetIndex();
00139         ar & index;
00140     }
00141 
00142     // Clean memory
00143     for (unsigned i = 0; i < point_list.size(); i++)
00144     {
00145         delete point_list[i];
00146     }
00147 }
00148 
00153 template<class Archive, unsigned SPACE_DIM>
00154 inline void load_construct_data(
00155     Archive & ar, ChasteNodesList<SPACE_DIM> * t, const unsigned int file_version)
00156 {
00157     // Unarchive the size first
00158     unsigned size;
00159     ar & size;
00160 
00161     // Rebuild the node list based on the unarchived points and indices
00162     std::vector<Node<SPACE_DIM>* > node_list;
00163     for (unsigned i = 0; i < size; i++)
00164     {
00165         ChastePoint<SPACE_DIM>* p_point;
00166         unsigned index;
00167         ar & p_point;
00168         ar & index;
00169 
00170         Node<SPACE_DIM>* p_node = new Node<SPACE_DIM>( index, *(p_point));
00171         node_list.push_back(p_node);
00172 
00173         delete p_point; // not needed any longer
00174     }
00175 
00176     ::new(t)ChasteNodesList<SPACE_DIM>(node_list, true);
00177 }
00178 }
00179 } // namespace ...
00180 
00181 #endif /*CHASTENODESLIST_HPP_*/