[73298b]: contrib / gel / vdgl / vdgl_interpolator_linear.cxx Maximize Restore History

Download this file

vdgl_interpolator_linear.cxx    191 lines (147 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// This is gel/vdgl/vdgl_interpolator_linear.cxx
#ifdef VCL_NEEDS_PRAGMA_INTERFACE
#pragma implementation
#endif
#include "vdgl_interpolator_linear.h"
#include <vnl/vnl_math.h>
#include <vdgl/vdgl_edgel.h>
#include <vdgl/vdgl_edgel_chain.h>
#include <vsol/vsol_point_2d.h>
#include <vcl_cassert.h>
vdgl_interpolator_linear::vdgl_interpolator_linear( vdgl_edgel_chain_sptr chain)
: vdgl_interpolator( chain)
{
recompute_all();
}
vdgl_interpolator_linear::~vdgl_interpolator_linear()
{
}
vsol_point_2d_sptr vdgl_interpolator_linear::closest_point_on_curve ( vsol_point_2d_sptr p )
{
double min_distance = 1e6;
int index = -1;
for ( int i=0; i< chain_->size(); i++)
{
vgl_point_2d<double> curve_point = chain_->edgel(i).get_pt();
double d = p->distance ( vsol_point_2d ( curve_point ) );
if ( d < min_distance )
{
index = i;
min_distance = d;
}
}
if ( index == -1 )
{
return NULL;
}
return new vsol_point_2d ( chain_->edgel(index).get_pt() );
}
double vdgl_interpolator_linear::get_x( const double index)
{
assert(index >= 0 && index < chain_->size());
int a= int( index); // round down
double d= index- a;
double ae = chain_->edgel( a).get_x();
if (d==0) return ae; // exactly at an edgel
double be = chain_->edgel( a+1).get_x();
return be*d+ae*(1-d);
}
double vdgl_interpolator_linear::get_y( const double index)
{
assert(index >= 0 && index < chain_->size());
int a= int( index); // round down
double d= index- a;
double ae = chain_->edgel( a).get_y();
if (d==0) return ae; // exactly at an edgel
double be = chain_->edgel( a+1).get_y();
return be*d+ae*(1-d);
}
double vdgl_interpolator_linear::get_theta( const double index)
{
assert(index >= 0 && index < chain_->size());
int a= int( index); // round down
double d= index- a;
double ae = chain_->edgel( a).get_theta();
if (d==0) return ae; // exactly at an edgel
double be = chain_->edgel( a+1).get_theta();
return be*d+ae*(1-d);
}
double vdgl_interpolator_linear::get_curvature( const double index)
{
int a= int( index); // round down
if ( a == index ) // if exactly at an edgel, curvature is undefined
return vnl_math::maxdouble;
else
return 0; // curvature of straight line segments is always zero
}
double vdgl_interpolator_linear::get_length()
{
// length is cached (because it's expensive to compute)
if ( older( chain_.ptr()))
recompute_all();
return lengthcache_;
}
double vdgl_interpolator_linear::get_min_x()
{
if ( older( chain_.ptr()))
recompute_all();
return minxcache_;
}
double vdgl_interpolator_linear::get_max_x()
{
if ( older( chain_.ptr()))
recompute_all();
return maxxcache_;
}
double vdgl_interpolator_linear::get_min_y()
{
if ( older( chain_.ptr()))
recompute_all();
return minycache_;
}
double vdgl_interpolator_linear::get_max_y()
{
if ( older( chain_.ptr()))
recompute_all();
return maxycache_;
}
// cache maintenance
void vdgl_interpolator_linear::recompute_all()
{
recompute_length();
recompute_bbox();
touch();
}
void vdgl_interpolator_linear::recompute_length()
{
lengthcache_= 0;
for ( int i=0; i< chain_->size(); i++)
{
int j = i==0 ? chain_->size()-1 : i-1;
vgl_point_2d<double> p1= chain_->edgel(j).get_pt();
vgl_point_2d<double> p2= chain_->edgel(i).get_pt();
// NOTE THERE IS A PROBLEM HERE UNDER WINDOWS
// WHICH I WILL HAVE TO FIX AT SOME POINT
// Maybe solved now with the use of vgl_vector_2d<double> ? - PVr
lengthcache_ += length(p2-p1);
}
}
void vdgl_interpolator_linear::recompute_bbox()
{
if ( chain_->size() == 0 )
{
minxcache_= maxxcache_= minycache_= maxycache_ = 0.0;
return;
}
minxcache_= chain_->edgel( 0).get_x();
maxxcache_= chain_->edgel( 0).get_x();
minycache_= chain_->edgel( 0).get_y();
maxycache_= chain_->edgel( 0).get_y();
for ( int i=1; i< chain_->size(); i++)
{
if ( chain_->edgel( i).get_x()< minxcache_) minxcache_= chain_->edgel( i).get_x();
if ( chain_->edgel( i).get_x()> maxxcache_) maxxcache_= chain_->edgel( i).get_x();
if ( chain_->edgel( i).get_y()< minycache_) minycache_= chain_->edgel( i).get_y();
if ( chain_->edgel( i).get_y()> maxycache_) maxycache_= chain_->edgel( i).get_y();
}
}