Lesson of 20 July 2009 (afternoon)¶
Skeleton of a Simple vector/matrix library for 3d vector and 3x3 matrix (C++ style)
File lib3d.hh
1/*
2 File: lib3d.hh
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_HH
10#define LIB3D_HH // define an empty macro, only to define the symbol LIB3D_HH
11
12#include <iostream>
13
14// valueType is the "real number" type
15typedef double valueType ;
16
17// A vector is a CLASS with 3 field, x y z
18class point3d {
19private:
20 valueType x ;
21 valueType y ;
22 valueType z ;
23public:
24 valueType *ptr ;
25private:
26private:
27private:
28public:
29 // constructor
30 point3d() {
31 std::cout << "pass to constructor 1\n" ;
32 x=y=z=0 ;
33 ptr = new valueType[100] ;
34 }
35
36 point3d( valueType x, valueType y, valueType z ) {
37 std::cout << "pass to constructor 2\n" ;
38 this -> x = x ;
39 this -> y = y ;
40 this -> z = z ;
41 ptr = new valueType[10] ;
42 }
43
44 ~point3d() {
45 delete [] ptr ;
46 std::cout << "pass to destructor\n" ;
47 }
48
49 point3d const &
50 operator = ( point3d const & in ) ;
51
52 void
53 setup( valueType x, valueType y, valueType z ) ;
54
55 valueType getx() const { return x ; } // const means that the method
56 // do not modify the class
57
58 valueType gety() const { return y ; }
59 valueType getz() const { return z ; }
60} ;
61
62std::ostream & // return a reference (pointer) to the object
63 // ostream is into the namespace std
64operator << ( std::ostream & s, // left argumenet of left << right
65 point3d const & v ) ; // right argumenet of left << right
66
67
68point3d scal( valueType s, point3d const & v ) ;
69
70point3d
71add( point3d const & v1,
72 point3d const & v2 ) ;
73
74point3d
75sub( point3d const & v1,
76 point3d const & v2 ) ;
77
78point3d
79vector_product( point3d const & v1,
80 point3d const & v2 ) ;
81
82valueType dot( point3d const & v1, point3d const & v2 ) ;
83
84// Use external operator
85inline
86point3d
87operator ^ ( point3d const & a, point3d const & b )
88{ return vector_product( a, b ) ; }
89
90inline
91valueType
92operator * ( point3d const & a, point3d const & b )
93{ return dot( a, b ) ; }
94
95
96// matrix 3 by 3 type
97class matrix3d {
98 valueType m[3][3] ;
99} ;
100
101#endif /* close LIB3D_HH */
File lib3d.cc
1/*
2 File: lib3d.c
3 Simple vector/matrix library for 3d vector and 3x3 matrix.
4*/
5
6#include "lib3d.hh"
7
8// define the copy opersator = for class point3d
9point3d const &
10point3d::operator = ( point3d const & in ) {
11 std::cout << "pass to copy operator\n" ;
12 this -> x = in . x ;
13 this -> y = in . y ;
14 this -> z = in . z ;
15}
16
17// define the method setup of class point3d
18void
19point3d::setup( valueType x, valueType y, valueType z ) {
20 this -> x = x ;
21 this -> y = y ;
22 this -> z = z ;
23}
24
25std::ostream & // return a reference (pointer) to the object
26 // ostream is into the namespace std
27operator << ( std::ostream & s, // left argumenet of left << right
28 point3d const & v ) { // right argumenet of left << right
29 s << "[ " << v.getx() << " , " << v.gety() << " , " << v.getz() << " ]" ;
30 return s ;
31}
32
33point3d
34scal( valueType s, point3d const & v ) {
35 valueType x = v . getx() * s ;
36 valueType y = v . gety() * s ;
37 valueType z = v . getz() * s ;
38 //point3d res( x, y, z ) ;
39 //return res ;
40 return point3d(x, y, z) ;
41}
42
43point3d
44add( point3d const & v1,
45 point3d const & v2 ) {
46 return point3d( v1 . getx() + v2 . getx(),
47 v1 . gety() + v2 . gety(),
48 v1 . getz() + v2 . getz() ) ;
49}
50
51point3d
52sub( point3d const & v1,
53 point3d const & v2 ) {
54 return point3d( v1 . getx() - v2 . getx(),
55 v1 . gety() - v2 . gety(),
56 v1 . getz() - v2 . getz() ) ;
57}
58
59point3d
60vector_product( point3d const & v1,
61 point3d const & v2 ) {
62 return point3d( v1 . gety() * v2 . getz() - v1 . getz() * v2 . gety(),
63 v1 . getz() * v2 . getx() - v1 . getx() * v2 . getz(),
64 v1 . getx() * v2 . gety() - v1 . gety() * v2 . getx() ) ;
65}
66
67valueType
68dot( point3d const & v1,
69 point3d const & v2 ) {
70 return v1 . getx() * v2 . getx() +
71 v1 . gety() * v2 . gety() +
72 v1 . getz() * v2 . getz() ;
73}
74
75/*
76void
77l3d_zero_matrix( matrix3d m ) {
78 m[0][0] = m[0][1] = m[0][2] =
79 m[1][0] = m[1][1] = m[1][2] =
80 m[2][0] = m[2][1] = m[2][2] = 0 ;
81}
82
83void
84l3d_id_matrix( matrix3d m ) {
85 m[0][1] = m[0][2] = m[1][0] =
86 m[1][2] = m[2][0] = m[2][1] = 0 ;
87 m[0][0] = m[1][1] = m[2][2] = 1 ;
88}
89
90void
91l3d_ones_matrix( matrix3d m ) {
92 m[0][0] = m[0][1] = m[0][2] =
93 m[1][0] = m[1][1] = m[1][2] =
94 m[2][0] = m[2][1] = m[2][2] = 1 ;
95}
96
97void
98l3d_mv( matrix3d m,
99 point3d const * v,
100 point3d * pRes ) {
101 valueType tmp1 = m[0][0] * v -> x + m[0][1] * v -> y + m[0][2] * v -> z ;
102 valueType tmp2 = m[1][0] * v -> x + m[1][1] * v -> y + m[1][2] * v -> z ;
103 pRes -> z = m[2][0] * v -> x + m[2][1] * v -> y + m[2][2] * v -> z ;
104 pRes -> y = tmp2 ;
105 pRes -> x = tmp1 ;
106}
107*/
File test.cc
1#include "lib3d.hh"
2#include <cmath>
3
4using namespace std ;
5
6int
7main() {
8
9 cout << "begin program\n" ;
10 point3d a, b, c ;
11 cout << "v1 and v2 defined and instanced\n" ;
12 matrix3d M ;
13 cout << "M and instanced\n" ;
14
15 a . setup( 1, sqrt(3.0), 0 ) ;
16 b . setup( 0.1, 1, 0 ) ;
17 c . setup( 0, 1, 2.1 ) ;
18
19 cout << "Volume = " << dot( vector_product(a,b), c ) << '\n' ;
20 cout << "Volume = " << (a^b)*c << '\n' ;
21
22 a = b ;
23 // is equivalent
24 a . operator = ( b ) ;
25
26 operator * ( (operator ^(a,b)), c ) ;
27
28// add( v1, v2, v3 ) ;
29// sub( v1, v2, v3 ) ;
30// vector_product( v1, v2, v3 ) ;
31
32 cout << "before point3d v3 = scal( 3, a ) \n" ;
33 point3d v3 = scal( 3, a ) ;
34 cout << "after point3d v3 = scal( 3, a ) \n" ;
35
36 std::cout << "V3 = " << v3 << '\n' ;
37
38 return 0 ;
39}