How to create a VTK project in Windows Form Project (VisualC++ 2012 ) - c++

I'm trying to embed scene created with Visualization ToolKit (VTK)library into VisualC++ 2012 created windows form so I can design my Windows native GUI interface.
I'd like to underline that, all examples with console app are configured with (Cmake), compiled with VC++2012 and works flawlessly, as instructed by the official VTK wiki page.
The issue is, if I try to call those VTK functions and class initializations inside of Win Form application I get the Error LNK1107: invalid or corrupt file: cannot read at 0x2E0 D:\.....\VTK_61_BUILD_VS2012\bin\Debug\vtkViewsCore-6.1.dll even if I add everything normally as expected, include headers and external library dependencies.
This makes me think that I'm originating from wrong Visual C++ 2012 project template or something obvious that I'm completely missing, otherwise compiler would arise many not found files or syntax error.
This is the first lines where I'm trying to invoke the VTK library, even the intellisense suggest the vtk..... named proc,functions and structures, but application fails to compile.
#pragma once
#include <vtkSmartPointer.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkRenderWindow.h>
namespace CLR_Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
...
...
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
What is the problem here ?

The following link (in particular the answer of Mr. bnsteel) helped me very much, when I had the same problem ==>
http://vtk.1045678.n5.nabble.com/VTK-and-Winforms-integration-td5721086.html
You can use Swig as it is suggested under the link. But it is not necessary. Using the similar way (passing the windows form panel handle) you can create a C++/CLI wrapper as jalal sadeghi proposed. You will pass the panel handle through the wrapper to your C++ library that does all the "VTK work". This way you don't have to create individual wrappers for all VTK classes, all the "VTK work" and dependencies on VTK stay hidden in your C++ layer.
Something like this ==>
C++ side
setImageRenderWindowParentID(void *theID){
... (init your vtk render window)
renWin->SetParentId(theID);
}
C++/CLI side
void setRenderWindowParentID(IntPtr parentID, .. also pass the panel size .. ){
void* p = parentID.ToPointer();
myCPPVTK->setRenderWindowParentID(p, .. also pass the panel size ..);
}
C# side
VTKWrapper.setRenderWindowParentID(m_panel.Handle, .. also pass the panel size ..);

Related

how to include aspose.pdf.cpp in vs c++ project?

