| 41 | }; |
| 42 | |
| 43 | Batcher::Batcher(const std::vector<Primitive*>& primitives) { |
| 44 | |
| 45 | FullscreenPrimitive fullscreen; |
| 46 | |
| 47 | const std::size_t numberOfPrimitives = primitives.size(); |
| 48 | mBatches.reserve(numberOfPrimitives); |
| 49 | |
| 50 | std::vector<Batch> batchCandidates; |
| 51 | batchCandidates.reserve(numberOfPrimitives); |
| 52 | |
| 53 | for (std::vector<Primitive*>::const_iterator itr = primitives.begin(); itr != primitives.end(); ++itr) { |
| 54 | |
| 55 | // primitive completely outside viewport, no need to process it any further |
| 56 | if (!Algo::intersectXY(*itr, &fullscreen)) |
| 57 | continue; |
| 58 | |
| 59 | // fullscreen is completely part of the primitive's bounding box, |
| 60 | // no other primitive can be part of the same batch |
| 61 | if (Algo::containsXY(&fullscreen, *itr)) { |
| 62 | mBatches.push_back(Batch()); |
| 63 | Batch& batch = mBatches.back(); |
| 64 | batch.push_back(*itr); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | std::vector<Batch>::iterator batchItr = batchCandidates.begin(); |
| 69 | std::vector<Batch>::iterator batchEnd = batchCandidates.end(); |
| 70 | |
| 71 | for ( ; batchItr != batchEnd; ++batchItr) { |
| 72 | |
| 73 | Batch & batch = *batchItr; |
| 74 | Batch::const_iterator batchPrimitives = batch.begin(); |
| 75 | Batch::const_iterator batchPrimitivesEnd = batch.end(); |
| 76 | |
| 77 | // if the primitive does not intersect any of the primitives in |
| 78 | // the current batch, we can add the primitive to that batch |
| 79 | for ( ; batchPrimitives != batchPrimitivesEnd; ++batchPrimitives) { |
| 80 | if (Algo::intersectXY(*itr, *batchPrimitives)) { |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | if (batchPrimitives == batchPrimitivesEnd) { |
| 85 | batch.push_back(*itr); |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // primitive could not added to any batch -> create a new batch |
| 91 | if (batchItr == batchEnd) { |
| 92 | batchCandidates.push_back(Batch()); |
| 93 | Batch& batch = batchCandidates.back(); |
| 94 | batch.push_back(*itr); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Could do here the following. But since since batchCandidates is not needed anymore, |
| 100 | // we can use the std::vector swap trick to avoid calling of the copy constructor. |
nothing calls this directly
no test coverage detected