Apollo 11.0
自动驾驶开放平台
segment_graph.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16/*
17Copyright (C) 2006 Pedro Felzenszwalb
18
19This program is free software; you can redistribute it and/or modify
20it under the terms of the GNU General Public License as published by
21the Free Software Foundation; either version 2 of the License, or
22(at your option) any later version.
23
24This program is distributed in the hope that it will be useful,
25but WITHOUT ANY WARRANTY; without even the implied warranty of
26MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27GNU General Public License for more details.
28
29You should have received a copy of the GNU General Public License
30along with this program; if not, write to the Free Software
31Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32*/
33
34#pragma once
35#include <algorithm>
36#include <cmath>
37
39
40namespace apollo {
41namespace perception {
42namespace lidar {
43
44// threshold function
45#define THRESHOLD(size, c) (c / size)
46
47typedef struct {
48 float w;
49 int a;
50 int b;
51} edge;
52
53bool operator<(const edge &a, const edge &b) {
54 return a.w < b.w;
55}
56
66Universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c) {
67 // sort edges by weight
68 std::sort(edges, edges + num_edges);
69
70 // make a disjoint-set forest
71 Universe *u = new Universe(num_vertices);
72
73 // init thresholds
74 float *threshold = new float[num_vertices];
75 for (int i = 0; i < num_vertices; i++) {
76 threshold[i] = THRESHOLD(1, c);
77 }
78
79 // for each edge, in non-decreasing weight order...
80 for (int i = 0; i < num_edges; i++) {
81 edge *pedge = &edges[i];
82
83 // components connected by this edge
84 int a = u->find(pedge->a);
85 int b = u->find(pedge->b);
86 if (a != b) {
87 if ((pedge->w <= threshold[a]) && (pedge->w <= threshold[b])) {
88 u->join(a, b);
89 a = u->find(a);
90 threshold[a] = pedge->w + THRESHOLD(u->size(a), c);
91 }
92 }
93 }
94
95 // free up
96 delete threshold;
97 return u;
98}
99
100} // namespace lidar
101} // namespace perception
102} // namespace apollo
int find(int x)
Find parent of x
void join(int x, int y)
Join set x and set y
int size(int x) const
Get size of set x
Universe * segment_graph(int num_vertices, int num_edges, edge *edges, float c)
Segment a graph
Image< uchar > * threshold(Image< T > *src, int t)
Threshold image
Definition imutil.h:79
bool operator<(const edge &a, const edge &b)
class register implement
Definition arena_queue.h:37
#define THRESHOLD(size, c)