DGtal  1.5.beta
CBDR.h
Go to the documentation of this file.
1 
17 #pragma once
18 
28 #if defined(CBDR_RECURSES)
29 #error Recursive header files inclusion detected in CBDR.h
30 #else // defined(CBDR_RECURSES)
32 #define CBDR_RECURSES
33 
34 #if !defined CBDR_h
36 #define CBDR_h
37 
39 // Inclusions
40 #include "GAVector.h"
41 #include "CBDRSolver.h"
42 #include "CBDRFastSolver.h"
43 
44 #include "Rotationtables.h"
45 #include "NBijectiveReflectionGenerator.h"
46 
47 
48 namespace DGtal {
56  template<typename TSpace, typename TInputValue = typename TSpace::RealPoint, typename TOutputValue = typename TSpace::Point>
57 struct CBDR {
60  BOOST_STATIC_ASSERT(( TSpace::dimension == 2 ));
61  BOOST_STATIC_ASSERT(( TOutputValue::dimension == 2 ));
62  BOOST_STATIC_ASSERT(( TInputValue::dimension == 2 ));
63 
64 
66  typedef std::vector<std::pair<std::vector<GAVector<TSpace>>,GAVector<TSpace>>> BijectiveSearchTree;
67 
68 
79  CBDR(const double theta,const typename TSpace::Point center,const size_t nbreflect,const size_t km, std::shared_ptr<Policy<TSpace,HyperRectDomain< TSpace>,CBDR_naiverotation<TSpace>>> policy,
80  const bool precompute=true, const bool fast =true ):nbijectiveGen(km),my_domain(typename TSpace::Point(0,0),typename TSpace::Point(100,100)),my_angle(theta),my_center(center),nbReflections(nbreflect),kmax(km),my_policy(policy),
82  }
83 
84 
85 
86 
87  // whenever fast is set, load the table that stores for each angle the optimised set of bijective reflections
88  std::shared_ptr<CBDR_naiverotation<TSpace>> initCBDRVec() {
89  if(useFastTable) {
90  std::cout << "use fast table"<<std::endl;
91  std::string fastTableName=("CBDROptimisedTable_"+std::to_string(nbReflections)+"_"+std::to_string(kmax)+".txt");
92  struct stat buffer;
93  fastTableFound = (stat (fastTableName.c_str(), &buffer) == 0);
94  if(fastTableFound) {
96  }else {
99  }
102  return std::make_shared<CBDR_naiverotation<TSpace>>(cbdrFastSolver.solve());
103  }else{
107  return std::make_shared<CBDR_naiverotation<TSpace>>(cbdrSolver.solve(my_domain,nbijectiveGen, vecBijectiveSearchTree,*my_policy));
108  }
109  }
110 
111  void set_angle(const double newAngle) {
112  my_angle = newAngle;
113  if(fastTableFound) {
114  // does not use the policy since the table is already stored.
116  my_cbdr=std::make_shared<CBDR_naiverotation<TSpace>>(cbdrFastSolver.solve());
117  }else {
119  my_cbdr=std::make_shared<CBDR_naiverotation<TSpace>>(cbdrSolver.solve(my_domain,nbijectiveGen, vecBijectiveSearchTree,*my_policy));
120  }
121  }
122 
123  void setPolicy(const std::shared_ptr<Policy<TSpace,HyperRectDomain< TSpace>,CBDR_naiverotation<TSpace>>>& newPolicy) {
124  // need to search again for the precomputed tables
125  my_policy = newPolicy;
126  my_cbdr = initCBDRVec();
127  }
128 
129 
130  TOutputValue operator()( const TInputValue & aInput ) const
131  {
132  return my_cbdr->operator()(aInput-my_center)+my_center;
133  }
134 
135  template<typename TImage>
136  TImage rotateImage(TImage img) const {
137  return my_cbdr->rotateImage(img);
138  }
139 
140  std::string tostring() const {
141  return {"CBDR"};
142  }
143 
145  inline TOutputValue center() const{return my_center;}
146 
147  public:
149  std::vector<BijectiveSearchTree> vecBijectiveSearchTree;
151 
152  size_t kmax;
156  int N;
157  double my_angle;
158  TOutputValue my_center;
159  std::shared_ptr<Policy<TSpace,HyperRectDomain< TSpace>,CBDR_naiverotation<TSpace>>> my_policy;
160  std::vector<CBDR_naiverotation<TSpace,TInputValue>> fastCBDRTable;
161  std::shared_ptr<CBDR_naiverotation<TSpace>> my_cbdr;
162 
163 
164  private:
166 
168  std::vector<CBDR_naiverotation<TSpace, TInputValue>> loadFastOptimisedTable(const std::string& fastTableName);
170  std::vector<CBDR_naiverotation<TSpace, TInputValue>> initFastPrecomputationTable(
171  const HyperRectDomain<TSpace>& points,
172  NBijectiveGenerator<TSpace>& nbijectiveVectors,
173  std::vector<BijectiveSearchTree>& vecBijectiveSearchTree);
174  };
175 
176 
177  template<typename TSpace, typename TInputValue, typename TOutputValue>
178  std::vector<CBDR_naiverotation<TSpace, TInputValue>> CBDR<TSpace, TInputValue, TOutputValue>::loadFastOptimisedTable(const std::string &fastTableName) {
179  auto vecTable = DGtal::functions::loadFastCBDRTable<DGtal::SpaceND< 2, DGtal::int32_t >>(fastTableName);
180  std::vector<CBDR_naiverotation<TSpace, TInputValue>> fastCBDRtab(vecTable.size());
181  std::transform(vecTable.begin(),vecTable.end(), fastCBDRtab.begin(), [](std::tuple<std::vector<DGtal::GAVector<DGtal::SpaceND< 2, DGtal::int32_t >>>,double,double>& x) {
182  return DGtal::CBDR_naiverotation<DGtal::SpaceND< 2, DGtal::int32_t >>(std::get<0>(x));
183  });
184  return fastCBDRtab;
185  }
186 
187  template<typename TSpace, typename TInputValue, typename TOutputValue>
189 
190  for(size_t nbReflec = 2 ; nbReflec <= nbReflections ; nbReflec+=2) {
191  BijectiveSearchTree vecBijNormals;
192  std::string tableName("CBDRTable_"+std::to_string(nbReflec)+"_"+std::to_string(kmax)+".txt");
193  // check that the file exists when usePrecomputedTable is set to true
194  if(usePrecomputedTable ) {
195  struct stat buffer;
196  bool exists = (stat (tableName.c_str(), &buffer) == 0);
197  if(!exists) {
198  usePrecomputedTable=false;
199  }
200  }
201 
202  if(usePrecomputedTable){
203  std::cout << "loading precomputed table ..."<<std::endl;
204  vecBijNormals = functions::loadBijectiveRotationTable<TSpace,TInputValue>(tableName,nbReflec,kmax);
205  } else {
206  std::cout << "does not use precomputed table"<<std::endl;
207  auto tableIntVecBijNormals = nbijectiveGen.n_bijectiveReflections_get_NormalVectorsAngles(nbReflec);
208  nbijectiveGen.writeBijectiveVectors("CBDRTable_"+std::to_string(nbReflec)+"_"+std::to_string(kmax)+".txt",tableIntVecBijNormals );
209  vecBijNormals = nbijectiveGen.vecBijNormals_index_2_GAVector(tableIntVecBijNormals);
210  }
211  vecBijectiveSearchTree.push_back(vecBijNormals);
212  }
213 
214  }
215 
216  template<typename TSpace, typename TInputValue, typename TOutputValue>
217  std::vector<CBDR_naiverotation<TSpace, TInputValue>> CBDR<TSpace, TInputValue, TOutputValue>::
219  std::vector<BijectiveSearchTree> &vecBijectiveSearchTree) {
220  std::vector<CBDR_naiverotation<TSpace, TInputValue>> fastCBDRtab;
221  auto Linf = std::make_shared<DGtal::LinfPolicy<TSpace,DGtal::HyperRectDomain<TSpace>,DGtal::CBDR_naiverotation<TSpace>>>();
222  auto LContinuity = std::make_shared<DGtal::LcontinuityPolicy<TSpace,DGtal::HyperRectDomain<TSpace>,DGtal::CBDR_naiverotation<TSpace>>>();
223 
224  std::string tablefilename ="CBDROptimisedTable_"+std::to_string(nbReflections)+"_"+std::to_string(kmax)+".txt";
225  std::ofstream file(tablefilename);
226  for(int alpha = 0 ; alpha < 91; ++alpha) {
227  double currentAngle = (alpha * M_PI) / 180.0;
228  CBDRSolver<TSpace,HyperRectDomain<TSpace>> cbdrsolv(currentAngle, my_center,kmax,N);
229  std::vector<Reflection<TSpace>> bestReflections =cbdrsolv.solve(points,nbijectiveVectors,
230  vecBijectiveSearchTree,*my_policy);
231 
232  CBDR_naiverotation<TSpace> currentRotation(bestReflections);
233  fastCBDRtab.push_back(currentRotation);
234 
235  // display vector of reflections
236  for(size_t i = 0 ; i<bestReflections.size() ; ++i) {
237  if(i<bestReflections.size()-1)
238  file << bestReflections[i].normalVector.my_gavec << ",";
239  else
240  file << bestReflections[i].normalVector.my_gavec;
241  }
242  file << ";"<<Linf->evaluate(points,currentRotation,currentAngle,my_center);
243  file << ";"<<LContinuity->evaluate(points,currentRotation,currentAngle,my_center);
244  file << std::endl;
245 
246  }
247  file.close();
248  std::cout << "table written !"<<std::endl;
249  fastTableFound=true;
250  return fastCBDRtab;
251  }
252 
253 
254 }
255 
256 
257 
258 
259 #endif //CBDR
260 #undef CBDR_RECURSES
261 #endif // else defined(CBDR_RECURSES)
BijectiveReflections solve()
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
void precompute()
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: CBDR solver, use a policy to choose the composition of digitized reflections that minimises an e...
Definition: CBDRSolver.h:120
std::vector< Reflection< TSpace > > solve(const TDomain &points, NBijectiveGenerator< TSpace > &nbijectiveVectors, std::vector< BijectiveSearchTree > &vecBijectiveSearchTree, const Policy< TSpace, TDomain, BijectiveReflections > &policy)
Definition: CBDRSolver.h:133
vec since the parameters are the vectors of digitized reflections
CBDR : Composition of Bijective Digitized Reflections,.
Definition: CBDR.h:57
TImage rotateImage(TImage img) const
Definition: CBDR.h:136
TOutputValue operator()(const TInputValue &aInput) const
Definition: CBDR.h:130
bool fastTableFound
Definition: CBDR.h:165
TOutputValue center() const
Definition: CBDR.h:145
void setPolicy(const std::shared_ptr< Policy< TSpace, HyperRectDomain< TSpace >, CBDR_naiverotation< TSpace >>> &newPolicy)
Definition: CBDR.h:123
TOutputValue my_center
Definition: CBDR.h:158
bool useFastTable
Definition: CBDR.h:155
std::vector< CBDR_naiverotation< TSpace, TInputValue > > fastCBDRTable
Definition: CBDR.h:160
size_t nbReflections
Definition: CBDR.h:153
std::string tostring() const
Definition: CBDR.h:140
std::shared_ptr< CBDR_naiverotation< TSpace > > initCBDRVec()
Definition: CBDR.h:88
BOOST_STATIC_ASSERT((TOutputValue::dimension==2))
std::vector< std::pair< std::vector< GAVector< TSpace > >, GAVector< TSpace > > > BijectiveSearchTree
Definition: CBDR.h:66
HyperRectDomain< TSpace > my_domain
Definition: CBDR.h:150
std::vector< BijectiveSearchTree > vecBijectiveSearchTree
Definition: CBDR.h:149
void set_angle(const double newAngle)
Definition: CBDR.h:111
void initcbdr_loadBijectiveReflectionSearchTree(const HyperRectDomain< TSpace > &my_domain)
Definition: CBDR.h:188
std::vector< CBDR_naiverotation< TSpace, TInputValue > > initFastPrecomputationTable(const HyperRectDomain< TSpace > &points, NBijectiveGenerator< TSpace > &nbijectiveVectors, std::vector< BijectiveSearchTree > &vecBijectiveSearchTree)
Definition: CBDR.h:218
NBijectiveGenerator< TSpace, TInputValue > nbijectiveGen
Definition: CBDR.h:148
std::shared_ptr< CBDR_naiverotation< TSpace > > my_cbdr
Definition: CBDR.h:161
bool usePrecomputedTable
Definition: CBDR.h:154
BOOST_STATIC_ASSERT((TSpace::dimension==2))
BOOST_CONCEPT_ASSERT((concepts::CSpace< TSpace >))
Checking concepts.
std::vector< CBDR_naiverotation< TSpace, TInputValue > > loadFastOptimisedTable(const std::string &fastTableName)
load fast optimised table in case the table is found
Definition: CBDR.h:178
double my_angle
number of sample rotation angle
Definition: CBDR.h:157
int N
Definition: CBDR.h:156
std::shared_ptr< Policy< TSpace, HyperRectDomain< TSpace >, CBDR_naiverotation< TSpace > > > my_policy
Definition: CBDR.h:159
BOOST_STATIC_ASSERT((TInputValue::dimension==2))
CBDR(const double theta, const typename TSpace::Point center, const size_t nbreflect, const size_t km, std::shared_ptr< Policy< TSpace, HyperRectDomain< TSpace >, CBDR_naiverotation< TSpace >>> policy, const bool precompute=true, const bool fast=true)
Definition: CBDR.h:79
size_t kmax
Definition: CBDR.h:152
Reflection< TSpace, TInputValue > DigitizedReflection
Definition: CBDR.h:65
Policy : ,.
Definition: Policy.h:55
Aim: Defines the concept describing a digital space, ie a cartesian product of integer lines.
Definition: CSpace.h:106
MyPointD Point
Definition: testClone2.cpp:383