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>
9
11{
12 m_normal = (p2 - p1).cross(p3 - p2);
13 m_normal.normalize();
14 m_pos = p1;
15 m_dist = m_pos.dot(m_normal);
16}
17
19{
20 m_normal = normal;
21 m_pos = p1;
22 m_dist = m_pos.dot(m_normal);
23}
24
26{
27}
28
29bool
31{
32 m_normal = (p2 - p1).cross(p3 - p2);
33 if (m_normal.sqrLength() < 1E-6f)
34 {
35 return false;
36 }
37 m_normal.normalize();
38 m_pos = p1;
39 m_dist = m_pos.dot(m_normal);
40 return true;
41}
42
43bool
45{
46 if (samples.size() < 6)
47 {
48 return false;
49 }
50 return Init(samples[0], samples[1], samples[2]);
51}
52
53bool
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());
74 m_dist = m_pos.dot(m_normal);
75 return true;
76}
77
78bool
79Plane::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
102void
103Plane::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
113void
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
121bool
123{
124 return ((other.getNormal().dot(getNormal()) > 0.90) &&
125 (getDistance(other.getPosition()) < 0.2));
126}
127
128template <class WeightT>
129class LevMarPlane : public WeightT
130{
131public:
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>
149 Chi(const ScalarType* params,
150 IteratorT begin,
151 IteratorT end,
152 ScalarType* values,
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
199bool
207
208bool
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
225void
226Plane::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
258size_t
260{
261 return sizeof(Vec3f) + sizeof(float) + sizeof(Vec3f);
262}
263
264size_t
266{
267 return 7;
268}
269
270void
271Plane::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
281void
282Plane::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
289void
290Plane::Transform(float scale, const Vec3f& translate)
291{
292 m_pos *= scale;
293 m_pos += translate;
294 m_dist = m_pos.dot(m_normal);
295}
296
297float
298Plane::Intersect(const Vec3f& p, const Vec3f& r) const
299{
300 return -SignedDistance(p) / (m_normal.dot(r));
301}
constexpr T c
ScalarType Chi(const ScalarType *params, IteratorT begin, IteratorT end, ScalarType *values, ScalarType *temp) const
Definition Plane.cpp:149
void Normalize(ScalarType *params) const
Definition Plane.cpp:189
float ScalarType
Definition Plane.cpp:137
void Derivatives(const ScalarType *params, IteratorT begin, IteratorT end, const ScalarType *values, const ScalarType *temp, ScalarType *matrix) const
Definition Plane.cpp:169
size_type size() const
Definition Vector.h:215
static bool Interpolate(const MiscLib::Vector< Plane > &planes, const MiscLib::Vector< float > &weights, Plane *ip)
Definition Plane.cpp:209
static size_t SerializedFloatSize()
Definition Plane.cpp:265
Vec3f m_normal
Definition Plane.h:117
float getDistance(const Vec3f &pos) const
Definition Plane.h:41
Vec3f m_pos
Definition Plane.h:118
float Intersect(const Vec3f &p, const Vec3f &r) const
Definition Plane.cpp:298
static size_t SerializedSize()
Definition Plane.cpp:259
bool LeastSquaresFit(const PointCloud &pc, MiscLib::Vector< size_t >::const_iterator begin, MiscLib::Vector< size_t >::const_iterator end)
Definition Plane.cpp:200
const Vec3f & getPosition() const
Definition Plane.h:78
const Vec3f & getNormal() const
Definition Plane.h:72
void Transform(float scale, const Vec3f &translate)
Definition Plane.cpp:290
void Serialize(bool binary, std::ostream *o) const
Definition Plane.cpp:226
Plane()
Definition Plane.h:26
float m_dist
Definition Plane.h:119
bool InitAverage(const MiscLib::Vector< Vec3f > &samples)
Definition Plane.cpp:54
float SignedDistance(const Vec3f &pos) const
Definition Plane.h:53
bool Init(Vec3f p1, Vec3f p2, Vec3f p3)
Definition Plane.cpp:30
virtual ~Plane()
Definition Plane.cpp:25
bool equals(Plane plane)
Definition Plane.cpp:122
Definition basic.h:18
float normalize()
Definition basic.h:141
float dot(const Vec3f &v) const
Definition basic.h:104
void Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT *mean)
Definition Mean.h:14
VectorXD< 3, float > Vector3Df
Definition VectorXD.h:718
IndexedIterator< IndexIteratorT, IteratorT > IndexIterate(IndexIteratorT idxIt, IteratorT it)
bool MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights, MeanT *mean)
Definition Mean.h:62
Point cross(const Point &x, const Point &y)
Definition point.hpp:35