Visual Servoing Platform  version 3.6.1 under development (2024-12-03)
ImageDisplayWithContext.mm
1 /*
2  * This file is part of the ViSP software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30 
31 #import "ImageDisplayWithContext.h"
32 
34 
36 // UIColor *color = <the color of the line>
37 // int tickness = <the tickness of the lines on the AprilTag contour>
38 + (void)displayLineWithContext:(CGContextRef)context :(std::vector<vpImagePoint>)polygon :(UIColor*)color :(int)tickness
39 {
40 
41  CGContextSetLineWidth(context, tickness);
42  CGContextSetStrokeColorWithColor(context, [color CGColor]);
43  for (size_t j = 0; j < polygon.size(); j++) {
44 
45  CGContextMoveToPoint(context, polygon[j].get_u(), polygon[j].get_v());
46  CGContextAddLineToPoint(context, polygon[(j+1)%polygon.size()].get_u(), polygon[(j+1)%polygon.size()].get_v());
47 
48  CGContextStrokePath(context);
49  }
50 
51  return;
52 }
54 
56 // vpHomogeneousMatrix cMo = <Homegeneous transformation>
57 // vpCameraParameters cam = <Camera parameters>
58 // double size = <Size of the frame in meter>
59 // int tickness = <the tickness of the lines describing the frame>
60 + (void)displayFrameWithContext:(CGContextRef)context :(const vpHomogeneousMatrix &)cMo :(const vpCameraParameters &)cam
61  :(double) size :(int)tickness
62 {
63 
64  vpPoint o( 0.0, 0.0, 0.0);
65  vpPoint x(size, 0.0, 0.0);
66  vpPoint y( 0.0, size, 0.0);
67  vpPoint z( 0.0, 0.0, size);
68 
69  o.track(cMo);
70  x.track(cMo);
71  y.track(cMo);
72  z.track(cMo);
73 
74  vpImagePoint ipo, ip1;
75  vpMeterPixelConversion::convertPoint (cam, o.p[0], o.p[1], ipo);
76 
77  // Draw red line on top of original image
78  vpMeterPixelConversion::convertPoint (cam, x.p[0], x.p[1], ip1);
79  CGContextSetLineWidth(context, tickness);
80  CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
81  CGContextMoveToPoint(context, ipo.get_u(), ipo.get_v());
82  CGContextAddLineToPoint(context, ip1.get_u(), ip1.get_v());
83  CGContextStrokePath(context);
84 
85  // Draw green line on top of original image
86  vpMeterPixelConversion::convertPoint ( cam, y.p[0], y.p[1], ip1) ;
87  context = UIGraphicsGetCurrentContext();
88  CGContextSetLineWidth(context, tickness);
89  CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);
90  CGContextMoveToPoint(context, ipo.get_u(), ipo.get_v());
91  CGContextAddLineToPoint(context, ip1.get_u(), ip1.get_v());
92  CGContextStrokePath(context);
93 
94  // Draw blue line on top of original image
95  vpMeterPixelConversion::convertPoint ( cam, z.p[0], z.p[1], ip1) ;
96  context = UIGraphicsGetCurrentContext();
97  CGContextSetLineWidth(context, tickness);
98  CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
99  CGContextMoveToPoint(context, ipo.get_u(), ipo.get_v());
100  CGContextAddLineToPoint(context, ip1.get_u(), ip1.get_v());
101  CGContextStrokePath(context);
102 
103  return;
104 }
106 
108 + (void)displayText:(NSString*)text :(double)x :(double)y :(int)width :(int)height :(UIColor*)color :(UIColor*)bgColor{
109 
110  CGRect rect = CGRectMake(x,y,width,height);
111 
112  NSDictionary *attributes =
113  @{
114  NSForegroundColorAttributeName : color,
115  NSFontAttributeName : [UIFont boldSystemFontOfSize:50],
116  NSBackgroundColorAttributeName: bgColor
117  };
118 
119  [text drawInRect:CGRectIntegral(rect) withAttributes:attributes];
120 
121  return;
122 }
124 
125 @end
Generic class defining intrinsic camera parameters.
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79