loss.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Clearpath Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #ifndef FUSE_CORE_LOSS_H
35 #define FUSE_CORE_LOSS_H
36 
37 #include <fuse_core/fuse_macros.h>
39 
40 #include <boost/serialization/access.hpp>
41 #include <boost/type_index/stl_type_index.hpp>
42 #include <ceres/loss_function.h>
43 
44 #include <ostream>
45 #include <string>
46 
47 
61 #define FUSE_LOSS_CLONE_DEFINITION(...) \
62  fuse_core::Loss::UniquePtr clone() const override \
63  { \
64  return __VA_ARGS__::make_unique(*this); \
65  }
66 
80 #define FUSE_LOSS_SERIALIZE_DEFINITION(...) \
81  void serialize(fuse_core::BinaryOutputArchive& archive) const override \
82  { \
83  archive << *this; \
84  } /* NOLINT */ \
85  void serialize(fuse_core::TextOutputArchive& archive) const override \
86  { \
87  archive << *this; \
88  } /* NOLINT */ \
89  void deserialize(fuse_core::BinaryInputArchive& archive) override \
90  { \
91  archive >> *this; \
92  } /* NOLINT */ \
93  void deserialize(fuse_core::TextInputArchive& archive) override \
94  { \
95  archive >> *this; \
96  }
97 
113 #define FUSE_LOSS_TYPE_DEFINITION(...) \
114  struct detail \
115  { \
116  static std::string type() \
117  { \
118  return boost::typeindex::stl_type_index::type_id<__VA_ARGS__>().pretty_name(); \
119  } /* NOLINT */ \
120  }; /* NOLINT */ \
121  std::string type() const override \
122  { \
123  return detail::type(); \
124  }
125 
139 #define FUSE_LOSS_DEFINITIONS(...) \
140  FUSE_SMART_PTR_DEFINITIONS(__VA_ARGS__) \
141  FUSE_LOSS_TYPE_DEFINITION(__VA_ARGS__) \
142  FUSE_LOSS_CLONE_DEFINITION(__VA_ARGS__) \
143  FUSE_LOSS_SERIALIZE_DEFINITION(__VA_ARGS__)
144 
145 
146 namespace fuse_core
147 {
148 
169 class Loss
170 {
171 public:
173 
174  static constexpr ceres::Ownership Ownership =
175  ceres::Ownership::TAKE_OWNERSHIP;
176 
180  Loss() = default;
181 
185  virtual ~Loss() = default;
186 
194  virtual void initialize(const std::string& name) = 0;
195 
202  virtual std::string type() const = 0;
203 
209  virtual void print(std::ostream& stream = std::cout) const = 0;
210 
221  virtual ceres::LossFunction* lossFunction() const = 0;
222 
233  virtual Loss::UniquePtr clone() const = 0;
234 
245  virtual void serialize(fuse_core::BinaryOutputArchive& /* archive */) const = 0;
246 
257  virtual void serialize(fuse_core::TextOutputArchive& /* archive */) const = 0;
258 
269  virtual void deserialize(fuse_core::BinaryInputArchive& /* archive */) = 0;
270 
281  virtual void deserialize(fuse_core::TextInputArchive& /* archive */) = 0;
282 
283 private:
284  // Allow Boost Serialization access to private methods
286 
293  template<class Archive>
294  void serialize(Archive& /* archive */, const unsigned int /* version */)
295  {
296  }
297 };
298 
302 std::ostream& operator <<(std::ostream& stream, const Loss& loss);
303 
304 } // namespace fuse_core
305 
306 #endif // FUSE_CORE_LOSS_H
fuse_core::Loss
The Loss function interface definition.
Definition: loss.h:169
fuse_core::Loss::initialize
virtual void initialize(const std::string &name)=0
Perform any required post-construction initialization, such as reading from the parameter server.
fuse_macros.h
fuse_core::Loss::clone
virtual Loss::UniquePtr clone() const =0
Perform a deep copy of the Loss and return a unique pointer to the copy.
fuse_core::TextInputArchive
boost::archive::text_iarchive TextInputArchive
Definition: serialization.h:62
fuse_core::Loss::Loss
Loss()=default
Default constructor.
fuse_core::Loss::print
virtual void print(std::ostream &stream=std::cout) const =0
Print a human-readable description of the loss function to the provided stream.
fuse_core::Loss::type
virtual std::string type() const =0
Returns a unique name for this loss function type.
fuse_core
Definition: async_motion_model.h:48
fuse_core::BinaryOutputArchive
boost::archive::binary_oarchive BinaryOutputArchive
Definition: serialization.h:61
fuse_core::operator<<
std::ostream & operator<<(std::ostream &stream, const Constraint &constraint)
Definition: constraint.cpp:53
fuse_core::Loss::~Loss
virtual ~Loss()=default
Destructor.
fuse_core::Loss::deserialize
virtual void deserialize(fuse_core::BinaryInputArchive &)=0
Deserialize data from the provided binary archive into this Loss.
fuse_core::Loss::serialize
void serialize(Archive &, const unsigned int)
The Boost Serialize method that serializes all of the data members in to/out of the archive.
Definition: loss.h:294
fuse_core::Loss::Ownership
static constexpr ceres::Ownership Ownership
The ownership of the ceres::LossFunction* returned by lossFunction()
Definition: loss.h:174
fuse_core::Loss::FUSE_SMART_PTR_ALIASES_ONLY
FUSE_SMART_PTR_ALIASES_ONLY(Loss)
fuse_core::TextOutputArchive
boost::archive::text_oarchive TextOutputArchive
Definition: serialization.h:63
serialization.h
fuse_core::BinaryInputArchive
boost::archive::binary_iarchive BinaryInputArchive
Definition: serialization.h:60
fuse_core::Loss::serialize
virtual void serialize(fuse_core::BinaryOutputArchive &) const =0
Serialize this Loss into the provided binary archive.
fuse_core::Loss::access
friend class boost::serialization::access
Definition: loss.h:285
fuse_core::Loss::lossFunction
virtual ceres::LossFunction * lossFunction() const =0
Return a raw pointer to a ceres::LossFunction that implements the loss function.


fuse_core
Author(s): Stephen Williams
autogenerated on Thu Apr 24 2025 02:18:55