All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2014 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Utils.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "Utils.h"
30 #include "Context.h"
31 #include "Scene.h"
32 #include "../../../color/RGBAColor.h"
33 #include "../../../geometry/Polygon.h"
34 #include "../../../geometry/Enums.h"
35 #include "../../../geometry/LinearRing.h"
36 #include "../../../geometry/Point.h"
37 #include "../../../qt/widgets/canvas/Canvas.h"
38 
40 {
41 
42 }
43 
45 {
46 
47 }
48 
50 {
52 
53  if(!canvas)
54  {
55  return;
56  }
57 
58  te::gm::Polygon* rect = new te::gm::Polygon(1, te::gm::PolygonType, 0, &box);
59 
61  outRingPtr0->setPointN( 0, te::gm::Point(box.getLowerLeftX(), box.getLowerLeftY()));
62  outRingPtr0->setPointN( 1, te::gm::Point(box.getUpperRightX(), box.getLowerLeftY()));
63  outRingPtr0->setPointN( 2, te::gm::Point(box.getUpperRightX(), box.getUpperRightY()));
64  outRingPtr0->setPointN( 3, te::gm::Point(box.getLowerLeftX(), box.getUpperRightY()));
65  outRingPtr0->setPointN( 4, te::gm::Point(box.getLowerLeftX(), box.getLowerLeftY()));
66 
67  rect->setRingN(0, outRingPtr0);
68 
69  canvas->draw(rect);
70 
71 }
72 
74 {
76 
77  if(!canvas)
78  {
79  return;
80  }
81 
82  canvas->draw(line);
83 }
84 
86 {
88  line->setNumCoordinates(3);
89 
90  line->setPointN(0, te::gm::Point(box.getLowerLeftX(), box.getLowerLeftY()));
91  line->setPointN(1, te::gm::Point(box.getUpperRightX(), box.getUpperRightY()));
92  line->setPointN(2, te::gm::Point(box.getLowerLeftX(), box.getLowerLeftY()));
93 
94  return line;
95 }
96 
98 {
99  te::color::RGBAColor** pixmap = 0;
100 
102 
103  if(!canvas)
104  {
105  return pixmap;
106  }
107 
108  te::gm::Envelope boxViewport = viewportBox(boxmm);
109 
110  if(boxViewport.isValid())
111  {
112  pixmap = canvas->getImage(0, 0, boxViewport.getWidth(), boxViewport.getHeight());
113  }
114  return pixmap;
115 }
116 
118 {
119  int devDpi = Context::getInstance()->getDpiX();
120  int px = (mm * devDpi) / 25.4 ;
121  return px;
122 }
123 
125 {
126  te::gm::Envelope boxViewport = viewportBox(box);
127  changeCanvas(boxViewport, box, resize);
128 }
129 
131 {
132  te::gm::Envelope boxViewport = viewportBox(boxmm);
133  changeCanvas(boxViewport, boxgeo, resize);
134 }
135 
137 {
138  te::gm::Envelope boxViewport = viewportBoxFromGeo(boxgeo, boxmm);
139  changeCanvas(boxViewport, boxgeo, resize);
140 }
141 
142 void te::layout::Utils::changeCanvas( te::gm::Envelope viewport, te::gm::Envelope world, bool resize /*= true*/ )
143 {
145 
146  if(!canvas)
147  {
148  return;
149  }
150 
151  if(resize)
152  {
153  //Transparent
154  te::color::RGBAColor color(255,255,255, 0);
155  canvas->setBackgroundColor(color);
156 
157  canvas->resize(viewport.getWidth(), viewport.getHeight());
158  }
159 
160  canvas->setWindow(world.getLowerLeftX(), world.getLowerLeftY(),
161  world.getUpperRightX(), world.getUpperRightY());
162 }
163 
165 {
166  te::gm::Envelope boxViewport;
167 
168  if(!box.isValid())
169  return boxViewport;
170 
171  boxViewport = viewportBoxFromMM(box);
172  return boxViewport;
173 }
174 
176 {
177  te::map::WorldDeviceTransformer transf; // World Device Transformer.
178 
179  int pxwidth = mm2pixel(box.getWidth());
180  int pxheight = mm2pixel(box.getHeight());
181 
182  // Adjust internal renderer transformer
184  box.getUpperRightX(), box.getUpperRightY(), pxwidth, pxheight);
185 
186  te::gm::Envelope boxViewport = transformToViewport(transf, box);
187  return boxViewport;
188 }
189 
191 {
192  te::gm::Envelope boxViewport;
193 
194  if(!boxgeo.isValid())
195  return boxViewport;
196 
197  if(!boxmm.isValid())
198  return boxViewport;
199 
200  te::map::WorldDeviceTransformer transf; // World Device Transformer.
201 
202  int pxwidth = mm2pixel(boxmm.getWidth());
203  int pxheight = mm2pixel(boxmm.getHeight());
204 
205  // Adjust internal renderer transformer
206  transf.setTransformationParameters(boxgeo.getLowerLeftX(), boxgeo.getLowerLeftY(),
207  boxgeo.getUpperRightX(), boxgeo.getUpperRightY(), pxwidth, pxheight);
208 
209  boxViewport = transformToViewport(transf, boxgeo);
210  return boxViewport;
211 }
212 
214 {
215  double px1 = 0;
216  double py1 = 0;
217  double px2 = 0;
218  double py2 = 0;
219  double pycopy = 0;
220  transf.world2Device(box.getLowerLeftX(), box.getLowerLeftY(), px1, py1);
221  transf.world2Device(box.getUpperRightX(), box.getUpperRightY(), px2, py2);
222 
223  if(py1 > py2 )
224  {
225  pycopy = py1;
226  py1 = py2;
227  py2 = pycopy;
228  }
229 
230  te::gm::Envelope boxViewport(px1, py1, px2, py2);
231  return boxViewport;
232 }
233 
235 {
237  std::map<int, te::gm::Point> coords;
238 
239  int count = 1;
240  for(double sub_x = box.getLowerLeftX(); sub_x < box.getUpperRightX(); sub_x +=(gap / 4.))
241  {
242  coords[count] = te::gm::Point(sub_x, box.getLowerLeftY());
243  count+=1;
244  }
245 
246  line->setNumCoordinates(count + 1);
247  line->setPointN(0, te::gm::Point(box.getLowerLeftX(), box.getLowerLeftY()));
248 
249  for(int i = 1 ; i < count ; ++i)
250  {
251  line->setPointN(i, coords[i]);
252  }
253 
254  line->setPointN(count, te::gm::Point(box.getUpperRightX(), box.getLowerLeftY()));
255 
256  return line;
257 }
258 
260 {
262  std::map<int, te::gm::Point> coords;
263 
264  int count = 1;
265  for(double sub_y = box.getLowerLeftY(); sub_y < box.getUpperRightY(); sub_y +=(gap / 4.))
266  {
267  coords[count] = te::gm::Point(box.getLowerLeftX(), sub_y);
268  count+=1;
269  }
270 
271  line->setNumCoordinates(count + 1);
272  line->setPointN(0, te::gm::Point(box.getLowerLeftX(), box.getLowerLeftY()));
273 
274  for(int i = 1 ; i < count ; ++i)
275  {
276  line->setPointN(i, coords[i]);
277  }
278 
279  line->setPointN(count, te::gm::Point(box.getUpperRightX(), box.getLowerLeftY()));
280 
281  return line;
282 }
283 
284 void te::layout::Utils::textBoundingBox( double &w, double &h, std::string txt )
285 {
287 
288  if(!canvas)
289  {
290  return;
291  }
292 
293  w = 0;
294  h = 0;
295 
296  te::gm::Polygon* poly = canvas->getTextBoundary(0, 0, txt, 0);
297  if(poly)
298  {
299  //Box = mbr: minimum bounding rectangle
300  const te::gm::Envelope* env = poly->getMBR();
301  te::gm::Envelope* box = 0;
302  box = const_cast<te::gm::Envelope*>(env);
303  if(box)
304  {
305  w = box->getWidth();
306  h = box->getHeight();
307  }
308  }
309 }
virtual void resize(int w, int h)=0
It adjusts the canvas size (width and height).
virtual te::gm::Envelope viewportBoxFromMM(te::gm::Envelope box)
Definition: Utils.cpp:175
virtual void textBoundingBox(double &w, double &h, std::string txt)
Definition: Utils.cpp:284
virtual te::gm::Polygon * getTextBoundary(int x, int y, const std::string &txt, float angle=0.0, te::at::HorizontalAlignment hAlign=te::at::Start, te::at::VerticalAlignment vAlign=te::at::Baseline)=0
It returns the text boundary (its enclose rectangle).
virtual void drawRectW(te::gm::Envelope box)
Definition: Utils.cpp:49
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
void setTransformationParameters(const double &wllx, const double &wlly, const double &wurx, const double &wury, int deviceWidth, int deviceHeight)
It adjusts a new transformation function that maps between the spatial coordinate system of a feature...
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
virtual void configCanvas(te::gm::Envelope box, bool resize=true)
Definition: Utils.cpp:124
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
virtual void configGeoCanvasFromGeo(te::gm::Envelope boxgeo, te::gm::Envelope boxmm, bool resize=true)
Definition: Utils.cpp:136
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
virtual void setBackgroundColor(const te::color::RGBAColor &color)=0
It sets the canvas background color.
virtual te::gm::LinearRing * createSimpleLine(te::gm::Envelope box)
Definition: Utils.cpp:85
This class implements the logic for transforming from device coordinate to world coordinate and vice-...
virtual te::gm::LinearRing * addCoordsInX(te::gm::Envelope box, double gap)
Definition: Utils.cpp:234
virtual void setWindow(const double &llx, const double &lly, const double &urx, const double &ury)=0
It sets the world (or window) coordinates area (supposing a cartesian reference system).
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
virtual int mm2pixel(double mm)
Definition: Utils.cpp:117
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual te::gm::LinearRing * addCoordsInY(te::gm::Envelope box, double gap)
Definition: Utils.cpp:259
void setNumCoordinates(std::size_t size)
It reserves room for the number of coordinates in this LineString.
Definition: LineString.cpp:264
virtual void drawLineW(te::gm::LinearRing *line)
Definition: Utils.cpp:73
virtual te::gm::Envelope viewportBox(te::gm::Envelope box)
Definition: Utils.cpp:164
virtual te::gm::Envelope viewportBoxFromGeo(te::gm::Envelope boxgeo, te::gm::Envelope boxmm)
Definition: Utils.cpp:190
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
virtual te::gm::Envelope transformToViewport(te::map::WorldDeviceTransformer transf, te::gm::Envelope box)
Definition: Utils.cpp:213
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
virtual te::color::RGBAColor ** getImageW(te::gm::Envelope boxmm)
Definition: Utils.cpp:97
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void world2Device(double &wx, double &wy) const
It transforms the coordinate wx and wy from world coordinates to device (canvas) coordinates without ...
void setPointN(std::size_t i, const Point &p)
It sets the value of the specified point to this new one.
Definition: LineString.cpp:338
virtual ~Utils()
Definition: Utils.cpp:44
static Context * getInstance()
This function is called to create an instance of the class.
Definition: Context.cpp:46
virtual char * getImage(ImageType t, std::size_t &size, int quality=75, int fg=0) const =0
It returns the internal content as an image in a specific format (PNG, JPEG, ...).
virtual void changeCanvas(te::gm::Envelope viewport, te::gm::Envelope world, bool resize=true)
Definition: Utils.cpp:142
virtual void draw(const te::gm::Geometry *geom)=0
It draws the geometry on canvas.
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:103
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
te::map::Canvas * getCanvas()
Definition: Context.cpp:116
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
virtual void configGeoCanvas(te::gm::Envelope boxgeo, te::gm::Envelope boxmm, bool resize=true)
Definition: Utils.cpp:130