forked from MartinezTorres/uSnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mitosis.hpp
87 lines (67 loc) · 1.47 KB
/
mitosis.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once
#include <opencv2/opencv.hpp>
template<typename T>
struct PlanarCluster_ {
typedef cv::Vec<T, 3> Vec3;
typedef cv::Matx<T, 3, 3> Mat33;
Vec3 Acenter;
Vec3 center;
Vec3 normal;
Mat33 aggMat;
Vec3 aggVec;
int size;
PlanarCluster_() : Acenter(0,0,0), center(0,0,0), normal(0,0,0), aggMat(0.), aggVec(0.), size(0) {}
double dist(Vec3 p) {
return (p-center).dot(p-center);
}
double isOnPlane(Vec3 p) {
return abs((p-center).dot(normal));
}
void update() {
auto mNorm = aggMat.inv()*aggVec;
normal = { mNorm(0), mNorm(1), -1 };
normal *= 1/cv::norm(normal);
if (size) center = Acenter * T(1./size);
}
void push(Vec3 p) {
auto x=p[0], y=p[1], z=p[2];
aggMat(0,0) += x*x;
aggMat(1,0) += x*y;
aggMat(2,0) += x;
aggMat(0,1) += x*y;
aggMat(1,1) += y*y;
aggMat(2,1) += y;
aggMat(0,2) += x;
aggMat(1,2) += y;
aggMat(2,2) += 1;
aggVec(0) += x*z;
aggVec(1) += y*z;
aggVec(2) += z;
Acenter += p;
size++;
}
void pop(Vec3 p) {
auto x=p[0], y=p[1], z=p[2];
aggMat(0,0) -= x*x;
aggMat(1,0) -= x*y;
aggMat(2,0) -= x;
aggMat(0,1) -= x*y;
aggMat(1,1) -= y*y;
aggMat(2,1) -= y;
aggMat(0,2) -= x;
aggMat(1,2) -= y;
aggMat(2,2) -= 1;
aggVec(0) -= x*z;
aggVec(1) -= y*z;
aggVec(2) -= z;
center -= p;
size--;
}
void push(const PlanarCluster_ &c) {
aggMat += c.aggMat;
aggVec += c.aggVec;
Acenter += c.Acenter;
size += c.size;
}
};
typedef PlanarCluster_<double> PlanarCluster;