DGtal  1.5.beta
CDLR.h
1 
17 #pragma once
18 
28 #if defined(CDLR_RECURSES)
29 #error Recursive header files inclusion detected in CDLR.h
30 #else // defined(CDLR_RECURSES)
32 #define CDLR_RECURSES
33 
34 #if !defined CDLR_h
36 #define CDLR_h
37 
38 #include "DGtal/base/Common.h"
39 #include <DGtal/images/RigidTransformation2D.h>
40 #include "CDLR_naiverotation.h"
41 #include "DigitizedReflection.h"
42 #include "Policy.h"
43 namespace DGtal {
44 
52  template < typename TSpace, typename TInputValue = typename TSpace::Point, typename TOutputValue = typename TSpace::Point>
53  class CDLR {
54  public:
61  CDLR( double ang, TOutputValue ptCenter, std::shared_ptr<Policy<TSpace,HyperRectDomain< TSpace>,CDLR_naiverotation<TSpace>>> policy );
62 
64  inline double angle() const{return my_angle;};
66  inline void set_angle(const double new_angle) {
67  my_angle=new_angle;
68  my_naive_rdlr_rotation.set_angle(new_angle);
69  dslSolver(new_angle,my_center);
70  }
71 
74  void dslSolver(double ang, TOutputValue ptCenter);
75 
76 
77 
79  inline TOutputValue center() const{return my_center;};
81  inline TOutputValue& center(){return my_center;};
82 
86  TOutputValue rotate( const TInputValue& p ) const;
87  TOutputValue operator()( const TInputValue& p ) const;
88 
89  template<typename Img>
90  Img rotateImage(Img img) const;
91 
92  std::string tostring() const {
93  return {"CDLR"};
94  }
95 
96  protected:
98  double my_angle;
100  TOutputValue my_center;
101 
102 
103 
105  std::shared_ptr<Policy<TSpace,HyperRectDomain< TSpace>,CDLR_naiverotation<TSpace>>> my_policy;
106 
107  private:
109  double a;
110  double b;
111  double my_minAngle;
112  };
113 
114  template<typename TSpace, typename TInputValue, typename TOutputValue>
115  CDLR<TSpace,TInputValue,TOutputValue>::CDLR( const double ang, const TOutputValue ptCenter, std::shared_ptr<Policy<TSpace,HyperRectDomain< TSpace>,CDLR_naiverotation<TSpace>>> policy ):my_angle(ang),my_center(ptCenter),my_naive_rdlr_rotation(ang,ptCenter,0.),my_policy(policy){
116  dslSolver(ang,ptCenter);
117  }
118 
119  template<typename TSpace, typename TInputValue, typename TOutputValue>
120  void CDLR<TSpace, TInputValue, TOutputValue>::dslSolver(double ang, TOutputValue ptCenter) {
121 
122  a = std::sin(ang/2.0);
123  b = std::cos(ang/2.0);
124 
125  TInputValue origin(0,0);
127 
128  double errorMinAlpha = 200.*200;
129  int N = 200;
130 
131  // create the domain
132  TOutputValue A(0,0);
133  TOutputValue B(1000,1000);
134  HyperRectDomain<TSpace> my_domain(A,B);
135 
136 
138  std::vector<double> mix_errors;
139  std::vector<double> angles;
140  for(int k=0;k<N;++k){
141  double alphaCourant = -M_PI_2 + (k*M_PI_2)/N;
142  angles.push_back(alphaCourant);
143 
144  // compute both reflections and get both the Linf and Lcontinuity errors
145  std::vector<double> linf_per_image;
146  my_naive_rdlr_rotation.set_startingAngle(alphaCourant);
147  double error = my_policy->evaluate(my_domain,my_naive_rdlr_rotation,my_angle,my_center);
148  mix_errors.push_back(error);
149  }
150  for(int idxImages = 0 ; idxImages < mix_errors.size() ; idxImages++) {
151  if(mix_errors[idxImages] < errorMinAlpha) {
152  errorMinAlpha = mix_errors[idxImages];
153  my_minAngle = angles[idxImages];
154  }
155  }
156  }
157 
158 
159  template<typename TSpace, typename TInputValue, typename TOutputValue>
160  TOutputValue CDLR<TSpace,TInputValue,TOutputValue>::rotate( const TInputValue& p ) const{
161  return this->operator()(p);
162  }
163 
164 
165 
166 
167 
168 
169 
170  template<typename TSpace, typename TInputValue, typename TOutputValue>
171  TOutputValue CDLR<TSpace,TInputValue,TOutputValue>::operator()( const TInputValue& p ) const{
172  return my_naive_rdlr_rotation.reflect(my_minAngle+(my_angle),my_center,my_naive_rdlr_rotation.reflect(my_minAngle,my_center,p-my_center))+my_center;
173  }
174 
175 
176  template<typename TSpace, typename TInputValue, typename TOutputValue>
177  template<typename TImage>
178  TImage CDLR<TSpace,TInputValue,TOutputValue>::rotateImage( const TImage img ) const{
179  typedef typename TImage::Domain TDomain;
181  typedef std::pair < typename TSpace::Point, typename TSpace::Point > Bounds;
182 
183  typename TSpace::Point bottomLeft(-2,-2);
184  typename TSpace::Point topRight(2,2);
185  MyDomainTransformer domainTransformer ( *this );
186  Bounds bounds = domainTransformer ( img.domain() );
187  TDomain transformedDomain ( bounds.first+bottomLeft, bounds.second+topRight );
188  TImage rotatedImage ( transformedDomain );
189 
190  for (typename TDomain::ConstIterator it = img.domain().begin(); it != img.domain().end(); ++it )
191  {
192  rotatedImage.setValue((*this)(*it),img(*it));
193  }
194  return rotatedImage;
195  }
196 
197 
198 }
199 
200 
201 #endif //CDLR
202 #undef CDLR_RECURSES
203 #endif // else defined(CDLR_RECURSES)
CDLR : Rotation with Discrete Line Reflections,.
Definition: CDLR.h:53
CDLR_naiverotation< TSpace, TInputValue, TOutputValue > my_naive_rdlr_rotation
Definition: CDLR.h:104
TOutputValue & center()
Definition: CDLR.h:81
TOutputValue rotate(const TInputValue &p) const
Definition: CDLR.h:160
double my_minAngle
Definition: CDLR.h:111
std::shared_ptr< Policy< TSpace, HyperRectDomain< TSpace >, CDLR_naiverotation< TSpace > > > my_policy
Definition: CDLR.h:105
TOutputValue operator()(const TInputValue &p) const
Definition: CDLR.h:171
double a
DSL specific variables, see Andres paper.
Definition: CDLR.h:109
double angle() const
Definition: CDLR.h:64
Img rotateImage(Img img) const
CDLR(double ang, TOutputValue ptCenter, std::shared_ptr< Policy< TSpace, HyperRectDomain< TSpace >, CDLR_naiverotation< TSpace >>> policy)
Definition: CDLR.h:115
TOutputValue center() const
Definition: CDLR.h:79
void dslSolver(double ang, TOutputValue ptCenter)
Definition: CDLR.h:120
void set_angle(const double new_angle)
Definition: CDLR.h:66
std::string tostring() const
Definition: CDLR.h:92
double my_angle
The angle of rotation.
Definition: CDLR.h:98
double b
Definition: CDLR.h:110
TOutputValue my_center
The center of rotation.
Definition: CDLR.h:100
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Aim: implements bounds of transformed domain.
Aim: implements forward rigid transformation of point in the 2D integer space. Warring: This version ...
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Policy : ,.
Definition: Policy.h:55
Aim: Define a simple default functor that just returns its argument.
MyPointD Point
Definition: testClone2.cpp:383
HyperRectDomain< Space > Domain