Converting c++ to c# and procedural galaxy generation.

I am currently working on a project were I want to randomly generate a galaxy. Using the information found here I have been able to generate a rather good looking galaxy, however, I think that I could do better.

The website linked above contains source code on generating a galaxy, however, it is made in c++. I have been playing around with different aspects of this code for the last week, however, I have not been able to convert one section of the script into c#.

Bare with me, as the section is composed of a series of sections.

To start with the following line of code is presented in the .h script.

class CumulativeDistributionFunction
{
  public:
    typedef double (CumulativeDistributionFunction::*dist_fun_t)(double x);
  private:
    dist_fun_t m_pDistFun;
}

I did some research into ‘typedef’ and found that it is used to define a variable. In this case it is showing that m_pDistFun is a double. However, I do not understand what the ‘*’ and the extra (double x) is doing.

Another point of confusion is when the variable for m_pDistFun is set in the cpp script.

void CumulativeDistributionFunction::SetupRealistic(double I0, double k, double a, double RBulge, double min, double max, int nSteps)
{

m_pDistFun = &CumulativeDistributionFunction::Intensity;

}

What confuses me most about this is that the function “Intensity” takes a variable like shown bellow.

double Intensity(double x){
  return somevalue;
}

I believe that this can be done because of how 'm_pDistFun ’ is used in a calculation.

    y += h/3 * ((this->*m_pDistFun)(m_fMin + i*h) + 4*(this->*m_pDistFun)(m_fMin + (i+1)*h) + (this->*m_pDistFun)(m_fMin + (i+2)*h) );

I have no idea what ‘this->’ does. I think it is a value being put into the ‘Intensity’ calculation but I am not sure.

I would really appreciate if someone will c++ knowledge could have a look at the source code and explain to me what is happening so I can replicate the affects in c#.

dist_fun_t is a function pointer which takes double as a parameter and returns a double. This is how you can assign it to “CumulativeDistributionFunction::Intensity” function since the signature is valid for the case. You can think function pointers as delegates in c#. (Also i should mention that “functors” in c++ are more similar to delegates in c# since they are callable objects too.)

The expression “(this->m_pDistFun)(m_fMin + ih)” is how member function pointers are called.

http://www.learncpp.com/cpp-tutorial/78-function-pointers/

“this->” does the same for c++ and c#. “this” is the object itself in the class scope. (Also you might use “this.” sometimes instead of of “this->” if the member object is stack allocated.)

http://en.cppreference.com/w/cpp/language/this

Hope this will be useful.