00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <pcl/point_types.h>
00039 #include <pcl/visualization/common/common.h>
00040 #include <stdlib.h>
00041
00043 void
00044 pcl::visualization::getRandomColors (double &r, double &g, double &b, double min, double max)
00045 {
00046 double sum;
00047 static unsigned stepRGBA = 100;
00048 do
00049 {
00050 sum = 0;
00051 r = (rand () % stepRGBA) / static_cast<double> (stepRGBA);
00052 while ((g = (rand () % stepRGBA) / static_cast<double> (stepRGBA)) == r) {}
00053 while (((b = (rand () % stepRGBA) / static_cast<double> (stepRGBA)) == r) && (b == g)) {}
00054 sum = r + g + b;
00055 }
00056 while (sum <= min || sum >= max);
00057 }
00058
00060 void
00061 pcl::visualization::getRandomColors (pcl::RGB &rgb, double min, double max)
00062 {
00063 double sum;
00064 static unsigned stepRGBA = 100;
00065 double r, g, b;
00066 do
00067 {
00068 sum = 0;
00069 r = (rand () % stepRGBA) / static_cast<double> (stepRGBA);
00070 while ((g = (rand () % stepRGBA) / static_cast<double> (stepRGBA)) == r) {}
00071 while (((b = (rand () % stepRGBA) / static_cast<double> (stepRGBA)) == r) && (b == g)) {}
00072 sum = r + g + b;
00073 }
00074 while (sum <= min || sum >= max);
00075 rgb.r = uint8_t (r * 255.0);
00076 rgb.g = uint8_t (g * 255.0);
00077 rgb.b = uint8_t (b * 255.0);
00078 }
00079
00081
00082 Eigen::Matrix4d
00083 pcl::visualization::vtkToEigen (vtkMatrix4x4* vtk_matrix)
00084 {
00085 Eigen::Matrix4d eigen_matrix = Eigen::Matrix4d::Identity ();
00086 for (int i=0; i < 4; i++)
00087 {
00088 for (int j=0; j < 4; j++)
00089 {
00090
00091 eigen_matrix (i, j) = vtk_matrix->GetElement (i, j);
00092 }
00093 }
00094 return eigen_matrix;
00095 }
00096
00098 Eigen::Vector2i
00099 pcl::visualization::worldToView (const Eigen::Vector4d &world_pt, const Eigen::Matrix4d &view_projection_matrix, int width, int height)
00100 {
00101
00102 Eigen::Vector4d world (view_projection_matrix * world_pt);
00103
00104 world /= world.w ();
00105
00106
00107 int screen_x = int (floor (double (((world.x () + 1) / 2.0) * width) + 0.5));
00108 int screen_y = int (floor (double (((world.y () + 1) / 2.0) * height) + 0.5));
00109
00110
00111
00112
00113 return (Eigen::Vector2i (screen_x, screen_y));
00114 }
00115
00117 void
00118 pcl::visualization::getViewFrustum (const Eigen::Matrix4d &view_projection_matrix, double planes[24])
00119 {
00120
00121 Eigen::Vector4d normals[6];
00122 for (int i=0; i < 6; i++)
00123 {
00124 normals[i] = Eigen::Vector4d (0.0, 0.0, 0.0, 1.0);
00125
00126
00127 normals[i] (i/2) = 1 - (i%2)*2;
00128 }
00129
00130
00131 Eigen::Matrix4d view_matrix = view_projection_matrix.transpose ();
00132
00133
00134 for (int i=0; i < 6; i++)
00135 {
00136 normals[i] = view_matrix * normals[i];
00137
00138 double f = 1.0/sqrt (normals[i].x () * normals[i].x () +
00139 normals[i].y () * normals[i].y () +
00140 normals[i].z () * normals[i].z ());
00141
00142 planes[4*i + 0] = normals[i].x ()*f;
00143 planes[4*i + 1] = normals[i].y ()*f;
00144 planes[4*i + 2] = normals[i].z ()*f;
00145 planes[4*i + 3] = normals[i].w ()*f;
00146 }
00147 }
00148
00149 int
00150 pcl::visualization::cullFrustum (double frustum[24], const Eigen::Vector3d &min_bb, const Eigen::Vector3d &max_bb)
00151 {
00152 int result = PCL_INSIDE_FRUSTUM;
00153
00154 for(int i =0; i < 6; i++){
00155 double a = frustum[(i*4)];
00156 double b = frustum[(i*4)+1];
00157 double c = frustum[(i*4)+2];
00158 double d = frustum[(i*4)+3];
00159
00160
00161
00162
00163 Eigen::Vector3d center ((max_bb.x () - min_bb.x ()) / 2 + min_bb.x (),
00164 (max_bb.y () - min_bb.y ()) / 2 + min_bb.y (),
00165 (max_bb.z () - min_bb.z ()) / 2 + min_bb.z ());
00166
00167 Eigen::Vector3d radius (fabs (static_cast<double> (max_bb.x () - center.x ())),
00168 fabs (static_cast<double> (max_bb.y () - center.y ())),
00169 fabs (static_cast<double> (max_bb.z () - center.z ())));
00170
00171 double m = (center.x () * a) + (center.y () * b) + (center.z () * c) + d;
00172 double n = (radius.x () * fabs(a)) + (radius.y () * fabs(b)) + (radius.z () * fabs(c));
00173
00174 if (m + n < 0){
00175 result = PCL_OUTSIDE_FRUSTUM;
00176 break;
00177 }
00178
00179 if (m - n < 0)
00180 {
00181 result = PCL_INTERSECT_FRUSTUM;
00182 }
00183 }
00184
00185 return result;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 int hull_vertex_table[43][7] = {
00212 { 0, 0, 0, 0, 0, 0, 0 },
00213 { 0, 4, 7, 3, 0, 0, 4 },
00214 { 1, 2, 6, 5, 0, 0, 4 },
00215 { 0, 0, 0, 0, 0, 0, 0 },
00216 { 0, 1, 5, 4, 0, 0, 4 },
00217 { 0, 1, 5, 4, 7, 3, 6 },
00218 { 0, 1, 2, 6, 5, 4, 6 },
00219 { 0, 0, 0, 0, 0, 0, 0 },
00220 { 2, 3, 7, 6, 0, 0, 4 },
00221 { 4, 7, 6, 2, 3, 0, 6 },
00222 { 2, 3, 7, 6, 5, 1, 6 },
00223 { 0, 0, 0, 0, 0, 0, 0 },
00224 { 0, 0, 0, 0, 0, 0, 0 },
00225 { 0, 0, 0, 0, 0, 0, 0 },
00226 { 0, 0, 0, 0, 0, 0, 0 },
00227 { 0, 0, 0, 0, 0, 0, 0 },
00228 { 0, 3, 2, 1, 0, 0, 4 },
00229 { 0, 4, 7, 3, 2, 1, 6 },
00230 { 0, 3, 2, 6, 5, 1, 6 },
00231 { 0, 0, 0, 0, 0, 0, 0 },
00232 { 0, 3, 2, 1, 5, 4, 6 },
00233 { 2, 1, 5, 4, 7, 3, 6 },
00234 { 0, 3, 2, 6, 5, 4, 6 },
00235 { 0, 0, 0, 0, 0, 0, 0 },
00236 { 0, 3, 7, 6, 2, 1, 6 },
00237 { 0, 4, 7, 6, 2, 1, 6 },
00238 { 0, 3, 7, 6, 5, 1, 6 },
00239 { 0, 0, 0, 0, 0, 0, 0 },
00240 { 0, 0, 0, 0, 0, 0, 0 },
00241 { 0, 0, 0, 0, 0, 0, 0 },
00242 { 0, 0, 0, 0, 0, 0, 0 },
00243 { 0, 0, 0, 0, 0, 0, 0 },
00244 { 4, 5, 6, 7, 0, 0, 4 },
00245 { 4, 5, 6, 7, 3, 0, 6 },
00246 { 1, 2, 6, 7, 4, 5, 6 },
00247 { 0, 0, 0, 0, 0, 0, 0 },
00248 { 0, 1, 5, 6, 7, 4, 6 },
00249 { 0, 1, 5, 6, 7, 3, 6 },
00250 { 0, 1, 2, 6, 7, 4, 6 },
00251 { 0, 0, 0, 0, 0, 0, 0 },
00252 { 2, 3, 7, 4, 5, 6, 6 },
00253 { 0, 4, 5, 6, 2, 3, 6 },
00254 { 1, 2, 3, 7, 4, 5, 6 }
00255 };
00256
00258 float
00259 pcl::visualization::viewScreenArea (
00260 const Eigen::Vector3d &eye,
00261 const Eigen::Vector3d &min_bb, const Eigen::Vector3d &max_bb,
00262 const Eigen::Matrix4d &view_projection_matrix, int width, int height)
00263 {
00264 Eigen::Vector4d bounding_box[8];
00265 bounding_box[0] = Eigen::Vector4d(min_bb.x (), min_bb.y (), min_bb.z (), 1.0);
00266 bounding_box[1] = Eigen::Vector4d(max_bb.x (), min_bb.y (), min_bb.z (), 1.0);
00267 bounding_box[2] = Eigen::Vector4d(max_bb.x (), max_bb.y (), min_bb.z (), 1.0);
00268 bounding_box[3] = Eigen::Vector4d(min_bb.x (), max_bb.y (), min_bb.z (), 1.0);
00269 bounding_box[4] = Eigen::Vector4d(min_bb.x (), min_bb.y (), max_bb.z (), 1.0);
00270 bounding_box[5] = Eigen::Vector4d(max_bb.x (), min_bb.y (), max_bb.z (), 1.0);
00271 bounding_box[6] = Eigen::Vector4d(max_bb.x (), max_bb.y (), max_bb.z (), 1.0);
00272 bounding_box[7] = Eigen::Vector4d(min_bb.x (), max_bb.y (), max_bb.z (), 1.0);
00273
00274
00275 int pos = ((eye.x () < bounding_box[0].x ()) )
00276 + ((eye.x () > bounding_box[6].x ()) << 1)
00277 + ((eye.y () < bounding_box[0].y ()) << 2)
00278 + ((eye.y () > bounding_box[6].y ()) << 3)
00279 + ((eye.z () < bounding_box[0].z ()) << 4)
00280 + ((eye.z () > bounding_box[6].z ()) << 5);
00281
00282
00283 int num = hull_vertex_table[pos][6];
00284 if (num == 0)
00285 {
00286 return (float (width * height));
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 Eigen::Vector2d dst[8];
00343 for (int i = 0; i < num; i++)
00344 {
00345 Eigen::Vector4d world_pt = bounding_box[hull_vertex_table[pos][i]];
00346 Eigen::Vector2i screen_pt = pcl::visualization::worldToView(world_pt, view_projection_matrix, width, height);
00347
00348 dst[i] = Eigen::Vector2d(screen_pt.x (), screen_pt.y ());
00349 }
00350
00351 double sum = 0.0;
00352 for (int i = 0; i < num; ++i)
00353 {
00354 sum += (dst[i].x () - dst[(i+1) % num].x ()) * (dst[i].y () + dst[(i+1) % num].y ());
00355 }
00356
00357 return (fabsf (float (sum * 0.5f)));
00358 }
00359
00361 void
00362 pcl::visualization::Camera::computeViewMatrix (Eigen::Matrix4d &view_mat) const
00363 {
00364
00365
00366 Eigen::Vector3d focal_point (focal[0], focal[1], focal[2]);
00367 Eigen::Vector3d posv (pos[0] , pos[1] , pos[2]);
00368 Eigen::Vector3d up (view[0] , view[1] , view[2]);
00369
00370 Eigen::Vector3d zAxis = (focal_point - posv).normalized();
00371 Eigen::Vector3d xAxis = zAxis.cross(up).normalized();
00372
00373 Eigen::Vector3d yAxis = xAxis.cross (zAxis);
00374
00375 view_mat.block <1, 3> (0, 0) = xAxis;
00376 view_mat.block <1, 3> (1, 0) = yAxis;
00377 view_mat.block <1, 3> (2, 0) = -zAxis;
00378 view_mat.row (3) << 0, 0, 0, 1;
00379
00380 view_mat.block <3, 1> (0, 3) = view_mat.topLeftCorner<3, 3> () * (-posv);
00381 }
00382
00384 void
00385 pcl::visualization::Camera::computeProjectionMatrix (Eigen::Matrix4d& proj) const
00386 {
00387 float top = static_cast<float> (clip[0]) * tanf (0.5f * static_cast<float> (fovy));
00388 float left = -top * static_cast<float> (window_size[0] / window_size[1]);
00389 float right = -left;
00390 float bottom = -top;
00391
00392 float temp1, temp2, temp3, temp4;
00393 temp1 = 2.0f * static_cast<float> (clip[0]);
00394 temp2 = 1.0f / (right - left);
00395 temp3 = 1.0f / (top - bottom);
00396 temp4 = 1.0f / static_cast<float> (clip[1] - clip[0]);
00397
00398 proj.setZero ();
00399
00400 proj(0,0) = temp1 * temp2;
00401 proj(1,1) = temp1 * temp3;
00402 proj(0,2) = (right + left) * temp2;
00403 proj(1,2) = (top + bottom) * temp3;
00404 proj(2,2) = (-clip[1] - clip[0]) * temp4;
00405 proj(3,2) = -1.0;
00406 proj(2,3) = (-temp1 * clip[1]) * temp4;
00407 }