lsst.afw  13.0-49-g6f23dda
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
KernelFormatter.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Implementation of KernelFormatter class
27  */
28 
29 #ifndef __GNUC__
30 #define __attribute__(x) /*NOTHING*/
31 #endif
32 static char const* SVNid __attribute__((unused)) = "$Id$";
33 
35 
36 #include <stdexcept>
37 #include <string>
38 #include <vector>
39 
40 #include <boost/archive/text_oarchive.hpp>
41 #include <boost/archive/text_iarchive.hpp>
42 #include <boost/archive/binary_oarchive.hpp>
43 #include <boost/archive/binary_iarchive.hpp>
44 #include <boost/archive/xml_oarchive.hpp>
45 #include <boost/archive/xml_iarchive.hpp>
46 #include <boost/serialization/nvp.hpp>
47 #include <boost/serialization/vector.hpp>
48 
49 #include <boost/serialization/export.hpp>
50 
51 #include "lsst/afw/math/Kernel.h"
52 #include "lsst/afw/math/Function.h"
54 #include "lsst/daf/persistence/FormatterImpl.h"
55 #include "lsst/daf/persistence/LogicalLocation.h"
56 #include "lsst/daf/persistence/BoostStorage.h"
57 #include "lsst/daf/persistence/XmlStorage.h"
58 #include "lsst/log/Log.h"
59 #include <lsst/pex/exceptions.h>
60 #include <lsst/pex/policy/Policy.h>
61 
62 BOOST_CLASS_EXPORT(lsst::afw::math::Kernel)
63 BOOST_CLASS_EXPORT(lsst::afw::math::FixedKernel)
64 BOOST_CLASS_EXPORT(lsst::afw::math::AnalyticKernel)
65 BOOST_CLASS_EXPORT(lsst::afw::math::DeltaFunctionKernel)
66 BOOST_CLASS_EXPORT(lsst::afw::math::LinearCombinationKernel)
67 BOOST_CLASS_EXPORT(lsst::afw::math::SeparableKernel)
68 
69 BOOST_CLASS_EXPORT(lsst::afw::math::Function<float>)
70 BOOST_CLASS_EXPORT(lsst::afw::math::NullFunction1<float>)
71 BOOST_CLASS_EXPORT(lsst::afw::math::NullFunction2<float>)
72 
73 BOOST_CLASS_EXPORT(lsst::afw::math::Function<double>)
74 BOOST_CLASS_EXPORT(lsst::afw::math::NullFunction1<double>)
75 BOOST_CLASS_EXPORT(lsst::afw::math::NullFunction2<double>)
76 
77 BOOST_CLASS_EXPORT(lsst::afw::math::IntegerDeltaFunction2<float>)
78 BOOST_CLASS_EXPORT(lsst::afw::math::GaussianFunction1<float>)
79 BOOST_CLASS_EXPORT(lsst::afw::math::GaussianFunction2<float>)
80 BOOST_CLASS_EXPORT(lsst::afw::math::DoubleGaussianFunction2<float>)
81 BOOST_CLASS_EXPORT(lsst::afw::math::PolynomialFunction1<float>)
82 BOOST_CLASS_EXPORT(lsst::afw::math::PolynomialFunction2<float>)
83 BOOST_CLASS_EXPORT(lsst::afw::math::Chebyshev1Function1<float>)
84 BOOST_CLASS_EXPORT(lsst::afw::math::Chebyshev1Function2<float>)
85 BOOST_CLASS_EXPORT(lsst::afw::math::LanczosFunction1<float>)
86 BOOST_CLASS_EXPORT(lsst::afw::math::LanczosFunction2<float>)
87 
88 BOOST_CLASS_EXPORT(lsst::afw::math::IntegerDeltaFunction2<double>)
89 BOOST_CLASS_EXPORT(lsst::afw::math::GaussianFunction1<double>)
90 BOOST_CLASS_EXPORT(lsst::afw::math::GaussianFunction2<double>)
91 BOOST_CLASS_EXPORT(lsst::afw::math::DoubleGaussianFunction2<double>)
92 BOOST_CLASS_EXPORT(lsst::afw::math::PolynomialFunction1<double>)
93 BOOST_CLASS_EXPORT(lsst::afw::math::PolynomialFunction2<double>)
94 BOOST_CLASS_EXPORT(lsst::afw::math::Chebyshev1Function1<double>)
95 BOOST_CLASS_EXPORT(lsst::afw::math::Chebyshev1Function2<double>)
96 BOOST_CLASS_EXPORT(lsst::afw::math::LanczosFunction1<double>)
97 BOOST_CLASS_EXPORT(lsst::afw::math::LanczosFunction2<double>)
98 
99 namespace {
100 LOG_LOGGER _log = LOG_GET("afw.math.KernelFormatter");
101 }
102 
103 namespace lsst {
104 namespace afw {
105 namespace formatters {
106 
107 namespace dafBase = lsst::daf::base;
108 namespace dafPersist = lsst::daf::persistence;
109 namespace pexPolicy = lsst::pex::policy;
110 
111 using boost::serialization::make_nvp;
112 
113 dafPersist::FormatterRegistration KernelFormatter::kernelRegistration("Kernel", typeid(math::Kernel),
114  createInstance);
115 dafPersist::FormatterRegistration KernelFormatter::fixedKernelRegistration("FixedKernel",
116  typeid(math::FixedKernel),
117  createInstance);
118 dafPersist::FormatterRegistration KernelFormatter::analyticKernelRegistration("AnalyticKernel",
119  typeid(math::AnalyticKernel),
120  createInstance);
121 dafPersist::FormatterRegistration KernelFormatter::deltaFunctionKernelRegistration(
122  "DeltaFunctionKernel", typeid(math::DeltaFunctionKernel), createInstance);
123 dafPersist::FormatterRegistration KernelFormatter::linearCombinationKernelRegistration(
124  "LinearCombinationKernel", typeid(math::LinearCombinationKernel), createInstance);
125 dafPersist::FormatterRegistration KernelFormatter::separableKernelRegistration("SeparableKernel",
126  typeid(math::SeparableKernel),
127  createInstance);
128 
129 KernelFormatter::KernelFormatter(std::shared_ptr<pexPolicy::Policy> policy)
130  : dafPersist::Formatter(typeid(this)), _policy(policy) {}
131 
132 KernelFormatter::~KernelFormatter(void) {}
133 
134 void KernelFormatter::write(dafBase::Persistable const* persistable,
135  std::shared_ptr<dafPersist::Storage> storage,
136  std::shared_ptr<dafBase::PropertySet>) {
137  LOGL_DEBUG(_log, "KernelFormatter write start");
138  math::Kernel const* kp = dynamic_cast<math::Kernel const*>(persistable);
139  if (kp == 0) {
140  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Persisting non-Kernel");
141  }
142  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
143  LOGL_DEBUG(_log, "KernelFormatter write BoostStorage");
144  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
145  boost->getOArchive() & kp;
146  LOGL_DEBUG(_log, "KernelFormatter write end");
147  return;
148  } else if (typeid(*storage) == typeid(dafPersist::XmlStorage)) {
149  LOGL_DEBUG(_log, "KernelFormatter write XmlStorage");
150  dafPersist::XmlStorage* xml = dynamic_cast<dafPersist::XmlStorage*>(storage.get());
151  xml->getOArchive() & make_nvp("ptr", kp);
152  LOGL_DEBUG(_log, "KernelFormatter write end");
153  return;
154  }
155  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Unrecognized Storage for Kernel");
156 }
157 
158 dafBase::Persistable* KernelFormatter::read(std::shared_ptr<dafPersist::Storage> storage,
159  std::shared_ptr<dafBase::PropertySet>) {
160  LOGL_DEBUG(_log, "KernelFormatter read start");
161  math::Kernel* kp;
162  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
163  LOGL_DEBUG(_log, "KernelFormatter read BoostStorage");
164  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
165  boost->getIArchive() & kp;
166  LOGL_DEBUG(_log, "KernelFormatter read end");
167  return kp;
168  } else if (typeid(*storage) == typeid(dafPersist::XmlStorage)) {
169  LOGL_DEBUG(_log, "KernelFormatter read XmlStorage");
170  dafPersist::XmlStorage* xml = dynamic_cast<dafPersist::XmlStorage*>(storage.get());
171  xml->getIArchive() & make_nvp("ptr", kp);
172  LOGL_DEBUG(_log, "KernelFormatter read end");
173  return kp;
174  }
175  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Unrecognized Storage for Kernel");
176 }
177 
178 void KernelFormatter::update(dafBase::Persistable*, std::shared_ptr<dafPersist::Storage>,
179  std::shared_ptr<dafBase::PropertySet>) {
180  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Unexpected call to update for Kernel");
181 }
182 
183 template <class Archive>
184 void KernelFormatter::delegateSerialize(Archive& ar, unsigned int const, dafBase::Persistable* persistable) {
185  LOGL_DEBUG(_log, "KernelFormatter delegateSerialize start");
186  math::Kernel* kp = dynamic_cast<math::Kernel*>(persistable);
187  if (kp == 0) {
188  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Serializing non-Kernel");
189  }
190  ar& make_nvp("base", boost::serialization::base_object<dafBase::Persistable>(*kp));
191  ar& make_nvp("width", kp->_width);
192  ar& make_nvp("height", kp->_height);
193  ar& make_nvp("ctrX", kp->_ctrX);
194  ar& make_nvp("ctrY", kp->_ctrY);
195  ar& make_nvp("nParams", kp->_nKernelParams);
196  ar& make_nvp("spatialFunctionList", kp->_spatialFunctionList);
197 
198  LOGL_DEBUG(_log, "KernelFormatter delegateSerialize end");
199 }
200 
201 // Explicit template specializations confuse Doxygen, tell it to ignore them
203 template void KernelFormatter::delegateSerialize(boost::archive::text_oarchive& ar, unsigned int const,
204  dafBase::Persistable*);
205 template void KernelFormatter::delegateSerialize(boost::archive::text_iarchive& ar, unsigned int const,
206  dafBase::Persistable*);
207 template void KernelFormatter::delegateSerialize(boost::archive::xml_oarchive& ar, unsigned int const,
208  dafBase::Persistable*);
209 template void KernelFormatter::delegateSerialize(boost::archive::xml_iarchive& ar, unsigned int const,
210  dafBase::Persistable*);
211 template void KernelFormatter::delegateSerialize(boost::archive::binary_oarchive& ar, unsigned int const,
212  dafBase::Persistable*);
213 template void KernelFormatter::delegateSerialize(boost::archive::binary_iarchive& ar, unsigned int const,
214  dafBase::Persistable*);
216 
217 std::shared_ptr<dafPersist::Formatter> KernelFormatter::createInstance(
218  std::shared_ptr<pexPolicy::Policy> policy) {
219  return std::shared_ptr<dafPersist::Formatter>(new KernelFormatter(policy));
220 }
221 }
222 }
223 }
#define __attribute__(x)
Formatter for persistence of Kernel instances.
std::vector< SpatialFunctionPtr > _spatialFunctionList
Definition: Kernel.h:482
Kernels are used for convolution with MaskedImages and (eventually) Images.
Definition: Kernel.h:123