VAT  3.0
Video Analysis Tool
world_def.h
1 #ifndef _WORLD_DEF_H_
2 #define _WORLD_DEF_H_
3 
4 #include "common.h"
5 #include <string>
6 #include <vector>
7 #include <QSharedPointer>
8 #include <QString>
9 #include <QDomNode>
10 #include <QPainter>
11 
12 class SceneModel;
13 class Area;
14 class Blob;
15 
16 template<typename T>
17 class polygon2D;
18 
19 template<typename T>
20 class polygon3D;
21 
22 template<typename T>
23 class Rectangle;
24 
25 template<typename T>
26 class point2D;
27 
28 template<typename T>
29 class point3D;
30 
31 namespace world {
32 
33 class AOI {
34  public:
35  AOI();
36  ~AOI();
37  bool pointInside(double x, double y);
38  void buildPoly3DOnGround(std::vector< QSharedPointer<Area> > &areas);
39  bool blobInsideGround3D(Blob *blob);
40  int id;
41  QString name;
42  QSharedPointer < polygon3D<double> > poly3DOnGround;
43  QSharedPointer < polygon2D<double> > poly2DOnGround;
44  double height;
45  QSharedPointer < polygon2D<double> > outlineOnImg;
46 };
47 
48 class Zone {
49  public:
50  Zone();
51  ~Zone();
52  bool rectangleInside(Rectangle<int> *rect);
53  bool pointInZone(double x, double y);
54 
55  int id;
56  QString name;
57  QSharedPointer < polygon3D<double> > outline3D;
58  QSharedPointer < polygon2D<int> > outline;
59 };
60 
61 /*enum zone3DType {
62  Z3D_polygon,
63  Z3D_ellipse
64 };
65 
66 typedef enum zone3DType zone3DType;
67 */
68 
69 enum zone2DType {
70  Z2D_polygon,
71  Z2D_ellipse
72 };
73 
74 typedef enum zone2DType zone2DType;
75 
76 class ZoneH {
77  public:
78  ZoneH();
79  virtual ~ZoneH()=0;
80  virtual bool rectangleInside(Rectangle<int> *rect)=0;
81  virtual bool pointInZone(double x, double y)=0;
82  virtual bool setParameters(QDomNode &params, homography_matrix h_matrix)=0;
83  virtual bool draw(QPainter &p, QColor &color, QTransform &h_trans)=0;
84  virtual double distanceToCenter(double x, double y)=0;
85 
86  int id;
87  QString name;
88  zone2DType type;
89 };
90 
91 
92 class ZoneHPolygon: public ZoneH {
93  public:
94  ZoneHPolygon();
95  ~ZoneHPolygon();
96  bool rectangleInside(Rectangle<int> *rect);
97  bool pointInZone(double x, double y);
98  bool setParameters(QDomNode &params, homography_matrix h_matrix);
99  bool draw(QPainter &p, QColor &color, QTransform &h_trans);
100  double distanceToCenter(double x, double y);
101 
102  //Homography
103  QSharedPointer < polygon2D<double> > outline2D;
104  //Image
105  QSharedPointer < polygon2D<int> > outline;
106 };
107 
108 class ZoneHEllipse: public ZoneH {
109  public:
110  ZoneHEllipse();
111  ~ZoneHEllipse();
112  bool rectangleInside(Rectangle<int> *rect);
113  bool pointInZone(double x, double y);
114  bool setParameters(QDomNode &params, homography_matrix h_matrix);
115  bool draw(QPainter &p, QColor &color, QTransform &h_trans);
116  double distanceToCenter(double x, double y);
117 
118  //Homography
119  double x, y, rx, ry;
120 };
121 
122 
123 class Zone2D {
124  public:
125  Zone2D();
126  virtual ~Zone2D()=0;
127  virtual bool rectangleInside(Rectangle<int> *rect) = 0;
128  virtual bool pointInZone(double x, double y) = 0;
129  virtual bool setParameters(QDomNode &params)=0;
130  virtual bool draw(QPainter &p, QColor color)=0;
131 
132  int id;
133  QString name;
134  zone2DType type;
135 };
136 
137 class Zone2DPolygon: public Zone2D {
138  public:
139  Zone2DPolygon();
140  ~Zone2DPolygon();
141  bool rectangleInside(Rectangle<int> *rect);
142  bool pointInZone(double x, double y);
143  bool setParameters(QDomNode &params);
144  bool draw(QPainter &p, QColor color);
145 
146  //Image
147  QSharedPointer < polygon2D<int> > outline;
148 };
149 
150 class Zone2DEllipse: public Zone2D {
151  public:
152  Zone2DEllipse();
153  ~Zone2DEllipse();
154  bool rectangleInside(Rectangle<int> *rect);
155  bool pointInZone(double x, double y);
156  bool setParameters(QDomNode &params);
157  bool draw(QPainter &p, QColor color);
158 
159  //Image
160  int x, y, rx, ry;
161 };
162 
163 
164 class Area {
165  public:
166  Area();
167  ~Area();
168 
169  bool pointInside(double x, double y);
170  bool zoneInside(QString name);
171 
172  QString name;
173  std::vector< QSharedPointer<Zone> > visibleParts;
174 };
175 
176 class Wall {
177  public:
178  Wall();
179  ~Wall();
180  //Calculates parameters for 3D line equation calculated from wall limiting points
181  void setWallAsLineEquation();
182 
183  int id;
184  QString name;
185  QSharedPointer< point3D<double> > startPoint;
186  QSharedPointer< point3D<double> > endPoint;
187  double height;
188  bool solid; //true: wall is real
189  //false: wall is virtual (just for building context object)
190  bool segmentIsFunction; // if a -> infinite => segmentIsFunction = false and x = constant for all y
191  // else segmentIsFunction = true and y = a*x + b
192  double a, b; // Segment equation parameters
193 };
194 
195 //Represent a limiting wall segment of a wall in 2D image coordinates, classifying line segments according to their 3D nature.
196 //if inHeight is 2, the point (x1,y1) will correspond to the projection of the 3D point on the floor and (x2,y2) to the one in height.
197 class WallSegment {
198  public:
199  WallSegment();
200  ~WallSegment();
201  void setWallSegment(double x1, double y1, double x2, double y2, int in_height);
202 
203  //Returns the pertinence of a coordinate according to a wall segment and stores the value if pertinent
204  // false: if point has no connection with wall segment, according to point orientation
205  int inWallSegmentInterval(double coord, int orientation, double& val1, double& val2);
206 
207  //Returns false if no intersection or parallel lines, true else. Considers line information, plus segment limit information.
208  static bool twoSegmentsIntersection(WallSegment *wsegment1, WallSegment *wsegment2, double& x, double& y);
209 
210  //Returns false if no intersection or parallel lines, true else. Only considers line information, without segment limits.
211  static bool twoLinesIntersection(WallSegment *wsegment1, WallSegment *wsegment2, double& x, double& y);
212 
213  //Returns false if no intersection, true else. Evaluates segment limits given an intersection point.
214  bool inSegmentGivenIntersection(double x, double y);
215 
216  double x1;
217  double y1;
218  double x2;
219  double y2;
220  double slope;
221  double intercept;
222  bool vertical; // true: When slope tends to infinite;
223  // false: Normal slope
224  int inHeight; // 0: Segment 2D corresponding to the one on the floor of the 3D scene.
225  // 1: Segment 2D corresponding to the one on the maximal height of wall in 3D coordinates.
226  // 2: Segment 2D corresponding to the one going from the floor to the maximal height of wall in 3D coordinates.
227 };
228 
229 //Defines the 2D image transform to wall at different 3D heights h
230 //with y2d = x2d * a(h) + b(h)
231 // and:
232 // a = (K[0]*z + K[1])/(K[4]*z + K[5]);
233 // b = (K[2]*z + K[3])/(K[4]*z + K[5]);
234 //The wall projection at height h have as 2d image points:
235 // X2d1 = (pp2*z + A[0])/(pp10*z + C[0]);
236 // Y2d1 = (pp6*z + B[0])/(pp10*z + C[0]);
237 // X2d2 = (pp2*z + A[1])/(pp10*z + C[1]);
238 // Y2d2 = (pp6*z + B[1])/(pp10*z + C[1]);
239 //, where ppX is the value in position X in the perspective matrix
240 class Wall2D {
241  public:
242  Wall2D();
243  Wall2D(Wall *wall, SceneModel *smodel);
244  ~Wall2D();
245 
246  //Valid for pinhole camera model, using a perspective matrix
247  //Returns the parameters a and b for a image plane line y2d = a*x2d + b, given wall and h
248  bool get2DWallLine(double h, double *a, double *b);
249 
250  //Returns the limit 2D image points p1 and p2, given wall and h
251  void get2DwallLimitPoints(double h, perspective_matrix persp_mat, point2D<double> *p1, point2D<double> *p2);
252 
253  //Returns true if point (x2d,y2d) is inside of pertinent zone according to wall and height h
254  bool pointIsPertinent(double h, perspective_matrix persp_mat, double x2d, double y2d);
255 
256  //Returns true if point p is inside of pertinent zone according to wall, for a point in the floor
257  bool pointInFloorIsPertinent(perspective_matrix persp_mat, double x2d, double y2d);
258 
259  int id;
260  QString name;
261  bool visible; //false: Not visible
262  //true: Visible (or partially visible)
263  int discriminationType; //Defines the way of considering a point inside or outside camera's pertinent zone
264  //0: y2d - x2d*a - b <= 0
265  //1: y2d - x2d*a - b >= 0
266  DetectionProblemType occPertinance;
267  QSharedPointer < polygon2D<double> > outlineOnImage;
268  double max_h;
269  double A[2],B[2],C[2]; //Parameters for wall 2D limit points given h
270  double K[6]; //Parameters for wall 2D line equation parameters a and b, given h
271  WallSegment wsegments[4]; //Wall representation as a set of four limiting segments
272  WallSegment base3Dsegment; //Segment representing the base line (h = 0) in 3D scene coordinates
273 };
274 
276  public:
277  ContextObject();
278  ~ContextObject();
279  int setObjectOutlineFromWallList();
280  int setObjectWallListFromOutline();
281 
282  int id;
283  QString name;
284  double height; // the height above the plane
285  QSharedPointer< polygon3D<double> > outline; // Representation of context object by list of 3D points
286 
287  // Representation of objects by walls (uses the possibility of virtual walls for hollow objects)
288  std::vector< QSharedPointer<Wall> > wallList;
289  // 2D Representation of walls which form the context object
290  std::vector< QSharedPointer<Wall2D> > wall2DList;
291 
292  QSharedPointer < polygon2D<double> > outline2DOnImage; // projection of object on the image, including its limiting 2D bbox
293  QSharedPointer < polygon2D<double> > poly2DOfObjectBase; // projection of the object base on the image
294  QSharedPointer < polygon2D<double> > poly2DOfObjectRoof; // projection of the object roof on the image
295  QSharedPointer < polygon2D<double> > poly2DOfOutline; // 2D polygon of the base of the 3D polygon in world coordinates.
296 
297  bool hollow; //true: object is a bunch of walls
298  bool noRoof; //true: object has no roof
299 };
300 
301 class Camera {
302  public:
303  Camera();
304  ~Camera();
305  int id; // use for interface with the wrapper
306  QString name;
307  QSharedPointer < point3D<double> > location;
308  QSharedPointer < point3D<double> > focalPoint; // 3D focal point for calibrated scene
309  QSharedPointer < point2D<double> > focalPoint2D; // focal point projected on the ground of the scene.
310 };
311 
312 //MACROS
313 
314 #define AREA_NAME(ar) ((ar)->name)
315 #define AREA_NTH_PART(ar, n) ((ar)->visibleParts[n])
316 
317 #define AOI_NAME(aoi) ((aoi)->name)
318 #define AOI_ID(aoi) ((aoi)->id)
319 #define AOI_POLY3D_ON_GROUND(aoi) ((aoi)->poly3dOnGround)
320 #define AOI_POLY2D_ON_GROUND(aoi) ((aoi)->poly2dOnGround)
321 #define AOI_OUTLINE_ON_IMG(aoi) ((aoi)->outlineOnImg)
322 #define AOI_HEIGHT(aoi) ((aoi)->height)
323 
324 #define ZONE_ID(zn) ((zn)->id)
325 #define ZONE_NAME(zn) ((zn)->name)
326 #define ZONE_OUTLINE(zn) ((zn)->outline)
327 #define ZONE_OUTLINE_2D(zn) ((zn)->outline2D)
328 #define ZONE_OUTLINE_3D(zn) ((zn)->outline3D)
329 
330 #define WALL_ID(wl) ((wl)->id)
331 #define WALL_NAME(wl) ((wl)->name)
332 #define WALL_START_POINT(wl) ((wl)->startPoint)
333 #define WALL_START_POINT_X(wl) ((wl)->startPoint->x)
334 #define WALL_START_POINT_Y(wl) ((wl)->startPoint->y)
335 #define WALL_START_POINT_Z(wl) ((wl)->startPoint->z)
336 #define WALL_END_POINT(wl) ((wl)->endPoint)
337 #define WALL_END_POINT_X(wl) ((wl)->endPoint->x)
338 #define WALL_END_POINT_Y(wl) ((wl)->endPoint->y)
339 #define WALL_END_POINT_Z(wl) ((wl)->endPoint->z)
340 #define WALL_HEIGHT(wl) ((wl)->height)
341 #define WALL_IS_SOLID(wl) ((wl)->solid)
342 #define WALL_SEGMENT_IS_FUNCTION(wl) ((wl)->segmentIsFunction)
343 #define WALL_A(wl) ((wl)->a)
344 #define WALL_B(wl) ((wl)->b)
345 
346 #define WALL2D_ID(wl) ((wl)->id)
347 #define WALL2D_NAME(wl) ((wl)->name)
348 #define WALL2D_VISIBLE(wl) ((wl)->visible)
349 #define WALL2D_DIS_TYPE(wl) ((wl)->discriminationType)
350 #define WALL2D_OCC_PERT(wl) ((wl)->occPertinance)
351 #define WALL2D_PARAM_A(wl) ((wl)->A)
352 #define WALL2D_PARAM_B(wl) ((wl)->B)
353 #define WALL2D_PARAM_C(wl) ((wl)->C)
354 #define WALL2D_PARAM_K(wl) ((wl)->K)
355 #define WALL2D_PARAM_A_i(wl,i) ((wl)->A[i])
356 #define WALL2D_PARAM_B_i(wl,i) ((wl)->B[i])
357 #define WALL2D_PARAM_C_i(wl,i) ((wl)->C[i])
358 #define WALL2D_PARAM_K_i(wl,i) ((wl)->K[i])
359 #define WALL2D_MAXH(wl) ((wl)->max_h)
360 #define WALL2D_OUTLINE_ON_IMAGE(wl) ((wl)->outlineOnImage)
361 #define WALL2D_SEGMENT(wl,i) (&((wl)->wsegments[i]))
362 #define WALL2D_SEGMENT_BASE3D(wl) (&((wl)->base3Dsegment))
363 
364 #define WSEGMENT_X1(ws) ((ws)->x1)
365 #define WSEGMENT_Y1(ws) ((ws)->y1)
366 #define WSEGMENT_X2(ws) ((ws)->x2)
367 #define WSEGMENT_Y2(ws) ((ws)->y2)
368 #define WSEGMENT_SLOPE(ws) ((ws)->slope)
369 #define WSEGMENT_INTERCEPT(ws) ((ws)->intercept)
370 #define WSEGMENT_IS_VERTICAL(ws) ((ws)->vertical)
371 #define WSEGMENT_IN_HEIGHT(ws) ((ws)->inHeight)
372 
373 #define WOBJECT_ID(wo) ((wo)->id)
374 #define WOBJECT_NAME(wo) ((wo)->name)
375 #define WOBJECT_HEIGHT(wo) ((wo)->height)
376 #define WOBJECT_OUTLINE(wo) ((wo)->outline)
377 #define WOBJECT_WALLS_LIST(wo) (&(wo)->wallList)
378 #define WOBJECT_WALLS_2D_LIST(wo) (&(wo)->wall2DList)
379 #define WOBJECT_2D_OF_OUTLINE(wo) ((wo)->poly2DOfOutline)
380 #define WOBJECT_OUTLINE2D_ON_IMAGE(wo) ((wo)->outline2DOnImage)
381 #define WOBJECT_BASE2D_ON_IMAGE(wo) ((wo)->poly2DOfObjectBase)
382 #define WOBJECT_ROOF2D_ON_IMAGE(wo) ((wo)->poly2DOfObjectRoof)
383 #define WOBJECT_IS_HOLLOW(wo) ((wo)->hollow)
384 #define WOBJECT_NO_ROOF(wo) ((wo)->noRoof)
385 
386 #define CAMERA_ID(ca) ((ca).id)
387 #define CAMERA_NAME(ca) ((ca)->name)
388 #define CAMERA_LOCATION(ca) (&(ca).location)
389 #define CAMERA_X_LOCATION(ca) ((ca).location.x)
390 #define CAMERA_Y_LOCATION(ca) ((ca).location.y)
391 #define CAMERA_Z_LOCATION(ca) ((ca).location.z)
392 
393 } //end namespace world
394 
395 #endif /* _WORLD_DEF_H_ */
Definition: world_def.h:197
Definition: world_def.h:108
Definition: world_def.h:150
Definition: world_def.h:92
Definition: geometric.h:20
Definition: world_def.h:164
Definition: world_def.h:176
Definition: world_def.h:33
Definition: world_def.h:76
Definition: world_def.h:301
Definition: world_def.h:48
Definition: calibration.h:51
Definition: world_def.h:275
Definition: world_def.h:240
Definition: world_def.cpp:13
Definition: calibration.h:26
Definition: calibration.h:23
Definition: blob.h:79
Definition: world_def.h:137
Definition: geometric.h:256
Definition: geometric.h:224
Definition: world_def.h:123