1 /********************************************************************************
2 * *
3 * D o u b l e - P r e c i s i o n 4 - E l e m e n t V e c t o r *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1994,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXVec4d.cpp,v 1.15 2006/01/22 17:58:51 fox Exp $ *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "FXHash.h"
28 #include "FXStream.h"
29 #include "FXObject.h"
30 #include "FXVec2d.h"
31 #include "FXVec3d.h"
32 #include "FXVec4d.h"
33 #include "FXQuatd.h"
34 #include "FXMat4d.h"
35
36
37 using namespace FX;
38
39 /*******************************************************************************/
40
41 namespace FX {
42
FXVec4d(FXColor color)43 FXVec4d::FXVec4d(FXColor color){
44 x=0.003921568627*FXREDVAL(color);
45 y=0.003921568627*FXGREENVAL(color);
46 z=0.003921568627*FXBLUEVAL(color);
47 w=0.003921568627*FXALPHAVAL(color);
48 }
49
50
operator =(FXColor color)51 FXVec4d& FXVec4d::operator=(FXColor color){
52 x=0.003921568627*FXREDVAL(color);
53 y=0.003921568627*FXGREENVAL(color);
54 z=0.003921568627*FXBLUEVAL(color);
55 w=0.003921568627*FXALPHAVAL(color);
56 return *this;
57 }
58
59
operator FXColor() const60 FXVec4d::operator FXColor() const {
61 return FXRGBA((x*255.0),(y*255.0),(z*255.0),(w*255.0));
62 }
63
64
normalize(const FXVec4d & v)65 FXVec4d normalize(const FXVec4d& v){
66 register FXdouble t=v.length();
67 if(t>0.0){ return FXVec4d(v.x/t,v.y/t,v.z/t,v.w/t); }
68 return FXVec4d(0.0,0.0,0.0,0.0);
69 }
70
71
72 // Compute plane equation from 3 points a,b,c
plane(const FXVec3d & a,const FXVec3d & b,const FXVec3d & c)73 FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c){
74 FXVec3d nm(normal(a,b,c));
75 return FXVec4d(nm,-(nm.x*a.x+nm.y*a.y+nm.z*a.z));
76 }
77
78
79 // Compute plane equation from vector and distance
plane(const FXVec3d & vec,FXdouble dist)80 FXVec4d plane(const FXVec3d& vec,FXdouble dist){
81 FXVec3d nm(normalize(vec));
82 return FXVec4d(nm,-dist);
83 }
84
85
86 // Compute plane equation from vector and point on plane
plane(const FXVec3d & vec,const FXVec3d & p)87 FXVec4d plane(const FXVec3d& vec,const FXVec3d& p){
88 FXVec3d nm(normalize(vec));
89 return FXVec4d(nm,-(nm.x*p.x+nm.y*p.y+nm.z*p.z));
90 }
91
92
93 // Compute plane equation from 4 vector
plane(const FXVec4d & vec)94 FXVec4d plane(const FXVec4d& vec){
95 register FXdouble t=sqrt(vec.x*vec.x+vec.y*vec.y+vec.z*vec.z);
96 return FXVec4d(vec.x/t,vec.y/t,vec.z/t,vec.w/t);
97 }
98
99
100 // Signed distance normalized plane and point
distance(const FXVec3d & p) const101 FXdouble FXVec4d::distance(const FXVec3d& p) const {
102 return x*p.x+y*p.y+z*p.z+w;
103 }
104
105
106 // Return true if edge a-b crosses plane
crosses(const FXVec3d & a,const FXVec3d & b) const107 bool FXVec4d::crosses(const FXVec3d& a,const FXVec3d& b) const {
108 return (distance(a)>=0.0) ^ (distance(b)>=0.0);
109 }
110
111
112 // Vector times matrix
operator *(const FXMat4d & m) const113 FXVec4d FXVec4d::operator*(const FXMat4d& m) const {
114 return FXVec4d(x*m[0][0]+y*m[1][0]+z*m[2][0]+w*m[3][0], x*m[0][1]+y*m[1][1]+z*m[2][1]+w*m[3][1], x*m[0][2]+y*m[1][2]+z*m[2][2]+w*m[3][2], x*m[0][3]+y*m[1][3]+z*m[2][3]+w*m[3][3]);
115 }
116
117
118 // Save vector to stream
operator <<(FXStream & store,const FXVec4d & v)119 FXStream& operator<<(FXStream& store,const FXVec4d& v){
120 store << v.x << v.y << v.z << v.w;
121 return store;
122 }
123
124
125 // Load vector from stream
operator >>(FXStream & store,FXVec4d & v)126 FXStream& operator>>(FXStream& store,FXVec4d& v){
127 store >> v.x >> v.y >> v.z >> v.w;
128 return store;
129 }
130
131 }
132