00001 // OMICRON ENGINE HEADER FILE 00002 // 00003 // -------------------------------------------------------------------------- 00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de> 00005 // 00006 // This file is part of the Omicron Engine. 00007 // 00008 // The Omicron Engine is free software; you can redistribute it and/or modify 00009 // it under the terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 2 of the License, or (at your 00011 // option) any later version. 00012 // 00013 // The Omicron Engine is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00015 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00016 // for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License along 00019 // with The Omicron Engine; if not, write to the Free Software Foundation, 00020 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 // 00022 // -------------------------------------------------------------------------- 00023 // Last modified: $Date: 2002/12/07 19:02:06 $ 00024 // By : $Author: kolrabi $ 00025 // $Id: m_plane.cpp,v 1.1.1.1 2002/12/07 19:02:06 kolrabi Exp $ 00026 00027 /* 00028 00029 $Log: m_plane.cpp,v $ 00030 Revision 1.1.1.1 2002/12/07 19:02:06 kolrabi 00031 initial release 00032 00033 00034 */ 00035 00040 #include "omicron/internal.h" 00041 #include <math.h> 00042 00043 /**************************************************************************** 00044 **************************************************************************** 00045 * PLANE ROUTINES *********************************************************** 00046 **************************************************************************** 00047 ****************************************************************************/ 00048 00049 /**************************************************************************** 00050 * plane_frompoints create plane from points * 00051 ****************************************************************************/ 00052 plane_t plane_frompoints 00053 ( 00054 const vec3_t a, 00055 const vec3_t b, 00056 const vec3_t c 00057 ) 00058 { 00059 // need optimization ;) 00060 plane_t p; 00061 vec3_c ta,tb, tmp; 00062 00063 p.d = 0; 00064 vec3_c(0,0,0).copy(p.n); 00065 00066 AssertReturnValue3(a,b,c, p); 00067 00068 ta = vec3_c(b) - a; 00069 tb = vec3_c(c) - a; 00070 00071 tmp = ta.cross(tb); 00072 tmp.normalize().copy(p.n); 00073 00074 p.d = -tmp.dot(a); 00075 00076 return p; 00077 } 00078 00079 /**************************************************************************** 00080 * plane_line_intersect find intersection point * 00081 ****************************************************************************/ 00082 float plane_line_intersect 00083 ( 00084 const vec3_t a, 00085 const vec3_t b, 00086 const plane_t *p, 00087 vec3_t out 00088 ) 00089 { 00090 AssertReturnValue4(a,b,p,out,0); 00091 00092 float aDot = vec3_c(a).dot(p->n); 00093 float bDot = vec3_c(b).dot(p->n); 00094 float scale = ( p->d - aDot ) / ( bDot - aDot ); 00095 00096 vec3_c d; 00097 00098 if (bDot == aDot) 00099 return 0; 00100 00101 d = (vec3_c(b) - a) * scale + a; 00102 d.copy(out); 00103 00104 return scale; 00105 } 00106 00107 00108 /**************************************************************************** 00109 * plane_pointdist distance of vector and plane * 00110 ****************************************************************************/ 00111 float plane_pointdist 00112 ( 00113 const vec3_t v, 00114 const plane_t *p 00115 ) 00116 { 00117 AssertReturnValue1(v, 0); 00118 AssertReturnValue1(p, 0); 00119 00120 return vec3_c(p->n).dot(v)-p->d; 00121 } 00122 00123 /**************************************************************************** 00124 * plane_pointside vec's side of plane * 00125 ****************************************************************************/ 00126 int plane_pointside 00127 ( 00128 const vec3_t v, 00129 const plane_t *p 00130 ) 00131 { 00132 AssertReturnValue1(v, 0); 00133 AssertReturnValue1(p, 0); 00134 00135 return (int)_sgn(vec3_c(p->n).dot(v)-p->d); 00136 } 00137 00138 /**************************************************************************** 00139 * plane_clipbox intersection of a box and plane * 00140 ****************************************************************************/ 00141 int plane_clip_box 00142 ( 00143 const plane_t *p, 00144 const bbox_t *bbox 00145 ) 00146 { 00147 int k; 00148 int d; 00149 bool back = false; 00150 bool front = false; 00151 00152 AssertReturnValue2(p, bbox, 0); 00153 00154 for (k=0; k<8; k++) 00155 { 00156 vec3_c pt; 00157 00158 pt.set( bbox->size[(k>>0)&1][0], 00159 bbox->size[(k>>1)&1][1], 00160 bbox->size[(k>>2)&1][2] ); 00161 00162 d = plane_pointside(pt, p); 00163 00164 if (d>0) 00165 front = true; 00166 else if (d<0) 00167 back = true; 00168 00169 if (front && back) 00170 return 0; 00171 } 00172 00173 if (front && !back) 00174 return 1; 00175 00176 return -1; 00177 00178 // 1 total in front 00179 // 0 partially in front 00180 // -1 all behind 00181 }
1.2.18