root/OpenSceneGraph/branches/OpenSceneGraph-3.2/src/osgPlugins/3ds/WriterCompareTriangle.cpp @ 14038

Revision 14038, 5.1 kB (checked in by robert, 38 minutes ago)

Reordered method implemenations to make it easier to compare similar methods

  • Property svn:eol-style set to native
Line 
1#include "WriterCompareTriangle.h"
2#include <assert.h>
3
4WriterCompareTriangle::WriterCompareTriangle(const osg::Geode & geode, unsigned int nbVertices) : geode(geode)
5{
6    cutscene(nbVertices, geode.getBoundingBox());
7}
8
9bool
10WriterCompareTriangle::operator()(const std::pair<Triangle, int> & t1,
11                                  const std::pair<Triangle, int> & t2) const
12{
13    const osg::Geometry *g = geode.getDrawable( t1.second )->asGeometry();
14
15    const osg::Vec3Array * vecs= static_cast<const osg::Vec3Array *>(g->getVertexArray());
16    const osg::BoundingBox::vec_type v1( (*vecs)[t1.first.t1] );
17
18    if (t1.second != t2.second)
19    {
20        const osg::Geometry *g = geode.getDrawable( t2.second )->asGeometry();
21        vecs = static_cast<const osg::Vec3Array *>(g->getVertexArray());
22    };
23    const osg::BoundingBox::vec_type v2( (*vecs)[t2.first.t1] );
24    int val1 = inWhichBox(v1);
25    int val2 = inWhichBox(v2);
26
27    return (val1 < val2);
28}
29
30void
31WriterCompareTriangle::setMaxMin(int & nbVerticesX,
32                                 int & nbVerticesY,
33                                 int & nbVerticesZ) const
34{
35    static const int min = 1;
36    static const int max = 5;        // Number of blocks used to divide the scene (arbitrary but seems ok)
37    nbVerticesX = osg::clampBetween<int>(nbVerticesX, min, max);
38    nbVerticesY = osg::clampBetween<int>(nbVerticesY, min, max);
39    nbVerticesZ = osg::clampBetween<int>(nbVerticesZ, min, max);
40}
41
42void WriterCompareTriangle::cutscene(int nbVertices, const osg::BoundingBox & sceneBox)
43{
44    osg::BoundingBox::vec_type length = sceneBox._max - sceneBox._min;
45
46    static const float k = 1.3f;        // Arbitrary constant multiplier for density computation ("simulates" non-uniform point distributions)
47    // Computes "density" of points, and thus the number of blocks to divide the mesh into
48    int nbVerticesX = static_cast<int>( (nbVertices * k) / (length.z() * length.y()) );
49    int nbVerticesY = static_cast<int>( (nbVertices * k) / (length.z() * length.x()) );
50    int nbVerticesZ = static_cast<int>( (nbVertices * k) / (length.x() * length.y()) );
51
52    setMaxMin (nbVerticesX, nbVerticesY, nbVerticesZ); // This function prevent from cutting the scene in too many blocs
53
54    OSG_INFO
55        << "Cutting x by " << nbVerticesX << std::endl
56        << "Cutting y by " << nbVerticesY << std::endl
57        << "Cutting z by " << nbVerticesZ << std::endl;
58
59    osg::BoundingBox::value_type blocX = length.x() / nbVerticesX;    // These 3 lines set the size of a bloc in x, y and z
60    osg::BoundingBox::value_type blocY = length.y() / nbVerticesY;
61    osg::BoundingBox::value_type blocZ = length.z() / nbVerticesZ;
62
63    boxList.reserve(nbVerticesX * nbVerticesY * nbVerticesZ);
64    short yinc = 1;
65    short xinc = 1;
66    int y = 0;
67    int x = 0;
68    for (int z = 0; z < nbVerticesZ; ++z)
69    {
70        while (x < nbVerticesX && x >= 0)
71        {
72            while (y < nbVerticesY && y >= 0)
73            {
74                osg::BoundingBox::value_type xMin = sceneBox.xMin() + x * blocX;
75                if (x == 0) //to prevent from mesh with no case
76                    xMin -= 10;
77
78                osg::BoundingBox::value_type yMin = sceneBox.yMin() + y * blocY;
79                if (y == 0) //to prevent from mesh with no case
80                    yMin -= 10;
81
82                osg::BoundingBox::value_type zMin = sceneBox.zMin() + z * blocZ;
83                if (z == 0) //to prevent from mesh with no case
84                    zMin -= 10;
85
86                osg::BoundingBox::value_type xMax = sceneBox.xMin() + (x + 1) * blocX;
87                if (x == nbVerticesX - 1) //to prevent from mesh with no case
88                    xMax += 10;
89
90                osg::BoundingBox::value_type yMax = sceneBox.yMin() + (y + 1) * blocY;
91                if (y == nbVerticesY - 1) //to prevent from mesh with no case
92                    yMax += 10;
93
94                osg::BoundingBox::value_type zMax = sceneBox.zMin() + (z + 1) * blocZ;
95                if (z == nbVerticesZ - 1) //to prevent from mesh with no case
96                    zMax += 10;
97
98                boxList.push_back(osg::BoundingBox(xMin, // Add a bloc to the list
99                    yMin,
100                    zMin,
101                    xMax,
102                    yMax,
103                    zMax));
104                y += yinc;
105            }
106            yinc = -yinc;
107            y += yinc;
108            x += xinc;
109        }
110        xinc = -xinc;
111        x += xinc;
112    }
113}
114
115int
116WriterCompareTriangle::inWhichBox(const osg::BoundingBox::value_type x,
117                                  const osg::BoundingBox::value_type y,
118                                  const osg::BoundingBox::value_type z) const
119{
120    for (unsigned int i = 0; i < boxList.size(); ++i)
121    {
122        if (x >= boxList[i].xMin() &&
123            x <  boxList[i].xMax() &&
124            y >= boxList[i].yMin() &&
125            y <  boxList[i].yMax() &&
126            z >= boxList[i].zMin() &&
127            z <  boxList[i].zMax())
128        {
129            return i;
130        }
131    }
132    assert(false && "Point is not in any blocs");
133    return 0;
134}
135
136int WriterCompareTriangle::inWhichBox(const osg::BoundingBox::vec_type & point) const {
137    return inWhichBox(point.x(), point.y(), point.z());
138}
Note: See TracBrowser for help on using the browser.