Lesson of 20 July 2009 (morning)¶
Skeleton of a Simple vector/matrix library for 3d vector and 3x3 matrix (C style)
File lib3d.h
1/*
2 File: lib3d.h
3 Simple vector/matrix library for 3d vector and 3x3 matrix.
4*/
5
6/* first trick, avoid double inclusion using conditional compilation */
7
8/* check if LIB3D_H is defined, if NOT the lines up to #endif are expanded */
9#ifndef LIB3D_H
10#define LIB3D_H // define an empty macro, only to define the symbol LIB3D_H
11
12#include <stdio.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18 /* valueType is the "real number" type */
19 typedef double valueType ;
20
21 /* A vector is a struct with 3 field, x y z */
22 /*
23 struct _point3d {
24 valueType x ;
25 valueType y ;
26 valueType z ;
27 } ;
28
29 typedef struct _point3d point3d ;
30 */
31 typedef struct {
32 valueType x ;
33 valueType y ;
34 valueType z ;
35 } point3d ;
36
37 /* matrix 3 by 3 type */
38 typedef valueType matrix3d[3][3] ;
39
40 /* prototype for addition of two vectors */
41 void l3d_print( FILE * fd, point3d const * v ) ;
42
43 void l3d_scal_by_vec( valueType, point3d const * v, point3d * pRes );
44
45 /* prototype for addition of two vectors */
46 void l3d_add( point3d const * v1,
47 point3d const * v2,
48 point3d * pRes ) ;
49
50 /* prototype for subtraction of two vectors */
51 void l3d_sub( point3d const * v1,
52 point3d const * v2,
53 point3d * pRes ) ;
54
55 /* prototype for vector product of two vectors */
56 void l3d_vec_product( point3d const * v1,
57 point3d const * v2,
58 point3d * pRes ) ;
59
60 /* prototype for dot product of two vectors */
61 valueType l3d_dot_product( point3d const * v1,
62 point3d const * v2 ) ;
63
64 /* initialize a matrix */
65 void l3d_zero_matrix( matrix3d m ) ;
66 void l3d_id_matrix( matrix3d m ) ;
67 void l3d_ones_matrix( matrix3d m ) ;
68
69 void l3d_addm( matrix3d const m1,
70 matrix3d const m2,
71 matrix3d res ) ;
72
73 /* prototype for subtraction of two vectors */
74 void l3d_subm( matrix3d const m1,
75 matrix3d const m2,
76 matrix3d res ) ;
77
78 void l3d_mv( matrix3d m,
79 point3d const * v,
80 point3d * pRes ) ;
81
82 void l3d_mm( matrix3d const m1,
83 matrix3d const m2,
84 matrix3d res ) ;
85
86#ifdef __cplusplus
87}
88#endif
89
90#endif /* close LIB3D_H */
File lib3d.c
1/*
2 File: lib3d.c
3 Simple vector/matrix library for 3d vector and 3x3 matrix.
4*/
5
6#include "lib3d.h"
7
8void
9l3d_print( FILE * fd, point3d const * v ) {
10 fprintf( fd, "[ %lf %lf %lf]\n", v -> x, v -> y, v -> z ) ;
11}
12
13void
14l3d_scal_by_vec( valueType s, point3d const * v, point3d * pRes ) {
15 pRes -> x = v -> x * s ;
16 pRes -> y = v -> y * s ;
17 pRes -> z = v -> z * s ;
18}
19
20
21void
22l3d_add( point3d const * v1,
23 point3d const * v2,
24 point3d * pRes ) {
25 /*
26 (*pRes) . x = v1 . x + v2 . x ;
27 (*pRes) . y = v1 . y + v2 . y ;
28 (*pRes) . z = v1 . z + v2 . z ;
29 */
30 pRes -> x = v1 -> x + v2 -> x ;
31 pRes -> y = v1 -> y + v2 -> y ;
32 pRes -> z = v1 -> z + v2 -> z ;
33}
34
35void
36l3d_sub( point3d const * v1,
37 point3d const * v2,
38 point3d * pRes ) {
39 pRes -> x = v1 -> x - v2 -> x ;
40 pRes -> y = v1 -> y - v2 -> y ;
41 pRes -> z = v1 -> z - v2 -> z ;
42}
43
44void
45l3d_vec_product( point3d const * v1,
46 point3d const * v2,
47 point3d * pRes ) {
48 pRes -> x = v1 -> y * v2 -> z - v1 -> z * v2 -> y ;
49 pRes -> y = v1 -> z * v2 -> x - v1 -> x * v2 -> z ;
50 pRes -> z = v1 -> x * v2 -> y - v1 -> y * v2 -> x ;
51}
52
53valueType
54l3d_dot_product( point3d const * v1,
55 point3d const * v2 ) {
56 return v1 -> x * v2 -> x +
57 v1 -> y * v2 -> y +
58 v1 -> z * v2 -> z ;
59}
60
61void
62l3d_zero_matrix( matrix3d m ) {
63 m[0][0] = m[0][1] = m[0][2] =
64 m[1][0] = m[1][1] = m[1][2] =
65 m[2][0] = m[2][1] = m[2][2] = 0 ;
66}
67
68void
69l3d_id_matrix( matrix3d m ) {
70 m[0][1] = m[0][2] = m[1][0] =
71 m[1][2] = m[2][0] = m[2][1] = 0 ;
72 m[0][0] = m[1][1] = m[2][2] = 1 ;
73}
74
75void
76l3d_ones_matrix( matrix3d m ) {
77 m[0][0] = m[0][1] = m[0][2] =
78 m[1][0] = m[1][1] = m[1][2] =
79 m[2][0] = m[2][1] = m[2][2] = 1 ;
80}
81
82void
83l3d_mv( matrix3d m,
84 point3d const * v,
85 point3d * pRes ) {
86 valueType tmp1 = m[0][0] * v -> x + m[0][1] * v -> y + m[0][2] * v -> z ;
87 valueType tmp2 = m[1][0] * v -> x + m[1][1] * v -> y + m[1][2] * v -> z ;
88 pRes -> z = m[2][0] * v -> x + m[2][1] * v -> y + m[2][2] * v -> z ;
89 pRes -> y = tmp2 ;
90 pRes -> x = tmp1 ;
91}
File test.c
1#include "lib3d.h"
2#include <stdio.h>
3
4int
5main() {
6
7 point3d v1, v2, v3, vbuff ;
8 matrix3d M ;
9
10 v1 . x = 1 ;
11 v1 . y = 0 ;
12 v1 . z = 0 ;
13
14 v2 . x = 0 ;
15 v2 . y = 1 ;
16 v2 . z = 0 ;
17
18 l3d_ones_matrix( M ) ;
19
20 /* M * (v1 + 3 * v2) */
21 l3d_scal_by_vec( 3, & v2, & vbuff ); /* 3 * v2 --> vbuff */
22 l3d_add( & v1, & vbuff, & vbuff ); /* v1 + vbuff --> vbuff */
23 l3d_mv( M, & vbuff, & vbuff ) ;
24 l3d_print( stdout, & vbuff ) ;
25
26 l3d_vec_product( & v1, & v2, & v3 );
27
28 l3d_print( stdout, & v3 ) ;
29
30 /*printf( " v3 . x = %lf\n", v3 . x ) ;
31 printf( " v3 . y = %lf\n", v3 . y ) ;
32 printf( " v3 . z = %lf\n", v3 . z ) ;
33 */
34 return 0 ;
35}