I am trying to implement this basic example https://docs.aspose.com/pdf/cpp/hello-world-example/ but I am getting errors that MakeObject, Document and TextFragment are not defined.
I was developing on vscode but then I realized that aspose can be downloaded through nuget in vs so I switched to vs thinking that adding aspose will be automatically integrated in my project dependencies or something, but no it didn't and I don't know currently how to include it (my use case is also very basic I tried hummuspdf/PDF-Writer but also I didn't knew how to implement it or include it, maybe it isn't as simple as #include "file.h" ? )
#include "Aspose.PDF.Cpp/Document.h"
#include "Aspose.PDF.Cpp/SaveFormat.h"
using namespace Aspose::Pdf;
using namespace System;
int main()
{
auto doc = MakeObject<Document>(u"Example.pdf");
doc->Save(u"Example.docx", SaveFormat::DocX);
}

CImg Compilation Error: t_normal not in global namespace

I'm currently working on a class assignment that requires the use of the CImg library. To be clear, the assignment is not linking the library into the program; The class is using it access the pixel data for later use in the heart of the assignment.
I'm working in Xcode (OS X 10.10). CImg (2.2.2) is installed from homebrew, and I've managed to navigate the weird way Xcode deals with search paths (added the header to the section), and have successfully-ish included CImg.
my full code is as below.
#include <iostream>
#define cimg_display 0 //I don't need X11 at all
#include "CImg.h"
using namespace cimg_library;
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
However, I get 17 Compile-time errors from CImg.h, which are very unusual, and all of the form:
"No member named 't_normal' in the global namespace; did you mean simply 't_normal'?"
Thinking I might have received a bad download, I have attempted to redownload CImg, with no luck. I have also gotten to this same point with non-homebrew versions of CImg.
To verify the download, I also compiled the examples from the command line and they ran perfectly.
Is there a problem with CImg that I'm not aware of, a problem with Xcode that I'm not aware of, or is there something fundamental that I'm missing (definitely an option, my C-style programming is a little rusty) ?
halp pls.
Your code runs fine if you do this:
Create a new Xcode project, with:
type = "Command Line Tool"
language = "C++"
Then go to "Build Settings" and add the path to the directory containing CImg.h to your "User Header Search Paths"

Use dlib in Visual studio 2012

I want to use optimization algorithm lbfgs in my project but I do not want to code it myself. So I found Dlib is a good option.
http://dlib.net/compile.html is a good library. I downloaded it. I use windows 7 and visual studio 2012. If I create a new win 32 console project and set property->configuration properties->VC++ Directories->Include Directories to the path of Dlib(dlib-18.10/).
And it works fine which mean I can run examples.
But when I add it to my project. I occur errors.(error : "vector" is ambiguous)
I guess it maybe because the way I include it.
On the document of Dlib, it says,
Again, note that you should not add the dlib folder itself to your compiler's include path. Doing so will cause the build to fail because of name collisions (such as dlib/string.h and string.h from the standard library). Instead you should add the folder that contains the dlib folder to your include search path and then use include statements of the form #include <dlib/queue.h>. This will ensure that everything builds correctly.
But I am not clear what it means above. I googled the Visual Studio search path (Tools / Options / Projects and Solutions / VC++ Directories).. But in my project this is non-editable.
I only use optimization.h in dlib. If I delete 'using namespace dlib;', then ' typedef matrix column_vector;'then the error is matrix is not a template. If I keep 'using namespace dlib;' I have error "vector" is ambiguous`.
#include <dlib/optimization.h>
#include <iostream>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------------------
// In dlib, the general purpose solvers optimize functions that take a column
// vector as input and return a double. So here we make a typedef for a
// variable length column vector of doubles. This is the type we will use to
// represent the input to our objective functions which we will be minimizing.
typedef matrix<double,0,1> column_vector;
As the documentation says, include directory should be root directory of the zip you downloaded. Then you include as #include <dlib/vector.h>. However, since vector is defined under namespace of std as well, you should be specifically denote which namespace's STL you will use.
If you want to use std::vector,
#include <vector.h>
then use it as
std::vector<int> stdVar;
Similarly, for dlib,
#include <dlib/geometry/vector
then use it as
dlib::vector<int> dLibVar;
You could also delete using namespace std if you don't use it as frequent as dlib. Then every STL you reference will be dlib. If you want std, just type std::vector.
using namespace std;
using namespace dlib;
#define vector std::vector
use w/ caution

Stroupstrup Graphics Library Errors

I'm new to C++ and in my Intro to programming design and concepts class we're now on graphics. I've been able to make programs with just FLTK's library but we have to use Bjarne's library such as GUI.h, Graph.h, Simple_window.h, Point.h. A simple program like a simple window program won't compile and gives a usual response of:
Simple_window.h:17: error: reference to ‘Window’ is ambiguous
I have also tried compiling using:
fltk-config --compile main.cpp
This yields the same results.
I have tried running the make file that Bjarne has provided with in the folder but that always comes up with errors and makes no .o files.
Note: I have also tried compiling on mac OSX and Ubuntu.
I never used either of those libraries, but I saw that tutorials for FLTK always begin with using namespace fltk; statement, which imports all FLTK classes, including fltk::Window to the root namespace.
The library by B. Stroustrup is contained in namespace called Graph_lib and it also has a class called Window.
Now, the file Simple_window.h has using namespace Graph_lib; statement at the beginning, which imports Graph_lib::Window to the root namespace. And this is where the ambiguity is coming from.
So I would suggest to omit the using statement (at least from using namespace fltk) and to use FLTK classes with full specification (e.g. fltk::Window instead of just Window). This should solve the ambiguity.
As a side note, this is nice example, why having using namespace at file level in a header file is a bad idea.
References:
http://www.fltk.org/doc-2.0/html/index.html
http://www.stroustrup.com/Programming/Graphics/Simple_window.h
EDIT: I tried to compile the library containing Simple_window myself and, at least under linux, it the ambiguity seems to be between class Graph_lib::Window from the library and typedef Window from xlib as well. xlib is C library and you can't really do anything about it, so you will have to get rid of using namespace Graph_lib in Stroustup's library.
In the file Simple_window.h:
delete using namespace Graph_lib;
change Window to Graph_lib::Window
Button to Graph_lib::Button
and Address to Graph_lib::Address
Then in the file Simple_window.cpp:
change Address to Graph_lib::Address again
and reference_to<Simple_window> to Graph_lib::reference_to<Simple_window>
Then it should compile. If you have different version than the one that's on stroustrup.com, you may need to fully qualify (add Graph_lib::) more classes.
I just had the same kind of problems (unresolved external symbols) using Simple_window.h and trying to compile a the following peace of code:
int main(){
// create a reference point for the window
Point topLeft(50,50);
// initialize a Simple_window object to size: 600x400 pixels, labeled: My window
Simple_window myWindow(topLeft, 600, 400, "My window");
// pass control to GUI
myWindow.wait_for_button();
return 0;
}
The solution was to add to the project (along with the main.cpp) all the respective .cpp files of the included .h files:("Graph.h", "Window.h", "Simple_window.h", "GUI.h")

Native dll built with /CLR flag containing a managed class

I have a old native MFC/c++ dll I have managed to get it compiled with /CLR flag.
Now I have added a managed C++/CLI class to the dll within a namaspace.
The header file is below, and the cpp file only has #include for the header file.
The native dll is a huge dll project with lot of un managed code, but it has only one managed c++ class like below.
When I add that dll as a reference to a .net winforms project I don't see that namespace / class, in the object browser,
and I get compile error for not finding the namespace "ShashiTest"
I am using Visual studio 2008.
Native dlls in mixed mode can not be added as reference to a managed project ?
Or am I missing something
Please help.
#pragma once
#using<mscorlib.dll>
#using<system.windows.forms.dll>
// Class derived from Forms
using namespace System::Windows::Forms;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;
namespace ShashiTest {
/// <summary>
/// Summary for test
/// </summary>
public ref class test
{
public:
test(void)
{
};
void ShowMessage()
{
MessageBox::Show("Hello World");
}
};
}
When I simplified my problem..I created a fresh MFC dll and added a manged C++ class to it (same class as above) . Compiled with /CLR flag.
When I add this dll to winforms project and run it i get
System.BadImageFormatException. Any clue ?
However I see the class and the name space and winform project compiles fine unlike the problem I have the above.
System.BadImageFormatException is usually caused by having an AnyCPU .NET application reference a DLL containing 32-bit native code. You get an error when running on a 64-bit platform, because the AnyCPU application runs as 64-bit, and can't load the DLL. The fix for this is (easy) to mark the application as x86-only or (hard) to provide both 32-bit and 64-bit versions of all DLLs containing native code.
Of course, you could have some other problem. Checking your DLL with Red Gate Reflector as suggested by #cdleonard in the comments is a great next step. Or ILSpy, which is free.