1

我对这段代码有疑问:

#include "PolyVoxCore/MaterialDensityPair.h"
#include "PolyVoxCore/CubicSurfaceExtractorWithNormals.h"
#include "PolyVoxCore/SurfaceMesh.h"
#include "PolyVoxCore/SimpleVolume.h"

#include "geomVertexData.h"
#include "geomVertexWriter.h"
#include "geomTriangles.h"
#include "py_panda.h"
#include "pandaNode.h"
#include "geomNode.h"

//Use the PolyVox namespace
using namespace PolyVox;
using namespace std;

static void createSphereInVolume(SimpleVolume<MaterialDensityPair44>& volData, float fRadius)
{
        //This vector hold the position of the center of the volume
        Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2);

        //This three-level for loop iterates over every voxel in the volume
        for (int z = 0; z < volData.getWidth(); z++)
        {
                for (int y = 0; y < volData.getHeight(); y++)
                {
                        for (int x = 0; x < volData.getDepth(); x++)
                        {
                                //Store our current position as a vector...
                                Vector3DFloat v3dCurrentPos(x,y,z);    
                                //And compute how far the current position is from the center of the volume
                                float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();

                                uint8_t uDensity = 0;
                                uint8_t uMaterial = 0;

                                //If the current voxel is less than 'radius' units from the center then we make it solid.
                                if(fDistToCenter <= fRadius)
                                {
                                        //Our new density value
                                        uDensity = VoxelTypeTraits<MaterialDensityPair44>::maxDensity();
                                        uMaterial = 1;
                                }

                                //Get the old voxel
                                MaterialDensityPair44 voxel = volData.getVoxelAt(x,y,z);

                                //Modify the density and material
                                voxel.setDensity(uDensity);
                                voxel.setMaterial(uMaterial);

                                //Write the voxel value into the volume
                                volData.setVoxelAt(x, y, z, voxel);
                        }
                }
        }
}

static PyObject* CreateSphere(PyObject* self, PyObject* args)
{
        PyObject* pandaNodeWrapper;
        if (!PyArg_ParseTuple(args, "0", &pandaNodeWrapper))
                return NULL;


        //Create an empty volume and then place a sphere in it
        SimpleVolume<MaterialDensityPair44> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63)));
        createSphereInVolume(volData, 30);

        //Extract the surface
        SurfaceMesh<PositionMaterialNormal> mesh;
        CubicSurfaceExtractorWithNormals<SimpleVolume, MaterialDensityPair44 > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
        surfaceExtractor.execute();
        const vector<PositionMaterialNormal>& vertices = mesh.getVertices();
        const vector<uint32_t>& indices = mesh.getIndices();

        CPT(GeomVertexFormat) vertexFormat = GeomVertexFormat::get_v3n3();
        PT(GeomVertexData) vertexData = new GeomVertexData("ProceduralMesh", vertexFormat, GeomEnums::UH_static);

        GeomVertexWriter vertex, normal;

        vertex = GeomVertexWriter(vertexData, "vertex");
        normal = GeomVertexWriter(vertexData, "normal");

        for(vector<PositionMaterialNormal>::const_iterator it = vertices.begin(); it != vertices.end(); ++it)
        {
                vertex.add_data3f(it->position.getX(), it->position.getY(), it->position.getZ());
                normal.add_data3f(it->normal.getX(), it->position.getY(), it->position.getZ());
        }

        PT(GeomTriangles) chunk = new GeomTriangles(GeomEnums::UH_static);

        for(int i = 0; i < indices.size(); ++i)
        {
                chunk->add_vertex(indices[i]);
        }

        PT(Geom) geom = new Geom(vertexData);
        geom->add_primitive(chunk);

        PT(GeomNode) geomNode = new GeomNode("chunk");

        PandaNode* node = ((PandaNode*)((Dtool_PyInstDef*)pandaNodeWrapper)->_ptr_to_object);
        node->add_child(geomNode);

        Py_RETURN_NONE;
}

static PyMethodDef module_methods[] = {
        {"CreateSphere", CreateSphere, METH_VARARGS, "CreateSphere"},
        {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initpolyvox(void)
{
        (void) Py_InitModule("polyvox", module_methods);
}

这是我正在尝试编写的 C++ 扩展模块,我使用 setup.py 方法编译并安装它,问题是当我在 python 中导入并运行它时,python 说“SystemError:动态模块未正确初始化”。

我知道这个错误应该是模块名和init函数名不对应,但是看代码就不是这样了。那么可能是什么问题呢?

我在 Windows 7 上,使用 python 2.7.2,该模块是在 VS2008 上编译的,但它需要一些 .lib 我必须用 vs2010 编译,因为需要一些现代 c++ 功能。

4

0 回答 0