14 #include <yarp/math/Math.h>
18 using namespace yarp::os;
19 using namespace yarp::sig;
20 using namespace yarp::math;
36 Data_t(
const vector<Vector> &points_,
37 const double epsilon_,
38 const size_t minpts_) :
39 points(points_), epsilon(epsilon_), minpts(minpts_) {
40 ids.assign(points.size(),(
int)PointType::unclassified);
58 shared_ptr<Node_t> node(
new Node_t());
65 void append(
const size_t index, shared_ptr<Epsilon_neighbours_t> en)
68 if (en->head==
nullptr)
75 en->tail.lock()->next=node;
83 shared_ptr<Data_t> augData)
86 for (
size_t i=0; i<augData->points.size(); i++)
89 for (
size_t j=0; j<augData->points[index].length(); j++)
91 d+=pow(augData->points[index][j]-augData->points[i][j],2.0);
93 if ((i!=index) && (sqrt(d)<=augData->epsilon))
102 void spread(
const size_t index, shared_ptr<Epsilon_neighbours_t> seeds,
103 const size_t id, shared_ptr<Data_t> augData)
106 if (
spread->num_members>=augData->minpts)
108 for (shared_ptr<Node_t> node=
spread->head; node!=
nullptr; node=node->next)
110 if ((augData->ids[node->index]==(
int)PointType::noise) ||
111 (augData->ids[node->index]==(
int)PointType::unclassified))
113 if (augData->ids[node->index]==(
int)PointType::unclassified)
115 append(node->index,seeds);
117 augData->ids[node->index]=(int)
id;
124 bool expand(
const size_t index,
const size_t id, shared_ptr<Data_t> augData)
127 if (seeds->num_members<augData->minpts)
129 augData->ids[index]=(int)PointType::noise;
134 augData->ids[index]=(int)
id;
135 for (shared_ptr<Node_t>
h=seeds->head;
h!=
nullptr;
h=
h->next)
137 augData->ids[
h->index]=(int)
id;
139 for (shared_ptr<Node_t>
h=seeds->head;
h!=
nullptr;
h=
h->next)
141 spread(
h->index,seeds,
id,augData);
152 map<size_t,set<size_t>> DBSCAN::cluster(
const vector<Vector> &
data,
153 const Property &options)
155 double epsilon=options.check(
"epsilon",Value(1.0)).asFloat64();
156 size_t minpts=(size_t)options.check(
"minpts",Value(2)).asInt32();
160 for (
size_t i=0; i<augData->points.size(); i++)
162 if (augData->ids[i]==(
int)dbscan::PointType::unclassified)
171 map<size_t,set<size_t>> clusters;
172 for (
size_t i=0; i<augData->points.size(); i++)
174 if (augData->ids[i]!=(
int)dbscan::PointType::noise)
176 clusters[augData->ids[i]].insert(i);
shared_ptr< Epsilon_neighbours_t > get_epsilon_neighbours(const size_t index, shared_ptr< Data_t > augData)
shared_ptr< Node_t > create_node(const size_t index)
void append(const size_t index, shared_ptr< Epsilon_neighbours_t > en)
bool expand(const size_t index, const size_t id, shared_ptr< Data_t > augData)
void spread(const size_t index, shared_ptr< Epsilon_neighbours_t > seeds, const size_t id, shared_ptr< Data_t > augData)
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
const vector< Vector > & points
Data_t(const vector< Vector > &points_, const double epsilon_, const size_t minpts_)
shared_ptr< Node_t > head
shared_ptr< Node_t > next