Skip to main content

Passing a user defined python object to C++ code(3)


How to access to the self.vertex_list.

This time I will explain how to access to the self.vertex_list of TriMesh. This is a list of length three numpy.array. We have already seen how to get the python list (dict's keylist) in my former article http://shitohichiumaya.blogspot.com/2010/08/boostpython-how-to-pass-python-object.html.

How to access to the float numpy.array element is the following.

void print_float32_3(boost::python::object const & float32_3_obj)
{
    // check sequence length is 3 or not.
    if(boost::python::len(float32_3_obj) != 3){
        std::string const objstr =
            boost::python::extract< std::string >(
                boost::python::str(float32_3_obj));
        std::cerr << "print_float32_3: arg is not a float[3] obj ["
                  << objstr << "]" << std::endl;
        return;
    }


    float vec[] = {0.0f, 0.0f, 0.0f};
    for(int i = 0; i < 3; ++i){
        vec[i] = boost::python::extract< float >(
            float32_3_obj.attr("__getitem__")(i));
    }
    std::cout << "float[3] = " << vec[0] << " " << vec[1] << " "
              << vec[2] << std::endl;
}

First, numpy.array is a sequence, therefore, we can ask length of it by boost::python::len(). We check the length is 3 or not since I defined it is three dimensional vector in the python code. Then we can access to each element through the attribute __getitem__. The python code puts float values to numpy.array, thus we can extract float value with extract< float >. Strictly speaking, numpy.array contains numpy.float64 instead of float, but, extract< float > still works here.

The last member of TriMesh, the triangle face index, should be done in the same way. Actually I would like to say that, but, there is a glitch here in numpy. We can not access this elements in the same way. We will get an errer:
TypeError: No registered converter was able to produce a C++ rvalue of type int from this Python object of type numpy.int64(). 
I would like to talk about this issue in the next blog.

Comments

Popular posts from this blog

Geometric Multiplicity: eignvectors (2)

If eigenvectors of a matrix A are independent, it is a happy property. Because the matrix A can be diagonalized with a matrix S that column vectors are eigenvectors of A . For example, Why this is a happy property of A? Because I can find A's power easily. A^{10} is not a big deal. Because Λ is a diagonal matrix and power of a diagonal matrix is quite simple. A^{10} = SΛ^{10} S^{-1} Then, why if I want to compute power of A ? That is the same reason to find eigenvectors. Eigenvectors are a basis of a matrix. A matrix can be represented by a single scalar. I repeat this again. This is the happy point, a matrix becomes a scalar. What can be simpler than a scalar value. But, this is only possible when the matrix S's columns are independent. Because S^{-1} must be exist. Now I come back to my first question. Is the λ's multiplicity related with the number of eigenvectors? This time I found this has the name. Geometric multiplicity (GM): the number of in...

Gauss's quote for positive, negative, and imaginary number

Recently I watched the following great videos about imaginary numbers by Welch Labs. https://youtu.be/T647CGsuOVU?list=PLiaHhY2iBX9g6KIvZ_703G3KJXapKkNaF I like this article about naming of math by Kalid Azad. https://betterexplained.com/articles/learning-tip-idea-name/ Both articles mentioned about Gauss, who suggested to use other names of positive, negative, and imaginary numbers. Gauss wrote these names are wrong and that is one of the reason people didn't get why negative times negative is positive, or, pure positive imaginary times pure positive imaginary is negative real number. I made a few videos about explaining why -1 * -1 = +1, too. Explanation: why -1 * -1 = +1 by pattern https://youtu.be/uD7JRdAzKP8 Explanation: why -1 * -1 = +1 by climbing a mountain https://youtu.be/uD7JRdAzKP8 But actually Gauss's insight is much powerful. The original is in the Gauß, Werke, Bd. 2, S. 178 . Hätte man +1, -1, √-1) nicht positiv, negative, imaginäre (oder gar um...

Tezuka Osamu's Black Jack, "Shrinking"

I like several novel authors. My first favorite author is probably Teduka, Osamu. I still love him. The list grows by adding Hoshi, Shinichi, Agatha Christie, Hermann Hesse, and so forth. My first favorite article of Tezuka was Atom as most of the (boy's) Tezuka fans did. But my favorite is Black Jack. I try to summarize one story, it is still quite vivid in my memory. I first read this story when I was 13 - 15 years old. I re-read it at least several times since Black Jack is composed of many short episodes. The title should be "ちぢむ (SHRINKING)" or it might be "縮む(Shrinking)". (It is not so convenient to translate this to English, since English does not have a system to say the exact same word in several ways. So I just simulate it with capital letters.) Black Jack is a genius surgeon, but he does not have the license. In short, his medical activity is illegal. His skill is top level in the world, but, the fee is also out-of-law expensive. In the story ...