Apollo 11.0
自动驾驶开放平台
graph_segmentor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
18
19#include <limits>
20
21#include "cyber/common/log.h"
22
23namespace apollo {
24namespace perception {
25namespace algorithm {
26
27namespace {
28float GetThreshold(const size_t sz, const float c) {
29 return c / static_cast<float>(sz);
30}
31} // namespace
32
33void GraphSegmentor::Init(const float initial_threshold) {
34 initial_threshold_ = initial_threshold;
35 thresholds_.reserve(kMaxVerticesNum);
36
37 thresholds_table_.resize(kMaxThresholdsNum);
38 thresholds_table_[0] = std::numeric_limits<float>::max();
39 for (size_t i = 1; i < kMaxThresholdsNum; ++i) {
40 thresholds_table_[i] = GetThreshold(i, initial_threshold_);
41 }
42}
43
44void GraphSegmentor::SegmentGraph(const int num_vertices, const int num_edges,
45 Edge* edges, bool need_sort) {
46 if (edges == nullptr) {
47 AERROR << "Input Null Edges.";
48 return;
49 }
50
51 if (need_sort) {
52 std::sort(edges, edges + num_edges);
53 }
54
55 universe_.Reset(num_vertices);
56
57 thresholds_.assign(num_vertices, initial_threshold_);
58 for (int i = 0; i < num_edges; ++i) {
59 Edge& edge = edges[i];
60 int a = universe_.Find(edge.a);
61 int b = universe_.Find(edge.b);
62 if (a == b) {
63 continue;
64 }
65 if (edge.w <= thresholds_[a] && edge.w <= thresholds_[b]) {
66 universe_.Join(a, b);
67 a = universe_.Find(a);
68 int size_a = universe_.GetSize(a);
69 thresholds_[a] =
70 edge.w + (size_a < static_cast<int>(kMaxThresholdsNum)
71 ? thresholds_table_[size_a]
72 : GetThreshold(size_a, initial_threshold_));
73 }
74 }
75}
76
77} // namespace algorithm
78} // namespace perception
79} // namespace apollo
void Init(const float initial_threshold)
void SegmentGraph(const int num_vertices, const int num_edges, Edge *edges, bool need_sort=true)
void Join(const int x, const int y)
void Reset(const int elements_num)
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37