| 1 | #include "WriterCompareTriangle.h" |
|---|
| 2 | |
|---|
| 3 | WriterCompareTriangle::WriterCompareTriangle(const osg::Geode& geode, |
|---|
| 4 | unsigned int nbVertices) |
|---|
| 5 | : geode(geode) |
|---|
| 6 | { |
|---|
| 7 | cutscene(nbVertices, geode.getDrawable(0)->asGeometry()->getBound()); |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | bool |
|---|
| 11 | WriterCompareTriangle::operator()(const std::pair<Triangle, int>& t1, |
|---|
| 12 | const std::pair<Triangle, int>& t2) const |
|---|
| 13 | { |
|---|
| 14 | const osg::Geometry* g = geode.getDrawable( t1.second )->asGeometry(); |
|---|
| 15 | |
|---|
| 16 | const osg::Vec3Array* vecs= static_cast<const osg::Vec3Array*>(g->getVertexArray()); |
|---|
| 17 | const osg::Vec3::value_type x1 = (*vecs)[t1.first.t1].x(); |
|---|
| 18 | const osg::Vec3::value_type y1 = (*vecs)[t1.first.t1].y(); |
|---|
| 19 | const osg::Vec3::value_type z1 = (*vecs)[t1.first.t1].z(); |
|---|
| 20 | |
|---|
| 21 | if (t1.second != t2.second) |
|---|
| 22 | { |
|---|
| 23 | const osg::Geometry* g = geode.getDrawable( t2.second )->asGeometry(); |
|---|
| 24 | vecs = static_cast<const osg::Vec3Array*>(g->getVertexArray()); |
|---|
| 25 | } |
|---|
| 26 | const osg::Vec3::value_type x2 = (*vecs)[t2.first.t1].x(); |
|---|
| 27 | const osg::Vec3::value_type y2 = (*vecs)[t2.first.t1].y(); |
|---|
| 28 | const osg::Vec3::value_type z2 = (*vecs)[t2.first.t1].z(); |
|---|
| 29 | int val1 = inWhichBox(x1,y1,z1); |
|---|
| 30 | int val2 = inWhichBox(x2,y2,z2); |
|---|
| 31 | |
|---|
| 32 | return (val1 < val2); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | void |
|---|
| 36 | WriterCompareTriangle::setMaxMin(unsigned int& nbVerticesX, |
|---|
| 37 | unsigned int& nbVerticesY, |
|---|
| 38 | unsigned int& nbVerticesZ) const |
|---|
| 39 | { |
|---|
| 40 | static const unsigned int min = 1; |
|---|
| 41 | if (nbVerticesX < min) |
|---|
| 42 | nbVerticesX = min; |
|---|
| 43 | if (nbVerticesY < min) |
|---|
| 44 | nbVerticesY = min; |
|---|
| 45 | if (nbVerticesZ < min) |
|---|
| 46 | nbVerticesZ = min; |
|---|
| 47 | |
|---|
| 48 | static const unsigned int max = 20; |
|---|
| 49 | |
|---|
| 50 | if (nbVerticesX > max) |
|---|
| 51 | nbVerticesX = max; |
|---|
| 52 | if (nbVerticesY > max) |
|---|
| 53 | nbVerticesY = max; |
|---|
| 54 | if (nbVerticesZ > max) |
|---|
| 55 | nbVerticesZ = max; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void |
|---|
| 59 | WriterCompareTriangle::cutscene(int nbVertices, |
|---|
| 60 | const osg::BoundingBox& sceneBox) |
|---|
| 61 | { |
|---|
| 62 | osg::BoundingBox::vec_type length = sceneBox._max - sceneBox._min; |
|---|
| 63 | |
|---|
| 64 | static const unsigned int k = 4; |
|---|
| 65 | |
|---|
| 66 | unsigned int nbVerticesX = (nbVertices * k) / (length.z() * length.y()); |
|---|
| 67 | unsigned int nbVerticesY = (nbVertices * k) / (length.z() * length.x()); |
|---|
| 68 | unsigned int nbVerticesZ = (nbVertices * k) / (length.x() * length.y()); |
|---|
| 69 | |
|---|
| 70 | setMaxMin (nbVerticesX, nbVerticesY, nbVerticesZ); |
|---|
| 71 | |
|---|
| 72 | OSG_DEBUG << "Cutting x by " << nbVerticesX << std::endl |
|---|
| 73 | << "Cutting y by " << nbVerticesY << std::endl |
|---|
| 74 | << "Cutting z by " << nbVerticesZ << std::endl; |
|---|
| 75 | |
|---|
| 76 | osg::BoundingBox::value_type blocX = length.x() / nbVerticesX; |
|---|
| 77 | osg::BoundingBox::value_type blocY = length.y() / nbVerticesY; |
|---|
| 78 | osg::BoundingBox::value_type blocZ = length.z() / nbVerticesZ; |
|---|
| 79 | |
|---|
| 80 | boxList.reserve(nbVerticesX * nbVerticesY * nbVerticesZ); |
|---|
| 81 | short yinc = 1; |
|---|
| 82 | short xinc = 1; |
|---|
| 83 | unsigned int y = 0; |
|---|
| 84 | unsigned int x = 0; |
|---|
| 85 | for (unsigned int z = 0; z < nbVerticesZ; ++z) |
|---|
| 86 | { |
|---|
| 87 | while (x < nbVerticesX && x >= 0) |
|---|
| 88 | { |
|---|
| 89 | while (y < nbVerticesY && y >= 0) |
|---|
| 90 | { |
|---|
| 91 | osg::BoundingBox::value_type xMin = sceneBox.xMin() + x * blocX; |
|---|
| 92 | if (x == 0) |
|---|
| 93 | xMin -= 10; |
|---|
| 94 | |
|---|
| 95 | osg::BoundingBox::value_type yMin = sceneBox.yMin() + y * blocY; |
|---|
| 96 | if (y == 0) |
|---|
| 97 | yMin -= 10; |
|---|
| 98 | |
|---|
| 99 | osg::BoundingBox::value_type zMin = sceneBox.zMin() + z * blocZ; |
|---|
| 100 | if (z == 0) |
|---|
| 101 | zMin -= 10; |
|---|
| 102 | |
|---|
| 103 | osg::BoundingBox::value_type xMax = sceneBox.xMin() + (x + 1) * blocX; |
|---|
| 104 | if (x == nbVerticesX - 1) |
|---|
| 105 | xMax += 10; |
|---|
| 106 | |
|---|
| 107 | osg::BoundingBox::value_type yMax = sceneBox.yMin() + (y + 1) * blocY; |
|---|
| 108 | if (y == nbVerticesY - 1) |
|---|
| 109 | yMax += 10; |
|---|
| 110 | |
|---|
| 111 | osg::BoundingBox::value_type zMax = sceneBox.zMin() + (z + 1) * blocZ; |
|---|
| 112 | if (z == nbVerticesZ - 1) |
|---|
| 113 | zMax += 10; |
|---|
| 114 | |
|---|
| 115 | boxList.push_back(osg::BoundingBox(xMin, |
|---|
| 116 | yMin, |
|---|
| 117 | zMin, |
|---|
| 118 | xMax, |
|---|
| 119 | yMax, |
|---|
| 120 | zMax)); |
|---|
| 121 | y += yinc; |
|---|
| 122 | } |
|---|
| 123 | yinc = -yinc; |
|---|
| 124 | y += yinc; |
|---|
| 125 | x += xinc; |
|---|
| 126 | } |
|---|
| 127 | xinc = -xinc; |
|---|
| 128 | x += xinc; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | int |
|---|
| 133 | WriterCompareTriangle::inWhichBox(const osg::Vec3::value_type x, |
|---|
| 134 | const osg::Vec3::value_type y, |
|---|
| 135 | const osg::Vec3::value_type z) const |
|---|
| 136 | { |
|---|
| 137 | for (unsigned int i = 0; i < boxList.size(); ++i) |
|---|
| 138 | { |
|---|
| 139 | if (x >= boxList[i].xMin() && |
|---|
| 140 | x < boxList[i].xMax() && |
|---|
| 141 | y >= boxList[i].yMin() && |
|---|
| 142 | y < boxList[i].yMax() && |
|---|
| 143 | z >= boxList[i].zMin() && |
|---|
| 144 | z < boxList[i].zMax()) |
|---|
| 145 | { |
|---|
| 146 | return i; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | throw "Point is not in any box"; |
|---|
| 150 | } |
|---|