octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use Octave's interpreter in my C++ program...


From: John Swensen
Subject: Re: Use Octave's interpreter in my C++ program...
Date: Thu, 22 May 2008 17:41:39 -0400


On May 22, 2008, at 5:26 PM, address@hidden wrote:

Hello,
I would like to use Octave in a C++ multithreaded application. Is there something like this (see code below) in Octave so I can get an instance of Octave and use it throughout the life of my OctaveUser object? I'll even
take something close to this. :)

class OctaveUser
{
  private:
    OctaveInstance *p_oi;
  public:
   OctaveUser()
   {
       oi = OctaveFactory::createInstance();
   }
   void cppMethod()
   {
       Matrix m(2,2);
       m(0,0)=1; m(0,1)=2;
       m(1,0)=3; m(1,1)=4;
       oi.feval("invert", m);

       // call 'testFromCpp.m' file.
octave_value_list result = feval ("testFromCpp", octave_value (m),
1 );
       ...//do something with the value in C++
   }

   ~OctaveUser()
   {
       delete _oi;
   }
};

Thanks!
Molamini


Included is a bit of pseudo-code that I wrote to an Octave user offline from the mailing list that I think would be the "meat" of such a class. This listing, however, is unique to the Octave sources since the change to the sumbol_table in a singleton class as described in the explanation. Hope this helps. If I had more time, I would implement it, but would be willing to give help if you want to muck around with this. However, take note of David Bateman's warning about thread-safety. I suppose if you ensure you only call into octave one command at a time, then Bob is your uncle.

<begin previous email>
I think the better way of doing this would be to create a symbol_record
and insert it into the symbol_table singleton class.  Then you could use
the octave_call() function one the variable that you just pushed into
the octave environment.  Then, pull the resulting variable out of the
symbol_table and pull the data from the contained octave_value().  Below
is a bit of pseudo-code that seems like it should work.  I hope this
helps.  If this does end up working, would you be willing to write and
email to the mailing list with how you did it.  I think this would be
useful for others to know about, since I haven't seen someone do this
before.  Also, not that the pseudocode I give below is based on the most
recent octave from the source code repository.  Between 3.0.x and what
is in the repository, I think there was a major change in the symbol
table.  Before, it was just a global variable.  Now it is a singleton
class.  The changes between 3.0.1 and the current repository tip are
significant, but the basic idea should be the same.

unsigned int imageArry[1024*768];
unsigned int resultImageArry[1024*768];

captureImage (imageArry);

Matrix octImageArry(768,1024); // Note, octave indexes as row x col
// COPY YOUR IMAGE DATA INTO THIS MATRIX
for (int row = 0; row < size; ++row)
{
 for (int column = 0; column < size; ++column)
 {
// I'm not sure how your image is indexed in memory, so you should populate the matrix properly
   octImageArry (row, column) = imageArry[col][row];
 }
}

// Wrap your octave_value Matrix up in a symbol_record object
symbol_table::symbol_record symrec_imageArry ("imageArry", octImageArry);

// Insert your symbol_record into the symbol_table
symbol_table::insert_symbol_record (symrec_imageArry);

// Call your routine to process the image
octave_call("resultImageArry=processImageData(imageArry);");

// Pull the newly created symbol record called "resultImageArry" out of the symbol_table symbol_record symrec_resultImageArry = symbol_table::find_symbol("resultImageArry");

// Pull the octave_value out of the result symbol_record
octave_value octResultImageArry = symrec_resultImageArry.varval();

// COPY THE DATA OUT OF THIS OCTAVE_VALUE AND BACK INTO YOUR C/C++ ARRAY
<end previous email>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]