Clipper
nxmap.h
1 
4 //C Copyright (C) 2000-2006 Kevin Cowtan and University of York
5 //L
6 //L This library is free software and is distributed under the terms
7 //L and conditions of version 2.1 of the GNU Lesser General Public
8 //L Licence (LGPL) with the following additional clause:
9 //L
10 //L `You may also combine or link a "work that uses the Library" to
11 //L produce a work containing portions of the Library, and distribute
12 //L that work under terms of your choice, provided that you give
13 //L prominent notice with each copy of the work that the specified
14 //L version of the Library is used in it, and that you include or
15 //L provide public access to the complete corresponding
16 //L machine-readable source code for the Library including whatever
17 //L changes were used in the work. (i.e. If you make changes to the
18 //L Library you must distribute those, but you do not need to
19 //L distribute source or object code to those portions of the work
20 //L not covered by this licence.)'
21 //L
22 //L Note that this clause grants an additional right and does not impose
23 //L any additional restriction, and so does not affect compatibility
24 //L with the GNU General Public Licence (GPL). If you wish to negotiate
25 //L other terms, please contact the maintainer.
26 //L
27 //L You can redistribute it and/or modify the library under the terms of
28 //L the GNU Lesser General Public License as published by the Free Software
29 //L Foundation; either version 2.1 of the License, or (at your option) any
30 //L later version.
31 //L
32 //L This library is distributed in the hope that it will be useful, but
33 //L WITHOUT ANY WARRANTY; without even the implied warranty of
34 //L MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 //L Lesser General Public License for more details.
36 //L
37 //L You should have received a copy of the CCP4 licence and/or GNU
38 //L Lesser General Public License along with this library; if not, write
39 //L to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
40 //L The GNU Lesser General Public can also be obtained by writing to the
41 //L Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 //L MA 02111-1307 USA
43 
44 
45 #ifndef CLIPPER_NXMAP
46 #define CLIPPER_NXMAP
47 
48 
49 #include "derivs.h"
50 
51 
52 namespace clipper
53 {
54 
56 
66  class NXmap_base
67  {
68  public:
70  bool is_null() const;
71 
73  const Grid& grid() const { return grid_; }
75  const RTop<>& operator_orth_grid() const { return rt_orth_grid; }
77  const RTop<>& operator_grid_orth() const { return rt_grid_orth; }
79 
81  inline Coord_orth coord_orth( const Coord_map& cm ) const
82  { return Coord_orth( rt_grid_orth*cm ); }
84 
86  inline Coord_map coord_map( const Coord_orth& co ) const
87  { return Coord_map ( rt_orth_grid*co ); }
88 
90  bool in_map( const Coord_grid& pos ) const { return grid_.in_grid( pos ); }
92  template<class I> bool in_map( const Coord_map& cm ) const;
93 
95  int multiplicity( const Coord_grid& ) const { return 1; }
96 
98 
103  {
104  public:
106  inline const NXmap_base& base_nxmap() const { return *map_; }
108  inline const int& index() const { return index_; }
110  inline bool last() const { return ( index_ >= map_->grid_.size() ); }
111  protected:
113  const NXmap_base* map_;
115  int index_;
116  };
117 
119 
130  {
131  public:
135  explicit Map_reference_index( const NXmap_base& map )
136  { map_ = &map; index_ = 0; }
138  Map_reference_index( const NXmap_base& map, const Coord_grid& pos )
139  { map_ = &map; index_ = map_->grid_.index( pos ); }
141  inline Coord_grid coord() const
142  { return map_->grid_.deindex(index_); }
144  inline const Coord_orth coord_orth() const
145  { return map_->coord_orth( coord().coord_map() ); }
148  { index_ = map_->grid_.index( pos ); return *this; }
150  inline Map_reference_index& next() { index_++; return *this; }
152  /* Use for e.g. peak search. Valid for -1 <= du/dv/dw <= 1 only.
153  \param du/dv/dw Coordinate offset. \return Map index. */
154  inline int index_offset(const int& du,const int& dv,const int& dw) const {
155  return index_ + du*map_->du + dv*map_->dv + dw*map_->dw;
156  }
157  // inherited functions listed for documentation purposes
158  //-- const NXmap_base& base_nxmap() const;
159  //-- const int& index() const;
160  //-- bool last() const;
161  };
162 
164 
175  {
176  public:
180  explicit Map_reference_coord( const NXmap_base& map )
181  { map_ = &map; }
183  Map_reference_coord( const NXmap_base& map, const Coord_grid& pos )
184  { map_ = &map; set_coord( pos ); }
186  inline Coord_grid coord() const { return pos_; }
188  inline const Coord_orth coord_orth() const
189  { return map_->coord_orth( coord().coord_map() ); }
192  { pos_ = pos; index_ = map_->grid_.index( pos_ ); return *this; }
194 
196  index_++;
198  return *this;
199  }
200  // Increment u,v,w
201  inline Map_reference_coord& next_u() { pos_.u()++; index_ += map_->du; return *this; }
202  inline Map_reference_coord& next_v() { pos_.v()++; index_ += map_->dv; return *this; }
203  inline Map_reference_coord& next_w() { pos_.w()++; index_ += map_->dw; return *this; }
204  inline Map_reference_coord& prev_u() { pos_.u()--; index_ -= map_->du; return *this; }
205  inline Map_reference_coord& prev_v() { pos_.v()--; index_ -= map_->dv; return *this; }
206  inline Map_reference_coord& prev_w() { pos_.w()--; index_ -= map_->dw; return *this; }
207  inline Map_reference_coord& operator =( const Coord_grid& pos )
209  { return set_coord( pos ); }
210  // inherited functions listed for documentation purposes
211  //-- const NXmap_base& base_nxmap() const;
212  //-- const int& index() const;
213  //-- bool last() const;
214  protected:
217  };
218 
220  Map_reference_index first() const { return Map_reference_index( *this ); }
223 
224  protected:
228  int du, dv, dw;
229 
231  NXmap_base();
233  void init( const Grid& grid, const RTop<>& rt );
235  void init( const Cell& cell, const Grid_sampling& grid, const Grid_range& grid_extent );
236 
237  friend class NXmap_base::Map_reference_base;
238  friend class NXmap_base::Map_reference_index;
239  friend class NXmap_base::Map_reference_coord;
240  };
241 
242 
243 
244 
246 
260  template<class T> class NXmap : public NXmap_base
261  {
262  public:
264  NXmap() {}
266  NXmap( const Grid& grid, const RTop<>& rt );
268  NXmap( const Cell& cell, const Grid_sampling& grid, const Grid_range& grid_extent );
270  void init( const Grid& grid, const RTop<>& rt );
272  void init( const Cell& cell, const Grid_sampling& grid, const Grid_range& grid_extent );
273 
275  inline const T& operator[] (const NXmap_base::Map_reference_index i) const
276  { return list[i.index()]; }
279  { return list[i.index()]; }
280 
282  inline const T& operator[] (const NXmap_base::Map_reference_coord i) const
283  { return list[i.index()]; }
286  { return list[i.index()]; }
287 
289  inline const T& get_data( const Coord_grid& pos ) const
290  { return list[ grid_.index( pos ) ]; }
292  inline void set_data( const Coord_grid& pos, const T& val )
293  { list[ grid_.index( pos ) ] = val; }
294 
296  template<class I> T interp( const Coord_map& pos ) const;
298  template<class I> void interp_grad( const Coord_map& pos, T& val, Grad_map<T>& grad ) const;
300  template<class I> void interp_curv( const Coord_map& pos, T& val, Grad_map<T>& grad, Curv_map<T>& curv ) const;
301 
302  // inherited functions listed for documentation purposes
303  //-- const Grid& grid() const;
304  //-- const RTop<> operator_orth_grid() const;
305  //-- const RTop<> operator_grid_orth() const;
306  //-- const Coord_orth coord_orth( const Coord_map& cg ) const;
307  //-- const Coord_map coord_map ( const Coord_orth& co ) const;
308  //-- const Map_reference_index first();
309  //-- const Map_reference_coord first_coord();
310 
312  const T& operator =( const T& value );
314  const NXmap<T>& operator +=( const NXmap<T>& other );
316  const NXmap<T>& operator -=( const NXmap<T>& other );
317 
318  private:
319  std::vector<T> list;
320  };
321 
322 
323 
324  // template fucntion definitions
325 
330  template<class I> bool NXmap_base::in_map( const Coord_map& cm ) const
331  { return I::can_interp( *this, cm ); }
332 
338  template<class T> NXmap<T>::NXmap( const Grid& grid, const RTop<>& rt )
339  { init( grid, rt ); }
340 
348  template<class T> NXmap<T>::NXmap( const Cell& cell, const Grid_sampling& grid, const Grid_range& grid_extent )
349  { init( cell, grid, grid_extent ); }
350 
356  template<class T> void NXmap<T>::init( const Grid& grid, const RTop<>& rt )
357  { NXmap_base::init( grid, rt ); list.resize( grid.size() ); }
358 
366  template<class T> void NXmap<T>::init( const Cell& cell, const Grid_sampling& grid, const Grid_range& grid_extent )
367  { NXmap_base::init( cell, grid, grid_extent ); list.resize( grid_extent.size() ); }
368 
369 
376  template<class T> template<class I> T NXmap<T>::interp( const Coord_map& pos ) const
377  {
378  T val;
379  I::interp( *this, pos, val );
380  return val;
381  }
382 
383 
393  template<class T> template<class I> void NXmap<T>::interp_grad( const Coord_map& pos, T& val, Grad_map<T>& grad ) const
394  {
395  I::interp_grad( *this, pos, val, grad );
396  }
397 
398 
408  template<class T> template<class I> void NXmap<T>::interp_curv( const Coord_map& pos, T& val, Grad_map<T>& grad, Curv_map<T>& curv ) const
409  {
410  I::interp_curv( *this, pos, val, grad, curv );
411  }
412 
413 
416  template<class T> const T& NXmap<T>::operator =( const T& value )
417  {
418  // copy value into map
420  for ( im = first(); !im.last(); im.next() ) list[im.index()] = value;
421  return value;
422  }
423 
424 
426  template<class T> const NXmap<T>& NXmap<T>::operator +=( const NXmap<T>& other )
427  {
428  if ( grid() != other.grid() )
429  Message::message( Message_fatal( "NXmap: map mismatch in +=" ) );
431  for ( im = first(); !im.last(); im.next() ) list[im.index()] += other[im];
432  return (*this);
433  }
434 
436  template<class T> const NXmap<T>& NXmap<T>::operator -=( const NXmap<T>& other )
437  {
438  if ( grid() != other.grid() )
439  Message::message( Message_fatal( "NXmap: map mismatch in -=" ) );
441  for ( im = first(); !im.last(); im.next() ) list[im.index()] -= other[im];
442  return (*this);
443  }
444 
445 
446 } // namespace clipper
447 
448 #endif