File: bsp_tree.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 3,176 bytes parent folder | download | duplicates (11)
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
88
89
90
91
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/service/display/bsp_tree.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/containers/circular_deque.h"
#include "cc/base/container_util.h"
#include "components/viz/service/display/bsp_compare_result.h"
#include "components/viz/service/display/draw_polygon.h"

namespace viz {

BspNode::BspNode(std::unique_ptr<DrawPolygon> data)
    : node_data(std::move(data)) {}

BspNode::~BspNode() = default;

BspTree::BspTree(base::circular_deque<std::unique_ptr<DrawPolygon>>* list) {
  if (list->size() == 0)
    return;

  root_ = std::make_unique<BspNode>(cc::PopFront(list));
  BuildTree(root_.get(), list);
}

BspTree::~BspTree() = default;

// The idea behind using a deque for BuildTree's input is that we want to be
// able to place polygons that we've decided aren't splitting plane candidates
// at the back of the queue while moving the candidate splitting planes to the
// front when the heuristic decides that they're a better choice. This way we
// can always simply just take from the front of the deque for our node's
// data.
void BspTree::BuildTree(
    BspNode* node,
    base::circular_deque<std::unique_ptr<DrawPolygon>>* polygon_list) {
  base::circular_deque<std::unique_ptr<DrawPolygon>> front_list;
  base::circular_deque<std::unique_ptr<DrawPolygon>> back_list;

  // We take in a list of polygons at this level of the tree, and have to
  // find a splitting plane, then classify polygons as either in front of
  // or behind that splitting plane.
  while (!polygon_list->empty()) {
    std::unique_ptr<DrawPolygon> polygon;
    std::unique_ptr<DrawPolygon> new_front;
    std::unique_ptr<DrawPolygon> new_back;
    // Time to split this geometry, *it needs to be split by node_data.
    polygon = cc::PopFront(polygon_list);
    bool is_coplanar;
    node->node_data->SplitPolygon(std::move(polygon), &new_front, &new_back,
                                  &is_coplanar);
    if (is_coplanar) {
      if (new_front)
        node->coplanars_front.push_back(std::move(new_front));
      if (new_back)
        node->coplanars_back.push_back(std::move(new_back));
    } else {
      if (new_front)
        front_list.push_back(std::move(new_front));
      if (new_back)
        back_list.push_back(std::move(new_back));
    }
  }

  // Build the back subtree using the front of the back_list as our splitter.
  if (back_list.size() > 0) {
    node->back_child = std::make_unique<BspNode>(cc::PopFront(&back_list));
    BuildTree(node->back_child.get(), &back_list);
  }

  // Build the front subtree using the front of the front_list as our splitter.
  if (front_list.size() > 0) {
    node->front_child = std::make_unique<BspNode>(cc::PopFront(&front_list));
    BuildTree(node->front_child.get(), &front_list);
  }
}

// The base comparer with 0,0,0 as camera position facing forward
BspCompareResult BspTree::GetCameraPositionRelative(const DrawPolygon& node) {
  if (node.normal().z() > 0.0f) {
    return BSP_FRONT;
  }
  return BSP_BACK;
}

}  // namespace viz