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,2005 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.9.2.1 2005/11/15 02:04:40 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 
34 using namespace FX;
35 
36 /*******************************************************************************/
37 
38 namespace FX {
39 
FXVec4d(FXColor color)40 FXVec4d::FXVec4d(FXColor color){
41   x=0.003921568627*FXREDVAL(color);
42   y=0.003921568627*FXGREENVAL(color);
43   z=0.003921568627*FXBLUEVAL(color);
44   w=0.003921568627*FXALPHAVAL(color);
45   }
46 
47 
operator =(FXColor color)48 FXVec4d& FXVec4d::operator=(FXColor color){
49   x=0.003921568627*FXREDVAL(color);
50   y=0.003921568627*FXGREENVAL(color);
51   z=0.003921568627*FXBLUEVAL(color);
52   w=0.003921568627*FXALPHAVAL(color);
53   return *this;
54   }
55 
56 
operator FXColor() const57 FXVec4d::operator FXColor() const {
58   return FXRGBA((x*255.0),(y*255.0),(z*255.0),(w*255.0));
59   }
60 
61 
normalize(const FXVec4d & a)62 FXVec4d normalize(const FXVec4d& a){
63   register FXdouble t=len(a);
64   if(t>0.0){ return FXVec4d(a.x/t,a.y/t,a.z/t,a.w/t); }
65   return FXVec4d(0.0,0.0,0.0,0.0);
66   }
67 
68 
69 // Compute plane equation from 3 points a,b,c
plane(const FXVec3d & a,const FXVec3d & b,const FXVec3d & c)70 FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c){
71   FXVec3d nm(normal(a,b,c));
72   return FXVec4d(nm,-(nm.x*a.x+nm.y*a.y+nm.z*a.z));
73   }
74 
75 
76 // Compute plane equation from vector and distance
plane(const FXVec3d & vec,FXdouble dist)77 FXVec4d plane(const FXVec3d& vec,FXdouble dist){
78   FXVec3d nm(normalize(vec));
79   return FXVec4d(nm,-dist);
80   }
81 
82 
83 // Compute plane equation from vector and point on plane
plane(const FXVec3d & vec,const FXVec3d & p)84 FXVec4d plane(const FXVec3d& vec,const FXVec3d& p){
85   FXVec3d nm(normalize(vec));
86   return FXVec4d(nm,-(nm.x*p.x+nm.y*p.y+nm.z*p.z));
87   }
88 
89 
90 // Compute plane equation from 4 vector
plane(const FXVec4d & vec)91 FXVec4d plane(const FXVec4d& vec){
92   register FXdouble t=sqrt(vec.x*vec.x+vec.y*vec.y+vec.z*vec.z);
93   return FXVec4d(vec.x/t,vec.y/t,vec.z/t,vec.w/t);
94   }
95 
96 
97 // Signed distance normalized plane and point
distance(const FXVec4d & plane,const FXVec3d & p)98 FXdouble distance(const FXVec4d& plane,const FXVec3d& p){
99   return plane.x*p.x+plane.y*p.y+plane.z*p.z+plane.w;
100   }
101 
102 
103 // Return true if edge a-b crosses plane
crosses(const FXVec4d & plane,const FXVec3d & a,const FXVec3d & b)104 FXbool crosses(const FXVec4d& plane,const FXVec3d& a,const FXVec3d& b){
105   return (distance(plane,a)>=0.0) ^ (distance(plane,b)>=0.0);
106   }
107 
108 
109 // Save vector to stream
operator <<(FXStream & store,const FXVec4d & v)110 FXStream& operator<<(FXStream& store,const FXVec4d& v){
111   store << v.x << v.y << v.z << v.w;
112   return store;
113   }
114 
115 
116 // Load vector from stream
operator >>(FXStream & store,FXVec4d & v)117 FXStream& operator>>(FXStream& store,FXVec4d& v){
118   store >> v.x >> v.y >> v.z >> v.w;
119   return store;
120   }
121 
122 }
123