simplified coord transformation interface.

This commit is contained in:
Artem Pavlenko 2006-10-09 09:52:15 +00:00
parent 18ef498140
commit e2d645ad80
5 changed files with 52 additions and 118 deletions

View file

@ -34,21 +34,21 @@ namespace mapnik {
template <typename Transform,typename Geometry> template <typename Transform,typename Geometry>
struct MAPNIK_DECL coord_transform struct MAPNIK_DECL coord_transform
{ {
coord_transform(Transform const& t,Geometry& geom) coord_transform(Transform const& t, Geometry& geom)
: t_(t), geom_(geom) {} : t_(t), geom_(geom) {}
unsigned vertex(double *x , double *y) const unsigned vertex(double *x , double *y) const
{ {
unsigned command = geom_.vertex(x,y); unsigned command = geom_.vertex(x,y);
*x = t_.forward_x_(x); t_.forward(x,y);
*y = t_.forward_y_(y);
return command; return command;
} }
void rewind (unsigned pos) void rewind (unsigned pos)
{ {
geom_.rewind(pos); geom_.rewind(pos);
} }
private: private:
Transform const& t_; Transform const& t_;
Geometry& geom_; Geometry& geom_;
@ -60,13 +60,13 @@ namespace mapnik {
int width; int width;
int height; int height;
double scale_; double scale_;
Envelope<double> extent; Envelope<double> extent_;
public: public:
CoordTransform(int width,int height,const Envelope<double>& extent) CoordTransform(int width,int height,const Envelope<double>& extent)
:width(width),height(height),extent(extent) :width(width),height(height),extent_(extent)
{ {
double sx=((double)width)/extent.width(); double sx=((double)width)/extent_.width();
double sy=((double)height)/extent.height(); double sy=((double)height)/extent_.height();
scale_=std::min(sx,sy); scale_=std::min(sx,sy);
} }
@ -74,48 +74,29 @@ namespace mapnik {
{ {
return scale_; return scale_;
} }
inline void forward_x(double* x) const inline void forward(double * x, double * y) const
{ {
*x = (*x - extent.minx() ) * scale_;
*x = (*x - extent_.minx()) * scale_;
*y = (extent_.maxy() - *y) * scale_;
} }
inline void forward_y(double* y) const inline void backward(double * x, double * y) const
{ {
*y = (extent.maxy() - *y) * scale_; *x = extent_.minx() + *x/scale_;
*y = extent_.maxy() - *y/scale_;
} }
inline double forward_x_(double* x) const
{
return (*x - extent.minx() ) * scale_;
}
inline double forward_y_(double* y) const
{
return (extent.maxy() - *y) * scale_;
}
inline void backward_x(double* x) const
{
*x = extent.minx() + *x/scale_;
}
inline void backward_y(double* y) const
{
*y = extent.maxy() - *y/scale_;
}
inline coord2d& forward(coord2d& c) const inline coord2d& forward(coord2d& c) const
{ {
forward_x(&c.x); forward(&c.x,&c.y);
forward_y(&c.y);
return c; return c;
} }
inline coord2d& backward(coord2d& c) const inline coord2d& backward(coord2d& c) const
{ {
backward_x(&c.x); backward(&c.x,&c.y);
backward_y(&c.y);
return c; return c;
} }
@ -125,10 +106,8 @@ namespace mapnik {
double y0 = e.miny(); double y0 = e.miny();
double x1 = e.maxx(); double x1 = e.maxx();
double y1 = e.maxy(); double y1 = e.maxy();
forward_x(&x0); forward(&x0,&y0);
forward_y(&y0); forward(&x1,&y1);
forward_x(&x1);
forward_y(&y1);
return Envelope<double>(x0,y0,x1,y1); return Envelope<double>(x0,y0,x1,y1);
} }
@ -138,11 +117,8 @@ namespace mapnik {
double y0 = e.miny(); double y0 = e.miny();
double x1 = e.maxx(); double x1 = e.maxx();
double y1 = e.maxy(); double y1 = e.maxy();
backward_x(&x0); backward(&x0,&y0);
backward_y(&y0); backward(&x1,&y1);
backward_x(&x1);
backward_y(&y1);
return Envelope<double>(x0,y0,x1,y1); return Envelope<double>(x0,y0,x1,y1);
} }

View file

@ -31,7 +31,6 @@
#include <boost/utility.hpp> #include <boost/utility.hpp>
// mapnik // mapnik
#include <mapnik/vertex_vector.hpp> #include <mapnik/vertex_vector.hpp>
#include <mapnik/vertex_transform.hpp>
#include <mapnik/ctrans.hpp> #include <mapnik/ctrans.hpp>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
@ -83,7 +82,6 @@ namespace mapnik {
virtual void label_position(double *x, double *y) const=0; virtual void label_position(double *x, double *y) const=0;
virtual void move_to(value_type x,value_type y)=0; virtual void move_to(value_type x,value_type y)=0;
virtual void line_to(value_type x,value_type y)=0; virtual void line_to(value_type x,value_type y)=0;
virtual void transform(const mapnik::CoordTransform& t)=0;
virtual unsigned num_points() const = 0; virtual unsigned num_points() const = 0;
virtual unsigned vertex(double* x, double* y)=0; virtual unsigned vertex(double* x, double* y)=0;
virtual void rewind(unsigned )=0; virtual void rewind(unsigned )=0;
@ -121,13 +119,7 @@ namespace mapnik {
} }
void line_to(value_type ,value_type ) {} void line_to(value_type ,value_type ) {}
void transform(const mapnik::CoordTransform& t)
{
t.forward_x(&pt_.x);
t.forward_y(&pt_.y);
}
unsigned num_points() const unsigned num_points() const
{ {
return 1; return 1;
@ -193,7 +185,6 @@ namespace mapnik {
unsigned i,j; unsigned i,j;
for (i = size-1,j = 0; j < size; i = j, ++j) for (i = size-1,j = 0; j < size; i = j, ++j)
{ {
cont_.get_vertex(i,&x0,&y0); cont_.get_vertex(i,&x0,&y0);
cont_.get_vertex(j,&x1,&y1); cont_.get_vertex(j,&x1,&y1);
ai = x0 * y1 - x1 * y0; ai = x0 * y1 - x1 * y0;
@ -221,15 +212,6 @@ namespace mapnik {
cont_.push_back(x,y,SEG_MOVETO); cont_.push_back(x,y,SEG_MOVETO);
} }
void transform(mapnik::CoordTransform const& t)
{
unsigned size = cont_.size();
for (unsigned pos=0; pos < size; ++pos)
{
cont_.transform_at(pos,t);
}
}
unsigned num_points() const unsigned num_points() const
{ {
return cont_.size(); return cont_.size();
@ -339,16 +321,7 @@ namespace mapnik {
{ {
cont_.push_back(x,y,SEG_MOVETO); cont_.push_back(x,y,SEG_MOVETO);
} }
void transform(mapnik::CoordTransform const& t)
{
unsigned size = cont_.size();
for (unsigned pos=0; pos < size; ++pos)
{
cont_.transform_at(pos,t);
}
}
unsigned num_points() const unsigned num_points() const
{ {
return cont_.size(); return cont_.size();

View file

@ -33,34 +33,34 @@ namespace mapnik
template <typename T0 ,typename T1,int shift=8> template <typename T0 ,typename T1,int shift=8>
struct Shift struct Shift
{ {
typedef T0 value_type; typedef T0 value_type;
typedef T1 return_type; typedef T1 return_type;
static return_type apply(value_type val) static return_type apply(value_type val)
{ {
return static_cast<return_type>(val*(1<<shift)); return static_cast<return_type>(val*(1<<shift));
} }
}; };
template <typename T0,typename T1> template <typename T0,typename T1>
struct Shift<T0,T1,0> struct Shift<T0,T1,0>
{ {
typedef T0 value_type; typedef T0 value_type;
typedef T1 return_type; typedef T1 return_type;
static return_type apply(value_type val) static return_type apply(value_type val)
{ {
return static_cast<return_type>(val); return static_cast<return_type>(val);
} }
}; };
template <typename T> template <typename T>
struct Shift<T,T,0> struct Shift<T,T,0>
{ {
typedef T value_type; typedef T value_type;
typedef T return_type; typedef T return_type;
static T& apply(T& val) static T& apply(T& val)
{ {
return val; return val;
} }
}; };
typedef Shift<double,double,0> NO_SHIFT; typedef Shift<double,double,0> NO_SHIFT;

View file

@ -110,19 +110,11 @@ namespace mapnik
return commands_[block] [pos & block_mask]; return commands_[block] [pos & block_mask];
} }
void transform_at(unsigned pos,const CoordTransform& t)
{
if (pos >= pos_) return;
unsigned block = pos >> block_shift;
value_type* vertex = vertexs_[block] + (( pos & block_mask) << 1);
t.forward_x(vertex);
++vertex;
t.forward_y(vertex);
}
void set_capacity(size_t) void set_capacity(size_t)
{ {
//do nothing //do nothing
} }
private: private:
void allocate_block(unsigned block) void allocate_block(unsigned block)
{ {
@ -181,13 +173,6 @@ namespace mapnik
return cont_.end(); return cont_.end();
} }
void transform_at(unsigned pos,const CoordTransform& t)
{
if (pos >= cont_.size()) return;
vertex_type & c = cont_[pos];
t.forward_x(&boost::get<0>(c));
t.forward_y(&boost::get<1>(c));
}
void set_capacity(size_t size) void set_capacity(size_t size)
{ {
cont_.reserve(size); cont_.reserve(size);

View file

@ -96,7 +96,7 @@ namespace mapnik
{ {
Color const& bg=m.getBackground(); Color const& bg=m.getBackground();
pixmap_.setBackground(bg); pixmap_.setBackground(bg);
std::clog << "scale="<<m.scale()<<std::endl; std::clog << "scale=" << m.scale() << "\n";
} }
template <typename T> template <typename T>
@ -172,7 +172,9 @@ namespace mapnik
if (geom && geom->num_points() > 1) if (geom && geom->num_points() > 1)
{ {
path_type path(t_,*geom); path_type path(t_,*geom);
agg::row_ptr_cache<agg::int8u> buf(pixmap_.raw_data(),pixmap_.width(),pixmap_.height(), agg::row_ptr_cache<agg::int8u> buf(pixmap_.raw_data(),
pixmap_.width(),
pixmap_.height(),
pixmap_.width()*4); pixmap_.width()*4);
agg::pixfmt_rgba32 pixf(buf); agg::pixfmt_rgba32 pixf(buf);
ren_base renb(pixf); ren_base renb(pixf);
@ -286,8 +288,7 @@ namespace mapnik
if ( data ) if ( data )
{ {
geom->label_position(&x,&y); geom->label_position(&x,&y);
t_.forward_x(&x); t_.forward(&x,&y);
t_.forward_y(&y);
int w = data->width(); int w = data->width();
int h = data->height(); int h = data->height();
int px=int(floor(x - 0.5 * w)); int px=int(floor(x - 0.5 * w));
@ -435,8 +436,7 @@ namespace mapnik
double x; double x;
double y; double y;
geom->label_position(&x,&y); geom->label_position(&x,&y);
t_.forward_x(&x); t_.forward(&x,&y);
t_.forward_y(&y);
face_ptr face = font_manager_.get_face("Bitstream Vera Sans Roman");//TODO face_ptr face = font_manager_.get_face("Bitstream Vera Sans Roman");//TODO
//face_ptr face = font_manager_.get_face("Times New Roman Regular");//TODO //face_ptr face = font_manager_.get_face("Times New Roman Regular");//TODO