Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpPanda3DCommonFilters.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 #include <visp3/ar/vpPanda3DCommonFilters.h>
32 #include <visp3/ar/vpPanda3DRGBRenderer.h>
33 
34 #if defined(VISP_HAVE_PANDA3D)
35 
36 BEGIN_VISP_NAMESPACE
37 const std::string vpPanda3DLuminanceFilter::FRAGMENT_SHADER =
38 "#version 330\n"
39 "in vec2 texcoords;\n"
40 "uniform sampler2D p3d_Texture0;\n"
41 "out vec4 p3d_FragData;\n"
42 "void main() {\n"
43 " vec4 v = texture(p3d_Texture0, texcoords);\n"
44 " p3d_FragData.b = 0.299 * v.r + 0.587 * v.g + 0.114 * v.b;\n"
45 "}\n";
46 
47 vpPanda3DLuminanceFilter::vpPanda3DLuminanceFilter(const std::string &name, std::shared_ptr<vpPanda3DRGBRenderer> inputRenderer, bool isOutput)
48  : vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, std::string(vpPanda3DLuminanceFilter::FRAGMENT_SHADER))
49 { }
50 FrameBufferProperties vpPanda3DLuminanceFilter::getBufferProperties() const
51 {
52  FrameBufferProperties fbp;
53  fbp.set_depth_bits(0);
54  fbp.set_rgba_bits(0, 0, 8, 0);
55  fbp.set_float_color(false);
56  return fbp;
57 }
59 {
61 }
62 
63 const std::string vpPanda3DGaussianBlur::FRAGMENT_SHADER =
64 "#version 330\n"
65 "in vec2 texcoords;\n"
66 "uniform sampler2D p3d_Texture0;\n"
67 "uniform vec2 dp; // 1 divided by number of pixels\n"
68 "const float kernel[25] = float[25](\n"
69 " 2, 4, 5, 4, 2,\n"
70 " 4, 9, 12, 9, 4,\n"
71 " 5, 12, 15, 12, 5,\n"
72 " 4, 9, 12, 9, 4,\n"
73 " 2, 4, 5, 4, 2\n"
74 ");\n"
75 "const float normalize = 1 / 159.0;\n"
76 "vec2 offset[25] = vec2[25](\n"
77 " vec2(-2*dp.x,-2*dp.y), vec2(-dp.x,-2*dp.y), vec2(0,-2*dp.y), vec2(dp.x,-2*dp.y), vec2(2*dp.x,-2*dp.y),\n"
78 " vec2(-2*dp.x,-dp.y), vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y), vec2(2*dp.x,-dp.y),\n"
79 " vec2(-2*dp.x,0.0), vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0), vec2(2*dp.x,0.0),\n"
80 " vec2(-2*dp.x, dp.y), vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y), vec2(2*dp.x, dp.y),\n"
81 " vec2(-2*dp.x, 2*dp.y), vec2(-dp.x, 2*dp.y), vec2(0.0, 2*dp.y), vec2(dp.x, 2*dp.y), vec2(2*dp.x, 2*dp.y)\n"
82 ");\n"
83 "out vec4 p3d_FragData;\n"
84 "void main() {\n"
85 " float v = 0.f;\n"
86 " for(int i = 0; i < 25; ++i) {\n"
87 " v += kernel[i] * texture(p3d_Texture0, texcoords + offset[i]).b;\n"
88 " }\n"
89 " p3d_FragData.b = v * normalize;\n"
90 "}\n";
91 
92 vpPanda3DGaussianBlur::vpPanda3DGaussianBlur(const std::string &name, std::shared_ptr<vpPanda3DBaseRenderer> inputRenderer, bool isOutput)
93  : vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, vpPanda3DGaussianBlur::FRAGMENT_SHADER)
94 { }
95 
96 FrameBufferProperties vpPanda3DGaussianBlur::getBufferProperties() const
97 {
98  FrameBufferProperties fbp;
99  fbp.set_depth_bits(0);
100  fbp.set_rgba_bits(0, 0, 8, 0);
101  fbp.set_float_color(false);
102  return fbp;
103 }
104 
106 {
108 }
109 
110 const std::string vpPanda3DCanny::FRAGMENT_SHADER =
111 "#version 330\n"
112 "in vec2 texcoords;\n"
113 "uniform sampler2D p3d_Texture0;\n"
114 "uniform vec2 dp; // 1 divided by number of pixels\n"
115 "uniform float edgeThreshold;\n"
116 "const float kernel[9] = float[9](\n"
117 " 0.0, 1.0, 0.0,\n"
118 " 1.0,-4.0, 1.0,\n"
119 " 0.0, 1.0, 0.0\n"
120 ");\n"
121 "const float kernel_h[9] = float[9](\n"
122 " -1.0, 0.0, 1.0,\n"
123 " -2.0, 0.0, 2.0,\n"
124 " -1.0, 0.0, 1.0\n"
125 ");\n"
126 "const float kernel_v[9] = float[9](\n"
127 " -1.0, -2.0, -1.0,\n"
128 " 0.0, 0.0, 0.0,\n"
129 " 1.0, 2.0, 1.0\n"
130 ");\n"
131 "vec2 offset[9] = vec2[9](\n"
132 " vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y),\n"
133 " vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0),\n"
134 " vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y)\n"
135 ");\n"
136 "out vec4 p3d_FragData;\n"
137 "void main() {\n"
138 " float sum = 0.f;\n"
139 " for(int i = 0; i < 9; ++i) {\n"
140 " float pix = texture(p3d_Texture0, texcoords + offset[i]).b;\n"
141 " sum += pix * kernel[i];\n"
142 " }\n"
143 " if(abs(sum * 255.f) > edgeThreshold) {\n"
144 " float sum_h = 0.f;\n"
145 " float sum_v = 0.f;\n"
146 " for(int i = 0; i < 9; ++i) {\n"
147 " float pix = texture(p3d_Texture0, texcoords + offset[i]).b;\n"
148 " sum_h += pix * kernel_h[i];\n"
149 " sum_v += pix * kernel_v[i];\n"
150 " }\n"
151 " vec2 orientationAndValid = sum_h * sum_h + sum_v * sum_v > 0 ? vec2(-atan(sum_v/sum_h), 1.f) : vec2(0.f, 0.f);\n"
152 " p3d_FragData = vec4(sum_h, sum_v, orientationAndValid.x, orientationAndValid.y);\n"
153 " } else {\n"
154 " p3d_FragData = vec4(0.f, 0.f, 0.f, 0.f);\n"
155 " }\n"
156 "}\n";
157 
158 vpPanda3DCanny::vpPanda3DCanny(const std::string &name, std::shared_ptr<vpPanda3DBaseRenderer> inputRenderer, bool isOutput, float edgeThreshold)
159  : vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, vpPanda3DCanny::FRAGMENT_SHADER), m_edgeThreshold(edgeThreshold)
160 { }
161 
163 {
165  m_renderRoot.set_shader_input("edgeThreshold", LVector2f(m_edgeThreshold));
166 }
167 
168 void vpPanda3DCanny::setEdgeThreshold(float edgeThreshold)
169 {
170  m_edgeThreshold = edgeThreshold;
171  m_renderRoot.set_shader_input("edgeThreshold", LVector2f(m_edgeThreshold));
172 }
173 
174 FrameBufferProperties vpPanda3DCanny::getBufferProperties() const
175 {
176  FrameBufferProperties fbp;
177  fbp.set_depth_bits(0);
178  fbp.set_rgba_bits(32, 32, 32, 32);
179  fbp.set_float_color(true);
180  return fbp;
181 }
182 
184 {
186 }
187 
188 END_VISP_NAMESPACE
189 
190 #elif !defined(VISP_BUILD_SHARED_LIBS)
191 // Work around to avoid warning: libvisp_ar.a(vpPanda3DCanny.cpp.o) has no symbols
192 void dummy_vpPanda3DCanny() { };
193 
194 #endif
NodePath m_renderRoot
Rendering parameters.
Implementation of canny filtering, using Sobel kernels.
void setupScene() VP_OVERRIDE
Initialize the scene for this specific renderer.
void getRender(vpImage< vpRGBf > &I) const
void setEdgeThreshold(float edgeThreshold)
FrameBufferProperties getBufferProperties() const VP_OVERRIDE
vpPanda3DCanny(const std::string &name, std::shared_ptr< vpPanda3DBaseRenderer > inputRenderer, bool isOutput, float edgeThreshold)
Class that implements a gaussian filter on a grayscale image. The grayscale image should be contained...
FrameBufferProperties getBufferProperties() const VP_OVERRIDE
void getRender(vpImage< unsigned char > &I) const
vpPanda3DGaussianBlur(const std::string &name, std::shared_ptr< vpPanda3DBaseRenderer > inputRenderer, bool isOutput)
Class that implements an RGB to grayscale conversion.
FrameBufferProperties getBufferProperties() const VP_OVERRIDE
void getRender(vpImage< unsigned char > &I) const
vpPanda3DLuminanceFilter(const std::string &name, std::shared_ptr< vpPanda3DRGBRenderer > inputRenderer, bool isOutput)
Base class for postprocessing filters that map the result of a vpPanda3DBaseRenderer to a new image.
void getRenderBasic(vpImage< unsigned char > &I) const
virtual void setupScene() VP_OVERRIDE
Initialize the scene for this specific renderer.