Vector3d.cpp
Upload User: hcfgz168
Upload Date: 2011-09-11
Package Size: 116k
Code Size: 9k
Category:

OpenGL program

Development Platform:

WINDOWS

  1. //********************************************
  2. // Vector3d.cpp
  3. //********************************************
  4. // class CVector3d
  5. //********************************************
  6. // pierre.alliez@cnet.francetelecom.fr
  7. // Created : 10/12/97
  8. // Modified : 20/01/98
  9. //********************************************
  10. #include "stdafx.h"
  11. #include <math.h>
  12. #include "Vector3d.h"
  13. #include "Base3d.h"
  14. //////////////////////////////////////////////
  15. // CONSTRUCTORS
  16. //////////////////////////////////////////////
  17. //********************************************
  18. // Destructor
  19. //********************************************
  20. CVector3d::~CVector3d()
  21. {
  22. }
  23. //********************************************
  24. // Constructor
  25. //********************************************
  26. CVector3d::CVector3d(CVector3d &vector)
  27. {
  28. Set(&vector);
  29. }
  30. //********************************************
  31. // Constructor
  32. //********************************************
  33. CVector3d::CVector3d(CVector3d *pVector)
  34. {
  35. Set(pVector);
  36. }
  37. //********************************************
  38. // Constructor
  39. //********************************************
  40. CVector3d::CVector3d(const float x,
  41.  const float y,
  42.  const float z)
  43. {
  44. Set(x,y,z);
  45. }
  46. //********************************************
  47. // Constructor
  48. //********************************************
  49. CVector3d::CVector3d(CVertex3d *pVertex1,
  50.  CVertex3d *pVertex2)
  51. {
  52. m_x = pVertex2->x()-pVertex1->x();
  53. m_y = pVertex2->y()-pVertex1->y();
  54. m_z = pVertex2->z()-pVertex1->z();
  55. }
  56. //********************************************
  57. // Constructor
  58. //********************************************
  59. CVector3d::CVector3d(CVertex3d &vertex1,
  60.  CVertex3d &vertex2)
  61. {
  62. m_x = vertex2.x()-vertex1.x();
  63. m_y = vertex2.y()-vertex1.y();
  64. m_z = vertex2.z()-vertex1.z();
  65. }
  66. //////////////////////////////////////////////
  67. // DATAS
  68. //////////////////////////////////////////////
  69. //********************************************
  70. // Copy
  71. //********************************************
  72. void CVector3d::Copy(CVector3d &vector)
  73. {
  74. Set(&vector);
  75. }
  76. //********************************************
  77. // Copy
  78. //********************************************
  79. void CVector3d::Copy(CVector3d *pVector)
  80. {
  81. Set(pVector);
  82. }
  83. //********************************************
  84. // GetType
  85. //********************************************
  86. int CVector3d::GetType()
  87. {
  88. return TYPE_VECTOR3D;
  89. }
  90. //********************************************
  91. // Set
  92. //********************************************
  93. void CVector3d::Clear(void)
  94. {
  95. Set(0.0f,0.0f,0.0f);
  96. }
  97. //********************************************
  98. // Set
  99. //********************************************
  100. void CVector3d::Set(const float x,
  101. const float y,
  102. const float z)
  103. {
  104. m_x = x;
  105. m_y = y;
  106. m_z = z;
  107. }
  108. //********************************************
  109. // Set
  110. //********************************************
  111. void CVector3d::Set(CVector3d *pVector)
  112. {
  113. Set(pVector->x(),pVector->y(),pVector->z());
  114. }
  115. //********************************************
  116. // Set
  117. //********************************************
  118. void CVector3d::Set(CVector3d &vector)
  119. {
  120. Set(vector.x(),vector.y(),vector.z());
  121. }
  122. //********************************************
  123. // Set
  124. //********************************************
  125. void CVector3d::Set(CVertex3d *pVertex1,
  126.  CVertex3d *pVertex2)
  127. {
  128. m_x = pVertex2->x()-pVertex1->x();
  129. m_y = pVertex2->y()-pVertex1->y();
  130. m_z = pVertex2->z()-pVertex1->z();
  131. }
  132. //********************************************
  133. // Trace
  134. //********************************************
  135. void CVector3d::Trace()
  136. {
  137. TRACE("n");
  138. TRACE("** Vector **n");
  139. TRACE("Adress      : %xn",this);
  140. TRACE("Coordinates : (%g %g %g)n",m_x,m_y,m_z);
  141. }
  142. //////////////////////////////////////////////
  143. // OPERATORS
  144. //////////////////////////////////////////////
  145. //********************************************
  146. // Operator = 
  147. //********************************************
  148. CVector3d CVector3d::operator=(CVector3d& vector)
  149. {
  150. CVector3d v;
  151. v.Set(vector);
  152. return v;
  153. }
  154. //********************************************
  155. // Operator + 
  156. //********************************************
  157. void CVector3d::operator+=(CVector3d* pVector)
  158. {
  159. m_x += pVector->x();
  160. m_y += pVector->y();
  161. m_z += pVector->z();
  162. }
  163. //********************************************
  164. // Operator /=
  165. //********************************************
  166. void CVector3d::operator/=(float factor)
  167. {
  168. m_x /= factor;
  169. m_y /= factor;
  170. m_z /= factor;
  171. }
  172. //********************************************
  173. // Operator *=
  174. //********************************************
  175. void CVector3d::operator*=(float factor)
  176. {
  177. m_x *= factor;
  178. m_y *= factor;
  179. m_z *= factor;
  180. }
  181. //********************************************
  182. // Inner
  183. //********************************************
  184. void CVector3d::Inner(CVector3d& vector)
  185. {
  186. double x = (double)m_y*(double)vector.z()-(double)m_z*(double)vector.y();
  187. double y = (double)m_z*(double)vector.x()-(double)m_x*(double)vector.z();
  188. double z = (double)m_x*(double)vector.y()-(double)m_y*(double)vector.x();
  189. Set((float)x,(float)y,(float)z);
  190. }
  191. //////////////////////////////////////////////
  192. //////////////////////////////////////////////
  193. // PROCESSING
  194. //////////////////////////////////////////////
  195. //////////////////////////////////////////////
  196. //********************************************
  197. // Normalize
  198. //********************************************
  199. void CVector3d::NormalizeL2(void)
  200. {
  201. double norm = GetNormL2();
  202. if(norm != 0.0f)
  203. {
  204. m_x = (float)(m_x / norm);
  205. m_y = (float)(m_y / norm);
  206. m_z = (float)(m_z / norm);
  207. }
  208. //TRACE("norm : %gn",GetNormL2());
  209. }
  210. //********************************************
  211. // Normalize
  212. //********************************************
  213. void CVector3d::NormalizeL2(float value)
  214. {
  215. double norm = GetNormL2();
  216. if(norm != 0.0f)
  217. {
  218. m_x *= (float)(value/norm);
  219. m_y *= (float)(value/norm);
  220. m_z *= (float)(value/norm);
  221. }
  222. //TRACE("norm : %g (wanted : %g)n",GetNormL2(),value);
  223. }
  224. //********************************************
  225. // GetNormSquare
  226. //********************************************
  227. double CVector3d::GetNormL2Square(void)
  228. {
  229. return ((double)m_x*(double)m_x + 
  230.       (double)m_y*(double)m_y + 
  231. (double)m_z*(double)m_z);
  232. }
  233. //********************************************
  234. // GetNorm
  235. //********************************************
  236. double CVector3d::GetNormL2(void)
  237. {
  238. return sqrt((double)m_x*(double)m_x + 
  239.           (double)m_y*(double)m_y + 
  240. (double)m_z*(double)m_z);
  241. }
  242. //********************************************
  243. // Collinear
  244. //********************************************
  245. int CVector3d::Collinear(CVector3d *pVector)
  246. {
  247. float x = pVector->x() / m_x;
  248. float y = pVector->y() / m_y;
  249. float z = pVector->z() / m_z;
  250. return ((x == y) && (y == z));
  251. }
  252. //********************************************
  253. // Collinear
  254. //********************************************
  255. int CVector3d::Collinear(CVector3d &vector)
  256. {
  257. float x = vector.x() / m_x;
  258. float y = vector.y() / m_y;
  259. float z = vector.z() / m_z;
  260. return ((x == y) && (y == z));
  261. }
  262. //////////////////////////////////////////////
  263. //////////////////////////////////////////////
  264. // EXTERNAL OPERATORS
  265. //////////////////////////////////////////////
  266. //////////////////////////////////////////////
  267. //********************************************
  268. // Operator + 
  269. //********************************************
  270. CVector3d operator+(CVector3d& u,
  271.                     CVector3d& v)
  272. {
  273. CVector3d w;
  274. w.Set(u.x()+v.x(),u.y()+v.y(),u.z()+v.z());
  275. return w;
  276. }
  277. //********************************************
  278. // Operator -
  279. //********************************************
  280. CVector3d operator-(CVector3d& u,
  281.                     CVector3d& v)
  282. {
  283. CVector3d w;
  284. w.Set(u.x()-v.x(),u.y()-v.y(),u.z()-v.z());
  285. return w;
  286. }
  287. //********************************************
  288. // Operator ^
  289. //********************************************
  290. CVector3d operator^(CVector3d& u,
  291.                     CVector3d& v)
  292. {
  293. // w = u ^ v
  294. CVector3d w;
  295. w.Set(u.y()*v.z()-u.z()*v.y(),
  296.     u.z()*v.x()-u.x()*v.z(),
  297.     u.x()*v.y()-u.y()*v.x());
  298. return w;
  299. }
  300. //********************************************
  301. // Inner
  302. //********************************************
  303. CVector3d CVector3d::Inner(CVector3d* u,
  304.                            CVector3d* v)
  305. {
  306. // w = u ^ v
  307. CVector3d w;
  308. w.Set(u->y()*v->z()-u->z()*v->y(),
  309.     u->z()*v->x()-u->x()*v->z(),
  310.     u->x()*v->y()-u->y()*v->x());
  311. return w;
  312. }
  313. void CVector3d::RotateXZ(float alpha)
  314. {
  315. float x = (float)(m_x*cos(alpha)+m_z*sin(alpha));
  316. float z = (float)(-m_x*sin(alpha)+m_z*cos(alpha));
  317. m_x = x;
  318. m_z = z;
  319. }
  320. // ** EOF **