#include <Imagine/Graphics.h>
using namespace std;
const bool stepByStep=true;
void aWindow()
{
cout << "A window ... click when done" << endl;
}
void twoWindows()
{
cout << "Two windows ... click when done" << endl;
}
void twoDimBasics() {
cout << "Basic 2D drawings ... click when done" << endl;
for (int i=0;i<20;i+=2)
int px[]={201,200,260,240},py[]={301,350,330,280};
int t[]={300,300,300,400,400,350};
}
void bitmapBasics() {
cout << "Basic bitmap drawings... click when done" << endl;
octet r[256*256],g[256*256],b[256*256];
for (int j=0;j<256;j++) {
for (int i=0;i<256;i++) {
r[i+256*j]=i;
g[i+256*j]=255-i;
b[i+256*j]=(j<128)?255:0;
}
}
for (int j=0;j<256;j++)
for (int i=0;i<256;i++)
cols[i+256*j]=
Color(i,(2*j)%256,(i+j)%256);
for (int j=0;j<256;j++)
for (int i=0;i<256;i++)
}
void mouseBasics() {
cout << "Basic mouse functions" << endl;
int button;
}
void imageBasics() {
cout << "Basic image reading/writing. click when done" << endl;
int w,h;
delete[]r;delete[]g;delete[]b;
delete[]I;
}
void captureFunctions()
{
int w = 456;
int h = 444;
return;
do {
} while(p2.x()-p1.x()<=0 || p2.y()-p1.y()<=0);
octet *capt_r =
new octet[(p2.x()-p1.x())*(p2.y()-p1.y())];
octet *capt_g =
new octet[(p2.x()-p1.x())*(p2.y()-p1.y())];
octet *capt_b =
new octet[(p2.x()-p1.x())*(p2.y()-p1.y())];
captureRectangle(p1.x(), p1.y(), capt_r, capt_g, capt_b, p2.x()-p1.x(), p2.y()-p1.y());
putColorImage(p1.x(), p1.y(), capt_r, capt_g, capt_b, p2.x()-p1.x(), p2.y()-p1.y());
delete [] capt_r;
delete [] capt_g;
delete [] capt_b;
}
void naiveAnimation()
{
for(int i = 0; i < 10; i++)
{
fillRect(rand()%300, rand()%300, rand()%50, rand()%50,
Color(rand()%256, rand()%256, rand()%256));
}
cout << "Finished!" << endl;
}
void fastDrawings() {
cout << "Fast drawings ... click when done" << endl;
for (int i=0;i<1000;i++) {
double r=200*i/1000.;
}
for (int i=0;i<100;i+=5) {
}
const int sz=128;
octet r[sz*sz],g[sz*sz],b[sz*sz];
for (int y=0;y<sz;y++) {
for (int x=0;x<sz;x++) {
r[x+sz*y]=2*x;
g[x+sz*y]=2*y;
b[x+sz*y]=2*(x+y);
}
}
natBM.setColorImage(0,0,r,g,b,sz,sz);
for (int y=0;y<64;y++)
for (int x=0;x<64;x++)
cl[x+64*y]=
Color((x+y)%256,0,0);
natBM.setColorImage(
IntPoint2(32,32),cl,64,64);
for (int x=0;x<sz;x++)
for (int i=0;i<20;i++) {
double f=1+0.8*sin(i/2.);
putNativeBitmap(
int(256-sz/2+100*cos(i/4.0)),
int(256-sz/2+100*sin(i/4.0)),natBM,
true,f);
putNativeBitmap(
int(256-sz/2+100*cos(i/4.0)),
int(256-sz/2+100*sin(i/4.0)),natBM,
true,f);
}
}
void imageAnimation()
{
cout << "check 'ksmall.jpg' path" << endl;
cout <<
srcPath(
"ksmall.jpg") << endl;
int w, h;
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
std::cout << "wait for click!" << std::endl;
int x = 0, y = 0;
int step = 1;
for(int i = 0; i < 5*w/2; i++)
{
x += step;
if (x < 0 || x > w)
step *= -1;
}
cout << "Finished!" << endl;
}
void advancedEvents()
{
cout << "Advanced event handling" << endl;
drawString(10,30,
"Come back here again and press Up Arrow when done",
RED);
do {
case EVT_NONE:
cout << "No event for 500ms" << endl;
break;
case EVT_MOTION:
cout << "Motion" << endl;
break;
case EVT_KEY_ON:
cout <<
"Key " << ev.
key <<
" pressed"<< endl;
break;
case EVT_KEY_OFF:
cout <<
"Key " << ev.
key <<
" released"<< endl;
break;
case EVT_BUT_ON:
cout <<
"Button " << ev.
button <<
" pressed"<< endl;
break;
case EVT_BUT_OFF:
cout <<
"Button " << ev.
button <<
" released"<< endl;
break;
}
cout <<
" Mouse=[" << ev.
pix.
x() <<
','<<ev.
pix.
y() <<
"] "
<<
",Win=" << ev.
win <<
",SubWin=" << ev.
subWin << endl;
}
void subWindows()
{
cout << "A window with sub windows..." << endl;
string names[]={"sub window 0","sub window 1","sub window 2"};
}
#ifdef IMAGINE_OPENGL
void threeDim()
{
for(int i = 0; i < n; i++)
{
points_[i].
x() = Sphere.
vertices()[i].x() + 3;
points_[i].
y() = Sphere.
vertices()[i].y() + 3;
points_[i].
z() = Sphere.
vertices()[i].z() + 3;
}
string fileName =
srcPath(
"bunny.obj");
cout << "Reading filename: \"" << fileName << "\"" << endl;
readMeshFromObjFile(Bunny, fileName);
mesh=Bunny; cout << "Bunny" << endl;
mesh=Sphere; cout << "Sphere" << endl;
mesh=Cylinder; cout << "Cylinder" << endl;
mesh=Cone; cout << "Cone" << endl;
mesh=Plane; cout << "Plane" << endl;
mesh=Cube; cout << "Cube" << endl;
mesh=Arrow; cout << "Arrow" << endl;
mesh=PointCloud; cout << "Point cloud" << endl;
mesh=PolyLine; cout << "PolyLine" << endl;
}
#endif
int main(int argc, char** argv) {
typedef void (*FUNC)();
vector< pair<string,FUNC> > tests;
#define ADD_TEST(func) \
tests.push_back(make_pair(string(#func), func));
ADD_TEST(aWindow);
ADD_TEST(twoWindows);
ADD_TEST(twoDimBasics);
ADD_TEST(bitmapBasics);
ADD_TEST(mouseBasics);
ADD_TEST(imageBasics);
ADD_TEST(subWindows);
ADD_TEST(naiveAnimation);
ADD_TEST(imageAnimation);
ADD_TEST(fastDrawings);
ADD_TEST(advancedEvents);
ADD_TEST(captureFunctions);
#ifdef IMAGINE_OPENGL
ADD_TEST(threeDim);
#endif
std::vector< pair<string,FUNC> >::const_iterator it;
if(argc<=1)
for(it=tests.begin(); it!=tests.end(); ++it) {
cout << "---" << it->first << "---" << std::endl;
it->second();
}
else {
bool fail=false;
for(int i=1; i<argc; i++) {
for(it=tests.begin(); it!=tests.end(); ++it) {
if(it->first == argv[i]) {
cout << "---" << it->first << "---" << std::endl;
it->second();
break;
}
}
if(it==tests.end()) {
std::cout << "Unknown test " << argv[i] << endl;
fail = true;
}
}
if(fail) {
cout << "--- Available tests:" << endl;
for(it=tests.begin(); it!=tests.end(); ++it)
cout << it->first << ' ';
cout << endl;
}
}
return 0;
}
const T & z() const
Read alias.
Definition FVector.h:132
const T & y() const
Read alias.
Definition FVector.h:118
const T & x() const
Read alias.
Definition FVector.h:104
A mesh representing a 3D object.
Definition Mesh.h:151
static Mesh Sphere(const DoublePoint3 ¢er, double r, int res=24)
Sphere.
const std::vector< FloatPoint3 > & vertices() const
Get points.
void setColor(Color col)
Set color.
static Mesh Cone(const DoublePoint3 &base, const DoubleVector3 &axis, double r, int resolution=32)
Cone.
static Mesh PolyLine(DoublePoint3 *points, int n, bool closed)
PolyLine.
static Mesh PointCloud(const DoublePoint3 *points, int n)
PointCloud.
static Mesh Cylinder(const DoublePoint3 ¢er, const DoubleVector3 &axis, double r, int res=32)
Cylinder.
static Mesh Arrow(const DoublePoint3 &base, const DoubleVector3 &shaft, double r, int resolution=32)
Arrow.
void setOpacity(float opacity)
Set opacity.
static Mesh Cube(const DoublePoint3 ¢er, const DoubleVector3 &axis1, const DoubleVector3 &axis2, const DoubleVector3 &axis3)
Parallelepiped.
static Mesh Plane(const DoublePoint3 ¢er, const DoubleVector3 &axis1, const DoubleVector3 &axis2)
Parallelogram.
Native bitmap: image stored in graphics card format, fast to display.
Definition ImageIO.h:514
#define srcPath(s)
Transform relative file path to absolute path.
Definition Base.h:43
RGB< octet > Color
RGB<octet> alias.
Definition Color.h:283
const Color BLACK(0, 0, 0)
Predefined color.
const Color MAGENTA(255, 0, 255)
Predefined color.
const Color RED(255, 0, 0)
Predefined color.
unsigned char octet
0 to 255 integer type.
Definition Color.h:18
const Color CYAN(0, 255, 255)
Predefined color.
const Color GREEN(0, 255, 0)
Predefined color.
const Color BLUE(0, 0, 255)
Predefined color.
void closeWindow(Window w)
Close window.
void putGreyImage(int x, int y, const octet *g, int w, int h, bool xorMode=false, double fact=1.)
Display grey bitmap.
DoublePoint3 DoubleVector3
Double space vector.
Definition Types.h:21
void getEvent(int ms, Event &ev)
Get keyboard and mouse events.
bool saveColorImage(const std::string &name, const Color *cols, int w, int h, int quality=85)
Save color image.
void captureRectangle(int x, int y, Color *c, int w, int h)
Capture (sub window).
void hideMesh(const Mesh &M, bool reinitCam=true)
Hide mesh.
void putNativeBitmap(int x, int y, const NativeBitmap &bm, bool xorMode=false, double fact=1.)
Display native bitmap.
Window openWindow(int w, int h, const std::string &windowTitle="Imagine++", int x=-1, int y=-1)
New window for 2D graphics.
void drawPoint(int x, int y, const Color &col, bool xorMode=false)
Point.
void setActiveWindow(Window w, int subWin=0)
Set active window.
void drawPoly(const int x[], const int y[], int n, const Color &col, int width=1, bool xorMode=false)
Draw a polygon.
void drawEllipse(int x, int y, int w, int h, const Color &col, int penWidth=1, bool xorMode=false)
Ellipse.
int getMouse(int &x, int &y)
Wait for mouse click in active window (returning position).
int click()
Wait for mouse click in active window.
Window openWindow3D(int w, int h, const std::string &windowTitle="Imagine++", int x=-1, int y=-1)
New window for 3D graphics.
FVector< double, 3 > DoublePoint3
Double space point.
Definition Types.h:19
Window openComplexWindow(int w, int h, const std::string &name="Imagine++", int tabsNb=1, const std::string *tabsNames=0, const WindowType *tabsTypes=0, int x=-1, int y=-1)
New complex window.
bool loadGreyImage(const std::string &name, octet *&g, int &w, int &h)
Load grey image.
void fillCircle(int xc, int yc, int r, const AlphaColor &col, bool xorMode=false)
Filled Circle.
bool saveGreyImage(const std::string &name, const octet *g, int w, int h, int quality=85)
Save grey image.
void showMesh(const Mesh &M, bool reinitCam=true)
Show mesh.
void drawString(int x, int y, const std::string &s, const AlphaColor &col, int fontSize=12, double alpha=0, bool italic=false, bool bold=false, bool underlined=false, bool xorMode=false)
String.
void drawCircle(int xc, int yc, int r, const Color &col, int penWidth=1, bool xorMode=false)
Circle.
WindowInternal * Window
Handle to a display window.
Definition Types.h:48
void noRefreshEnd()
Norefresh end.
void endGraphics()
Terminate graphics application.
void drawRect(int x, int y, int w, int h, const Color &col, int penWidth=1, bool xorMode=false)
Rectangle.
int anyClick()
Wait for mouse click in any window.
void fillRect(int x, int y, int w, int h, const AlphaColor &col, bool xorMode=false)
Filled rectangle.
bool loadColorImage(const std::string &name, Color *&cols, int &w, int &h)
Load color image.
void fillEllipse(int x, int y, int w, int h, const AlphaColor &col, bool xorMode=false)
Filled Ellipse.
void milliSleep(int msec)
... Pause program for a certain period.
void fillPoly(const int x[], const int y[], int n, const AlphaColor &col, bool xorMode=false)
Filled Polygon.
void clearWindow()
clearWindow.
void noRefreshBegin()
Norefresh begin.
Coords< 2 > IntPoint2
Plane point with integral coordinates.
Definition Types.h:17
void putColorImage(int x, int y, const Color *cols, int w, int h, bool xorMode=false, double fact=1.)
Display color bitmap.
void drawArrow(int x1, int y1, int x2, int y2, const AlphaColor &col, int arrowWidth=8, int arrowHeight=5, int style=0, int width=1, bool xorMode=false)
Arrow.
void drawLine(int x1, int y1, int x2, int y2, const Color &col, int penWidth=1, bool xorMode=false)
Line.
Image< octet, dim > grey(const Image< T, dim > &I, T m, T M)
Grey level representation.
Definition IO.h:289
Event info.
Definition Events.h:69
int key
Key code that triggered the event, if any (only for types EVT_KEY_ON and EVT_KEY_OFF).
Definition Events.h:78
int subWin
SubWindow where the event occurred.
Definition Events.h:84
EventType type
Event type.
Definition Events.h:71
int button
Mouse button (1,2,3) that triggered the event, if any.
Definition Events.h:75
Window win
Window where the event occurred.
Definition Events.h:82
EventState state
Keyboard state (bitfield) just before the event occured.
Definition Events.h:80
IntPoint2 pix
Mouse position (only for EVT_BUT_ON, EVT_BUT_OFF and EVT_MOTION).
Definition Events.h:73