summaryrefslogtreecommitdiff
path: root/hw2/include/vec3d.h
blob: 50d918f8d37f79d530eadd70a7901f193d974fa8 (plain) (blame)
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
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/
#ifndef _VEC3D_H_
#define _VEC3D_H_

#include <ostream>
#include <cmath>

class vec3d {
 public:
  /////////////
  // Typedef //
  /////////////
  typedef float             value_type;
  typedef value_type&       reference;
  typedef const value_type& const_reference;
  typedef value_type*       iterator;
  typedef const value_type* const_iterator;

  //////////////////
  // Data Members //
  //////////////////
  union {
    struct { value_type x, y, z; };
    value_type data[3];
  };

 public:
  /////////////////
  // Constructor //
  /////////////////
  explicit vec3d(const_reference value=0.0f);
  vec3d(const_reference x, const_reference y, const_reference z);
  vec3d(const vec3d& v);
 
  ////////////////
  // Inspectors //
  ////////////////
  static size_t size(void) { return 3; }

  iterator begin(void);
  const_iterator begin(void) const;
  iterator end(void);
  const_iterator end(void) const;

  const_reference  operator[](size_t index) const;
  reference        operator[](size_t index);

  ///////////////
  // Operators //
  ///////////////
  vec3d& operator=(const vec3d& v);

  bool operator==(const vec3d& v) const;
  bool operator!=(const vec3d& v) const;

  vec3d operator-(void) const;
  
  vec3d operator+(const vec3d& v) const;
  vec3d operator-(const vec3d& v) const;
  vec3d operator*(const vec3d& v) const;
  vec3d operator*(const_reference scale) const;
  vec3d operator/(const vec3d& v) const;
  vec3d operator/(const_reference scale) const;

  vec3d& operator+=(const vec3d& v);
  vec3d& operator-=(const vec3d& v);
  vec3d& operator*=(const vec3d& v);
  vec3d& operator*=(const_reference scale);
  vec3d& operator/=(const vec3d& v);
  vec3d& operator/=(const_reference scale);

  /////////////
  // Methods //
  /////////////
  value_type dot(const vec3d& v) const;
  value_type squared_length(void) const;
  value_type length(void) const;
  value_type squared_distance(const vec3d& v) const;
  value_type distance(const vec3d& v) const;

  vec3d cross(const vec3d& v) const;

  ///////////////
  // Modifiers //
  ///////////////
  vec3d& abs(void);
  vec3d& clamp(value_type lower=0.0f, value_type upper=1.0f); 
  vec3d& normalize(void);

  /////////////
  // Friends //
  /////////////
  friend void  swap(vec3d& a, vec3d& b) { return a._swap(b); }
  friend vec3d  normalize(const vec3d& v) { return vec3d(v).normalize(); }
  friend vec3d  abs(const vec3d& v) { return vec3d(v).abs(); }  
  friend vec3d  clamp(const vec3d& v, value_type lower=0.0f, value_type upper=1.0f) { return vec3d(v).clamp(lower, upper); }
  friend vec3d operator*(const_reference scale, const vec3d& v) { return (v*scale); }

  friend std::ostream& operator<<(std::ostream& s, const vec3d& v)
  {
    s << "[" << v.x << "," << v.y << "," << v.z << "]";
    return s;
  }

 private:
  /////////////////////
  // Private Methods //
  /////////////////////
  void _assign(const vec3d& v);
  void _swap(vec3d& v);
};

#endif /* _VEC3D_H_ */