/*================================================================= * * TRIANGLE.CPP * * Calculate the points in Sierpinski's triangle, an elementary * fractal. * * This function calls a * using MATLAB Compiler. * * Copyright 1984-2007 The MathWorks, Inc. * *=================================================================*/ // Include the MCR header file and the library specific header file // as generated by MATLAB Compiler #include "libtrianglep.h" #include "main_for_lib.h" void usage(const char *name) { std::cout << "Usage: " << name << " [number of points]" << std::endl; exit (-1); } // This file is compiled with a C file (main_for_lib.c). MSVC compiles // the C file without name mangling and the CPP file with name mangling. // Due to this, the run_main symbol as generated in each of these files // is different and thus gives a link time error. To resolve this issue, // we force the Microsoft compiler to use C style linking for run_main. // This is not needed on UNIX because MBUILD uses C++ compilers for both // C and CPP files. So the symbol names are consistent. #ifdef _MSC_VER extern "C" #endif int run_main(int ac, const char *av[]) { // Default number of iterations int num_points = 7500; // Validate the number of inputs if (ac < 1 || ac > 2) { fprintf(stderr, "Expecting 0 or 1 input(s). Found %d\n", ac); usage(av[0]); } // If we have the right number of inputs (1), try to convert the input // string to an integer. // if (ac == 2) num_points = atoi(av[1]); // Type check on input argument -- atoi() will fail if the input is // not an integer. // if (num_points == 0) { std::cerr << "First argument must be an integer." << std::endl; usage(av[0]); } // Call the library intialization routine and make sure that the // library was initialized properly // if (!mclInitializeApplication(NULL,0)) { std::cerr << "could not initialize the triangle library properly" << std::endl; return -1; } if (!libtrianglepInitialize()) { std::cerr << "could not initialize the trianglep library properly" << std::endl; return -2; } else { // Create the input data // Input parameters: // // iterations: Number of points to draw in the triangle // draw: If true, draw the triangle in a figure window before returning. // mwArray iterations((mxDouble)num_points); mwArray draw(1.0); // Create the output variables // The Sierpinski function returns the X and Y coordinates of the points // forming the pattern in the triangle. mwArray x; mwArray y; // Call the library function sierpinski(2, x, y, iterations, draw); // Display the return value of the library function //std::cout << "Calculated " << x.NumberOfElements() << " points" << std::endl; #ifdef _WIN32 printf("\nCalculated %Iu points\n",x.NumberOfElements()); #else printf("\nCalculated %zu points\n",x.NumberOfElements()); #endif // Block until user dismisses the figure mclWaitForFiguresToDie(NULL); // Call the library termination routine libtrianglepTerminate(); } /* Note that you should call mclTerminate application in the end of * your application. mclTerminateApplication terminates the entire * application. */ // Shut down all MCR instances mclTerminateApplication(); /* Success */ return 0; }