INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
PointVector.h
Go to the documentation of this file.
1/**
2@file PointVector.h
3@author JOL
4*/
5#pragma once
6#ifndef _POINT_VECTOR_H_
7#define _POINT_VECTOR_H_
8
9#include <cassert>
10#include <cmath>
11#include <array>
12
13/// Namespace RayTracer
14namespace rt {
15
16 /**
17 Model a static vector T[N], with some operations.
18 */
19 template <typename T, std::size_t N>
20 struct PointVector : public std::array<T, N> {
21 typedef PointVector<T, N> Self;
22 typedef std::array<T, N> Base;
23 typedef std::size_t Size;
24
25 using Base::begin;
26 using Base::end;
27 using Base::size;
28 using Base::max_size;
29 using Base::operator[];
30 using Base::at;
31 using Base::front;
32 using Base::back;
33 using Base::data;
34
35 PointVector() {}
36
37 PointVector( std::initializer_list<T> L )
38 {
39 Size i = 0;
40 for ( auto v : L ) if ( i < N ) (*this)[ i++ ] = v;
41 }
42 PointVector( T val0 )
43 {
44 assert( 0 < N );
45 (*this)[ 0 ] = val0;
46 }
47 PointVector( T val0, T val1 )
48 {
49 assert( 1 < N );
50 (*this)[ 0 ] = val0;
51 (*this)[ 1 ] = val1;
52 }
53 PointVector( T val0, T val1, T val2 )
54 {
55 assert( 2 < N );
56 (*this)[ 0 ] = val0;
57 (*this)[ 1 ] = val1;
58 (*this)[ 2 ] = val2;
59 }
60 PointVector( T val0, T val1, T val2, T val3 )
61 {
62 assert( 3 < N );
63 (*this)[ 0 ] = val0;
64 (*this)[ 1 ] = val1;
65 (*this)[ 2 ] = val2;
66 (*this)[ 3 ] = val3;
67 }
68 PointVector( const T* vals )
69 {
70 for ( Size i = 0; i < N; i++ ) (*this)[ i ] = *vals++;
71 }
72 // Useful for conversion to OpenGL vectors
73 operator T*() { return data(); }
74 // Useful for conversion to OpenGL vectors
75 operator const T*() const { return data(); }
76
77 void selfDisplay( std::ostream& out ) const
78 {
79 out << "(";
80 for ( Size i = 0; i < N; i++ )
81 out << (*this)[ i ] << ( ( i < N-1 ) ? ',' : ')' );
82 }
83
84 Self& operator+=( const Self& other )
85 {
86 for ( Size i = 0; i < N; ++i ) (*this)[ i ] += other[ i ];
87 return *this;
88 }
89 Self& operator-=( const Self& other )
90 {
91 for ( Size i = 0; i < N; ++i ) (*this)[ i ] -= other[ i ];
92 return *this;
93 }
94 Self& operator*=( T val )
95 {
96 for ( Size i = 0; i < N; ++i ) (*this)[ i ] *= val;
97 return *this;
98 }
99 Self& operator/=( T val )
100 {
101 for ( Size i = 0; i < N; ++i ) (*this)[ i ] /= val;
102 return *this;
103 }
104
105 /// dot product (produit scalaire).
106 T dot( const Self& other ) const
107 {
108 T result = 0;
109 for ( Size i = 0; i < N; ++i ) result += (*this)[ i ] * other[ i ];
110 return result;
111 }
112 /// cross product (produit vectoriel).
113 Self cross( const Self& other ) const
114 {
115 assert( N == 3 );
116 return Self( (*this)[1]*other[2] - (*this)[2]*other[1],
117 (*this)[2]*other[0] - (*this)[0]*other[2],
118 (*this)[0]*other[1] - (*this)[1]*other[0] );
119 }
120
121 Self operator+( const Self& other ) const
122 {
123 Self result( *this );
124 result += other;
125 return result;
126 }
127
128 Self operator-( const Self& other ) const
129 {
130 Self result( *this );
131 result -= other;
132 return result;
133 }
134
135 T norm() const
136 {
137 return sqrt( dot( *this ) );
138 }
139 };
140
141 ///////////////////////////////////////////////////////////////////////////////
142 // A few useful operators
143 ///////////////////////////////////////////////////////////////////////////////
144
145 template <typename T, std::size_t N>
146 std::ostream& operator<<( std::ostream& out, const PointVector<T,N>& PV )
147 {
148 PV.selfDisplay( out );
149 return out;
150 }
151
152 template <typename T, std::size_t N>
153 PointVector<T,N> operator*( T val, const PointVector<T,N>& PV )
154 {
155 typedef typename PointVector<T,N>::Size Size;
156 PointVector<T,N> result( PV );
157 for ( Size i = 0; i < N; ++i ) result[ i ] *= val;
158 return result;
159 }
160
161 template <typename T, std::size_t N>
162 PointVector<T,N> operator*( const PointVector<T,N>& PV, T val )
163 {
164 typedef typename PointVector<T,N>::Size Size;
165 PointVector<T,N> result( PV );
166 for ( Size i = 0; i < N; ++i ) result[ i ] *= val;
167 return result;
168 }
169
170 template <typename T, std::size_t N>
171 PointVector<T,N> operator/( T val, const PointVector<T,N>& PV )
172 {
173 typedef typename PointVector<T,N>::Size Size;
174 PointVector<T,N> result( PV );
175 for ( Size i = 0; i < N; ++i ) result[ i ] = val / result[ i ];
176 return result;
177 }
178
179 template <typename T, std::size_t N>
180 PointVector<T,N> operator/( const PointVector<T,N>& PV, T val )
181 {
182 typedef typename PointVector<T,N>::Size Size;
183 PointVector<T,N> result( PV );
184 for ( Size i = 0; i < N; ++i ) result[ i ] /= val;
185 return result;
186 }
187
188 template <typename T, std::size_t N>
189 inline
190 T distance2( const PointVector<T,N>& p1, const PointVector<T,N>& p2 )
191 {
192 PointVector<T,N> tmp = p2 - p1;
193 return tmp.dot( tmp );
194 }
195
196 template <typename T, std::size_t N>
197 inline
198 T distance( const PointVector<T,N>& p1, const PointVector<T,N>& p2 )
199 {
200 return sqrt( distance2( p1, p2 ) );
201 }
202 ///////////////////////////////////////////////////////////////////////////////
203 // Used types
204 ///////////////////////////////////////////////////////////////////////////////
205 /// the type for representing a real number.
206 typedef float Real;
209 /// The type for representing a 3d point.
211 /// The type for representing a 3d vector;
215
216} // namespace rt
217
218#endif // _POINT_VECTOR_H_
219
Namespace RayTracer.
Definition Color.h:11
float Real
the type for representing a real number.
PointVector< Real, 3 > Point3
The type for representing a 3d vector;.
PointVector< Real, 3 > Vector3
The type for representing a 3d point.
T dot(const Self &other) const
dot product (produit scalaire).
Self cross(const Self &other) const
cross product (produit vectoriel).