1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_OUTPUT_BSP_TREE_H_
6 #define CC_OUTPUT_BSP_TREE_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "cc/base/scoped_ptr_deque.h"
12 #include "cc/base/scoped_ptr_vector.h"
13 #include "cc/output/bsp_compare_result.h"
14 #include "cc/quads/draw_polygon.h"
19 // This represents the splitting plane.
20 scoped_ptr
<DrawPolygon
> node_data
;
21 // This represents any coplanar geometry we found while building the BSP.
22 ScopedPtrVector
<DrawPolygon
> coplanars_front
;
23 ScopedPtrVector
<DrawPolygon
> coplanars_back
;
25 scoped_ptr
<BspNode
> back_child
;
26 scoped_ptr
<BspNode
> front_child
;
28 explicit BspNode(scoped_ptr
<DrawPolygon
> data
);
32 class CC_EXPORT BspTree
{
34 explicit BspTree(ScopedPtrDeque
<DrawPolygon
>* list
);
35 scoped_ptr
<BspNode
>& root() { return root_
; }
37 template <typename ActionHandlerType
>
38 void TraverseWithActionHandler(ActionHandlerType
* action_handler
) const {
40 WalkInOrderRecursion
<ActionHandlerType
>(action_handler
, root_
.get());
47 scoped_ptr
<BspNode
> root_
;
49 void FromList(ScopedPtrVector
<DrawPolygon
>* list
);
50 void BuildTree(BspNode
* node
, ScopedPtrDeque
<DrawPolygon
>* data
);
52 template <typename ActionHandlerType
>
53 void WalkInOrderAction(ActionHandlerType
* action_handler
,
54 DrawPolygon
* item
) const {
55 (*action_handler
)(item
);
58 template <typename ActionHandlerType
>
59 void WalkInOrderVisitNodes(
60 ActionHandlerType
* action_handler
,
62 const BspNode
* first_child
,
63 const BspNode
* second_child
,
64 const ScopedPtrVector
<DrawPolygon
>& first_coplanars
,
65 const ScopedPtrVector
<DrawPolygon
>& second_coplanars
) const {
67 WalkInOrderRecursion(action_handler
, first_child
);
69 for (size_t i
= 0; i
< first_coplanars
.size(); i
++) {
70 WalkInOrderAction(action_handler
, first_coplanars
[i
]);
72 WalkInOrderAction(action_handler
, node
->node_data
.get());
73 for (size_t i
= 0; i
< second_coplanars
.size(); i
++) {
74 WalkInOrderAction(action_handler
, second_coplanars
[i
]);
77 WalkInOrderRecursion(action_handler
, second_child
);
81 template <typename ActionHandlerType
>
82 void WalkInOrderRecursion(ActionHandlerType
* action_handler
,
83 const BspNode
* node
) const {
84 // If our view is in front of the the polygon
85 // in this node then walk back then front.
86 if (GetCameraPositionRelative(*(node
->node_data
)) == BSP_FRONT
) {
87 WalkInOrderVisitNodes
<ActionHandlerType
>(action_handler
,
89 node
->back_child
.get(),
90 node
->front_child
.get(),
91 node
->coplanars_front
,
92 node
->coplanars_back
);
94 WalkInOrderVisitNodes
<ActionHandlerType
>(action_handler
,
96 node
->front_child
.get(),
97 node
->back_child
.get(),
99 node
->coplanars_front
);
103 // Returns whether or not nodeA is on one or the other side of nodeB,
104 // coplanar, or whether it crosses nodeB's plane and needs to be split
105 static BspCompareResult
GetNodePositionRelative(const DrawPolygon
& node_a
,
106 const DrawPolygon
& node_b
);
107 // Returns whether or not our viewer is in front of or behind the plane
108 // defined by this polygon/node
109 static BspCompareResult
GetCameraPositionRelative(const DrawPolygon
& node
);
114 #endif // CC_OUTPUT_BSP_TREE_H_