-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point3D.cpp
158 lines (96 loc) · 3.32 KB
/
Point3D.cpp
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
// Point3D Class
// Damien MATTEI
#include "Point3D.hpp"
// implementations
template <class T> Point3D<T>::Point3D() : x(0) , y(0) , z(0) {
#ifdef CHECK_MEMORY
cpt_cons++; // for verifying memory allocation/desallocation
#endif
#ifdef DISPLAY_CONSTRUCTOR
cout << "# constructor " << this->display() << " #" << endl;
#endif
}
template <class T> Point3D<T>::Point3D(T x , T y , T z) : x(x) , y(y) , z(z) {
// this->z =z;
#ifdef CHECK_MEMORY
cpt_cons++; // for verifying memory allocation/desallocation
#endif
#ifdef DISPLAY_CONSTRUCTOR
cout << "# constructor " << this->display() << " #" << endl;
#endif
}
template <class T> Point3D<T>::~Point3D() {
#ifdef CHECK_MEMORY
cpt_cons--; // for verifying memory allocation/desallocation
printf("Point3D<T>::~Point3D() : remaining %i Point3D(s).\n",cpt_cons);
#endif
#ifdef DISPLAY_CONSTRUCTOR
cout << "# destructor " << this->display() << " #" << endl;
#endif
}
template <class T> string Point3D<T>::display(void) {
std::stringstream stream;
stream << "Point3D @" << " 0x" << std::hex << (long)this << " " << *this;
return stream.str();
}
template <class T> ostream& operator<< (ostream &out, const Point3D<T> &p3d)
{
out << "Point3D "
<< &p3d // display the adress, without & i should make a recursive call to << operator !!!
<< " (" << p3d.x << ", " << p3d.y << ", " << p3d.z << ")";
return out;
}
// copy constructor
// exist but should NOT be used because one point in Universe should be unique
template <class T> Point3D<T>::Point3D(const Point3D<T> &onePoint3D) {
#ifdef CHECK_MEMORY
cpt_cons++; // for verifying memory allocation/desallocation
#endif
x=onePoint3D.x;
y=onePoint3D.y;
z=onePoint3D.z;
#ifdef DISPLAY_CONSTRUCTOR
cout << "# Point3D copy constructor # " << &onePoint3D << " => " << this << " " << *this << endl;
#endif
}
// assignation operator
// same comment as for copy constructor:
// exist but should NOT be used because one point in Universe should be unique
template <class T> Point3D<T> & Point3D<T>::operator=(const Point3D<T> &onePoint3D) {
#ifdef DISPLAY_ASSIGN
cout << "# Point3D assignation operator #" << endl;
#endif
if (this != &onePoint3D)
{
Point3D<T>::x=onePoint3D.x; // just for test
y=onePoint3D.y;
z=onePoint3D.z;
// delete [] tableau;
}
return *this;
}
/* we need to specialize the hash function for our class
because standart hash function works only with basic types
such as string,int,etc...
*/
// hash function for Point3D <-> Pixel
template <class T> struct hash_point3d {
size_t operator()(const Point3D<T> &p3d ) const
{
return hash<const Point3D<T> *>()(&p3d); // hash code is made with address !!!
}
};
// equality test, mainly used with hash tables
template <class T> struct point3DEquals : binary_function<const Point3D<T>&, const Point3D<T>&, bool> {
bool operator()( const Point3D<T>& lhs, const Point3D<T>& rhs ) const
{
return (&lhs == &rhs); // i compare the addresses !!!
}
};
// equality operator
template <class T> bool Point3D<T>::operator== (const Point3D<T> &p3d) {
return (x==p3d.x) && (y==p3d.y) && (z==p3d.z);
}
// create an instanciation that will be usefull at linking
template class Point3D<float>;
template ostream& operator<< (ostream &, const Point3D<float> &);