Plane.cpp
Go to the documentation of this file.
1 #include "Plane.h"
2 //#include "pca.h"
3 #include "LevMarFitting.h"
5 #include <GfxTL/Mean.h>
6 #include <GfxTL/Plane.h>
7 #include <GfxTL/VectorXD.h>
8 #include <MiscLib/Performance.h>
9 
11 {
12  m_normal = (p2 - p1).cross(p3 - p2);
14  m_pos = p1;
16 }
17 
19 {
20  m_normal = normal;
21  m_pos = p1;
23 }
24 
26 {
27 }
28 
29 bool
31 {
32  m_normal = (p2 - p1).cross(p3 - p2);
33  if (m_normal.sqrLength() < 1E-6f)
34  {
35  return false;
36  }
38  m_pos = p1;
40  return true;
41 }
42 
43 bool
45 {
46  if (samples.size() < 6)
47  {
48  return false;
49  }
50  return Init(samples[0], samples[1], samples[2]);
51 }
52 
53 bool
55 {
56  if (samples.size() < 1)
57  {
58  return false;
59  }
60  m_normal = Vec3f(0, 0, 0);
61  m_pos = Vec3f(0, 0, 0);
62  size_t c = samples.size() / 2;
64  for (intptr_t i = 0; i < c; ++i)
65  {
66  normals[i] = GfxTL::Vector3Df(samples[i + c]);
67  }
68  GfxTL::Vector3Df meanNormal;
69  GfxTL::MeanOfNormals(normals.begin(), normals.end(), &meanNormal);
70  m_normal = Vec3f(meanNormal.Data());
72  GfxTL::Mean(samples.begin(), samples.begin() + c, &mean);
73  m_pos = Vec3f(mean.Data());
75  return true;
76 }
77 
78 bool
79 Plane::Init(bool binary, std::istream* i)
80 {
81  if (binary)
82  {
83  i->read((char*)&m_normal, sizeof(m_normal));
84  i->read((char*)&m_dist, sizeof(m_dist));
85  i->read((char*)&m_pos, sizeof(m_pos));
86  }
87  else
88  {
89  for (size_t j = 0; j < 3; ++j)
90  {
91  (*i) >> m_normal[j];
92  }
93  (*i) >> m_dist;
94  for (size_t j = 0; j < 3; ++j)
95  {
96  (*i) >> m_pos[j];
97  }
98  }
99  return true;
100 }
101 
102 void
103 Plane::Init(float* array)
104 {
105  for (int i = 0; i < 3; i++)
106  {
107  m_normal[i] = array[i];
108  m_pos[i] = array[i + 4];
109  }
110  m_dist = array[3];
111 }
112 
113 void
114 Plane::Init(FILE* i)
115 {
116  fread(&m_normal, sizeof(m_normal), 1, i);
117  fread(&m_dist, sizeof(m_dist), 1, i);
118  fread(&m_pos, sizeof(m_pos), 1, i);
119 }
120 
121 bool
123 {
124  return ((other.getNormal().dot(getNormal()) > 0.90) &&
125  (getDistance(other.getPosition()) < 0.2));
126 }
127 
128 template <class WeightT>
129 class LevMarPlane : public WeightT
130 {
131 public:
132  enum
133  {
135  };
136 
137  typedef float ScalarType;
138 
140  {
141  }
142 
143  // parametrization:
144  // params[0] - params[2] = normal vector
145  // params[3] = dist to origin
146 
147  template <class IteratorT>
148  ScalarType
149  Chi(const ScalarType* params,
150  IteratorT begin,
151  IteratorT end,
153  ScalarType* temp) const
154  {
155  ScalarType chi = 0;
156  int size = end - begin;
157 #pragma omp parallel for schedule(static) reduction(+ : chi)
158  for (int idx = 0; idx < size; ++idx)
159  {
160  temp[idx] = params[0] * begin[idx][0] + params[1] * begin[idx][1] +
161  params[2] * begin[idx][2] - params[3];
162  chi += (values[idx] = WeightT::Weigh(temp[idx])) * values[idx];
163  }
164  return chi;
165  }
166 
167  template <class IteratorT>
168  void
169  Derivatives(const ScalarType* params,
170  IteratorT begin,
171  IteratorT end,
172  const ScalarType* values,
173  const ScalarType* temp,
174  ScalarType* matrix) const
175  {
176  int size = end - begin;
177 #pragma omp parallel for schedule(static)
178  for (int idx = 0; idx < size; ++idx)
179  {
180  matrix[idx * NumParams + 0] = begin[idx][0];
181  matrix[idx * NumParams + 1] = begin[idx][1];
182  matrix[idx * NumParams + 2] = begin[idx][2];
183  matrix[idx * NumParams + 3] = -1;
184  WeightT::template DerivWeigh<NumParams>(temp[idx], matrix + idx * NumParams);
185  }
186  }
187 
188  void
189  Normalize(ScalarType* params) const
190  {
191  ScalarType len =
192  std::sqrt(params[0] * params[0] + params[1] * params[1] + params[2] * params[2]);
193  params[0] /= len;
194  params[1] /= len;
195  params[2] /= len;
196  }
197 };
198 
199 bool
203 {
204  LeastSquaresFit(GfxTL::IndexIterate(begin, pc.begin()), GfxTL::IndexIterate(end, pc.begin()));
205  return true;
206 }
207 
208 bool
210  const MiscLib::Vector<float>& weights,
211  Plane* ip)
212 {
213  Vec3f normal(0, 0, 0);
214  Vec3f position(0, 0, 0);
215  for (size_t i = 0; i < planes.size(); ++i)
216  {
217  normal += weights[i] * planes[i].getNormal();
218  position += weights[i] * planes[i].getPosition();
219  }
220  normal.normalize();
221  *ip = Plane(position, normal);
222  return true;
223 }
224 
225 void
226 Plane::Serialize(bool binary, std::ostream* o) const
227 {
228  if (binary)
229  {
230  o->write((const char*)&m_normal, sizeof(m_normal));
231  o->write((const char*)&m_dist, sizeof(m_dist));
232  o->write((const char*)&m_pos, sizeof(m_pos));
233  }
234  else
235 #ifdef DIRK_FORMAT
236  {
237  (*o) << std::endl
238  << "n " << m_normal[0] << " " << m_normal[1] << " " << m_normal[2] << std::endl
239  << "d " << m_dist << std::endl
240  << "p ";
241  for (size_t i = 0; i < 3; ++i)
242  {
243  (*o) << m_pos[i] << " ";
244  }
245  (*o) << std::endl;
246  }
247 #else
248  {
249  (*o) << m_normal[0] << " " << m_normal[1] << " " << m_normal[2] << " " << m_dist << " ";
250  for (size_t i = 0; i < 3; ++i)
251  {
252  (*o) << m_pos[i] << " ";
253  }
254  }
255 #endif
256 }
257 
258 size_t
260 {
261  return sizeof(Vec3f) + sizeof(float) + sizeof(Vec3f);
262 }
263 
264 size_t
266 {
267  return 7;
268 }
269 
270 void
271 Plane::Serialize(float* array) const
272 {
273  for (int i = 0; i < 3; i++)
274  {
275  array[i] = m_normal[i];
276  array[i + 4] = m_pos[i];
277  }
278  array[3] = m_dist;
279 }
280 
281 void
282 Plane::Serialize(FILE* o) const
283 {
284  fwrite(&m_normal, sizeof(m_normal), 1, o);
285  fwrite(&m_dist, sizeof(m_dist), 1, o);
286  fwrite(&m_pos, sizeof(m_pos), 1, o);
287 }
288 
289 void
290 Plane::Transform(float scale, const Vec3f& translate)
291 {
292  m_pos *= scale;
293  m_pos += translate;
295 }
296 
297 float
298 Plane::Intersect(const Vec3f& p, const Vec3f& r) const
299 {
300  return -SignedDistance(p) / (m_normal.dot(r));
301 }
GfxTL::VectorXD
Definition: MatrixXX.h:24
GfxTL::Vec3f
VectorXD< 3, float > Vec3f
Definition: VectorXD.h:733
MiscLib::Vector::begin
T * begin()
Definition: Vector.h:472
Mean.h
Vec3f::normalize
float normalize()
Definition: basic.h:141
LevMarPlane::Chi
ScalarType Chi(const ScalarType *params, IteratorT begin, IteratorT end, ScalarType *values, ScalarType *temp) const
Definition: Plane.cpp:149
Vec3f
Definition: basic.h:17
Performance.h
LevMarPlane::ScalarType
float ScalarType
Definition: Plane.cpp:137
Plane::LeastSquaresFit
bool LeastSquaresFit(const PointCloud &pc, MiscLib::Vector< size_t >::const_iterator begin, MiscLib::Vector< size_t >::const_iterator end)
Definition: Plane.cpp:200
Plane::Interpolate
static bool Interpolate(const MiscLib::Vector< Plane > &planes, const MiscLib::Vector< float > &weights, Plane *ip)
Definition: Plane.cpp:209
LevMarPlane
Definition: Plane.cpp:129
Plane::Transform
void Transform(float scale, const Vec3f &translate)
Definition: Plane.cpp:290
LevMarPlane::Derivatives
void Derivatives(const ScalarType *params, IteratorT begin, IteratorT end, const ScalarType *values, const ScalarType *temp, ScalarType *matrix) const
Definition: Plane.cpp:169
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
GfxTL::Vector3Df
VectorXD< 3, float > Vector3Df
Definition: VectorXD.h:718
Plane::InitAverage
bool InitAverage(const MiscLib::Vector< Vec3f > &samples)
Definition: Plane.cpp:54
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
LevMarPlane::NumParams
@ NumParams
Definition: Plane.cpp:134
LevMarPlane::LevMarPlane
LevMarPlane()
Definition: Plane.cpp:139
Plane.h
MiscLib::Vector
Definition: Vector.h:19
VectorXD.h
LevMarFitting.h
Plane::SerializedSize
static size_t SerializedSize()
Definition: Plane.cpp:259
Plane::Plane
Plane()
Definition: Plane.h:26
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:215
Plane::getPosition
const Vec3f & getPosition() const
Definition: Plane.h:78
GfxTL::Mean
void Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT *mean)
Definition: Mean.h:14
Plane::equals
bool equals(Plane plane)
Definition: Plane.cpp:122
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1620
cross
Point cross(const Point &x, const Point &y)
Definition: point.hpp:35
GfxTL::IndexIterate
IndexedIterator< IndexIteratorT, IteratorT > IndexIterate(IndexIteratorT idxIt, IteratorT it)
Definition: IndexedIterator.h:173
MiscLib::Vector::end
T * end()
Definition: Vector.h:484
Plane::SerializedFloatSize
static size_t SerializedFloatSize()
Definition: Plane.cpp:265
Plane::m_pos
Vec3f m_pos
Definition: Plane.h:118
IndexedIterator.h
PointCloud
Definition: PointCloud.h:85
Plane::Init
bool Init(Vec3f p1, Vec3f p2, Vec3f p3)
Definition: Plane.cpp:30
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
Plane::m_normal
Vec3f m_normal
Definition: Plane.h:117
Plane::Intersect
float Intersect(const Vec3f &p, const Vec3f &r) const
Definition: Plane.cpp:298
Vec3f::sqrLength
float sqrLength() const
Definition: basic.h:129
Vec3f::dot
float dot(const Vec3f &v) const
Definition: basic.h:104
Plane::SignedDistance
float SignedDistance(const Vec3f &pos) const
Definition: Plane.h:53
Plane::getNormal
const Vec3f & getNormal() const
Definition: Plane.h:72
Plane::Serialize
void Serialize(bool binary, std::ostream *o) const
Definition: Plane.cpp:226
Plane
Definition: Plane.h:18
LevMarPlane::Normalize
void Normalize(ScalarType *params) const
Definition: Plane.cpp:189
pc
Introduction Thank you for taking interest in our work and downloading this software This library implements the algorithm described in the paper R R R Klein Efficient RANSAC for Point Cloud Shape in Computer Graphics Blackwell June If you use this software you should cite the aforementioned paper in any resulting publication Please send comments or bug reports to Ruwen Roland BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE Example usage This section shows how to use the library to detect the shapes in a point cloud PointCloud pc
Definition: ReadMe.txt:68
Plane::m_dist
float m_dist
Definition: Plane.h:119
Plane::getDistance
float getDistance(const Vec3f &pos) const
Definition: Plane.h:41
Plane::~Plane
virtual ~Plane()
Definition: Plane.cpp:25
GfxTL::MeanOfNormals
bool MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights, MeanT *mean)
Definition: Mean.h:62