SundanceCellFilter.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #include "SundanceCellFilter.hpp"
00043 #include "SundanceCellFilterBase.hpp"
00044 #include "SundanceExplicitCellSet.hpp"
00045 #include "SundanceBinaryCellFilter.hpp"
00046 #include "SundanceSubsetCellFilter.hpp"
00047 #include "SundanceLabelCellPredicate.hpp"
00048 #include "SundancePositionalCellPredicate.hpp"
00049 #include "SundanceNullCellFilterStub.hpp"
00050 #include "SundanceNullCellFilter.hpp"
00051 #include "PlayaTabs.hpp"
00052 #include "SundanceSubsetManager.hpp"
00053 
00054 using namespace Sundance;
00055 using namespace Sundance;
00056 using namespace Sundance;
00057 using namespace Teuchos;
00058 
00059 bool CellFilter::isNullCellFilter() const 
00060 {
00061   return dynamic_cast<const NullCellFilterStub*>(ptr().get()) != 0;
00062 }
00063 
00064 bool CellFilter::isNull() const
00065 {
00066   return ptr().get() == 0 || isNullCellFilter();
00067 }
00068 
00069 void CellFilter::setName(const std::string& name)
00070 {
00071   nonConstCfbPtr()->setName(name);
00072 }
00073 
00074 CellSet CellFilter::getCells(const Mesh& mesh) const
00075 {
00076   if (isNull() || isNullCellFilter())
00077   {
00078     return new ExplicitCellSet(mesh, -1, 
00079       NullCell);
00080   }
00081   return cfbPtr()->getCells(mesh);
00082 }
00083 
00084 
00085 
00086 int CellFilter::dimension(const Mesh& mesh) const
00087 {
00088   if (isNullCellFilter())
00089   {
00090     return -1;
00091   }
00092   return cfbPtr()->dimension(mesh);
00093 }
00094 
00095 
00096 
00097 CellFilter CellFilter::operator+(const CellFilter& other) const 
00098 {
00099   if (isNull())
00100   {
00101     return other;
00102   }
00103   else if (other.isNull())
00104   {
00105     return *this;
00106   }
00107   else
00108   {
00109     CellFilter rtn 
00110       = new BinaryCellFilter(*this, other, BinaryCellFilter::Union);
00111     rtn.registerSubset(*this);
00112     rtn.registerSubset(other);
00113     return rtn;
00114   }
00115 }
00116 
00117 
00118 
00119 CellFilter CellFilter::operator-(const CellFilter& other) const 
00120 {
00121   if (other.isNull())
00122   {
00123     return *this;
00124   }
00125   else if (isKnownDisjointWith(other) || other.isKnownDisjointWith(*this))
00126   {
00127     return *this;
00128   }
00129   else if (isKnownSubsetOf(other))
00130   {
00131     CellFilter rtn;
00132     return rtn;
00133   }
00134   else if (*this == other)
00135   {
00136     CellFilter rtn;
00137     return rtn;
00138   }
00139   else
00140   {
00141     CellFilter rtn 
00142       = new BinaryCellFilter(*this, other, BinaryCellFilter::Difference);
00143     rtn.registerDisjoint(other);
00144     this->registerSubset(rtn);
00145     return rtn;
00146   }
00147 }
00148 
00149 
00150 
00151 CellFilter CellFilter::intersection(const CellFilter& other) const 
00152 {
00153   if (isNull() || other.isNull())
00154   {
00155     CellFilter rtn;
00156     return rtn;
00157   }
00158   else if (isKnownDisjointWith(other) || other.isKnownDisjointWith(*this))
00159   {
00160     CellFilter rtn;
00161     return rtn;
00162   }
00163   else if (isKnownSubsetOf(other))
00164   {
00165     return *this;
00166   }
00167   else if (other.isKnownSubsetOf(*this))
00168   {
00169     return other;
00170   }
00171   else if (*this==other)
00172   {
00173     return *this;
00174   }
00175   else
00176   {
00177     CellFilter rtn 
00178       = new BinaryCellFilter(*this, other, BinaryCellFilter::Intersection);
00179     other.registerSubset(rtn);
00180     this->registerSubset(rtn);
00181     
00182     return rtn;
00183   }
00184 }
00185 
00186 
00187 
00188 CellFilter CellFilter::labeledSubset(int label) const
00189 {
00190   return labeledSubset(tuple(label));
00191 }
00192 
00193 CellFilter CellFilter::labeledSubset(const Array<int>& labels) const
00194 {
00195   Set<int> labelSet = makeSet(labels);
00196   CellPredicate pred = new LabelCellPredicate(labelSet);
00197   CellFilter rtn = new SubsetCellFilter(*this, pred);
00198   this->registerLabeledSubset(labelSet, rtn);
00199   this->registerSubset(rtn);
00200 
00201   return rtn;
00202 }
00203 
00204 CellFilter CellFilter::coordSubset(int dir, const double& coordVal) const
00205 {
00206   CellPredicate pred = new CoordinateValueCellPredicate(dir, coordVal);
00207   CellFilter rtn = new SubsetCellFilter(*this, pred);
00208   this->registerSubset(rtn);
00209 
00210   return rtn;
00211 }
00212 
00213 CellFilter CellFilter::subset(const CellPredicate& pred) const
00214 {
00215   CellFilter rtn = new SubsetCellFilter(*this, pred);
00216   this->registerSubset(rtn);
00217   return rtn;
00218 }
00219 
00220 
00221 CellFilter CellFilter::subset(const RCP<CellPredicateFunctorBase>& test) const
00222 {
00223   CellFilter rtn = new SubsetCellFilter(*this, CellPredicate(test));
00224   this->registerSubset(rtn);
00225   return rtn;
00226 }
00227 
00228 bool CellFilter::isKnownSubsetOf(const CellFilter& other) const
00229 {
00230   if (other.knownSubsets().contains(*this)) return true;
00231   return false;
00232 }
00233 
00234 bool CellFilter::isKnownDisjointWith(const CellFilter& other) const
00235 {
00236   if (other.knownDisjoints().contains(*this)) return true;
00237   if (this->knownDisjoints().contains(other)) return true;
00238 
00239   return false;
00240 }
00241 
00242 bool CellFilter::isSubsetOf(const CellFilter& other,
00243   const Mesh& mesh) const
00244 {
00245   if (isKnownSubsetOf(other)) 
00246   {
00247     return true;
00248   }
00249   else
00250   {
00251     CellSet myCells = getCells(mesh);
00252     CellSet yourCells = other.getCells(mesh);
00253     CellSet inter = myCells.setIntersection(yourCells);
00254     if (inter.begin() == inter.end()) return false;
00255     CellSet diff = myCells.setDifference(inter);
00256     return (diff.begin() == diff.end());
00257   }
00258 }
00259 
00260 
00261 
00262 bool CellFilter::operator==(const CellFilter& other) const
00263 {
00264   if (*this < other) return false;
00265   if (other < *this) return false;
00266   return true;
00267 }
00268 
00269 bool CellFilter::operator!=(const CellFilter& other) const
00270 {
00271   return !( *this == other );
00272 }
00273 
00274 
00275 const Set<CellFilter>& CellFilter::knownSubsets() const
00276 {
00277   return SubsetManager::getSubsets(*this);
00278 }
00279 const Set<CellFilter>& CellFilter::knownDisjoints() const
00280 {
00281   return SubsetManager::getDisjoints(*this);
00282 }
00283 
00284 void CellFilter::registerSubset(const CellFilter& sub) const
00285 {
00286   SubsetManager::registerSubset(*this, sub);
00287   
00288   for (Set<CellFilter>::const_iterator 
00289          i=sub.knownSubsets().begin(); i!=sub.knownSubsets().end(); i++)
00290   {
00291     SubsetManager::registerSubset(*this, *i);
00292   }
00293 }
00294 
00295 
00296 void CellFilter::registerDisjoint(const CellFilter& sub) const
00297 {
00298   SubsetManager::registerDisjoint(*this, sub);
00299   
00300   for (Set<CellFilter>::const_iterator 
00301          i=sub.knownDisjoints().begin(); i!=sub.knownDisjoints().end(); i++)
00302   {
00303     SubsetManager::registerDisjoint(*this, *i);
00304   }
00305 }
00306 
00307 void CellFilter::registerLabeledSubset(const Set<int>& label, 
00308   const CellFilter& sub) const
00309 {
00310   SubsetManager::registerLabeledSubset(*this, label, sub);
00311   
00312   const Map<Set<int>, CellFilter>& subsub = SubsetManager::getLabeledSubsets(sub);
00313 
00314   for (Map<Set<int>, CellFilter>::const_iterator 
00315          iter=subsub.begin(); iter!=subsub.end(); iter++)
00316   {
00317     if (iter->first == label) continue;
00318     SubsetManager::registerDisjoint(sub, iter->second);
00319     SubsetManager::registerDisjoint(iter->second, sub);
00320   }
00321 }
00322 
00323 
00324 XMLObject CellFilter::toXML() const 
00325 {
00326   return ptr()->toXML();
00327 }
00328 
00329 string CellFilter::toString() const 
00330 {
00331   return cfbPtr()->toString();
00332 }
00333 
00334 const CellFilterBase* CellFilter::cfbPtr() const
00335 {
00336   const CellFilterBase* rtn = dynamic_cast<const CellFilterBase*>(ptr().get());
00337   TEUCHOS_TEST_FOR_EXCEPTION(rtn==0, std::logic_error, "CellFilter::cfbPtr() cast failed");
00338   return rtn;
00339 }
00340 
00341 CellFilterBase* CellFilter::nonConstCfbPtr()
00342 {
00343   CellFilterBase* rtn = dynamic_cast<CellFilterBase*>(ptr().get());
00344   TEUCHOS_TEST_FOR_EXCEPTION(rtn==0, std::logic_error, "CellFilter::nonConstCfbPtr() cast failed");
00345   return rtn;
00346 }

Site Contact