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_
11 #include "base/memory/scoped_ptr.h"
12 #include "cc/base/scoped_ptr_deque.h"
13 #include "cc/base/scoped_ptr_vector.h"
14 #include "cc/output/bsp_compare_result.h"
15 #include "cc/quads/draw_polygon.h"
20 // This represents the splitting plane.
21 scoped_ptr
<DrawPolygon
> node_data
;
22 // This represents any coplanar geometry we found while building the BSP.
23 ScopedPtrVector
<DrawPolygon
> coplanars_front
;
24 ScopedPtrVector
<DrawPolygon
> coplanars_back
;
26 scoped_ptr
<BspNode
> back_child
;
27 scoped_ptr
<BspNode
> front_child
;
29 explicit BspNode(scoped_ptr
<DrawPolygon
> data
);
33 class CC_EXPORT BspTree
{
35 explicit BspTree(ScopedPtrDeque
<DrawPolygon
>* list
);
36 scoped_ptr
<BspNode
>& root() { return root_
; }
38 template <typename ActionHandlerType
>
39 void TraverseWithActionHandler(ActionHandlerType
* action_handler
) const {
41 WalkInOrderRecursion
<ActionHandlerType
>(action_handler
, root_
.get());
48 scoped_ptr
<BspNode
> root_
;
50 void FromList(ScopedPtrVector
<DrawPolygon
>* list
);
51 void BuildTree(BspNode
* node
, ScopedPtrDeque
<DrawPolygon
>* data
);
53 template <typename ActionHandlerType
>
54 void WalkInOrderAction(ActionHandlerType
* action_handler
,
55 DrawPolygon
* item
) const {
56 (*action_handler
)(item
);
59 template <typename ActionHandlerType
>
60 void WalkInOrderVisitNodes(
61 ActionHandlerType
* action_handler
,
63 const BspNode
* first_child
,
64 const BspNode
* second_child
,
65 const ScopedPtrVector
<DrawPolygon
>& first_coplanars
,
66 const ScopedPtrVector
<DrawPolygon
>& second_coplanars
) const {
68 WalkInOrderRecursion(action_handler
, first_child
);
70 for (size_t i
= 0; i
< first_coplanars
.size(); i
++) {
71 WalkInOrderAction(action_handler
, first_coplanars
[i
]);
73 WalkInOrderAction(action_handler
, node
->node_data
.get());
74 for (size_t i
= 0; i
< second_coplanars
.size(); i
++) {
75 WalkInOrderAction(action_handler
, second_coplanars
[i
]);
78 WalkInOrderRecursion(action_handler
, second_child
);
82 template <typename ActionHandlerType
>
83 void WalkInOrderRecursion(ActionHandlerType
* action_handler
,
84 const BspNode
* node
) const {
85 // If our view is in front of the the polygon
86 // in this node then walk back then front.
87 if (GetCameraPositionRelative(*(node
->node_data
)) == BSP_FRONT
) {
88 WalkInOrderVisitNodes
<ActionHandlerType
>(action_handler
,
90 node
->back_child
.get(),
91 node
->front_child
.get(),
92 node
->coplanars_front
,
93 node
->coplanars_back
);
95 WalkInOrderVisitNodes
<ActionHandlerType
>(action_handler
,
97 node
->front_child
.get(),
98 node
->back_child
.get(),
100 node
->coplanars_front
);
104 // Returns whether or not nodeA is on one or the other side of nodeB,
105 // coplanar, or whether it crosses nodeB's plane and needs to be split
106 static BspCompareResult
GetNodePositionRelative(const DrawPolygon
& node_a
,
107 const DrawPolygon
& node_b
);
108 // Returns whether or not our viewer is in front of or behind the plane
109 // defined by this polygon/node
110 static BspCompareResult
GetCameraPositionRelative(const DrawPolygon
& node
);
115 #endif // CC_OUTPUT_BSP_TREE_H_