summaryrefslogtreecommitdiffstats
path: root/cc/output/bsp_tree.h
blob: 695c47e5b9fbddc85e934ab421ecdecdae684277 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_OUTPUT_BSP_TREE_H_
#define CC_OUTPUT_BSP_TREE_H_

#include <stddef.h>

#include <deque>
#include <vector>

#include "base/memory/scoped_ptr.h"
#include "cc/output/bsp_compare_result.h"
#include "cc/quads/draw_polygon.h"

namespace cc {

struct BspNode {
  // This represents the splitting plane.
  scoped_ptr<DrawPolygon> node_data;
  // This represents any coplanar geometry we found while building the BSP.
  std::vector<scoped_ptr<DrawPolygon>> coplanars_front;
  std::vector<scoped_ptr<DrawPolygon>> coplanars_back;

  scoped_ptr<BspNode> back_child;
  scoped_ptr<BspNode> front_child;

  explicit BspNode(scoped_ptr<DrawPolygon> data);
  ~BspNode();
};

class CC_EXPORT BspTree {
 public:
  explicit BspTree(std::deque<scoped_ptr<DrawPolygon>>* list);
  scoped_ptr<BspNode>& root() { return root_; }

  template <typename ActionHandlerType>
  void TraverseWithActionHandler(ActionHandlerType* action_handler) const {
    if (root_) {
      WalkInOrderRecursion<ActionHandlerType>(action_handler, root_.get());
    }
  }

  ~BspTree();

 private:
  scoped_ptr<BspNode> root_;

  void FromList(std::vector<scoped_ptr<DrawPolygon>>* list);
  void BuildTree(BspNode* node, std::deque<scoped_ptr<DrawPolygon>>* data);

  template <typename ActionHandlerType>
  void WalkInOrderAction(ActionHandlerType* action_handler,
                         DrawPolygon* item) const {
    (*action_handler)(item);
  }

  template <typename ActionHandlerType>
  void WalkInOrderVisitNodes(
      ActionHandlerType* action_handler,
      const BspNode* node,
      const BspNode* first_child,
      const BspNode* second_child,
      const std::vector<scoped_ptr<DrawPolygon>>& first_coplanars,
      const std::vector<scoped_ptr<DrawPolygon>>& second_coplanars) const {
    if (first_child) {
      WalkInOrderRecursion(action_handler, first_child);
    }
    for (size_t i = 0; i < first_coplanars.size(); i++) {
      WalkInOrderAction(action_handler, first_coplanars[i].get());
    }
    WalkInOrderAction(action_handler, node->node_data.get());
    for (size_t i = 0; i < second_coplanars.size(); i++) {
      WalkInOrderAction(action_handler, second_coplanars[i].get());
    }
    if (second_child) {
      WalkInOrderRecursion(action_handler, second_child);
    }
  }

  template <typename ActionHandlerType>
  void WalkInOrderRecursion(ActionHandlerType* action_handler,
                            const BspNode* node) const {
    // If our view is in front of the the polygon
    // in this node then walk back then front.
    if (GetCameraPositionRelative(*(node->node_data)) == BSP_FRONT) {
      WalkInOrderVisitNodes<ActionHandlerType>(action_handler,
                                               node,
                                               node->back_child.get(),
                                               node->front_child.get(),
                                               node->coplanars_front,
                                               node->coplanars_back);
    } else {
      WalkInOrderVisitNodes<ActionHandlerType>(action_handler,
                                               node,
                                               node->front_child.get(),
                                               node->back_child.get(),
                                               node->coplanars_back,
                                               node->coplanars_front);
    }
  }

  // Returns whether or not nodeA is on one or the other side of nodeB,
  // coplanar, or whether it crosses nodeB's plane and needs to be split
  static BspCompareResult GetNodePositionRelative(const DrawPolygon& node_a,
                                                  const DrawPolygon& node_b);
  // Returns whether or not our viewer is in front of or behind the plane
  // defined by this polygon/node
  static BspCompareResult GetCameraPositionRelative(const DrawPolygon& node);
};

}  // namespace cc

#endif  // CC_OUTPUT_BSP_TREE_H_