Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LeeRichards.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/scoring/packstat/PackingScore.hh
11 ///
12 /// @brief
13 /// @author will sheffler
14 
15 
16 #ifndef INCLUDED_core_scoring_packstat_LeeRichards_hh
17 #define INCLUDED_core_scoring_packstat_LeeRichards_hh
18 
19 #include <core/id/AtomID.hh>
20 #include <core/id/AtomID_Map.hh>
21 
24 
25 #include <core/pose/Pose.hh>
26 
27 #include <numeric/xyzVector.hh>
28 // AUTO-REMOVED #include <utility/vector1.hh>
29 
30 #include <core/types.hh>
31 #include <utility/pointer/ReferenceCount.hh>
32 #include <utility/pointer/owning_ptr.hh>
33 
34 #include <numeric/constants.hh>
35 
36 #include <iostream>
37 
38 #include <core/kinematics/Jump.hh>
39 #include <utility/vector1.hh>
40 
41 
42 
43 namespace core {
44 namespace scoring {
45 namespace packstat {
46 
47  extern core::Size const N_PROBES;
48 
49  struct Event;
50  struct Circle;
51  struct trace;
52  struct Slice;
61 
62  typedef std::pair< core::id::AtomID,Real > Arc;
65 
67  virtual void accumulate_area( core::id::AtomID atom, core::Real area, bool buried ) = 0;
68  virtual void accumulate_dxdy( core::id::AtomID atom, core::Real dx, core::Real dy, bool buried ) = 0;
69  //dz// virtual void accumulate_dz ( core::id::AtomID atom, core::Real dz ) = 0;
70  };
72 
73  struct AreaAccumulator : public Accumulator {
75  virtual void accumulate_area( core::id::AtomID, core::Real area, bool buried ) {
76  // std::cerr << "accumulate_area " << area << std::endl;
77  // isnan is not a standard function (not known in VC) - use the function used in hbonding for nan detection instead.
78  //assert( !std::isnan(area) );
79  total_area += area;
80  if( buried ) buried_area += area;
81  }
83  //dz// void accumulate_dz( core::id::AtomID, core::Real ) {}
84  void reset() { total_area = 0.0; }
86  };
88 
89  struct LR_AtomData {
90  LR_AtomData() : area(0.0),dx(0.0),dy(0.0)/*,dz(0.0)*/ {}
91  Real area,dx,dy/*,dz*/;
92  };
93 
96  atom_map_.resize(spheres.size());
97  for( Size i = 1; i <= spheres.size(); ++i ) atom_map_.resize(i,1);
98  }
99  virtual void accumulate_area( core::id::AtomID id, core::Real area, bool ) {
100  atom_map_[id].area += area;
101  }
102  virtual void accumulate_dxdy( core::id::AtomID id, core::Real dx, core::Real dy, bool ) {
103  atom_map_[id].dx += dx;
104  atom_map_[id].dy += dy;
105  }
106  //dz// virtual void accumulate_dz( core::id::AtomID id, core::Real dz ) {
107  //dz// atom_map_[id].dz += dz;
108  //dz// }
110  };
112 
113  struct LR_MP_AtomData {
116  };
117 
120  virtual void accumulate_area( core::id::AtomID id, core::Real area, bool buried ) {
121  if( buried ) atom_map_[id].barea[pr_idx_] += area;
122  else atom_map_[id].area[pr_idx_] += area;
123  }
124  virtual void accumulate_dxdy( core::id::AtomID id, core::Real dx, core::Real dy, bool buried ) {
125  if( buried ) {
126  atom_map_[id].bdx[pr_idx_] += dx;
127  atom_map_[id].bdy[pr_idx_] += dy;
128  } else {
129  atom_map_[id].dx[pr_idx_] += dx;
130  atom_map_[id].dy[pr_idx_] += dy;
131  }
132  }
133  void set_pr_idx( Size pr_idx ) { pr_idx_ = pr_idx; }
135  void show( std::ostream & out );
140  };
142 
143 
146  atom_map_.resize(pd->spheres.size());
147  for( Size i = 1; i <= pd->spheres.size(); ++i ) atom_map_.resize(i,1);
148  }
149  // ~MultiProbePerSphereAccumulator() { std::cerr << "delete MultiProbePerSphereAccumulator" << std::endl;}
151  virtual void accumulate_area( core::id::AtomID id, core::Real area, bool ) {
152  atom_map_[id].area[pr_idx_] += area;
153  }
154  virtual void accumulate_dxdy( core::id::AtomID id, core::Real dx, core::Real dy, bool ) {
155  atom_map_[id].dx[pr_idx_] += dx;
156  atom_map_[id].dy[pr_idx_] += dy;
157  }
158  //dz// virtual void accumulate_dz( core::id::AtomID id, core::Real dz ) {
159  //dz// atom_map_[id].dz[pr_idx_] += dz;
160  //dz// }
161  void set_pr_idx( Size pr_idx ) {
162  // std::cerr << "set_pr_idx " << pr_idx << std::endl;
163  pr_idx_ = pr_idx;
164  }
165  void show( std::ostream & out ) {
166  Reals tot(31,0.0);
167  for( core::Size i = 1; i <= atom_map_.size(); ++i ) {
168  out << "ATOM_DAT " << i << " " << pd_->spheres[i].radius << " ";
169  for( core::Size j = 1; j <= 31; ++j ) {
170  tot[j] += atom_map_[core::id::AtomID(1,i)].area[j];
171  out << atom_map_[core::id::AtomID(1,i)].area[j] << " ";
172  }
173  out << " " << std::endl;
174  }
175  for( core::Size j = 1; j <= 31; ++j ) std::cerr << j << " " << tot[j] << std::endl;
176  }
181  };
183 
184  struct Point {
185  Point(core::Real _x,core::Real _y) : x(_x),y(_y) {}
187  };
188 
189  struct PointPair {
190  PointPair( core::Real x0, core::Real y0, core::Real x1, core::Real y1 ) : a(x0,y0), b(x1,y1) {}
192  };
193 
194  // struct Arc {
195  // Arc( Circle *_circle, core::Real _start_angle, core::Real _end_angle, bool _ccw )
196  // : circle(_circle), start_angle(_start_angle), end_angle(_end_angle), ccw(_ccw)
197  // {}
198  // Circle *circle;
199  // core::Real start_angle,end_angle;
200  // bool ccw;
201  // };
202 
203  struct Circle {
205  : x(_x), y(_y), r(_r), drdz(_drdz), dada(_dada), tcw(NULL), tccw(NULL), atom(_atom_id) {}
206 
207  PointPair
208  overlap(
209  Circle * other
210  );
211 
212  inline
213  bool
215  Circle * other
216  ) {
217  return sqrt((x-other->x)*(x-other->x) + (y-other->y)*(y-other->y)) < r + other->r;
218  }
219 
220  inline
221  bool
223  core::Real _x,
224  core::Real _y
225  ) {
226  return (x-_x)*(x-_x) + (y-_y)*(y-_y) <= r*r;
227  }
228 
229  core::Real x,y,r,drdz,dada; // data = d(area)/d(arc angle)
232 
233 
234  };
235 
236  std::ostream & operator<< ( std::ostream & out, Circle & circle );
237 
238  enum EventType {
240  };
241 
242  struct Event {
244  core::Real _x,
245  core::Real _y,
246  EventType _kind,
247  Circle *_circle,
248  Circle *_ccw=NULL
249  ) : x(_x),
250  y(_y),
251  cw_angle(-1),
252  ccw_angle(-1),
253  kind(_kind),
254  circle(_circle),
255  ccw(_ccw),
256  trace_(NULL)
257  {
258  // std::cerr << "Event " << _kind << " " << circle << std::endl;
259  if( kind == ISECT ) {
260  cw_angle = acos(std::max(-1.0,std::min(1.0,(x-circle->x)/circle->r)));
261  ccw_angle = acos(std::max(-1.0,std::min(1.0,(x-ccw->x)/ccw->r)));
262  // isnan is not a standard function (not known in VC) - use the function used in hbonding for nan detection instead.
263  //assert( !std::isnan(cw_angle) );
264  //assert( !std::isnan(ccw_angle) );
265  if( y > circle->y ) cw_angle = 2.0*numeric::constants::d::pi-cw_angle;
266  if( y > ccw->y ) ccw_angle = 2.0*numeric::constants::d::pi-ccw_angle;
267  }
268  }
269 
270  inline
271  int
273  Event *other
274  ) {
275  if( x == other->x ) return 0;
276  if( x < other->x ) return -1;
277  return 1;
278  }
279 
284 
285  };
286 
287  struct Slice {
288 
289  Slice( AccumulatorOP _accum, Real thickness, bool internal_allowed )
290  : accum_(_accum), thickness_(thickness), internal_allowed_(internal_allowed) {}
291 
292  virtual ~Slice();
293 
294  void compute() {
295  compute_events();
296  compute_surface();
298  }
299 
300  inline void add_circle( Circle *circ ) {
301  circles_.push_back(circ);
302  }
303 
310 
311  private:
312 
313  void compute_surface();
314  void compute_events();
315  void compute_derivatives();
316 
317  };
318 
319  struct trace {
320 
322  AccumulatorOP _accum,
323  Event *e,
324  bool _ccw,
325  Circle *_circle,
326  core::Real _angle,
327  trace *other_trace
328  ) : accum_(_accum), ccw(_ccw), start_(e),
329  circle(_circle), angle(_angle), next_trace_(NULL)
330  {
331  e->trace_ = this;
332  if(ccw) {
333  circle->tccw = this;
334  if( other_trace ) other_trace->next_trace_ = this;
335  } else {
336  circle->tcw = this;
337  if( angle==0.0 ) angle = 2*numeric::constants::d::pi;
338  if( other_trace ) next_trace_ = other_trace;
339  }
340  }
341 
342  inline void next_circle( Circle *newcircle, core::Real end_angle, core::Real begin_angle )
343  {
344  finish_arc(end_angle);
345  if(ccw) circle->tccw = NULL;
346  else circle->tcw = NULL;
347  circle = newcircle;
348  angle = begin_angle;
349  if(ccw) circle->tccw = this;
350  else circle->tcw = this;
351  }
352 
353  inline void end( trace *merge_trace, core::Real end_angle = numeric::constants::d::pi ) {
354  finish_arc(end_angle);
355  if(ccw) next_trace_ = merge_trace;
356  else merge_trace->next_trace_ = this;
357  }
358 
360  if( stop == this || next_trace_==NULL ) return start_;
361  Event *best = next_trace_->get_first(stop);
362  if( start_->cmp(best) > 0 ) return start_;
363  else return best;
364  }
365 
366  void set_first( trace *stop, Event *e ){
367  start_ = e;
368  if( stop == this || next_trace_==NULL ) return;
369  next_trace_->set_first( stop, e );
370  next_trace_ = NULL;
371  }
372 
374  bool ccw;
380 
381 private:
382 
383  inline void finish_arc( core::Real end_angle ) {
384  // assert( fabs(angle-end_angle) * circle->dada < 1.0 );
385  // accum_->accumulate_area( circle->atom, fabs(angle-end_angle) * circle->dada );
386  arcs_.push_back( Arc( circle->atom, fabs(angle-end_angle) * circle->dada ) );
387  //dz//accum_->accumulate_dz( circle->atom, fabs(angle-end_angle) * circle->drdz );
388  // slice->arcs_.push_back(new Arc(circle,angle,end_angle,ccw));
389  }
390 
391  };
392 
393  struct Array2D {
394 
395  void init( core::Size Nx, core::Size Ny ) {
396  Nx_ = Nx;
397  Ny_ = Ny;
398  array_ = new Circles[Nx*Ny];
399  }
400 
401  ~Array2D() { if(array_) delete[] array_; }
402 
403  inline Circles &
405  assert( i < Nx_ );
406  assert( j < Ny_ );
407  // std::cerr << Ny_*i+j << " " << Nx_*Ny_ << std::endl;
408  return array_[Ny_*i+j];
409  }
410 
414 
415  };
416 
417  struct Octree2D {
418  Octree2D( Circles & items ) {
419  mnx_=9e9,mny_=9e9,mxx_=-9e9,mxy_=-9e9,mxr_=1.0;
420  for( CircleIter i = items.begin(); i != items.end(); ++i ){
421  Circle* item(*i);
422  if( item->r > mxr_ ) mxr_ = item->r;
423  if( item->x > mxx_ ) mxx_ = item->x;
424  if( item->x < mnx_ ) mnx_ = item->x;
425  if( item->y > mxy_ ) mxy_ = item->y;
426  if( item->y < mny_ ) mny_ = item->y;
427  }
428  // std::cerr << mnx_ << " " << mxx_ << " " << mny_ << " " << mxy_ << " " << mxr_ << std::endl;
429  Xdim_ = get_i(mxx_)+1;
430  Ydim_ = get_j(mxy_)+1;
431  cubes.init( Xdim_+1, Ydim_+1 );
432  // std::cerr << "Octree2D size " << Xdim_+1 << " " << Ydim_+1 << " " << items.size() << " " << mxx_ << " " << mnx_ << " " << mxr_ << std::endl;
433  for( CircleIter i = items.begin(); i != items.end(); ++i ){
434  Circle* item(*i);
435  Circles & cube(get_cube(item));
436  for( CircleIter j = cube.begin(); j != cube.end(); ++j ) {
437  Circle *item2(*j);
438  if( item->x == item2->x && item->y == item2->y && item->r == item2->r ) goto SKIPCIRC;
439  }
440  cube.push_back(item);
441  SKIPCIRC:;
442  }
443  }
444 
445  inline core::Size get_i(core::Real x) const { return Size((x-mnx_)/mxr_); }
446  inline core::Size get_j(core::Real y) const { return Size((y-mny_)/mxr_); }
447 
448  inline Circles & get_cube( Circle * a ) { return cubes(get_i(a->x),get_j(a->y)); }
449  // inline Circles & get_cube( Event* e ) { return cubes(get_i(e->x),get_j(e->y)); }
450 
451  bool
453  Circle *exclude1 = NULL, Circle *exclude2 = NULL )
454  {
455  int I2 = get_i(x);
456  int J2 = get_j(y);
457  for( int i2 = std::max(I2-extend,0); i2 <= std::min(I2+extend,(int)Xdim_); ++i2 ) {
458  for( int j2 = std::max(J2-extend,0); j2 <= std::min(J2+extend,(int)Ydim_); ++j2 ) {
459  for( CircleIter ja2 = cubes(i2,j2).begin(); ja2 != cubes(i2,j2).end(); ja2++ ) {
460  Circle *circle(*ja2);
461  if( circle == exclude1 || circle == exclude2 ) continue;
462  if( circle->contains(x,y) ){
463  return true;
464  }
465  }
466  }
467  }
468  return false;
469  }
470 
474  };
475 
476  struct LeeRichards {
477 
478  LeeRichards(
479  PosePackDataOP pd,
480  AccumulatorOP accum,
481  core::Real spacing = 0.33,
482  core::Real probe_radius = 1.4,
483  bool csa = false,
485  );
486 
487  LeeRichards(
488  core::pose::Pose & pose,
489  AccumulatorOP accum,
490  core::Real spacing = 0.33,
491  core::Real probe_radius = 1.4,
492  bool csa = false,
494  );
495 
497  for( SliceIter i = slices_.begin(); i != slices_.end(); ++i ) delete *i;
498  }
499 
500  inline core::Size
502  if( r >= slice_coords_.back() ) return 0;
503  return find_first_slice(r,1,slice_coords_.size());
504  }
505 
506 
508 
509  private:
510 
512 
516 
517  inline core::Size
519  // std::cerr << "find_first_slice " << r << " " << beg << " " << end << std::endl;
520  if( end - beg < 5 ) { // once close, just iterate up
521  while( slice_coords_[beg] <= r ) ++beg;
522  return beg;
523  }
524  core::Size splt = (end-beg)/2+beg;
525  Real t = slice_coords_[splt];
526  if( t <= r ) return find_first_slice(r,splt+1, end);
527  else return find_first_slice(r, beg ,splt);
528  }
529 
530  void
531  compute(
532  Spheres & spheres,
533  AccumulatorOP accum,
534  core::Real spacing = 0.33,
535  core::Real probe_radius = 1.4,
536  bool csa = false,
538  );
539 
540  };
541 
542  core::Real
544  PosePackDataOP pd,
545  core::Real slicesize,
547  );
548 
549  core::Real
551  Real & buried_area_out,
552  PosePackDataOP pd,
553  Real slicesize,
554  Real pr,
555  bool csa = false,
557  );
558 
559  core::Real
561  PosePackDataOP pd,
562  Real slicesize,
563  Real pr,
564  bool csa = false,
566  );
567 
568  XYZs
570  PosePackDataOP pd,
571  Real slicesize,
572  Real pr,
573  bool csa = false//,
574  // numeric::xyzVector<Real> plane = numeric::xyzVector<Real>(0,0,1)
575  );
576 
577  XYZs
579  PosePackDataOP pd,
580  Real slicesize,
581  Real pr,
582  bool csa = false,
583  Size max_num = 10
584  // numeric::xyzVector<Real> plane = numeric::xyzVector<Real>(0,0,1)
585  );
586 
587 
588 
589 } // namespace packstat
590 } // namespace scoring
591 } // namespace core
592 
593 
594 
595 
596 #endif