IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Problème avec rotation 3D, "effets secondaires"


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 3
    Par défaut Problème avec rotation 3D, "effets secondaires"
    Bonjour à tous,

    je post pour la première fois sur ce forum, ce pour un problème sur lequel je sèche un peu...
    le programme est sous Win32, réalisé avec C::B, et permet à partir des clics de souris de créer un alcane (rajout d'un -CH3 en cliquant sur un hydrogène) et rotation 3D.
    Seulement, pb, lorsqu'on fait tourner la molécule, elle rétrécie (comme au lavage), ce que je ne comprend pas...

    J'ai pourtant essayé deux algorithmes différents pour la 3D, tout déclaré (les variables) en double, vérifié que chaque vecteur de la matrice de rotation était normé et tout, rien n'y fait...

    A terme, le programme doit gérer une "TrackBall" pour faire tourner la molécule, elle est a peu près faite avec encore quelques petits soucis mineurs.

    Et tant qu'à faire, je ne vois pas trop comment afficher les liaisons dans l'ordre des z croissant (du plus loin au plus près).

    Voilà, je me doute que ça doit prendre un peu de temps pour se plonger dans le code, d'autant que je suis un apprenti, mais vous m'apporteriez une grande aide!
    Merci d'avance!

    PS:publication utilisée pour la TrackBall:"A study in interactive 3-D rotation using 2-D control devices", Computer Graphics, Volume 22, number 4, August 1988

    Surtout n'hésitez pas à me poser des questions pour améliorer la compréhension du programme!

    Fonction principale (ProjAlcane.c):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    #include <windows.h>
     
    LRESULT CALLBACK WndProc( HWND hWnd, UINT messg,
    								WPARAM wParam, LPARAM lParam );
     
     
    char szProgName[] = "ConsAl"; /* name of application */
    extern int i;
     
    void init(), InitApp();
    void construction(HWND hWnd, LPARAM lParam), affiche(HWND hWnd), test_position(LPARAM lParam);
    void methane(), ajouter_CH3(int i), coordonnees_fenetre();
    int save();
    void matrice(WPARAM wParam, LPARAM lParam, UINT messg, HWND hWnd), TrackBall(WPARAM wParam, LPARAM lParam, UINT messg, HWND hWnd);
     
     
     
     
     
    void affiche(HWND hWnd);
    int WINAPI WinMain( HINSTANCE hInst, 	/*Win32 entry-point routine */
    					HINSTANCE hPreInst,
    					LPSTR lpszCmdLine,
    					int nCmdShow )
    {
    	HWND hWnd;
    	MSG lpMsg;
    	WNDCLASS wc;
     
    	if( !hPreInst )			/*set up window class and register it */
    	{
    		wc.lpszClassName 	= szProgName;
    		wc.hInstance 		= hInst;
    		wc.lpfnWndProc		= WndProc;
    		wc.hCursor			= LoadCursor( NULL, IDC_ARROW );
    		wc.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
    		wc.lpszMenuName		= NULL;
    		wc.hbrBackground	= GetStockObject( WHITE_BRUSH );
    		wc.style			= 0;
    		wc.cbClsExtra		= 0;
    		wc.cbWndExtra		= 0;
     
    		if( !RegisterClass( &wc ) )
    			return FALSE;
    	}
     
     
    	hWnd = CreateWindow( 	szProgName,							/* now create the window */
    							"Zag&Jac all right reserved",
    							WS_OVERLAPPEDWINDOW,
    							CW_USEDEFAULT,
    							CW_USEDEFAULT,
    							CW_USEDEFAULT,
    							CW_USEDEFAULT,
    							(HWND)NULL,
    							(HMENU)NULL,
    							(HANDLE)hInst,
    							(LPSTR)NULL		);
     
    	ShowWindow(hWnd, nCmdShow );
    	UpdateWindow( hWnd );
     
        InitApp();
     
    	while( GetMessage( &lpMsg, NULL, 0, 0 ) )					/* begin the message loop */
    	{
    		TranslateMessage( &lpMsg );
    		DispatchMessage( &lpMsg );
    	}
    	return( lpMsg.wParam);
     
    }
     
     
     
    LRESULT CALLBACK WndProc( HWND hWnd, UINT messg,				/*callback procedure */
    								WPARAM wParam, LPARAM lParam )
    {
    	HDC hdc; 				/* handle to the device context */
    	PAINTSTRUCT pstruct; 	/*struct for the call to BeginPaint */
     
    	switch(messg)
    	{
     
    		case WM_LBUTTONDOWN:
    		test_position(lParam);
    		InvalidateRect(hWnd,NULL,TRUE);
    		break;
     
    		case WM_MOUSEMOVE:
    		sphere3D(wParam, lParam, messg, hWnd);
    		break;
     
    		case WM_PAINT:
    		affiche(hWnd);
    		break;
     
    		case WM_DESTROY:
    		save();
    		PostQuitMessage( 0 );
    		break;
     
    		default:
    		return( DefWindowProc( hWnd, messg, wParam, lParam ) );
    	}
     
    	return( 0L );
    }
    struc.c (gère l'affichage ainsi que le test de position de la souris et le calcul des coordonnées dans la fenêtre):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    //Constantes
     
    #include <stdio.h>
    #include <string.h> //cas de manipulation de chaine de caractères
    #include <math.h>
    #include <windows.h>
     
    #define MAXATOM 100
    #define CC 1.54
    #define CH 1.05
     
    //Variables
     
    int nbatom;
    int i,j;
    double coef;
    double x[MAXATOM],y[MAXATOM],z[MAXATOM];
    double print[3][MAXATOM];
    int t[MAXATOM], cnh[MAXATOM], cnc[MAXATOM][5], test[MAXATOM], z_croiss[MAXATOM];
    double g, alpha, N, a[3], liaison, dd=0, vecteur[3], col[3], R[3][3], transfert;
    char symb[MAXATOM][3], Val[MAXATOM], coucou[10], trans[4];
    int x0, yzero, x00, y00, xP, yP, z1, x2, y2, ox, oy;
     
    double xC, yC, zC, xGa=0, yGa=0, zGa=0, xGp, yGp, zGp, omega, tau, theta, phi;
     
    COLORREF blanc,gris,noir,rouge,vert,bleue;
    HBRUSH fond_noir, fond_blanc;
    HWND       hWnd ;
    HDC      hdc;
    PAINTSTRUCT    pstruct ;
     
    //Fonctions
     
    void init(), InitApp();
    void construction(HWND hWnd, LPARAM lParam), affiche(HWND hWnd), test_position(LPARAM lParam);
    void methane(), ajouter_CH3(int i), coordonnees_fenetre();
    void save(), z_croissant();
     
     
     
     
    void InitApp(void)
    {
     
       blanc=RGB(255,255,255);
       noir=RGB(0,0,0);
       gris=RGB(125,125,125);
       rouge=RGB(255,0,0);
       vert=RGB(0,255,0);
       bleue=RGB(0,0,255);
     
     
     
       hWnd=GetForegroundWindow();
       hdc = GetDC (hWnd) ;
     
     
     
    	for(i=0;i<=MAXATOM;i++)
    	{
    	    z_croiss[i]=i;
    		for(j=0;j<=4;j++)
    			cnc[i][j]=0;
    	}
     
     
    	srand(time(NULL));
     
    }
     
    void affiche(HWND hWnd)
    {
    	int k, rH, rC;
     
    		hdc = BeginPaint(hWnd, &pstruct ); /* prepare window for painting*/
     
    	rH=20;
    	rC=35;
     
        z_croissant();
     
    for(i=1;i<=nbatom;i++)
    	{
    		if(strcmp(symb[i],"C")==0)
    		{
    		    for(k=1;k<=4;k++)
    		    {
                    MoveToEx(hdc,print[0][cnc[i][k]],print[1][cnc[i][k]],NULL);
                    LineTo(hdc,print[0][i],print[1][i]);
    		    }
    		}
    	}
     
    	for(i=1;i<=nbatom;i++)
    	{
    		if(strcmp(symb[z_croiss[i]],"H")==0) //trace les liaisons CH
    		{
     
    			/*MoveToEx(hdc,print[0][cnc[i][1]],print[1][cnc[i][1]],NULL);
    			LineTo(hdc,print[0][i],print[1][i]);*/
     
    			SelectObject(hdc,GetStockObject(WHITE_BRUSH));
     
    			Ellipse(hdc, print[0][z_croiss[i]]-rH/2, print[1][z_croiss[i]]-rH/2, print[0][z_croiss[i]]+rH/2 ,print[1][z_croiss[i]]+rH/2);
    		}
    		if(strcmp(symb[z_croiss[i]],"C")==0)
    		{
    			for(k=1;k<=4;k++)
    			{
    				/*if(cnc[i][k]<i)
    					k++;
    				if(cnc[i][k]==0)
    					break;*/
     
    				/*MoveToEx(hdc,print[0][cnc[z_croiss[i]][k]],print[1][cnc[z_croiss[i]][k]],NULL);
    				LineTo(hdc,print[0][z_croiss[i]],print[1][z_croiss[i]]);*/
    			}
    			SelectObject(hdc,GetStockObject(BLACK_BRUSH));
     
    			Ellipse(hdc, print[0][z_croiss[i]]-rC/2, print[1][z_croiss[i]]+rC/2 ,print[0][z_croiss[i]]+rC/2, print[1][z_croiss[i]]-rC/2);
    		}
    	}
     
     
    	MoveToEx(hdc,xGp-5,yGp,NULL);
    	LineTo(hdc,xGp+5,yGp);
    	MoveToEx(hdc,xGp,yGp-5,NULL);
    	LineTo(hdc,xGp,yGp+5);
    	/*
    	TextOut(hdc, 50, 50, "print[0][]  print[1][]  print[2][]", strlen("print[0][]  print[1][]  print[2][]"));
    	for(i=1;i<=nbatom;i++)
    	{
    	sprintf(Val, "%f  %f  %f", print[0][i], print[1][i], print[2][i]);
    		TextOut(hdc, 50, 250+20*i, Val, strlen(Val) );
    	}
    	*/
    	sprintf(Val, "nbatom:  %d", nbatom);
    	TextOut(hdc, 50, 20, Val, strlen(Val) );
     
    	TextOut(hdc, 300, 50, "xGp   yGp", strlen("xGp   yGp"));
    	sprintf(Val, "%f  %f %f  %f %f", xGp, yGp, print[0][2], print[1][2], print[2][2]);
    	TextOut(hdc, 300, 70, Val, strlen(Val) );
     
    	//sprintf(Val, "Norme= %f", N);
    	//TextOut(hdc, 550, 70, Val, strlen(Val) );
     
    	TextOut(hdc, 500, 100, "v[0]     v[1]     v[2]", strlen("v[0]     v[1]     v[2]"));
    		sprintf(Val, "%f  %f  %f %f", vecteur[0], vecteur[1], vecteur[2], sqrt(vecteur[0]*vecteur[0]+vecteur[1]*vecteur[1]+vecteur[2]*vecteur[2]));
    		TextOut(hdc, 500, 120, Val, strlen(Val) );
     
     
        if ((dd<=sqrt((print[0][1]-print[0][3])*(print[0][1]-print[0][3])+(print[1][1]-print[1][3])*(print[1][1]-print[1][3])+(print[2][1]-print[2][3])*(print[2][1]-print[2][3]))/liaison))
            dd=sqrt((print[0][1]-print[0][3])*(print[0][1]-print[0][3])+(print[1][1]-print[1][3])*(print[1][1]-print[1][3])+(print[2][1]-print[2][3])*(print[2][1]-print[2][3]))/liaison;
     
     
    	sprintf(Val, "d1/d1'= %f", liaison);
    	TextOut(hdc, 300, 100, Val, strlen(Val) );
     
        for(i=0;i<=2;i++)
    	{
    		col[i]=R[i][0]*R[i][0] + R[i][1]*R[i][1] + R[i][2]*R[i][2];
    	}
     
    		sprintf(Val, " %f   %f   %f  %f", tau, theta, omega, phi);
    		TextOut(hdc, 50, 190, Val, strlen(Val) );
     
            /*sprintf(Val, "%f   %f  %f", col[0], col[1], col[2]);
    		TextOut(hdc, 50, 190, Val, strlen(Val) );*/
     
    	//TextOut(hdc, 70, 80, coucou, strlen(coucou) );
     
    		EndPaint(hWnd, &pstruct );
    }
     
    void save()
    {
    	FILE *fichier;
     
    	fichier=fopen("alcane.txt","w");
    	if (fichier==0)
    	{
    	 	printf("probleme de fichier");
           // return 0;
        }
    	fprintf(fichier,"%d\n\n", nbatom);
    	for(i=1;i<=nbatom;i++)
    		fprintf(fichier,"%s %f %f %f  %d %d %d %d   %d\n",symb[i], x[i], y[i], z[i], cnc[i][1], cnc[i][2], cnc[i][3], cnc[i][4], z_croiss[i]);
    	fclose(fichier);
     
    	//printf("coucou");
    	//digital_output(0);
    }
     
    void test_position(LPARAM lParam)
    {
    	int r, r2=150;
    	r=10;
    	x00 = LOWORD (lParam);		//récupération des coordonnées du pointeur souris
    	y00 = HIWORD (lParam);
     
    	if(nbatom==0)
    	{
    		methane();
    	}
    	for(i=1;i<=nbatom;i++)  // appelle la fonction CH3 qui remplace un hydrogène par un methyl
    	{
    		if(strcmp(symb[i],"H")==0)
    		{
    			if((x00-print[0][i])*(x00-print[0][i])+(y00-print[1][i])*(y00-print[1][i])<=r*r)
    			{
    				ajouter_CH3(i);
     
    			}
    		}
    	}
    	if(nbatom<8) // arrête de déplacer la molécule au dela de 8 atomes
    	{
    		for(i=1;i<=nbatom;i++)
    		{
    			if(strcmp(symb[i],"H")==0)
    			{
    				if((x00-print[0][i])*(x00-print[0][i])+(y00-print[1][i])*(y00-print[1][i])>r*r)
    				{
    					x0=x00;
    					yzero=y00;
    				}
    			}
    		}
    	}
     
        x00 = (x00-xGp)/r2;
        y00 = (y00-yGp)/r2;
    }
     
    void coordonnees_fenetre()
    {
    	int k;
     
    	coef=80;
     
    	for(i=1;i<=nbatom;i++)
    	{
    		if(strcmp(symb[i],"H")==0) //trace les liaisons CH
    		{
    			print[0][cnc[i][1
    			]]=(x[cnc[i][1]]-xGa)*coef+xGp;
    			print[1][cnc[i][1]]=(y[cnc[i][1]]-yGa)*coef+yGp;
    			print[2][cnc[i][1]]=(z[cnc[i][1]]-zGa)*coef+zGp;
    			print[0][i]=(x[i]-xGa)*coef+xGp;
    			print[1][i]=(y[i]-yGa)*coef+yGp;
    			print[2][i]=(z[i]-zGa)*coef+zGp;
     
    		}
    		if(strcmp(symb[i],"C")==0)
    		{
    			for(k=1;k<=4;k++)
    			{
    				if(cnc[i][k]<i)
    					k++;
    				if(cnc[i][k]==0)
    					break;
     
    				print[0][cnc[i][k]]=(x[cnc[i][k]]-xGa)*coef+xGp; //trace les liaison CC
    				print[1][cnc[i][k]]=(y[cnc[i][k]]-yGa)*coef+yGp;
    				print[2][cnc[i][k]]=(z[cnc[i][k]]-zGa)*coef+zGp;
    				print[0][i]=(x[i]-xGa)*coef+xGp;
    				print[1][i]=(y[i]-yGa)*coef+yGp;
    				print[2][i]=(z[i]-zGa)*coef+zGp;
    			}
    		}
    	}
     
    	xGp=0, yGp=0, zGp=0;
    	xGa=0, yGa=0, zGa=0;
     
    	for(j=1;j<=nbatom;j++)  //calcul du barycentre de la molécule
    	{
    		xGa=xGa+x[j];
    		yGa=yGa+y[j];
    		zGa=zGa+z[j];
     
    		xGp=xGp+print[0][j];
    		yGp=yGp+print[1][j];
    		zGp=zGp+print[2][j];
    	}
    	xGa=xGa/nbatom;
    	yGa=yGa/nbatom;
    	zGa=zGa/nbatom;
     
    	xGp=xGp/nbatom;
    	yGp=yGp/nbatom;
    	zGp=zGp/nbatom;
    }
     
    void z_croissant()
    {
     
    for(i=1;i<=nbatom;i++)
    {
        int k;
        for(j=1;j<=nbatom-i;j++)
        if(z[z_croiss[j+1]]<z[z_croiss[j]])
        {
            transfert=z_croiss[j];
            z_croiss[j]=z_croiss[j+1];
            z_croiss[j+1]=transfert;
     
            /*strcpy(trans,symb[j]);
            strcpy(symb[j],symb[j+1]);
            strcpy(symb[j+1],trans);
     
            transfert=x[j];
            x[j]=x[j+1];
            x[j+1]=transfert;
            transfert=y[j];
            y[j]=y[j+1];
            y[j+1]=transfert;
            transfert=z[j];
            z[j]=z[j+1];
            z[j+1]=transfert;
     
            transfert=print[0][j];
            print[0][j]=print[0][j+1];
            print[0][j+1]=transfert;
            transfert=print[1][j];
            print[1][j]=print[1][j+1];
            print[1][j+1]=transfert;
            transfert=print[2][j];
            print[2][j]=print[2][j+1];
            print[2][j+1]=transfert;
     
            for(k=1;k<=4;k++)
            {
                transfert=cnc[j][k];
                cnc[j][k]=cnc[j+1][k];
                cnc[j+1][k]=transfert;
            }*/
     
     
     
        }
    }
    }
    sphère.c (gère la rotation 3D):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    //Constantes
     
    #include <stdio.h>
    #include <string.h> //cas de manipulation de chaine de caractères
    #include <math.h>
    #include <windows.h>
     
    #define MAXATOM 100
    #define CC 1.54
    #define CH 1.05
    #define Pi 3.141592654
     
    //Variables
     
     
    double y00 ,x00, z00, xP, yP, zP, z1, r2=150;
    int i,j;
    int nbatom;
    double R[3][3], Romega[3][3], Rtheta[3][3];
    double x[MAXATOM],y[MAXATOM],z[MAXATOM];
    double print[3][MAXATOM];
    double a[3], a1[3], a2[3], alpha, N, s, c, t, u, d, col[3], omega, tau, theta, phi, k;
    double xGp=0, yGp=0, zGp=0, xGa, yGa, zGa, liaison;
    char Val[50];
    int m=0, x2=0, y2=0;
    double tetraedre[5][3], vecteur[3]={1,1,1};
     
    void sphere3D(WPARAM wParam, LPARAM lParam, UINT messg, HWND hWnd), rotation(), TrackBall(WPARAM wParam, LPARAM lParam, UINT messg, HWND hWnd);
    void coordonnees_fenetre();
     
    void TrackBall(WPARAM wParam, LPARAM lParam, UINT messg, HWND hWnd)
    {
        xP = LOWORD (lParam);		//récupération des coordonnées du pointeur souris
        yP = HIWORD (lParam);
     
        xP = (xP-xGp)/r2;
        yP = (yP-yGp)/r2;
     
        zP=sqrt(r2*r2-yP*yP-xP*xP);
     
     
    if(wParam & MK_LBUTTON) // permet de savoir si le bouton gauche de la souris est maintenu enfoncée
    	{
            d=sqrt((xP-x00)*(xP-x00)+(yP-y00)*(yP-y00)+(zP-z00)*(zP-z00));
    		theta = acos(x00);                                     //calcul des angles nécessaires à la rotation à partir du mouvement du pointeur.
    		tau = acos((xP-x00)/d)-theta;
     
    		if((x00*x00+y00*y00)>=1)
                omega=Pi/2;
            else
                omega=asin(sqrt(x00*x00+y00*y00));
    		//phi = (Pi/2)*d*(1-(1-0,2/Pi)*omega*(2/Pi)*(1-sqrt(cos(tau)*cos(tau)))); //angle de rotation
            phi=(Pi*d);
     
     
            a[0]=-sin(tau);
            a[1]=cos(tau);
            a[2]=0;
     
            a1[0]=a[0]*cos(omega)+a[2]*sin(omega);   //calcul du vecteur orthogonal à la rotation
            a1[1]=a[1];
            a1[2]=-a[0]*sin(omega)+a[2]*cos(omega);
     
            a2[0]=a1[0]*cos(theta)-a1[1]*sin(theta);
            a2[1]=a1[0]*sin(theta)+a1[1]*cos(theta);
            a2[2]=a1[2];
     
    		s=sin(phi);			//variables utilisées pour le calcul de la matrice de rotation
    		c=cos(phi);
    		t=1-cos(phi);
     
    		for(i=0;i<=2;i++)
    		{
    			R[i][i]=(t*a2[i]*a2[i]+c);
    		}
     
    		R[0][1]=t*a2[0]*a2[1]+s*a2[2];		//calcul de la matrice de rotation
    		R[0][2]=t*a2[0]*a2[2]-s*a2[1];
    		R[1][0]=t*a2[0]*a2[1]-s*a2[2];
    		R[1][2]=t*a2[1]*a2[2]+s*a2[0];
            R[2][0]=t*a2[0]*a2[2]+s*a2[1];
    		R[2][1]=t*a2[1]*a2[2]-s*a2[0];
     
            /*for(i=0;i<=2;i++)
            {
                col[i]=sqrt(R[i][0]*R[i][0]+R[i][1]*R[i][1]+R[i][2]+R[i][2]);
                R[i][0]=R[i][0]/col[i];
                R[i][1]=R[i][1]/col[i];
                R[i][2]=R[i][2]/col[i];
            }*/
     
        //    rotation();
     
     
    		InvalidateRect(hWnd,NULL,TRUE);
    	}
     
            x00=xP;
            y00=yP;
            z00=zP;
     
     
    }
     
     
     
    void sphere3D(WPARAM wParam, LPARAM lParam, UINT messg, HWND hWnd)
    {
    	HDC hdc; 				/* handle to the device context */
     
    		xP = LOWORD (lParam);		//récupération des coordonnées du pointeur souris
    		yP = HIWORD (lParam);
     
            if(((yP-y00)*(yP-y00)+(xP-x00)*(xP-x00))>=r2*r2)
            {
                xP=x00+(xP-x00)/sqrt((yP-y00)*(yP-y00)+(xP-x00)*(xP-x00));
                yP=y00+(yP-y00)/sqrt((yP-y00)*(yP-y00)+(xP-x00)*(xP-x00));
            }
     
            /*if (w<=sqrt((yP-y00)*(yP-y00)+(xP-x00)*(xP-x00)))
                w=sqrt((yP-y00)*(yP-y00)+(xP-x00)*(xP-x00));
            sprintf(Val, "(yP-y00)*(yP-y00)+(xP-x00)*(xP-x00):  %f", w);
            TextOut(hdc, 50, 120, Val, strlen(Val) );*/
     
     
     
    	if(wParam & MK_LBUTTON) // pemrnet de savoir si le bouton gauche de la souris est maintenu enfoncée
    	{
     
            //liaison=sqrt((print[0][1]-print[0][3])*(print[0][1]-print[0][3])+(print[1][1]-print[1][3])*(print[1][1]-print[1][3])+(print[2][1]-print[2][3])*(print[2][1]-print[2][3]));
     
     
    		a[0]=-r2*(yP-y00);
    		a[1]=r2*(xP-x00);
    		a[2]=0;
     
     
    		N=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);		//calcul de la norme de a
    		alpha=asin(N/(r2*r2));
     
    		a[0]=a[0]/N;
    		a[1]=a[1]/N;
    		a[2]=a[2]/N;
     
     
    		s=N/(r2*r2);			//variables utilisées pour le calcul de la matrice de rotation
    		c=cos(alpha);
    		t=1-cos(alpha);
     
            k=1.000 ;
    		for(i=0;i<=2;i++)
    		{
    			R[i][i]=k*(t*a[i]*a[i]+c);
    		}
     
    		R[1][0]=k*(t*a[0]*a[1]+s*a[2]);		//calcul de la matrice de rotation
    		R[2][0]=k*(t*a[0]*a[2]-s*a[1]);
    		R[0][1]=k*(t*a[0]*a[1]-s*a[2]);
    		R[2][1]=k*(t*a[1]*a[2]+s*a[0]);
    		R[0][2]=k*(t*a[0]*a[2]+s*a[1]);
    		R[1][2]=k*(t*a[1]*a[2]-s*a[0]);
     
            rotation();
     
     
    		InvalidateRect(hWnd,NULL,TRUE);
    	}
     
     
    	sprintf(Val, "calcul norme matrice rotation");
    	TextOut(hdc, 50, 150, Val, strlen(Val) );
     
    	sprintf(Val, "R[0]       R[1]       R[2]");
    	TextOut(hdc, 90, 170, Val, strlen(Val) );
     
    	for(i=0;i<=2;i++)
    	{
    		col[i]=R[i][0]*R[i][0] + R[i][1]*R[i][1] + R[i][2]*R[i][2];
    	}
     
    		sprintf(Val, "  %f  %f  %f", col[0], col[1], col[2]);
    		TextOut(hdc, 50, 190, Val, strlen(Val) );
     
    	/*
    	sprintf(Val, "tetraedre");
    	TextOut(hdc, 50, 150, Val, strlen(Val) );
     
    	sprintf(Val, "tet[][0]     tet[][1]     tet[][2]", strlen(Val) );
    	TextOut(hdc, 90, 170, Val, strlen(Val) );
     
    	for(i=0;i<=4;i++)
    	{
    		sprintf(Val, "tet[%d][]  %f  %f  %f", i, tetraedre[i][0], tetraedre[i][1], tetraedre[i][2]);
    		TextOut(hdc, 50, 190+20*i, Val, strlen(Val) );
    	}
    	*/
     
            x00=xP;
            y00=yP;
     
     
    	ReleaseDC(hWnd, hdc);
     
    }
     
    void rotation()
    {
            vecteur[0]=(vecteur[0])*R[0][0]+(vecteur[1])*R[0][1]+(vecteur[2])*R[0][2];
            vecteur[1]=(vecteur[0])*R[1][0]+(vecteur[1])*R[1][1]+(vecteur[2])*R[1][2];
            vecteur[2]=(vecteur[0])*R[2][0]+(vecteur[1])*R[2][1]+(vecteur[2])*R[2][2];
     
        for(i=1;i<=4;i++)
    		{
    			tetraedre[i][0]=(tetraedre[i][0])*R[0][0]+(tetraedre[i][1])*R[0][1]+(tetraedre[i][2])*R[0][2];
    			tetraedre[i][1]=(tetraedre[i][0])*R[1][0]+(tetraedre[i][1])*R[1][1]+(tetraedre[i][2])*R[1][2];
    			tetraedre[i][2]=(tetraedre[i][0])*R[2][0]+(tetraedre[i][1])*R[2][1]+(tetraedre[i][2])*R[2][2];
    		}
     
     
    		for(i=1;i<=nbatom;i++)
    		{
     
    			x[i]=(x[i]-xGa)*R[0][0]+(y[i]-yGa)*R[0][1]+(z[i]-zGa)*R[0][2]+xGa;
    			y[i]=(x[i]-xGa)*R[1][0]+(y[i]-yGa)*R[1][1]+(z[i]-zGa)*R[1][2]+yGa;
    			z[i]=(x[i]-xGa)*R[2][0]+(y[i]-yGa)*R[2][1]+(z[i]-zGa)*R[2][2]+zGa;
     
    			/*print[0][i]=(print[0][i]-xGp)*R[0][0]+(print[1][i]-yGp)*R[0][1]+(print[2][i]-zGp)*R[0][2]+xGp;
    			print[1][i]=(print[0][i]-xGp)*R[1][0]+(print[1][i]-yGp)*R[1][1]+(print[2][i]-zGp)*R[1][2]+yGp;
    			print[2][i]=(print[0][i]-xGp)*R[2][0]+(print[1][i]-yGp)*R[2][1]+(print[2][i]-zGp)*R[2][2]+zGp;*/
    		}
    		coordonnees_fenetre();
    }
    molecule.c (gère l'ajout des atomes lors du clic sur un hydrogène):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
     
    #include <stdio.h>
    #include <string.h> //cas de manipulation de chaine de caractères
    #include <math.h>
    #include <windows.h>
     
    #define MAXATOM 100
    #define CC 1.54
    #define CH 1.05
     
    //Variables
     
    int nat=0, nbatom=0;
    int i,j;
    double coef;
    double x[MAXATOM],y[MAXATOM],z[MAXATOM];
    double printx[MAXATOM],printy[MAXATOM];
    int t[MAXATOM], cnh[MAXATOM], cnc[MAXATOM][5], test[MAXATOM];
    double g, kH;
    char symb[MAXATOM][3], Val[30], coucou[10];
    int x0, yzero, x00, y00;
     
    void init(), InitApp();
    void construction(HWND hWnd, LPARAM lParam), affiche(HWND hWnd), test_position(LPARAM lParam);
    void methane(), ajouter_CH3(int i), coordonnees_fenetre();
    int save();
     
    double xC=0, yC=0, zC=0, xGp, yGp;
    double tetraedre[5][3]={{0., 0., 0.},
    				   	    {1., 1., 1.},
    					    {1., -1., -1.},
    					    {-1., -1., 1.},
    					    {-1., 1., -1.}};
     
     
     
     
    void construction(HWND hWnd, LPARAM lParam)
    {
    	double kH;
     
    	nbatom=nbatom+5;
    	strcpy(symb[1],"C");
    	for (i=2;i<=nbatom;i++)
    	{
    		strcpy(symb[i],"H");
    	}
    	for (i=1;i<=nbatom;i++)	//ajout des coordonnées des nouveaux atomes
    	{
    		x[i]= tetraedre[i-1][0];
    		y[i]= tetraedre[i-1][1];
    		z[i]= tetraedre[i-1][2];
    	}
    	for (i=2;i<=nbatom;i++)	//mise à l'echelle des coordonnées
    	{
    		kH=CH/sqrt((x[i]-xC)*(x[i]-xC)+(y[i]-yC)*(y[i]-yC)+(z[i]-zC)*(z[i]-zC));
    		x[i]=(x[i]-xC)*kH+xC;
    		y[i]=(y[i]-yC)*kH+yC;
    		z[i]=(z[i]-zC)*kH+zC;
    	}
    }
     
    void methane()
    {
        int k;
    	nbatom=5;
    	//strcpy (coucou,"coucou");
    	strcpy(symb[1],"C");
    	for (i=2;i<=nbatom;i++)
    	{
    		strcpy(symb[i],"H");
    		t[i]=1;
     
    	}
    	for (i=1;i<=nbatom;i++)	//ajout des coordonnées des nouveaux atomes
    	{
    		x[i]= tetraedre[i-1][0];
    		y[i]= tetraedre[i-1][1];
    		z[i]= tetraedre[i-1][2];
    	}
    	for (i=2;i<=nbatom;i++)	//mise à l'echelle des coordonnées
    	{
    		kH=CH/sqrt((x[i]-xC)*(x[i]-xC)+(y[i]-yC)*(y[i]-yC)+(z[i]-zC)*(z[i]-zC));
    		x[i]=(x[i]-xC)*kH+xC;
    		y[i]=(y[i]-yC)*kH+yC;
    		z[i]=(z[i]-zC)*kH+zC;
    		cnc[i][1]=1;
    		cnc[1][i-1]=i;
    	}
    	xGp=x00;
    	yGp=y00;
     
       for(k=1;k<=5;k++)
    	{
            test[k]=rand()%10+1;
    	}
    	coordonnees_fenetre();
     
    }
     
    void ajouter_CH3(int i)
    {
    	int k;
    	double a, b, c;
    	strcpy(symb[i],"C");
     
    	x[i]=(x[i]-x[cnc[i][1]])*CC/CH+x[cnc[i][1]];
    	y[i]=(y[i]-y[cnc[i][1]])*CC/CH+y[cnc[i][1]];
    	z[i]=(z[i]-z[cnc[i][1]])*CC/CH+z[cnc[i][1]];
     
    	for (j=1;j<=4;j++)
    	{
    		a=(x[i]-x[cnc[i][1]])*t[i]*tetraedre[j][0];
    		b=(y[i]-y[cnc[i][1]])*t[i]*tetraedre[j][1];
    		c=(z[i]-z[cnc[i][1]])*t[i]*tetraedre[j][2];
    		sprintf(Val, "%f   %f   %f  %d", a, b, c, i);
     
    		if(	  a>0				//repérer quel est l'atome d'hydrogène cliqué
    			&&b>0
    			&&c>0)
    		{
    			for (k=1;k<=4;k++)
    			{
    				if(k==j)
    					continue;
    				nbatom++;
    				strcpy(symb[nbatom],"H"); //ajoute les H et leur nouvelles coordonnées
    				t[nbatom]=-t[i];
    				x[nbatom]=x[i]+t[nbatom]*kH*tetraedre[k][0];
    				y[nbatom]=y[i]+t[nbatom]*kH*tetraedre[k][1];
    				z[nbatom]=z[i]+t[nbatom]*kH*tetraedre[k][2];
    				cnc[nbatom][1]=i;
    			}
    			t[i]=-t[i];		//met à jour la matrice des numéros de connexion des carbones
    			for(k=2;k<=4;k++)
                    cnc[i][k]=nbatom-4+k;
     
    			break;
    		}
    	}
     
    	coordonnees_fenetre();
    }

  2. #2
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 3
    Par défaut
    personne n'a jamais eu ce type de problème?

    Au départ, je pensais que c'était du à de nombreuses approximations successives mais le fait de passer les variables en double n'a rien changé.

    Qu'est ce qu'il faudrait que je fasse pour que le programme soit plus simple à comprendre? (en soit les fonctions utilisées sont ultra basiques!) ?

    Merci.

    Chemjack

  3. #3
    Expert confirmé

    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    10 610
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 610
    Billets dans le blog
    2
    Par défaut
    Citation Envoyé par chemjack Voir le message
    personne n'a jamais eu ce type de problème?

    Au départ, je pensais que c'était du à de nombreuses approximations successives mais le fait de passer les variables en double n'a rien changé.

    Qu'est ce qu'il faudrait que je fasse pour que le programme soit plus simple à comprendre? (en soit les fonctions utilisées sont ultra basiques!) ?

    Merci.

    Chemjack
    si mais ton code est long, et on ne vas pas passer à travers tout..

    Simplement en général c'est un facteur oublié (ou au contraire un redressement... ) : dès que tu fais un cos ou un sin, tu es inférieur à 1.. Donc si tu souhaites revenir à des coordonées normales, il faut des règles de 3.

    Dans une rotation, tu fais des cos et des sin. MAIS le résultat est correct si tu es dans un intervalle [0,1] (et angle [0,PI/2] ou [-PI/2,PI/2] ou [0,PI] ou [0,2*PI])

    Il faut donc, avant et après, recentrer par rapport au centre de la rotation..

    x1 = x - xc
    y1 = y - yc

    puis rotation puis

    x' = rot(x1) + xc
    y' = rot(y1) + yc

  4. #4
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 3
    Par défaut
    Merci Souviron

    Cependant j'y ai réfléchi et je doute un peu que le problème vienne de là...
    Imaginons une rotation autour de l'axe z (donc la molécule tourne dans le plan),
    la matrice de rotation vaudra:

    ( cos (theta) sin(theta) 0 )
    ( -sin(theta) cos(theta) 0 )
    ( 0 0 1 )

    Et donc on vérifie que chaque vecteur colonne a une norme bien égale à 1.

    Et j'ai pensé à recentrer, au début de l'algo de rotation, je centre par rapport au barycentre de la molécule et je divise par le rayon de la sphère de rotation pour travailler avec valeurs € [0,1] et ne pas avoir besoin de mise à l'échelle.

    Je te remet la fonction "TrackBall" qui normalement contient toutes les infos nécessaires. ("rotation" ne fait qu'appliquer la matrice aux coodronnées).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    void TrackBall(WPARAM wParam, LPARAM lParam, UINT messg, HWND hWnd)
    {
        xP = LOWORD (lParam);		//récupération des coordonnées du pointeur souris
        yP = HIWORD (lParam);
     
        xP = (xP-xGp)/r2;
        yP = (yP-yGp)/r2;
     
        zP=sqrt(r2*r2-yP*yP-xP*xP);
     
     
    if(wParam & MK_LBUTTON) // permet de savoir si le bouton gauche de la souris est maintenu enfoncée
    	{
            d=sqrt((xP-x00)*(xP-x00)+(yP-y00)*(yP-y00)+(zP-z00)*(zP-z00));
    		theta = acos(x00);                                     //calcul des angles nécessaires à la rotation à partir du mouvement du pointeur.
    		tau = acos((xP-x00)/d)-theta;
     
    		if((x00*x00+y00*y00)>=1)
                omega=Pi/2;
            else
                omega=asin(sqrt(x00*x00+y00*y00));
    		//phi = (Pi/2)*d*(1-(1-0,2/Pi)*omega*(2/Pi)*(1-sqrt(cos(tau)*cos(tau)))); //angle de rotation
            phi=(Pi*d);
     
     
            a[0]=-sin(tau);
            a[1]=cos(tau);
            a[2]=0;
     
            a1[0]=a[0]*cos(omega)+a[2]*sin(omega);   //calcul du vecteur orthogonal à la rotation
            a1[1]=a[1];
            a1[2]=-a[0]*sin(omega)+a[2]*cos(omega);
     
            a2[0]=a1[0]*cos(theta)-a1[1]*sin(theta);
            a2[1]=a1[0]*sin(theta)+a1[1]*cos(theta);
            a2[2]=a1[2];
     
    		s=sin(phi);			//variables utilisées pour le calcul de la matrice de rotation
    		c=cos(phi);
    		t=1-cos(phi);
     
    		for(i=0;i<=2;i++)
    		{
    			R[i][i]=(t*a2[i]*a2[i]+c);
    		}
     
    		R[0][1]=t*a2[0]*a2[1]+s*a2[2];		//calcul de la matrice de rotation
    		R[0][2]=t*a2[0]*a2[2]-s*a2[1];
    		R[1][0]=t*a2[0]*a2[1]-s*a2[2];
    		R[1][2]=t*a2[1]*a2[2]+s*a2[0];
            R[2][0]=t*a2[0]*a2[2]+s*a2[1];
    		R[2][1]=t*a2[1]*a2[2]-s*a2[0];
     
            /*for(i=0;i<=2;i++)
            {
                col[i]=sqrt(R[i][0]*R[i][0]+R[i][1]*R[i][1]+R[i][2]+R[i][2]);
                R[i][0]=R[i][0]/col[i];
                R[i][1]=R[i][1]/col[i];
                R[i][2]=R[i][2]/col[i];
            }*/
     
        //    rotation();
     
     
    		InvalidateRect(hWnd,NULL,TRUE);
    	}
     
            x00=xP;
            y00=yP;
            z00=zP;
     
     
    }

  5. #5
    Expert confirmé

    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    10 610
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 610
    Billets dans le blog
    2
    Par défaut
    c'est bien ce que j'ai dit plus haut....


Discussions similaires

  1. Réponses: 15
    Dernier message: 21/02/2007, 17h29
  2. [MySQL] Problème avec un double quote
    Par wormseric dans le forum PHP & Base de données
    Réponses: 7
    Dernier message: 26/01/2007, 14h44
  3. Formulaires : problème avec les slashes et les quotes
    Par GarGamel55 dans le forum Langage
    Réponses: 1
    Dernier message: 12/10/2005, 15h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo