Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GenVisiblePoints.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "GenVisiblePoints.hh"
26 #include <Utils/ThreeDOut.hh>
27 #include <Geometry/ProjectionParametersPerspective.hh>
28 #include <Geometry/ProjectionParametersFactory.hh>
29 
30 using namespace BIAS;
31 using namespace std;
32 
34 GeneratePoints(const vector<ProjectionParametersBase*>& pp,
35  const int numVisiblePointsPerCamera,
36  const int numTries,
37  const double minDistance,
38  const double maxDistance,
39  vector<HomgPoint3D>& points)
40 {
41  _rand.Reset();
42 
43  // for each camera
44  for (unsigned int i = 0; i < pp.size(); i++)
45  {
46  // count existing points visible by camera
47  int numVisiblePoints = 0;
48  for (unsigned int j = 0; j < points.size(); j++)
49  {
50  if (IsPointVisible(*pp[i], points[j]))
51  numVisiblePoints++;
52  }
53 
54  // try to add points not visible by any other camera
55  int tries = 0;
56  for (int j = 0; j < numTries; j++)
57  {
58  // enough points visible by this camera?
59  if (numVisiblePoints >= numVisiblePointsPerCamera)
60  break;
61 
62  HomgPoint3D point;
63  GenerateVisiblePoint(*pp[i], minDistance, maxDistance, point);
64 
65  // check if point is visible by other camera
66  bool visibleByOtherCamera = false;
67  for (unsigned int k = 0; k < i; k++)
68  {
69  if (IsPointVisible(*pp[k], point))
70  {
71  visibleByOtherCamera = true;
72  break;
73  }
74  }
75 
76  // add point to list if not visible by other camera
77  if (!visibleByOtherCamera)
78  {
79  points.push_back(point);
80  numVisiblePoints++;
81  }
82  tries++;
83  }
84 
85  // if not enough points could be generated that are not visible
86  // by other cameras, just add any points
87  while (numVisiblePoints < numVisiblePointsPerCamera)
88  {
89  HomgPoint3D point;
90  GenerateVisiblePoint(*pp[i], minDistance, maxDistance, point);
91 
92  points.push_back(point);
93  numVisiblePoints++;
94  }
95  }
96 }
97 
98 
99 void GenVisiblePoints::
100 GenerateVisiblePoint(const ProjectionParametersBase& projection,
101  double minDistance, double maxDistance,
102  HomgPoint3D& point)
103 {
104  unsigned int width, height;
105  projection.GetImageSize(width, height);
106 
107  double x = _rand.GetUniformDistributed(0, width - 1);
108  double y = _rand.GetUniformDistributed(0, height - 1);
109  double d = _rand.GetUniformDistributed(minDistance, maxDistance);
110 
111  Vector3<double> dir, pos;
112  projection.UnProjectToRay(HomgPoint2D(x, y), pos, dir);
113  dir.Normalize();
114  point = HomgPoint3D(projection.GetC() + d * dir);
115 }
116 
117 bool GenVisiblePoints::
118 IsPointVisible(const ProjectionParametersBase& pp,
119  const HomgPoint3D& point)
120 {
121  HomgPoint3D p3d = point;
122  p3d.Homogenize();
123  HomgPoint2D p2d;
124  return pp.DoesPointProjectIntoImage(p3d, p2d);
125 }
virtual BIAS::Vector3< double > GetC() const
Get projection center.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint3D.hh:308
virtual bool DoesPointProjectIntoImage(const HomgPoint3D &X, HomgPoint2D &x, bool IgnoreDistortion=false) const
Checks if 3D point projects into specified image and returns belonging 2D image point.
virtual void UnProjectToRay(const HomgPoint2D &pos, Vector3< double > &origin, Vector3< double > &direction, bool ignoreDistortion=false) const
Calculates the view ray, which belongs to the given position on the image plane, in global coordinate...
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
void GeneratePoints(const std::vector< ProjectionParametersBase * > &pp, const int numVisiblePointsPerCamera, const int numTries, const double minDistance, const double maxDistance, std::vector< HomgPoint3D > &points)
Generates 3D points visible by the given cameras.
class BIASGeometryBase_EXPORT HomgPoint2D
class BIASGeometryBase_EXPORT HomgPoint3D