Chromium Code Reviews| Index: cc/output/bsp_tree.cc |
| diff --git a/cc/output/bsp_tree.cc b/cc/output/bsp_tree.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cb49c4eb90992faa64fbc8cb4b4835e744f0d9d8 |
| --- /dev/null |
| +++ b/cc/output/bsp_tree.cc |
| @@ -0,0 +1,178 @@ |
| +// 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. |
| + |
| +#include "cc/output/bsp_tree.h" |
| + |
| +#include <list> |
| +#include <vector> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "cc/base/scoped_ptr_deque.h" |
| +#include "cc/base/scoped_ptr_vector.h" |
| +#include "cc/output/bsp_compare_result.h" |
| +#include "cc/output/bsp_controller.h" |
| +#include "cc/quads/draw_polygon.h" |
| + |
| +namespace cc { |
| + |
| +BspNode::BspNode(scoped_ptr<DrawPolygon> data) { |
| + node_data = data.Pass(); |
| +} |
| + |
| +BspNode::~BspNode() { |
| +} |
| + |
| +BspTree::BspTree(BspController* bsp_controller, |
| + ScopedPtrVector<DrawPolygon>* list) |
| + : controller_(bsp_controller) { |
| + FromList(list); |
| +} |
| + |
| +// FromList takes an input list and moves the better splitting polygons to the |
| +// front of the queue so when it comes time to build the tree, we already have |
| +// our splitting plane decided in the first element of the queue. BuildTree is |
| +// then called to perform the actual building of the tree using this list. |
| +void BspTree::FromList(ScopedPtrVector<DrawPolygon>* list) { |
| + if (list->size() == 0) |
| + return; |
| + |
| + float max_weight = 0.0f; |
| + ScopedPtrDeque<DrawPolygon> list_data; |
| + for (ScopedPtrVector<DrawPolygon>::iterator it = list->begin(); |
| + it != list->end(); |
| + ++it) { |
| + float poly_weight = controller_->SplitWeight(*list->back()); |
| + if (poly_weight > max_weight) { |
| + list_data.push_front(list->take(it)); |
| + max_weight = poly_weight; |
| + } else { |
| + list_data.push_back(list->take(it)); |
| + } |
| + } |
| + |
| + root_ = scoped_ptr<BspNode>(new BspNode(list_data.take_front())); |
| + BuildTree(root_, &list_data); |
| +} |
| + |
| +static void BuildHelper(float new_weight, |
|
Ian Vollick
2014/07/19 00:45:01
nit: how about UpdateNodeListAndMaxWeight? I know
troyhildebrandt
2014/07/21 19:16:50
Done.
|
| + float* current_max_weight, |
| + ScopedPtrDeque<DrawPolygon>* list, |
| + scoped_ptr<DrawPolygon> node_data) { |
| + if (new_weight > *current_max_weight) { |
| + list->push_front(node_data.Pass()); |
| + *current_max_weight = new_weight; |
| + } else { |
| + list->push_back(node_data.Pass()); |
| + } |
| +} |
| + |
| +// 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(const scoped_ptr<BspNode>& node, |
| + ScopedPtrDeque<DrawPolygon>* data) { |
| + ScopedPtrDeque<DrawPolygon> front_list; |
| + ScopedPtrDeque<DrawPolygon> back_list; |
| + // We keep track of the splitting weights (the "score" of a polygon that |
| + // chooses whether or not it's the splitting plane of a new node) for both |
| + // the front and back list independently so the better splitting plane is |
| + // chosen for both sides. |
| + float max_front_weight = 0.0f; |
| + float max_back_weight = 0.0f; |
| + |
| + // 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 (data->size() > 0) { |
| + // Is this particular polygon in front of or behind our splitting polygon. |
| + BspCompareResult comparer_result = controller_->GetNodePositionRelative( |
| + *data->front(), *(node->node_data)); |
| + float poly_weight = controller_->SplitWeight(*data->front()); |
| + |
| + // If it's clearly behind or in front of the splitting plane, we use the |
| + // heuristic to decide whether or not we should put it at the back |
| + // or front of the list. |
| + scoped_ptr<DrawPolygon> new_front; |
| + scoped_ptr<DrawPolygon> new_back; |
| + float front_poly_weight = 0.0f; |
| + float back_poly_weight = 0.0f; |
| + switch (comparer_result) { |
| + // Front. |
|
Ian Vollick
2014/07/19 00:45:01
These comments don't add much; I'd ditch them.
troyhildebrandt
2014/07/21 19:16:50
Done.
|
| + case BSP_FRONT: |
| + BuildHelper(poly_weight, |
| + &max_front_weight, |
| + &front_list, |
| + data->take_front().Pass()); |
| + break; |
| + // Back. |
| + case BSP_BACK: |
| + BuildHelper(poly_weight, |
| + &max_back_weight, |
| + &back_list, |
| + data->take_front().Pass()); |
| + break; |
| + // Split. |
| + case BSP_SPLIT: |
| + // Time to split this geometry, *it needs to be split by node_data. |
| + if (controller_->SplitPolygon(data->take_front(), |
| + *(node->node_data), |
| + &new_front, |
| + &new_back)) { |
| + // Now add new_front and new_back to their respective lists, again |
| + // using the heuristic in an attempt to choose the best splitting |
| + // plane, and keep track of the highest weighted polygon known so far |
| + // so if we find something better we can use that instead. |
| + front_poly_weight = controller_->SplitWeight(*new_front); |
| + back_poly_weight = controller_->SplitWeight(*new_back); |
| + |
| + BuildHelper(front_poly_weight, |
| + &max_front_weight, |
| + &front_list, |
| + new_front.Pass()); |
| + BuildHelper( |
| + back_poly_weight, &max_back_weight, &back_list, new_back.Pass()); |
| + } |
| + break; |
| + // Coplanar front. |
| + case BSP_COPLANAR_FRONT: |
| + node->coplanars_front.push_back(data->take_front()); |
| + break; |
| + // Coplanar back. |
| + case BSP_COPLANAR_BACK: |
| + node->coplanars_back.push_back(data->take_front()); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + break; |
| + } |
| + } |
| + |
| + // Build the back subtree using the front of the back_list as our splitter. |
| + if (back_list.size() > 0) { |
| + node->back_child = scoped_ptr<BspNode>(new BspNode(back_list.take_front())); |
| + BuildTree(node->back_child, &back_list); |
| + } |
| + |
| + // Build the front subtree using the front of the front_list as our splitter. |
| + if (front_list.size() > 0) { |
| + node->front_child = |
| + scoped_ptr<BspNode>(new BspNode(front_list.take_front())); |
| + BuildTree(node->front_child, &front_list); |
| + } |
| +} |
| + |
| +void BspTree::Clear() { |
| + if (root_) { |
| + root_.reset(); |
| + } |
| +} |
| + |
| +BspTree::~BspTree() { |
| + Clear(); |
| +} |
| + |
| +} // namespace cc |