SourceXtractorPlusPlus  0.19
SourceXtractor++, the next generation SExtractor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExtendedModel.icpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include <cmath> // for std::sqrt
25 
27 
29 
31 
33 
34 namespace ModelFitting {
35 
36 namespace _impl {
37 
38  template <typename ImageType>
39  void addSharp(ImageType& image, double pixel_scale, ModelComponent& component) {
41  ssize_t size_x = Traits::width(image);
42  ssize_t size_y = Traits::height(image);
43  for (auto& sample : component.getSharpSampling()) {
44  ssize_t image_x = std::get<0>(sample) / pixel_scale + size_x / 2.;
45  ssize_t image_y = std::get<1>(sample) / pixel_scale + size_y / 2.;
46  if (image_x >= 0 && image_x < size_x && image_y >= 0 && image_y < size_y) {
47  Traits::at(image, image_x, image_y) += std::get<2>(sample);
48  }
49  }
50  }
51 
52  template <typename ImageType>
53  void addSmooth(ImageType& image, double pixel_scale, ModelComponent& component) {
55  auto size_x = Traits::width(image);
56  auto size_y = Traits::height(image);
57  for (std::size_t y = 0; y < size_y; ++y) {
58  double y_model = y - (size_y - 1) / 2.;
59  y_model *= pixel_scale;
60  for (std::size_t x = 0; x < size_x; ++x) {
61  double x_model = x - (size_x - 1) / 2.;
62  x_model *= pixel_scale;
63 
64  if (!component.insideSharpRegion(x_model - pixel_scale / 2., y_model - pixel_scale / 2.) ||
65  !component.insideSharpRegion(x_model - pixel_scale / 2., y_model + pixel_scale / 2.) ||
66  !component.insideSharpRegion(x_model + pixel_scale / 2., y_model - pixel_scale / 2.) ||
67  !component.insideSharpRegion(x_model + pixel_scale / 2., y_model + pixel_scale / 2.)) {
68  Traits::at(image, x, y) = component.getValue(x_model, y_model) * pixel_scale * pixel_scale;
69  }
70  }
71  }
72  }
73 
74 } // end of namespace _impl
75 
76 template<typename ImageType>
78  //std::cout << "]] " << getX() << " " << getY() << "\n";
80  if (size_x % 2 == 0 || size_y % 2 == 0) {
81  throw Elements::Exception() << "Rasterized image dimensions must be odd numbers "
82  << "but got (" << size_x << ',' << size_y << ")";
83  }
84  ImageType image = Traits::factory(size_x, size_y);
85  double r_max = std::sqrt(size_x * size_x + size_y * size_y) / 2.;
86  for (auto& component : m_component_list) {
87  component->updateRasterizationInfo(pixel_scale, r_max);
88  ImageType comp_image = Traits::factory(size_x, size_y);
89  _impl::addSharp(comp_image, pixel_scale, *component);
90  _impl::addSmooth(comp_image, pixel_scale, *component);
91  for (auto im_it = Traits::begin(image), comp_it = Traits::begin(comp_image);
92  im_it != Traits::end(image); ++im_it, ++comp_it) {
93  *im_it += *comp_it;
94  }
95  }
96  return image;
97 }
98 
99 template<typename ImageType>
102  std::shared_ptr<BasicParameter> rotation_angle, double width, double height,
104  : PositionedModel(x, y), m_width(width), m_height(height) {
105  for (auto& component : component_list) {
106  auto scaled = Euclid::make_unique<ScaledModelComponent>(std::move(component), x_scale, y_scale);
107  auto rotated = Euclid::make_unique<RotatedModelComponent>(std::move(scaled), rotation_angle);
108  m_component_list.emplace_back(std::move(rotated));
109  }
110 }
111 
112 template<typename ImageType>
113 double ExtendedModel<ImageType>::getValue(double x, double y) const {
114  x -= getX();
115  y -= getY();
116  return std::accumulate(m_component_list.begin(), m_component_list.end(), 0.,
117  [x, y](double a, const std::unique_ptr<ModelComponent>& b) {
118  return a + b->getValue(x, y);
119  });
120 }
121 
122 } // end of namespace ModelFitting
void addSharp(ImageType &image, double pixel_scale, ModelComponent &component)
virtual ImageType getRasterizedImage(double pixel_scale, std::size_t size_x, std::size_t size_y) const
virtual bool insideSharpRegion(double x, double y)=0
virtual std::vector< ModelSample > getSharpSampling()=0
ExtendedModel(std::vector< std::unique_ptr< ModelComponent >> &&component_list, std::shared_ptr< BasicParameter > x_scale, std::shared_ptr< BasicParameter > y_scale, std::shared_ptr< BasicParameter > rotation_angle, double width, double height, std::shared_ptr< BasicParameter > x, std::shared_ptr< BasicParameter > y)
virtual double getValue(double x, double y)=0
std::vector< std::unique_ptr< ModelComponent > > m_component_list
Definition: ExtendedModel.h:65
T move(T...args)
STL class.
ModelFitting::ImageTraits< ImageInterfaceTypePtr > Traits
STL class.
std::unique_ptr< T > make_unique(Args &&...args)
virtual double getValue(double x, double y) const
T sqrt(T...args)
T accumulate(T...args)
void addSmooth(ImageType &image, double pixel_scale, ModelComponent &component)
const double pixel_scale
Definition: TestImage.cpp:74