- Timestamp:
- 09/08/08 00:52:31 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
r2911 r2913 1153 1153 1154 1154 1155 Plane3 AxisAlignedBox3::GetPlane( constint face) const1155 Plane3 AxisAlignedBox3::GetPlane(int face) const 1156 1156 { 1157 1157 switch (face) 1158 1158 { 1159 1160 1159 case 0: 1160 return Plane3(Vector3(0, -1, 0), mMin); 1161 case 1: 1162 return Plane3(Vector3(0, 1, 0), mMax); 1163 case 2: 1161 1164 return Plane3(Vector3(-1, 0, 0), mMin); 1162 case 1:1165 case 3: 1163 1166 return Plane3(Vector3(1, 0, 0), mMax); 1164 case 2:1165 return Plane3(Vector3(0, -1, 0), mMin);1166 case 3:1167 return Plane3(Vector3(0, 1, 0), mMax);1168 1167 case 4: 1169 1168 return Plane3(Vector3(0, 0, -1), mMin); … … 1176 1175 } 1177 1176 1177 1178 /*int AxisAlignedBox3::Side(const Plane3 &plane) const 1179 { 1180 Vector3 v; 1181 int i, m=3, M=-3, s; 1182 1183 for (i=0;i<8;i++) 1184 { 1185 GetVertex(i, v); 1186 1187 if((s = plane.Side(v)) < m) m=s; 1188 if(s > M) M=s; 1189 if (m && m==-M) return 0; 1190 } 1191 1192 return (m == M) ? m : m + M; 1193 } 1194 */ 1178 1195 1179 1196 int AxisAlignedBox3::Side(const Plane3 &plane) const … … 1203 1220 1204 1221 1205 Polyhedron *AxisAlignedBox3::CalcIntersection(Polyhedron *polyhedron) const 1206 { 1207 Polyhedron *oldPolyhedron = new Polyhedron(*polyhedron); 1208 1222 Polyhedron *AxisAlignedBox3::CalcIntersection(const Polyhedron &polyhedron) const 1223 { 1224 Polyhedron *oldPolyhedron = new Polyhedron(polyhedron); 1225 1226 if (!oldPolyhedron->Valid()) cerr << "******************* not valid!! ************* " << endl; 1227 1209 1228 Polyhedron *newPolyhedron = NULL; 1210 1229 1211 1230 for (int i = 0; i < 6; ++ i) 1212 1231 { 1213 Polyhedron *newPolyhedron = oldPolyhedron->CalcIntersection(GetPlane(i)); 1232 newPolyhedron = oldPolyhedron->CalcIntersection(GetPlane(i)); 1233 1234 if (!newPolyhedron || !newPolyhedron->Valid()) 1235 { 1236 DEL_PTR(newPolyhedron); 1237 cerr << "polyhedron not valid or NULL!" << endl; 1238 1239 return NULL; 1240 } 1241 1214 1242 DEL_PTR(oldPolyhedron); 1215 1216 if (!newPolyhedron) 1217 return NULL; 1243 oldPolyhedron = newPolyhedron; 1218 1244 } 1219 1245 … … 1222 1248 1223 1249 1224 } 1225 1250 1251 bool AxisAlignedBox3::Intersects(const SimpleRay &ray, float &tnear, float &tfar) const 1252 { 1253 tnear = -1e20f; 1254 tfar = 1e20f; 1255 1256 const Vector3 origin = ray.mOrigin; 1257 const Vector3 dir = ray.mDirection; 1258 1259 float t1, t2; 1260 1261 // ray is parallel to the planes 1262 if (dir.x == 0) 1263 { 1264 if ((origin.x < mMin.x) || (origin.x > mMax.x)) 1265 return false; // origin not between planes 1266 } 1267 else 1268 { 1269 // time at which ray intersects minimum X plane 1270 t1 = (mMin.x - origin.x) / dir.x; 1271 // time at which ray intersects maximum X plane 1272 t2 = (mMax.x - origin.x) / dir.x; 1273 1274 if (t1 > t2) 1275 swap(t1, t2); 1276 1277 if (t1 > tnear) 1278 tnear = t1; 1279 1280 if (t2 < tfar) 1281 tfar = t2; 1282 1283 if (tnear > tfar) 1284 return false; 1285 1286 if (tfar < 0) 1287 return false; 1288 } 1289 1290 if (dir.y == 0) // ray is parallel to the planes 1291 { 1292 if ((origin.y < mMin.y) || (origin.y > mMax.y)) 1293 return false; // origin not between planes) 1294 } 1295 else 1296 { 1297 t1 = (mMin.y - origin.y) / dir.y; 1298 t2 = (mMax.y - origin.y) / dir.y; 1299 1300 if (t1 > t2) 1301 swap(t1, t2); 1302 1303 if (t1 > tnear) 1304 tnear = t1; 1305 1306 if (t2 < tfar) 1307 tfar = t2; 1308 1309 if (tnear > tfar) 1310 return false; 1311 1312 if (tfar < 0) 1313 return false; 1314 } 1315 1316 if (dir.z == 0) // ray is parallel to the planes 1317 { 1318 if ((origin.z < mMin.z) || (origin.z > mMax.z)) 1319 return false; // origin not between planes) 1320 } 1321 else 1322 { 1323 t1 = (mMin.z - origin.z) / dir.z; 1324 t2 = (mMax.z - origin.z) / dir.z; 1325 1326 if (t1 > t2) 1327 swap(t1, t2); 1328 1329 if (t1 > tnear) 1330 tnear = t1; 1331 1332 if (t2 < tfar) 1333 tfar = t2; 1334 1335 if (tnear > tfar) 1336 return false; 1337 1338 if (tfar < 0) 1339 return false; 1340 } 1341 1342 return true; 1343 } 1344 1345 1346 } 1347
Note: See TracChangeset
for help on using the changeset viewer.