| 1 | // Created on: 2014-11-21 |
| 2 | // Created by: Varvara POSKONINA |
| 3 | // Copyright (c) 2005-2014 OPEN CASCADE SAS |
| 4 | // |
| 5 | // This file is part of Open CASCADE Technology software library. |
| 6 | // |
| 7 | // This library is free software; you can redistribute it and/or modify it under |
| 8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
| 9 | // by the Free Software Foundation, with special exception defined in the file |
| 10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
| 11 | // distribution for complete text of the license and disclaimer of any warranty. |
| 12 | // |
| 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
| 14 | // commercial license or contractual agreement. |
| 15 | |
| 16 | #include <SelectMgr_TriangularFrustum.hxx> |
| 17 | |
| 18 | #define DOT(A, B) (A.x() * B.x() + A.y() * B.y() + A.z() * B.z()) |
| 19 | #define DOTp(A, B) (A.x() * B.X() + A.y() * B.Y() + A.z() * B.Z()) |
| 20 | #define LENGTH(A) (std::sqrt (A.x() * A.x() + A.y() * A.y() + A.z() * A.z())) |
| 21 | |
| 22 | SelectMgr_TriangularFrustum::~SelectMgr_TriangularFrustum() |
| 23 | { |
| 24 | Clear(); |
| 25 | } |
| 26 | |
| 27 | //======================================================================= |
| 28 | // function : SelectMgr_TriangularFrustum |
| 29 | // purpose : Creates new triangular frustum with bases of triangles with |
| 30 | // vertices theP1, theP2 and theP3 projections onto near and |
| 31 | // far view frustum planes |
| 32 | //======================================================================= |
| 33 | void SelectMgr_TriangularFrustum::Build (const gp_Pnt2d& theP1, |
| 34 | const gp_Pnt2d& theP2, |
| 35 | const gp_Pnt2d& theP3) |
| 36 | { |
| 37 | // V0_Near |
| 38 | myVertices[0] = myBuilder->ProjectPntOnViewPlane (theP1.X(), theP1.Y(), 0.0); |
| 39 | // V1_Near |
| 40 | myVertices[1] = myBuilder->ProjectPntOnViewPlane (theP2.X(), theP2.Y(), 0.0); |
| 41 | // V2_Near |
| 42 | myVertices[2] = myBuilder->ProjectPntOnViewPlane (theP3.X(), theP3.Y(), 0.0); |
| 43 | // V0_Far |
| 44 | myVertices[3] = myBuilder->ProjectPntOnViewPlane (theP1.X(), theP1.Y(), 1.0); |
| 45 | // V1_Far |
| 46 | myVertices[4] = myBuilder->ProjectPntOnViewPlane (theP2.X(), theP2.Y(), 1.0); |
| 47 | // V2_Far |
| 48 | myVertices[5] = myBuilder->ProjectPntOnViewPlane (theP3.X(), theP3.Y(), 1.0); |
| 49 | |
| 50 | // V0V1 |
| 51 | myPlanes[0] = myBuilder->PlaneEquation (myVertices[0], |
| 52 | myVertices[3], |
| 53 | myVertices[4], |
| 54 | myVertices[1]); |
| 55 | // V1V2 |
| 56 | myPlanes[1] = myBuilder->PlaneEquation (myVertices[1], |
| 57 | myVertices[4], |
| 58 | myVertices[5], |
| 59 | myVertices[2]); |
| 60 | // V0V2 |
| 61 | myPlanes[2] = myBuilder->PlaneEquation (myVertices[0], |
| 62 | myVertices[3], |
| 63 | myVertices[5], |
| 64 | myVertices[2]); |
| 65 | // Near |
| 66 | myPlanes[3] = myBuilder->PlaneEquation (myVertices[0], |
| 67 | myVertices[1], |
| 68 | myVertices[2]); |
| 69 | // Far |
| 70 | myPlanes[4] = myBuilder->PlaneEquation (myVertices[3], |
| 71 | myVertices[4], |
| 72 | myVertices[5]); |
| 73 | |
| 74 | for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; ++aPlaneIdx) |
| 75 | { |
| 76 | Standard_Real aMax = -DBL_MAX; |
| 77 | Standard_Real aMin = DBL_MAX; |
| 78 | const SelectMgr_Vec3 aPlane = myPlanes[aPlaneIdx]; |
| 79 | for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx) |
| 80 | { |
| 81 | Standard_Real aProjection = DOT (aPlane, myVertices[aVertIdx]); |
| 82 | aMax = Max (aMax, aProjection); |
| 83 | aMin = Min (aMin, aProjection); |
| 84 | } |
| 85 | myMaxVertsProjections[aPlaneIdx] = aMax; |
| 86 | myMinVertsProjections[aPlaneIdx] = aMin; |
| 87 | } |
| 88 | |
| 89 | SelectMgr_Vec3 aDimensions[3] = |
| 90 | { |
| 91 | SelectMgr_Vec3 (1.0, 0.0, 0.0), |
| 92 | SelectMgr_Vec3 (0.0, 1.0, 0.0), |
| 93 | SelectMgr_Vec3 (0.0, 0.0, 1.0) |
| 94 | }; |
| 95 | |
| 96 | for (Standard_Integer aDim = 0; aDim < 3; ++aDim) |
| 97 | { |
| 98 | Standard_Real aMax = -DBL_MAX; |
| 99 | Standard_Real aMin = DBL_MAX; |
| 100 | for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx) |
| 101 | { |
| 102 | Standard_Real aProjection = DOT (aDimensions[aDim], myVertices[aVertIdx]); |
| 103 | aMax = Max (aMax, aProjection); |
| 104 | aMin = Min (aMin, aProjection); |
| 105 | } |
| 106 | myMaxOrthoVertsProjections[aDim] = aMax; |
| 107 | myMinOrthoVertsProjections[aDim] = aMin; |
| 108 | } |
| 109 | |
| 110 | // V0_Near - V0_Far |
| 111 | myEdgeDirs[0] = myVertices[0] - myVertices[3]; |
| 112 | // V1_Near - V1_Far |
| 113 | myEdgeDirs[1] = myVertices[1] - myVertices[4]; |
| 114 | // V2_Near - V1_Far |
| 115 | myEdgeDirs[2] = myVertices[2] - myVertices[5]; |
| 116 | // V1_Near - V0_Near |
| 117 | myEdgeDirs[3] = myVertices[1] - myVertices[0]; |
| 118 | // V2_Near - V1_Near |
| 119 | myEdgeDirs[4] = myVertices[2] - myVertices[1]; |
| 120 | // V1_Near - V0_Near |
| 121 | myEdgeDirs[5] = myVertices[2] - myVertices[0]; |
| 122 | } |
| 123 | |
| 124 | //======================================================================= |
| 125 | // function : Transform |
| 126 | // purpose : Returns a copy of the frustum transformed according to the matrix given |
| 127 | //======================================================================= |
| 128 | NCollection_Handle<SelectMgr_BaseFrustum> SelectMgr_TriangularFrustum::Transform (const gp_Trsf& theTrsf) |
| 129 | { |
| 130 | SelectMgr_TriangularFrustum* aRes = new SelectMgr_TriangularFrustum(); |
| 131 | |
| 132 | // V0_Near |
| 133 | aRes->myVertices[0] = SelectMgr_MatOp::Transform (theTrsf, myVertices[0]); |
| 134 | // V1_Near |
| 135 | aRes->myVertices[1] = SelectMgr_MatOp::Transform (theTrsf, myVertices[1]); |
| 136 | // V2_Near |
| 137 | aRes->myVertices[2] = SelectMgr_MatOp::Transform (theTrsf, myVertices[2]); |
| 138 | // V0_Far |
| 139 | aRes->myVertices[3] = SelectMgr_MatOp::Transform (theTrsf, myVertices[3]); |
| 140 | // V1_Far |
| 141 | aRes->myVertices[4] = SelectMgr_MatOp::Transform (theTrsf, myVertices[4]); |
| 142 | // V2_Far |
| 143 | aRes->myVertices[5] = SelectMgr_MatOp::Transform (theTrsf, myVertices[5]); |
| 144 | |
| 145 | aRes->myIsOrthographic = myIsOrthographic; |
| 146 | |
| 147 | // V0V1 |
| 148 | aRes->myPlanes[0] = myBuilder->PlaneEquation (aRes->myVertices[0], |
| 149 | aRes->myVertices[3], |
| 150 | aRes->myVertices[4], |
| 151 | aRes->myVertices[1]); |
| 152 | // V1V2 |
| 153 | aRes->myPlanes[1] = myBuilder->PlaneEquation (aRes->myVertices[1], |
| 154 | aRes->myVertices[4], |
| 155 | aRes->myVertices[5], |
| 156 | aRes->myVertices[2]); |
| 157 | // V0V2 |
| 158 | aRes->myPlanes[2] = myBuilder->PlaneEquation (aRes->myVertices[0], |
| 159 | aRes->myVertices[3], |
| 160 | aRes->myVertices[5], |
| 161 | aRes->myVertices[2]); |
| 162 | // Near |
| 163 | aRes->myPlanes[3] = myBuilder->PlaneEquation (aRes->myVertices[0], |
| 164 | aRes->myVertices[1], |
| 165 | aRes->myVertices[2]); |
| 166 | // Far |
| 167 | aRes->myPlanes[4] = myBuilder->PlaneEquation (aRes->myVertices[3], |
| 168 | aRes->myVertices[4], |
| 169 | aRes->myVertices[5]); |
| 170 | |
| 171 | for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; ++aPlaneIdx) |
| 172 | { |
| 173 | Standard_Real aMax = -DBL_MAX; |
| 174 | Standard_Real aMin = DBL_MAX; |
| 175 | const SelectMgr_Vec3 aPlane = aRes->myPlanes[aPlaneIdx]; |
| 176 | for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx) |
| 177 | { |
| 178 | Standard_Real aProjection = DOT (aPlane, aRes->myVertices[aVertIdx]); |
| 179 | aMax = Max (aMax, aProjection); |
| 180 | aMin = Min (aMin, aProjection); |
| 181 | } |
| 182 | aRes->myMaxVertsProjections[aPlaneIdx] = aMax; |
| 183 | aRes->myMinVertsProjections[aPlaneIdx] = aMin; |
| 184 | } |
| 185 | |
| 186 | SelectMgr_Vec3 aDimensions[3] = |
| 187 | { |
| 188 | SelectMgr_Vec3 (1.0, 0.0, 0.0), |
| 189 | SelectMgr_Vec3 (0.0, 1.0, 0.0), |
| 190 | SelectMgr_Vec3 (0.0, 0.0, 1.0) |
| 191 | }; |
| 192 | |
| 193 | for (Standard_Integer aDim = 0; aDim < 3; ++aDim) |
| 194 | { |
| 195 | Standard_Real aMax = -DBL_MAX; |
| 196 | Standard_Real aMin = DBL_MAX; |
| 197 | for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx) |
| 198 | { |
| 199 | Standard_Real aProjection = DOT (aDimensions[aDim], aRes->myVertices[aVertIdx]); |
| 200 | aMax = Max (aMax, aProjection); |
| 201 | aMin = Min (aMin, aProjection); |
| 202 | } |
| 203 | aRes->myMaxOrthoVertsProjections[aDim] = aMax; |
| 204 | aRes->myMinOrthoVertsProjections[aDim] = aMin; |
| 205 | } |
| 206 | |
| 207 | // V0_Near - V0_Far |
| 208 | aRes->myEdgeDirs[0] = aRes->myVertices[0] - aRes->myVertices[3]; |
| 209 | // V1_Near - V1_Far |
| 210 | aRes->myEdgeDirs[1] = aRes->myVertices[1] - aRes->myVertices[4]; |
| 211 | // V2_Near - V1_Far |
| 212 | aRes->myEdgeDirs[2] = aRes->myVertices[2] - aRes->myVertices[5]; |
| 213 | // V1_Near - V0_Near |
| 214 | aRes->myEdgeDirs[3] = aRes->myVertices[1] - aRes->myVertices[0]; |
| 215 | // V2_Near - V1_Near |
| 216 | aRes->myEdgeDirs[4] = aRes->myVertices[2] - aRes->myVertices[1]; |
| 217 | // V1_Near - V0_Near |
| 218 | aRes->myEdgeDirs[5] = aRes->myVertices[2] - aRes->myVertices[0]; |
| 219 | |
| 220 | return NCollection_Handle<SelectMgr_BaseFrustum> (aRes); |
| 221 | } |
| 222 | |
| 223 | //======================================================================= |
| 224 | // function : Overlaps |
| 225 | // purpose : SAT intersection test between defined volume and |
| 226 | // given axis-aligned box |
| 227 | //======================================================================= |
| 228 | const Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const BVH_Box<Standard_Real, 3>& theBox, |
| 229 | Standard_Real& /*theDepth*/) |
| 230 | { |
| 231 | return hasOverlap (theBox.CornerMin(), theBox.CornerMax()); |
| 232 | } |
| 233 | |
| 234 | // ======================================================================= |
| 235 | // function : Overlaps |
| 236 | // purpose : Returns true if selecting volume is overlapped by |
| 237 | // axis-aligned bounding box with minimum corner at point |
| 238 | // theMinPt and maximum at point theMaxPt |
| 239 | // ======================================================================= |
| 240 | const Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const SelectMgr_Vec3& theMinPt, |
| 241 | const SelectMgr_Vec3& theMaxPt) |
| 242 | { |
| 243 | return hasOverlap (theMinPt, theMaxPt); |
| 244 | } |
| 245 | |
| 246 | // ======================================================================= |
| 247 | // function : Overlaps |
| 248 | // purpose : Intersection test between defined volume and given point |
| 249 | // ======================================================================= |
| 250 | const Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const gp_Pnt& thePnt, |
| 251 | Standard_Real& /*theDepth*/) |
| 252 | { |
| 253 | return hasOverlap (thePnt); |
| 254 | } |
| 255 | |
| 256 | // ======================================================================= |
| 257 | // function : Overlaps |
| 258 | // purpose : SAT intersection test between defined volume and given |
| 259 | // ordered set of points, representing line segments. The test |
| 260 | // may be considered of interior part or boundary line defined |
| 261 | // by segments depending on given sensitivity type |
| 262 | // ======================================================================= |
| 263 | const Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const Handle(TColgp_HArray1OfPnt)& theArrayOfPnts, |
| 264 | Select3D_TypeOfSensitivity theSensType, |
| 265 | Standard_Real& /*theDepth*/) |
| 266 | { |
| 267 | if (theSensType == Select3D_TOS_BOUNDARY) |
| 268 | { |
| 269 | Standard_Integer aLower = theArrayOfPnts->Lower(); |
| 270 | Standard_Integer anUpper = theArrayOfPnts->Upper(); |
| 271 | |
| 272 | for (Standard_Integer aPtIdx = aLower; aPtIdx <= anUpper; ++aPtIdx) |
| 273 | { |
| 274 | const gp_Pnt& aStartPt = theArrayOfPnts->Value (aPtIdx); |
| 275 | const gp_Pnt& aEndPt = aPtIdx == anUpper ? theArrayOfPnts->Value (aLower) : theArrayOfPnts->Value (aPtIdx + 1); |
| 276 | |
| 277 | if (!hasOverlap (aStartPt, aEndPt)) |
| 278 | { |
| 279 | return Standard_False; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | else if (theSensType == Select3D_TOS_INTERIOR) |
| 284 | { |
| 285 | SelectMgr_Vec3 aNorm (RealLast()); |
| 286 | return hasOverlap (theArrayOfPnts, aNorm); |
| 287 | } |
| 288 | |
| 289 | return Standard_False; |
| 290 | } |
| 291 | |
| 292 | // ======================================================================= |
| 293 | // function : Overlaps |
| 294 | // purpose : Checks if line segment overlaps selecting frustum |
| 295 | // ======================================================================= |
| 296 | const Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const gp_Pnt& thePnt1, |
| 297 | const gp_Pnt& thePnt2, |
| 298 | Standard_Real& /*theDepth*/) |
| 299 | { |
| 300 | return hasOverlap (thePnt1, thePnt2); |
| 301 | } |
| 302 | |
| 303 | // ======================================================================= |
| 304 | // function : Overlaps |
| 305 | // purpose : SAT intersection test between defined volume and given |
| 306 | // triangle. The test may be considered of interior part or |
| 307 | // boundary line defined by triangle vertices depending on |
| 308 | // given sensitivity type |
| 309 | // ======================================================================= |
| 310 | const Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const gp_Pnt& thePnt1, |
| 311 | const gp_Pnt& thePnt2, |
| 312 | const gp_Pnt& thePnt3, |
| 313 | Select3D_TypeOfSensitivity theSensType, |
| 314 | Standard_Real& theDepth) |
| 315 | { |
| 316 | if (theSensType == Select3D_TOS_BOUNDARY) |
| 317 | { |
| 318 | Handle(TColgp_HArray1OfPnt) aPtsArray = new TColgp_HArray1OfPnt(1, 4); |
| 319 | aPtsArray->SetValue (1, thePnt1); |
| 320 | aPtsArray->SetValue (2, thePnt2); |
| 321 | aPtsArray->SetValue (3, thePnt3); |
| 322 | return Overlaps (aPtsArray, Select3D_TOS_BOUNDARY, theDepth); |
| 323 | } |
| 324 | else if (theSensType == Select3D_TOS_INTERIOR) |
| 325 | { |
| 326 | SelectMgr_Vec3 aNorm (RealLast()); |
| 327 | return hasOverlap (thePnt1, thePnt2, thePnt3, aNorm); |
| 328 | } |
| 329 | |
| 330 | return Standard_True; |
| 331 | } |
| 332 | |
| 333 | // ======================================================================= |
| 334 | // function : Clear |
| 335 | // purpose : Nullifies the handle for corresponding builder instance to prevent |
| 336 | // memory leaks |
| 337 | // ======================================================================= |
| 338 | void SelectMgr_TriangularFrustum::Clear() |
| 339 | { |
| 340 | myBuilder.Nullify(); |
| 341 | } |